2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
37 #include <sys/queue.h>
38 #include <sys/syslog.h>
43 #ifndef WITHOUT_KERBEROS
53 #include <gssapi/gssapi.h>
55 #include <rpc/rpc_com.h>
59 #ifndef _PATH_GSS_MECH
60 #define _PATH_GSS_MECH "/etc/gss/mech"
62 #ifndef _PATH_GSSDSOCK
63 #define _PATH_GSSDSOCK "/var/run/gssd.sock"
65 #define GSSD_CREDENTIAL_CACHE_FILE "/tmp/krb5cc_gssd"
68 LIST_ENTRY(gss_resource) gr_link;
69 uint64_t gr_id; /* identifier exported to kernel */
70 void* gr_res; /* GSS-API resource pointer */
72 LIST_HEAD(gss_resource_list, gss_resource) gss_resources;
73 int gss_resource_count;
75 uint32_t gss_start_time;
77 static char ccfile_dirlist[PATH_MAX + 1], ccfile_substring[NAME_MAX + 1];
78 static char pref_realm[1024];
80 static int hostbased_initiator_cred;
81 #ifndef WITHOUT_KERBEROS
82 /* 1.2.752.43.13.14 */
83 static gss_OID_desc gss_krb5_set_allowable_enctypes_x_desc =
84 {6, (void *) "\x2a\x85\x70\x2b\x0d\x0e"};
85 static gss_OID GSS_KRB5_SET_ALLOWABLE_ENCTYPES_X =
86 &gss_krb5_set_allowable_enctypes_x_desc;
87 static gss_OID_desc gss_krb5_mech_oid_x_desc =
88 {9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
89 static gss_OID GSS_KRB5_MECH_OID_X =
90 &gss_krb5_mech_oid_x_desc;
93 static void gssd_load_mech(void);
94 static int find_ccache_file(const char *, uid_t, char *);
95 static int is_a_valid_tgt_cache(const char *, uid_t, int *, time_t *);
96 static void gssd_verbose_out(const char *, ...);
97 #ifndef WITHOUT_KERBEROS
98 static krb5_error_code gssd_get_cc_from_keytab(const char *);
99 static OM_uint32 gssd_get_user_cred(OM_uint32 *, uid_t, gss_cred_id_t *);
101 void gssd_terminate(int);
103 extern void gssd_1(struct svc_req *rqstp, SVCXPRT *transp);
104 extern int gssd_syscall(char *path);
107 main(int argc, char **argv)
110 * We provide an RPC service on a local-domain socket. The
111 * kernel's GSS-API code will pass what it can't handle
114 struct sockaddr_un sun;
115 int fd, oldmask, ch, debug;
119 * Initialize the credential cache file name substring and the
120 * search directory list.
122 strlcpy(ccfile_substring, "krb5cc_", sizeof(ccfile_substring));
123 ccfile_dirlist[0] = '\0';
124 pref_realm[0] = '\0';
127 while ((ch = getopt(argc, argv, "dhvs:c:r:")) != -1) {
133 #ifndef WITHOUT_KERBEROS
135 * Enable use of a host based initiator credential
136 * in the default keytab file.
138 hostbased_initiator_cred = 1;
140 errx(1, "This option not available when built"
141 " without MK_KERBEROS\n");
148 #ifndef WITHOUT_KERBEROS
150 * Set the directory search list. This enables use of
151 * find_ccache_file() to search the directories for a
152 * suitable credentials cache file.
154 strlcpy(ccfile_dirlist, optarg, sizeof(ccfile_dirlist));
156 errx(1, "This option not available when built"
157 " without MK_KERBEROS\n");
162 * Specify a non-default credential cache file
165 strlcpy(ccfile_substring, optarg,
166 sizeof(ccfile_substring));
170 * Set the preferred realm for the credential cache tgt.
172 strlcpy(pref_realm, optarg, sizeof(pref_realm));
176 "usage: %s [-d] [-s dir-list] [-c file-substring]"
177 " [-r preferred-realm]\n", argv[0]);
186 if (daemon(0, 0) != 0)
187 err(1, "Can't daemonize");
188 signal(SIGINT, SIG_IGN);
189 signal(SIGQUIT, SIG_IGN);
190 signal(SIGHUP, SIG_IGN);
192 signal(SIGTERM, gssd_terminate);
193 signal(SIGPIPE, gssd_terminate);
195 memset(&sun, 0, sizeof sun);
196 sun.sun_family = AF_LOCAL;
197 unlink(_PATH_GSSDSOCK);
198 strcpy(sun.sun_path, _PATH_GSSDSOCK);
199 sun.sun_len = SUN_LEN(&sun);
200 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
202 if (debug_level == 0) {
203 syslog(LOG_ERR, "Can't create local gssd socket");
206 err(1, "Can't create local gssd socket");
208 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
209 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
210 if (debug_level == 0) {
211 syslog(LOG_ERR, "Can't bind local gssd socket");
214 err(1, "Can't bind local gssd socket");
217 if (listen(fd, SOMAXCONN) < 0) {
218 if (debug_level == 0) {
219 syslog(LOG_ERR, "Can't listen on local gssd socket");
222 err(1, "Can't listen on local gssd socket");
224 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
226 if (debug_level == 0) {
228 "Can't create transport for local gssd socket");
231 err(1, "Can't create transport for local gssd socket");
233 if (!svc_reg(xprt, GSSD, GSSDVERS, gssd_1, NULL)) {
234 if (debug_level == 0) {
236 "Can't register service for local gssd socket");
239 err(1, "Can't register service for local gssd socket");
242 LIST_INIT(&gss_resources);
244 gss_start_time = time(0);
246 gssd_syscall(_PATH_GSSDSOCK);
259 char *name, *oid, *lib, *kobj;
261 fp = fopen(_PATH_GSS_MECH, "r");
265 while (fgets(buf, sizeof(buf), fp)) {
269 name = strsep(&p, "\t\n ");
270 if (p) while (isspace(*p)) p++;
271 oid = strsep(&p, "\t\n ");
272 if (p) while (isspace(*p)) p++;
273 lib = strsep(&p, "\t\n ");
274 if (p) while (isspace(*p)) p++;
275 kobj = strsep(&p, "\t\n ");
276 if (!name || !oid || !lib || !kobj)
279 if (strcmp(kobj, "-")) {
281 * Attempt to load the kernel module if its
282 * not already present.
284 if (modfind(kobj) < 0) {
285 if (kldload(kobj) < 0) {
287 "%s: can't find or load kernel module %s for %s\n",
288 getprogname(), kobj, name);
297 gssd_find_resource(uint64_t id)
299 struct gss_resource *gr;
304 LIST_FOREACH(gr, &gss_resources, gr_link)
312 gssd_make_resource(void *res)
314 struct gss_resource *gr;
319 gr = malloc(sizeof(struct gss_resource));
322 gr->gr_id = (gss_next_id++) + ((uint64_t) gss_start_time << 32);
324 LIST_INSERT_HEAD(&gss_resources, gr, gr_link);
325 gss_resource_count++;
327 printf("%d resources allocated\n", gss_resource_count);
333 gssd_delete_resource(uint64_t id)
335 struct gss_resource *gr;
337 LIST_FOREACH(gr, &gss_resources, gr_link) {
338 if (gr->gr_id == id) {
339 LIST_REMOVE(gr, gr_link);
341 gss_resource_count--;
343 printf("%d resources allocated\n",
351 gssd_verbose_out(const char *fmt, ...)
357 if (debug_level == 0)
358 vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap);
360 vfprintf(stderr, fmt, ap);
366 gssd_null_1_svc(void *argp, void *result, struct svc_req *rqstp)
369 gssd_verbose_out("gssd_null: done\n");
374 gssd_init_sec_context_1_svc(init_sec_context_args *argp, init_sec_context_res *result, struct svc_req *rqstp)
376 gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
377 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
378 gss_name_t name = GSS_C_NO_NAME;
379 char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
382 #ifndef WITHOUT_KERBEROS
383 gss_buffer_desc principal_desc;
384 char enctype[sizeof(uint32_t)];
389 memset(result, 0, sizeof(*result));
390 if (hostbased_initiator_cred != 0 && argp->cred != 0 &&
393 * These credentials are for a host based initiator name
394 * in a keytab file, which should now have credentials
395 * in /tmp/krb5cc_gssd, because gss_acquire_cred() did
396 * the equivalent of "kinit -k".
398 snprintf(ccname, sizeof(ccname), "FILE:%s",
399 GSSD_CREDENTIAL_CACHE_FILE);
400 } else if (ccfile_dirlist[0] != '\0' && argp->cred == 0) {
402 * For the "-s" case and no credentials provided as an
403 * argument, search the directory list for an appropriate
404 * credential cache file. If the search fails, return failure.
409 cp2 = strchr(cp, ':');
412 gotone = find_ccache_file(cp, argp->uid, ccname);
418 } while (cp != NULL && *cp != '\0');
420 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
421 gssd_verbose_out("gssd_init_sec_context: -s no"
422 " credential cache file found for uid=%d\n",
428 * If there wasn't a "-s" option or the credentials have
429 * been provided as an argument, do it the old way.
430 * When credentials are provided, the uid should be root.
432 if (argp->cred != 0 && argp->uid != 0) {
433 if (debug_level == 0)
434 syslog(LOG_ERR, "gss_init_sec_context:"
435 " cred for non-root");
437 fprintf(stderr, "gss_init_sec_context:"
438 " cred for non-root\n");
440 snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
443 setenv("KRB5CCNAME", ccname, TRUE);
446 cred = gssd_find_resource(argp->cred);
448 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
449 gssd_verbose_out("gssd_init_sec_context: cred"
450 " resource not found\n");
455 ctx = gssd_find_resource(argp->ctx);
457 result->major_status = GSS_S_CONTEXT_EXPIRED;
458 gssd_verbose_out("gssd_init_sec_context: context"
459 " resource not found\n");
464 name = gssd_find_resource(argp->name);
466 result->major_status = GSS_S_BAD_NAME;
467 gssd_verbose_out("gssd_init_sec_context: name"
468 " resource not found\n");
474 result->major_status = gss_init_sec_context(&result->minor_status,
475 cred, &ctx, name, argp->mech_type,
476 argp->req_flags, argp->time_req, argp->input_chan_bindings,
477 &argp->input_token, &result->actual_mech_type,
478 &result->output_token, &result->ret_flags, &result->time_rec);
479 gssd_verbose_out("gssd_init_sec_context: done major=0x%x minor=%d"
480 " uid=%d\n", (unsigned int)result->major_status,
481 (int)result->minor_status, (int)argp->uid);
483 gss_release_cred(&min_stat, &cred);
485 if (result->major_status == GSS_S_COMPLETE
486 || result->major_status == GSS_S_CONTINUE_NEEDED) {
488 result->ctx = argp->ctx;
490 result->ctx = gssd_make_resource(ctx);
497 gssd_accept_sec_context_1_svc(accept_sec_context_args *argp, accept_sec_context_res *result, struct svc_req *rqstp)
499 gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
500 gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
502 gss_cred_id_t delegated_cred_handle;
504 memset(result, 0, sizeof(*result));
506 ctx = gssd_find_resource(argp->ctx);
508 result->major_status = GSS_S_CONTEXT_EXPIRED;
509 gssd_verbose_out("gssd_accept_sec_context: ctx"
510 " resource not found\n");
515 cred = gssd_find_resource(argp->cred);
517 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
518 gssd_verbose_out("gssd_accept_sec_context: cred"
519 " resource not found\n");
524 memset(result, 0, sizeof(*result));
525 result->major_status = gss_accept_sec_context(&result->minor_status,
526 &ctx, cred, &argp->input_token, argp->input_chan_bindings,
527 &src_name, &result->mech_type, &result->output_token,
528 &result->ret_flags, &result->time_rec,
529 &delegated_cred_handle);
530 gssd_verbose_out("gssd_accept_sec_context: done major=0x%x minor=%d\n",
531 (unsigned int)result->major_status, (int)result->minor_status);
533 if (result->major_status == GSS_S_COMPLETE
534 || result->major_status == GSS_S_CONTINUE_NEEDED) {
536 result->ctx = argp->ctx;
538 result->ctx = gssd_make_resource(ctx);
539 result->src_name = gssd_make_resource(src_name);
540 result->delegated_cred_handle =
541 gssd_make_resource(delegated_cred_handle);
548 gssd_delete_sec_context_1_svc(delete_sec_context_args *argp, delete_sec_context_res *result, struct svc_req *rqstp)
550 gss_ctx_id_t ctx = gssd_find_resource(argp->ctx);
553 result->major_status = gss_delete_sec_context(
554 &result->minor_status, &ctx, &result->output_token);
555 gssd_delete_resource(argp->ctx);
557 result->major_status = GSS_S_COMPLETE;
558 result->minor_status = 0;
560 gssd_verbose_out("gssd_delete_sec_context: done major=0x%x minor=%d\n",
561 (unsigned int)result->major_status, (int)result->minor_status);
567 gssd_export_sec_context_1_svc(export_sec_context_args *argp, export_sec_context_res *result, struct svc_req *rqstp)
569 gss_ctx_id_t ctx = gssd_find_resource(argp->ctx);
572 result->major_status = gss_export_sec_context(
573 &result->minor_status, &ctx,
574 &result->interprocess_token);
575 result->format = KGSS_HEIMDAL_1_1;
576 gssd_delete_resource(argp->ctx);
578 result->major_status = GSS_S_FAILURE;
579 result->minor_status = 0;
580 result->interprocess_token.length = 0;
581 result->interprocess_token.value = NULL;
583 gssd_verbose_out("gssd_export_sec_context: done major=0x%x minor=%d\n",
584 (unsigned int)result->major_status, (int)result->minor_status);
590 gssd_import_name_1_svc(import_name_args *argp, import_name_res *result, struct svc_req *rqstp)
594 result->major_status = gss_import_name(&result->minor_status,
595 &argp->input_name_buffer, argp->input_name_type, &name);
596 gssd_verbose_out("gssd_import_name: done major=0x%x minor=%d\n",
597 (unsigned int)result->major_status, (int)result->minor_status);
599 if (result->major_status == GSS_S_COMPLETE)
600 result->output_name = gssd_make_resource(name);
602 result->output_name = 0;
608 gssd_canonicalize_name_1_svc(canonicalize_name_args *argp, canonicalize_name_res *result, struct svc_req *rqstp)
610 gss_name_t name = gssd_find_resource(argp->input_name);
611 gss_name_t output_name;
613 memset(result, 0, sizeof(*result));
615 result->major_status = GSS_S_BAD_NAME;
619 result->major_status = gss_canonicalize_name(&result->minor_status,
620 name, argp->mech_type, &output_name);
621 gssd_verbose_out("gssd_canonicalize_name: done major=0x%x minor=%d\n",
622 (unsigned int)result->major_status, (int)result->minor_status);
624 if (result->major_status == GSS_S_COMPLETE)
625 result->output_name = gssd_make_resource(output_name);
627 result->output_name = 0;
633 gssd_export_name_1_svc(export_name_args *argp, export_name_res *result, struct svc_req *rqstp)
635 gss_name_t name = gssd_find_resource(argp->input_name);
637 memset(result, 0, sizeof(*result));
639 result->major_status = GSS_S_BAD_NAME;
640 gssd_verbose_out("gssd_export_name: name resource not found\n");
644 result->major_status = gss_export_name(&result->minor_status,
645 name, &result->exported_name);
646 gssd_verbose_out("gssd_export_name: done major=0x%x minor=%d\n",
647 (unsigned int)result->major_status, (int)result->minor_status);
653 gssd_release_name_1_svc(release_name_args *argp, release_name_res *result, struct svc_req *rqstp)
655 gss_name_t name = gssd_find_resource(argp->input_name);
658 result->major_status = gss_release_name(&result->minor_status,
660 gssd_delete_resource(argp->input_name);
662 result->major_status = GSS_S_COMPLETE;
663 result->minor_status = 0;
665 gssd_verbose_out("gssd_release_name: done major=0x%x minor=%d\n",
666 (unsigned int)result->major_status, (int)result->minor_status);
672 gssd_pname_to_uid_1_svc(pname_to_uid_args *argp, pname_to_uid_res *result, struct svc_req *rqstp)
674 gss_name_t name = gssd_find_resource(argp->pname);
676 char buf[1024], *bufp;
677 struct passwd pwd, *pw;
680 static size_t buflen_hint = 1024;
682 memset(result, 0, sizeof(*result));
684 result->major_status =
685 gss_pname_to_uid(&result->minor_status,
686 name, argp->mech, &uid);
687 if (result->major_status == GSS_S_COMPLETE) {
689 buflen = buflen_hint;
693 if (buflen > sizeof(buf))
694 bufp = malloc(buflen);
697 error = getpwuid_r(uid, &pwd, bufp, buflen,
701 if (buflen > sizeof(buf))
704 if (buflen > buflen_hint)
705 buflen_hint = buflen;
710 result->gid = pw->pw_gid;
711 getgrouplist(pw->pw_name, pw->pw_gid,
713 result->gidlist.gidlist_len = len;
714 result->gidlist.gidlist_val =
715 mem_alloc(len * sizeof(int));
716 memcpy(result->gidlist.gidlist_val, groups,
718 gssd_verbose_out("gssd_pname_to_uid: mapped"
719 " to uid=%d, gid=%d\n", (int)result->uid,
723 result->gidlist.gidlist_len = 0;
724 result->gidlist.gidlist_val = NULL;
725 gssd_verbose_out("gssd_pname_to_uid: mapped"
726 " to uid=%d, but no groups\n",
729 if (bufp != NULL && buflen > sizeof(buf))
732 gssd_verbose_out("gssd_pname_to_uid: failed major=0x%x"
733 " minor=%d\n", (unsigned int)result->major_status,
734 (int)result->minor_status);
736 result->major_status = GSS_S_BAD_NAME;
737 result->minor_status = 0;
738 gssd_verbose_out("gssd_pname_to_uid: no name\n");
745 gssd_acquire_cred_1_svc(acquire_cred_args *argp, acquire_cred_res *result, struct svc_req *rqstp)
747 gss_name_t desired_name = GSS_C_NO_NAME;
749 char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
751 #ifndef WITHOUT_KERBEROS
752 gss_buffer_desc namebuf;
754 krb5_error_code kret;
757 memset(result, 0, sizeof(*result));
758 if (argp->desired_name) {
759 desired_name = gssd_find_resource(argp->desired_name);
761 result->major_status = GSS_S_BAD_NAME;
762 gssd_verbose_out("gssd_acquire_cred: no desired name"
768 #ifndef WITHOUT_KERBEROS
769 if (hostbased_initiator_cred != 0 && argp->desired_name != 0 &&
770 argp->uid == 0 && argp->cred_usage == GSS_C_INITIATE) {
771 /* This is a host based initiator name in the keytab file. */
772 snprintf(ccname, sizeof(ccname), "FILE:%s",
773 GSSD_CREDENTIAL_CACHE_FILE);
774 setenv("KRB5CCNAME", ccname, TRUE);
775 result->major_status = gss_display_name(&result->minor_status,
776 desired_name, &namebuf, NULL);
777 gssd_verbose_out("gssd_acquire_cred: desired name for host "
778 "based initiator cred major=0x%x minor=%d\n",
779 (unsigned int)result->major_status,
780 (int)result->minor_status);
781 if (result->major_status != GSS_S_COMPLETE)
783 if (namebuf.length > PATH_MAX + 5) {
784 result->minor_status = 0;
785 result->major_status = GSS_S_FAILURE;
788 memcpy(ccname, namebuf.value, namebuf.length);
789 ccname[namebuf.length] = '\0';
790 if ((cp = strchr(ccname, '@')) != NULL)
792 kret = gssd_get_cc_from_keytab(ccname);
793 gssd_verbose_out("gssd_acquire_cred: using keytab entry for "
794 "%s, kerberos ret=%d\n", ccname, (int)kret);
795 gss_release_buffer(&minstat, &namebuf);
797 result->minor_status = kret;
798 result->major_status = GSS_S_FAILURE;
802 #endif /* !WITHOUT_KERBEROS */
803 if (ccfile_dirlist[0] != '\0' && argp->desired_name == 0) {
805 * For the "-s" case and no name provided as an
806 * argument, search the directory list for an appropriate
807 * credential cache file. If the search fails, return failure.
812 cp2 = strchr(cp, ':');
815 gotone = find_ccache_file(cp, argp->uid, ccname);
821 } while (cp != NULL && *cp != '\0');
823 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
824 gssd_verbose_out("gssd_acquire_cred: no cred cache"
828 setenv("KRB5CCNAME", ccname, TRUE);
831 * If there wasn't a "-s" option or the name has
832 * been provided as an argument, do it the old way.
833 * When a name is provided, it will normally exist in the
834 * default keytab file and the uid will be root.
836 if (argp->desired_name != 0 && argp->uid != 0) {
837 if (debug_level == 0)
838 syslog(LOG_ERR, "gss_acquire_cred:"
839 " principal_name for non-root");
841 fprintf(stderr, "gss_acquire_cred:"
842 " principal_name for non-root\n");
844 snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
846 setenv("KRB5CCNAME", ccname, TRUE);
849 result->major_status = gss_acquire_cred(&result->minor_status,
850 desired_name, argp->time_req, argp->desired_mechs,
851 argp->cred_usage, &cred, &result->actual_mechs, &result->time_rec);
852 gssd_verbose_out("gssd_acquire_cred: done major=0x%x minor=%d\n",
853 (unsigned int)result->major_status, (int)result->minor_status);
855 if (result->major_status == GSS_S_COMPLETE)
856 result->output_cred = gssd_make_resource(cred);
858 result->output_cred = 0;
864 gssd_set_cred_option_1_svc(set_cred_option_args *argp, set_cred_option_res *result, struct svc_req *rqstp)
866 gss_cred_id_t cred = gssd_find_resource(argp->cred);
868 memset(result, 0, sizeof(*result));
870 result->major_status = GSS_S_CREDENTIALS_EXPIRED;
871 gssd_verbose_out("gssd_set_cred: no credentials\n");
875 result->major_status = gss_set_cred_option(&result->minor_status,
876 &cred, argp->option_name, &argp->option_value);
877 gssd_verbose_out("gssd_set_cred: done major=0x%x minor=%d\n",
878 (unsigned int)result->major_status, (int)result->minor_status);
884 gssd_release_cred_1_svc(release_cred_args *argp, release_cred_res *result, struct svc_req *rqstp)
886 gss_cred_id_t cred = gssd_find_resource(argp->cred);
889 result->major_status = gss_release_cred(&result->minor_status,
891 gssd_delete_resource(argp->cred);
893 result->major_status = GSS_S_COMPLETE;
894 result->minor_status = 0;
896 gssd_verbose_out("gssd_release_cred: done major=0x%x minor=%d\n",
897 (unsigned int)result->major_status, (int)result->minor_status);
903 gssd_display_status_1_svc(display_status_args *argp, display_status_res *result, struct svc_req *rqstp)
906 result->message_context = argp->message_context;
907 result->major_status = gss_display_status(&result->minor_status,
908 argp->status_value, argp->status_type, argp->mech_type,
909 &result->message_context, &result->status_string);
910 gssd_verbose_out("gssd_display_status: done major=0x%x minor=%d\n",
911 (unsigned int)result->major_status, (int)result->minor_status);
917 gssd_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
920 * We don't use XDR to free the results - anything which was
921 * allocated came from GSS-API. We use xdr_result to figure
926 if (xdr_result == (xdrproc_t) xdr_init_sec_context_res) {
927 init_sec_context_res *p = (init_sec_context_res *) result;
928 gss_release_buffer(&junk, &p->output_token);
929 } else if (xdr_result == (xdrproc_t) xdr_accept_sec_context_res) {
930 accept_sec_context_res *p = (accept_sec_context_res *) result;
931 gss_release_buffer(&junk, &p->output_token);
932 } else if (xdr_result == (xdrproc_t) xdr_delete_sec_context_res) {
933 delete_sec_context_res *p = (delete_sec_context_res *) result;
934 gss_release_buffer(&junk, &p->output_token);
935 } else if (xdr_result == (xdrproc_t) xdr_export_sec_context_res) {
936 export_sec_context_res *p = (export_sec_context_res *) result;
937 if (p->interprocess_token.length)
938 memset(p->interprocess_token.value, 0,
939 p->interprocess_token.length);
940 gss_release_buffer(&junk, &p->interprocess_token);
941 } else if (xdr_result == (xdrproc_t) xdr_export_name_res) {
942 export_name_res *p = (export_name_res *) result;
943 gss_release_buffer(&junk, &p->exported_name);
944 } else if (xdr_result == (xdrproc_t) xdr_acquire_cred_res) {
945 acquire_cred_res *p = (acquire_cred_res *) result;
946 gss_release_oid_set(&junk, &p->actual_mechs);
947 } else if (xdr_result == (xdrproc_t) xdr_pname_to_uid_res) {
948 pname_to_uid_res *p = (pname_to_uid_res *) result;
949 if (p->gidlist.gidlist_val)
950 free(p->gidlist.gidlist_val);
951 } else if (xdr_result == (xdrproc_t) xdr_display_status_res) {
952 display_status_res *p = (display_status_res *) result;
953 gss_release_buffer(&junk, &p->status_string);
960 * Search a directory for the most likely candidate to be used as the
961 * credential cache for a uid. If successful, return 1 and fill the
962 * file's path id into "rpath". Otherwise, return 0.
965 find_ccache_file(const char *dirpath, uid_t uid, char *rpath)
970 time_t exptime, oexptime;
971 int gotone, len, rating, orating;
972 char namepath[PATH_MAX + 5 + 1];
973 char retpath[PATH_MAX + 5 + 1];
975 dirp = opendir(dirpath);
981 while ((dp = readdir(dirp)) != NULL) {
982 len = snprintf(namepath, sizeof(namepath), "%s/%s", dirpath,
984 if (len < sizeof(namepath) &&
985 (hostbased_initiator_cred == 0 || strcmp(namepath,
986 GSSD_CREDENTIAL_CACHE_FILE) != 0) &&
987 strstr(dp->d_name, ccfile_substring) != NULL &&
988 lstat(namepath, &sb) >= 0 &&
990 S_ISREG(sb.st_mode)) {
991 len = snprintf(namepath, sizeof(namepath), "FILE:%s/%s",
992 dirpath, dp->d_name);
993 if (len < sizeof(namepath) &&
994 is_a_valid_tgt_cache(namepath, uid, &rating,
996 if (gotone == 0 || rating > orating ||
997 (rating == orating && exptime > oexptime)) {
1000 strcpy(retpath, namepath);
1008 strcpy(rpath, retpath);
1015 * Try to determine if the file is a valid tgt cache file.
1016 * Check that the file has a valid tgt for a principal.
1017 * If it does, return 1, otherwise return 0.
1018 * It also returns a "rating" and the expiry time for the TGT, when found.
1019 * This "rating" is higher based on heuristics that make it more
1020 * likely to be the correct credential cache file to use. It can
1021 * be used by the caller, along with expiry time, to select from
1022 * multiple credential cache files.
1025 is_a_valid_tgt_cache(const char *filepath, uid_t uid, int *retrating,
1028 #ifndef WITHOUT_KERBEROS
1029 krb5_context context;
1030 krb5_principal princ;
1032 krb5_error_code retval;
1033 krb5_cc_cursor curse;
1035 int gotone, orating, rating, ret;
1037 char *cp, *cp2, *pname;
1040 /* Find a likely name for the uid principal. */
1044 * Do a bunch of krb5 library stuff to try and determine if
1045 * this file is a credentials cache with an appropriate TGT
1048 retval = krb5_init_context(&context);
1051 retval = krb5_cc_resolve(context, filepath, &ccache);
1053 krb5_free_context(context);
1059 retval = krb5_cc_start_seq_get(context, ccache, &curse);
1061 while ((retval = krb5_cc_next_cred(context, ccache, &curse,
1065 retval = krb5_unparse_name(context, krbcred.server,
1068 cp = strchr(pname, '/');
1071 if (strcmp(pname, "krbtgt") == 0 &&
1072 krbcred.times.endtime > time(NULL)
1076 * Test to see if this is a
1077 * tgt for cross-realm auth.
1078 * Rate it higher, if it is not.
1080 cp2 = strchr(cp, '@');
1083 if (strcmp(cp, cp2) ==
1092 retval = krb5_unparse_name(context,
1093 krbcred.client, &pname);
1095 cp = strchr(pname, '@');
1098 if (pw != NULL && strcmp(pname,
1101 if (strchr(pname, '/') == NULL)
1103 if (pref_realm[0] != '\0' &&
1104 strcmp(cp, pref_realm) == 0)
1109 if (rating > orating) {
1111 exptime = krbcred.times.endtime;
1112 } else if (rating == orating &&
1113 krbcred.times.endtime > exptime)
1114 exptime = krbcred.times.endtime;
1117 krb5_free_cred_contents(context, &krbcred);
1119 krb5_cc_end_seq_get(context, ccache, &curse);
1121 krb5_cc_close(context, ccache);
1122 krb5_free_context(context);
1124 *retrating = orating;
1125 *retexptime = exptime;
1128 #else /* WITHOUT_KERBEROS */
1130 #endif /* !WITHOUT_KERBEROS */
1133 #ifndef WITHOUT_KERBEROS
1135 * This function attempts to do essentially a "kinit -k" for the principal
1136 * name provided as the argument, so that there will be a TGT in the
1139 static krb5_error_code
1140 gssd_get_cc_from_keytab(const char *name)
1142 krb5_error_code ret, opt_ret, princ_ret, cc_ret, kt_ret, cred_ret;
1143 krb5_context context;
1144 krb5_principal principal;
1147 krb5_get_init_creds_opt *opt;
1148 krb5_deltat start_time = 0;
1151 ret = krb5_init_context(&context);
1154 opt_ret = cc_ret = kt_ret = cred_ret = 1; /* anything non-zero */
1155 princ_ret = ret = krb5_parse_name(context, name, &principal);
1157 opt_ret = ret = krb5_get_init_creds_opt_alloc(context, &opt);
1159 cc_ret = ret = krb5_cc_default(context, &ccache);
1161 ret = krb5_cc_initialize(context, ccache, principal);
1163 krb5_get_init_creds_opt_set_default_flags(context, "gssd",
1164 krb5_principal_get_realm(context, principal), opt);
1165 kt_ret = ret = krb5_kt_default(context, &kt);
1168 cred_ret = ret = krb5_get_init_creds_keytab(context, &cred,
1169 principal, kt, start_time, NULL, opt);
1171 ret = krb5_cc_store_cred(context, ccache, &cred);
1173 krb5_kt_close(context, kt);
1175 krb5_cc_close(context, ccache);
1177 krb5_get_init_creds_opt_free(context, opt);
1179 krb5_free_principal(context, principal);
1181 krb5_free_cred_contents(context, &cred);
1182 krb5_free_context(context);
1187 * Acquire a gss credential for a uid.
1190 gssd_get_user_cred(OM_uint32 *min_statp, uid_t uid, gss_cred_id_t *credp)
1192 gss_buffer_desc principal_desc;
1194 OM_uint32 maj_stat, min_stat;
1195 gss_OID_set mechlist;
1201 return (GSS_S_FAILURE);
1205 * The mechanism must be set to KerberosV for acquisition
1206 * of credentials to work reliably.
1208 maj_stat = gss_create_empty_oid_set(min_statp, &mechlist);
1209 if (maj_stat != GSS_S_COMPLETE)
1211 maj_stat = gss_add_oid_set_member(min_statp, GSS_KRB5_MECH_OID_X,
1213 if (maj_stat != GSS_S_COMPLETE) {
1214 gss_release_oid_set(&min_stat, &mechlist);
1218 principal_desc.value = (void *)pw->pw_name;
1219 principal_desc.length = strlen(pw->pw_name);
1220 maj_stat = gss_import_name(min_statp, &principal_desc,
1221 GSS_C_NT_USER_NAME, &name);
1222 if (maj_stat != GSS_S_COMPLETE) {
1223 gss_release_oid_set(&min_stat, &mechlist);
1226 /* Acquire the credentials. */
1227 maj_stat = gss_acquire_cred(min_statp, name, 0, mechlist,
1228 GSS_C_INITIATE, credp, NULL, NULL);
1229 gss_release_name(&min_stat, &name);
1230 gss_release_oid_set(&min_stat, &mechlist);
1233 #endif /* !WITHOUT_KERBEROS */
1235 void gssd_terminate(int sig __unused)
1238 #ifndef WITHOUT_KERBEROS
1239 if (hostbased_initiator_cred != 0)
1240 unlink(GSSD_CREDENTIAL_CACHE_FILE);