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