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;
181 memset(&best_match_stat, 0, sizeof(best_match_stat));
183 n = scandir(ccachedir, &namelist, select_krb5_ccache, 0);
185 perror("scandir looking for krb5 credentials caches");
189 for (i = 0; i < n; i++) {
190 printerr(3, "CC file '%s' being considered\n",
191 namelist[i]->d_name);
192 snprintf(statname, sizeof(statname),
193 "%s/%s", ccachedir, namelist[i]->d_name);
194 if (lstat(statname, &tmp_stat)) {
195 printerr(0, "Error doing stat on file '%s'\n",
200 /* Only pick caches owned by the user (uid) */
201 if (tmp_stat.st_uid != uid) {
202 printerr(3, "'%s' owned by %u, not %u\n",
203 statname, tmp_stat.st_uid, uid);
207 if (!S_ISREG(tmp_stat.st_mode)) {
208 printerr(3, "'%s' is not a regular file\n",
213 printerr(3, "CC file '%s' matches owner check and has "
215 namelist[i]->d_name, tmp_stat.st_mtime);
217 * if more than one match is found, return the most
218 * recent (the one with the latest mtime), and
219 * don't free the dirent
222 best_match_dir = namelist[i];
223 best_match_stat = tmp_stat;
228 * If the current match has an mtime later
229 * than the one we are looking at, then use
230 * the current match. Otherwise, we still
231 * have the best match.
233 if (tmp_stat.st_mtime >
234 best_match_stat.st_mtime) {
235 free(best_match_dir);
236 best_match_dir = namelist[i];
237 best_match_stat = tmp_stat;
242 printerr(3, "CC file '%s' is our "
243 "current best match "
244 "with mtime of %u\n",
245 best_match_dir->d_name,
246 best_match_stat.st_mtime);
259 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
261 * this routine obtains a credentials handle via gss_acquire_cred()
262 * then calls gss_krb5_set_allowable_enctypes() to limit the encryption
265 * XXX Should call some function to determine the enctypes supported
266 * by the kernel. (Only need to do that once!)
270 * -1 => there was an error
274 limit_krb5_enctypes(struct rpc_gss_sec *sec, uid_t uid)
276 u_int maj_stat, min_stat;
278 gss_OID_set_desc desired_mechs;
279 krb5_enctype enctypes[] = { ENCTYPE_DES_CBC_CRC };
280 int num_enctypes = sizeof(enctypes) / sizeof(enctypes[0]);
282 /* We only care about getting a krb5 cred */
283 desired_mechs.count = 1;
284 desired_mechs.elements = &krb5oid;
286 maj_stat = gss_acquire_cred(&min_stat, NULL, 0,
287 &desired_mechs, GSS_C_INITIATE,
290 if (maj_stat != GSS_S_COMPLETE) {
291 pgsserr("gss_acquire_cred",
292 maj_stat, min_stat, &krb5oid);
296 maj_stat = gss_set_allowable_enctypes(&min_stat, credh, &krb5oid,
297 num_enctypes, &enctypes);
298 if (maj_stat != GSS_S_COMPLETE) {
299 pgsserr("gss_set_allowable_enctypes",
300 maj_stat, min_stat, &krb5oid);
301 gss_release_cred(&min_stat, &credh);
308 #endif /* HAVE_SET_ALLOWABLE_ENCTYPES */
311 * Obtain credentials via a key in the keytab given
312 * a keytab handle and a gssd_k5_kt_princ structure.
313 * Checks to see if current credentials are expired,
314 * if not, uses the keytab to obtain new credentials.
317 * 0 => success (or credentials have not expired)
321 gssd_get_single_krb5_cred(krb5_context context,
323 struct gssd_k5_kt_princ *ple)
325 krb5_get_init_creds_opt options;
327 krb5_ccache ccache = NULL;
328 char kt_name[BUFSIZ];
329 char cc_name[BUFSIZ];
331 time_t now = time(0);
334 memset(&my_creds, 0, sizeof(my_creds));
336 if (ple->ccname && ple->endtime > now) {
337 printerr(2, "INFO: Credentials in CC '%s' are good until %d\n",
338 ple->ccname, ple->endtime);
343 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
344 printerr(0, "ERROR: Unable to get keytab name in "
345 "gssd_get_single_krb5_cred\n");
349 krb5_get_init_creds_opt_init(&options);
350 krb5_get_init_creds_opt_set_address_list(&options, NULL);
352 #ifdef TEST_SHORT_LIFETIME
353 /* set a short lifetime (for debugging only!) */
354 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
355 krb5_get_init_creds_opt_set_tkt_life(&options, 5*60);
357 if ((code = krb5_get_init_creds_keytab(context, &my_creds, ple->princ,
358 kt, 0, NULL, &options))) {
360 if ((krb5_unparse_name(context, ple->princ, &pname))) {
363 printerr(0, "WARNING: %s while getting initial ticket for "
364 "principal '%s' from keytab '%s'\n",
366 pname ? pname : "<unparsable>", kt_name);
368 if (pname) krb5_free_unparsed_name(context, pname);
370 if (pname) free(pname);
376 * Initialize cache file which we're going to be using
380 cache_type = "MEMORY";
383 snprintf(cc_name, sizeof(cc_name), "%s:%s/%s%s_%s",
385 GSSD_DEFAULT_CRED_DIR, GSSD_DEFAULT_CRED_PREFIX,
386 GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm);
387 ple->endtime = my_creds.times.endtime;
388 ple->ccname = strdup(cc_name);
389 if (ple->ccname == NULL) {
390 printerr(0, "ERROR: no storage to duplicate credentials "
395 if ((code = krb5_cc_resolve(context, cc_name, &ccache))) {
396 printerr(0, "ERROR: %s while opening credential cache '%s'\n",
397 error_message(code), cc_name);
400 if ((code = krb5_cc_initialize(context, ccache, ple->princ))) {
401 printerr(0, "ERROR: %s while initializing credential "
402 "cache '%s'\n", error_message(code), cc_name);
405 if ((code = krb5_cc_store_cred(context, ccache, &my_creds))) {
406 printerr(0, "ERROR: %s while storing credentials in '%s'\n",
407 error_message(code), cc_name);
412 printerr(1, "Using (machine) credentials cache: '%s'\n", cc_name);
415 krb5_cc_close(context, ccache);
416 krb5_free_cred_contents(context, &my_creds);
421 * Determine if we already have a ple for the given realm
424 * 0 => no ple found for given realm
425 * 1 => found ple for given realm
428 gssd_have_realm_ple(void *r)
430 struct gssd_k5_kt_princ *ple;
432 krb5_data *realm = (krb5_data *)r;
434 char *realm = (char *)r;
437 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
439 if ((realm->length == strlen(ple->realm)) &&
440 (strncmp(realm->data, ple->realm, realm->length) == 0)) {
442 if (strcmp(realm, ple->realm) == 0) {
451 * Process the given keytab file and create a list of principals we
452 * might use as machine credentials.
459 gssd_process_krb5_keytab(krb5_context context, krb5_keytab kt, char *kt_name)
461 krb5_kt_cursor cursor;
462 krb5_keytab_entry kte;
463 krb5_error_code code;
464 struct gssd_k5_kt_princ *ple;
468 * Look through each entry in the keytab file and determine
469 * if we might want to use it as machine credentials. If so,
470 * save info in the global principal list (gssd_k5_kt_princ_list).
471 * Note: (ple == principal list entry)
473 if ((code = krb5_kt_start_seq_get(context, kt, &cursor))) {
474 printerr(0, "ERROR: %s while beginning keytab scan "
476 error_message(code), kt_name);
481 while ((code = krb5_kt_next_entry(context, kt, &kte, &cursor)) == 0) {
483 if ((code = krb5_unparse_name(context, kte.principal,
485 printerr(0, "WARNING: Skipping keytab entry because "
486 "we failed to unparse principal name: %s\n",
487 error_message(code));
488 krb5_kt_free_entry(context, &kte);
491 printerr(2, "Processing keytab entry for principal '%s'\n",
493 /* Just use the first keytab entry found for each realm */
494 if ((!gssd_have_realm_ple((void *)&kte.principal->realm)) ) {
495 printerr(2, "We WILL use this entry (%s)\n", pname);
496 ple = malloc(sizeof(struct gssd_k5_kt_princ));
498 printerr(0, "ERROR: could not allocate storage "
499 "for principal list entry\n");
501 krb5_free_unparsed_name(context, pname);
505 krb5_kt_free_entry(context, &kte);
509 /* These will be filled in later */
515 strndup(kte.principal->realm.data,
516 kte.principal->realm.length))
518 strdup(kte.principal->realm))
521 printerr(0, "ERROR: %s while copying realm to "
522 "principal list entry\n",
523 "not enough memory");
525 krb5_free_unparsed_name(context, pname);
529 krb5_kt_free_entry(context, &kte);
533 if ((code = krb5_copy_principal(context,
534 kte.principal, &ple->princ))) {
535 printerr(0, "ERROR: %s while copying principal "
536 "to principal list entry\n",
537 error_message(code));
539 krb5_free_unparsed_name(context, pname);
543 krb5_kt_free_entry(context, &kte);
547 if (gssd_k5_kt_princ_list == NULL)
548 gssd_k5_kt_princ_list = ple;
550 ple->next = gssd_k5_kt_princ_list;
551 gssd_k5_kt_princ_list = ple;
555 printerr(2, "We will NOT use this entry (%s)\n",
559 krb5_free_unparsed_name(context, pname);
563 krb5_kt_free_entry(context, &kte);
566 if ((code = krb5_kt_end_seq_get(context, kt, &cursor))) {
567 printerr(0, "WARNING: %s while ending keytab scan for "
569 error_message(code), kt_name);
578 * Depending on the version of Kerberos, we either need to use
579 * a private function, or simply set the environment variable.
582 gssd_set_krb5_ccache_name(char *ccname)
584 #ifdef USE_GSS_KRB5_CCACHE_NAME
585 u_int maj_stat, min_stat;
587 printerr(2, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
589 maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
590 if (maj_stat != GSS_S_COMPLETE) {
591 printerr(0, "WARNING: gss_krb5_ccache_name with "
592 "name '%s' failed (%s)\n",
593 ccname, error_message(min_stat));
597 * Set the KRB5CCNAME environment variable to tell the krb5 code
598 * which credentials cache to use. (Instead of using the private
599 * function above for which there is no generic gssapi
602 printerr(2, "using environment variable to select krb5 ccache %s\n",
604 setenv("KRB5CCNAME", ccname, 1);
608 /*==========================*/
609 /*=== External routines ===*/
610 /*==========================*/
613 * Attempt to find the best match for a credentials cache file
614 * given only a UID. We really need more information, but we
615 * do the best we can.
621 gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername)
623 char buf[MAX_NETOBJ_SZ];
626 printerr(2, "getting credentials for client with uid %u for "
627 "server %s\n", uid, servername);
628 memset(buf, 0, sizeof(buf));
629 if (gssd_find_existing_krb5_ccache(uid, &d)) {
630 snprintf(buf, sizeof(buf), "FILE:%s/%s",
631 ccachedir, d->d_name);
635 snprintf(buf, sizeof(buf), "FILE:%s/%s%u",
636 ccachedir, GSSD_DEFAULT_CRED_PREFIX, uid);
637 printerr(2, "using %s as credentials cache for client with "
638 "uid %u for server %s\n", buf, uid, servername);
639 gssd_set_krb5_ccache_name(buf);
643 * Let the gss code know where to find the machine credentials ccache.
649 gssd_setup_krb5_machine_gss_ccache(char *ccname)
651 printerr(2, "using %s as credentials cache for machine creds\n",
653 gssd_set_krb5_ccache_name(ccname);
657 * The first time through this routine, go through the keytab and
658 * determine which keys we will try to use as machine credentials.
659 * Every time through this routine, try to obtain credentials using
660 * the keytab entries selected the first time through.
663 * 0 => obtained one or more credentials
669 gssd_refresh_krb5_machine_creds(void)
671 krb5_context context = NULL;
672 krb5_keytab kt = NULL;;
673 krb5_error_code code;
675 struct gssd_k5_kt_princ *ple;
677 static int processed_keytab = 0;
680 code = krb5_init_context(&context);
682 printerr(0, "ERROR: %s while initializing krb5 in "
683 "gssd_refresh_krb5_machine_creds\n",
684 error_message(code));
689 printerr(1, "Using keytab file '%s'\n", keytabfile);
691 if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
692 printerr(0, "ERROR: %s while resolving keytab '%s'\n",
693 error_message(code), keytabfile);
697 /* Only go through the keytab file once. Only print messages once. */
698 if (gssd_k5_kt_princ_list == NULL && !processed_keytab) {
699 processed_keytab = 1;
700 gssd_process_krb5_keytab(context, kt, keytabfile);
701 if (gssd_k5_kt_princ_list == NULL) {
702 printerr(0, "ERROR: No usable keytab entries found in "
703 "keytab '%s'\n", keytabfile);
704 printerr(0, "Do you have a valid keytab entry for "
705 "%s/<your.host>@<YOUR.REALM> in "
706 "keytab file %s ?\n",
707 GSSD_SERVICE_NAME, keytabfile);
708 printerr(0, "Continuing without (machine) credentials "
709 "- nfs4 mounts with Kerberos will fail\n");
714 * If we don't have any keytab entries we liked, then we have a problem
716 if (gssd_k5_kt_princ_list == NULL) {
722 * Now go through the list of saved entries and get initial
723 * credentials for them (We can't do this while making the
724 * list because it messes up the keytab iteration cursor
725 * when we use the keytab to get credentials.)
727 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
728 if ((gssd_get_single_krb5_cred(context, kt, ple)) == 0) {
733 printerr(0, "ERROR: No usable machine credentials obtained\n");
739 if (kt) krb5_kt_close(context, kt);
740 krb5_free_context(context);
747 * Return an array of pointers to names of credential cache files
748 * which can be used to try to create gss contexts with a server.
751 * 0 => list is attached
755 gssd_get_krb5_machine_cred_list(char ***list)
759 int listsize = listinc;
762 struct gssd_k5_kt_princ *ple;
766 *list = (char **) NULL;
768 /* Refresh machine credentials */
769 if ((retval = gssd_refresh_krb5_machine_creds())) {
773 if ((l = (char **) malloc(listsize * sizeof(char *))) == NULL) {
778 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
780 if (i + 1 > listsize) {
783 realloc(l, listsize * sizeof(char *));
789 if ((l[i++] = strdup(ple->ccname)) == NULL) {
806 * Frees the list of names returned in get_krb5_machine_cred_list()
809 gssd_free_krb5_machine_cred_list(char **list)
815 for (n = list; n && *n; n++) {
822 * Called upon exit. Destroys machine credentials.
825 gssd_destroy_krb5_machine_creds(void)
827 krb5_context context;
828 krb5_error_code code = 0;
830 struct gssd_k5_kt_princ *ple;
832 code = krb5_init_context(&context);
834 printerr(0, "ERROR: %s while initializing krb5\n",
835 error_message(code));
839 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
842 if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
843 printerr(0, "WARNING: %s while resolving credential "
844 "cache '%s' for destruction\n",
845 error_message(code), ple->ccname);
849 if ((code = krb5_cc_destroy(context, ccache))) {
850 printerr(0, "WARNING: %s while destroying credential "
852 error_message(code), ple->ccname);
856 krb5_free_context(context);