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);
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)
364 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
365 krb5_get_init_creds_opt *init_opts = NULL;
367 krb5_get_init_creds_opt options;
369 krb5_get_init_creds_opt *opts;
371 krb5_ccache ccache = NULL;
372 char kt_name[BUFSIZ];
373 char cc_name[BUFSIZ];
375 time_t now = time(0);
379 memset(&my_creds, 0, sizeof(my_creds));
381 if (ple->ccname && ple->endtime > now) {
382 printerr(2, "INFO: Credentials in CC '%s' are good until %d\n",
383 ple->ccname, ple->endtime);
388 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
389 printerr(0, "ERROR: Unable to get keytab name in "
390 "gssd_get_single_krb5_cred\n");
394 if ((krb5_unparse_name(context, ple->princ, &pname)))
397 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
398 code = krb5_get_init_creds_opt_alloc(context, &init_opts);
400 printerr(0, "ERROR: %s allocating gic options\n",
401 gssd_k5_err_msg(context, code));
404 if (krb5_get_init_creds_opt_set_addressless(context, init_opts, 1))
405 printerr(1, "WARNING: Unable to set option for addressless "
406 "tickets. May have problems behind a NAT.\n");
407 #ifdef TEST_SHORT_LIFETIME
408 /* set a short lifetime (for debugging only!) */
409 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
410 krb5_get_init_creds_opt_set_tkt_life(init_opts, 5*60);
414 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS */
416 krb5_get_init_creds_opt_init(&options);
417 krb5_get_init_creds_opt_set_address_list(&options, NULL);
418 #ifdef TEST_SHORT_LIFETIME
419 /* set a short lifetime (for debugging only!) */
420 printerr(0, "WARNING: Using (debug) short machine cred lifetime!\n");
421 krb5_get_init_creds_opt_set_tkt_life(&options, 5*60);
426 if ((code = krb5_get_init_creds_keytab(context, &my_creds, ple->princ,
427 kt, 0, NULL, opts))) {
428 printerr(1, "WARNING: %s while getting initial ticket for "
429 "principal '%s' using keytab '%s'\n",
430 gssd_k5_err_msg(context, code),
431 pname ? pname : "<unparsable>", kt_name);
436 * Initialize cache file which we're going to be using
440 cache_type = "MEMORY";
443 snprintf(cc_name, sizeof(cc_name), "%s:%s/%s%s_%s",
445 ccachesearch[0], GSSD_DEFAULT_CRED_PREFIX,
446 GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm);
447 ple->endtime = my_creds.times.endtime;
448 if (ple->ccname != NULL)
450 ple->ccname = strdup(cc_name);
451 if (ple->ccname == NULL) {
452 printerr(0, "ERROR: no storage to duplicate credentials "
453 "cache name '%s'\n", cc_name);
457 if ((code = krb5_cc_resolve(context, cc_name, &ccache))) {
458 printerr(0, "ERROR: %s while opening credential cache '%s'\n",
459 gssd_k5_err_msg(context, code), cc_name);
462 if ((code = krb5_cc_initialize(context, ccache, ple->princ))) {
463 printerr(0, "ERROR: %s while initializing credential "
464 "cache '%s'\n", gssd_k5_err_msg(context, code),
468 if ((code = krb5_cc_store_cred(context, ccache, &my_creds))) {
469 printerr(0, "ERROR: %s while storing credentials in '%s'\n",
470 gssd_k5_err_msg(context, code), cc_name);
475 printerr(2, "Successfully obtained machine credentials for "
476 "principal '%s' stored in ccache '%s'\n", pname, cc_name);
478 #if HAVE_KRB5_GET_INIT_CREDS_OPT_SET_ADDRESSLESS
480 krb5_get_init_creds_opt_free(context, init_opts);
483 k5_free_unparsed_name(context, pname);
485 krb5_cc_close(context, ccache);
486 krb5_free_cred_contents(context, &my_creds);
491 * Depending on the version of Kerberos, we either need to use
492 * a private function, or simply set the environment variable.
495 gssd_set_krb5_ccache_name(char *ccname)
497 #ifdef USE_GSS_KRB5_CCACHE_NAME
498 u_int maj_stat, min_stat;
500 printerr(2, "using gss_krb5_ccache_name to select krb5 ccache %s\n",
502 maj_stat = gss_krb5_ccache_name(&min_stat, ccname, NULL);
503 if (maj_stat != GSS_S_COMPLETE) {
504 printerr(0, "WARNING: gss_krb5_ccache_name with "
505 "name '%s' failed (%s)\n",
506 ccname, error_message(min_stat));
510 * Set the KRB5CCNAME environment variable to tell the krb5 code
511 * which credentials cache to use. (Instead of using the private
512 * function above for which there is no generic gssapi
515 printerr(2, "using environment variable to select krb5 ccache %s\n",
517 setenv("KRB5CCNAME", ccname, 1);
522 * Given a principal, find a matching ple structure
524 static struct gssd_k5_kt_princ *
525 find_ple_by_princ(krb5_context context, krb5_principal princ)
527 struct gssd_k5_kt_princ *ple;
529 for (ple = gssd_k5_kt_princ_list; ple != NULL; ple = ple->next) {
530 if (krb5_principal_compare(context, ple->princ, princ))
538 * Create, initialize, and add a new ple structure to the global list
540 static struct gssd_k5_kt_princ *
541 new_ple(krb5_context context, krb5_principal princ)
543 struct gssd_k5_kt_princ *ple = NULL, *p;
544 krb5_error_code code;
546 int is_default_realm = 0;
548 ple = malloc(sizeof(struct gssd_k5_kt_princ));
551 memset(ple, 0, sizeof(*ple));
554 ple->realm = strndup(princ->realm.data,
555 princ->realm.length);
557 ple->realm = strdup(princ->realm);
559 if (ple->realm == NULL)
561 code = krb5_copy_principal(context, princ, &ple->princ);
566 * Add new entry onto the list (if this is the default
567 * realm, always add to the front of the list)
570 code = krb5_get_default_realm(context, &default_realm);
572 if (strcmp(ple->realm, default_realm) == 0)
573 is_default_realm = 1;
574 k5_free_default_realm(context, default_realm);
577 if (is_default_realm) {
578 ple->next = gssd_k5_kt_princ_list;
579 gssd_k5_kt_princ_list = ple;
581 p = gssd_k5_kt_princ_list;
582 while (p != NULL && p->next != NULL)
585 gssd_k5_kt_princ_list = ple;
601 * Given a principal, find an existing ple structure, or create one
603 static struct gssd_k5_kt_princ *
604 get_ple_by_princ(krb5_context context, krb5_principal princ)
606 struct gssd_k5_kt_princ *ple;
608 /* Need to serialize list if we ever become multi-threaded! */
610 ple = find_ple_by_princ(context, princ);
612 ple = new_ple(context, princ);
619 * Given a (possibly unqualified) hostname,
620 * return the fully qualified (lower-case!) hostname
623 get_full_hostname(const char *inhost, char *outhost, int outhostlen)
625 struct addrinfo *addrs = NULL;
626 struct addrinfo hints;
630 memset(&hints, 0, sizeof(hints));
631 hints.ai_socktype = SOCK_STREAM;
632 hints.ai_family = PF_UNSPEC;
633 hints.ai_flags = AI_CANONNAME;
635 /* Get full target hostname */
636 retval = getaddrinfo(inhost, NULL, &hints, &addrs);
638 printerr(1, "%s while getting full hostname for '%s'\n",
639 gai_strerror(retval), inhost);
642 strncpy(outhost, addrs->ai_canonname, outhostlen);
644 for (c = outhost; *c != '\0'; c++)
647 printerr(3, "Full hostname for '%s' is '%s'\n", inhost, outhost);
654 * If principal matches the given realm and service name,
655 * and has *any* instance (hostname), return 1.
656 * Otherwise return 0, indicating no match.
659 realm_and_service_match(krb5_context context, krb5_principal p,
660 const char *realm, const char *service)
663 /* Must have two components */
666 if ((strlen(realm) == p->realm.length)
667 && (strncmp(realm, p->realm.data, p->realm.length) == 0)
668 && (strlen(service) == p->data[0].length)
669 && (strncmp(service, p->data[0].data, p->data[0].length) == 0))
672 const char *name, *inst;
674 if (p->name.name_string.len != 2)
676 name = krb5_principal_get_comp_string(context, p, 0);
677 inst = krb5_principal_get_comp_string(context, p, 1);
678 if (name == NULL || inst == NULL)
680 if ((strcmp(realm, p->realm) == 0)
681 && (strcmp(service, name) == 0))
688 * Search the given keytab file looking for an entry with the given
689 * service name and realm, ignoring hostname (instance).
693 * non-zero => An error occurred
695 * If a keytab entry is found, "found" is set to one, and the keytab
696 * entry is returned in "kte". Otherwise, "found" is zero, and the
697 * value of "kte" is unpredictable.
700 gssd_search_krb5_keytab(krb5_context context, krb5_keytab kt,
701 const char *realm, const char *service,
702 int *found, krb5_keytab_entry *kte)
704 krb5_kt_cursor cursor;
705 krb5_error_code code;
706 struct gssd_k5_kt_princ *ple;
708 char kt_name[BUFSIZ];
718 * Look through each entry in the keytab file and determine
719 * if we might want to use it as machine credentials. If so,
720 * save info in the global principal list (gssd_k5_kt_princ_list).
722 if ((code = krb5_kt_get_name(context, kt, kt_name, BUFSIZ))) {
723 printerr(0, "ERROR: %s attempting to get keytab name\n",
724 gssd_k5_err_msg(context, code));
728 if ((code = krb5_kt_start_seq_get(context, kt, &cursor))) {
729 printerr(0, "ERROR: %s while beginning keytab scan "
731 gssd_k5_err_msg(context, code), kt_name);
736 while ((code = krb5_kt_next_entry(context, kt, kte, &cursor)) == 0) {
737 if ((code = krb5_unparse_name(context, kte->principal,
739 printerr(0, "WARNING: Skipping keytab entry because "
740 "we failed to unparse principal name: %s\n",
741 gssd_k5_err_msg(context, code));
742 k5_free_kt_entry(context, kte);
745 printerr(4, "Processing keytab entry for principal '%s'\n",
747 /* Use the first matching keytab entry found */
748 if ((realm_and_service_match(context, kte->principal, realm,
750 printerr(4, "We WILL use this entry (%s)\n", pname);
751 ple = get_ple_by_princ(context, kte->principal);
753 * Return, don't free, keytab entry if
754 * we were successful!
758 k5_free_kt_entry(context, kte);
763 k5_free_unparsed_name(context, pname);
767 printerr(4, "We will NOT use this entry (%s)\n",
770 k5_free_unparsed_name(context, pname);
771 k5_free_kt_entry(context, kte);
774 if ((code = krb5_kt_end_seq_get(context, kt, &cursor))) {
775 printerr(0, "WARNING: %s while ending keytab scan for "
777 gssd_k5_err_msg(context, code), kt_name);
786 * Find a keytab entry to use for a given target hostname.
787 * Tries to find the most appropriate keytab to use given the
788 * name of the host we are trying to connect with.
791 find_keytab_entry(krb5_context context, krb5_keytab kt, const char *hostname,
792 krb5_keytab_entry *kte)
794 krb5_error_code code;
795 const char *svcnames[] = { "root", "nfs", "host", NULL };
796 char **realmnames = NULL;
797 char myhostname[NI_MAXHOST], targethostname[NI_MAXHOST];
799 char *default_realm = NULL;
801 int tried_all = 0, tried_default = 0;
802 krb5_principal princ;
805 /* Get full target hostname */
806 retval = get_full_hostname(hostname, targethostname,
807 sizeof(targethostname));
811 /* Get full local hostname */
812 retval = gethostname(myhostname, sizeof(myhostname));
814 printerr(1, "%s while getting local hostname\n",
815 gssd_k5_err_msg(context, retval));
818 retval = get_full_hostname(myhostname, myhostname, sizeof(myhostname));
822 code = krb5_get_default_realm(context, &default_realm);
825 printerr(1, "%s while getting default realm name\n",
826 gssd_k5_err_msg(context, code));
831 * Get the realm name(s) for the target hostname.
832 * In reality, this function currently only returns a
833 * single realm, but we code with the assumption that
834 * someday it may actually return a list.
836 code = krb5_get_host_realm(context, targethostname, &realmnames);
838 printerr(0, "ERROR: %s while getting realm(s) for host '%s'\n",
839 gssd_k5_err_msg(context, code), targethostname);
845 * Try the "appropriate" realm first, and if nothing found for that
846 * realm, try the default realm (if it hasn't already been tried).
849 realm = realmnames[i];
854 realm = default_realm;
856 if (tried_all && tried_default)
858 if (strcmp(realm, default_realm) == 0)
860 for (j = 0; svcnames[j] != NULL; j++) {
861 code = krb5_build_principal_ext(context, &princ,
870 printerr(1, "%s while building principal for "
872 gssd_k5_err_msg(context, code),
873 svcnames[j], myhostname, realm);
876 code = krb5_kt_get_entry(context, kt, princ, 0, 0, kte);
877 krb5_free_principal(context, princ);
879 printerr(3, "%s while getting keytab entry for "
881 gssd_k5_err_msg(context, code),
882 svcnames[j], myhostname, realm);
884 printerr(3, "Success getting keytab entry for "
886 svcnames[j], myhostname, realm);
893 * Nothing found with our hostname instance, now look for
894 * names with any instance (they must have an instance)
896 for (j = 0; svcnames[j] != NULL; j++) {
898 code = gssd_search_krb5_keytab(context, kt, realm,
899 svcnames[j], &found, kte);
900 if (!code && found) {
901 printerr(3, "Success getting keytab entry for "
902 "%s/*@%s\n", svcnames[j], realm);
909 realm = realmnames[i];
914 k5_free_default_realm(context, default_realm);
916 krb5_free_host_realm(context, realmnames);
921 static inline int data_is_equal(krb5_data d1, krb5_data d2)
923 return (d1.length == d2.length
924 && memcmp(d1.data, d2.data, d1.length) == 0);
928 check_for_tgt(krb5_context context, krb5_ccache ccache,
929 krb5_principal principal)
936 ret = krb5_cc_start_seq_get(context, ccache, &cur);
941 (ret = krb5_cc_next_cred(context, ccache, &cur, &creds)) == 0) {
942 if (creds.server->length == 2 &&
943 data_is_equal(creds.server->realm,
945 creds.server->data[0].length == 6 &&
946 memcmp(creds.server->data[0].data,
948 data_is_equal(creds.server->data[1],
950 creds.times.endtime > time(NULL))
952 krb5_free_cred_contents(context, &creds);
954 krb5_cc_end_seq_get(context, ccache, &cur);
960 query_krb5_ccache(const char* cred_cache, char **ret_princname,
964 krb5_context context;
966 krb5_principal principal;
971 ret = krb5_init_context(&context);
975 if(!cred_cache || krb5_cc_resolve(context, cred_cache, &ccache))
978 if (krb5_cc_set_flags(context, ccache, 0))
981 ret = krb5_cc_get_principal(context, ccache, &principal);
985 found = check_for_tgt(context, ccache, principal);
987 ret = krb5_unparse_name(context, principal, &princstring);
989 if ((str = strchr(princstring, '@')) != NULL) {
991 *ret_princname = strdup(princstring);
992 *ret_realm = strdup(str+1);
994 k5_free_unparsed_name(context, princstring);
999 krb5_free_principal(context, principal);
1001 krb5_cc_set_flags(context, ccache, KRB5_TC_OPENCLOSE);
1002 krb5_cc_close(context, ccache);
1004 krb5_free_context(context);
1008 /*==========================*/
1009 /*=== External routines ===*/
1010 /*==========================*/
1013 * Attempt to find the best match for a credentials cache file
1014 * given only a UID. We really need more information, but we
1015 * do the best we can.
1018 * 0 => a ccache was found
1019 * 1 => no ccache was found
1022 gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername, char *dirname)
1024 char buf[MAX_NETOBJ_SZ];
1027 printerr(2, "getting credentials for client with uid %u for "
1028 "server %s\n", uid, servername);
1029 memset(buf, 0, sizeof(buf));
1030 if (gssd_find_existing_krb5_ccache(uid, dirname, &d)) {
1031 snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname, d->d_name);
1036 printerr(2, "using %s as credentials cache for client with "
1037 "uid %u for server %s\n", buf, uid, servername);
1038 gssd_set_krb5_ccache_name(buf);
1043 * Let the gss code know where to find the machine credentials ccache.
1049 gssd_setup_krb5_machine_gss_ccache(char *ccname)
1051 printerr(2, "using %s as credentials cache for machine creds\n",
1053 gssd_set_krb5_ccache_name(ccname);
1057 * Return an array of pointers to names of credential cache files
1058 * which can be used to try to create gss contexts with a server.
1061 * 0 => list is attached
1065 gssd_get_krb5_machine_cred_list(char ***list)
1069 int listsize = listinc;
1072 struct gssd_k5_kt_princ *ple;
1074 /* Assume failure */
1076 *list = (char **) NULL;
1078 if ((l = (char **) malloc(listsize * sizeof(char *))) == NULL) {
1083 /* Need to serialize list if we ever become multi-threaded! */
1085 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
1087 /* Make sure cred is up-to-date before returning it */
1088 retval = gssd_refresh_krb5_machine_credential(NULL, ple);
1091 if (i + 1 > listsize) {
1092 listsize += listinc;
1094 realloc(l, listsize * sizeof(char *));
1100 if ((l[i++] = strdup(ple->ccname)) == NULL) {
1117 * Frees the list of names returned in get_krb5_machine_cred_list()
1120 gssd_free_krb5_machine_cred_list(char **list)
1126 for (n = list; n && *n; n++) {
1133 * Called upon exit. Destroys machine credentials.
1136 gssd_destroy_krb5_machine_creds(void)
1138 krb5_context context;
1139 krb5_error_code code = 0;
1141 struct gssd_k5_kt_princ *ple;
1143 code = krb5_init_context(&context);
1145 printerr(0, "ERROR: %s while initializing krb5\n",
1146 gssd_k5_err_msg(NULL, code));
1150 for (ple = gssd_k5_kt_princ_list; ple; ple = ple->next) {
1153 if ((code = krb5_cc_resolve(context, ple->ccname, &ccache))) {
1154 printerr(0, "WARNING: %s while resolving credential "
1155 "cache '%s' for destruction\n",
1156 gssd_k5_err_msg(context, code), ple->ccname);
1160 if ((code = krb5_cc_destroy(context, ccache))) {
1161 printerr(0, "WARNING: %s while destroying credential "
1163 gssd_k5_err_msg(context, code), ple->ccname);
1167 krb5_free_context(context);
1171 * Obtain (or refresh if necessary) Kerberos machine credentials
1174 gssd_refresh_krb5_machine_credential(char *hostname,
1175 struct gssd_k5_kt_princ *ple)
1177 krb5_error_code code = 0;
1178 krb5_context context;
1179 krb5_keytab kt = NULL;;
1182 if (hostname == NULL && ple == NULL)
1185 code = krb5_init_context(&context);
1187 printerr(0, "ERROR: %s: %s while initializing krb5 context\n",
1188 __FUNCTION__, gssd_k5_err_msg(NULL, code));
1193 if ((code = krb5_kt_resolve(context, keytabfile, &kt))) {
1194 printerr(0, "ERROR: %s: %s while resolving keytab '%s'\n",
1195 __FUNCTION__, gssd_k5_err_msg(context, code),
1201 krb5_keytab_entry kte;
1203 code = find_keytab_entry(context, kt, hostname, &kte);
1205 printerr(0, "ERROR: %s: no usable keytab entry found "
1206 "in keytab %s for connection with host %s\n",
1207 __FUNCTION__, keytabfile, hostname);
1212 ple = get_ple_by_princ(context, kte.principal);
1213 k5_free_kt_entry(context, &kte);
1216 if ((krb5_unparse_name(context, kte.principal, &pname))) {
1219 printerr(0, "ERROR: %s: Could not locate or create "
1220 "ple struct for principal %s for connection "
1222 __FUNCTION__, pname ? pname : "<unparsable>",
1224 if (pname) k5_free_unparsed_name(context, pname);
1228 retval = gssd_get_single_krb5_cred(context, kt, ple);
1231 krb5_kt_close(context, kt);
1232 krb5_free_context(context);
1237 * A common routine for getting the Kerberos error message
1240 gssd_k5_err_msg(krb5_context context, krb5_error_code code)
1242 const char *msg = NULL;
1243 #if HAVE_KRB5_GET_ERROR_MESSAGE
1244 if (context != NULL)
1245 msg = krb5_get_error_message(context, code);
1250 return error_message(code);
1252 if (context != NULL)
1253 return krb5_get_err_text(context, code);
1255 return error_message(code);
1260 * Return default Kerberos realm
1263 gssd_k5_get_default_realm(char **def_realm)
1265 krb5_context context;
1267 if (krb5_init_context(&context))
1270 krb5_get_default_realm(context, def_realm);
1272 krb5_free_context(context);