No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / kcm / config.c
blob2f29dcf177360405add05be9fe2a6d7f904a1039
1 /*
2 * Copyright (c) 2005, PADL Software Pty Ltd.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of PADL Software nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include "kcm_locl.h"
34 #include <getarg.h>
35 #include <parse_bytes.h>
37 __RCSID("$Heimdal: config.c 15296 2005-05-30 10:17:43Z lha $"
38 "$NetBSD$");
40 static const char *config_file; /* location of kcm config file */
42 size_t max_request = 0; /* maximal size of a request */
43 char *socket_path = NULL;
44 char *door_path = NULL;
46 static char *max_request_str; /* `max_request' as a string */
48 int detach_from_console = -1;
49 #define DETACH_IS_DEFAULT FALSE
51 static const char *system_cache_name = NULL;
52 static const char *system_keytab = NULL;
53 static const char *system_principal = NULL;
54 static const char *system_server = NULL;
55 static const char *system_perms = NULL;
56 static const char *system_user = NULL;
57 static const char *system_group = NULL;
59 static const char *renew_life = NULL;
60 static const char *ticket_life = NULL;
62 int disallow_getting_krbtgt = -1;
63 int name_constraints = -1;
65 static int help_flag;
66 static int version_flag;
68 static struct getargs args[] = {
70 "cache-name", 0, arg_string, &system_cache_name,
71 "system cache name", "cachename"
74 "config-file", 'c', arg_string, &config_file,
75 "location of config file", "file"
78 "group", 'g', arg_string, &system_group,
79 "system cache group", "group"
82 "max-request", 0, arg_string, &max_request,
83 "max size for a kcm-request", "size"
85 #if DETACH_IS_DEFAULT
87 "detach", 'D', arg_negative_flag, &detach_from_console,
88 "don't detach from console"
90 #else
92 "detach", 0 , arg_flag, &detach_from_console,
93 "detach from console"
95 #endif
96 { "help", 'h', arg_flag, &help_flag },
98 "system-principal", 'k', arg_string, &system_principal,
99 "system principal name", "principal"
102 "lifetime", 'l', arg_string, &ticket_life,
103 "lifetime of system tickets", "time"
106 "mode", 'm', arg_string, &system_perms,
107 "octal mode of system cache", "mode"
110 "name-constraints", 'n', arg_negative_flag, &name_constraints,
111 "disable credentials cache name constraints"
114 "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt,
115 "disable fetching krbtgt from the cache"
118 "renewable-life", 'r', arg_string, &renew_life,
119 "renewable lifetime of system tickets", "time"
122 "socket-path", 's', arg_string, &socket_path,
123 "path to kcm domain socket", "path"
125 #ifdef HAVE_DOOR_CREATE
127 "door-path", 's', arg_string, &door_path,
128 "path to kcm door", "path"
130 #endif
132 "server", 'S', arg_string, &system_server,
133 "server to get system ticket for", "principal"
136 "keytab", 't', arg_string, &system_keytab,
137 "system keytab name", "keytab"
140 "user", 'u', arg_string, &system_user,
141 "system cache owner", "user"
143 { "version", 'v', arg_flag, &version_flag }
146 static int num_args = sizeof(args) / sizeof(args[0]);
148 static void
149 usage(int ret)
151 arg_printusage (args, num_args, NULL, "");
152 exit (ret);
155 static int parse_owners(kcm_ccache ccache)
157 uid_t uid = 0;
158 gid_t gid = 0;
159 struct passwd *pw;
160 struct group *gr;
161 int uid_p = 0;
162 int gid_p = 0;
164 if (system_user != NULL) {
165 if (isdigit((unsigned char)system_user[0])) {
166 pw = getpwuid(atoi(system_user));
167 } else {
168 pw = getpwnam(system_user);
170 if (pw == NULL) {
171 return errno;
174 system_user = strdup(pw->pw_name);
175 if (system_user == NULL) {
176 return ENOMEM;
179 uid = pw->pw_uid; uid_p = 1;
180 gid = pw->pw_gid; gid_p = 1;
183 if (system_group != NULL) {
184 if (isdigit((unsigned char)system_group[0])) {
185 gr = getgrgid(atoi(system_group));
186 } else {
187 gr = getgrnam(system_group);
189 if (gr == NULL) {
190 return errno;
193 gid = gr->gr_gid; gid_p = 1;
196 if (uid_p)
197 ccache->uid = uid;
198 else
199 ccache->uid = 0; /* geteuid() XXX */
201 if (gid_p)
202 ccache->gid = gid;
203 else
204 ccache->gid = 0; /* getegid() XXX */
206 return 0;
209 static const char *
210 kcm_system_config_get_string(const char *string)
212 return krb5_config_get_string(kcm_context, NULL, "kcm",
213 "system_ccache", string, NULL);
216 static krb5_error_code
217 ccache_init_system(void)
219 kcm_ccache ccache;
220 krb5_error_code ret;
222 if (system_cache_name == NULL)
223 system_cache_name = kcm_system_config_get_string("cc_name");
225 ret = kcm_ccache_new(kcm_context,
226 system_cache_name ? system_cache_name : "SYSTEM",
227 &ccache);
228 if (ret)
229 return ret;
231 ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM;
232 ccache->flags |= KCM_FLAGS_USE_KEYTAB;
234 ret = parse_owners(ccache);
235 if (ret)
236 return ret;
238 ret = krb5_parse_name(kcm_context, system_principal, &ccache->client);
239 if (ret) {
240 kcm_release_ccache(kcm_context, &ccache);
241 return ret;
244 if (system_server == NULL)
245 system_server = kcm_system_config_get_string("server");
247 if (system_server != NULL) {
248 ret = krb5_parse_name(kcm_context, system_server, &ccache->server);
249 if (ret) {
250 kcm_release_ccache(kcm_context, &ccache);
251 return ret;
255 if (system_keytab == NULL)
256 system_keytab = kcm_system_config_get_string("keytab_name");
258 if (system_keytab != NULL) {
259 ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->key.keytab);
260 } else {
261 ret = krb5_kt_default(kcm_context, &ccache->key.keytab);
263 if (ret) {
264 kcm_release_ccache(kcm_context, &ccache);
265 return ret;
268 if (renew_life == NULL)
269 renew_life = kcm_system_config_get_string("renew_life");
271 if (renew_life == NULL)
272 renew_life = "1 month";
274 if (renew_life != NULL) {
275 ccache->renew_life = parse_time(renew_life, "s");
276 if (ccache->renew_life < 0) {
277 kcm_release_ccache(kcm_context, &ccache);
278 return EINVAL;
282 if (ticket_life == NULL)
283 ticket_life = kcm_system_config_get_string("ticket_life");
285 if (ticket_life != NULL) {
286 ccache->tkt_life = parse_time(ticket_life, "s");
287 if (ccache->tkt_life < 0) {
288 kcm_release_ccache(kcm_context, &ccache);
289 return EINVAL;
293 if (system_perms == NULL)
294 system_perms = kcm_system_config_get_string("mode");
296 if (system_perms != NULL) {
297 int mode;
299 if (sscanf(system_perms, "%o", &mode) != 1)
300 return EINVAL;
302 ccache->mode = mode;
305 if (disallow_getting_krbtgt == -1) {
306 disallow_getting_krbtgt =
307 krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm",
308 "disallow-getting-krbtgt", NULL);
311 /* enqueue default actions for credentials cache */
312 ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL);
314 kcm_release_ccache(kcm_context, &ccache); /* retained by event queue */
316 return ret;
319 void
320 kcm_configure(int argc, char **argv)
322 krb5_error_code ret;
323 int optind = 0;
324 const char *p;
326 while(getarg(args, num_args, argc, argv, &optind))
327 warnx("error at argument `%s'", argv[optind]);
329 if(help_flag)
330 usage (0);
332 if (version_flag) {
333 print_version(NULL);
334 exit(0);
337 argc -= optind;
338 argv += optind;
340 if (argc != 0)
341 usage(1);
344 char **files;
346 if(config_file == NULL)
347 config_file = _PATH_KCM_CONF;
349 ret = krb5_prepend_config_files_default(config_file, &files);
350 if (ret)
351 krb5_err(kcm_context, 1, ret, "getting configuration files");
353 ret = krb5_set_config_files(kcm_context, files);
354 krb5_free_config_files(files);
355 if(ret)
356 krb5_err(kcm_context, 1, ret, "reading configuration files");
359 if(max_request_str)
360 max_request = parse_bytes(max_request_str, NULL);
362 if(max_request == 0){
363 p = krb5_config_get_string (kcm_context,
364 NULL,
365 "kcm",
366 "max-request",
367 NULL);
368 if(p)
369 max_request = parse_bytes(p, NULL);
372 if (system_principal == NULL) {
373 system_principal = kcm_system_config_get_string("principal");
376 if (system_principal != NULL) {
377 ret = ccache_init_system();
378 if (ret)
379 krb5_err(kcm_context, 1, ret, "initializing system ccache");
382 if(detach_from_console == -1)
383 detach_from_console = krb5_config_get_bool_default(kcm_context, NULL,
384 DETACH_IS_DEFAULT,
385 "kcm",
386 "detach", NULL);
387 kcm_openlog();
388 if(max_request == 0)
389 max_request = 64 * 1024;