2 * Copyright (c) 2005, PADL Software Pty Ltd.
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
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
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 */
48 int detach_from_console
= -1;
49 #define DETACH_IS_DEFAULT FALSE
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
;
64 int disallow_getting_krbtgt
= 0;
65 int name_constraints
= -1;
68 static int version_flag
;
70 static struct getargs args
[] = {
72 "cache-name", 0, arg_string
, &system_cache_name
,
73 "system cache name", "cachename"
76 "config-file", 'c', arg_string
, &config_file
,
77 "location of config file", "file"
80 "group", 'g', arg_string
, &system_group
,
81 "system cache group", "group"
84 "max-request", 0, arg_string
, &max_request
,
85 "max size for a kcm-request", "size"
88 "launchd", 0, arg_flag
, &launchd_flag
,
89 "when in use by launchd"
94 "detach", 'D', arg_negative_flag
, &detach_from_console
,
95 "don't detach from console"
99 "detach", 0 , arg_flag
, &detach_from_console
,
100 "detach from console"
104 { "help", 'h', arg_flag
, &help_flag
},
106 "system-principal", 'k', arg_string
, &system_principal
,
107 "system principal name", "principal"
110 "lifetime", 'l', arg_string
, &ticket_life
,
111 "lifetime of system tickets", "time"
114 "mode", 'm', arg_string
, &system_perms
,
115 "octal mode of system cache", "mode"
118 "name-constraints", 'n', arg_negative_flag
, &name_constraints
,
119 "disable credentials cache name constraints"
122 "disallow-getting-krbtgt", 0, arg_flag
, &disallow_getting_krbtgt
,
123 "disable fetching krbtgt from the cache"
126 "renewable-life", 'r', arg_string
, &renew_life
,
127 "renewable lifetime of system tickets", "time"
130 "socket-path", 's', arg_string
, &socket_path
,
131 "path to kcm domain socket", "path"
133 #ifdef HAVE_DOOR_CREATE
135 "door-path", 's', arg_string
, &door_path
,
136 "path to kcm door", "path"
140 "server", 'S', arg_string
, &system_server
,
141 "server to get system ticket for", "principal"
144 "keytab", 't', arg_string
, &system_keytab
,
145 "system keytab name", "keytab"
148 "user", 'u', arg_string
, &system_user
,
149 "system cache owner", "user"
151 { "version", 'v', arg_flag
, &version_flag
}
154 static int num_args
= sizeof(args
) / sizeof(args
[0]);
159 arg_printusage (args
, num_args
, NULL
, "");
163 static int parse_owners(kcm_ccache ccache
)
172 if (system_user
!= NULL
) {
173 if (isdigit((unsigned char)system_user
[0])) {
174 pw
= getpwuid(atoi(system_user
));
176 pw
= getpwnam(system_user
);
182 system_user
= strdup(pw
->pw_name
);
183 if (system_user
== NULL
) {
187 uid
= pw
->pw_uid
; uid_p
= 1;
188 gid
= pw
->pw_gid
; gid_p
= 1;
191 if (system_group
!= NULL
) {
192 if (isdigit((unsigned char)system_group
[0])) {
193 gr
= getgrgid(atoi(system_group
));
195 gr
= getgrnam(system_group
);
201 gid
= gr
->gr_gid
; gid_p
= 1;
207 ccache
->uid
= 0; /* geteuid() XXX */
212 ccache
->gid
= 0; /* getegid() XXX */
218 kcm_system_config_get_string(const char *string
)
220 return krb5_config_get_string(kcm_context
, NULL
, "kcm",
221 "system_ccache", string
, NULL
);
224 static krb5_error_code
225 ccache_init_system(void)
230 if (system_cache_name
== NULL
)
231 system_cache_name
= kcm_system_config_get_string("cc_name");
233 ret
= kcm_ccache_new(kcm_context
,
234 system_cache_name
? system_cache_name
: "SYSTEM",
239 ccache
->flags
|= KCM_FLAGS_OWNER_IS_SYSTEM
;
240 ccache
->flags
|= KCM_FLAGS_USE_KEYTAB
;
242 ret
= parse_owners(ccache
);
246 ret
= krb5_parse_name(kcm_context
, system_principal
, &ccache
->client
);
248 kcm_release_ccache(kcm_context
, ccache
);
252 if (system_server
== NULL
)
253 system_server
= kcm_system_config_get_string("server");
255 if (system_server
!= NULL
) {
256 ret
= krb5_parse_name(kcm_context
, system_server
, &ccache
->server
);
258 kcm_release_ccache(kcm_context
, ccache
);
263 if (system_keytab
== NULL
)
264 system_keytab
= kcm_system_config_get_string("keytab_name");
266 if (system_keytab
!= NULL
) {
267 ret
= krb5_kt_resolve(kcm_context
, system_keytab
, &ccache
->key
.keytab
);
269 ret
= krb5_kt_default(kcm_context
, &ccache
->key
.keytab
);
272 kcm_release_ccache(kcm_context
, ccache
);
276 if (renew_life
== NULL
)
277 renew_life
= kcm_system_config_get_string("renew_life");
279 if (renew_life
== NULL
)
280 renew_life
= "1 month";
282 if (renew_life
!= NULL
) {
283 ccache
->renew_life
= parse_time(renew_life
, "s");
284 if (ccache
->renew_life
< 0) {
285 kcm_release_ccache(kcm_context
, ccache
);
290 if (ticket_life
== NULL
)
291 ticket_life
= kcm_system_config_get_string("ticket_life");
293 if (ticket_life
!= NULL
) {
294 ccache
->tkt_life
= parse_time(ticket_life
, "s");
295 if (ccache
->tkt_life
< 0) {
296 kcm_release_ccache(kcm_context
, ccache
);
301 if (system_perms
== NULL
)
302 system_perms
= kcm_system_config_get_string("mode");
304 if (system_perms
!= NULL
) {
307 if (sscanf(system_perms
, "%o", &mode
) != 1)
313 if (disallow_getting_krbtgt
== -1) {
314 disallow_getting_krbtgt
=
315 krb5_config_get_bool_default(kcm_context
, NULL
, FALSE
, "kcm",
316 "disallow-getting-krbtgt", NULL
);
319 /* enqueue default actions for credentials cache */
320 ret
= kcm_ccache_enqueue_default(kcm_context
, ccache
, NULL
);
322 kcm_release_ccache(kcm_context
, ccache
); /* retained by event queue */
328 kcm_configure(int argc
, char **argv
)
334 while(getarg(args
, num_args
, argc
, argv
, &optind
))
335 warnx("error at argument `%s'", argv
[optind
]);
354 if(config_file
== NULL
)
355 config_file
= _PATH_KCM_CONF
;
357 ret
= krb5_prepend_config_files_default(config_file
, &files
);
359 krb5_err(kcm_context
, 1, ret
, "getting configuration files");
361 ret
= krb5_set_config_files(kcm_context
, files
);
362 krb5_free_config_files(files
);
364 krb5_err(kcm_context
, 1, ret
, "reading configuration files");
368 max_request
= parse_bytes(max_request_str
, NULL
);
370 if(max_request
== 0){
371 p
= krb5_config_get_string (kcm_context
,
377 max_request
= parse_bytes(p
, NULL
);
380 if (system_principal
== NULL
) {
381 system_principal
= kcm_system_config_get_string("principal");
384 if (system_principal
!= NULL
) {
385 ret
= ccache_init_system();
387 krb5_err(kcm_context
, 1, ret
, "initializing system ccache");
390 #ifdef SUPPORT_DETACH
391 if(detach_from_console
== -1)
392 detach_from_console
= krb5_config_get_bool_default(kcm_context
, NULL
,
399 max_request
= 64 * 1024;