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.
96 #endif /* HAVE_CONFIG_H */
102 #include <sys/param.h>
104 #include <sys/stat.h>
105 #include <sys/socket.h>
106 #include <arpa/inet.h>
117 #include <gssapi/gssapi.h>
118 #ifdef USE_PRIVATE_KRB5_FUNCTIONS
119 #include <gssapi/gssapi_krb5.h>
122 #include <rpc/auth_gss.h>
125 #include "err_util.h"
126 #include "gss_util.h"
127 #include "krb5_util.h"
129 /* Global list of principals/cache file names for machine credentials */
130 struct gssd_k5_kt_princ *gssd_k5_kt_princ_list = NULL;
132 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
133 int limit_to_legacy_enctypes = 0;
136 /*==========================*/
137 /*=== Internal routines ===*/
138 /*==========================*/
140 static int select_krb5_ccache(const struct dirent *d);
141 static int gssd_find_existing_krb5_ccache(uid_t uid, char *dirname,
143 static int gssd_get_single_krb5_cred(krb5_context context,
144 krb5_keytab kt, struct gssd_k5_kt_princ *ple, int nocache);
145 static int query_krb5_ccache(const char* cred_cache, char **ret_princname,
149 * Called from the scandir function to weed out potential krb5
150 * credentials cache files
153 * 0 => don't select this one
154 * 1 => select this one
157 select_krb5_ccache(const struct dirent *d)
160 * Note: We used to check d->d_type for DT_REG here,
161 * but apparenlty reiser4 always has DT_UNKNOWN.
162 * Check for IS_REG after stat() call instead.
164 if (strstr(d->d_name, GSSD_DEFAULT_CRED_PREFIX))
171 * Look in directory "dirname" for files that look like they
172 * are Kerberos Credential Cache files for a given UID. Return
173 * non-zero and the dirent pointer for the entry most likely to be
174 * what we want. Otherwise, return zero and no dirent pointer.
175 * The caller is responsible for freeing the dirent if one is returned.
177 * Returns 0 if a valid-looking entry was found and a non-zero error
181 gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
183 struct dirent **namelist;
187 struct dirent *best_match_dir = NULL;
188 struct stat best_match_stat, tmp_stat;
190 char *princname = NULL;
192 int score, best_match_score = 0, err = -EACCES;
194 memset(&best_match_stat, 0, sizeof(best_match_stat));
196 n = scandir(dirname, &namelist, select_krb5_ccache, 0);
198 printerr(1, "Error doing scandir on directory '%s': %s\n",
199 dirname, strerror(errno));
203 for (i = 0; i < n; i++) {
204 snprintf(statname, sizeof(statname),
205 "%s/%s", dirname, namelist[i]->d_name);
206 printerr(3, "CC file '%s' being considered, "
207 "with preferred realm '%s'\n",
208 statname, preferred_realm ?
209 preferred_realm : "<none selected>");
210 snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname,
211 namelist[i]->d_name);
212 if (lstat(statname, &tmp_stat)) {
213 printerr(0, "Error doing stat on file '%s'\n",
218 /* Only pick caches owned by the user (uid) */
219 if (tmp_stat.st_uid != uid) {
220 printerr(3, "CC file '%s' owned by %u, not %u\n",
221 statname, tmp_stat.st_uid, uid);
225 if (!S_ISREG(tmp_stat.st_mode)) {
226 printerr(3, "CC file '%s' is not a regular file\n",
231 if (uid == 0 && !root_uses_machine_creds &&
232 strstr(namelist[i]->d_name, "_machine_")) {
233 printerr(3, "CC file '%s' not available to root\n",
238 if (!query_krb5_ccache(buf, &princname, &realm)) {
239 printerr(3, "CC file '%s' is expired or corrupt\n",
247 if (preferred_realm &&
248 strcmp(realm, preferred_realm) == 0)
251 printerr(3, "CC file '%s'(%s@%s) passed all checks and"
252 " has mtime of %u\n",
253 statname, princname, realm,
256 * if more than one match is found, return the most
257 * recent (the one with the latest mtime), and
258 * don't free the dirent
261 best_match_dir = namelist[i];
262 best_match_stat = tmp_stat;
263 best_match_score = score;
268 * If current score is higher than best match
269 * score, we use the current match. Otherwise,
270 * if the current match has an mtime later
271 * than the one we are looking at, then use
272 * the current match. Otherwise, we still
273 * have the best match.
275 if (best_match_score < score ||
276 (best_match_score == score &&
278 best_match_stat.st_mtime)) {
279 free(best_match_dir);
280 best_match_dir = namelist[i];
281 best_match_stat = tmp_stat;
282 best_match_score = score;
287 printerr(3, "CC file '%s/%s' is our "
288 "current best match "
289 "with mtime of %u\n",
290 dirname, best_match_dir->d_name,
291 best_match_stat.st_mtime);
307 * Obtain credentials via a key in the keytab given
308 * a keytab handle and a gssd_k5_kt_princ structure.
309 * Checks to see if current credentials are expired,
310 * if not, uses the keytab to obtain new credentials.
313 * 0 => success (or credentials have not expired)
317 gssd_get_single_krb5_cred(krb5_context context,
319 struct gssd_k5_kt_princ *ple,
322 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
323 krb5_get_init_creds_opt *init_opts = NULL;
325 krb5_get_init_creds_opt options;
327 krb5_get_init_creds_opt *opts;
329 krb5_ccache ccache = NULL;
330 char kt_name[BUFSIZ];
331 char cc_name[BUFSIZ];
333 time_t now = time(0);
338 memset(&my_creds, 0, sizeof(my_creds));
340 if (ple->ccname && ple->endtime > now && !nocache) {
341 printerr(2, "INFO: Credentials in CC '%s' are good until %d\n",
342 ple->ccname, ple->endtime);
347 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
348 printerr(0, "ERROR: Unable to get keytab name in "
349 "gssd_get_single_krb5_cred\n");
353 if ((krb5_unparse_name(context, ple->princ, &pname)))
356 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
357 code = krb5_get_init_creds_opt_alloc(context, &init_opts);
359 k5err = gssd_k5_err_msg(context, code);
360 printerr(0, "ERROR: %s allocating gic options\n", k5err);
363 if (krb5_get_init_creds_opt_set_addressless(context, init_opts, 1))
364 printerr(1, "WARNING: Unable to set option for addressless "
365 "tickets. May have problems behind a NAT.\n");
366 #ifdef TEST_SHORT_LIFETIME
367 /* set a short lifetime (for debugging only!) */
368 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
369 krb5_get_init_creds_opt_set_tkt_life(init_opts, 5*60);
373 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS */
375 krb5_get_init_creds_opt_init(&options);
376 krb5_get_init_creds_opt_set_address_list(&options, NULL);
377 #ifdef TEST_SHORT_LIFETIME
378 /* set a short lifetime (for debugging only!) */
379 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
380 krb5_get_init_creds_opt_set_tkt_life(&options, 5*60);
385 if ((code = krb5_get_init_creds_keytab(context, &my_creds, ple->princ,
386 kt, 0, NULL, opts))) {
387 k5err = gssd_k5_err_msg(context, code);
388 printerr(1, "WARNING: %s while getting initial ticket for "
389 "principal '%s' using keytab '%s'\n", k5err,
390 pname ? pname : "<unparsable>", kt_name);
395 * Initialize cache file which we're going to be using
399 cache_type = "MEMORY";
402 snprintf(cc_name, sizeof(cc_name), "%s:%s/%s%s_%s",
404 ccachesearch[0], GSSD_DEFAULT_CRED_PREFIX,
405 GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm);
406 ple->endtime = my_creds.times.endtime;
407 if (ple->ccname != NULL)
409 ple->ccname = strdup(cc_name);
410 if (ple->ccname == NULL) {
411 printerr(0, "ERROR: no storage to duplicate credentials "
412 "cache name '%s'\n", cc_name);
416 if ((code = krb5_cc_resolve(context, cc_name, &ccache))) {
417 k5err = gssd_k5_err_msg(context, code);
418 printerr(0, "ERROR: %s while opening credential cache '%s'\n",
422 if ((code = krb5_cc_initialize(context, ccache, ple->princ))) {
423 k5err = gssd_k5_err_msg(context, code);
424 printerr(0, "ERROR: %s while initializing credential "
425 "cache '%s'\n", k5err, cc_name);
428 if ((code = krb5_cc_store_cred(context, ccache, &my_creds))) {
429 k5err = gssd_k5_err_msg(context, code);
430 printerr(0, "ERROR: %s while storing credentials in '%s'\n",
436 printerr(2, "Successfully obtained machine credentials for "
437 "principal '%s' stored in ccache '%s'\n", pname, cc_name);
439 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
441 krb5_get_init_creds_opt_free(context, init_opts);
444 k5_free_unparsed_name(context, pname);
446 krb5_cc_close(context, ccache);
447 krb5_free_cred_contents(context, &my_creds);
453 * Depending on the version of Kerberos, we either need to use
454 * a private function, or simply set the environment variable.
457 gssd_set_krb5_ccache_name(char *ccname)
459 #ifdef USE_GSS_KRB5_CCACHE_NAME
460 u_int maj_stat, min_stat;
462 printerr(2, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
464 maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
465 if (maj_stat != GSS_S_COMPLETE) {
466 printerr(0, "WARNING: gss_krb5_ccache_name with "
467 "name '%s' failed (%s)\n",
468 ccname, error_message(min_stat));
472 * Set the KRB5CCNAME environment variable to tell the krb5 code
473 * which credentials cache to use. (Instead of using the private
474 * function above for which there is no generic gssapi
477 printerr(2, "using environment variable to select krb5 ccache %s\n",
479 setenv("KRB5CCNAME", ccname, 1);
484 * Given a principal, find a matching ple structure
486 static struct gssd_k5_kt_princ *
487 find_ple_by_princ(krb5_context context, krb5_principal princ)
489 struct gssd_k5_kt_princ *ple;
491 for (ple = gssd_k5_kt_princ_list; ple != NULL; ple = ple->next) {
492 if (krb5_principal_compare(context, ple->princ, princ))
500 * Create, initialize, and add a new ple structure to the global list
502 static struct gssd_k5_kt_princ *
503 new_ple(krb5_context context, krb5_principal princ)
505 struct gssd_k5_kt_princ *ple = NULL, *p;
506 krb5_error_code code;
508 int is_default_realm = 0;
510 ple = malloc(sizeof(struct gssd_k5_kt_princ));
513 memset(ple, 0, sizeof(*ple));
516 ple->realm = strndup(princ->realm.data,
517 princ->realm.length);
519 ple->realm = strdup(princ->realm);
521 if (ple->realm == NULL)
523 code = krb5_copy_principal(context, princ, &ple->princ);
528 * Add new entry onto the list (if this is the default
529 * realm, always add to the front of the list)
532 code = krb5_get_default_realm(context, &default_realm);
534 if (strcmp(ple->realm, default_realm) == 0)
535 is_default_realm = 1;
536 k5_free_default_realm(context, default_realm);
539 if (is_default_realm) {
540 ple->next = gssd_k5_kt_princ_list;
541 gssd_k5_kt_princ_list = ple;
543 p = gssd_k5_kt_princ_list;
544 while (p != NULL && p->next != NULL)
547 gssd_k5_kt_princ_list = ple;
563 * Given a principal, find an existing ple structure, or create one
565 static struct gssd_k5_kt_princ *
566 get_ple_by_princ(krb5_context context, krb5_principal princ)
568 struct gssd_k5_kt_princ *ple;
570 /* Need to serialize list if we ever become multi-threaded! */
572 ple = find_ple_by_princ(context, princ);
574 ple = new_ple(context, princ);
581 * Given a (possibly unqualified) hostname,
582 * return the fully qualified (lower-case!) hostname
585 get_full_hostname(const char *inhost, char *outhost, int outhostlen)
587 struct addrinfo *addrs = NULL;
588 struct addrinfo hints;
592 memset(&hints, 0, sizeof(hints));
593 hints.ai_socktype = SOCK_STREAM;
594 hints.ai_family = PF_UNSPEC;
595 hints.ai_flags = AI_CANONNAME;
597 /* Get full target hostname */
598 retval = getaddrinfo(inhost, NULL, &hints, &addrs);
600 printerr(1, "%s while getting full hostname for '%s'\n",
601 gai_strerror(retval), inhost);
604 strncpy(outhost, addrs->ai_canonname, outhostlen);
606 for (c = outhost; *c != '\0'; c++)
609 printerr(3, "Full hostname for '%s' is '%s'\n", inhost, outhost);
616 * If principal matches the given realm and service name,
617 * and has *any* instance (hostname), return 1.
618 * Otherwise return 0, indicating no match.
622 realm_and_service_match(krb5_principal p, const char *realm, const char *service)
624 /* Must have two components */
628 if ((strlen(realm) == p->realm.length)
629 && (strncmp(realm, p->realm.data, p->realm.length) == 0)
630 && (strlen(service) == p->data[0].length)
631 && (strncmp(service, p->data[0].data, p->data[0].length) == 0))
638 realm_and_service_match(krb5_context context, krb5_principal p,
639 const char *realm, const char *service)
641 const char *name, *inst;
643 if (p->name.name_string.len != 2)
646 name = krb5_principal_get_comp_string(context, p, 0);
647 inst = krb5_principal_get_comp_string(context, p, 1);
648 if (name == NULL || inst == NULL)
650 if ((strcmp(realm, p->realm) == 0)
651 && (strcmp(service, name) == 0))
659 * Search the given keytab file looking for an entry with the given
660 * service name and realm, ignoring hostname (instance).
664 * non-zero => An error occurred
666 * If a keytab entry is found, "found" is set to one, and the keytab
667 * entry is returned in "kte". Otherwise, "found" is zero, and the
668 * value of "kte" is unpredictable.
671 gssd_search_krb5_keytab(krb5_context context, krb5_keytab kt,
672 const char *realm, const char *service,
673 int *found, krb5_keytab_entry *kte)
675 krb5_kt_cursor cursor;
676 krb5_error_code code;
677 struct gssd_k5_kt_princ *ple;
678 int retval = -1, status;
679 char kt_name[BUFSIZ];
690 * Look through each entry in the keytab file and determine
691 * if we might want to use it as machine credentials. If so,
692 * save info in the global principal list (gssd_k5_kt_princ_list).
694 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
695 k5err = gssd_k5_err_msg(context, code);
696 printerr(0, "ERROR: %s attempting to get keytab name\n", k5err);
700 if ((code = krb5_kt_start_seq_get(context, kt, &cursor))) {
701 k5err = gssd_k5_err_msg(context, code);
702 printerr(0, "ERROR: %s while beginning keytab scan "
703 "for keytab '%s'\n", k5err, kt_name);
708 while ((code = krb5_kt_next_entry(context, kt, kte, &cursor)) == 0) {
709 if ((code = krb5_unparse_name(context, kte->principal,
711 k5err = gssd_k5_err_msg(context, code);
712 printerr(0, "WARNING: Skipping keytab entry because "
713 "we failed to unparse principal name: %s\n",
715 k5_free_kt_entry(context, kte);
718 printerr(4, "Processing keytab entry for principal '%s'\n",
720 /* Use the first matching keytab entry found */
722 status = realm_and_service_match(kte->principal, realm, service);
724 status = realm_and_service_match(context, kte->principal, realm, service);
727 printerr(4, "We WILL use this entry (%s)\n", pname);
728 ple = get_ple_by_princ(context, kte->principal);
730 * Return, don't free, keytab entry if
731 * we were successful!
735 k5_free_kt_entry(context, kte);
740 k5_free_unparsed_name(context, pname);
744 printerr(4, "We will NOT use this entry (%s)\n",
747 k5_free_unparsed_name(context, pname);
748 k5_free_kt_entry(context, kte);
751 if ((code = krb5_kt_end_seq_get(context, kt, &cursor))) {
752 k5err = gssd_k5_err_msg(context, code);
753 printerr(0, "WARNING: %s while ending keytab scan for "
754 "keytab '%s'\n", k5err, kt_name);
764 * Find a keytab entry to use for a given target hostname.
765 * Tries to find the most appropriate keytab to use given the
766 * name of the host we are trying to connect with.
769 find_keytab_entry(krb5_context context, krb5_keytab kt, const char *hostname,
770 krb5_keytab_entry *kte, const char **svcnames)
772 krb5_error_code code;
773 char **realmnames = NULL;
774 char myhostname[NI_MAXHOST], targethostname[NI_MAXHOST];
775 char myhostad[NI_MAXHOST+1];
777 char *default_realm = NULL;
780 int tried_all = 0, tried_default = 0;
781 krb5_principal princ;
784 /* Get full target hostname */
785 retval = get_full_hostname(hostname, targethostname,
786 sizeof(targethostname));
790 /* Get full local hostname */
791 retval = gethostname(myhostname, sizeof(myhostname));
793 k5err = gssd_k5_err_msg(context, retval);
794 printerr(1, "%s while getting local hostname\n", k5err);
798 /* Compute the active directory machine name HOST$ */
799 strcpy(myhostad, myhostname);
800 for (i = 0; myhostad[i] != 0; ++i)
801 myhostad[i] = toupper(myhostad[i]);
805 retval = get_full_hostname(myhostname, myhostname, sizeof(myhostname));
809 code = krb5_get_default_realm(context, &default_realm);
812 k5err = gssd_k5_err_msg(context, code);
813 printerr(1, "%s while getting default realm name\n", k5err);
818 * Get the realm name(s) for the target hostname.
819 * In reality, this function currently only returns a
820 * single realm, but we code with the assumption that
821 * someday it may actually return a list.
823 code = krb5_get_host_realm(context, targethostname, &realmnames);
825 k5err = gssd_k5_err_msg(context, code);
826 printerr(0, "ERROR: %s while getting realm(s) for host '%s'\n",
827 k5err, targethostname);
833 * Try the "appropriate" realm first, and if nothing found for that
834 * realm, try the default realm (if it hasn't already been tried).
837 realm = realmnames[i];
842 realm = default_realm;
844 if (tried_all && tried_default)
846 if (strcmp(realm, default_realm) == 0)
848 for (j = 0; svcnames[j] != NULL; j++) {
852 * The special svcname "$" means 'try the active
853 * directory machine account'
855 if (strcmp(svcnames[j],"$") == 0) {
856 snprintf(spn, sizeof(spn), "%s@%s", myhostad, realm);
857 code = krb5_build_principal_ext(context, &princ,
864 snprintf(spn, sizeof(spn), "%s/%s@%s",
865 svcnames[j], myhostname, realm);
866 code = krb5_build_principal_ext(context, &princ,
877 k5err = gssd_k5_err_msg(context, code);
878 printerr(1, "%s while building principal for '%s'\n",
882 code = krb5_kt_get_entry(context, kt, princ, 0, 0, kte);
883 krb5_free_principal(context, princ);
885 k5err = gssd_k5_err_msg(context, code);
886 printerr(3, "%s while getting keytab entry for '%s'\n",
889 printerr(3, "Success getting keytab entry for '%s'\n",spn);
896 * Nothing found with our hostname instance, now look for
897 * names with any instance (they must have an instance)
899 for (j = 0; svcnames[j] != NULL; j++) {
901 if (strcmp(svcnames[j],"$") == 0)
903 code = gssd_search_krb5_keytab(context, kt, realm,
904 svcnames[j], &found, kte);
905 if (!code && found) {
906 printerr(3, "Success getting keytab entry for "
907 "%s/*@%s\n", svcnames[j], realm);
914 realm = realmnames[i];
919 k5_free_default_realm(context, default_realm);
921 krb5_free_host_realm(context, realmnames);
927 static inline int data_is_equal(krb5_data d1, krb5_data d2)
929 return (d1.length == d2.length
930 && memcmp(d1.data, d2.data, d1.length) == 0);
934 check_for_tgt(krb5_context context, krb5_ccache ccache,
935 krb5_principal principal)
942 ret = krb5_cc_start_seq_get(context, ccache, &cur);
947 (ret = krb5_cc_next_cred(context, ccache, &cur, &creds)) == 0) {
948 if (creds.server->length == 2 &&
949 data_is_equal(creds.server->realm,
951 creds.server->data[0].length == 6 &&
952 memcmp(creds.server->data[0].data,
954 data_is_equal(creds.server->data[1],
956 creds.times.endtime > time(NULL))
958 krb5_free_cred_contents(context, &creds);
960 krb5_cc_end_seq_get(context, ccache, &cur);
966 query_krb5_ccache(const char* cred_cache, char **ret_princname,
970 krb5_context context;
972 krb5_principal principal;
977 ret = krb5_init_context(&context);
981 if(!cred_cache || krb5_cc_resolve(context, cred_cache, &ccache))
984 if (krb5_cc_set_flags(context, ccache, 0))
987 ret = krb5_cc_get_principal(context, ccache, &principal);
991 found = check_for_tgt(context, ccache, principal);
993 ret = krb5_unparse_name(context, principal, &princstring);
995 if ((str = strchr(princstring, '@')) != NULL) {
997 *ret_princname = strdup(princstring);
998 *ret_realm = strdup(str+1);
1000 k5_free_unparsed_name(context, princstring);
1005 krb5_free_principal(context, principal);
1007 krb5_cc_set_flags(context, ccache, KRB5_TC_OPENCLOSE);
1008 krb5_cc_close(context, ccache);
1010 krb5_free_context(context);
1014 /*==========================*/
1015 /*=== External routines ===*/
1016 /*==========================*/
1019 * Attempt to find the best match for a credentials cache file
1020 * given only a UID. We really need more information, but we
1021 * do the best we can.
1023 * Returns 0 if a ccache was found, and a non-zero error code otherwise.
1026 gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername, char *dirname)
1028 char buf[MAX_NETOBJ_SZ];
1032 printerr(2, "getting credentials for client with uid %u for "
1033 "server %s\n", uid, servername);
1034 memset(buf, 0, sizeof(buf));
1035 err = gssd_find_existing_krb5_ccache(uid, dirname, &d);
1039 snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname, d->d_name);
1042 printerr(2, "using %s as credentials cache for client with "
1043 "uid %u for server %s\n", buf, uid, servername);
1044 gssd_set_krb5_ccache_name(buf);
1049 * Let the gss code know where to find the machine credentials ccache.
1055 gssd_setup_krb5_machine_gss_ccache(char *ccname)
1057 printerr(2, "using %s as credentials cache for machine creds\n",
1059 gssd_set_krb5_ccache_name(ccname);
1063 * Return an array of pointers to names of credential cache files
1064 * which can be used to try to create gss contexts with a server.
1067 * 0 => list is attached
1071 gssd_get_krb5_machine_cred_list(char ***list)
1075 int listsize = listinc;
1078 struct gssd_k5_kt_princ *ple;
1080 /* Assume failure */
1082 *list = (char **) NULL;
1084 if ((l = (char **) malloc(listsize * sizeof(char *))) == NULL) {
1089 /* Need to serialize list if we ever become multi-threaded! */
1091 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
1093 /* Make sure cred is up-to-date before returning it */
1094 retval = gssd_refresh_krb5_machine_credential(NULL, ple,
1098 if (i + 1 > listsize) {
1099 listsize += listinc;
1101 realloc(l, listsize * sizeof(char *));
1107 if ((l[i++] = strdup(ple->ccname)) == NULL) {
1124 * Frees the list of names returned in get_krb5_machine_cred_list()
1127 gssd_free_krb5_machine_cred_list(char **list)
1133 for (n = list; n && *n; n++) {
1140 * Called upon exit. Destroys machine credentials.
1143 gssd_destroy_krb5_machine_creds(void)
1145 krb5_context context;
1146 krb5_error_code code = 0;
1148 struct gssd_k5_kt_princ *ple;
1151 code = krb5_init_context(&context);
1153 k5err = gssd_k5_err_msg(NULL, code);
1154 printerr(0, "ERROR: %s while initializing krb5\n", k5err);
1158 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
1161 if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
1162 k5err = gssd_k5_err_msg(context, code);
1163 printerr(0, "WARNING: %s while resolving credential "
1164 "cache '%s' for destruction\n", k5err,
1169 if ((code = krb5_cc_destroy(context, ccache))) {
1170 k5err = gssd_k5_err_msg(context, code);
1171 printerr(0, "WARNING: %s while destroying credential "
1172 "cache '%s'\n", k5err, ple->ccname);
1177 krb5_free_context(context);
1181 * Obtain (or refresh if necessary) Kerberos machine credentials
1184 gssd_refresh_krb5_machine_credential(char *hostname,
1185 struct gssd_k5_kt_princ *ple,
1188 krb5_error_code code = 0;
1189 krb5_context context;
1190 krb5_keytab kt = NULL;;
1193 const char *svcnames[5] = { "$", "root", "nfs", "host", NULL };
1196 * If a specific service name was specified, use it.
1197 * Otherwise, use the default list.
1199 if (service != NULL && strcmp(service, "*") != 0) {
1200 svcnames[0] = service;
1203 if (hostname == NULL && ple == NULL)
1206 code = krb5_init_context(&context);
1208 k5err = gssd_k5_err_msg(NULL, code);
1209 printerr(0, "ERROR: %s: %s while initializing krb5 context\n",
1215 if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
1216 k5err = gssd_k5_err_msg(context, code);
1217 printerr(0, "ERROR: %s: %s while resolving keytab '%s'\n",
1218 __func__, k5err, keytabfile);
1223 krb5_keytab_entry kte;
1225 code = find_keytab_entry(context, kt, hostname, &kte, svcnames);
1227 printerr(0, "ERROR: %s: no usable keytab entry found "
1228 "in keytab %s for connection with host %s\n",
1229 __FUNCTION__, keytabfile, hostname);
1234 ple = get_ple_by_princ(context, kte.principal);
1235 k5_free_kt_entry(context, &kte);
1238 if ((krb5_unparse_name(context, kte.principal, &pname))) {
1241 printerr(0, "ERROR: %s: Could not locate or create "
1242 "ple struct for principal %s for connection "
1244 __FUNCTION__, pname ? pname : "<unparsable>",
1246 if (pname) k5_free_unparsed_name(context, pname);
1250 retval = gssd_get_single_krb5_cred(context, kt, ple, 0);
1253 krb5_kt_close(context, kt);
1254 krb5_free_context(context);
1260 * A common routine for getting the Kerberos error message
1263 gssd_k5_err_msg(krb5_context context, krb5_error_code code)
1265 const char *origmsg;
1268 #if HAVE_KRB5_GET_ERROR_MESSAGE
1269 if (context != NULL) {
1270 origmsg = krb5_get_error_message(context, code);
1271 msg = strdup(origmsg);
1272 krb5_free_error_message(context, origmsg);
1278 return strdup(error_message(code));
1280 if (context != NULL)
1281 return strdup(krb5_get_err_text(context, code));
1283 return strdup(error_message(code));
1288 * Return default Kerberos realm
1291 gssd_k5_get_default_realm(char **def_realm)
1293 krb5_context context;
1295 if (krb5_init_context(&context))
1298 krb5_get_default_realm(context, def_realm);
1300 krb5_free_context(context);
1303 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
1305 * this routine obtains a credentials handle via gss_acquire_cred()
1306 * then calls gss_krb5_set_allowable_enctypes() to limit the encryption
1309 * XXX Should call some function to determine the enctypes supported
1310 * by the kernel. (Only need to do that once!)
1313 * 0 => all went well
1314 * -1 => there was an error
1318 limit_krb5_enctypes(struct rpc_gss_sec *sec)
1320 u_int maj_stat, min_stat;
1321 gss_cred_id_t credh;
1322 gss_OID_set_desc desired_mechs;
1323 krb5_enctype enctypes[] = { ENCTYPE_DES_CBC_CRC,
1324 ENCTYPE_DES_CBC_MD5,
1325 ENCTYPE_DES_CBC_MD4 };
1326 int num_enctypes = sizeof(enctypes) / sizeof(enctypes[0]);
1327 extern int num_krb5_enctypes;
1328 extern krb5_enctype *krb5_enctypes;
1330 /* We only care about getting a krb5 cred */
1331 desired_mechs.count = 1;
1332 desired_mechs.elements = &krb5oid;
1334 maj_stat = gss_acquire_cred(&min_stat, NULL, 0,
1335 &desired_mechs, GSS_C_INITIATE,
1336 &credh, NULL, NULL);
1338 if (maj_stat != GSS_S_COMPLETE) {
1339 if (get_verbosity() > 0)
1340 pgsserr("gss_acquire_cred",
1341 maj_stat, min_stat, &krb5oid);
1346 * If we failed for any reason to produce global
1347 * list of supported enctypes, use local default here.
1349 if (krb5_enctypes == NULL || limit_to_legacy_enctypes)
1350 maj_stat = gss_set_allowable_enctypes(&min_stat, credh,
1351 &krb5oid, num_enctypes, enctypes);
1353 maj_stat = gss_set_allowable_enctypes(&min_stat, credh,
1354 &krb5oid, num_krb5_enctypes, krb5_enctypes);
1356 if (maj_stat != GSS_S_COMPLETE) {
1357 pgsserr("gss_set_allowable_enctypes",
1358 maj_stat, min_stat, &krb5oid);
1359 gss_release_cred(&min_stat, &credh);
1366 #endif /* HAVE_SET_ALLOWABLE_ENCTYPES */