YFS Coverity 11525
[heimdal.git] / kcm / config.c
bloba3a7d34655f6b52fbfdd9411ab38cd69cca6a596
1 /*
2 * Copyright (c) 2005, PADL Software Pty Ltd.
3 * All rights reserved.
5 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "kcm_locl.h"
36 #include <getarg.h>
37 #include <parse_bytes.h>
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 int detach_from_console = -1;
48 int daemon_child = -1;
50 static const char *system_cache_name = NULL;
51 static const char *system_keytab = NULL;
52 static const char *system_principal = NULL;
53 static const char *system_server = NULL;
54 static const char *system_perms = NULL;
55 static const char *system_user = NULL;
56 static const char *system_group = NULL;
58 static const char *renew_life = NULL;
59 static const char *ticket_life = NULL;
61 int launchd_flag = 0;
62 int disallow_getting_krbtgt = 0;
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"
86 "launchd", 0, arg_flag, &launchd_flag,
87 "when in use by launchd", NULL
90 "detach", 0 , arg_flag, &detach_from_console,
91 "detach from console", NULL
94 "daemon-child", 0 , arg_integer, &daemon_child,
95 "private argument, do not use", NULL
97 { "help", 'h', arg_flag, &help_flag, NULL, NULL },
99 "system-principal", 'k', arg_string, &system_principal,
100 "system principal name", "principal"
103 "lifetime", 'l', arg_string, &ticket_life,
104 "lifetime of system tickets", "time"
107 "mode", 'm', arg_string, &system_perms,
108 "octal mode of system cache", "mode"
111 "name-constraints", 'n', arg_negative_flag, &name_constraints,
112 "disable credentials cache name constraints", NULL
115 "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt,
116 "disable fetching krbtgt from the cache", NULL
119 "renewable-life", 'r', arg_string, &renew_life,
120 "renewable lifetime of system tickets", "time"
123 "socket-path", 's', arg_string, &socket_path,
124 "path to kcm domain socket", "path"
126 #ifdef HAVE_DOOR_CREATE
128 "door-path", 's', arg_string, &door_path,
129 "path to kcm door", "path"
131 #endif
133 "server", 'S', arg_string, &system_server,
134 "server to get system ticket for", "principal"
137 "keytab", 't', arg_string, &system_keytab,
138 "system keytab name", "keytab"
141 "user", 'u', arg_string, &system_user,
142 "system cache owner", "user"
144 { "version", 'v', arg_flag, &version_flag, NULL, NULL }
147 static int num_args = sizeof(args) / sizeof(args[0]);
149 static void
150 usage(int ret)
152 arg_printusage (args, num_args, NULL, "");
153 exit (ret);
156 static int parse_owners(kcm_ccache ccache)
158 uid_t uid = 0;
159 gid_t gid = 0;
160 struct passwd *pw;
161 struct group *gr;
162 int uid_p = 0;
163 int gid_p = 0;
165 if (system_user != NULL) {
166 if (isdigit((unsigned char)system_user[0])) {
167 pw = getpwuid(atoi(system_user));
168 } else {
169 pw = getpwnam(system_user);
171 if (pw == NULL) {
172 return errno;
175 system_user = strdup(pw->pw_name);
176 if (system_user == NULL) {
177 return ENOMEM;
180 uid = pw->pw_uid; uid_p = 1;
181 gid = pw->pw_gid; gid_p = 1;
184 if (system_group != NULL) {
185 if (isdigit((unsigned char)system_group[0])) {
186 gr = getgrgid(atoi(system_group));
187 } else {
188 gr = getgrnam(system_group);
190 if (gr == NULL) {
191 return errno;
194 gid = gr->gr_gid; gid_p = 1;
197 if (uid_p)
198 ccache->uid = uid;
199 else
200 ccache->uid = 0; /* geteuid() XXX */
202 if (gid_p)
203 ccache->gid = gid;
204 else
205 ccache->gid = 0; /* getegid() XXX */
207 return 0;
210 static const char *
211 kcm_system_config_get_string(const char *string)
213 return krb5_config_get_string(kcm_context, NULL, "kcm",
214 "system_ccache", string, NULL);
217 static krb5_error_code
218 ccache_init_system(void)
220 kcm_ccache ccache;
221 krb5_error_code ret;
223 if (system_cache_name == NULL)
224 system_cache_name = kcm_system_config_get_string("cc_name");
226 ret = kcm_ccache_new(kcm_context,
227 system_cache_name ? system_cache_name : "SYSTEM",
228 &ccache);
229 if (ret)
230 return ret;
232 ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM;
233 ccache->flags |= KCM_FLAGS_USE_KEYTAB;
235 ret = parse_owners(ccache);
236 if (ret)
237 return ret;
239 ret = krb5_parse_name(kcm_context, system_principal, &ccache->client);
240 if (ret) {
241 kcm_release_ccache(kcm_context, ccache);
242 return ret;
245 if (system_server == NULL)
246 system_server = kcm_system_config_get_string("server");
248 if (system_server != NULL) {
249 ret = krb5_parse_name(kcm_context, system_server, &ccache->server);
250 if (ret) {
251 kcm_release_ccache(kcm_context, ccache);
252 return ret;
256 if (system_keytab == NULL)
257 system_keytab = kcm_system_config_get_string("keytab_name");
259 if (system_keytab != NULL) {
260 ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->key.keytab);
261 } else {
262 ret = krb5_kt_default(kcm_context, &ccache->key.keytab);
264 if (ret) {
265 kcm_release_ccache(kcm_context, ccache);
266 return ret;
269 if (renew_life == NULL)
270 renew_life = kcm_system_config_get_string("renew_life");
272 if (renew_life == NULL)
273 renew_life = "1 month";
275 if (renew_life != NULL) {
276 ccache->renew_life = parse_time(renew_life, "s");
277 if (ccache->renew_life < 0) {
278 kcm_release_ccache(kcm_context, ccache);
279 return EINVAL;
283 if (ticket_life == NULL)
284 ticket_life = kcm_system_config_get_string("ticket_life");
286 if (ticket_life != NULL) {
287 ccache->tkt_life = parse_time(ticket_life, "s");
288 if (ccache->tkt_life < 0) {
289 kcm_release_ccache(kcm_context, ccache);
290 return EINVAL;
294 if (system_perms == NULL)
295 system_perms = kcm_system_config_get_string("mode");
297 if (system_perms != NULL) {
298 int mode;
300 if (sscanf(system_perms, "%o", &mode) != 1)
301 return EINVAL;
303 ccache->mode = mode;
306 if (disallow_getting_krbtgt == -1) {
307 disallow_getting_krbtgt =
308 krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm",
309 "disallow-getting-krbtgt", NULL);
312 /* enqueue default actions for credentials cache */
313 ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL);
315 kcm_release_ccache(kcm_context, ccache); /* retained by event queue */
317 return ret;
320 void
321 kcm_configure(int argc, char **argv)
323 krb5_error_code ret;
324 int optidx = 0;
325 const char *p;
327 while (getarg(args, num_args, argc, argv, &optidx))
328 warnx("error at argument `%s'", argv[optidx]);
330 if (help_flag)
331 usage (0);
333 if (version_flag) {
334 print_version(NULL);
335 exit(0);
338 argc -= optidx;
339 argv += optidx;
341 if (argc != 0)
342 usage(1);
345 char **files;
347 if(config_file == NULL)
348 config_file = _PATH_KCM_CONF;
350 ret = krb5_prepend_config_files_default(config_file, &files);
351 if (ret)
352 krb5_err(kcm_context, 1, ret, "getting configuration files");
354 ret = krb5_set_config_files(kcm_context, files);
355 krb5_free_config_files(files);
356 if(ret)
357 krb5_err(kcm_context, 1, ret, "reading configuration files");
360 if(max_request_str)
361 max_request = parse_bytes(max_request_str, NULL);
363 if(max_request == 0){
364 p = krb5_config_get_string (kcm_context,
365 NULL,
366 "kcm",
367 "max-request",
368 NULL);
369 if(p)
370 max_request = parse_bytes(p, NULL);
373 if (system_principal == NULL) {
374 system_principal = kcm_system_config_get_string("principal");
377 if (system_principal != NULL) {
378 ret = ccache_init_system();
379 if (ret)
380 krb5_err(kcm_context, 1, ret, "initializing system ccache");
383 if(detach_from_console == -1)
384 detach_from_console = krb5_config_get_bool_default(kcm_context, NULL,
385 FALSE,
386 "kcm",
387 "detach", NULL);
388 kcm_openlog();
389 if(max_request == 0)
390 max_request = 64 * 1024;