155682Smarkm/*
2233294Sstas * Copyright (c) 1997-2007 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
755682Smarkm *
8233294Sstas * Redistribution and use in source and binary forms, with or without
9233294Sstas * modification, are permitted provided that the following conditions
10233294Sstas * are met:
1155682Smarkm *
12233294Sstas * 1. Redistributions of source code must retain the above copyright
13233294Sstas *    notice, this list of conditions and the following disclaimer.
1455682Smarkm *
15233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer in the
17233294Sstas *    documentation and/or other materials provided with the distribution.
1855682Smarkm *
19233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
20233294Sstas *    may be used to endorse or promote products derived from this software
21233294Sstas *    without specific prior written permission.
2255682Smarkm *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233294Sstas * SUCH DAMAGE.
3455682Smarkm */
3555682Smarkm
3655682Smarkm#include "kdc_locl.h"
3755682Smarkm#include <getarg.h>
3855682Smarkm#include <parse_bytes.h>
3955682Smarkm
40178825Sdfrstruct dbinfo {
41178825Sdfr    char *realm;
42178825Sdfr    char *dbname;
43178825Sdfr    char *mkey_file;
44178825Sdfr    struct dbinfo *next;
45178825Sdfr};
4657419Smarkm
47178825Sdfrstatic char *config_file;	/* location of kdc config file */
4857419Smarkm
49178825Sdfrstatic int require_preauth = -1; /* 1 == require preauth for all principals */
5057419Smarkmstatic char *max_request_str;	/* `max_request' as a string */
5157419Smarkm
52178825Sdfrstatic int disable_des = -1;
5357419Smarkm
54178825Sdfrstatic int builtin_hdb_flag;
55178825Sdfrstatic int help_flag;
56178825Sdfrstatic int version_flag;
5757419Smarkm
5857419Smarkmstatic struct getarg_strings addresses_str;	/* addresses to listen on */
5957419Smarkm
60233294Sstaschar *runas_string;
61233294Sstaschar *chroot_string;
6255682Smarkm
63233294Sstas
6455682Smarkmstatic struct getargs args[] = {
65233294Sstas    {
66233294Sstas	"config-file",	'c',	arg_string,	&config_file,
67233294Sstas	"location of config file",	"file"
6855682Smarkm    },
69233294Sstas    {
70233294Sstas	"require-preauth",	'p',	arg_negative_flag, &require_preauth,
71233294Sstas	"don't require pa-data in as-reqs", NULL
7255682Smarkm    },
73233294Sstas    {
74233294Sstas	"max-request",	0,	arg_string, &max_request_str,
7555682Smarkm	"max size for a kdc-request", "size"
7655682Smarkm    },
77233294Sstas    { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support",
78233294Sstas   	 NULL },
79233294Sstas    {	"ports",	'P', 	arg_string, rk_UNCONST(&port_str),
8090926Snectar	"ports to listen to", "portspec"
8155682Smarkm    },
82233294Sstas#ifdef SUPPORT_DETACH
83102644Snectar#if DETACH_IS_DEFAULT
84102644Snectar    {
85233294Sstas	"detach",       'D',      arg_negative_flag, &detach_from_console,
86233294Sstas	"don't detach from console", NULL
87102644Snectar    },
88102644Snectar#else
89102644Snectar    {
90233294Sstas	"detach",       0 ,      arg_flag, &detach_from_console,
91233294Sstas	"detach from console", NULL
92102644Snectar    },
93102644Snectar#endif
94233294Sstas#endif
9557419Smarkm    {	"addresses",	0,	arg_strings, &addresses_str,
9657419Smarkm	"addresses to listen on", "list of addresses" },
97178825Sdfr    {	"disable-des",	0,	arg_flag, &disable_des,
98233294Sstas	"disable DES", NULL },
99178825Sdfr    {	"builtin-hdb",	0,	arg_flag,   &builtin_hdb_flag,
100233294Sstas	"list builtin hdb backends", NULL},
101233294Sstas    {   "runas-user",	0,	arg_string, &runas_string,
102233294Sstas	"run as this user when connected to network", NULL
103233294Sstas    },
104233294Sstas    {   "chroot",	0,	arg_string, &chroot_string,
105233294Sstas	"chroot directory to run in", NULL
106233294Sstas    },
107233294Sstas    {	"help",		'h',	arg_flag,   &help_flag, NULL, NULL },
108233294Sstas    {	"version",	'v',	arg_flag,   &version_flag, NULL, NULL }
10955682Smarkm};
11055682Smarkm
11155682Smarkmstatic int num_args = sizeof(args) / sizeof(args[0]);
11255682Smarkm
11355682Smarkmstatic void
11455682Smarkmusage(int ret)
11555682Smarkm{
11655682Smarkm    arg_printusage (args, num_args, NULL, "");
11755682Smarkm    exit (ret);
11855682Smarkm}
11955682Smarkm
12055682Smarkmstatic void
121178825Sdfradd_one_address (krb5_context context, const char *str, int first)
12255682Smarkm{
12357419Smarkm    krb5_error_code ret;
12457419Smarkm    krb5_addresses tmp;
12557419Smarkm
12657419Smarkm    ret = krb5_parse_address (context, str, &tmp);
12757419Smarkm    if (ret)
12857419Smarkm	krb5_err (context, 1, ret, "parse_address `%s'", str);
12957419Smarkm    if (first)
13057419Smarkm	krb5_copy_addresses(context, &tmp, &explicit_addresses);
13157419Smarkm    else
13257419Smarkm	krb5_append_addresses(context, &explicit_addresses, &tmp);
13357419Smarkm    krb5_free_addresses (context, &tmp);
13457419Smarkm}
13557419Smarkm
136178825Sdfrkrb5_kdc_configuration *
137178825Sdfrconfigure(krb5_context context, int argc, char **argv)
13855682Smarkm{
139178825Sdfr    krb5_kdc_configuration *config;
140178825Sdfr    krb5_error_code ret;
141178825Sdfr    int optidx = 0;
14255682Smarkm    const char *p;
143233294Sstas
144178825Sdfr    while(getarg(args, num_args, argc, argv, &optidx))
145178825Sdfr	warnx("error at argument `%s'", argv[optidx]);
14655682Smarkm
14755682Smarkm    if(help_flag)
14855682Smarkm	usage (0);
14955682Smarkm
15055682Smarkm    if (version_flag) {
15155682Smarkm	print_version(NULL);
15255682Smarkm	exit(0);
15355682Smarkm    }
15455682Smarkm
155178825Sdfr    if (builtin_hdb_flag) {
156178825Sdfr	char *list;
157178825Sdfr	ret = hdb_list_builtin(context, &list);
158178825Sdfr	if (ret)
159178825Sdfr	    krb5_err(context, 1, ret, "listing builtin hdb backends");
160178825Sdfr	printf("builtin hdb backends: %s\n", list);
161178825Sdfr	free(list);
162178825Sdfr	exit(0);
163178825Sdfr    }
16455682Smarkm
165178825Sdfr    argc -= optidx;
166178825Sdfr    argv += optidx;
167178825Sdfr
16855682Smarkm    if (argc != 0)
16955682Smarkm	usage(1);
170233294Sstas
171102644Snectar    {
172102644Snectar	char **files;
173178825Sdfr
174178825Sdfr	if (config_file == NULL) {
175178825Sdfr	    asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
176178825Sdfr	    if (config_file == NULL)
177178825Sdfr		errx(1, "out of memory");
178178825Sdfr	}
179178825Sdfr
180178825Sdfr	ret = krb5_prepend_config_files_default(config_file, &files);
181178825Sdfr	if (ret)
182178825Sdfr	    krb5_err(context, 1, ret, "getting configuration files");
183233294Sstas
184102644Snectar	ret = krb5_set_config_files(context, files);
185102644Snectar	krb5_free_config_files(files);
186233294Sstas	if(ret)
187102644Snectar	    krb5_err(context, 1, ret, "reading configuration files");
188102644Snectar    }
189102644Snectar
190178825Sdfr    ret = krb5_kdc_get_config(context, &config);
191178825Sdfr    if (ret)
192178825Sdfr	krb5_err(context, 1, ret, "krb5_kdc_default_config");
193178825Sdfr
194233294Sstas    kdc_openlog(context, "kdc", config);
195178825Sdfr
196178825Sdfr    ret = krb5_kdc_set_dbinfo(context, config);
197178825Sdfr    if (ret)
198178825Sdfr	krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
199178825Sdfr
200127808Snectar    if(max_request_str)
201233294Sstas	max_request_tcp = max_request_udp = parse_bytes(max_request_str, NULL);
20255682Smarkm
203233294Sstas    if(max_request_tcp == 0){
20455682Smarkm	p = krb5_config_get_string (context,
205102644Snectar				    NULL,
20655682Smarkm				    "kdc",
20755682Smarkm				    "max-request",
20855682Smarkm				    NULL);
20955682Smarkm	if(p)
210233294Sstas	    max_request_tcp = max_request_udp = parse_bytes(p, NULL);
21155682Smarkm    }
212233294Sstas
213178825Sdfr    if(require_preauth != -1)
214178825Sdfr	config->require_preauth = require_preauth;
21555682Smarkm
21655682Smarkm    if(port_str == NULL){
217102644Snectar	p = krb5_config_get_string(context, NULL, "kdc", "ports", NULL);
21855682Smarkm	if (p != NULL)
21955682Smarkm	    port_str = strdup(p);
22055682Smarkm    }
22157419Smarkm
22257419Smarkm    explicit_addresses.len = 0;
22357419Smarkm
22457419Smarkm    if (addresses_str.num_strings) {
22557419Smarkm	int i;
22657419Smarkm
22757419Smarkm	for (i = 0; i < addresses_str.num_strings; ++i)
228178825Sdfr	    add_one_address (context, addresses_str.strings[i], i == 0);
22978527Sassar	free_getarg_strings (&addresses_str);
23057419Smarkm    } else {
231102644Snectar	char **foo = krb5_config_get_strings (context, NULL,
23257419Smarkm					      "kdc", "addresses", NULL);
23357419Smarkm
23457419Smarkm	if (foo != NULL) {
235178825Sdfr	    add_one_address (context, *foo++, TRUE);
23657419Smarkm	    while (*foo)
237178825Sdfr		add_one_address (context, *foo++, FALSE);
23857419Smarkm	}
23957419Smarkm    }
24057419Smarkm
24155682Smarkm    if(enable_http == -1)
242233294Sstas	enable_http = krb5_config_get_bool(context, NULL, "kdc",
24355682Smarkm					   "enable-http", NULL);
24472445Sassar
245178825Sdfr    if(request_log == NULL)
246233294Sstas	request_log = krb5_config_get_string(context, NULL,
247233294Sstas					     "kdc",
248233294Sstas					     "kdc-request-log",
249178825Sdfr					     NULL);
25055682Smarkm
251233294Sstas    if (krb5_config_get_string(context, NULL, "kdc",
252178825Sdfr			       "enforce-transited-policy", NULL))
253178825Sdfr	krb5_errx(context, 1, "enforce-transited-policy deprecated, "
254178825Sdfr		  "use [kdc]transited-policy instead");
25555682Smarkm
256233294Sstas#ifdef SUPPORT_DETACH
257233294Sstas    if(detach_from_console == -1)
258233294Sstas	detach_from_console = krb5_config_get_bool_default(context, NULL,
259102644Snectar							   DETACH_IS_DEFAULT,
260102644Snectar							   "kdc",
261102644Snectar							   "detach", NULL);
262233294Sstas#endif /* SUPPORT_DETACH */
263178825Sdfr
264233294Sstas    if(max_request_tcp == 0)
265233294Sstas	max_request_tcp = 64 * 1024;
266233294Sstas    if(max_request_udp == 0)
267233294Sstas	max_request_udp = 64 * 1024;
268178825Sdfr
26955682Smarkm    if (port_str == NULL)
27055682Smarkm	port_str = "+";
271178825Sdfr
272178825Sdfr    if(disable_des == -1)
273233294Sstas	disable_des = krb5_config_get_bool_default(context, NULL,
274178825Sdfr						   FALSE,
275178825Sdfr						   "kdc",
276178825Sdfr						   "disable-des", NULL);
277178825Sdfr    if(disable_des) {
278178825Sdfr	krb5_enctype_disable(context, ETYPE_DES_CBC_CRC);
279178825Sdfr	krb5_enctype_disable(context, ETYPE_DES_CBC_MD4);
280178825Sdfr	krb5_enctype_disable(context, ETYPE_DES_CBC_MD5);
281178825Sdfr	krb5_enctype_disable(context, ETYPE_DES_CBC_NONE);
282178825Sdfr	krb5_enctype_disable(context, ETYPE_DES_CFB64_NONE);
283178825Sdfr	krb5_enctype_disable(context, ETYPE_DES_PCBC_NONE);
28455682Smarkm    }
285178825Sdfr
286178825Sdfr    krb5_kdc_windc_init(context);
287178825Sdfr
288233294Sstas    krb5_kdc_pkinit_config(context, config);
289233294Sstas
290178825Sdfr    return config;
29155682Smarkm}
292