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