xref: /freebsd/crypto/heimdal/kdc/config.c (revision 50d8893512e3c7160398bff7e3f9f056e1220fc1)
1 /*
2  * Copyright (c) 1997-2002 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "kdc_locl.h"
35 #include <getarg.h>
36 #include <parse_bytes.h>
37 
38 RCSID("$Id: config.c,v 1.39 2002/02/04 10:53:48 joda Exp $");
39 
40 static char *config_file;	/* location of kdc config file */
41 
42 int require_preauth = -1;	/* 1 == require preauth for all principals */
43 
44 size_t max_request;		/* maximal size of a request */
45 
46 static char *max_request_str;	/* `max_request' as a string */
47 
48 time_t kdc_warn_pwexpire;	/* time before expiration to print a warning */
49 
50 struct dbinfo *databases;
51 HDB **db;
52 int num_db;
53 
54 char *port_str;
55 
56 int enable_http = -1;
57 krb5_boolean encode_as_rep_as_tgs_rep; /* bug compatibility */
58 
59 krb5_boolean check_ticket_addresses;
60 krb5_boolean allow_null_ticket_addresses;
61 krb5_boolean allow_anonymous;
62 
63 static struct getarg_strings addresses_str;	/* addresses to listen on */
64 krb5_addresses explicit_addresses;
65 
66 #ifdef KRB4
67 char *v4_realm;
68 int enable_v4 = -1;
69 int enable_524 = -1;
70 int enable_kaserver = -1;
71 #endif
72 
73 static int help_flag;
74 static int version_flag;
75 
76 static struct getargs args[] = {
77     {
78 	"config-file",	'c',	arg_string,	&config_file,
79 	"location of config file",	"file"
80     },
81     {
82 	"require-preauth",	'p',	arg_negative_flag, &require_preauth,
83 	"don't require pa-data in as-reqs"
84     },
85     {
86 	"max-request",	0,	arg_string, &max_request,
87 	"max size for a kdc-request", "size"
88     },
89 #if 0
90     {
91 	"database",	'd', 	arg_string, &databases,
92 	"location of database", "database"
93     },
94 #endif
95     { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support" },
96 #ifdef KRB4
97     {	"kerberos4",	0, 	arg_negative_flag, &enable_v4,
98 	"don't respond to kerberos 4 requests"
99     },
100     {	"524",		0, 	arg_negative_flag, &enable_524,
101 	"don't respond to 524 requests"
102     },
103     {
104 	"v4-realm",	'r',	arg_string, &v4_realm,
105 	"realm to serve v4-requests for"
106     },
107     {
108 	"kaserver", 'K', arg_flag,   &enable_kaserver,
109 	"enable kaserver support"
110     },
111 #endif
112     {	"ports",	'P', 	arg_string, &port_str,
113 	"ports to listen to", "portspec"
114     },
115     {	"addresses",	0,	arg_strings, &addresses_str,
116 	"addresses to listen on", "list of addresses" },
117     {	"help",		'h',	arg_flag,   &help_flag },
118     {	"version",	'v',	arg_flag,   &version_flag }
119 };
120 
121 static int num_args = sizeof(args) / sizeof(args[0]);
122 
123 static void
124 usage(int ret)
125 {
126     arg_printusage (args, num_args, NULL, "");
127     exit (ret);
128 }
129 
130 static void
131 get_dbinfo(krb5_config_section *cf)
132 {
133     krb5_config_binding *top_binding = NULL;
134     krb5_config_binding *db_binding;
135     krb5_config_binding *default_binding = NULL;
136     struct dbinfo *di, **dt;
137     const char *default_dbname = HDB_DEFAULT_DB;
138     const char *default_mkey = HDB_DB_DIR "/m-key";
139     const char *p;
140 
141     databases = NULL;
142     dt = &databases;
143     while((db_binding = (krb5_config_binding *)
144 	   krb5_config_get_next(context, cf, &top_binding,
145 				krb5_config_list,
146 				"kdc",
147 				"database",
148 				NULL))) {
149 	p = krb5_config_get_string(context, db_binding, "realm", NULL);
150 	if(p == NULL) {
151 	    if(default_binding) {
152 		krb5_warnx(context, "WARNING: more than one realm-less "
153 			   "database specification");
154 		krb5_warnx(context, "WARNING: using the first encountered");
155 	    } else
156 		default_binding = db_binding;
157 	    continue;
158 	}
159 	di = calloc(1, sizeof(*di));
160 	di->realm = strdup(p);
161 	p = krb5_config_get_string(context, db_binding, "dbname", NULL);
162 	if(p)
163 	    di->dbname = strdup(p);
164 	p = krb5_config_get_string(context, db_binding, "mkey_file", NULL);
165 	if(p)
166 	    di->mkey_file = strdup(p);
167 	*dt = di;
168 	dt = &di->next;
169     }
170     if(default_binding) {
171 	di = calloc(1, sizeof(*di));
172 	p = krb5_config_get_string(context, default_binding, "dbname", NULL);
173 	if(p) {
174 	    di->dbname = strdup(p);
175 	    default_dbname = p;
176 	}
177 	p = krb5_config_get_string(context, default_binding, "mkey_file", NULL);
178 	if(p) {
179 	    di->mkey_file = strdup(p);
180 	    default_mkey = p;
181 	}
182 	*dt = di;
183 	dt = &di->next;
184     } else if(databases == NULL) {
185 	/* if there are none specified, use some default */
186 	di = calloc(1, sizeof(*di));
187 	di->dbname = strdup(default_dbname);
188 	di->mkey_file = strdup(default_mkey);
189 	*dt = di;
190 	dt = &di->next;
191     }
192     for(di = databases; di; di = di->next) {
193 	if(di->dbname == NULL)
194 	    di->dbname = strdup(default_dbname);
195 	if(di->mkey_file == NULL) {
196 	    p = strrchr(di->dbname, '.');
197 	    if(p == NULL || strchr(p, '/') != NULL)
198 		/* final pathname component does not contain a . */
199 		asprintf(&di->mkey_file, "%s.mkey", di->dbname);
200 	    else
201 		/* the filename is something.else, replace .else with
202                    .mkey */
203 		asprintf(&di->mkey_file, "%.*s.mkey",
204 			 (int)(p - di->dbname), di->dbname);
205 	}
206     }
207 }
208 
209 static void
210 add_one_address (const char *str, int first)
211 {
212     krb5_error_code ret;
213     krb5_addresses tmp;
214 
215     ret = krb5_parse_address (context, str, &tmp);
216     if (ret)
217 	krb5_err (context, 1, ret, "parse_address `%s'", str);
218     if (first)
219 	krb5_copy_addresses(context, &tmp, &explicit_addresses);
220     else
221 	krb5_append_addresses(context, &explicit_addresses, &tmp);
222     krb5_free_addresses (context, &tmp);
223 }
224 
225 void
226 configure(int argc, char **argv)
227 {
228     krb5_config_section *cf = NULL;
229     int optind = 0;
230     int e;
231     const char *p;
232 
233     while((e = getarg(args, num_args, argc, argv, &optind)))
234 	warnx("error at argument `%s'", argv[optind]);
235 
236     if(help_flag)
237 	usage (0);
238 
239     if (version_flag) {
240 	print_version(NULL);
241 	exit(0);
242     }
243 
244     argc -= optind;
245     argv += optind;
246 
247     if (argc != 0)
248 	usage(1);
249 
250     if(config_file == NULL)
251 	config_file = _PATH_KDC_CONF;
252 
253     if(krb5_config_parse_file(context, config_file, &cf))
254 	cf = NULL;
255 
256     get_dbinfo(cf);
257 
258     if(max_request_str){
259 	max_request = parse_bytes(max_request_str, NULL);
260     }
261 
262     if(max_request == 0){
263 	p = krb5_config_get_string (context,
264 				    cf,
265 				    "kdc",
266 				    "max-request",
267 				    NULL);
268 	if(p)
269 	    max_request = parse_bytes(p, NULL);
270     }
271 
272     if(require_preauth == -1)
273 	require_preauth = krb5_config_get_bool(context, cf, "kdc",
274 					       "require-preauth", NULL);
275 
276     if(port_str == NULL){
277 	p = krb5_config_get_string(context, cf, "kdc", "ports", NULL);
278 	if (p != NULL)
279 	    port_str = strdup(p);
280     }
281 
282     explicit_addresses.len = 0;
283 
284     if (addresses_str.num_strings) {
285 	int i;
286 
287 	for (i = 0; i < addresses_str.num_strings; ++i)
288 	    add_one_address (addresses_str.strings[i], i == 0);
289 	free_getarg_strings (&addresses_str);
290     } else {
291 	char **foo = krb5_config_get_strings (context, cf,
292 					      "kdc", "addresses", NULL);
293 
294 	if (foo != NULL) {
295 	    add_one_address (*foo++, TRUE);
296 	    while (*foo)
297 		add_one_address (*foo++, FALSE);
298 	}
299     }
300 
301 #ifdef KRB4
302     if(enable_v4 == -1)
303 	enable_v4 = krb5_config_get_bool_default(context, cf, TRUE, "kdc",
304 					 "enable-kerberos4", NULL);
305     if(enable_524 == -1)
306 	enable_524 = krb5_config_get_bool_default(context, cf, enable_v4,
307 						  "kdc", "enable-524", NULL);
308 #endif
309 
310     if(enable_http == -1)
311 	enable_http = krb5_config_get_bool(context, cf, "kdc",
312 					   "enable-http", NULL);
313     check_ticket_addresses =
314 	krb5_config_get_bool_default(context, cf, TRUE, "kdc",
315 				     "check-ticket-addresses", NULL);
316     allow_null_ticket_addresses =
317 	krb5_config_get_bool_default(context, cf, TRUE, "kdc",
318 				     "allow-null-ticket-addresses", NULL);
319 
320     allow_anonymous =
321 	krb5_config_get_bool(context, cf, "kdc",
322 			     "allow-anonymous", NULL);
323 #ifdef KRB4
324     if(v4_realm == NULL){
325 	p = krb5_config_get_string (context, cf,
326 				    "kdc",
327 				    "v4-realm",
328 				    NULL);
329 	if(p)
330 	    v4_realm = strdup(p);
331     }
332     if (enable_kaserver == -1)
333 	enable_kaserver = krb5_config_get_bool_default(context, cf, FALSE,
334 						       "kdc",
335 						       "enable-kaserver",
336 						       NULL);
337 #endif
338 
339     encode_as_rep_as_tgs_rep = krb5_config_get_bool(context, cf, "kdc",
340 						    "encode_as_rep_as_tgs_rep",
341 						    NULL);
342 
343     kdc_warn_pwexpire = krb5_config_get_time (context, cf,
344 					      "kdc",
345 					      "kdc_warn_pwexpire",
346 					      NULL);
347     kdc_openlog(cf);
348     if(cf)
349 	krb5_config_file_free (context, cf);
350     if(max_request == 0)
351 	max_request = 64 * 1024;
352     if(require_preauth == -1)
353 	require_preauth = 1;
354     if (port_str == NULL)
355 	port_str = "+";
356 #ifdef KRB4
357     if(v4_realm == NULL){
358 	v4_realm = malloc(40); /* REALM_SZ */
359 	krb_get_lrealm(v4_realm, 1);
360     }
361 #endif
362 }
363