use size_t for index
[heimdal.git] / kcm / config.c
blobd7c5e16884d9fa3d3bc355e5f5c3ba420c42759a
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("$Id$");
39 static const char *config_file; /* location of kcm config file */
41 size_t max_request = 0; /* maximal size of a request */
42 char *socket_path = NULL;
43 char *door_path = NULL;
45 static char *max_request_str; /* `max_request' as a string */
47 #ifdef SUPPORT_DETACH
48 int detach_from_console = -1;
49 #define DETACH_IS_DEFAULT FALSE
50 #endif
52 static const char *system_cache_name = NULL;
53 static const char *system_keytab = NULL;
54 static const char *system_principal = NULL;
55 static const char *system_server = NULL;
56 static const char *system_perms = NULL;
57 static const char *system_user = NULL;
58 static const char *system_group = NULL;
60 static const char *renew_life = NULL;
61 static const char *ticket_life = NULL;
63 int disallow_getting_krbtgt = -1;
64 int name_constraints = -1;
66 static int help_flag;
67 static int version_flag;
69 static struct getargs args[] = {
71 "cache-name", 0, arg_string, &system_cache_name,
72 "system cache name", "cachename"
75 "config-file", 'c', arg_string, &config_file,
76 "location of config file", "file"
79 "group", 'g', arg_string, &system_group,
80 "system cache group", "group"
83 "max-request", 0, arg_string, &max_request,
84 "max size for a kcm-request", "size"
86 #ifdef SUPPORT_DETACH
87 #if DETACH_IS_DEFAULT
89 "detach", 'D', arg_negative_flag, &detach_from_console,
90 "don't detach from console"
92 #else
94 "detach", 0 , arg_flag, &detach_from_console,
95 "detach from console"
97 #endif
98 #endif
99 { "help", 'h', arg_flag, &help_flag },
101 "system-principal", 'k', arg_string, &system_principal,
102 "system principal name", "principal"
105 "lifetime", 'l', arg_string, &ticket_life,
106 "lifetime of system tickets", "time"
109 "mode", 'm', arg_string, &system_perms,
110 "octal mode of system cache", "mode"
113 "name-constraints", 'n', arg_negative_flag, &name_constraints,
114 "disable credentials cache name constraints"
117 "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt,
118 "disable fetching krbtgt from the cache"
121 "renewable-life", 'r', arg_string, &renew_life,
122 "renewable lifetime of system tickets", "time"
125 "socket-path", 's', arg_string, &socket_path,
126 "path to kcm domain socket", "path"
128 #ifdef HAVE_DOOR_CREATE
130 "door-path", 's', arg_string, &door_path,
131 "path to kcm door", "path"
133 #endif
135 "server", 'S', arg_string, &system_server,
136 "server to get system ticket for", "principal"
139 "keytab", 't', arg_string, &system_keytab,
140 "system keytab name", "keytab"
143 "user", 'u', arg_string, &system_user,
144 "system cache owner", "user"
146 { "version", 'v', arg_flag, &version_flag }
149 static int num_args = sizeof(args) / sizeof(args[0]);
151 static void
152 usage(int ret)
154 arg_printusage (args, num_args, NULL, "");
155 exit (ret);
158 static int parse_owners(kcm_ccache ccache)
160 uid_t uid = 0;
161 gid_t gid = 0;
162 struct passwd *pw;
163 struct group *gr;
164 int uid_p = 0;
165 int gid_p = 0;
167 if (system_user != NULL) {
168 if (isdigit((unsigned char)system_user[0])) {
169 pw = getpwuid(atoi(system_user));
170 } else {
171 pw = getpwnam(system_user);
173 if (pw == NULL) {
174 return errno;
177 system_user = strdup(pw->pw_name);
178 if (system_user == NULL) {
179 return ENOMEM;
182 uid = pw->pw_uid; uid_p = 1;
183 gid = pw->pw_gid; gid_p = 1;
186 if (system_group != NULL) {
187 if (isdigit((unsigned char)system_group[0])) {
188 gr = getgrgid(atoi(system_group));
189 } else {
190 gr = getgrnam(system_group);
192 if (gr == NULL) {
193 return errno;
196 gid = gr->gr_gid; gid_p = 1;
199 if (uid_p)
200 ccache->uid = uid;
201 else
202 ccache->uid = 0; /* geteuid() XXX */
204 if (gid_p)
205 ccache->gid = gid;
206 else
207 ccache->gid = 0; /* getegid() XXX */
209 return 0;
212 static const char *
213 kcm_system_config_get_string(const char *string)
215 return krb5_config_get_string(kcm_context, NULL, "kcm",
216 "system_ccache", string, NULL);
219 static krb5_error_code
220 ccache_init_system(void)
222 kcm_ccache ccache;
223 krb5_error_code ret;
225 if (system_cache_name == NULL)
226 system_cache_name = kcm_system_config_get_string("cc_name");
228 ret = kcm_ccache_new(kcm_context,
229 system_cache_name ? system_cache_name : "SYSTEM",
230 &ccache);
231 if (ret)
232 return ret;
234 ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM;
235 ccache->flags |= KCM_FLAGS_USE_KEYTAB;
237 ret = parse_owners(ccache);
238 if (ret)
239 return ret;
241 ret = krb5_parse_name(kcm_context, system_principal, &ccache->client);
242 if (ret) {
243 kcm_release_ccache(kcm_context, &ccache);
244 return ret;
247 if (system_server == NULL)
248 system_server = kcm_system_config_get_string("server");
250 if (system_server != NULL) {
251 ret = krb5_parse_name(kcm_context, system_server, &ccache->server);
252 if (ret) {
253 kcm_release_ccache(kcm_context, &ccache);
254 return ret;
258 if (system_keytab == NULL)
259 system_keytab = kcm_system_config_get_string("keytab_name");
261 if (system_keytab != NULL) {
262 ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->key.keytab);
263 } else {
264 ret = krb5_kt_default(kcm_context, &ccache->key.keytab);
266 if (ret) {
267 kcm_release_ccache(kcm_context, &ccache);
268 return ret;
271 if (renew_life == NULL)
272 renew_life = kcm_system_config_get_string("renew_life");
274 if (renew_life == NULL)
275 renew_life = "1 month";
277 if (renew_life != NULL) {
278 ccache->renew_life = parse_time(renew_life, "s");
279 if (ccache->renew_life < 0) {
280 kcm_release_ccache(kcm_context, &ccache);
281 return EINVAL;
285 if (ticket_life == NULL)
286 ticket_life = kcm_system_config_get_string("ticket_life");
288 if (ticket_life != NULL) {
289 ccache->tkt_life = parse_time(ticket_life, "s");
290 if (ccache->tkt_life < 0) {
291 kcm_release_ccache(kcm_context, &ccache);
292 return EINVAL;
296 if (system_perms == NULL)
297 system_perms = kcm_system_config_get_string("mode");
299 if (system_perms != NULL) {
300 int mode;
302 if (sscanf(system_perms, "%o", &mode) != 1)
303 return EINVAL;
305 ccache->mode = mode;
308 if (disallow_getting_krbtgt == -1) {
309 disallow_getting_krbtgt =
310 krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm",
311 "disallow-getting-krbtgt", NULL);
314 /* enqueue default actions for credentials cache */
315 ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL);
317 kcm_release_ccache(kcm_context, &ccache); /* retained by event queue */
319 return ret;
322 void
323 kcm_configure(int argc, char **argv)
325 krb5_error_code ret;
326 int optind = 0;
327 const char *p;
329 while(getarg(args, num_args, argc, argv, &optind))
330 warnx("error at argument `%s'", argv[optind]);
332 if(help_flag)
333 usage (0);
335 if (version_flag) {
336 print_version(NULL);
337 exit(0);
340 argc -= optind;
341 argv += optind;
343 if (argc != 0)
344 usage(1);
347 char **files;
349 if(config_file == NULL)
350 config_file = _PATH_KCM_CONF;
352 ret = krb5_prepend_config_files_default(config_file, &files);
353 if (ret)
354 krb5_err(kcm_context, 1, ret, "getting configuration files");
356 ret = krb5_set_config_files(kcm_context, files);
357 krb5_free_config_files(files);
358 if(ret)
359 krb5_err(kcm_context, 1, ret, "reading configuration files");
362 if(max_request_str)
363 max_request = parse_bytes(max_request_str, NULL);
365 if(max_request == 0){
366 p = krb5_config_get_string (kcm_context,
367 NULL,
368 "kcm",
369 "max-request",
370 NULL);
371 if(p)
372 max_request = parse_bytes(p, NULL);
375 if (system_principal == NULL) {
376 system_principal = kcm_system_config_get_string("principal");
379 if (system_principal != NULL) {
380 ret = ccache_init_system();
381 if (ret)
382 krb5_err(kcm_context, 1, ret, "initializing system ccache");
385 #ifdef SUPPORT_DETACH
386 if(detach_from_console == -1)
387 detach_from_console = krb5_config_get_bool_default(kcm_context, NULL,
388 DETACH_IS_DEFAULT,
389 "kcm",
390 "detach", NULL);
391 #endif
392 kcm_openlog();
393 if(max_request == 0)
394 max_request = 64 * 1024;