config.c revision 1.3 1 1.3 christos /* $NetBSD: config.c,v 1.3 2017/09/08 15:29:43 christos Exp $ */
2 1.1 elric
3 1.1 elric /*
4 1.1 elric * Copyright (c) 2005, PADL Software Pty Ltd.
5 1.1 elric * All rights reserved.
6 1.1 elric *
7 1.1 elric * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 1.1 elric *
9 1.1 elric * Redistribution and use in source and binary forms, with or without
10 1.1 elric * modification, are permitted provided that the following conditions
11 1.1 elric * are met:
12 1.1 elric *
13 1.1 elric * 1. Redistributions of source code must retain the above copyright
14 1.1 elric * notice, this list of conditions and the following disclaimer.
15 1.1 elric *
16 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 elric * notice, this list of conditions and the following disclaimer in the
18 1.1 elric * documentation and/or other materials provided with the distribution.
19 1.1 elric *
20 1.1 elric * 3. Neither the name of PADL Software nor the names of its contributors
21 1.1 elric * may be used to endorse or promote products derived from this software
22 1.1 elric * without specific prior written permission.
23 1.1 elric *
24 1.1 elric * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
25 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
28 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 elric * SUCH DAMAGE.
35 1.1 elric */
36 1.1 elric
37 1.1 elric #include "kcm_locl.h"
38 1.1 elric #include <krb5/getarg.h>
39 1.1 elric #include <krb5/parse_bytes.h>
40 1.1 elric
41 1.1 elric static const char *config_file; /* location of kcm config file */
42 1.1 elric
43 1.1 elric size_t max_request = 0; /* maximal size of a request */
44 1.1 elric char *socket_path = NULL;
45 1.1 elric char *door_path = NULL;
46 1.1 elric
47 1.1 elric static char *max_request_str; /* `max_request' as a string */
48 1.1 elric
49 1.1 elric int detach_from_console = -1;
50 1.2 christos int daemon_child = -1;
51 1.1 elric
52 1.1 elric static const char *system_cache_name = NULL;
53 1.1 elric static const char *system_keytab = NULL;
54 1.1 elric static const char *system_principal = NULL;
55 1.1 elric static const char *system_server = NULL;
56 1.1 elric static const char *system_perms = NULL;
57 1.1 elric static const char *system_user = NULL;
58 1.1 elric static const char *system_group = NULL;
59 1.1 elric
60 1.1 elric static const char *renew_life = NULL;
61 1.1 elric static const char *ticket_life = NULL;
62 1.1 elric
63 1.1 elric int launchd_flag = 0;
64 1.1 elric int disallow_getting_krbtgt = 0;
65 1.1 elric int name_constraints = -1;
66 1.1 elric
67 1.1 elric static int help_flag;
68 1.1 elric static int version_flag;
69 1.1 elric
70 1.1 elric static struct getargs args[] = {
71 1.1 elric {
72 1.1 elric "cache-name", 0, arg_string, &system_cache_name,
73 1.1 elric "system cache name", "cachename"
74 1.1 elric },
75 1.1 elric {
76 1.1 elric "config-file", 'c', arg_string, &config_file,
77 1.1 elric "location of config file", "file"
78 1.1 elric },
79 1.1 elric {
80 1.1 elric "group", 'g', arg_string, &system_group,
81 1.1 elric "system cache group", "group"
82 1.1 elric },
83 1.1 elric {
84 1.1 elric "max-request", 0, arg_string, &max_request,
85 1.1 elric "max size for a kcm-request", "size"
86 1.1 elric },
87 1.1 elric {
88 1.1 elric "launchd", 0, arg_flag, &launchd_flag,
89 1.2 christos "when in use by launchd", NULL
90 1.1 elric },
91 1.1 elric {
92 1.2 christos "detach", 0 , arg_flag, &detach_from_console,
93 1.2 christos "detach from console", NULL
94 1.1 elric },
95 1.1 elric {
96 1.2 christos "daemon-child", 0 , arg_integer, &daemon_child,
97 1.2 christos "private argument, do not use", NULL
98 1.1 elric },
99 1.2 christos { "help", 'h', arg_flag, &help_flag, NULL, NULL },
100 1.1 elric {
101 1.1 elric "system-principal", 'k', arg_string, &system_principal,
102 1.1 elric "system principal name", "principal"
103 1.1 elric },
104 1.1 elric {
105 1.1 elric "lifetime", 'l', arg_string, &ticket_life,
106 1.1 elric "lifetime of system tickets", "time"
107 1.1 elric },
108 1.1 elric {
109 1.1 elric "mode", 'm', arg_string, &system_perms,
110 1.1 elric "octal mode of system cache", "mode"
111 1.1 elric },
112 1.1 elric {
113 1.1 elric "name-constraints", 'n', arg_negative_flag, &name_constraints,
114 1.2 christos "disable credentials cache name constraints", NULL
115 1.1 elric },
116 1.1 elric {
117 1.1 elric "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt,
118 1.2 christos "disable fetching krbtgt from the cache", NULL
119 1.1 elric },
120 1.1 elric {
121 1.1 elric "renewable-life", 'r', arg_string, &renew_life,
122 1.1 elric "renewable lifetime of system tickets", "time"
123 1.1 elric },
124 1.1 elric {
125 1.1 elric "socket-path", 's', arg_string, &socket_path,
126 1.1 elric "path to kcm domain socket", "path"
127 1.1 elric },
128 1.1 elric #ifdef HAVE_DOOR_CREATE
129 1.1 elric {
130 1.1 elric "door-path", 's', arg_string, &door_path,
131 1.1 elric "path to kcm door", "path"
132 1.1 elric },
133 1.1 elric #endif
134 1.1 elric {
135 1.1 elric "server", 'S', arg_string, &system_server,
136 1.1 elric "server to get system ticket for", "principal"
137 1.1 elric },
138 1.1 elric {
139 1.1 elric "keytab", 't', arg_string, &system_keytab,
140 1.1 elric "system keytab name", "keytab"
141 1.1 elric },
142 1.1 elric {
143 1.1 elric "user", 'u', arg_string, &system_user,
144 1.1 elric "system cache owner", "user"
145 1.1 elric },
146 1.2 christos { "version", 'v', arg_flag, &version_flag, NULL, NULL }
147 1.1 elric };
148 1.1 elric
149 1.1 elric static int num_args = sizeof(args) / sizeof(args[0]);
150 1.1 elric
151 1.1 elric static void
152 1.1 elric usage(int ret)
153 1.1 elric {
154 1.1 elric arg_printusage (args, num_args, NULL, "");
155 1.1 elric exit (ret);
156 1.1 elric }
157 1.1 elric
158 1.1 elric static int parse_owners(kcm_ccache ccache)
159 1.1 elric {
160 1.1 elric uid_t uid = 0;
161 1.1 elric gid_t gid = 0;
162 1.1 elric struct group *gr;
163 1.1 elric int uid_p = 0;
164 1.1 elric int gid_p = 0;
165 1.3 christos struct passwd pw, *pwd = NULL;
166 1.3 christos char pwbuf[2048];
167 1.1 elric
168 1.1 elric if (system_user != NULL) {
169 1.1 elric if (isdigit((unsigned char)system_user[0])) {
170 1.3 christos if (rk_getpwuid_r(atoi(system_user), &pw, pwbuf, sizeof(pwbuf),
171 1.3 christos &pwd) != 0)
172 1.3 christos pwd = NULL;
173 1.1 elric } else {
174 1.3 christos if (rk_getpwnam_r(system_user, &pw, pwbuf, sizeof(pwbuf),
175 1.3 christos &pwd) != 0)
176 1.3 christos pwd = NULL;
177 1.1 elric }
178 1.3 christos if (pwd == NULL) {
179 1.1 elric return errno;
180 1.1 elric }
181 1.1 elric
182 1.3 christos system_user = strdup(pwd->pw_name);
183 1.1 elric if (system_user == NULL) {
184 1.1 elric return ENOMEM;
185 1.1 elric }
186 1.1 elric
187 1.3 christos uid = pwd->pw_uid; uid_p = 1;
188 1.3 christos gid = pwd->pw_gid; gid_p = 1;
189 1.1 elric }
190 1.1 elric
191 1.1 elric if (system_group != NULL) {
192 1.1 elric if (isdigit((unsigned char)system_group[0])) {
193 1.1 elric gr = getgrgid(atoi(system_group));
194 1.1 elric } else {
195 1.1 elric gr = getgrnam(system_group);
196 1.1 elric }
197 1.1 elric if (gr == NULL) {
198 1.1 elric return errno;
199 1.1 elric }
200 1.1 elric
201 1.1 elric gid = gr->gr_gid; gid_p = 1;
202 1.1 elric }
203 1.1 elric
204 1.1 elric if (uid_p)
205 1.1 elric ccache->uid = uid;
206 1.1 elric else
207 1.1 elric ccache->uid = 0; /* geteuid() XXX */
208 1.1 elric
209 1.1 elric if (gid_p)
210 1.1 elric ccache->gid = gid;
211 1.1 elric else
212 1.1 elric ccache->gid = 0; /* getegid() XXX */
213 1.1 elric
214 1.1 elric return 0;
215 1.1 elric }
216 1.1 elric
217 1.1 elric static const char *
218 1.1 elric kcm_system_config_get_string(const char *string)
219 1.1 elric {
220 1.1 elric return krb5_config_get_string(kcm_context, NULL, "kcm",
221 1.1 elric "system_ccache", string, NULL);
222 1.1 elric }
223 1.1 elric
224 1.1 elric static krb5_error_code
225 1.1 elric ccache_init_system(void)
226 1.1 elric {
227 1.1 elric kcm_ccache ccache;
228 1.1 elric krb5_error_code ret;
229 1.1 elric
230 1.1 elric if (system_cache_name == NULL)
231 1.1 elric system_cache_name = kcm_system_config_get_string("cc_name");
232 1.1 elric
233 1.1 elric ret = kcm_ccache_new(kcm_context,
234 1.1 elric system_cache_name ? system_cache_name : "SYSTEM",
235 1.1 elric &ccache);
236 1.1 elric if (ret)
237 1.1 elric return ret;
238 1.1 elric
239 1.1 elric ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM;
240 1.1 elric ccache->flags |= KCM_FLAGS_USE_KEYTAB;
241 1.1 elric
242 1.1 elric ret = parse_owners(ccache);
243 1.1 elric if (ret)
244 1.1 elric return ret;
245 1.1 elric
246 1.1 elric ret = krb5_parse_name(kcm_context, system_principal, &ccache->client);
247 1.1 elric if (ret) {
248 1.1 elric kcm_release_ccache(kcm_context, ccache);
249 1.1 elric return ret;
250 1.1 elric }
251 1.1 elric
252 1.1 elric if (system_server == NULL)
253 1.1 elric system_server = kcm_system_config_get_string("server");
254 1.1 elric
255 1.1 elric if (system_server != NULL) {
256 1.1 elric ret = krb5_parse_name(kcm_context, system_server, &ccache->server);
257 1.1 elric if (ret) {
258 1.1 elric kcm_release_ccache(kcm_context, ccache);
259 1.1 elric return ret;
260 1.1 elric }
261 1.1 elric }
262 1.1 elric
263 1.1 elric if (system_keytab == NULL)
264 1.1 elric system_keytab = kcm_system_config_get_string("keytab_name");
265 1.1 elric
266 1.1 elric if (system_keytab != NULL) {
267 1.1 elric ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->key.keytab);
268 1.1 elric } else {
269 1.1 elric ret = krb5_kt_default(kcm_context, &ccache->key.keytab);
270 1.1 elric }
271 1.1 elric if (ret) {
272 1.1 elric kcm_release_ccache(kcm_context, ccache);
273 1.1 elric return ret;
274 1.1 elric }
275 1.1 elric
276 1.1 elric if (renew_life == NULL)
277 1.1 elric renew_life = kcm_system_config_get_string("renew_life");
278 1.1 elric
279 1.1 elric if (renew_life == NULL)
280 1.2 christos renew_life = "6 months";
281 1.1 elric
282 1.1 elric if (renew_life != NULL) {
283 1.1 elric ccache->renew_life = parse_time(renew_life, "s");
284 1.1 elric if (ccache->renew_life < 0) {
285 1.1 elric kcm_release_ccache(kcm_context, ccache);
286 1.1 elric return EINVAL;
287 1.1 elric }
288 1.1 elric }
289 1.1 elric
290 1.1 elric if (ticket_life == NULL)
291 1.1 elric ticket_life = kcm_system_config_get_string("ticket_life");
292 1.1 elric
293 1.1 elric if (ticket_life != NULL) {
294 1.1 elric ccache->tkt_life = parse_time(ticket_life, "s");
295 1.1 elric if (ccache->tkt_life < 0) {
296 1.1 elric kcm_release_ccache(kcm_context, ccache);
297 1.1 elric return EINVAL;
298 1.1 elric }
299 1.1 elric }
300 1.1 elric
301 1.1 elric if (system_perms == NULL)
302 1.1 elric system_perms = kcm_system_config_get_string("mode");
303 1.1 elric
304 1.1 elric if (system_perms != NULL) {
305 1.1 elric int mode;
306 1.1 elric
307 1.1 elric if (sscanf(system_perms, "%o", &mode) != 1)
308 1.1 elric return EINVAL;
309 1.1 elric
310 1.1 elric ccache->mode = mode;
311 1.1 elric }
312 1.1 elric
313 1.1 elric if (disallow_getting_krbtgt == -1) {
314 1.1 elric disallow_getting_krbtgt =
315 1.1 elric krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm",
316 1.1 elric "disallow-getting-krbtgt", NULL);
317 1.1 elric }
318 1.1 elric
319 1.1 elric /* enqueue default actions for credentials cache */
320 1.1 elric ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL);
321 1.1 elric
322 1.1 elric kcm_release_ccache(kcm_context, ccache); /* retained by event queue */
323 1.1 elric
324 1.1 elric return ret;
325 1.1 elric }
326 1.1 elric
327 1.1 elric void
328 1.1 elric kcm_configure(int argc, char **argv)
329 1.1 elric {
330 1.1 elric krb5_error_code ret;
331 1.2 christos int optidx = 0;
332 1.1 elric const char *p;
333 1.1 elric
334 1.2 christos while (getarg(args, num_args, argc, argv, &optidx))
335 1.2 christos warnx("error at argument `%s'", argv[optidx]);
336 1.1 elric
337 1.2 christos if (help_flag)
338 1.1 elric usage (0);
339 1.1 elric
340 1.1 elric if (version_flag) {
341 1.1 elric print_version(NULL);
342 1.1 elric exit(0);
343 1.1 elric }
344 1.1 elric
345 1.2 christos argc -= optidx;
346 1.2 christos argv += optidx;
347 1.1 elric
348 1.1 elric if (argc != 0)
349 1.1 elric usage(1);
350 1.1 elric
351 1.1 elric {
352 1.1 elric char **files;
353 1.1 elric
354 1.1 elric if(config_file == NULL)
355 1.1 elric config_file = _PATH_KCM_CONF;
356 1.1 elric
357 1.1 elric ret = krb5_prepend_config_files_default(config_file, &files);
358 1.1 elric if (ret)
359 1.1 elric krb5_err(kcm_context, 1, ret, "getting configuration files");
360 1.2 christos
361 1.1 elric ret = krb5_set_config_files(kcm_context, files);
362 1.1 elric krb5_free_config_files(files);
363 1.1 elric if(ret)
364 1.1 elric krb5_err(kcm_context, 1, ret, "reading configuration files");
365 1.1 elric }
366 1.1 elric
367 1.1 elric if(max_request_str)
368 1.1 elric max_request = parse_bytes(max_request_str, NULL);
369 1.1 elric
370 1.1 elric if(max_request == 0){
371 1.1 elric p = krb5_config_get_string (kcm_context,
372 1.1 elric NULL,
373 1.1 elric "kcm",
374 1.1 elric "max-request",
375 1.1 elric NULL);
376 1.1 elric if(p)
377 1.1 elric max_request = parse_bytes(p, NULL);
378 1.1 elric }
379 1.1 elric
380 1.1 elric if (system_principal == NULL) {
381 1.1 elric system_principal = kcm_system_config_get_string("principal");
382 1.1 elric }
383 1.1 elric
384 1.1 elric if (system_principal != NULL) {
385 1.1 elric ret = ccache_init_system();
386 1.1 elric if (ret)
387 1.1 elric krb5_err(kcm_context, 1, ret, "initializing system ccache");
388 1.1 elric }
389 1.1 elric
390 1.1 elric if(detach_from_console == -1)
391 1.1 elric detach_from_console = krb5_config_get_bool_default(kcm_context, NULL,
392 1.2 christos FALSE,
393 1.1 elric "kcm",
394 1.1 elric "detach", NULL);
395 1.1 elric kcm_openlog();
396 1.1 elric if(max_request == 0)
397 1.1 elric max_request = 64 * 1024;
398 1.1 elric }
399 1.1 elric
400