lorikeet-heimdal: rebase-lorikeet: Explicitly use bash.
[metze/heimdal/wip.git] / kcm / config.c
1 /*
2  * Copyright (c) 2005, PADL Software Pty Ltd.
3  * All rights reserved.
4  *
5  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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.
17  *
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.
21  *
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.
33  */
34
35 #include "kcm_locl.h"
36 #include <getarg.h>
37 #include <parse_bytes.h>
38
39 static const char *config_file; /* location of kcm config file */
40
41 size_t max_request = 0;         /* maximal size of a request */
42 char *socket_path = NULL;
43
44 static char *max_request_str;   /* `max_request' as a string */
45
46 int detach_from_console = -1;
47 int daemon_child = -1;
48
49 static const char *system_cache_name = NULL;
50 static const char *system_keytab = NULL;
51 static const char *system_principal = NULL;
52 static const char *system_server = NULL;
53 static const char *system_perms = NULL;
54 static const char *system_user = NULL;
55 static const char *system_group = NULL;
56
57 static const char *renew_life = NULL;
58 static const char *ticket_life = NULL;
59
60 int launchd_flag = 0;
61 int disallow_getting_krbtgt = 0;
62 int name_constraints = -1;
63
64 static int help_flag;
65 static int version_flag;
66
67 static struct getargs args[] = {
68     {
69         "cache-name",   0,      arg_string,     &system_cache_name,
70         "system cache name", "cachename"
71     },
72     {
73         "config-file",  'c',    arg_string,     &config_file,
74         "location of config file",      "file"
75     },
76     {
77         "group",        'g',    arg_string,     &system_group,
78         "system cache group",   "group"
79     },
80     {
81         "max-request",  0,      arg_string, &max_request,
82         "max size for a kcm-request", "size"
83     },
84     {
85         "launchd",      0,      arg_flag, &launchd_flag,
86         "when in use by launchd", NULL
87     },
88     {
89         "detach",       0 ,      arg_flag, &detach_from_console,
90         "detach from console", NULL
91     },
92     {
93         "daemon-child",       0 ,      arg_integer, &daemon_child,
94         "private argument, do not use", NULL
95     },
96     {   "help",         'h',    arg_flag,   &help_flag, NULL, NULL },
97     {
98         "system-principal",     'k',    arg_string,     &system_principal,
99         "system principal name",        "principal"
100     },
101     {
102         "lifetime",     'l', arg_string, &ticket_life,
103         "lifetime of system tickets", "time"
104     },
105     {
106         "mode",         'm', arg_string, &system_perms,
107         "octal mode of system cache", "mode"
108     },
109     {
110         "name-constraints",     'n', arg_negative_flag, &name_constraints,
111         "disable credentials cache name constraints", NULL
112     },
113     {
114         "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt,
115         "disable fetching krbtgt from the cache", NULL
116     },
117     {
118         "renewable-life",       'r', arg_string, &renew_life,
119         "renewable lifetime of system tickets", "time"
120     },
121     {
122         "socket-path",          's', arg_string, &socket_path,
123         "path to kcm domain socket", "path"
124     },
125     {
126         "server",               'S', arg_string, &system_server,
127         "server to get system ticket for", "principal"
128     },
129     {
130         "keytab",       't',    arg_string,     &system_keytab,
131         "system keytab name",   "keytab"
132     },
133     {
134         "user",         'u',    arg_string,     &system_user,
135         "system cache owner",   "user"
136     },
137     {   "version",      'v',    arg_flag,   &version_flag, NULL, NULL }
138 };
139
140 static int num_args = sizeof(args) / sizeof(args[0]);
141
142 static void
143 usage(int ret)
144 {
145     arg_printusage (args, num_args, NULL, "");
146     exit (ret);
147 }
148
149 static int parse_owners(kcm_ccache ccache)
150 {
151     uid_t uid = 0;
152     gid_t gid = 0;
153     struct passwd *pw;
154     struct group *gr;
155     int uid_p = 0;
156     int gid_p = 0;
157
158     if (system_user != NULL) {
159         if (isdigit((unsigned char)system_user[0])) {
160             pw = getpwuid(atoi(system_user));
161         } else {
162             pw = getpwnam(system_user);
163         }
164         if (pw == NULL) {
165             return errno;
166         }
167
168         system_user = strdup(pw->pw_name);
169         if (system_user == NULL) {
170             return ENOMEM;
171         }
172
173         uid = pw->pw_uid; uid_p = 1;
174         gid = pw->pw_gid; gid_p = 1;
175     }
176
177     if (system_group != NULL) {
178         if (isdigit((unsigned char)system_group[0])) {
179             gr = getgrgid(atoi(system_group));
180         } else {
181             gr = getgrnam(system_group);
182         }
183         if (gr == NULL) {
184             return errno;
185         }
186
187         gid = gr->gr_gid; gid_p = 1;
188     }
189
190     if (uid_p)
191         ccache->uid = uid;
192     else
193         ccache->uid = 0; /* geteuid() XXX */
194
195     if (gid_p)
196         ccache->gid = gid;
197     else
198         ccache->gid = 0; /* getegid() XXX */
199
200     return 0;
201 }
202
203 static const char *
204 kcm_system_config_get_string(const char *string)
205 {
206     return krb5_config_get_string(kcm_context, NULL, "kcm",
207                                   "system_ccache", string, NULL);
208 }
209
210 static krb5_error_code
211 ccache_init_system(void)
212 {
213     kcm_ccache ccache;
214     krb5_error_code ret;
215
216     if (system_cache_name == NULL)
217         system_cache_name = kcm_system_config_get_string("cc_name");
218
219     ret = kcm_ccache_new(kcm_context,
220                          system_cache_name ? system_cache_name : "SYSTEM",
221                          &ccache);
222     if (ret)
223         return ret;
224
225     ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM;
226     ccache->flags |= KCM_FLAGS_USE_KEYTAB;
227
228     ret = parse_owners(ccache);
229     if (ret)
230         return ret;
231
232     ret = krb5_parse_name(kcm_context, system_principal, &ccache->client);
233     if (ret) {
234         kcm_release_ccache(kcm_context, ccache);
235         return ret;
236     }
237
238     if (system_server == NULL)
239         system_server = kcm_system_config_get_string("server");
240
241     if (system_server != NULL) {
242         ret = krb5_parse_name(kcm_context, system_server, &ccache->server);
243         if (ret) {
244             kcm_release_ccache(kcm_context, ccache);
245             return ret;
246         }
247     }
248
249     if (system_keytab == NULL)
250         system_keytab = kcm_system_config_get_string("keytab_name");
251
252     if (system_keytab != NULL) {
253         ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->key.keytab);
254     } else {
255         ret = krb5_kt_default(kcm_context, &ccache->key.keytab);
256     }
257     if (ret) {
258         kcm_release_ccache(kcm_context, ccache);
259         return ret;
260     }
261
262     if (renew_life == NULL)
263         renew_life = kcm_system_config_get_string("renew_life");
264
265     if (renew_life == NULL)
266         renew_life = "6 months";
267
268     if (renew_life != NULL) {
269         ccache->renew_life = parse_time(renew_life, "s");
270         if (ccache->renew_life < 0) {
271             kcm_release_ccache(kcm_context, ccache);
272             return EINVAL;
273         }
274     }
275
276     if (ticket_life == NULL)
277         ticket_life = kcm_system_config_get_string("ticket_life");
278
279     if (ticket_life != NULL) {
280         ccache->tkt_life = parse_time(ticket_life, "s");
281         if (ccache->tkt_life < 0) {
282             kcm_release_ccache(kcm_context, ccache);
283             return EINVAL;
284         }
285     }
286
287     if (system_perms == NULL)
288         system_perms = kcm_system_config_get_string("mode");
289
290     if (system_perms != NULL) {
291         int mode;
292
293         if (sscanf(system_perms, "%o", &mode) != 1)
294             return EINVAL;
295
296         ccache->mode = mode;
297     }
298
299     if (disallow_getting_krbtgt == -1) {
300         disallow_getting_krbtgt =
301             krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm",
302                                          "disallow-getting-krbtgt", NULL);
303     }
304
305     /* enqueue default actions for credentials cache */
306     ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL);
307
308     kcm_release_ccache(kcm_context, ccache); /* retained by event queue */
309
310     return ret;
311 }
312
313 void
314 kcm_configure(int argc, char **argv)
315 {
316     krb5_error_code ret;
317     int optidx = 0;
318     const char *p;
319
320     while (getarg(args, num_args, argc, argv, &optidx))
321         warnx("error at argument `%s'", argv[optidx]);
322
323     if (help_flag)
324         usage (0);
325
326     if (version_flag) {
327         print_version(NULL);
328         exit(0);
329     }
330
331     argc -= optidx;
332     argv += optidx;
333
334     if (argc != 0)
335         usage(1);
336
337     {
338         char **files;
339
340         if(config_file == NULL)
341             config_file = _PATH_KCM_CONF;
342
343         ret = krb5_prepend_config_files_default(config_file, &files);
344         if (ret)
345             krb5_err(kcm_context, 1, ret, "getting configuration files");
346
347         ret = krb5_set_config_files(kcm_context, files);
348         krb5_free_config_files(files);
349         if(ret)
350             krb5_err(kcm_context, 1, ret, "reading configuration files");
351     }
352
353     if(max_request_str)
354         max_request = parse_bytes(max_request_str, NULL);
355
356     if(max_request == 0){
357         p = krb5_config_get_string (kcm_context,
358                                     NULL,
359                                     "kcm",
360                                     "max-request",
361                                     NULL);
362         if(p)
363             max_request = parse_bytes(p, NULL);
364     }
365
366     if (system_principal == NULL) {
367         system_principal = kcm_system_config_get_string("principal");
368     }
369
370     if (system_principal != NULL) {
371         ret = ccache_init_system();
372         if (ret)
373             krb5_err(kcm_context, 1, ret, "initializing system ccache");
374     }
375
376     if(detach_from_console == -1)
377         detach_from_console = krb5_config_get_bool_default(kcm_context, NULL,
378                                                            FALSE,
379                                                            "kcm",
380                                                            "detach", NULL);
381     kcm_openlog();
382     if(max_request == 0)
383         max_request = 64 * 1024;
384 }
385