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