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 /*==========================*/
133 /*=== Internal routines ===*/
134 /*==========================*/
136 static int select_krb5_ccache(const struct dirent *d);
137 static int gssd_find_existing_krb5_ccache(uid_t uid, char *dirname,
139 static int gssd_get_single_krb5_cred(krb5_context context,
140 krb5_keytab kt, struct gssd_k5_kt_princ *ple, int nocache);
141 static int query_krb5_ccache(const char* cred_cache, char **ret_princname,
145 * Called from the scandir function to weed out potential krb5
146 * credentials cache files
149 * 0 => don't select this one
150 * 1 => select this one
153 select_krb5_ccache(const struct dirent *d)
156 * Note: We used to check d->d_type for DT_REG here,
157 * but apparenlty reiser4 always has DT_UNKNOWN.
158 * Check for IS_REG after stat() call instead.
160 if (strstr(d->d_name, GSSD_DEFAULT_CRED_PREFIX))
167 * Look in directory "dirname" for files that look like they
168 * are Kerberos Credential Cache files for a given UID. Return
169 * non-zero and the dirent pointer for the entry most likely to be
170 * what we want. Otherwise, return zero and no dirent pointer.
171 * The caller is responsible for freeing the dirent if one is returned.
174 * 0 => could not find an existing entry
175 * 1 => found an existing entry
178 gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
180 struct dirent **namelist;
184 struct dirent *best_match_dir = NULL;
185 struct stat best_match_stat, tmp_stat;
187 char *princname = NULL;
189 int score, best_match_score = 0;
191 memset(&best_match_stat, 0, sizeof(best_match_stat));
193 n = scandir(dirname, &namelist, select_krb5_ccache, 0);
195 printerr(1, "Error doing scandir on directory '%s': %s\n",
196 dirname, strerror(errno));
200 for (i = 0; i < n; i++) {
201 snprintf(statname, sizeof(statname),
202 "%s/%s", dirname, namelist[i]->d_name);
203 printerr(3, "CC file '%s' being considered, "
204 "with preferred realm '%s'\n",
205 statname, preferred_realm ?
206 preferred_realm : "<none selected>");
207 snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname,
208 namelist[i]->d_name);
209 if (lstat(statname, &tmp_stat)) {
210 printerr(0, "Error doing stat on file '%s'\n",
215 /* Only pick caches owned by the user (uid) */
216 if (tmp_stat.st_uid != uid) {
217 printerr(3, "CC file '%s' owned by %u, not %u\n",
218 statname, tmp_stat.st_uid, uid);
222 if (!S_ISREG(tmp_stat.st_mode)) {
223 printerr(3, "CC file '%s' is not a regular file\n",
228 if (!query_krb5_ccache(buf, &princname, &realm)) {
229 printerr(3, "CC file '%s' is expired or corrupt\n",
236 if (preferred_realm &&
237 strcmp(realm, preferred_realm) == 0)
240 printerr(3, "CC file '%s'(%s@%s) passed all checks and"
241 " has mtime of %u\n",
242 statname, princname, realm,
245 * if more than one match is found, return the most
246 * recent (the one with the latest mtime), and
247 * don't free the dirent
250 best_match_dir = namelist[i];
251 best_match_stat = tmp_stat;
252 best_match_score = score;
257 * If current score is higher than best match
258 * score, we use the current match. Otherwise,
259 * if the current match has an mtime later
260 * than the one we are looking at, then use
261 * the current match. Otherwise, we still
262 * have the best match.
264 if (best_match_score < score ||
265 (best_match_score == score &&
267 best_match_stat.st_mtime)) {
268 free(best_match_dir);
269 best_match_dir = namelist[i];
270 best_match_stat = tmp_stat;
271 best_match_score = score;
276 printerr(3, "CC file '%s/%s' is our "
277 "current best match "
278 "with mtime of %u\n",
279 dirname, best_match_dir->d_name,
280 best_match_stat.st_mtime);
295 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
297 * this routine obtains a credentials handle via gss_acquire_cred()
298 * then calls gss_krb5_set_allowable_enctypes() to limit the encryption
301 * XXX Should call some function to determine the enctypes supported
302 * by the kernel. (Only need to do that once!)
306 * -1 => there was an error
310 limit_krb5_enctypes(struct rpc_gss_sec *sec, uid_t uid)
312 u_int maj_stat, min_stat;
314 gss_OID_set_desc desired_mechs;
315 krb5_enctype enctypes[] = { ENCTYPE_DES_CBC_CRC,
317 ENCTYPE_DES_CBC_MD4 };
318 int num_enctypes = sizeof(enctypes) / sizeof(enctypes[0]);
320 /* We only care about getting a krb5 cred */
321 desired_mechs.count = 1;
322 desired_mechs.elements = &krb5oid;
324 maj_stat = gss_acquire_cred(&min_stat, NULL, 0,
325 &desired_mechs, GSS_C_INITIATE,
328 if (maj_stat != GSS_S_COMPLETE) {
329 if (get_verbosity() > 0)
330 pgsserr("gss_acquire_cred",
331 maj_stat, min_stat, &krb5oid);
335 maj_stat = gss_set_allowable_enctypes(&min_stat, credh, &krb5oid,
336 num_enctypes, &enctypes);
337 if (maj_stat != GSS_S_COMPLETE) {
338 pgsserr("gss_set_allowable_enctypes",
339 maj_stat, min_stat, &krb5oid);
340 gss_release_cred(&min_stat, &credh);
347 #endif /* HAVE_SET_ALLOWABLE_ENCTYPES */
350 * Obtain credentials via a key in the keytab given
351 * a keytab handle and a gssd_k5_kt_princ structure.
352 * Checks to see if current credentials are expired,
353 * if not, uses the keytab to obtain new credentials.
356 * 0 => success (or credentials have not expired)
360 gssd_get_single_krb5_cred(krb5_context context,
362 struct gssd_k5_kt_princ *ple,
365 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
366 krb5_get_init_creds_opt *init_opts = NULL;
368 krb5_get_init_creds_opt options;
370 krb5_get_init_creds_opt *opts;
372 krb5_ccache ccache = NULL;
373 char kt_name[BUFSIZ];
374 char cc_name[BUFSIZ];
376 time_t now = time(0);
381 memset(&my_creds, 0, sizeof(my_creds));
383 if (ple->ccname && ple->endtime > now && !nocache) {
384 printerr(2, "INFO: Credentials in CC '%s' are good until %d\n",
385 ple->ccname, ple->endtime);
390 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
391 printerr(0, "ERROR: Unable to get keytab name in "
392 "gssd_get_single_krb5_cred\n");
396 if ((krb5_unparse_name(context, ple->princ, &pname)))
399 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
400 code = krb5_get_init_creds_opt_alloc(context, &init_opts);
402 k5err = gssd_k5_err_msg(context, code);
403 printerr(0, "ERROR: %s allocating gic options\n", k5err);
406 if (krb5_get_init_creds_opt_set_addressless(context, init_opts, 1))
407 printerr(1, "WARNING: Unable to set option for addressless "
408 "tickets. May have problems behind a NAT.\n");
409 #ifdef TEST_SHORT_LIFETIME
410 /* set a short lifetime (for debugging only!) */
411 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
412 krb5_get_init_creds_opt_set_tkt_life(init_opts, 5*60);
416 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS */
418 krb5_get_init_creds_opt_init(&options);
419 krb5_get_init_creds_opt_set_address_list(&options, NULL);
420 #ifdef TEST_SHORT_LIFETIME
421 /* set a short lifetime (for debugging only!) */
422 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
423 krb5_get_init_creds_opt_set_tkt_life(&options, 5*60);
428 if ((code = krb5_get_init_creds_keytab(context, &my_creds, ple->princ,
429 kt, 0, NULL, opts))) {
430 k5err = gssd_k5_err_msg(context, code);
431 printerr(1, "WARNING: %s while getting initial ticket for "
432 "principal '%s' using keytab '%s'\n", k5err,
433 pname ? pname : "<unparsable>", kt_name);
438 * Initialize cache file which we're going to be using
442 cache_type = "MEMORY";
445 snprintf(cc_name, sizeof(cc_name), "%s:%s/%s%s_%s",
447 ccachesearch[0], GSSD_DEFAULT_CRED_PREFIX,
448 GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm);
449 ple->endtime = my_creds.times.endtime;
450 if (ple->ccname != NULL)
452 ple->ccname = strdup(cc_name);
453 if (ple->ccname == NULL) {
454 printerr(0, "ERROR: no storage to duplicate credentials "
455 "cache name '%s'\n", cc_name);
459 if ((code = krb5_cc_resolve(context, cc_name, &ccache))) {
460 k5err = gssd_k5_err_msg(context, code);
461 printerr(0, "ERROR: %s while opening credential cache '%s'\n",
465 if ((code = krb5_cc_initialize(context, ccache, ple->princ))) {
466 k5err = gssd_k5_err_msg(context, code);
467 printerr(0, "ERROR: %s while initializing credential "
468 "cache '%s'\n", k5err, cc_name);
471 if ((code = krb5_cc_store_cred(context, ccache, &my_creds))) {
472 k5err = gssd_k5_err_msg(context, code);
473 printerr(0, "ERROR: %s while storing credentials in '%s'\n",
479 printerr(2, "Successfully obtained machine credentials for "
480 "principal '%s' stored in ccache '%s'\n", pname, cc_name);
482 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
484 krb5_get_init_creds_opt_free(context, init_opts);
487 k5_free_unparsed_name(context, pname);
489 krb5_cc_close(context, ccache);
490 krb5_free_cred_contents(context, &my_creds);
496 * Depending on the version of Kerberos, we either need to use
497 * a private function, or simply set the environment variable.
500 gssd_set_krb5_ccache_name(char *ccname)
502 #ifdef USE_GSS_KRB5_CCACHE_NAME
503 u_int maj_stat, min_stat;
505 printerr(2, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
507 maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
508 if (maj_stat != GSS_S_COMPLETE) {
509 printerr(0, "WARNING: gss_krb5_ccache_name with "
510 "name '%s' failed (%s)\n",
511 ccname, error_message(min_stat));
515 * Set the KRB5CCNAME environment variable to tell the krb5 code
516 * which credentials cache to use. (Instead of using the private
517 * function above for which there is no generic gssapi
520 printerr(2, "using environment variable to select krb5 ccache %s\n",
522 setenv("KRB5CCNAME", ccname, 1);
527 * Given a principal, find a matching ple structure
529 static struct gssd_k5_kt_princ *
530 find_ple_by_princ(krb5_context context, krb5_principal princ)
532 struct gssd_k5_kt_princ *ple;
534 for (ple = gssd_k5_kt_princ_list; ple != NULL; ple = ple->next) {
535 if (krb5_principal_compare(context, ple->princ, princ))
543 * Create, initialize, and add a new ple structure to the global list
545 static struct gssd_k5_kt_princ *
546 new_ple(krb5_context context, krb5_principal princ)
548 struct gssd_k5_kt_princ *ple = NULL, *p;
549 krb5_error_code code;
551 int is_default_realm = 0;
553 ple = malloc(sizeof(struct gssd_k5_kt_princ));
556 memset(ple, 0, sizeof(*ple));
559 ple->realm = strndup(princ->realm.data,
560 princ->realm.length);
562 ple->realm = strdup(princ->realm);
564 if (ple->realm == NULL)
566 code = krb5_copy_principal(context, princ, &ple->princ);
571 * Add new entry onto the list (if this is the default
572 * realm, always add to the front of the list)
575 code = krb5_get_default_realm(context, &default_realm);
577 if (strcmp(ple->realm, default_realm) == 0)
578 is_default_realm = 1;
579 k5_free_default_realm(context, default_realm);
582 if (is_default_realm) {
583 ple->next = gssd_k5_kt_princ_list;
584 gssd_k5_kt_princ_list = ple;
586 p = gssd_k5_kt_princ_list;
587 while (p != NULL && p->next != NULL)
590 gssd_k5_kt_princ_list = ple;
606 * Given a principal, find an existing ple structure, or create one
608 static struct gssd_k5_kt_princ *
609 get_ple_by_princ(krb5_context context, krb5_principal princ)
611 struct gssd_k5_kt_princ *ple;
613 /* Need to serialize list if we ever become multi-threaded! */
615 ple = find_ple_by_princ(context, princ);
617 ple = new_ple(context, princ);
624 * Given a (possibly unqualified) hostname,
625 * return the fully qualified (lower-case!) hostname
628 get_full_hostname(const char *inhost, char *outhost, int outhostlen)
630 struct addrinfo *addrs = NULL;
631 struct addrinfo hints;
635 memset(&hints, 0, sizeof(hints));
636 hints.ai_socktype = SOCK_STREAM;
637 hints.ai_family = PF_UNSPEC;
638 hints.ai_flags = AI_CANONNAME;
640 /* Get full target hostname */
641 retval = getaddrinfo(inhost, NULL, &hints, &addrs);
643 printerr(1, "%s while getting full hostname for '%s'\n",
644 gai_strerror(retval), inhost);
647 strncpy(outhost, addrs->ai_canonname, outhostlen);
649 for (c = outhost; *c != '\0'; c++)
652 printerr(3, "Full hostname for '%s' is '%s'\n", inhost, outhost);
659 * If principal matches the given realm and service name,
660 * and has *any* instance (hostname), return 1.
661 * Otherwise return 0, indicating no match.
664 realm_and_service_match(krb5_context context, krb5_principal p,
665 const char *realm, const char *service)
668 /* Must have two components */
671 if ((strlen(realm) == p->realm.length)
672 && (strncmp(realm, p->realm.data, p->realm.length) == 0)
673 && (strlen(service) == p->data[0].length)
674 && (strncmp(service, p->data[0].data, p->data[0].length) == 0))
677 const char *name, *inst;
679 if (p->name.name_string.len != 2)
681 name = krb5_principal_get_comp_string(context, p, 0);
682 inst = krb5_principal_get_comp_string(context, p, 1);
683 if (name == NULL || inst == NULL)
685 if ((strcmp(realm, p->realm) == 0)
686 && (strcmp(service, name) == 0))
693 * Search the given keytab file looking for an entry with the given
694 * service name and realm, ignoring hostname (instance).
698 * non-zero => An error occurred
700 * If a keytab entry is found, "found" is set to one, and the keytab
701 * entry is returned in "kte". Otherwise, "found" is zero, and the
702 * value of "kte" is unpredictable.
705 gssd_search_krb5_keytab(krb5_context context, krb5_keytab kt,
706 const char *realm, const char *service,
707 int *found, krb5_keytab_entry *kte)
709 krb5_kt_cursor cursor;
710 krb5_error_code code;
711 struct gssd_k5_kt_princ *ple;
713 char kt_name[BUFSIZ];
724 * Look through each entry in the keytab file and determine
725 * if we might want to use it as machine credentials. If so,
726 * save info in the global principal list (gssd_k5_kt_princ_list).
728 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
729 k5err = gssd_k5_err_msg(context, code);
730 printerr(0, "ERROR: %s attempting to get keytab name\n", k5err);
734 if ((code = krb5_kt_start_seq_get(context, kt, &cursor))) {
735 k5err = gssd_k5_err_msg(context, code);
736 printerr(0, "ERROR: %s while beginning keytab scan "
737 "for keytab '%s'\n", k5err, kt_name);
742 while ((code = krb5_kt_next_entry(context, kt, kte, &cursor)) == 0) {
743 if ((code = krb5_unparse_name(context, kte->principal,
745 k5err = gssd_k5_err_msg(context, code);
746 printerr(0, "WARNING: Skipping keytab entry because "
747 "we failed to unparse principal name: %s\n",
749 k5_free_kt_entry(context, kte);
752 printerr(4, "Processing keytab entry for principal '%s'\n",
754 /* Use the first matching keytab entry found */
755 if ((realm_and_service_match(context, kte->principal, realm,
757 printerr(4, "We WILL use this entry (%s)\n", pname);
758 ple = get_ple_by_princ(context, kte->principal);
760 * Return, don't free, keytab entry if
761 * we were successful!
765 k5_free_kt_entry(context, kte);
770 k5_free_unparsed_name(context, pname);
774 printerr(4, "We will NOT use this entry (%s)\n",
777 k5_free_unparsed_name(context, pname);
778 k5_free_kt_entry(context, kte);
781 if ((code = krb5_kt_end_seq_get(context, kt, &cursor))) {
782 k5err = gssd_k5_err_msg(context, code);
783 printerr(0, "WARNING: %s while ending keytab scan for "
784 "keytab '%s'\n", k5err, kt_name);
794 * Find a keytab entry to use for a given target hostname.
795 * Tries to find the most appropriate keytab to use given the
796 * name of the host we are trying to connect with.
799 find_keytab_entry(krb5_context context, krb5_keytab kt, const char *hostname,
800 krb5_keytab_entry *kte)
802 krb5_error_code code;
803 const char *svcnames[] = { "root", "nfs", "host", NULL };
804 char **realmnames = NULL;
805 char myhostname[NI_MAXHOST], targethostname[NI_MAXHOST];
807 char *default_realm = NULL;
810 int tried_all = 0, tried_default = 0;
811 krb5_principal princ;
814 /* Get full target hostname */
815 retval = get_full_hostname(hostname, targethostname,
816 sizeof(targethostname));
820 /* Get full local hostname */
821 retval = gethostname(myhostname, sizeof(myhostname));
823 k5err = gssd_k5_err_msg(context, retval);
824 printerr(1, "%s while getting local hostname\n", k5err);
827 retval = get_full_hostname(myhostname, myhostname, sizeof(myhostname));
831 code = krb5_get_default_realm(context, &default_realm);
834 k5err = gssd_k5_err_msg(context, code);
835 printerr(1, "%s while getting default realm name\n", k5err);
840 * Get the realm name(s) for the target hostname.
841 * In reality, this function currently only returns a
842 * single realm, but we code with the assumption that
843 * someday it may actually return a list.
845 code = krb5_get_host_realm(context, targethostname, &realmnames);
847 k5err = gssd_k5_err_msg(context, code);
848 printerr(0, "ERROR: %s while getting realm(s) for host '%s'\n",
849 k5err, targethostname);
855 * Try the "appropriate" realm first, and if nothing found for that
856 * realm, try the default realm (if it hasn't already been tried).
859 realm = realmnames[i];
864 realm = default_realm;
866 if (tried_all && tried_default)
868 if (strcmp(realm, default_realm) == 0)
870 for (j = 0; svcnames[j] != NULL; j++) {
871 code = krb5_build_principal_ext(context, &princ,
880 k5err = gssd_k5_err_msg(context, code);
881 printerr(1, "%s while building principal for "
882 "'%s/%s@%s'\n", k5err, svcnames[j],
886 code = krb5_kt_get_entry(context, kt, princ, 0, 0, kte);
887 krb5_free_principal(context, princ);
889 k5err = gssd_k5_err_msg(context, code);
890 printerr(3, "%s while getting keytab entry for "
891 "'%s/%s@%s'\n", k5err, svcnames[j],
894 printerr(3, "Success getting keytab entry for "
896 svcnames[j], myhostname, realm);
903 * Nothing found with our hostname instance, now look for
904 * names with any instance (they must have an instance)
906 for (j = 0; svcnames[j] != NULL; j++) {
908 code = gssd_search_krb5_keytab(context, kt, realm,
909 svcnames[j], &found, kte);
910 if (!code && found) {
911 printerr(3, "Success getting keytab entry for "
912 "%s/*@%s\n", svcnames[j], realm);
919 realm = realmnames[i];
924 k5_free_default_realm(context, default_realm);
926 krb5_free_host_realm(context, realmnames);
932 static inline int data_is_equal(krb5_data d1, krb5_data d2)
934 return (d1.length == d2.length
935 && memcmp(d1.data, d2.data, d1.length) == 0);
939 check_for_tgt(krb5_context context, krb5_ccache ccache,
940 krb5_principal principal)
947 ret = krb5_cc_start_seq_get(context, ccache, &cur);
952 (ret = krb5_cc_next_cred(context, ccache, &cur, &creds)) == 0) {
953 if (creds.server->length == 2 &&
954 data_is_equal(creds.server->realm,
956 creds.server->data[0].length == 6 &&
957 memcmp(creds.server->data[0].data,
959 data_is_equal(creds.server->data[1],
961 creds.times.endtime > time(NULL))
963 krb5_free_cred_contents(context, &creds);
965 krb5_cc_end_seq_get(context, ccache, &cur);
971 query_krb5_ccache(const char* cred_cache, char **ret_princname,
975 krb5_context context;
977 krb5_principal principal;
982 ret = krb5_init_context(&context);
986 if(!cred_cache || krb5_cc_resolve(context, cred_cache, &ccache))
989 if (krb5_cc_set_flags(context, ccache, 0))
992 ret = krb5_cc_get_principal(context, ccache, &principal);
996 found = check_for_tgt(context, ccache, principal);
998 ret = krb5_unparse_name(context, principal, &princstring);
1000 if ((str = strchr(princstring, '@')) != NULL) {
1002 *ret_princname = strdup(princstring);
1003 *ret_realm = strdup(str+1);
1005 k5_free_unparsed_name(context, princstring);
1010 krb5_free_principal(context, principal);
1012 krb5_cc_set_flags(context, ccache, KRB5_TC_OPENCLOSE);
1013 krb5_cc_close(context, ccache);
1015 krb5_free_context(context);
1019 /*==========================*/
1020 /*=== External routines ===*/
1021 /*==========================*/
1024 * Attempt to find the best match for a credentials cache file
1025 * given only a UID. We really need more information, but we
1026 * do the best we can.
1029 * 0 => a ccache was found
1030 * 1 => no ccache was found
1033 gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername, char *dirname)
1035 char buf[MAX_NETOBJ_SZ];
1038 printerr(2, "getting credentials for client with uid %u for "
1039 "server %s\n", uid, servername);
1040 memset(buf, 0, sizeof(buf));
1041 if (gssd_find_existing_krb5_ccache(uid, dirname, &d)) {
1042 snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname, d->d_name);
1047 printerr(2, "using %s as credentials cache for client with "
1048 "uid %u for server %s\n", buf, uid, servername);
1049 gssd_set_krb5_ccache_name(buf);
1054 * Let the gss code know where to find the machine credentials ccache.
1060 gssd_setup_krb5_machine_gss_ccache(char *ccname)
1062 printerr(2, "using %s as credentials cache for machine creds\n",
1064 gssd_set_krb5_ccache_name(ccname);
1068 * Return an array of pointers to names of credential cache files
1069 * which can be used to try to create gss contexts with a server.
1072 * 0 => list is attached
1076 gssd_get_krb5_machine_cred_list(char ***list)
1080 int listsize = listinc;
1083 struct gssd_k5_kt_princ *ple;
1085 /* Assume failure */
1087 *list = (char **) NULL;
1089 if ((l = (char **) malloc(listsize * sizeof(char *))) == NULL) {
1094 /* Need to serialize list if we ever become multi-threaded! */
1096 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
1098 /* Make sure cred is up-to-date before returning it */
1099 retval = gssd_refresh_krb5_machine_credential(NULL, ple, 0);
1102 if (i + 1 > listsize) {
1103 listsize += listinc;
1105 realloc(l, listsize * sizeof(char *));
1111 if ((l[i++] = strdup(ple->ccname)) == NULL) {
1128 * Frees the list of names returned in get_krb5_machine_cred_list()
1131 gssd_free_krb5_machine_cred_list(char **list)
1137 for (n = list; n && *n; n++) {
1144 * Called upon exit. Destroys machine credentials.
1147 gssd_destroy_krb5_machine_creds(void)
1149 krb5_context context;
1150 krb5_error_code code = 0;
1152 struct gssd_k5_kt_princ *ple;
1155 code = krb5_init_context(&context);
1157 k5err = gssd_k5_err_msg(NULL, code);
1158 printerr(0, "ERROR: %s while initializing krb5\n", k5err);
1162 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
1165 if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
1166 k5err = gssd_k5_err_msg(context, code);
1167 printerr(0, "WARNING: %s while resolving credential "
1168 "cache '%s' for destruction\n", k5err,
1173 if ((code = krb5_cc_destroy(context, ccache))) {
1174 k5err = gssd_k5_err_msg(context, code);
1175 printerr(0, "WARNING: %s while destroying credential "
1176 "cache '%s'\n", k5err, ple->ccname);
1181 krb5_free_context(context);
1185 * Obtain (or refresh if necessary) Kerberos machine credentials
1188 gssd_refresh_krb5_machine_credential(char *hostname,
1189 struct gssd_k5_kt_princ *ple, int nocache)
1191 krb5_error_code code = 0;
1192 krb5_context context;
1193 krb5_keytab kt = NULL;;
1197 if (hostname == NULL && ple == NULL)
1200 code = krb5_init_context(&context);
1202 k5err = gssd_k5_err_msg(NULL, code);
1203 printerr(0, "ERROR: %s: %s while initializing krb5 context\n",
1209 if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
1210 k5err = gssd_k5_err_msg(context, code);
1211 printerr(0, "ERROR: %s: %s while resolving keytab '%s'\n",
1212 __func__, k5err, keytabfile);
1217 krb5_keytab_entry kte;
1219 code = find_keytab_entry(context, kt, hostname, &kte);
1221 printerr(0, "ERROR: %s: no usable keytab entry found "
1222 "in keytab %s for connection with host %s\n",
1223 __FUNCTION__, keytabfile, hostname);
1228 ple = get_ple_by_princ(context, kte.principal);
1229 k5_free_kt_entry(context, &kte);
1232 if ((krb5_unparse_name(context, kte.principal, &pname))) {
1235 printerr(0, "ERROR: %s: Could not locate or create "
1236 "ple struct for principal %s for connection "
1238 __FUNCTION__, pname ? pname : "<unparsable>",
1240 if (pname) k5_free_unparsed_name(context, pname);
1244 retval = gssd_get_single_krb5_cred(context, kt, ple, nocache);
1247 krb5_kt_close(context, kt);
1248 krb5_free_context(context);
1254 * A common routine for getting the Kerberos error message
1257 gssd_k5_err_msg(krb5_context context, krb5_error_code code)
1259 const char *origmsg;
1262 #if HAVE_KRB5_GET_ERROR_MESSAGE
1263 if (context != NULL) {
1264 origmsg = krb5_get_error_message(context, code);
1265 msg = strdup(origmsg);
1266 krb5_free_error_message(context, origmsg);
1272 return strdup(error_message(code));
1274 if (context != NULL)
1275 return strdup(krb5_get_err_text(context, code));
1277 return strdup(error_message(code));
1282 * Return default Kerberos realm
1285 gssd_k5_get_default_realm(char **def_realm)
1287 krb5_context context;
1289 if (krb5_init_context(&context))
1292 krb5_get_default_realm(context, def_realm);
1294 krb5_free_context(context);