xref: /freebsd/crypto/heimdal/kdc/config.c (revision 283d988c23f8139afa6b31adef1909ddb9d0e4df)
1 /*
2  * Copyright (c) 1997-2000 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.30 2000/02/11 17:47:19 assar 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 
62 static struct getarg_strings addresses_str;	/* addresses to listen on */
63 krb5_addresses explicit_addresses;
64 
65 #ifdef KRB4
66 char *v4_realm;
67 #endif
68 #ifdef KASERVER
69 krb5_boolean enable_kaserver = -1;
70 #endif
71 
72 static int help_flag;
73 static int version_flag;
74 
75 static struct getargs args[] = {
76     {
77 	"config-file",	'c',	arg_string,	&config_file,
78 	"location of config file",	"file"
79     },
80     {
81 	"require-preauth",	'p',	arg_negative_flag, &require_preauth,
82 	"don't require pa-data in as-reqs"
83     },
84     {
85 	"max-request",	0,	arg_string, &max_request,
86 	"max size for a kdc-request", "size"
87     },
88 #if 0
89     {
90 	"database",	'd', 	arg_string, &databases,
91 	"location of database", "database"
92     },
93 #endif
94     { "enable-http", 'H', arg_flag, &enable_http, "turn on HTTP support" },
95 #ifdef KRB4
96     {
97 	"v4-realm",	'r',	arg_string, &v4_realm,
98 	"realm to serve v4-requests for"
99     },
100 #endif
101 #ifdef KASERVER
102     {
103 	"kaserver", 'K', arg_negative_flag,   &enable_kaserver,
104 	"turn off kaserver support"
105     },
106 #endif
107     {	"ports",	'P', 	arg_string, &port_str,
108 	"ports to listen to"
109     },
110     {	"addresses",	0,	arg_strings, &addresses_str,
111 	"addresses to listen on", "list of addresses" },
112     {	"help",		'h',	arg_flag,   &help_flag },
113     {	"version",	'v',	arg_flag,   &version_flag }
114 };
115 
116 static int num_args = sizeof(args) / sizeof(args[0]);
117 
118 static void
119 usage(int ret)
120 {
121     arg_printusage (args, num_args, NULL, "");
122     exit (ret);
123 }
124 
125 static void
126 get_dbinfo(krb5_config_section *cf)
127 {
128     krb5_config_binding *top_binding = NULL;
129     krb5_config_binding *db_binding;
130     krb5_config_binding *default_binding = NULL;
131     struct dbinfo *di, **dt;
132     const char *default_dbname = HDB_DEFAULT_DB;
133     const char *default_mkey = HDB_DB_DIR "/m-key";
134     const char *p;
135 
136     databases = NULL;
137     dt = &databases;
138     while((db_binding = (krb5_config_binding *)
139 	   krb5_config_get_next(context, cf, &top_binding,
140 				krb5_config_list,
141 				"kdc",
142 				"database",
143 				NULL))) {
144 	p = krb5_config_get_string(context, db_binding, "realm", NULL);
145 	if(p == NULL) {
146 	    if(default_binding) {
147 		krb5_warnx(context, "WARNING: more than one realm-less "
148 			   "database specification");
149 		krb5_warnx(context, "WARNING: using the first encountered");
150 	    } else
151 		default_binding = db_binding;
152 	    continue;
153 	}
154 	di = calloc(1, sizeof(*di));
155 	di->realm = strdup(p);
156 	p = krb5_config_get_string(context, db_binding, "dbname", NULL);
157 	if(p)
158 	    di->dbname = strdup(p);
159 	p = krb5_config_get_string(context, db_binding, "mkey_file", NULL);
160 	if(p)
161 	    di->mkey_file = strdup(p);
162 	*dt = di;
163 	dt = &di->next;
164     }
165     if(default_binding) {
166 	di = calloc(1, sizeof(*di));
167 	p = krb5_config_get_string(context, default_binding, "dbname", NULL);
168 	if(p) {
169 	    di->dbname = strdup(p);
170 	    default_dbname = p;
171 	}
172 	p = krb5_config_get_string(context, default_binding, "mkey_file", NULL);
173 	if(p) {
174 	    di->mkey_file = strdup(p);
175 	    default_mkey = p;
176 	}
177 	*dt = di;
178 	dt = &di->next;
179     } else {
180 	di = calloc(1, sizeof(*di));
181 	di->dbname = strdup(default_dbname);
182 	di->mkey_file = strdup(default_mkey);
183 	*dt = di;
184 	dt = &di->next;
185     }
186     for(di = databases; di; di = di->next) {
187 	if(di->dbname == NULL)
188 	    di->dbname = strdup(default_dbname);
189 	if(di->mkey_file == NULL) {
190 	    p = strrchr(di->dbname, '.');
191 	    if(p == NULL || strchr(p, '/') != NULL)
192 		asprintf(&di->mkey_file, "%s.mkey", di->dbname);
193 	    else
194 		asprintf(&di->mkey_file, "%.*s.mkey",
195 			 (int)(p - di->dbname), di->dbname);
196 	}
197     }
198 }
199 
200 static void
201 add_one_address (const char *str, int first)
202 {
203     krb5_error_code ret;
204     krb5_addresses tmp;
205 
206     ret = krb5_parse_address (context, str, &tmp);
207     if (ret)
208 	krb5_err (context, 1, ret, "parse_address `%s'", str);
209     if (first)
210 	krb5_copy_addresses(context, &tmp, &explicit_addresses);
211     else
212 	krb5_append_addresses(context, &explicit_addresses, &tmp);
213     krb5_free_addresses (context, &tmp);
214 }
215 
216 void
217 configure(int argc, char **argv)
218 {
219     krb5_config_section *cf = NULL;
220     int optind = 0;
221     int e;
222     const char *p;
223 
224     while((e = getarg(args, num_args, argc, argv, &optind)))
225 	warnx("error at argument `%s'", argv[optind]);
226 
227     if(help_flag)
228 	usage (0);
229 
230     if (version_flag) {
231 	print_version(NULL);
232 	exit(0);
233     }
234 
235     argc -= optind;
236     argv += optind;
237 
238     if (argc != 0)
239 	usage(1);
240 
241     if(config_file == NULL)
242 	config_file = HDB_DB_DIR "/kdc.conf";
243 
244     if(krb5_config_parse_file(config_file, &cf))
245 	cf = NULL;
246 
247     get_dbinfo(cf);
248 
249     if(max_request_str){
250 	max_request = parse_bytes(max_request_str, NULL);
251     }
252 
253     if(max_request == 0){
254 	p = krb5_config_get_string (context,
255 				    cf,
256 				    "kdc",
257 				    "max-request",
258 				    NULL);
259 	if(p)
260 	    max_request = parse_bytes(p, NULL);
261     }
262 
263     if(require_preauth == -1)
264 	require_preauth = krb5_config_get_bool(context, cf, "kdc",
265 					       "require-preauth", NULL);
266 
267     if(port_str == NULL){
268 	p = krb5_config_get_string(context, cf, "kdc", "ports", NULL);
269 	if (p != NULL)
270 	    port_str = strdup(p);
271     }
272 
273     explicit_addresses.len = 0;
274 
275     if (addresses_str.num_strings) {
276 	int i;
277 
278 	for (i = 0; i < addresses_str.num_strings; ++i)
279 	    add_one_address (addresses_str.strings[i], i == 0);
280     } else {
281 	char **foo = krb5_config_get_strings (context, cf,
282 					      "kdc", "addresses", NULL);
283 
284 	if (foo != NULL) {
285 	    add_one_address (*foo++, TRUE);
286 	    while (*foo)
287 		add_one_address (*foo++, FALSE);
288 	}
289     }
290 
291     if(enable_http == -1)
292 	enable_http = krb5_config_get_bool(context, cf, "kdc",
293 					   "enable-http", NULL);
294     check_ticket_addresses =
295 	krb5_config_get_bool(context, cf, "kdc",
296 			     "check-ticket-addresses", NULL);
297     allow_null_ticket_addresses =
298 	krb5_config_get_bool(context, cf, "kdc",
299 			     "allow-null-ticket-addresses", NULL);
300 #ifdef KRB4
301     if(v4_realm == NULL){
302 	p = krb5_config_get_string (context, cf,
303 				    "kdc",
304 				    "v4-realm",
305 				    NULL);
306 	if(p)
307 	    v4_realm = strdup(p);
308     }
309 #endif
310 #ifdef KASERVER
311     if (enable_kaserver == -1)
312 	enable_kaserver = krb5_config_get_bool_default(context, cf, TRUE,
313 						       "kdc",
314 						       "enable-kaserver",
315 						       NULL);
316 #endif
317 
318     encode_as_rep_as_tgs_rep = krb5_config_get_bool(context, cf, "kdc",
319 						    "encode_as_rep_as_tgs_rep",
320 						    NULL);
321 
322     kdc_warn_pwexpire = krb5_config_get_time (context, cf,
323 					      "kdc",
324 					      "kdc_warn_pwexpire",
325 					      NULL);
326     kdc_openlog(cf);
327     if(cf)
328 	krb5_config_file_free (context, cf);
329     if(max_request == 0)
330 	max_request = 64 * 1024;
331     if(require_preauth == -1)
332 	require_preauth = 1;
333     if (port_str == NULL)
334 	port_str = "+";
335 #ifdef KRB4
336     if(v4_realm == NULL){
337 	v4_realm = malloc(40); /* REALM_SZ */
338 	krb_get_lrealm(v4_realm, 1);
339     }
340 #endif
341 }
342