2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <sys/queue.h>
36 #include <sys/syslog.h>
41 #ifndef WITHOUT_KERBEROS
50 #include <gssapi/gssapi.h>
52 #include <rpc/rpc_com.h>
56 #ifndef _PATH_GSS_MECH
57 #define _PATH_GSS_MECH "/etc/gss/mech"
59 #ifndef _PATH_GSSDSOCK
60 #define _PATH_GSSDSOCK "/var/run/gssd.sock"
64 LIST_ENTRY(gss_resource) gr_link;
65 uint64_t gr_id; /* indentifier exported to kernel */
66 void* gr_res; /* GSS-API resource pointer */
68 LIST_HEAD(gss_resource_list, gss_resource) gss_resources;
69 int gss_resource_count;
71 uint32_t gss_start_time;
73 static char ccfile_dirlist[PATH_MAX + 1], ccfile_substring[NAME_MAX + 1];
74 static char pref_realm[1024];
77 static void gssd_load_mech(void);
78 static int find_ccache_file(const char *, uid_t, char *);
79 static int is_a_valid_tgt_cache(const char *, uid_t, int *, time_t *);
80 static void gssd_verbose_out(const char *, ...);
82 extern void gssd_1(struct svc_req *rqstp, SVCXPRT *transp);
83 extern int gssd_syscall(char *path);
86 main(int argc, char **argv)
89 * We provide an RPC service on a local-domain socket. The
90 * kernel's GSS-API code will pass what it can't handle
93 struct sockaddr_un sun;
94 int fd, oldmask, ch, debug;
98 * Initialize the credential cache file name substring and the
99 * search directory list.
101 strlcpy(ccfile_substring, "krb5cc_", sizeof(ccfile_substring));
102 ccfile_dirlist[0] = '\0';
103 pref_realm[0] = '\0';
106 while ((ch = getopt(argc, argv, "dvs:c:r:")) != -1) {
115 #ifndef WITHOUT_KERBEROS
117 * Set the directory search list. This enables use of
118 * find_ccache_file() to search the directories for a
119 * suitable credentials cache file.
121 strlcpy(ccfile_dirlist, optarg, sizeof(ccfile_dirlist));
123 errx(1, "This option not available when built"
124 " without MK_KERBEROS\n");
129 * Specify a non-default credential cache file
132 strlcpy(ccfile_substring, optarg,
133 sizeof(ccfile_substring));
137 * Set the preferred realm for the credential cache tgt.
139 strlcpy(pref_realm, optarg, sizeof(pref_realm));
143 "usage: %s [-d] [-s dir-list] [-c file-substring]"
144 " [-r preferred-realm]\n", argv[0]);
155 memset(&sun, 0, sizeof sun);
156 sun.sun_family = AF_LOCAL;
157 unlink(_PATH_GSSDSOCK);
158 strcpy(sun.sun_path, _PATH_GSSDSOCK);
159 sun.sun_len = SUN_LEN(&sun);
160 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
162 if (debug_level == 0) {
163 syslog(LOG_ERR, "Can't create local gssd socket");
166 err(1, "Can't create local gssd socket");
168 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
169 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
170 if (debug_level == 0) {
171 syslog(LOG_ERR, "Can't bind local gssd socket");
174 err(1, "Can't bind local gssd socket");
177 if (listen(fd, SOMAXCONN) < 0) {
178 if (debug_level == 0) {
179 syslog(LOG_ERR, "Can't listen on local gssd socket");
182 err(1, "Can't listen on local gssd socket");
184 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
186 if (debug_level == 0) {
188 "Can't create transport for local gssd socket");
191 err(1, "Can't create transport for local gssd socket");
193 if (!svc_reg(xprt, GSSD, GSSDVERS, gssd_1, NULL)) {
194 if (debug_level == 0) {
196 "Can't register service for local gssd socket");
199 err(1, "Can't register service for local gssd socket");
202 LIST_INIT(&gss_resources);
204 gss_start_time = time(0);
206 gssd_syscall(_PATH_GSSDSOCK);
218 char *name, *oid, *lib, *kobj;
220 fp = fopen(_PATH_GSS_MECH, "r");
224 while (fgets(buf, sizeof(buf), fp)) {
228 name = strsep(&p, "\t\n ");
229 if (p) while (isspace(*p)) p++;
230 oid = strsep(&p, "\t\n ");
231 if (p) while (isspace(*p)) p++;
232 lib = strsep(&p, "\t\n ");
233 if (p) while (isspace(*p)) p++;
234 kobj = strsep(&p, "\t\n ");
235 if (!name || !oid || !lib || !kobj)
238 if (strcmp(kobj, "-")) {
240 * Attempt to load the kernel module if its
241 * not already present.
243 if (modfind(kobj) < 0) {
244 if (kldload(kobj) < 0) {
246 "%s: can't find or load kernel module %s for %s\n",
247 getprogname(), kobj, name);
256 gssd_find_resource(uint64_t id)
258 struct gss_resource *gr;
263 LIST_FOREACH(gr, &gss_resources, gr_link)
271 gssd_make_resource(void *res)
273 struct gss_resource *gr;
278 gr = malloc(sizeof(struct gss_resource));
281 gr->gr_id = (gss_next_id++) + ((uint64_t) gss_start_time << 32);
283 LIST_INSERT_HEAD(&gss_resources, gr, gr_link);
284 gss_resource_count++;
286 printf("%d resources allocated\n", gss_resource_count);
292 gssd_delete_resource(uint64_t id)
294 struct gss_resource *gr;
296 LIST_FOREACH(gr, &gss_resources, gr_link) {
297 if (gr->gr_id == id) {
298 LIST_REMOVE(gr, gr_link);
300 gss_resource_count--;
302 printf("%d resources allocated\n",
310 gssd_verbose_out(const char *fmt, ...)
316 if (debug_level == 0)
317 vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap);
319 vfprintf(stderr, fmt, ap);
325 gssd_null_1_svc(void *argp, void *result, struct svc_req *rqstp)
328 gssd_verbose_out("gssd_null: done\n");
333 gssd_init_sec_context_1_svc(init_sec_context_args *argp, init_sec_context_res *result, struct svc_req *rqstp)
335 gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
336 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
337 gss_name_t name = GSS_C_NO_NAME;
338 char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
341 memset(result, 0, sizeof(*result));
342 if (ccfile_dirlist[0] != '\0' && argp->cred == 0) {
344 * For the "-s" case and no credentials provided as an
345 * argument, search the directory list for an appropriate
346 * credential cache file. If the search fails, return failure.
351 cp2 = strchr(cp, ':');
354 gotone = find_ccache_file(cp, argp->uid, ccname);
360 } while (cp != NULL && *cp != '\0');
362 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
363 gssd_verbose_out("gssd_init_sec_context: -s no"
364 " credential cache file found for uid=%d\n",
370 * If there wasn't a "-s" option or the credentials have
371 * been provided as an argument, do it the old way.
372 * When credentials are provided, the uid should be root.
374 if (argp->cred != 0 && argp->uid != 0) {
375 if (debug_level == 0)
376 syslog(LOG_ERR, "gss_init_sec_context:"
377 " cred for non-root");
379 fprintf(stderr, "gss_init_sec_context:"
380 " cred for non-root\n");
382 snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
385 setenv("KRB5CCNAME", ccname, TRUE);
388 cred = gssd_find_resource(argp->cred);
390 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
391 gssd_verbose_out("gssd_init_sec_context: cred"
392 " resource not found\n");
397 ctx = gssd_find_resource(argp->ctx);
399 result->major_status = GSS_S_CONTEXT_EXPIRED;
400 gssd_verbose_out("gssd_init_sec_context: context"
401 " resource not found\n");
406 name = gssd_find_resource(argp->name);
408 result->major_status = GSS_S_BAD_NAME;
409 gssd_verbose_out("gssd_init_sec_context: name"
410 " resource not found\n");
415 result->major_status = gss_init_sec_context(&result->minor_status,
416 cred, &ctx, name, argp->mech_type,
417 argp->req_flags, argp->time_req, argp->input_chan_bindings,
418 &argp->input_token, &result->actual_mech_type,
419 &result->output_token, &result->ret_flags, &result->time_rec);
420 gssd_verbose_out("gssd_init_sec_context: done major=0x%x minor=%d"
421 " uid=%d\n", (unsigned int)result->major_status,
422 (int)result->minor_status, (int)argp->uid);
424 if (result->major_status == GSS_S_COMPLETE
425 || result->major_status == GSS_S_CONTINUE_NEEDED) {
427 result->ctx = argp->ctx;
429 result->ctx = gssd_make_resource(ctx);
436 gssd_accept_sec_context_1_svc(accept_sec_context_args *argp, accept_sec_context_res *result, struct svc_req *rqstp)
438 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
439 gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
441 gss_cred_id_t delegated_cred_handle;
443 memset(result, 0, sizeof(*result));
445 ctx = gssd_find_resource(argp->ctx);
447 result->major_status = GSS_S_CONTEXT_EXPIRED;
448 gssd_verbose_out("gssd_accept_sec_context: ctx"
449 " resource not found\n");
454 cred = gssd_find_resource(argp->cred);
456 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
457 gssd_verbose_out("gssd_accept_sec_context: cred"
458 " resource not found\n");
463 memset(result, 0, sizeof(*result));
464 result->major_status = gss_accept_sec_context(&result->minor_status,
465 &ctx, cred, &argp->input_token, argp->input_chan_bindings,
466 &src_name, &result->mech_type, &result->output_token,
467 &result->ret_flags, &result->time_rec,
468 &delegated_cred_handle);
469 gssd_verbose_out("gssd_accept_sec_context: done major=0x%x minor=%d\n",
470 (unsigned int)result->major_status, (int)result->minor_status);
472 if (result->major_status == GSS_S_COMPLETE
473 || result->major_status == GSS_S_CONTINUE_NEEDED) {
475 result->ctx = argp->ctx;
477 result->ctx = gssd_make_resource(ctx);
478 result->src_name = gssd_make_resource(src_name);
479 result->delegated_cred_handle =
480 gssd_make_resource(delegated_cred_handle);
487 gssd_delete_sec_context_1_svc(delete_sec_context_args *argp, delete_sec_context_res *result, struct svc_req *rqstp)
489 gss_ctx_id_t ctx = gssd_find_resource(argp->ctx);
492 result->major_status = gss_delete_sec_context(
493 &result->minor_status, &ctx, &result->output_token);
494 gssd_delete_resource(argp->ctx);
496 result->major_status = GSS_S_COMPLETE;
497 result->minor_status = 0;
499 gssd_verbose_out("gssd_delete_sec_context: done major=0x%x minor=%d\n",
500 (unsigned int)result->major_status, (int)result->minor_status);
506 gssd_export_sec_context_1_svc(export_sec_context_args *argp, export_sec_context_res *result, struct svc_req *rqstp)
508 gss_ctx_id_t ctx = gssd_find_resource(argp->ctx);
511 result->major_status = gss_export_sec_context(
512 &result->minor_status, &ctx,
513 &result->interprocess_token);
514 result->format = KGSS_HEIMDAL_1_1;
515 gssd_delete_resource(argp->ctx);
517 result->major_status = GSS_S_FAILURE;
518 result->minor_status = 0;
519 result->interprocess_token.length = 0;
520 result->interprocess_token.value = NULL;
522 gssd_verbose_out("gssd_export_sec_context: done major=0x%x minor=%d\n",
523 (unsigned int)result->major_status, (int)result->minor_status);
529 gssd_import_name_1_svc(import_name_args *argp, import_name_res *result, struct svc_req *rqstp)
533 result->major_status = gss_import_name(&result->minor_status,
534 &argp->input_name_buffer, argp->input_name_type, &name);
535 gssd_verbose_out("gssd_import_name: done major=0x%x minor=%d\n",
536 (unsigned int)result->major_status, (int)result->minor_status);
538 if (result->major_status == GSS_S_COMPLETE)
539 result->output_name = gssd_make_resource(name);
541 result->output_name = 0;
547 gssd_canonicalize_name_1_svc(canonicalize_name_args *argp, canonicalize_name_res *result, struct svc_req *rqstp)
549 gss_name_t name = gssd_find_resource(argp->input_name);
550 gss_name_t output_name;
552 memset(result, 0, sizeof(*result));
554 result->major_status = GSS_S_BAD_NAME;
558 result->major_status = gss_canonicalize_name(&result->minor_status,
559 name, argp->mech_type, &output_name);
560 gssd_verbose_out("gssd_canonicalize_name: done major=0x%x minor=%d\n",
561 (unsigned int)result->major_status, (int)result->minor_status);
563 if (result->major_status == GSS_S_COMPLETE)
564 result->output_name = gssd_make_resource(output_name);
566 result->output_name = 0;
572 gssd_export_name_1_svc(export_name_args *argp, export_name_res *result, struct svc_req *rqstp)
574 gss_name_t name = gssd_find_resource(argp->input_name);
576 memset(result, 0, sizeof(*result));
578 result->major_status = GSS_S_BAD_NAME;
579 gssd_verbose_out("gssd_export_name: name resource not found\n");
583 result->major_status = gss_export_name(&result->minor_status,
584 name, &result->exported_name);
585 gssd_verbose_out("gssd_export_name: done major=0x%x minor=%d\n",
586 (unsigned int)result->major_status, (int)result->minor_status);
592 gssd_release_name_1_svc(release_name_args *argp, release_name_res *result, struct svc_req *rqstp)
594 gss_name_t name = gssd_find_resource(argp->input_name);
597 result->major_status = gss_release_name(&result->minor_status,
599 gssd_delete_resource(argp->input_name);
601 result->major_status = GSS_S_COMPLETE;
602 result->minor_status = 0;
604 gssd_verbose_out("gssd_release_name: done major=0x%x minor=%d\n",
605 (unsigned int)result->major_status, (int)result->minor_status);
611 gssd_pname_to_uid_1_svc(pname_to_uid_args *argp, pname_to_uid_res *result, struct svc_req *rqstp)
613 gss_name_t name = gssd_find_resource(argp->pname);
615 char buf[1024], *bufp;
616 struct passwd pwd, *pw;
619 static size_t buflen_hint = 1024;
621 memset(result, 0, sizeof(*result));
623 result->major_status =
624 gss_pname_to_uid(&result->minor_status,
625 name, argp->mech, &uid);
626 if (result->major_status == GSS_S_COMPLETE) {
628 buflen = buflen_hint;
632 if (buflen > sizeof(buf))
633 bufp = malloc(buflen);
636 error = getpwuid_r(uid, &pwd, bufp, buflen,
640 if (buflen > sizeof(buf))
643 if (buflen > buflen_hint)
644 buflen_hint = buflen;
649 result->gid = pw->pw_gid;
650 getgrouplist(pw->pw_name, pw->pw_gid,
652 result->gidlist.gidlist_len = len;
653 result->gidlist.gidlist_val =
654 mem_alloc(len * sizeof(int));
655 memcpy(result->gidlist.gidlist_val, groups,
657 gssd_verbose_out("gssd_pname_to_uid: mapped"
658 " to uid=%d, gid=%d\n", (int)result->uid,
662 result->gidlist.gidlist_len = 0;
663 result->gidlist.gidlist_val = NULL;
664 gssd_verbose_out("gssd_pname_to_uid: mapped"
665 " to uid=%d, but no groups\n",
668 if (bufp != NULL && buflen > sizeof(buf))
671 gssd_verbose_out("gssd_pname_to_uid: failed major=0x%x"
672 " minor=%d\n", (unsigned int)result->major_status,
673 (int)result->minor_status);
675 result->major_status = GSS_S_BAD_NAME;
676 result->minor_status = 0;
677 gssd_verbose_out("gssd_pname_to_uid: no name\n");
684 gssd_acquire_cred_1_svc(acquire_cred_args *argp, acquire_cred_res *result, struct svc_req *rqstp)
686 gss_name_t desired_name = GSS_C_NO_NAME;
688 char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
691 memset(result, 0, sizeof(*result));
692 if (ccfile_dirlist[0] != '\0' && argp->desired_name == 0) {
694 * For the "-s" case and no name provided as an
695 * argument, search the directory list for an appropriate
696 * credential cache file. If the search fails, return failure.
701 cp2 = strchr(cp, ':');
704 gotone = find_ccache_file(cp, argp->uid, ccname);
710 } while (cp != NULL && *cp != '\0');
712 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
713 gssd_verbose_out("gssd_acquire_cred: no cred cache"
719 * If there wasn't a "-s" option or the name has
720 * been provided as an argument, do it the old way.
721 * When a name is provided, it will normally exist in the
722 * default keytab file and the uid will be root.
724 if (argp->desired_name != 0 && argp->uid != 0) {
725 if (debug_level == 0)
726 syslog(LOG_ERR, "gss_acquire_cred:"
727 " principal_name for non-root");
729 fprintf(stderr, "gss_acquire_cred:"
730 " principal_name for non-root\n");
732 snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
735 setenv("KRB5CCNAME", ccname, TRUE);
737 if (argp->desired_name) {
738 desired_name = gssd_find_resource(argp->desired_name);
740 result->major_status = GSS_S_BAD_NAME;
741 gssd_verbose_out("gssd_acquire_cred: no desired name"
747 result->major_status = gss_acquire_cred(&result->minor_status,
748 desired_name, argp->time_req, argp->desired_mechs,
749 argp->cred_usage, &cred, &result->actual_mechs, &result->time_rec);
750 gssd_verbose_out("gssd_acquire_cred: done major=0x%x minor=%d\n",
751 (unsigned int)result->major_status, (int)result->minor_status);
753 if (result->major_status == GSS_S_COMPLETE)
754 result->output_cred = gssd_make_resource(cred);
756 result->output_cred = 0;
762 gssd_set_cred_option_1_svc(set_cred_option_args *argp, set_cred_option_res *result, struct svc_req *rqstp)
764 gss_cred_id_t cred = gssd_find_resource(argp->cred);
766 memset(result, 0, sizeof(*result));
768 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
769 gssd_verbose_out("gssd_set_cred: no credentials\n");
773 result->major_status = gss_set_cred_option(&result->minor_status,
774 &cred, argp->option_name, &argp->option_value);
775 gssd_verbose_out("gssd_set_cred: done major=0x%x minor=%d\n",
776 (unsigned int)result->major_status, (int)result->minor_status);
782 gssd_release_cred_1_svc(release_cred_args *argp, release_cred_res *result, struct svc_req *rqstp)
784 gss_cred_id_t cred = gssd_find_resource(argp->cred);
787 result->major_status = gss_release_cred(&result->minor_status,
789 gssd_delete_resource(argp->cred);
791 result->major_status = GSS_S_COMPLETE;
792 result->minor_status = 0;
794 gssd_verbose_out("gssd_release_cred: done major=0x%x minor=%d\n",
795 (unsigned int)result->major_status, (int)result->minor_status);
801 gssd_display_status_1_svc(display_status_args *argp, display_status_res *result, struct svc_req *rqstp)
804 result->message_context = argp->message_context;
805 result->major_status = gss_display_status(&result->minor_status,
806 argp->status_value, argp->status_type, argp->mech_type,
807 &result->message_context, &result->status_string);
808 gssd_verbose_out("gssd_display_status: done major=0x%x minor=%d\n",
809 (unsigned int)result->major_status, (int)result->minor_status);
815 gssd_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
818 * We don't use XDR to free the results - anything which was
819 * allocated came from GSS-API. We use xdr_result to figure
824 if (xdr_result == (xdrproc_t) xdr_init_sec_context_res) {
825 init_sec_context_res *p = (init_sec_context_res *) result;
826 gss_release_buffer(&junk, &p->output_token);
827 } else if (xdr_result == (xdrproc_t) xdr_accept_sec_context_res) {
828 accept_sec_context_res *p = (accept_sec_context_res *) result;
829 gss_release_buffer(&junk, &p->output_token);
830 } else if (xdr_result == (xdrproc_t) xdr_delete_sec_context_res) {
831 delete_sec_context_res *p = (delete_sec_context_res *) result;
832 gss_release_buffer(&junk, &p->output_token);
833 } else if (xdr_result == (xdrproc_t) xdr_export_sec_context_res) {
834 export_sec_context_res *p = (export_sec_context_res *) result;
835 if (p->interprocess_token.length)
836 memset(p->interprocess_token.value, 0,
837 p->interprocess_token.length);
838 gss_release_buffer(&junk, &p->interprocess_token);
839 } else if (xdr_result == (xdrproc_t) xdr_export_name_res) {
840 export_name_res *p = (export_name_res *) result;
841 gss_release_buffer(&junk, &p->exported_name);
842 } else if (xdr_result == (xdrproc_t) xdr_acquire_cred_res) {
843 acquire_cred_res *p = (acquire_cred_res *) result;
844 gss_release_oid_set(&junk, &p->actual_mechs);
845 } else if (xdr_result == (xdrproc_t) xdr_pname_to_uid_res) {
846 pname_to_uid_res *p = (pname_to_uid_res *) result;
847 if (p->gidlist.gidlist_val)
848 free(p->gidlist.gidlist_val);
849 } else if (xdr_result == (xdrproc_t) xdr_display_status_res) {
850 display_status_res *p = (display_status_res *) result;
851 gss_release_buffer(&junk, &p->status_string);
858 * Search a directory for the most likely candidate to be used as the
859 * credential cache for a uid. If successful, return 1 and fill the
860 * file's path id into "rpath". Otherwise, return 0.
863 find_ccache_file(const char *dirpath, uid_t uid, char *rpath)
868 time_t exptime, oexptime;
869 int gotone, len, rating, orating;
870 char namepath[PATH_MAX + 5 + 1];
871 char retpath[PATH_MAX + 5 + 1];
873 dirp = opendir(dirpath);
879 while ((dp = readdir(dirp)) != NULL) {
880 len = snprintf(namepath, sizeof(namepath), "%s/%s", dirpath,
882 if (len < sizeof(namepath) &&
883 strstr(dp->d_name, ccfile_substring) != NULL &&
884 lstat(namepath, &sb) >= 0 &&
886 S_ISREG(sb.st_mode)) {
887 len = snprintf(namepath, sizeof(namepath), "FILE:%s/%s",
888 dirpath, dp->d_name);
889 if (len < sizeof(namepath) &&
890 is_a_valid_tgt_cache(namepath, uid, &rating,
892 if (gotone == 0 || rating > orating ||
893 (rating == orating && exptime > oexptime)) {
896 strcpy(retpath, namepath);
904 strcpy(rpath, retpath);
911 * Try to determine if the file is a valid tgt cache file.
912 * Check that the file has a valid tgt for a principal.
913 * If it does, return 1, otherwise return 0.
914 * It also returns a "rating" and the expiry time for the TGT, when found.
915 * This "rating" is higher based on heuristics that make it more
916 * likely to be the correct credential cache file to use. It can
917 * be used by the caller, along with expiry time, to select from
918 * multiple credential cache files.
921 is_a_valid_tgt_cache(const char *filepath, uid_t uid, int *retrating,
924 #ifndef WITHOUT_KERBEROS
925 krb5_context context;
926 krb5_principal princ;
928 krb5_error_code retval;
929 krb5_cc_cursor curse;
931 int gotone, orating, rating, ret;
933 char *cp, *cp2, *pname;
936 /* Find a likely name for the uid principal. */
940 * Do a bunch of krb5 library stuff to try and determine if
941 * this file is a credentials cache with an appropriate TGT
944 retval = krb5_init_context(&context);
947 retval = krb5_cc_resolve(context, filepath, &ccache);
949 krb5_free_context(context);
955 retval = krb5_cc_start_seq_get(context, ccache, &curse);
957 while ((retval = krb5_cc_next_cred(context, ccache, &curse,
961 retval = krb5_unparse_name(context, krbcred.server,
964 cp = strchr(pname, '/');
967 if (strcmp(pname, "krbtgt") == 0 &&
968 krbcred.times.endtime > time(NULL)
972 * Test to see if this is a
973 * tgt for cross-realm auth.
974 * Rate it higher, if it is not.
976 cp2 = strchr(cp, '@');
979 if (strcmp(cp, cp2) ==
988 retval = krb5_unparse_name(context,
989 krbcred.client, &pname);
991 cp = strchr(pname, '@');
994 if (pw != NULL && strcmp(pname,
997 if (strchr(pname, '/') == NULL)
999 if (pref_realm[0] != '\0' &&
1000 strcmp(cp, pref_realm) == 0)
1005 if (rating > orating) {
1007 exptime = krbcred.times.endtime;
1008 } else if (rating == orating &&
1009 krbcred.times.endtime > exptime)
1010 exptime = krbcred.times.endtime;
1013 krb5_free_cred_contents(context, &krbcred);
1015 krb5_cc_end_seq_get(context, ccache, &curse);
1017 krb5_cc_close(context, ccache);
1018 krb5_free_context(context);
1020 *retrating = orating;
1021 *retexptime = exptime;
1024 #else /* WITHOUT_KERBEROS */
1026 #endif /* !WITHOUT_KERBEROS */