2 * Adapted in part from MIT Kerberos 5-1.2.1 slave/kprop.c and from
3 * http://docs.sun.com/?p=/doc/816-1331/6m7oo9sms&a=view
5 * Copyright (c) 2002-2004 The Regents of the University of Michigan.
8 * Andy Adamson <andros@umich.edu>
9 * J. Bruce Fields <bfields@umich.edu>
10 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Kevin Coffman <kwc@umich.edu>
17 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
18 * All Rights Reserved.
20 * Export of this software from the United States of America may
21 * require a specific license from the United States Government.
22 * It is the responsibility of any person or organization contemplating
23 * export to obtain such a license before exporting.
25 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
26 * distribute this software and its documentation for any purpose and
27 * without fee is hereby granted, provided that the above copyright
28 * notice appear in all copies and that both that copyright notice and
29 * this permission notice appear in supporting documentation, and that
30 * the name of M.I.T. not be used in advertising or publicity pertaining
31 * to distribution of the software without specific, written prior
32 * permission. Furthermore if you modify this software you must label
33 * your software as modified software and not distribute it in such a
34 * fashion that it might be confused with the original M.I.T. software.
35 * M.I.T. makes no representations about the suitability of
36 * this software for any purpose. It is provided "as is" without express
37 * or implied warranty.
41 * Copyright 1994 by OpenVision Technologies, Inc.
43 * Permission to use, copy, modify, distribute, and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appears in all copies and
46 * that both that copyright notice and this permission notice appear in
47 * supporting documentation, and that the name of OpenVision not be used
48 * in advertising or publicity pertaining to distribution of the software
49 * without specific, written prior permission. OpenVision makes no
50 * representations about the suitability of this software for any
51 * purpose. It is provided "as is" without express or implied warranty.
53 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
54 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
55 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
56 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
57 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
58 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
59 * PERFORMANCE OF THIS SOFTWARE.
64 Copyright (c) 2004 The Regents of the University of Michigan.
67 Redistribution and use in source and binary forms, with or without
68 modification, are permitted provided that the following conditions
71 1. Redistributions of source code must retain the above copyright
72 notice, this list of conditions and the following disclaimer.
73 2. Redistributions in binary form must reproduce the above copyright
74 notice, this list of conditions and the following disclaimer in the
75 documentation and/or other materials provided with the distribution.
76 3. Neither the name of the University nor the names of its
77 contributors may be used to endorse or promote products derived
78 from this software without specific prior written permission.
80 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
81 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
82 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
83 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
85 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
86 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
87 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
88 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
89 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
98 #include <sys/param.h>
100 #include <sys/stat.h>
101 #include <sys/socket.h>
102 #include <arpa/inet.h>
110 #include <gssapi/gssapi.h>
111 #ifdef USE_PRIVATE_KRB5_FUNCTIONS
112 #include <gssapi/gssapi_krb5.h>
115 #include <rpc/auth_gss.h>
118 #include "err_util.h"
119 #include "gss_util.h"
120 #include "gss_oids.h"
121 #include "krb5_util.h"
123 /* Global list of principals/cache file names for machine credentials */
124 struct gssd_k5_kt_princ *gssd_k5_kt_princ_list = NULL;
126 /*==========================*/
127 /*=== Internal routines ===*/
128 /*==========================*/
130 static int select_krb5_ccache(const struct dirent *d);
131 static int gssd_find_existing_krb5_ccache(uid_t uid, struct dirent **d);
132 static int gssd_get_single_krb5_cred(krb5_context context,
133 krb5_keytab kt, struct gssd_k5_kt_princ *ple);
134 static int gssd_have_realm_ple(void *realm);
135 static int gssd_process_krb5_keytab(krb5_context context, krb5_keytab kt,
139 * Called from the scandir function to weed out potential krb5
140 * credentials cache files
143 * 0 => don't select this one
144 * 1 => select this one
147 select_krb5_ccache(const struct dirent *d)
150 * Note: We used to check d->d_type for DT_REG here,
151 * but apparenlty reiser4 always has DT_UNKNOWN.
152 * Check for IS_REG after stat() call instead.
154 if (strstr(d->d_name, GSSD_DEFAULT_CRED_PREFIX))
161 * Look in the ccachedir for files that look like they
162 * are Kerberos Credential Cache files for a given UID. Return
163 * non-zero and the dirent pointer for the entry most likely to be
164 * what we want. Otherwise, return zero and no dirent pointer.
165 * The caller is responsible for freeing the dirent if one is returned.
168 * 0 => could not find an existing entry
169 * 1 => found an existing entry
172 gssd_find_existing_krb5_ccache(uid_t uid, struct dirent **d)
174 struct dirent **namelist;
178 struct dirent *best_match_dir = NULL;
179 struct stat best_match_stat, tmp_stat;
182 n = scandir(ccachedir, &namelist, select_krb5_ccache, 0);
184 perror("scandir looking for krb5 credentials caches");
188 char fullstring[128];
190 snprintf(substring, sizeof(substring), "_%d_", uid);
191 snprintf(fullstring, sizeof(fullstring), "_%d", uid);
192 for (i = 0; i < n; i++) {
193 printerr(3, "CC file '%s' being considered\n",
194 namelist[i]->d_name);
195 if (strstr(namelist[i]->d_name, substring) ||
196 !strcmp(namelist[i]->d_name, fullstring)) {
197 snprintf(statname, sizeof(statname),
199 namelist[i]->d_name);
200 if (stat(statname, &tmp_stat)) {
201 printerr(0, "Error doing stat "
206 if (!S_ISREG(tmp_stat.st_mode)) {
207 printerr(3, "File '%s' is not "
212 printerr(3, "CC file '%s' matches "
213 "name check and has "
217 /* if more than one match is found,
218 * return the most recent (the one
219 * with the latest mtime),
220 * and don't free the dirent */
222 best_match_dir = namelist[i];
223 best_match_stat = tmp_stat;
228 * If the current match has
229 * an mtime later than the
230 * one we are looking at,
231 * then use the current match.
232 * Otherwise, we still have
235 if (tmp_stat.st_mtime >
236 best_match_stat.st_mtime) {
237 free(best_match_dir);
238 best_match_dir = namelist[i];
239 best_match_stat = tmp_stat;
244 printerr(3, "CC file '%s' is our "
245 "current best match "
246 "with mtime of %u\n",
247 best_match_dir->d_name,
248 best_match_stat.st_mtime);
264 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
266 * this routine obtains a credentials handle via gss_acquire_cred()
267 * then calls gss_krb5_set_allowable_enctypes() to limit the encryption
270 * XXX Should call some function to determine the enctypes supported
271 * by the kernel. (Only need to do that once!)
275 * -1 => there was an error
279 limit_krb5_enctypes(struct rpc_gss_sec *sec, uid_t uid)
281 u_int maj_stat, min_stat;
283 gss_OID_set_desc desired_mechs;
284 krb5_enctype enctypes[] = { ENCTYPE_DES_CBC_CRC };
285 int num_enctypes = sizeof(enctypes) / sizeof(enctypes[0]);
287 /* We only care about getting a krb5 cred */
288 desired_mechs.count = 1;
289 desired_mechs.elements = &krb5oid;
291 maj_stat = gss_acquire_cred(&min_stat, NULL, 0,
292 &desired_mechs, GSS_C_INITIATE,
295 if (maj_stat != GSS_S_COMPLETE) {
296 pgsserr("gss_acquire_cred",
297 maj_stat, min_stat, &krb5oid);
301 maj_stat = gss_set_allowable_enctypes(&min_stat, credh, &krb5oid,
302 num_enctypes, &enctypes);
303 if (maj_stat != GSS_S_COMPLETE) {
304 pgsserr("gss_set_allowable_enctypes",
305 maj_stat, min_stat, &krb5oid);
312 #endif /* HAVE_SET_ALLOWABLE_ENCTYPES */
315 * Obtain credentials via a key in the keytab given
316 * a keytab handle and a gssd_k5_kt_princ structure.
317 * Checks to see if current credentials are expired,
318 * if not, uses the keytab to obtain new credentials.
321 * 0 => success (or credentials have not expired)
325 gssd_get_single_krb5_cred(krb5_context context,
327 struct gssd_k5_kt_princ *ple)
329 krb5_get_init_creds_opt options;
331 krb5_ccache ccache = NULL;
332 char kt_name[BUFSIZ];
333 char cc_name[BUFSIZ];
335 time_t now = time(0);
337 memset(&my_creds, 0, sizeof(my_creds));
339 if (ple->ccname && ple->endtime > now) {
340 printerr(2, "INFO: Credentials in CC '%s' are good until %d\n",
341 ple->ccname, ple->endtime);
346 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
347 printerr(0, "ERROR: Unable to get keytab name in "
348 "gssd_get_single_krb5_cred\n");
352 krb5_get_init_creds_opt_init(&options);
353 krb5_get_init_creds_opt_set_address_list(&options, NULL);
355 #ifdef TEST_SHORT_LIFETIME
356 /* set a short lifetime (for debugging only!) */
357 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
358 krb5_get_init_creds_opt_set_tkt_life(&options, 5*60);
360 if ((code = krb5_get_init_creds_keytab(context, &my_creds, ple->princ,
361 kt, 0, NULL, &options))) {
363 if ((krb5_unparse_name(context, ple->princ, &pname))) {
366 printerr(0, "WARNING: %s while getting initial ticket for "
367 "principal '%s' from keytab '%s'\n",
369 pname ? pname : "<unparsable>", kt_name);
371 if (pname) krb5_free_unparsed_name(context, pname);
373 if (pname) free(pname);
379 * Initialize cache file which we're going to be using
382 snprintf(cc_name, sizeof(cc_name), "FILE:%s/%s%s_%s",
383 GSSD_DEFAULT_CRED_DIR, GSSD_DEFAULT_CRED_PREFIX,
384 GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm);
385 ple->endtime = my_creds.times.endtime;
386 ple->ccname = strdup(cc_name);
387 if (ple->ccname == NULL) {
388 printerr(0, "ERROR: no storage to duplicate credentials "
393 if ((code = krb5_cc_resolve(context, cc_name, &ccache))) {
394 printerr(0, "ERROR: %s while opening credential cache '%s'\n",
395 error_message(code), cc_name);
398 if ((code = krb5_cc_initialize(context, ccache, ple->princ))) {
399 printerr(0, "ERROR: %s while initializing credential "
400 "cache '%s'\n", error_message(code), cc_name);
403 if ((code = krb5_cc_store_cred(context, ccache, &my_creds))) {
404 printerr(0, "ERROR: %s while storing credentials in '%s'\n",
405 error_message(code), cc_name);
410 printerr(1, "Using (machine) credentials cache: '%s'\n", cc_name);
413 krb5_cc_close(context, ccache);
414 krb5_free_cred_contents(context, &my_creds);
419 * Determine if we already have a ple for the given realm
422 * 0 => no ple found for given realm
423 * 1 => found ple for given realm
426 gssd_have_realm_ple(void *r)
428 struct gssd_k5_kt_princ *ple;
430 krb5_data *realm = (krb5_data *)r;
432 char *realm = (char *)r;
435 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
437 if ((realm->length == strlen(ple->realm)) &&
438 (strncmp(realm->data, ple->realm, realm->length) == 0)) {
440 if (strcmp(realm, ple->realm) == 0) {
449 * Process the given keytab file and create a list of principals we
450 * might use to perform mount operations.
457 gssd_process_krb5_keytab(krb5_context context, krb5_keytab kt, char *kt_name)
459 krb5_kt_cursor cursor;
460 krb5_keytab_entry kte;
461 krb5_error_code code;
462 struct gssd_k5_kt_princ *ple;
466 * Look through each entry in the keytab file and determine
467 * if we might want to use it later to do a mount. If so,
468 * save info in the global principal list
469 * (gssd_k5_kt_princ_list).
470 * Note: (ple == principal list entry)
472 if ((code = krb5_kt_start_seq_get(context, kt, &cursor))) {
473 printerr(0, "ERROR: %s while beginning keytab scan "
475 error_message(code), kt_name);
480 while ((code = krb5_kt_next_entry(context, kt, &kte, &cursor)) == 0) {
482 if ((code = krb5_unparse_name(context, kte.principal,
484 printerr(0, "WARNING: Skipping keytab entry because "
485 "we failed to unparse principal name: %s\n",
486 error_message(code));
489 printerr(2, "Processing keytab entry for principal '%s'\n",
492 if ( (kte.principal->data[0].length == GSSD_SERVICE_NAME_LEN) &&
493 (strncmp(kte.principal->data[0].data, GSSD_SERVICE_NAME,
494 GSSD_SERVICE_NAME_LEN) == 0) &&
496 if ( (strlen(kte.principal->name.name_string.val[0]) == GSSD_SERVICE_NAME_LEN) &&
497 (strncmp(kte.principal->name.name_string.val[0], GSSD_SERVICE_NAME,
498 GSSD_SERVICE_NAME_LEN) == 0) &&
501 (!gssd_have_realm_ple((void *)&kte.principal->realm)) ) {
502 printerr(2, "We will use this entry (%s)\n", pname);
503 ple = malloc(sizeof(struct gssd_k5_kt_princ));
505 printerr(0, "ERROR: could not allocate storage "
506 "for principal list entry\n");
508 krb5_free_unparsed_name(context, pname);
515 /* These will be filled in later */
521 strndup(kte.principal->realm.data,
522 kte.principal->realm.length))
524 strdup(kte.principal->realm))
527 printerr(0, "ERROR: %s while copying realm to "
528 "principal list entry\n",
529 "not enough memory");
531 krb5_free_unparsed_name(context, pname);
538 if ((code = krb5_copy_principal(context,
539 kte.principal, &ple->princ))) {
540 printerr(0, "ERROR: %s while copying principal "
541 "to principal list entry\n",
542 error_message(code));
544 krb5_free_unparsed_name(context, pname);
551 if (gssd_k5_kt_princ_list == NULL)
552 gssd_k5_kt_princ_list = ple;
554 ple->next = gssd_k5_kt_princ_list;
555 gssd_k5_kt_princ_list = ple;
559 printerr(2, "We will NOT use this entry (%s)\n",
563 krb5_free_unparsed_name(context, pname);
569 if ((code = krb5_kt_end_seq_get(context, kt, &cursor))) {
570 printerr(0, "WARNING: %s while ending keytab scan for "
572 error_message(code), kt_name);
581 * Depending on the version of Kerberos, we either need to use
582 * a private function, or simply set the environment variable.
585 gssd_set_krb5_ccache_name(char *ccname)
587 #ifdef USE_GSS_KRB5_CCACHE_NAME
588 u_int maj_stat, min_stat;
590 printerr(2, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
592 maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
593 if (maj_stat != GSS_S_COMPLETE) {
594 printerr(0, "WARNING: gss_krb5_ccache_name with "
595 "name '%s' failed (%s)\n",
596 ccname, error_message(min_stat));
600 * Set the KRB5CCNAME environment variable to tell the krb5 code
601 * which credentials cache to use. (Instead of using the private
602 * function above for which there is no generic gssapi
605 printerr(2, "using environment variable to select krb5 ccache %s\n",
607 setenv("KRB5CCNAME", ccname, 1);
611 /*==========================*/
612 /*=== External routines ===*/
613 /*==========================*/
616 * Attempt to find the best match for a credentials cache file
617 * given only a UID. We really need more information, but we
618 * do the best we can.
624 gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername)
626 char buf[MAX_NETOBJ_SZ];
629 printerr(2, "getting credentials for client with uid %u for "
630 "server %s\n", uid, servername);
631 memset(buf, 0, sizeof(buf));
632 if (gssd_find_existing_krb5_ccache(uid, &d)) {
633 snprintf(buf, sizeof(buf), "FILE:%s/%s",
634 ccachedir, d->d_name);
638 snprintf(buf, sizeof(buf), "FILE:%s/%s%u",
639 ccachedir, GSSD_DEFAULT_CRED_PREFIX, uid);
640 printerr(2, "using %s as credentials cache for client with "
641 "uid %u for server %s\n", buf, uid, servername);
642 gssd_set_krb5_ccache_name(buf);
646 * Let the gss code know where to find the machine credentials ccache.
652 gssd_setup_krb5_machine_gss_ccache(char *ccname)
654 printerr(2, "using %s as credentials cache for machine creds\n",
656 gssd_set_krb5_ccache_name(ccname);
660 * The first time through this routine, go through the keytab and
661 * determine which keys we will try to use as machine credentials.
662 * Every time through this routine, try to obtain credentials using
663 * the keytab entries selected the first time through.
666 * 0 => obtained one or more credentials
672 gssd_refresh_krb5_machine_creds(void)
674 krb5_context context = NULL;
675 krb5_keytab kt = NULL;;
676 krb5_error_code code;
678 struct gssd_k5_kt_princ *ple;
680 static int processed_keytab = 0;
683 code = krb5_init_context(&context);
685 printerr(0, "ERROR: %s while initializing krb5 in "
686 "gssd_refresh_krb5_machine_creds\n",
687 error_message(code));
692 printerr(1, "Using keytab file '%s'\n", keytabfile);
694 if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
695 printerr(0, "ERROR: %s while resolving keytab '%s'\n",
696 error_message(code), keytabfile);
700 /* Only go through the keytab file once. Only print messages once. */
701 if (gssd_k5_kt_princ_list == NULL && !processed_keytab) {
702 processed_keytab = 1;
703 gssd_process_krb5_keytab(context, kt, keytabfile);
704 if (gssd_k5_kt_princ_list == NULL) {
705 printerr(0, "ERROR: No usable keytab entries found in "
706 "keytab '%s'\n", keytabfile);
707 printerr(0, "Do you have a valid keytab entry for "
708 "%s/<your.host>@<YOUR.REALM> in "
709 "keytab file %s ?\n",
710 GSSD_SERVICE_NAME, keytabfile);
711 printerr(0, "Continuing without (machine) credentials "
712 "- nfs4 mounts with Kerberos will fail\n");
717 * If we don't have any keytab entries we liked, then we have a problem
719 if (gssd_k5_kt_princ_list == NULL) {
725 * Now go through the list of saved entries and get initial
726 * credentials for them (We can't do this while making the
727 * list because it messes up the keytab iteration cursor
728 * when we use the keytab to get credentials.)
730 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
731 if ((gssd_get_single_krb5_cred(context, kt, ple)) == 0) {
736 printerr(0, "ERROR: No usable machine credentials obtained\n");
742 if (kt) krb5_kt_close(context, kt);
743 krb5_free_context(context);
750 * Return an array of pointers to names of credential cache files
751 * which can be used to try to create gss contexts with a server.
754 * 0 => list is attached
758 gssd_get_krb5_machine_cred_list(char ***list)
762 int listsize = listinc;
765 struct gssd_k5_kt_princ *ple;
769 *list = (char **) NULL;
771 /* Refresh machine credentials */
772 if ((retval = gssd_refresh_krb5_machine_creds())) {
776 if ((l = (char **) malloc(listsize * sizeof(char *))) == NULL) {
781 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
783 if (i + 1 > listsize) {
786 realloc(l, listsize * sizeof(char *));
792 if ((l[i++] = strdup(ple->ccname)) == NULL) {
809 * Frees the list of names returned in get_krb5_machine_cred_list()
812 gssd_free_krb5_machine_cred_list(char **list)
818 for (n = list; n && *n; n++) {
825 * Called upon exit. Destroys machine credentials.
828 gssd_destroy_krb5_machine_creds(void)
830 krb5_context context;
831 krb5_error_code code = 0;
833 struct gssd_k5_kt_princ *ple;
835 code = krb5_init_context(&context);
837 printerr(0, "ERROR: %s while initializing krb5\n",
838 error_message(code));
842 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
845 if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
846 printerr(0, "WARNING: %s while resolving credential "
847 "cache '%s' for destruction\n",
848 error_message(code), ple->ccname);
852 if ((code = krb5_cc_destroy(context, ccache))) {
853 printerr(0, "WARNING: %s while destroying credential "
855 error_message(code), ple->ccname);
859 krb5_free_context(context);