1a9148abdSDoug Rabson /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4a9148abdSDoug Rabson * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5a9148abdSDoug Rabson * Authors: Doug Rabson <dfr@rabson.org>
6a9148abdSDoug Rabson * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7a9148abdSDoug Rabson *
8a9148abdSDoug Rabson * Redistribution and use in source and binary forms, with or without
9a9148abdSDoug Rabson * modification, are permitted provided that the following conditions
10a9148abdSDoug Rabson * are met:
11a9148abdSDoug Rabson * 1. Redistributions of source code must retain the above copyright
12a9148abdSDoug Rabson * notice, this list of conditions and the following disclaimer.
13a9148abdSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
14a9148abdSDoug Rabson * notice, this list of conditions and the following disclaimer in the
15a9148abdSDoug Rabson * documentation and/or other materials provided with the distribution.
16a9148abdSDoug Rabson *
17a9148abdSDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18a9148abdSDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a9148abdSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a9148abdSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a9148abdSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a9148abdSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a9148abdSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a9148abdSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a9148abdSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a9148abdSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a9148abdSDoug Rabson * SUCH DAMAGE.
28a9148abdSDoug Rabson */
29a9148abdSDoug Rabson
30a9148abdSDoug Rabson #include <sys/param.h>
31a9148abdSDoug Rabson #include <sys/stat.h>
32a9148abdSDoug Rabson #include <sys/linker.h>
33a9148abdSDoug Rabson #include <sys/module.h>
34a9148abdSDoug Rabson #include <sys/queue.h>
3582ea0132SRick Macklem #include <sys/socket.h>
3669772711SRick Macklem #include <sys/sysctl.h>
37765782e7SRick Macklem #include <sys/syslog.h>
38a9148abdSDoug Rabson #include <ctype.h>
39158b9798SRick Macklem #include <dirent.h>
40a9148abdSDoug Rabson #include <err.h>
41d1e9aa46SRick Macklem #include <errno.h>
428ac5ae56SRick Macklem #ifndef WITHOUT_KERBEROS
43158b9798SRick Macklem #include <krb5.h>
448ac5ae56SRick Macklem #endif
4582ea0132SRick Macklem #include <netdb.h>
46a9148abdSDoug Rabson #include <pwd.h>
47c3234382SRick Macklem #include <signal.h>
4835ab0ce9SRick Macklem #include <stdarg.h>
49a9148abdSDoug Rabson #include <stdio.h>
50a9148abdSDoug Rabson #include <stdlib.h>
51a9148abdSDoug Rabson #include <string.h>
52a9148abdSDoug Rabson #include <unistd.h>
5382ea0132SRick Macklem #include <arpa/inet.h>
5482ea0132SRick Macklem #include <netinet/in.h>
55a9148abdSDoug Rabson #include <gssapi/gssapi.h>
56a9148abdSDoug Rabson #include <rpc/rpc.h>
57a9148abdSDoug Rabson #include <rpc/rpc_com.h>
58a9148abdSDoug Rabson
59a9148abdSDoug Rabson #include "gssd.h"
60a9148abdSDoug Rabson
61a9148abdSDoug Rabson #ifndef _PATH_GSS_MECH
62a9148abdSDoug Rabson #define _PATH_GSS_MECH "/etc/gss/mech"
63a9148abdSDoug Rabson #endif
64bd5012b2SRick Macklem #define GSSD_CREDENTIAL_CACHE_FILE "/tmp/krb5cc_gssd"
65a9148abdSDoug Rabson
66a9148abdSDoug Rabson struct gss_resource {
67a9148abdSDoug Rabson LIST_ENTRY(gss_resource) gr_link;
68b5635ba0SPedro F. Giffuni uint64_t gr_id; /* identifier exported to kernel */
69a9148abdSDoug Rabson void* gr_res; /* GSS-API resource pointer */
70a9148abdSDoug Rabson };
71a9148abdSDoug Rabson LIST_HEAD(gss_resource_list, gss_resource) gss_resources;
72a9148abdSDoug Rabson int gss_resource_count;
73a9148abdSDoug Rabson uint32_t gss_next_id;
74a9148abdSDoug Rabson uint32_t gss_start_time;
75a9148abdSDoug Rabson int debug_level;
76158b9798SRick Macklem static char ccfile_dirlist[PATH_MAX + 1], ccfile_substring[NAME_MAX + 1];
77158b9798SRick Macklem static char pref_realm[1024];
787e301aa9SRick Macklem static int verbose;
79bd5012b2SRick Macklem static int hostbased_initiator_cred;
80c3234382SRick Macklem #ifndef WITHOUT_KERBEROS
81c3234382SRick Macklem /* 1.2.752.43.13.14 */
82c3234382SRick Macklem static gss_OID_desc gss_krb5_set_allowable_enctypes_x_desc =
83c3234382SRick Macklem {6, (void *) "\x2a\x85\x70\x2b\x0d\x0e"};
84c3234382SRick Macklem static gss_OID GSS_KRB5_SET_ALLOWABLE_ENCTYPES_X =
85c3234382SRick Macklem &gss_krb5_set_allowable_enctypes_x_desc;
86c3234382SRick Macklem static gss_OID_desc gss_krb5_mech_oid_x_desc =
87c3234382SRick Macklem {9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
88c3234382SRick Macklem static gss_OID GSS_KRB5_MECH_OID_X =
89c3234382SRick Macklem &gss_krb5_mech_oid_x_desc;
90c3234382SRick Macklem #endif
91a9148abdSDoug Rabson
92a9148abdSDoug Rabson static void gssd_load_mech(void);
93158b9798SRick Macklem static int find_ccache_file(const char *, uid_t, char *);
94158b9798SRick Macklem static int is_a_valid_tgt_cache(const char *, uid_t, int *, time_t *);
957e301aa9SRick Macklem static void gssd_verbose_out(const char *, ...);
96c3234382SRick Macklem #ifndef WITHOUT_KERBEROS
97bd5012b2SRick Macklem static krb5_error_code gssd_get_cc_from_keytab(const char *);
98c3234382SRick Macklem static OM_uint32 gssd_get_user_cred(OM_uint32 *, uid_t, gss_cred_id_t *);
99c3234382SRick Macklem #endif
100bd5012b2SRick Macklem void gssd_terminate(int);
101a9148abdSDoug Rabson
102a9148abdSDoug Rabson extern void gssd_1(struct svc_req *rqstp, SVCXPRT *transp);
103a9148abdSDoug Rabson
104a9148abdSDoug Rabson int
main(int argc,char ** argv)105a9148abdSDoug Rabson main(int argc, char **argv)
106a9148abdSDoug Rabson {
107a9148abdSDoug Rabson /*
108*a8f61691SGleb Smirnoff * We provide an RPC service on a Netlink socket. The kernel's GSS API
109*a8f61691SGleb Smirnoff * code will multicast its calls, we will listen to them, receive them,
110*a8f61691SGleb Smirnoff * process them and reply.
111a9148abdSDoug Rabson */
112*a8f61691SGleb Smirnoff int oldmask, ch, debug, jailed;
113a9148abdSDoug Rabson SVCXPRT *xprt;
11469772711SRick Macklem size_t jailed_size;
115a9148abdSDoug Rabson
116158b9798SRick Macklem /*
117158b9798SRick Macklem * Initialize the credential cache file name substring and the
118158b9798SRick Macklem * search directory list.
119158b9798SRick Macklem */
120158b9798SRick Macklem strlcpy(ccfile_substring, "krb5cc_", sizeof(ccfile_substring));
121158b9798SRick Macklem ccfile_dirlist[0] = '\0';
122158b9798SRick Macklem pref_realm[0] = '\0';
123a9148abdSDoug Rabson debug = 0;
1247e301aa9SRick Macklem verbose = 0;
125ba10a6d0SJohn Baldwin while ((ch = getopt(argc, argv, "dhvs:c:r:")) != -1) {
126a9148abdSDoug Rabson switch (ch) {
127a9148abdSDoug Rabson case 'd':
128a9148abdSDoug Rabson debug_level++;
129a9148abdSDoug Rabson break;
130bd5012b2SRick Macklem case 'h':
131bd5012b2SRick Macklem #ifndef WITHOUT_KERBEROS
132bd5012b2SRick Macklem /*
133bd5012b2SRick Macklem * Enable use of a host based initiator credential
134bd5012b2SRick Macklem * in the default keytab file.
135bd5012b2SRick Macklem */
136bd5012b2SRick Macklem hostbased_initiator_cred = 1;
137bd5012b2SRick Macklem #else
138bd5012b2SRick Macklem errx(1, "This option not available when built"
139bd5012b2SRick Macklem " without MK_KERBEROS\n");
140bd5012b2SRick Macklem #endif
141bd5012b2SRick Macklem break;
1427e301aa9SRick Macklem case 'v':
1437e301aa9SRick Macklem verbose = 1;
1447e301aa9SRick Macklem break;
145158b9798SRick Macklem case 's':
1468ac5ae56SRick Macklem #ifndef WITHOUT_KERBEROS
147158b9798SRick Macklem /*
148158b9798SRick Macklem * Set the directory search list. This enables use of
149158b9798SRick Macklem * find_ccache_file() to search the directories for a
150158b9798SRick Macklem * suitable credentials cache file.
151158b9798SRick Macklem */
152158b9798SRick Macklem strlcpy(ccfile_dirlist, optarg, sizeof(ccfile_dirlist));
1538ac5ae56SRick Macklem #else
1548ac5ae56SRick Macklem errx(1, "This option not available when built"
1558ac5ae56SRick Macklem " without MK_KERBEROS\n");
1568ac5ae56SRick Macklem #endif
157158b9798SRick Macklem break;
158158b9798SRick Macklem case 'c':
159158b9798SRick Macklem /*
160158b9798SRick Macklem * Specify a non-default credential cache file
161158b9798SRick Macklem * substring.
162158b9798SRick Macklem */
163158b9798SRick Macklem strlcpy(ccfile_substring, optarg,
164158b9798SRick Macklem sizeof(ccfile_substring));
165158b9798SRick Macklem break;
166158b9798SRick Macklem case 'r':
167158b9798SRick Macklem /*
168158b9798SRick Macklem * Set the preferred realm for the credential cache tgt.
169158b9798SRick Macklem */
170158b9798SRick Macklem strlcpy(pref_realm, optarg, sizeof(pref_realm));
171158b9798SRick Macklem break;
172a9148abdSDoug Rabson default:
173158b9798SRick Macklem fprintf(stderr,
174158b9798SRick Macklem "usage: %s [-d] [-s dir-list] [-c file-substring]"
175158b9798SRick Macklem " [-r preferred-realm]\n", argv[0]);
176a9148abdSDoug Rabson exit(1);
177a9148abdSDoug Rabson break;
178a9148abdSDoug Rabson }
179a9148abdSDoug Rabson }
180a9148abdSDoug Rabson
181a9148abdSDoug Rabson gssd_load_mech();
182a9148abdSDoug Rabson
183c3234382SRick Macklem if (!debug_level) {
1842f04825aSMark Johnston if (daemon(0, 0) != 0)
1852f04825aSMark Johnston err(1, "Can't daemonize");
186c3234382SRick Macklem signal(SIGINT, SIG_IGN);
187c3234382SRick Macklem signal(SIGQUIT, SIG_IGN);
188c3234382SRick Macklem signal(SIGHUP, SIG_IGN);
189c3234382SRick Macklem }
190bd5012b2SRick Macklem signal(SIGTERM, gssd_terminate);
191c6da8eb2SSean Eric Fagan signal(SIGPIPE, gssd_terminate);
192a9148abdSDoug Rabson
193*a8f61691SGleb Smirnoff if ((xprt = svc_nl_create("kgss")) == NULL) {
194765782e7SRick Macklem if (debug_level == 0) {
195765782e7SRick Macklem syslog(LOG_ERR,
196765782e7SRick Macklem "Can't create transport for local gssd socket");
197765782e7SRick Macklem exit(1);
198765782e7SRick Macklem }
199a9148abdSDoug Rabson err(1, "Can't create transport for local gssd socket");
200a9148abdSDoug Rabson }
201a9148abdSDoug Rabson if (!svc_reg(xprt, GSSD, GSSDVERS, gssd_1, NULL)) {
202765782e7SRick Macklem if (debug_level == 0) {
203765782e7SRick Macklem syslog(LOG_ERR,
204765782e7SRick Macklem "Can't register service for local gssd socket");
205765782e7SRick Macklem exit(1);
206765782e7SRick Macklem }
207a9148abdSDoug Rabson err(1, "Can't register service for local gssd socket");
208a9148abdSDoug Rabson }
209a9148abdSDoug Rabson
210a9148abdSDoug Rabson LIST_INIT(&gss_resources);
211a9148abdSDoug Rabson gss_next_id = 1;
212a9148abdSDoug Rabson gss_start_time = time(0);
213a9148abdSDoug Rabson svc_run();
214a9148abdSDoug Rabson
215a9148abdSDoug Rabson return (0);
216a9148abdSDoug Rabson }
217a9148abdSDoug Rabson
218a9148abdSDoug Rabson static void
gssd_load_mech(void)219a9148abdSDoug Rabson gssd_load_mech(void)
220a9148abdSDoug Rabson {
221a9148abdSDoug Rabson FILE *fp;
222a9148abdSDoug Rabson char buf[256];
223a9148abdSDoug Rabson char *p;
224a9148abdSDoug Rabson char *name, *oid, *lib, *kobj;
225a9148abdSDoug Rabson
226a9148abdSDoug Rabson fp = fopen(_PATH_GSS_MECH, "r");
227a9148abdSDoug Rabson if (!fp)
228a9148abdSDoug Rabson return;
229a9148abdSDoug Rabson
230a9148abdSDoug Rabson while (fgets(buf, sizeof(buf), fp)) {
231a9148abdSDoug Rabson if (*buf == '#')
232a9148abdSDoug Rabson continue;
233a9148abdSDoug Rabson p = buf;
234a9148abdSDoug Rabson name = strsep(&p, "\t\n ");
235a9148abdSDoug Rabson if (p) while (isspace(*p)) p++;
236a9148abdSDoug Rabson oid = strsep(&p, "\t\n ");
237a9148abdSDoug Rabson if (p) while (isspace(*p)) p++;
238a9148abdSDoug Rabson lib = strsep(&p, "\t\n ");
239a9148abdSDoug Rabson if (p) while (isspace(*p)) p++;
240a9148abdSDoug Rabson kobj = strsep(&p, "\t\n ");
241a9148abdSDoug Rabson if (!name || !oid || !lib || !kobj)
242a9148abdSDoug Rabson continue;
243a9148abdSDoug Rabson
244a9148abdSDoug Rabson if (strcmp(kobj, "-")) {
245a9148abdSDoug Rabson /*
246a9148abdSDoug Rabson * Attempt to load the kernel module if its
247a9148abdSDoug Rabson * not already present.
248a9148abdSDoug Rabson */
249a9148abdSDoug Rabson if (modfind(kobj) < 0) {
250a9148abdSDoug Rabson if (kldload(kobj) < 0) {
251a9148abdSDoug Rabson fprintf(stderr,
252a9148abdSDoug Rabson "%s: can't find or load kernel module %s for %s\n",
253a9148abdSDoug Rabson getprogname(), kobj, name);
254a9148abdSDoug Rabson }
255a9148abdSDoug Rabson }
256a9148abdSDoug Rabson }
257a9148abdSDoug Rabson }
258a9148abdSDoug Rabson fclose(fp);
259a9148abdSDoug Rabson }
260a9148abdSDoug Rabson
261a9148abdSDoug Rabson static void *
gssd_find_resource(uint64_t id)262a9148abdSDoug Rabson gssd_find_resource(uint64_t id)
263a9148abdSDoug Rabson {
264a9148abdSDoug Rabson struct gss_resource *gr;
265a9148abdSDoug Rabson
266a9148abdSDoug Rabson if (!id)
267a9148abdSDoug Rabson return (NULL);
268a9148abdSDoug Rabson
269a9148abdSDoug Rabson LIST_FOREACH(gr, &gss_resources, gr_link)
270a9148abdSDoug Rabson if (gr->gr_id == id)
271a9148abdSDoug Rabson return (gr->gr_res);
272a9148abdSDoug Rabson
273a9148abdSDoug Rabson return (NULL);
274a9148abdSDoug Rabson }
275a9148abdSDoug Rabson
276a9148abdSDoug Rabson static uint64_t
gssd_make_resource(void * res)277a9148abdSDoug Rabson gssd_make_resource(void *res)
278a9148abdSDoug Rabson {
279a9148abdSDoug Rabson struct gss_resource *gr;
280a9148abdSDoug Rabson
281a9148abdSDoug Rabson if (!res)
282a9148abdSDoug Rabson return (0);
283a9148abdSDoug Rabson
284a9148abdSDoug Rabson gr = malloc(sizeof(struct gss_resource));
285a9148abdSDoug Rabson if (!gr)
286a9148abdSDoug Rabson return (0);
287a9148abdSDoug Rabson gr->gr_id = (gss_next_id++) + ((uint64_t) gss_start_time << 32);
288a9148abdSDoug Rabson gr->gr_res = res;
289a9148abdSDoug Rabson LIST_INSERT_HEAD(&gss_resources, gr, gr_link);
290a9148abdSDoug Rabson gss_resource_count++;
291a9148abdSDoug Rabson if (debug_level > 1)
292a9148abdSDoug Rabson printf("%d resources allocated\n", gss_resource_count);
293a9148abdSDoug Rabson
294a9148abdSDoug Rabson return (gr->gr_id);
295a9148abdSDoug Rabson }
296a9148abdSDoug Rabson
297a9148abdSDoug Rabson static void
gssd_delete_resource(uint64_t id)298a9148abdSDoug Rabson gssd_delete_resource(uint64_t id)
299a9148abdSDoug Rabson {
300a9148abdSDoug Rabson struct gss_resource *gr;
301a9148abdSDoug Rabson
302a9148abdSDoug Rabson LIST_FOREACH(gr, &gss_resources, gr_link) {
303a9148abdSDoug Rabson if (gr->gr_id == id) {
304a9148abdSDoug Rabson LIST_REMOVE(gr, gr_link);
305a9148abdSDoug Rabson free(gr);
306a9148abdSDoug Rabson gss_resource_count--;
307a9148abdSDoug Rabson if (debug_level > 1)
308a9148abdSDoug Rabson printf("%d resources allocated\n",
309a9148abdSDoug Rabson gss_resource_count);
310a9148abdSDoug Rabson return;
311a9148abdSDoug Rabson }
312a9148abdSDoug Rabson }
313a9148abdSDoug Rabson }
314a9148abdSDoug Rabson
3157e301aa9SRick Macklem static void
gssd_verbose_out(const char * fmt,...)3167e301aa9SRick Macklem gssd_verbose_out(const char *fmt, ...)
3177e301aa9SRick Macklem {
3187e301aa9SRick Macklem va_list ap;
3197e301aa9SRick Macklem
3207e301aa9SRick Macklem if (verbose != 0) {
3217e301aa9SRick Macklem va_start(ap, fmt);
3227e301aa9SRick Macklem if (debug_level == 0)
3237e301aa9SRick Macklem vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap);
3247e301aa9SRick Macklem else
3257e301aa9SRick Macklem vfprintf(stderr, fmt, ap);
3267e301aa9SRick Macklem va_end(ap);
3277e301aa9SRick Macklem }
3287e301aa9SRick Macklem }
3297e301aa9SRick Macklem
330a9148abdSDoug Rabson bool_t
gssd_null_1_svc(void * argp,void * result,struct svc_req * rqstp)331a9148abdSDoug Rabson gssd_null_1_svc(void *argp, void *result, struct svc_req *rqstp)
332a9148abdSDoug Rabson {
333a9148abdSDoug Rabson
3347e301aa9SRick Macklem gssd_verbose_out("gssd_null: done\n");
335a9148abdSDoug Rabson return (TRUE);
336a9148abdSDoug Rabson }
337a9148abdSDoug Rabson
338a9148abdSDoug Rabson bool_t
gssd_init_sec_context_1_svc(init_sec_context_args * argp,init_sec_context_res * result,struct svc_req * rqstp)339a9148abdSDoug Rabson gssd_init_sec_context_1_svc(init_sec_context_args *argp, init_sec_context_res *result, struct svc_req *rqstp)
340a9148abdSDoug Rabson {
341a9148abdSDoug Rabson gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
342a9148abdSDoug Rabson gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
343a9148abdSDoug Rabson gss_name_t name = GSS_C_NO_NAME;
344158b9798SRick Macklem char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
345c3234382SRick Macklem int gotone, gotcred;
346c3234382SRick Macklem OM_uint32 min_stat;
347c3234382SRick Macklem #ifndef WITHOUT_KERBEROS
348c3234382SRick Macklem gss_buffer_desc principal_desc;
349c3234382SRick Macklem char enctype[sizeof(uint32_t)];
350c3234382SRick Macklem int key_enctype;
351c3234382SRick Macklem OM_uint32 maj_stat;
352c3234382SRick Macklem #endif
353a9148abdSDoug Rabson
354a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
355bd5012b2SRick Macklem if (hostbased_initiator_cred != 0 && argp->cred != 0 &&
356bd5012b2SRick Macklem argp->uid == 0) {
357bd5012b2SRick Macklem /*
358bd5012b2SRick Macklem * These credentials are for a host based initiator name
359bd5012b2SRick Macklem * in a keytab file, which should now have credentials
360bd5012b2SRick Macklem * in /tmp/krb5cc_gssd, because gss_acquire_cred() did
361bd5012b2SRick Macklem * the equivalent of "kinit -k".
362bd5012b2SRick Macklem */
363bd5012b2SRick Macklem snprintf(ccname, sizeof(ccname), "FILE:%s",
364bd5012b2SRick Macklem GSSD_CREDENTIAL_CACHE_FILE);
365bd5012b2SRick Macklem } else if (ccfile_dirlist[0] != '\0' && argp->cred == 0) {
366158b9798SRick Macklem /*
367158b9798SRick Macklem * For the "-s" case and no credentials provided as an
368158b9798SRick Macklem * argument, search the directory list for an appropriate
369158b9798SRick Macklem * credential cache file. If the search fails, return failure.
370158b9798SRick Macklem */
371158b9798SRick Macklem gotone = 0;
372158b9798SRick Macklem cp = ccfile_dirlist;
373158b9798SRick Macklem do {
374158b9798SRick Macklem cp2 = strchr(cp, ':');
375158b9798SRick Macklem if (cp2 != NULL)
376158b9798SRick Macklem *cp2 = '\0';
377158b9798SRick Macklem gotone = find_ccache_file(cp, argp->uid, ccname);
378158b9798SRick Macklem if (gotone != 0)
379158b9798SRick Macklem break;
380158b9798SRick Macklem if (cp2 != NULL)
381158b9798SRick Macklem *cp2++ = ':';
382158b9798SRick Macklem cp = cp2;
383158b9798SRick Macklem } while (cp != NULL && *cp != '\0');
384158b9798SRick Macklem if (gotone == 0) {
385158b9798SRick Macklem result->major_status = GSS_S_CREDENTIALS_EXPIRED;
3867e301aa9SRick Macklem gssd_verbose_out("gssd_init_sec_context: -s no"
3877e301aa9SRick Macklem " credential cache file found for uid=%d\n",
3887e301aa9SRick Macklem (int)argp->uid);
389158b9798SRick Macklem return (TRUE);
390158b9798SRick Macklem }
391158b9798SRick Macklem } else {
392158b9798SRick Macklem /*
393158b9798SRick Macklem * If there wasn't a "-s" option or the credentials have
394158b9798SRick Macklem * been provided as an argument, do it the old way.
395158b9798SRick Macklem * When credentials are provided, the uid should be root.
396158b9798SRick Macklem */
397158b9798SRick Macklem if (argp->cred != 0 && argp->uid != 0) {
398158b9798SRick Macklem if (debug_level == 0)
399158b9798SRick Macklem syslog(LOG_ERR, "gss_init_sec_context:"
400158b9798SRick Macklem " cred for non-root");
401158b9798SRick Macklem else
402158b9798SRick Macklem fprintf(stderr, "gss_init_sec_context:"
403158b9798SRick Macklem " cred for non-root\n");
404158b9798SRick Macklem }
405158b9798SRick Macklem snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
406158b9798SRick Macklem (int) argp->uid);
407158b9798SRick Macklem }
408158b9798SRick Macklem setenv("KRB5CCNAME", ccname, TRUE);
409158b9798SRick Macklem
410a9148abdSDoug Rabson if (argp->cred) {
411a9148abdSDoug Rabson cred = gssd_find_resource(argp->cred);
412a9148abdSDoug Rabson if (!cred) {
413a9148abdSDoug Rabson result->major_status = GSS_S_CREDENTIALS_EXPIRED;
4147e301aa9SRick Macklem gssd_verbose_out("gssd_init_sec_context: cred"
4157e301aa9SRick Macklem " resource not found\n");
416a9148abdSDoug Rabson return (TRUE);
417a9148abdSDoug Rabson }
418a9148abdSDoug Rabson }
419a9148abdSDoug Rabson if (argp->ctx) {
420a9148abdSDoug Rabson ctx = gssd_find_resource(argp->ctx);
421a9148abdSDoug Rabson if (!ctx) {
422a9148abdSDoug Rabson result->major_status = GSS_S_CONTEXT_EXPIRED;
4237e301aa9SRick Macklem gssd_verbose_out("gssd_init_sec_context: context"
4247e301aa9SRick Macklem " resource not found\n");
425a9148abdSDoug Rabson return (TRUE);
426a9148abdSDoug Rabson }
427a9148abdSDoug Rabson }
428a9148abdSDoug Rabson if (argp->name) {
429a9148abdSDoug Rabson name = gssd_find_resource(argp->name);
430a9148abdSDoug Rabson if (!name) {
431a9148abdSDoug Rabson result->major_status = GSS_S_BAD_NAME;
4327e301aa9SRick Macklem gssd_verbose_out("gssd_init_sec_context: name"
4337e301aa9SRick Macklem " resource not found\n");
434a9148abdSDoug Rabson return (TRUE);
435a9148abdSDoug Rabson }
436a9148abdSDoug Rabson }
437c3234382SRick Macklem gotcred = 0;
438a9148abdSDoug Rabson
439a9148abdSDoug Rabson result->major_status = gss_init_sec_context(&result->minor_status,
440a9148abdSDoug Rabson cred, &ctx, name, argp->mech_type,
441a9148abdSDoug Rabson argp->req_flags, argp->time_req, argp->input_chan_bindings,
442a9148abdSDoug Rabson &argp->input_token, &result->actual_mech_type,
443a9148abdSDoug Rabson &result->output_token, &result->ret_flags, &result->time_rec);
4447e301aa9SRick Macklem gssd_verbose_out("gssd_init_sec_context: done major=0x%x minor=%d"
4457e301aa9SRick Macklem " uid=%d\n", (unsigned int)result->major_status,
4467e301aa9SRick Macklem (int)result->minor_status, (int)argp->uid);
447c3234382SRick Macklem if (gotcred != 0)
448c3234382SRick Macklem gss_release_cred(&min_stat, &cred);
449a9148abdSDoug Rabson
450a9148abdSDoug Rabson if (result->major_status == GSS_S_COMPLETE
451a9148abdSDoug Rabson || result->major_status == GSS_S_CONTINUE_NEEDED) {
452a9148abdSDoug Rabson if (argp->ctx)
453a9148abdSDoug Rabson result->ctx = argp->ctx;
454a9148abdSDoug Rabson else
455a9148abdSDoug Rabson result->ctx = gssd_make_resource(ctx);
456a9148abdSDoug Rabson }
457a9148abdSDoug Rabson
458a9148abdSDoug Rabson return (TRUE);
459a9148abdSDoug Rabson }
460a9148abdSDoug Rabson
461a9148abdSDoug Rabson bool_t
gssd_accept_sec_context_1_svc(accept_sec_context_args * argp,accept_sec_context_res * result,struct svc_req * rqstp)462a9148abdSDoug Rabson gssd_accept_sec_context_1_svc(accept_sec_context_args *argp, accept_sec_context_res *result, struct svc_req *rqstp)
463a9148abdSDoug Rabson {
464a9148abdSDoug Rabson gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;
465a9148abdSDoug Rabson gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
466a9148abdSDoug Rabson gss_name_t src_name;
467a9148abdSDoug Rabson gss_cred_id_t delegated_cred_handle;
468a9148abdSDoug Rabson
469a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
470a9148abdSDoug Rabson if (argp->ctx) {
471a9148abdSDoug Rabson ctx = gssd_find_resource(argp->ctx);
472a9148abdSDoug Rabson if (!ctx) {
473a9148abdSDoug Rabson result->major_status = GSS_S_CONTEXT_EXPIRED;
4747e301aa9SRick Macklem gssd_verbose_out("gssd_accept_sec_context: ctx"
4757e301aa9SRick Macklem " resource not found\n");
476a9148abdSDoug Rabson return (TRUE);
477a9148abdSDoug Rabson }
478a9148abdSDoug Rabson }
479a9148abdSDoug Rabson if (argp->cred) {
480a9148abdSDoug Rabson cred = gssd_find_resource(argp->cred);
481a9148abdSDoug Rabson if (!cred) {
482a9148abdSDoug Rabson result->major_status = GSS_S_CREDENTIALS_EXPIRED;
4837e301aa9SRick Macklem gssd_verbose_out("gssd_accept_sec_context: cred"
4847e301aa9SRick Macklem " resource not found\n");
485a9148abdSDoug Rabson return (TRUE);
486a9148abdSDoug Rabson }
487a9148abdSDoug Rabson }
488a9148abdSDoug Rabson
489a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
490a9148abdSDoug Rabson result->major_status = gss_accept_sec_context(&result->minor_status,
491a9148abdSDoug Rabson &ctx, cred, &argp->input_token, argp->input_chan_bindings,
492a9148abdSDoug Rabson &src_name, &result->mech_type, &result->output_token,
493a9148abdSDoug Rabson &result->ret_flags, &result->time_rec,
494a9148abdSDoug Rabson &delegated_cred_handle);
4957e301aa9SRick Macklem gssd_verbose_out("gssd_accept_sec_context: done major=0x%x minor=%d\n",
4967e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
497a9148abdSDoug Rabson
498a9148abdSDoug Rabson if (result->major_status == GSS_S_COMPLETE
499a9148abdSDoug Rabson || result->major_status == GSS_S_CONTINUE_NEEDED) {
500a9148abdSDoug Rabson if (argp->ctx)
501a9148abdSDoug Rabson result->ctx = argp->ctx;
502a9148abdSDoug Rabson else
503a9148abdSDoug Rabson result->ctx = gssd_make_resource(ctx);
504a9148abdSDoug Rabson result->src_name = gssd_make_resource(src_name);
505a9148abdSDoug Rabson result->delegated_cred_handle =
506a9148abdSDoug Rabson gssd_make_resource(delegated_cred_handle);
507a9148abdSDoug Rabson }
508a9148abdSDoug Rabson
509a9148abdSDoug Rabson return (TRUE);
510a9148abdSDoug Rabson }
511a9148abdSDoug Rabson
512a9148abdSDoug Rabson bool_t
gssd_delete_sec_context_1_svc(delete_sec_context_args * argp,delete_sec_context_res * result,struct svc_req * rqstp)513a9148abdSDoug Rabson gssd_delete_sec_context_1_svc(delete_sec_context_args *argp, delete_sec_context_res *result, struct svc_req *rqstp)
514a9148abdSDoug Rabson {
515a9148abdSDoug Rabson gss_ctx_id_t ctx = gssd_find_resource(argp->ctx);
516a9148abdSDoug Rabson
517a9148abdSDoug Rabson if (ctx) {
518a9148abdSDoug Rabson result->major_status = gss_delete_sec_context(
519a9148abdSDoug Rabson &result->minor_status, &ctx, &result->output_token);
520a9148abdSDoug Rabson gssd_delete_resource(argp->ctx);
521a9148abdSDoug Rabson } else {
522a9148abdSDoug Rabson result->major_status = GSS_S_COMPLETE;
523a9148abdSDoug Rabson result->minor_status = 0;
524a9148abdSDoug Rabson }
5257e301aa9SRick Macklem gssd_verbose_out("gssd_delete_sec_context: done major=0x%x minor=%d\n",
5267e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
527a9148abdSDoug Rabson
528a9148abdSDoug Rabson return (TRUE);
529a9148abdSDoug Rabson }
530a9148abdSDoug Rabson
531a9148abdSDoug Rabson bool_t
gssd_export_sec_context_1_svc(export_sec_context_args * argp,export_sec_context_res * result,struct svc_req * rqstp)532a9148abdSDoug Rabson gssd_export_sec_context_1_svc(export_sec_context_args *argp, export_sec_context_res *result, struct svc_req *rqstp)
533a9148abdSDoug Rabson {
534a9148abdSDoug Rabson gss_ctx_id_t ctx = gssd_find_resource(argp->ctx);
535a9148abdSDoug Rabson
536a9148abdSDoug Rabson if (ctx) {
537a9148abdSDoug Rabson result->major_status = gss_export_sec_context(
538a9148abdSDoug Rabson &result->minor_status, &ctx,
539a9148abdSDoug Rabson &result->interprocess_token);
540a9148abdSDoug Rabson result->format = KGSS_HEIMDAL_1_1;
541a9148abdSDoug Rabson gssd_delete_resource(argp->ctx);
542a9148abdSDoug Rabson } else {
543a9148abdSDoug Rabson result->major_status = GSS_S_FAILURE;
544a9148abdSDoug Rabson result->minor_status = 0;
545a9148abdSDoug Rabson result->interprocess_token.length = 0;
546a9148abdSDoug Rabson result->interprocess_token.value = NULL;
547a9148abdSDoug Rabson }
5487e301aa9SRick Macklem gssd_verbose_out("gssd_export_sec_context: done major=0x%x minor=%d\n",
5497e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
550a9148abdSDoug Rabson
551a9148abdSDoug Rabson return (TRUE);
552a9148abdSDoug Rabson }
553a9148abdSDoug Rabson
554a9148abdSDoug Rabson bool_t
gssd_import_name_1_svc(import_name_args * argp,import_name_res * result,struct svc_req * rqstp)555a9148abdSDoug Rabson gssd_import_name_1_svc(import_name_args *argp, import_name_res *result, struct svc_req *rqstp)
556a9148abdSDoug Rabson {
557a9148abdSDoug Rabson gss_name_t name;
558a9148abdSDoug Rabson
559a9148abdSDoug Rabson result->major_status = gss_import_name(&result->minor_status,
560a9148abdSDoug Rabson &argp->input_name_buffer, argp->input_name_type, &name);
5617e301aa9SRick Macklem gssd_verbose_out("gssd_import_name: done major=0x%x minor=%d\n",
5627e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
563a9148abdSDoug Rabson
564a9148abdSDoug Rabson if (result->major_status == GSS_S_COMPLETE)
565a9148abdSDoug Rabson result->output_name = gssd_make_resource(name);
566a9148abdSDoug Rabson else
567a9148abdSDoug Rabson result->output_name = 0;
568a9148abdSDoug Rabson
569a9148abdSDoug Rabson return (TRUE);
570a9148abdSDoug Rabson }
571a9148abdSDoug Rabson
57282ea0132SRick Macklem /*
57382ea0132SRick Macklem * If the name is a numeric IP host address, do a DNS lookup on it and
57482ea0132SRick Macklem * return the DNS name in a malloc'd string.
57582ea0132SRick Macklem */
57682ea0132SRick Macklem static char *
gssd_conv_ip_to_dns(int len,char * name)57782ea0132SRick Macklem gssd_conv_ip_to_dns(int len, char *name)
57882ea0132SRick Macklem {
57982ea0132SRick Macklem struct sockaddr_in sin;
58082ea0132SRick Macklem struct sockaddr_in6 sin6;
58182ea0132SRick Macklem char *retcp;
58282ea0132SRick Macklem
58382ea0132SRick Macklem retcp = NULL;
58482ea0132SRick Macklem if (len > 0) {
58582ea0132SRick Macklem retcp = mem_alloc(NI_MAXHOST);
58682ea0132SRick Macklem memcpy(retcp, name, len);
58782ea0132SRick Macklem retcp[len] = '\0';
58882ea0132SRick Macklem if (inet_pton(AF_INET, retcp, &sin.sin_addr) != 0) {
58982ea0132SRick Macklem sin.sin_family = AF_INET;
59082ea0132SRick Macklem sin.sin_len = sizeof(sin);
59182ea0132SRick Macklem sin.sin_port = 0;
59282ea0132SRick Macklem if (getnameinfo((struct sockaddr *)&sin,
59382ea0132SRick Macklem sizeof(sin), retcp, NI_MAXHOST,
59482ea0132SRick Macklem NULL, 0, NI_NAMEREQD) != 0) {
59582ea0132SRick Macklem mem_free(retcp, NI_MAXHOST);
59682ea0132SRick Macklem return (NULL);
59782ea0132SRick Macklem }
59882ea0132SRick Macklem } else if (inet_pton(AF_INET6, retcp, &sin6.sin6_addr) != 0) {
59982ea0132SRick Macklem sin6.sin6_family = AF_INET6;
60082ea0132SRick Macklem sin6.sin6_len = sizeof(sin6);
60182ea0132SRick Macklem sin6.sin6_port = 0;
60282ea0132SRick Macklem if (getnameinfo((struct sockaddr *)&sin6,
60382ea0132SRick Macklem sizeof(sin6), retcp, NI_MAXHOST,
60482ea0132SRick Macklem NULL, 0, NI_NAMEREQD) != 0) {
60582ea0132SRick Macklem mem_free(retcp, NI_MAXHOST);
60682ea0132SRick Macklem return (NULL);
60782ea0132SRick Macklem }
60882ea0132SRick Macklem } else {
60982ea0132SRick Macklem mem_free(retcp, NI_MAXHOST);
61082ea0132SRick Macklem return (NULL);
61182ea0132SRick Macklem }
61282ea0132SRick Macklem gssd_verbose_out("gssd_conv_ip_to_dns: %s\n", retcp);
61382ea0132SRick Macklem }
61482ea0132SRick Macklem return (retcp);
61582ea0132SRick Macklem }
61682ea0132SRick Macklem
617a9148abdSDoug Rabson bool_t
gssd_canonicalize_name_1_svc(canonicalize_name_args * argp,canonicalize_name_res * result,struct svc_req * rqstp)618a9148abdSDoug Rabson gssd_canonicalize_name_1_svc(canonicalize_name_args *argp, canonicalize_name_res *result, struct svc_req *rqstp)
619a9148abdSDoug Rabson {
620a9148abdSDoug Rabson gss_name_t name = gssd_find_resource(argp->input_name);
621a9148abdSDoug Rabson gss_name_t output_name;
622a9148abdSDoug Rabson
623a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
624a9148abdSDoug Rabson if (!name) {
625a9148abdSDoug Rabson result->major_status = GSS_S_BAD_NAME;
626a9148abdSDoug Rabson return (TRUE);
627a9148abdSDoug Rabson }
628a9148abdSDoug Rabson
629a9148abdSDoug Rabson result->major_status = gss_canonicalize_name(&result->minor_status,
630a9148abdSDoug Rabson name, argp->mech_type, &output_name);
6317e301aa9SRick Macklem gssd_verbose_out("gssd_canonicalize_name: done major=0x%x minor=%d\n",
6327e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
633a9148abdSDoug Rabson
634a9148abdSDoug Rabson if (result->major_status == GSS_S_COMPLETE)
635a9148abdSDoug Rabson result->output_name = gssd_make_resource(output_name);
636a9148abdSDoug Rabson else
637a9148abdSDoug Rabson result->output_name = 0;
638a9148abdSDoug Rabson
639a9148abdSDoug Rabson return (TRUE);
640a9148abdSDoug Rabson }
641a9148abdSDoug Rabson
642a9148abdSDoug Rabson bool_t
gssd_export_name_1_svc(export_name_args * argp,export_name_res * result,struct svc_req * rqstp)643a9148abdSDoug Rabson gssd_export_name_1_svc(export_name_args *argp, export_name_res *result, struct svc_req *rqstp)
644a9148abdSDoug Rabson {
645a9148abdSDoug Rabson gss_name_t name = gssd_find_resource(argp->input_name);
646a9148abdSDoug Rabson
647a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
648a9148abdSDoug Rabson if (!name) {
649a9148abdSDoug Rabson result->major_status = GSS_S_BAD_NAME;
6507e301aa9SRick Macklem gssd_verbose_out("gssd_export_name: name resource not found\n");
651a9148abdSDoug Rabson return (TRUE);
652a9148abdSDoug Rabson }
653a9148abdSDoug Rabson
654a9148abdSDoug Rabson result->major_status = gss_export_name(&result->minor_status,
655a9148abdSDoug Rabson name, &result->exported_name);
6567e301aa9SRick Macklem gssd_verbose_out("gssd_export_name: done major=0x%x minor=%d\n",
6577e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
658a9148abdSDoug Rabson
659a9148abdSDoug Rabson return (TRUE);
660a9148abdSDoug Rabson }
661a9148abdSDoug Rabson
662a9148abdSDoug Rabson bool_t
gssd_release_name_1_svc(release_name_args * argp,release_name_res * result,struct svc_req * rqstp)663a9148abdSDoug Rabson gssd_release_name_1_svc(release_name_args *argp, release_name_res *result, struct svc_req *rqstp)
664a9148abdSDoug Rabson {
665a9148abdSDoug Rabson gss_name_t name = gssd_find_resource(argp->input_name);
666a9148abdSDoug Rabson
667a9148abdSDoug Rabson if (name) {
668a9148abdSDoug Rabson result->major_status = gss_release_name(&result->minor_status,
669a9148abdSDoug Rabson &name);
670a9148abdSDoug Rabson gssd_delete_resource(argp->input_name);
671a9148abdSDoug Rabson } else {
672a9148abdSDoug Rabson result->major_status = GSS_S_COMPLETE;
673a9148abdSDoug Rabson result->minor_status = 0;
674a9148abdSDoug Rabson }
6757e301aa9SRick Macklem gssd_verbose_out("gssd_release_name: done major=0x%x minor=%d\n",
6767e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
677a9148abdSDoug Rabson
678a9148abdSDoug Rabson return (TRUE);
679a9148abdSDoug Rabson }
680a9148abdSDoug Rabson
681a9148abdSDoug Rabson bool_t
gssd_pname_to_uid_1_svc(pname_to_uid_args * argp,pname_to_uid_res * result,struct svc_req * rqstp)682a9148abdSDoug Rabson gssd_pname_to_uid_1_svc(pname_to_uid_args *argp, pname_to_uid_res *result, struct svc_req *rqstp)
683a9148abdSDoug Rabson {
684a9148abdSDoug Rabson gss_name_t name = gssd_find_resource(argp->pname);
685a9148abdSDoug Rabson uid_t uid;
686d1e9aa46SRick Macklem char buf[1024], *bufp;
687a9148abdSDoug Rabson struct passwd pwd, *pw;
688d1e9aa46SRick Macklem size_t buflen;
689d1e9aa46SRick Macklem int error;
690d1e9aa46SRick Macklem static size_t buflen_hint = 1024;
691a9148abdSDoug Rabson
692a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
693a9148abdSDoug Rabson if (name) {
694a9148abdSDoug Rabson result->major_status =
695a9148abdSDoug Rabson gss_pname_to_uid(&result->minor_status,
696a9148abdSDoug Rabson name, argp->mech, &uid);
697a9148abdSDoug Rabson if (result->major_status == GSS_S_COMPLETE) {
698a9148abdSDoug Rabson result->uid = uid;
699d1e9aa46SRick Macklem buflen = buflen_hint;
700d1e9aa46SRick Macklem for (;;) {
701d1e9aa46SRick Macklem pw = NULL;
702d1e9aa46SRick Macklem bufp = buf;
703d1e9aa46SRick Macklem if (buflen > sizeof(buf))
704d1e9aa46SRick Macklem bufp = malloc(buflen);
705d1e9aa46SRick Macklem if (bufp == NULL)
706d1e9aa46SRick Macklem break;
707d1e9aa46SRick Macklem error = getpwuid_r(uid, &pwd, bufp, buflen,
708d1e9aa46SRick Macklem &pw);
709d1e9aa46SRick Macklem if (error != ERANGE)
710d1e9aa46SRick Macklem break;
711d1e9aa46SRick Macklem if (buflen > sizeof(buf))
712d1e9aa46SRick Macklem free(bufp);
713d1e9aa46SRick Macklem buflen += 1024;
714d1e9aa46SRick Macklem if (buflen > buflen_hint)
715d1e9aa46SRick Macklem buflen_hint = buflen;
716d1e9aa46SRick Macklem }
717a9148abdSDoug Rabson if (pw) {
7185eff3ec6SJosh Paetzel int len = NGROUPS;
7195eff3ec6SJosh Paetzel int groups[NGROUPS];
720a9148abdSDoug Rabson result->gid = pw->pw_gid;
721a9148abdSDoug Rabson getgrouplist(pw->pw_name, pw->pw_gid,
722a9148abdSDoug Rabson groups, &len);
723a9148abdSDoug Rabson result->gidlist.gidlist_len = len;
724a9148abdSDoug Rabson result->gidlist.gidlist_val =
725a9148abdSDoug Rabson mem_alloc(len * sizeof(int));
726a9148abdSDoug Rabson memcpy(result->gidlist.gidlist_val, groups,
727a9148abdSDoug Rabson len * sizeof(int));
7287e301aa9SRick Macklem gssd_verbose_out("gssd_pname_to_uid: mapped"
7297e301aa9SRick Macklem " to uid=%d, gid=%d\n", (int)result->uid,
7307e301aa9SRick Macklem (int)result->gid);
731a9148abdSDoug Rabson } else {
732a9148abdSDoug Rabson result->gid = 65534;
733a9148abdSDoug Rabson result->gidlist.gidlist_len = 0;
734a9148abdSDoug Rabson result->gidlist.gidlist_val = NULL;
7357e301aa9SRick Macklem gssd_verbose_out("gssd_pname_to_uid: mapped"
7367e301aa9SRick Macklem " to uid=%d, but no groups\n",
7377e301aa9SRick Macklem (int)result->uid);
738a9148abdSDoug Rabson }
739d1e9aa46SRick Macklem if (bufp != NULL && buflen > sizeof(buf))
740d1e9aa46SRick Macklem free(bufp);
7417e301aa9SRick Macklem } else
7427e301aa9SRick Macklem gssd_verbose_out("gssd_pname_to_uid: failed major=0x%x"
7437e301aa9SRick Macklem " minor=%d\n", (unsigned int)result->major_status,
7447e301aa9SRick Macklem (int)result->minor_status);
745a9148abdSDoug Rabson } else {
746a9148abdSDoug Rabson result->major_status = GSS_S_BAD_NAME;
747a9148abdSDoug Rabson result->minor_status = 0;
7487e301aa9SRick Macklem gssd_verbose_out("gssd_pname_to_uid: no name\n");
749a9148abdSDoug Rabson }
750a9148abdSDoug Rabson
751a9148abdSDoug Rabson return (TRUE);
752a9148abdSDoug Rabson }
753a9148abdSDoug Rabson
754a9148abdSDoug Rabson bool_t
gssd_acquire_cred_1_svc(acquire_cred_args * argp,acquire_cred_res * result,struct svc_req * rqstp)755a9148abdSDoug Rabson gssd_acquire_cred_1_svc(acquire_cred_args *argp, acquire_cred_res *result, struct svc_req *rqstp)
756a9148abdSDoug Rabson {
757a9148abdSDoug Rabson gss_name_t desired_name = GSS_C_NO_NAME;
758a9148abdSDoug Rabson gss_cred_id_t cred;
759158b9798SRick Macklem char ccname[PATH_MAX + 5 + 1], *cp, *cp2;
760158b9798SRick Macklem int gotone;
761c3234382SRick Macklem #ifndef WITHOUT_KERBEROS
762c3234382SRick Macklem gss_buffer_desc namebuf;
763c3234382SRick Macklem uint32_t minstat;
764c3234382SRick Macklem krb5_error_code kret;
765c3234382SRick Macklem #endif
766a9148abdSDoug Rabson
767a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
768bd5012b2SRick Macklem if (argp->desired_name) {
769bd5012b2SRick Macklem desired_name = gssd_find_resource(argp->desired_name);
770bd5012b2SRick Macklem if (!desired_name) {
771bd5012b2SRick Macklem result->major_status = GSS_S_BAD_NAME;
772bd5012b2SRick Macklem gssd_verbose_out("gssd_acquire_cred: no desired name"
773bd5012b2SRick Macklem " found\n");
774bd5012b2SRick Macklem return (TRUE);
775bd5012b2SRick Macklem }
776bd5012b2SRick Macklem }
777bd5012b2SRick Macklem
778bd5012b2SRick Macklem #ifndef WITHOUT_KERBEROS
779bd5012b2SRick Macklem if (hostbased_initiator_cred != 0 && argp->desired_name != 0 &&
780bd5012b2SRick Macklem argp->uid == 0 && argp->cred_usage == GSS_C_INITIATE) {
781bd5012b2SRick Macklem /* This is a host based initiator name in the keytab file. */
782bd5012b2SRick Macklem snprintf(ccname, sizeof(ccname), "FILE:%s",
783bd5012b2SRick Macklem GSSD_CREDENTIAL_CACHE_FILE);
784bd5012b2SRick Macklem setenv("KRB5CCNAME", ccname, TRUE);
785bd5012b2SRick Macklem result->major_status = gss_display_name(&result->minor_status,
786bd5012b2SRick Macklem desired_name, &namebuf, NULL);
787bd5012b2SRick Macklem gssd_verbose_out("gssd_acquire_cred: desired name for host "
788bd5012b2SRick Macklem "based initiator cred major=0x%x minor=%d\n",
789bd5012b2SRick Macklem (unsigned int)result->major_status,
790bd5012b2SRick Macklem (int)result->minor_status);
791bd5012b2SRick Macklem if (result->major_status != GSS_S_COMPLETE)
792bd5012b2SRick Macklem return (TRUE);
793bd5012b2SRick Macklem if (namebuf.length > PATH_MAX + 5) {
794bd5012b2SRick Macklem result->minor_status = 0;
795bd5012b2SRick Macklem result->major_status = GSS_S_FAILURE;
796bd5012b2SRick Macklem return (TRUE);
797bd5012b2SRick Macklem }
798bd5012b2SRick Macklem memcpy(ccname, namebuf.value, namebuf.length);
799bd5012b2SRick Macklem ccname[namebuf.length] = '\0';
800bd5012b2SRick Macklem if ((cp = strchr(ccname, '@')) != NULL)
801bd5012b2SRick Macklem *cp = '/';
802bd5012b2SRick Macklem kret = gssd_get_cc_from_keytab(ccname);
803bd5012b2SRick Macklem gssd_verbose_out("gssd_acquire_cred: using keytab entry for "
804bd5012b2SRick Macklem "%s, kerberos ret=%d\n", ccname, (int)kret);
805bd5012b2SRick Macklem gss_release_buffer(&minstat, &namebuf);
806bd5012b2SRick Macklem if (kret != 0) {
807bd5012b2SRick Macklem result->minor_status = kret;
808bd5012b2SRick Macklem result->major_status = GSS_S_FAILURE;
809bd5012b2SRick Macklem return (TRUE);
810bd5012b2SRick Macklem }
811bd5012b2SRick Macklem } else
812bd5012b2SRick Macklem #endif /* !WITHOUT_KERBEROS */
813158b9798SRick Macklem if (ccfile_dirlist[0] != '\0' && argp->desired_name == 0) {
814158b9798SRick Macklem /*
815158b9798SRick Macklem * For the "-s" case and no name provided as an
816158b9798SRick Macklem * argument, search the directory list for an appropriate
817158b9798SRick Macklem * credential cache file. If the search fails, return failure.
818158b9798SRick Macklem */
819158b9798SRick Macklem gotone = 0;
820158b9798SRick Macklem cp = ccfile_dirlist;
821158b9798SRick Macklem do {
822158b9798SRick Macklem cp2 = strchr(cp, ':');
823158b9798SRick Macklem if (cp2 != NULL)
824158b9798SRick Macklem *cp2 = '\0';
825158b9798SRick Macklem gotone = find_ccache_file(cp, argp->uid, ccname);
826158b9798SRick Macklem if (gotone != 0)
827158b9798SRick Macklem break;
828158b9798SRick Macklem if (cp2 != NULL)
829158b9798SRick Macklem *cp2++ = ':';
830158b9798SRick Macklem cp = cp2;
831158b9798SRick Macklem } while (cp != NULL && *cp != '\0');
832158b9798SRick Macklem if (gotone == 0) {
833158b9798SRick Macklem result->major_status = GSS_S_CREDENTIALS_EXPIRED;
8347e301aa9SRick Macklem gssd_verbose_out("gssd_acquire_cred: no cred cache"
8357e301aa9SRick Macklem " file found\n");
836158b9798SRick Macklem return (TRUE);
837158b9798SRick Macklem }
838bd5012b2SRick Macklem setenv("KRB5CCNAME", ccname, TRUE);
839158b9798SRick Macklem } else {
840158b9798SRick Macklem /*
841158b9798SRick Macklem * If there wasn't a "-s" option or the name has
842158b9798SRick Macklem * been provided as an argument, do it the old way.
843158b9798SRick Macklem * When a name is provided, it will normally exist in the
844158b9798SRick Macklem * default keytab file and the uid will be root.
845158b9798SRick Macklem */
846158b9798SRick Macklem if (argp->desired_name != 0 && argp->uid != 0) {
847158b9798SRick Macklem if (debug_level == 0)
848158b9798SRick Macklem syslog(LOG_ERR, "gss_acquire_cred:"
849158b9798SRick Macklem " principal_name for non-root");
850158b9798SRick Macklem else
851158b9798SRick Macklem fprintf(stderr, "gss_acquire_cred:"
852158b9798SRick Macklem " principal_name for non-root\n");
853158b9798SRick Macklem }
854158b9798SRick Macklem snprintf(ccname, sizeof(ccname), "FILE:/tmp/krb5cc_%d",
855158b9798SRick Macklem (int) argp->uid);
856158b9798SRick Macklem setenv("KRB5CCNAME", ccname, TRUE);
857a9148abdSDoug Rabson }
858a9148abdSDoug Rabson
859a9148abdSDoug Rabson result->major_status = gss_acquire_cred(&result->minor_status,
86024f42edcSRick Macklem desired_name, argp->time_req, argp->desired_mechs,
861a9148abdSDoug Rabson argp->cred_usage, &cred, &result->actual_mechs, &result->time_rec);
8627e301aa9SRick Macklem gssd_verbose_out("gssd_acquire_cred: done major=0x%x minor=%d\n",
8637e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
864a9148abdSDoug Rabson
865a9148abdSDoug Rabson if (result->major_status == GSS_S_COMPLETE)
866a9148abdSDoug Rabson result->output_cred = gssd_make_resource(cred);
867a9148abdSDoug Rabson else
868a9148abdSDoug Rabson result->output_cred = 0;
869a9148abdSDoug Rabson
870a9148abdSDoug Rabson return (TRUE);
871a9148abdSDoug Rabson }
872a9148abdSDoug Rabson
873a9148abdSDoug Rabson bool_t
gssd_set_cred_option_1_svc(set_cred_option_args * argp,set_cred_option_res * result,struct svc_req * rqstp)874a9148abdSDoug Rabson gssd_set_cred_option_1_svc(set_cred_option_args *argp, set_cred_option_res *result, struct svc_req *rqstp)
875a9148abdSDoug Rabson {
876a9148abdSDoug Rabson gss_cred_id_t cred = gssd_find_resource(argp->cred);
877a9148abdSDoug Rabson
878a9148abdSDoug Rabson memset(result, 0, sizeof(*result));
879a9148abdSDoug Rabson if (!cred) {
880a9148abdSDoug Rabson result->major_status = GSS_S_CREDENTIALS_EXPIRED;
8817e301aa9SRick Macklem gssd_verbose_out("gssd_set_cred: no credentials\n");
882a9148abdSDoug Rabson return (TRUE);
883a9148abdSDoug Rabson }
884a9148abdSDoug Rabson
885a9148abdSDoug Rabson result->major_status = gss_set_cred_option(&result->minor_status,
886a9148abdSDoug Rabson &cred, argp->option_name, &argp->option_value);
8877e301aa9SRick Macklem gssd_verbose_out("gssd_set_cred: done major=0x%x minor=%d\n",
8887e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
889a9148abdSDoug Rabson
890a9148abdSDoug Rabson return (TRUE);
891a9148abdSDoug Rabson }
892a9148abdSDoug Rabson
893a9148abdSDoug Rabson bool_t
gssd_release_cred_1_svc(release_cred_args * argp,release_cred_res * result,struct svc_req * rqstp)894a9148abdSDoug Rabson gssd_release_cred_1_svc(release_cred_args *argp, release_cred_res *result, struct svc_req *rqstp)
895a9148abdSDoug Rabson {
896a9148abdSDoug Rabson gss_cred_id_t cred = gssd_find_resource(argp->cred);
897a9148abdSDoug Rabson
898a9148abdSDoug Rabson if (cred) {
899a9148abdSDoug Rabson result->major_status = gss_release_cred(&result->minor_status,
900a9148abdSDoug Rabson &cred);
901a9148abdSDoug Rabson gssd_delete_resource(argp->cred);
902a9148abdSDoug Rabson } else {
903a9148abdSDoug Rabson result->major_status = GSS_S_COMPLETE;
904a9148abdSDoug Rabson result->minor_status = 0;
905a9148abdSDoug Rabson }
9067e301aa9SRick Macklem gssd_verbose_out("gssd_release_cred: done major=0x%x minor=%d\n",
9077e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
908a9148abdSDoug Rabson
909a9148abdSDoug Rabson return (TRUE);
910a9148abdSDoug Rabson }
911a9148abdSDoug Rabson
912a9148abdSDoug Rabson bool_t
gssd_display_status_1_svc(display_status_args * argp,display_status_res * result,struct svc_req * rqstp)913a9148abdSDoug Rabson gssd_display_status_1_svc(display_status_args *argp, display_status_res *result, struct svc_req *rqstp)
914a9148abdSDoug Rabson {
915a9148abdSDoug Rabson
916a9148abdSDoug Rabson result->message_context = argp->message_context;
917a9148abdSDoug Rabson result->major_status = gss_display_status(&result->minor_status,
918a9148abdSDoug Rabson argp->status_value, argp->status_type, argp->mech_type,
919a9148abdSDoug Rabson &result->message_context, &result->status_string);
9207e301aa9SRick Macklem gssd_verbose_out("gssd_display_status: done major=0x%x minor=%d\n",
9217e301aa9SRick Macklem (unsigned int)result->major_status, (int)result->minor_status);
922a9148abdSDoug Rabson
923a9148abdSDoug Rabson return (TRUE);
924a9148abdSDoug Rabson }
925a9148abdSDoug Rabson
92682ea0132SRick Macklem bool_t
gssd_ip_to_dns_1_svc(ip_to_dns_args * argp,ip_to_dns_res * result,struct svc_req * rqstp)92782ea0132SRick Macklem gssd_ip_to_dns_1_svc(ip_to_dns_args *argp, ip_to_dns_res *result, struct svc_req *rqstp)
92882ea0132SRick Macklem {
92982ea0132SRick Macklem char *host;
93082ea0132SRick Macklem
93182ea0132SRick Macklem memset(result, 0, sizeof(*result));
93282ea0132SRick Macklem /* Check to see if the name is actually an IP address. */
93382ea0132SRick Macklem host = gssd_conv_ip_to_dns(argp->ip_addr.ip_addr_len,
93482ea0132SRick Macklem argp->ip_addr.ip_addr_val);
93582ea0132SRick Macklem if (host != NULL) {
93682ea0132SRick Macklem result->major_status = GSS_S_COMPLETE;
93782ea0132SRick Macklem result->dns_name.dns_name_len = strlen(host);
93882ea0132SRick Macklem result->dns_name.dns_name_val = host;
93982ea0132SRick Macklem return (TRUE);
94082ea0132SRick Macklem }
94182ea0132SRick Macklem result->major_status = GSS_S_FAILURE;
94282ea0132SRick Macklem return (TRUE);
94382ea0132SRick Macklem }
94482ea0132SRick Macklem
945a9148abdSDoug Rabson int
gssd_1_freeresult(SVCXPRT * transp,xdrproc_t xdr_result,caddr_t result)946a9148abdSDoug Rabson gssd_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
947a9148abdSDoug Rabson {
948a9148abdSDoug Rabson /*
949a9148abdSDoug Rabson * We don't use XDR to free the results - anything which was
950a9148abdSDoug Rabson * allocated came from GSS-API. We use xdr_result to figure
951a9148abdSDoug Rabson * out what to do.
952a9148abdSDoug Rabson */
953a9148abdSDoug Rabson OM_uint32 junk;
954a9148abdSDoug Rabson
955a9148abdSDoug Rabson if (xdr_result == (xdrproc_t) xdr_init_sec_context_res) {
956a9148abdSDoug Rabson init_sec_context_res *p = (init_sec_context_res *) result;
957a9148abdSDoug Rabson gss_release_buffer(&junk, &p->output_token);
958a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_accept_sec_context_res) {
959a9148abdSDoug Rabson accept_sec_context_res *p = (accept_sec_context_res *) result;
960a9148abdSDoug Rabson gss_release_buffer(&junk, &p->output_token);
961a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_delete_sec_context_res) {
962a9148abdSDoug Rabson delete_sec_context_res *p = (delete_sec_context_res *) result;
963a9148abdSDoug Rabson gss_release_buffer(&junk, &p->output_token);
964a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_export_sec_context_res) {
965a9148abdSDoug Rabson export_sec_context_res *p = (export_sec_context_res *) result;
966a9148abdSDoug Rabson if (p->interprocess_token.length)
967a9148abdSDoug Rabson memset(p->interprocess_token.value, 0,
968a9148abdSDoug Rabson p->interprocess_token.length);
969a9148abdSDoug Rabson gss_release_buffer(&junk, &p->interprocess_token);
970a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_export_name_res) {
971a9148abdSDoug Rabson export_name_res *p = (export_name_res *) result;
972a9148abdSDoug Rabson gss_release_buffer(&junk, &p->exported_name);
973a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_acquire_cred_res) {
974a9148abdSDoug Rabson acquire_cred_res *p = (acquire_cred_res *) result;
975a9148abdSDoug Rabson gss_release_oid_set(&junk, &p->actual_mechs);
976a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_pname_to_uid_res) {
977a9148abdSDoug Rabson pname_to_uid_res *p = (pname_to_uid_res *) result;
978a9148abdSDoug Rabson if (p->gidlist.gidlist_val)
979a9148abdSDoug Rabson free(p->gidlist.gidlist_val);
980a9148abdSDoug Rabson } else if (xdr_result == (xdrproc_t) xdr_display_status_res) {
981a9148abdSDoug Rabson display_status_res *p = (display_status_res *) result;
982a9148abdSDoug Rabson gss_release_buffer(&junk, &p->status_string);
983a9148abdSDoug Rabson }
984a9148abdSDoug Rabson
985a9148abdSDoug Rabson return (TRUE);
986a9148abdSDoug Rabson }
987158b9798SRick Macklem
988158b9798SRick Macklem /*
989158b9798SRick Macklem * Search a directory for the most likely candidate to be used as the
990158b9798SRick Macklem * credential cache for a uid. If successful, return 1 and fill the
991158b9798SRick Macklem * file's path id into "rpath". Otherwise, return 0.
992158b9798SRick Macklem */
993158b9798SRick Macklem static int
find_ccache_file(const char * dirpath,uid_t uid,char * rpath)994158b9798SRick Macklem find_ccache_file(const char *dirpath, uid_t uid, char *rpath)
995158b9798SRick Macklem {
996158b9798SRick Macklem DIR *dirp;
997158b9798SRick Macklem struct dirent *dp;
998158b9798SRick Macklem struct stat sb;
999158b9798SRick Macklem time_t exptime, oexptime;
1000158b9798SRick Macklem int gotone, len, rating, orating;
1001158b9798SRick Macklem char namepath[PATH_MAX + 5 + 1];
1002158b9798SRick Macklem char retpath[PATH_MAX + 5 + 1];
1003158b9798SRick Macklem
1004158b9798SRick Macklem dirp = opendir(dirpath);
1005158b9798SRick Macklem if (dirp == NULL)
1006158b9798SRick Macklem return (0);
1007158b9798SRick Macklem gotone = 0;
1008158b9798SRick Macklem orating = 0;
1009158b9798SRick Macklem oexptime = 0;
1010158b9798SRick Macklem while ((dp = readdir(dirp)) != NULL) {
1011158b9798SRick Macklem len = snprintf(namepath, sizeof(namepath), "%s/%s", dirpath,
1012158b9798SRick Macklem dp->d_name);
1013158b9798SRick Macklem if (len < sizeof(namepath) &&
1014bd5012b2SRick Macklem (hostbased_initiator_cred == 0 || strcmp(namepath,
1015bd5012b2SRick Macklem GSSD_CREDENTIAL_CACHE_FILE) != 0) &&
1016158b9798SRick Macklem strstr(dp->d_name, ccfile_substring) != NULL &&
1017158b9798SRick Macklem lstat(namepath, &sb) >= 0 &&
1018158b9798SRick Macklem sb.st_uid == uid &&
1019158b9798SRick Macklem S_ISREG(sb.st_mode)) {
1020158b9798SRick Macklem len = snprintf(namepath, sizeof(namepath), "FILE:%s/%s",
1021158b9798SRick Macklem dirpath, dp->d_name);
1022158b9798SRick Macklem if (len < sizeof(namepath) &&
1023158b9798SRick Macklem is_a_valid_tgt_cache(namepath, uid, &rating,
1024158b9798SRick Macklem &exptime) != 0) {
1025158b9798SRick Macklem if (gotone == 0 || rating > orating ||
1026158b9798SRick Macklem (rating == orating && exptime > oexptime)) {
1027158b9798SRick Macklem orating = rating;
1028158b9798SRick Macklem oexptime = exptime;
1029158b9798SRick Macklem strcpy(retpath, namepath);
1030158b9798SRick Macklem gotone = 1;
1031158b9798SRick Macklem }
1032158b9798SRick Macklem }
1033158b9798SRick Macklem }
1034158b9798SRick Macklem }
1035158b9798SRick Macklem closedir(dirp);
1036158b9798SRick Macklem if (gotone != 0) {
1037158b9798SRick Macklem strcpy(rpath, retpath);
1038158b9798SRick Macklem return (1);
1039158b9798SRick Macklem }
1040158b9798SRick Macklem return (0);
1041158b9798SRick Macklem }
1042158b9798SRick Macklem
1043158b9798SRick Macklem /*
1044158b9798SRick Macklem * Try to determine if the file is a valid tgt cache file.
1045158b9798SRick Macklem * Check that the file has a valid tgt for a principal.
1046158b9798SRick Macklem * If it does, return 1, otherwise return 0.
1047158b9798SRick Macklem * It also returns a "rating" and the expiry time for the TGT, when found.
1048158b9798SRick Macklem * This "rating" is higher based on heuristics that make it more
1049158b9798SRick Macklem * likely to be the correct credential cache file to use. It can
1050158b9798SRick Macklem * be used by the caller, along with expiry time, to select from
1051158b9798SRick Macklem * multiple credential cache files.
1052158b9798SRick Macklem */
1053158b9798SRick Macklem static int
is_a_valid_tgt_cache(const char * filepath,uid_t uid,int * retrating,time_t * retexptime)1054158b9798SRick Macklem is_a_valid_tgt_cache(const char *filepath, uid_t uid, int *retrating,
1055158b9798SRick Macklem time_t *retexptime)
1056158b9798SRick Macklem {
10578ac5ae56SRick Macklem #ifndef WITHOUT_KERBEROS
1058158b9798SRick Macklem krb5_context context;
1059158b9798SRick Macklem krb5_principal princ;
1060158b9798SRick Macklem krb5_ccache ccache;
1061158b9798SRick Macklem krb5_error_code retval;
1062158b9798SRick Macklem krb5_cc_cursor curse;
1063158b9798SRick Macklem krb5_creds krbcred;
1064158b9798SRick Macklem int gotone, orating, rating, ret;
1065158b9798SRick Macklem struct passwd *pw;
1066158b9798SRick Macklem char *cp, *cp2, *pname;
1067158b9798SRick Macklem time_t exptime;
1068158b9798SRick Macklem
1069158b9798SRick Macklem /* Find a likely name for the uid principal. */
1070158b9798SRick Macklem pw = getpwuid(uid);
1071158b9798SRick Macklem
1072158b9798SRick Macklem /*
1073158b9798SRick Macklem * Do a bunch of krb5 library stuff to try and determine if
1074158b9798SRick Macklem * this file is a credentials cache with an appropriate TGT
1075158b9798SRick Macklem * in it.
1076158b9798SRick Macklem */
1077158b9798SRick Macklem retval = krb5_init_context(&context);
1078158b9798SRick Macklem if (retval != 0)
1079158b9798SRick Macklem return (0);
1080158b9798SRick Macklem retval = krb5_cc_resolve(context, filepath, &ccache);
1081158b9798SRick Macklem if (retval != 0) {
1082158b9798SRick Macklem krb5_free_context(context);
1083158b9798SRick Macklem return (0);
1084158b9798SRick Macklem }
1085158b9798SRick Macklem ret = 0;
1086158b9798SRick Macklem orating = 0;
1087158b9798SRick Macklem exptime = 0;
1088158b9798SRick Macklem retval = krb5_cc_start_seq_get(context, ccache, &curse);
1089158b9798SRick Macklem if (retval == 0) {
1090158b9798SRick Macklem while ((retval = krb5_cc_next_cred(context, ccache, &curse,
1091158b9798SRick Macklem &krbcred)) == 0) {
1092158b9798SRick Macklem gotone = 0;
1093158b9798SRick Macklem rating = 0;
1094158b9798SRick Macklem retval = krb5_unparse_name(context, krbcred.server,
1095158b9798SRick Macklem &pname);
1096158b9798SRick Macklem if (retval == 0) {
1097158b9798SRick Macklem cp = strchr(pname, '/');
1098158b9798SRick Macklem if (cp != NULL) {
1099158b9798SRick Macklem *cp++ = '\0';
1100158b9798SRick Macklem if (strcmp(pname, "krbtgt") == 0 &&
1101158b9798SRick Macklem krbcred.times.endtime > time(NULL)
1102158b9798SRick Macklem ) {
1103158b9798SRick Macklem gotone = 1;
1104158b9798SRick Macklem /*
1105158b9798SRick Macklem * Test to see if this is a
1106158b9798SRick Macklem * tgt for cross-realm auth.
1107158b9798SRick Macklem * Rate it higher, if it is not.
1108158b9798SRick Macklem */
1109158b9798SRick Macklem cp2 = strchr(cp, '@');
1110158b9798SRick Macklem if (cp2 != NULL) {
1111158b9798SRick Macklem *cp2++ = '\0';
1112158b9798SRick Macklem if (strcmp(cp, cp2) ==
1113158b9798SRick Macklem 0)
1114158b9798SRick Macklem rating++;
1115158b9798SRick Macklem }
1116158b9798SRick Macklem }
1117158b9798SRick Macklem }
1118158b9798SRick Macklem free(pname);
1119158b9798SRick Macklem }
1120158b9798SRick Macklem if (gotone != 0) {
1121158b9798SRick Macklem retval = krb5_unparse_name(context,
1122158b9798SRick Macklem krbcred.client, &pname);
1123158b9798SRick Macklem if (retval == 0) {
1124158b9798SRick Macklem cp = strchr(pname, '@');
1125158b9798SRick Macklem if (cp != NULL) {
1126158b9798SRick Macklem *cp++ = '\0';
1127158b9798SRick Macklem if (pw != NULL && strcmp(pname,
1128158b9798SRick Macklem pw->pw_name) == 0)
1129158b9798SRick Macklem rating++;
1130158b9798SRick Macklem if (strchr(pname, '/') == NULL)
1131158b9798SRick Macklem rating++;
1132158b9798SRick Macklem if (pref_realm[0] != '\0' &&
1133158b9798SRick Macklem strcmp(cp, pref_realm) == 0)
1134158b9798SRick Macklem rating++;
1135158b9798SRick Macklem }
1136158b9798SRick Macklem }
1137158b9798SRick Macklem free(pname);
1138158b9798SRick Macklem if (rating > orating) {
1139158b9798SRick Macklem orating = rating;
1140158b9798SRick Macklem exptime = krbcred.times.endtime;
1141158b9798SRick Macklem } else if (rating == orating &&
1142158b9798SRick Macklem krbcred.times.endtime > exptime)
1143158b9798SRick Macklem exptime = krbcred.times.endtime;
1144158b9798SRick Macklem ret = 1;
1145158b9798SRick Macklem }
1146158b9798SRick Macklem krb5_free_cred_contents(context, &krbcred);
1147158b9798SRick Macklem }
1148158b9798SRick Macklem krb5_cc_end_seq_get(context, ccache, &curse);
1149158b9798SRick Macklem }
1150158b9798SRick Macklem krb5_cc_close(context, ccache);
1151158b9798SRick Macklem krb5_free_context(context);
1152158b9798SRick Macklem if (ret != 0) {
1153158b9798SRick Macklem *retrating = orating;
1154158b9798SRick Macklem *retexptime = exptime;
1155158b9798SRick Macklem }
1156158b9798SRick Macklem return (ret);
11578ac5ae56SRick Macklem #else /* WITHOUT_KERBEROS */
11588ac5ae56SRick Macklem return (0);
11598ac5ae56SRick Macklem #endif /* !WITHOUT_KERBEROS */
1160158b9798SRick Macklem }
1161158b9798SRick Macklem
1162c3234382SRick Macklem #ifndef WITHOUT_KERBEROS
1163c3234382SRick Macklem /*
1164bd5012b2SRick Macklem * This function attempts to do essentially a "kinit -k" for the principal
1165bd5012b2SRick Macklem * name provided as the argument, so that there will be a TGT in the
1166bd5012b2SRick Macklem * credential cache.
1167bd5012b2SRick Macklem */
1168bd5012b2SRick Macklem static krb5_error_code
gssd_get_cc_from_keytab(const char * name)1169bd5012b2SRick Macklem gssd_get_cc_from_keytab(const char *name)
1170bd5012b2SRick Macklem {
1171bd5012b2SRick Macklem krb5_error_code ret, opt_ret, princ_ret, cc_ret, kt_ret, cred_ret;
1172bd5012b2SRick Macklem krb5_context context;
1173bd5012b2SRick Macklem krb5_principal principal;
1174bd5012b2SRick Macklem krb5_keytab kt;
1175bd5012b2SRick Macklem krb5_creds cred;
1176bd5012b2SRick Macklem krb5_get_init_creds_opt *opt;
1177bd5012b2SRick Macklem krb5_deltat start_time = 0;
1178bd5012b2SRick Macklem krb5_ccache ccache;
1179bd5012b2SRick Macklem
1180bd5012b2SRick Macklem ret = krb5_init_context(&context);
1181bd5012b2SRick Macklem if (ret != 0)
1182bd5012b2SRick Macklem return (ret);
1183992e6590SRick Macklem opt_ret = cc_ret = kt_ret = cred_ret = 1; /* anything non-zero */
1184bd5012b2SRick Macklem princ_ret = ret = krb5_parse_name(context, name, &principal);
1185bd5012b2SRick Macklem if (ret == 0)
1186bd5012b2SRick Macklem opt_ret = ret = krb5_get_init_creds_opt_alloc(context, &opt);
1187bd5012b2SRick Macklem if (ret == 0)
1188bd5012b2SRick Macklem cc_ret = ret = krb5_cc_default(context, &ccache);
1189bd5012b2SRick Macklem if (ret == 0)
1190bd5012b2SRick Macklem ret = krb5_cc_initialize(context, ccache, principal);
1191bd5012b2SRick Macklem if (ret == 0) {
1192bd5012b2SRick Macklem krb5_get_init_creds_opt_set_default_flags(context, "gssd",
1193bd5012b2SRick Macklem krb5_principal_get_realm(context, principal), opt);
1194bd5012b2SRick Macklem kt_ret = ret = krb5_kt_default(context, &kt);
1195bd5012b2SRick Macklem }
1196bd5012b2SRick Macklem if (ret == 0)
1197bd5012b2SRick Macklem cred_ret = ret = krb5_get_init_creds_keytab(context, &cred,
1198bd5012b2SRick Macklem principal, kt, start_time, NULL, opt);
1199bd5012b2SRick Macklem if (ret == 0)
1200bd5012b2SRick Macklem ret = krb5_cc_store_cred(context, ccache, &cred);
1201bd5012b2SRick Macklem if (kt_ret == 0)
1202bd5012b2SRick Macklem krb5_kt_close(context, kt);
1203bd5012b2SRick Macklem if (cc_ret == 0)
1204bd5012b2SRick Macklem krb5_cc_close(context, ccache);
1205bd5012b2SRick Macklem if (opt_ret == 0)
1206bd5012b2SRick Macklem krb5_get_init_creds_opt_free(context, opt);
1207bd5012b2SRick Macklem if (princ_ret == 0)
1208bd5012b2SRick Macklem krb5_free_principal(context, principal);
1209bd5012b2SRick Macklem if (cred_ret == 0)
1210bd5012b2SRick Macklem krb5_free_cred_contents(context, &cred);
1211bd5012b2SRick Macklem krb5_free_context(context);
1212bd5012b2SRick Macklem return (ret);
1213bd5012b2SRick Macklem }
1214bd5012b2SRick Macklem
1215bd5012b2SRick Macklem /*
1216c3234382SRick Macklem * Acquire a gss credential for a uid.
1217c3234382SRick Macklem */
1218c3234382SRick Macklem static OM_uint32
gssd_get_user_cred(OM_uint32 * min_statp,uid_t uid,gss_cred_id_t * credp)1219c3234382SRick Macklem gssd_get_user_cred(OM_uint32 *min_statp, uid_t uid, gss_cred_id_t *credp)
1220c3234382SRick Macklem {
1221c3234382SRick Macklem gss_buffer_desc principal_desc;
1222c3234382SRick Macklem gss_name_t name;
1223c3234382SRick Macklem OM_uint32 maj_stat, min_stat;
1224c3234382SRick Macklem gss_OID_set mechlist;
1225c3234382SRick Macklem struct passwd *pw;
1226c3234382SRick Macklem
1227c3234382SRick Macklem pw = getpwuid(uid);
1228c3234382SRick Macklem if (pw == NULL) {
1229c3234382SRick Macklem *min_statp = 0;
1230c3234382SRick Macklem return (GSS_S_FAILURE);
1231c3234382SRick Macklem }
1232c3234382SRick Macklem
1233c3234382SRick Macklem /*
1234c3234382SRick Macklem * The mechanism must be set to KerberosV for acquisition
1235c3234382SRick Macklem * of credentials to work reliably.
1236c3234382SRick Macklem */
1237c3234382SRick Macklem maj_stat = gss_create_empty_oid_set(min_statp, &mechlist);
1238c3234382SRick Macklem if (maj_stat != GSS_S_COMPLETE)
1239c3234382SRick Macklem return (maj_stat);
1240c3234382SRick Macklem maj_stat = gss_add_oid_set_member(min_statp, GSS_KRB5_MECH_OID_X,
1241c3234382SRick Macklem &mechlist);
1242c3234382SRick Macklem if (maj_stat != GSS_S_COMPLETE) {
1243c3234382SRick Macklem gss_release_oid_set(&min_stat, &mechlist);
1244c3234382SRick Macklem return (maj_stat);
1245c3234382SRick Macklem }
1246c3234382SRick Macklem
1247c3234382SRick Macklem principal_desc.value = (void *)pw->pw_name;
1248c3234382SRick Macklem principal_desc.length = strlen(pw->pw_name);
1249c3234382SRick Macklem maj_stat = gss_import_name(min_statp, &principal_desc,
1250c3234382SRick Macklem GSS_C_NT_USER_NAME, &name);
1251c3234382SRick Macklem if (maj_stat != GSS_S_COMPLETE) {
1252c3234382SRick Macklem gss_release_oid_set(&min_stat, &mechlist);
1253c3234382SRick Macklem return (maj_stat);
1254c3234382SRick Macklem }
1255c3234382SRick Macklem /* Acquire the credentials. */
1256c3234382SRick Macklem maj_stat = gss_acquire_cred(min_statp, name, 0, mechlist,
1257c3234382SRick Macklem GSS_C_INITIATE, credp, NULL, NULL);
1258c3234382SRick Macklem gss_release_name(&min_stat, &name);
1259c3234382SRick Macklem gss_release_oid_set(&min_stat, &mechlist);
1260c3234382SRick Macklem return (maj_stat);
1261c3234382SRick Macklem }
1262c3234382SRick Macklem #endif /* !WITHOUT_KERBEROS */
1263bd5012b2SRick Macklem
gssd_terminate(int sig __unused)1264bd5012b2SRick Macklem void gssd_terminate(int sig __unused)
1265bd5012b2SRick Macklem {
1266bd5012b2SRick Macklem
1267bd5012b2SRick Macklem #ifndef WITHOUT_KERBEROS
1268bd5012b2SRick Macklem if (hostbased_initiator_cred != 0)
1269bd5012b2SRick Macklem unlink(GSSD_CREDENTIAL_CACHE_FILE);
1270bd5012b2SRick Macklem #endif
1271bd5012b2SRick Macklem exit(0);
1272bd5012b2SRick Macklem }
1273bd5012b2SRick Macklem
1274