17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
228ffff9fdSgt  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*15c07adcSJohn Levon /*
27*15c07adcSJohn Levon  * Copyright (c) 2018, Joyent, Inc.
28*15c07adcSJohn Levon  */
29*15c07adcSJohn Levon 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * $Header:
347c478bd9Sstevel@tonic-gate  * /afs/gza.com/product/secure/rel-eng/src/1.1/rpc/RCS/auth_gssapi.c,v
357c478bd9Sstevel@tonic-gate  * 1.14 1995/03/22 22:07:55 jik Exp $
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include  <sys/systm.h>
397c478bd9Sstevel@tonic-gate #include  <sys/types.h>
407c478bd9Sstevel@tonic-gate #include  <gssapi/gssapi.h>
417c478bd9Sstevel@tonic-gate #include  <rpc/rpc.h>
427c478bd9Sstevel@tonic-gate #include  <rpc/rpcsec_defs.h>
437c478bd9Sstevel@tonic-gate #include  <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include  <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include  <sys/ddi.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static	void	rpc_gss_nextverf();
487c478bd9Sstevel@tonic-gate static	bool_t	rpc_gss_marshall();
497c478bd9Sstevel@tonic-gate static	bool_t	rpc_gss_validate();
507c478bd9Sstevel@tonic-gate static	bool_t	rpc_gss_refresh();
517c478bd9Sstevel@tonic-gate static	void	rpc_gss_destroy();
527c478bd9Sstevel@tonic-gate #if 0
537c478bd9Sstevel@tonic-gate static	void	rpc_gss_destroy_pvt();
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate static	void	rpc_gss_free_pvt();
567c478bd9Sstevel@tonic-gate static	int	rpc_gss_seccreate_pvt();
577c478bd9Sstevel@tonic-gate static  bool_t	rpc_gss_wrap();
587c478bd9Sstevel@tonic-gate static  bool_t	rpc_gss_unwrap();
597c478bd9Sstevel@tonic-gate static	bool_t	validate_seqwin();
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #ifdef	DEBUG
637c478bd9Sstevel@tonic-gate #include <sys/promif.h>
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static struct auth_ops rpc_gss_ops = {
677c478bd9Sstevel@tonic-gate 	rpc_gss_nextverf,
687c478bd9Sstevel@tonic-gate 	rpc_gss_marshall,
697c478bd9Sstevel@tonic-gate 	rpc_gss_validate,
707c478bd9Sstevel@tonic-gate 	rpc_gss_refresh,
717c478bd9Sstevel@tonic-gate 	rpc_gss_destroy,
727c478bd9Sstevel@tonic-gate 	rpc_gss_wrap,
737c478bd9Sstevel@tonic-gate 	rpc_gss_unwrap,
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Private data for RPCSEC_GSS.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate typedef struct _rpc_gss_data {
807c478bd9Sstevel@tonic-gate 	bool_t		established;	/* TRUE when established */
817c478bd9Sstevel@tonic-gate 	CLIENT		*clnt;		/* associated client handle */
827c478bd9Sstevel@tonic-gate 	int		version;	/* RPCSEC version */
837c478bd9Sstevel@tonic-gate 	gss_ctx_id_t	context;	/* GSS context id */
847c478bd9Sstevel@tonic-gate 	gss_buffer_desc	ctx_handle;	/* RPCSEC GSS context handle */
857c478bd9Sstevel@tonic-gate 	uint_t		seq_num;	/* last sequence number rcvd */
867c478bd9Sstevel@tonic-gate 	gss_cred_id_t	my_cred;	/* caller's GSS credentials */
877c478bd9Sstevel@tonic-gate 	OM_uint32	qop;		/* requested QOP */
887c478bd9Sstevel@tonic-gate 	rpc_gss_service_t	service;	/* requested service */
897c478bd9Sstevel@tonic-gate 	uint_t		gss_proc;	/* GSS control procedure */
907c478bd9Sstevel@tonic-gate 	gss_name_t	target_name;	/* target server */
917c478bd9Sstevel@tonic-gate 	int		req_flags;	/* GSS request bits */
927c478bd9Sstevel@tonic-gate 	gss_OID		mech_type;	/* GSS mechanism */
937c478bd9Sstevel@tonic-gate 	OM_uint32	time_req;	/* requested cred lifetime */
947c478bd9Sstevel@tonic-gate 	bool_t		invalid;	/* can't use this any more */
957c478bd9Sstevel@tonic-gate 	OM_uint32	seq_window;	/* server sequence window */
967c478bd9Sstevel@tonic-gate 	struct opaque_auth *verifier;	/* rpc reply verifier saved for */
977c478bd9Sstevel@tonic-gate 					/* validating the sequence window */
987c478bd9Sstevel@tonic-gate 	gss_channel_bindings_t	icb;
997c478bd9Sstevel@tonic-gate } rpc_gss_data;
1007c478bd9Sstevel@tonic-gate #define	AUTH_PRIVATE(auth)	((rpc_gss_data *)auth->ah_private)
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	INTERRUPT_OK	1	/* allow interrupt */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  *  RPCSEC_GSS auth cache definitions.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* The table size must be a power of two. */
1097c478bd9Sstevel@tonic-gate #define	GSSAUTH_TABLESIZE 16
1107c478bd9Sstevel@tonic-gate #define	HASH(keynum, uid_num) \
111f48205beScasper 	((((intptr_t)(keynum)) ^ ((int)uid_num)) & (GSSAUTH_TABLESIZE - 1))
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * gss auth cache entry.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate typedef struct ga_cache_entry {
1177c478bd9Sstevel@tonic-gate 	void	*cache_key;
1187c478bd9Sstevel@tonic-gate 	uid_t	uid;
1197c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
1207c478bd9Sstevel@tonic-gate 	bool_t	in_use;
1217c478bd9Sstevel@tonic-gate 	time_t	ref_time; /* the time referenced previously */
1227c478bd9Sstevel@tonic-gate 	time_t	ctx_expired_time; /* when the context will be expired */
1237c478bd9Sstevel@tonic-gate 	AUTH	*auth;
1247c478bd9Sstevel@tonic-gate 	struct ga_cache_entry *next;
1257c478bd9Sstevel@tonic-gate } *ga_cache_list;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate struct ga_cache_entry	*ga_cache_table[GSSAUTH_TABLESIZE];
1287c478bd9Sstevel@tonic-gate static krwlock_t	ga_cache_table_lock;
1297c478bd9Sstevel@tonic-gate static struct kmem_cache *ga_cache_handle;
1307c478bd9Sstevel@tonic-gate static void gssauth_cache_reclaim(void *);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void gssauth_zone_fini(zoneid_t, void *);
1337c478bd9Sstevel@tonic-gate static zone_key_t	gssauth_zone_key;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int ga_cache_hit;
1367c478bd9Sstevel@tonic-gate int ga_cache_miss;
1377c478bd9Sstevel@tonic-gate int ga_cache_reclaim;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	NOT_DEAD(ptr)	ASSERT((((intptr_t)(ptr)) != 0xdeadbeef))
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
gssauth_init(void)1427c478bd9Sstevel@tonic-gate gssauth_init(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Initialize gss auth cache table lock
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	rw_init(&ga_cache_table_lock, NULL, RW_DEFAULT, NULL);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Allocate gss auth cache handle
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	ga_cache_handle = kmem_cache_create("ga_cache_handle",
1538ffff9fdSgt 	    sizeof (struct ga_cache_entry), 0, NULL, NULL,
1548ffff9fdSgt 	    gssauth_cache_reclaim, NULL, NULL, 0);
1557c478bd9Sstevel@tonic-gate 	zone_key_create(&gssauth_zone_key, NULL, NULL, gssauth_zone_fini);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Destroy the structures previously initialized in gssauth_init()
1607c478bd9Sstevel@tonic-gate  * This routine is called by _init() if mod_install() failed.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate void
gssauth_fini(void)1637c478bd9Sstevel@tonic-gate gssauth_fini(void)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(gssauth_zone_key);
1667c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(ga_cache_handle);
1677c478bd9Sstevel@tonic-gate 	rw_destroy(&ga_cache_table_lock);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * This is a cleanup routine to release cached entries when a zone is being
1727c478bd9Sstevel@tonic-gate  * destroyed.  The code is also used when kmem calls us to free up memory, at
1737c478bd9Sstevel@tonic-gate  * which point ``zoneid'' will be ALL_ZONES.  We don't honor the cache timeout
1747c478bd9Sstevel@tonic-gate  * when the zone is going away, since the zoneid (and all associated cached
1757c478bd9Sstevel@tonic-gate  * entries) are invalid.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate time_t rpc_gss_cache_time = 60 * 60;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /* ARGSUSED */
1807c478bd9Sstevel@tonic-gate static void
gssauth_zone_fini(zoneid_t zoneid,void * unused)1817c478bd9Sstevel@tonic-gate gssauth_zone_fini(zoneid_t zoneid, void *unused)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	struct ga_cache_entry *p, *prev, *next;
1847c478bd9Sstevel@tonic-gate 	int i;
1857c478bd9Sstevel@tonic-gate 	time_t now;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
1907c478bd9Sstevel@tonic-gate 		prev = NULL;
1917c478bd9Sstevel@tonic-gate 		for (p = ga_cache_table[i]; p; p = next) {
1927c478bd9Sstevel@tonic-gate 			NOT_DEAD(p->next);
1937c478bd9Sstevel@tonic-gate 			next = p->next;
1947c478bd9Sstevel@tonic-gate 			NOT_DEAD(next);
1957c478bd9Sstevel@tonic-gate 			if (zoneid == ALL_ZONES) {	/* kmem callback */
1967c478bd9Sstevel@tonic-gate 				/*
1977c478bd9Sstevel@tonic-gate 				 * Free entries that have not been
1987c478bd9Sstevel@tonic-gate 				 * used for rpc_gss_cache_time seconds.
1997c478bd9Sstevel@tonic-gate 				 */
2007c478bd9Sstevel@tonic-gate 				now = gethrestime_sec();
2017c478bd9Sstevel@tonic-gate 				if ((p->ref_time + rpc_gss_cache_time >
2028ffff9fdSgt 				    now) || p->in_use) {
2037c478bd9Sstevel@tonic-gate 					if ((p->ref_time + rpc_gss_cache_time <=
2048ffff9fdSgt 					    now) && p->in_use) {
2057c478bd9Sstevel@tonic-gate 						RPCGSS_LOG0(2, "gssauth_cache_"
2067c478bd9Sstevel@tonic-gate 						    "reclaim: in_use\n");
2077c478bd9Sstevel@tonic-gate 					}
2087c478bd9Sstevel@tonic-gate 					prev = p;
2097c478bd9Sstevel@tonic-gate 					continue;
2107c478bd9Sstevel@tonic-gate 				}
2117c478bd9Sstevel@tonic-gate 			} else {
2127c478bd9Sstevel@tonic-gate 				if (p->zoneid != zoneid) {
2137c478bd9Sstevel@tonic-gate 					prev = p;
2147c478bd9Sstevel@tonic-gate 					continue;
2157c478bd9Sstevel@tonic-gate 				}
2167c478bd9Sstevel@tonic-gate 				ASSERT(!p->in_use);
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 			RPCGSS_LOG(2, "gssauth_cache_reclaim: destroy auth "
2208ffff9fdSgt 			    "%p\n", (void *)p->auth);
2217c478bd9Sstevel@tonic-gate 			rpc_gss_destroy(p->auth);
2227c478bd9Sstevel@tonic-gate 			kmem_cache_free(ga_cache_handle, (void *)p);
2237c478bd9Sstevel@tonic-gate 			if (prev == NULL) {
2247c478bd9Sstevel@tonic-gate 				ga_cache_table[i] = next;
2257c478bd9Sstevel@tonic-gate 			} else {
2267c478bd9Sstevel@tonic-gate 				NOT_DEAD(prev->next);
2277c478bd9Sstevel@tonic-gate 				prev->next = next;
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * Called by the kernel memory allocator when
2387c478bd9Sstevel@tonic-gate  * memory is low. Free unused cache entries.
2397c478bd9Sstevel@tonic-gate  * If that's not enough, the VM system will
2407c478bd9Sstevel@tonic-gate  * call again for some more.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2437c478bd9Sstevel@tonic-gate static void
gssauth_cache_reclaim(void * cdrarg)2447c478bd9Sstevel@tonic-gate gssauth_cache_reclaim(void *cdrarg)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	gssauth_zone_fini(ALL_ZONES, NULL);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate #define	NOT_NULL(ptr)	ASSERT(ptr)
2507c478bd9Sstevel@tonic-gate #define	IS_ALIGNED(ptr)	ASSERT((((intptr_t)(ptr)) & 3) == 0)
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  *  Get the client gss security service handle.
2547c478bd9Sstevel@tonic-gate  *  If it is in the cache table, get it, otherwise, create
2557c478bd9Sstevel@tonic-gate  *  a new one by calling rpc_gss_seccreate().
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate int
rpc_gss_secget(CLIENT * clnt,char * principal,rpc_gss_OID mechanism,rpc_gss_service_t service_type,uint_t qop,rpc_gss_options_req_t * options_req,rpc_gss_options_ret_t * options_ret,void * cache_key,cred_t * cr,AUTH ** retauth)2587c478bd9Sstevel@tonic-gate rpc_gss_secget(CLIENT *clnt,
2597c478bd9Sstevel@tonic-gate 	char	*principal,
2607c478bd9Sstevel@tonic-gate 	rpc_gss_OID	mechanism,
2617c478bd9Sstevel@tonic-gate 	rpc_gss_service_t service_type,
2627c478bd9Sstevel@tonic-gate 	uint_t	qop,
2637c478bd9Sstevel@tonic-gate 	rpc_gss_options_req_t *options_req,
2647c478bd9Sstevel@tonic-gate 	rpc_gss_options_ret_t *options_ret,
2657c478bd9Sstevel@tonic-gate 	void *cache_key,
2667c478bd9Sstevel@tonic-gate 	cred_t *cr,
2677c478bd9Sstevel@tonic-gate 	AUTH **retauth)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	struct ga_cache_entry **head, *current, *new, *prev;
2707c478bd9Sstevel@tonic-gate 	AUTH *auth = NULL;
2717c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap;
2727c478bd9Sstevel@tonic-gate 	rpc_gss_options_ret_t opt_ret;
2737c478bd9Sstevel@tonic-gate 	int status = 0;
2747c478bd9Sstevel@tonic-gate 	uid_t uid = crgetuid(cr);
2757c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (retauth == NULL)
2787c478bd9Sstevel@tonic-gate 		return (EINVAL);
2797c478bd9Sstevel@tonic-gate 	*retauth = NULL;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	NOT_NULL(cr);
2827c478bd9Sstevel@tonic-gate 	IS_ALIGNED(cr);
2837c478bd9Sstevel@tonic-gate #ifdef DEBUG
284*15c07adcSJohn Levon 	if (HASH(cache_key, uid) < 0) {
285*15c07adcSJohn Levon 		prom_printf("cache_key %p, cr %p\n", cache_key, (void *)cr);
286*15c07adcSJohn Levon 	}
2877c478bd9Sstevel@tonic-gate #endif
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 *  Get a valid gss auth handle from the cache table.
2917c478bd9Sstevel@tonic-gate 	 *  If auth in cache is invalid and not in use, destroy it.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	prev = NULL;
2947c478bd9Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	ASSERT(HASH(cache_key, uid) >= 0);
2977c478bd9Sstevel@tonic-gate 	head = &ga_cache_table[HASH(cache_key, uid)];
2987c478bd9Sstevel@tonic-gate 	NOT_NULL(head);
2997c478bd9Sstevel@tonic-gate 	IS_ALIGNED(head);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	for (current = *head; current; current = current->next) {
3027c478bd9Sstevel@tonic-gate 		NOT_NULL(current);
3037c478bd9Sstevel@tonic-gate 		IS_ALIGNED(current);
3047c478bd9Sstevel@tonic-gate 		if ((cache_key == current->cache_key) &&
3057c478bd9Sstevel@tonic-gate 			(uid == current->uid) && (zoneid == current->zoneid) &&
3067c478bd9Sstevel@tonic-gate 			!current->in_use) {
3077c478bd9Sstevel@tonic-gate 			current->in_use = TRUE;
3087c478bd9Sstevel@tonic-gate 			current->ref_time = gethrestime_sec();
3097c478bd9Sstevel@tonic-gate 			ap = AUTH_PRIVATE(current->auth);
3107c478bd9Sstevel@tonic-gate 			ap->clnt = clnt;
3117c478bd9Sstevel@tonic-gate 			ga_cache_hit++;
3127c478bd9Sstevel@tonic-gate 			if (ap->invalid ||
3137c478bd9Sstevel@tonic-gate 			    ((current->ctx_expired_time != GSS_C_INDEFINITE) &&
314dda0f8f7Sgtb 			    (gethrestime_sec() >=
3157c478bd9Sstevel@tonic-gate 			    current->ctx_expired_time))) {
3167c478bd9Sstevel@tonic-gate 			    RPCGSS_LOG0(1, "NOTICE: rpc_gss_secget: time to "
3177c478bd9Sstevel@tonic-gate 					"refresh the auth\n");
3187c478bd9Sstevel@tonic-gate 			    if (prev == NULL) {
3197c478bd9Sstevel@tonic-gate 				*head = current->next;
3207c478bd9Sstevel@tonic-gate 			    } else {
3217c478bd9Sstevel@tonic-gate 				prev->next = current->next;
3227c478bd9Sstevel@tonic-gate 			    }
3237c478bd9Sstevel@tonic-gate 			    rpc_gss_destroy(current->auth);
3247c478bd9Sstevel@tonic-gate 			    kmem_cache_free(ga_cache_handle, (void *) current);
3257c478bd9Sstevel@tonic-gate 			    auth = NULL;
3267c478bd9Sstevel@tonic-gate 			} else {
3277c478bd9Sstevel@tonic-gate 			    auth = current->auth;
3287c478bd9Sstevel@tonic-gate 			}
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		} else {
3317c478bd9Sstevel@tonic-gate 			prev = current;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 *  If no valid gss auth handle can be found in the cache, create
3387c478bd9Sstevel@tonic-gate 	 *  a new one.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	if (!auth) {
3417c478bd9Sstevel@tonic-gate 		ga_cache_miss++;
3427c478bd9Sstevel@tonic-gate 		if (options_ret == NULL)
3437c478bd9Sstevel@tonic-gate 			options_ret = &opt_ret;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		status = rpc_gss_seccreate(clnt, principal, mechanism,
3467c478bd9Sstevel@tonic-gate 			service_type, qop, options_req, options_ret, cr, &auth);
3477c478bd9Sstevel@tonic-gate 		if (status == 0) {
3487c478bd9Sstevel@tonic-gate 			RPCGSS_LOG(2, "rpc_gss_secget: new auth %p\n",
3497c478bd9Sstevel@tonic-gate 					(void *)auth);
3507c478bd9Sstevel@tonic-gate 			new = kmem_cache_alloc(ga_cache_handle, KM_NOSLEEP);
3517c478bd9Sstevel@tonic-gate 			IS_ALIGNED(new);
3527c478bd9Sstevel@tonic-gate 			NOT_DEAD(new);
3537c478bd9Sstevel@tonic-gate 			if (new) {
3547c478bd9Sstevel@tonic-gate 				new->cache_key = cache_key;
3557c478bd9Sstevel@tonic-gate 				new->uid = uid;
3567c478bd9Sstevel@tonic-gate 				new->zoneid = zoneid;
3577c478bd9Sstevel@tonic-gate 				new->in_use = TRUE;
3587c478bd9Sstevel@tonic-gate 				new->ref_time = gethrestime_sec();
3597c478bd9Sstevel@tonic-gate 				if (options_ret->time_ret != GSS_C_INDEFINITE) {
3607c478bd9Sstevel@tonic-gate 				    new->ctx_expired_time = new->ref_time +
3617c478bd9Sstevel@tonic-gate 					options_ret->time_ret;
3627c478bd9Sstevel@tonic-gate 				} else {
3637c478bd9Sstevel@tonic-gate 				    new->ctx_expired_time = GSS_C_INDEFINITE;
3647c478bd9Sstevel@tonic-gate 				}
3657c478bd9Sstevel@tonic-gate 				new->auth = auth;
3667c478bd9Sstevel@tonic-gate 				rw_enter(&ga_cache_table_lock, RW_WRITER);
3677c478bd9Sstevel@tonic-gate 				NOT_DEAD(*head);
3687c478bd9Sstevel@tonic-gate 				NOT_DEAD(new->next);
3697c478bd9Sstevel@tonic-gate 				new->next = *head;
3707c478bd9Sstevel@tonic-gate 				*head = new;
3717c478bd9Sstevel@tonic-gate 				rw_exit(&ga_cache_table_lock);
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 			/* done with opt_ret */
3747c478bd9Sstevel@tonic-gate 			if (options_ret == &opt_ret) {
3757c478bd9Sstevel@tonic-gate 			    kgss_free_oid((gss_OID) opt_ret.actual_mechanism);
3767c478bd9Sstevel@tonic-gate 			}
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	*retauth = auth;
3817c478bd9Sstevel@tonic-gate 	return (status);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate  *  rpc_gss_secfree will destroy a rpcsec_gss context only if
3887c478bd9Sstevel@tonic-gate  *  the auth handle is not in the cache table.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate void
rpc_gss_secfree(AUTH * auth)3917c478bd9Sstevel@tonic-gate rpc_gss_secfree(AUTH *auth)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	struct ga_cache_entry *next, *cur;
3947c478bd9Sstevel@tonic-gate 	int i;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 *  Check the cache table to find the auth.
3987c478bd9Sstevel@tonic-gate 	 *  Marked it unused.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
4017c478bd9Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
4027c478bd9Sstevel@tonic-gate 		for (cur = ga_cache_table[i]; cur; cur = next) {
4037c478bd9Sstevel@tonic-gate 			NOT_DEAD(cur);
4047c478bd9Sstevel@tonic-gate 			next = cur->next;
4057c478bd9Sstevel@tonic-gate 			NOT_DEAD(next);
4067c478bd9Sstevel@tonic-gate 			if (cur->auth == auth) {
4078ffff9fdSgt 				ASSERT(cur->in_use == TRUE);
4088ffff9fdSgt 				cur->in_use = FALSE;
4098ffff9fdSgt 				rw_exit(&ga_cache_table_lock);
4108ffff9fdSgt 				return;
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
4157c478bd9Sstevel@tonic-gate 	RPCGSS_LOG(2, "rpc_gss_secfree: destroy auth %p\n", (void *)auth);
4167c478bd9Sstevel@tonic-gate 	rpc_gss_destroy(auth);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  *  Create a gss security service context.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate int
rpc_gss_seccreate(CLIENT * clnt,char * principal,rpc_gss_OID mechanism,rpc_gss_service_t service_type,uint_t qop,rpc_gss_options_req_t * options_req,rpc_gss_options_ret_t * options_ret,cred_t * cr,AUTH ** retauth)4247c478bd9Sstevel@tonic-gate rpc_gss_seccreate(CLIENT *clnt,
4257c478bd9Sstevel@tonic-gate 	char			*principal,	/* target service@server */
4267c478bd9Sstevel@tonic-gate 	rpc_gss_OID		mechanism,	/* security mechanism */
4277c478bd9Sstevel@tonic-gate 	rpc_gss_service_t	service_type,	/* security service */
4287c478bd9Sstevel@tonic-gate 	uint_t			qop,		/* requested QOP */
4297c478bd9Sstevel@tonic-gate 	rpc_gss_options_req_t	*options_req,	/* requested options */
4307c478bd9Sstevel@tonic-gate 	rpc_gss_options_ret_t	*options_ret,	/* returned options */
4317c478bd9Sstevel@tonic-gate 	cred_t			*cr,		/* client's unix cred */
4327c478bd9Sstevel@tonic-gate 	AUTH			**retauth)	/* auth handle */
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	OM_uint32		gssstat;
4357c478bd9Sstevel@tonic-gate 	OM_uint32		minor_stat;
4367c478bd9Sstevel@tonic-gate 	gss_name_t		target_name;
4377c478bd9Sstevel@tonic-gate 	int			ret_flags;
4387c478bd9Sstevel@tonic-gate 	OM_uint32		time_rec;
4397c478bd9Sstevel@tonic-gate 	gss_buffer_desc		input_name;
4407c478bd9Sstevel@tonic-gate 	AUTH			*auth = NULL;
4417c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = NULL;
4427c478bd9Sstevel@tonic-gate 	int			error;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/*
4457c478bd9Sstevel@tonic-gate 	 * convert name to GSS internal type
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	input_name.value = principal;
4487c478bd9Sstevel@tonic-gate 	input_name.length = strlen(principal);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	gssstat = gss_import_name(&minor_stat, &input_name,
4518ffff9fdSgt 	    (gss_OID)GSS_C_NT_HOSTBASED_SERVICE, &target_name);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (gssstat != GSS_S_COMPLETE) {
4547c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1,
4558ffff9fdSgt 		    "rpc_gss_seccreate: unable to import gss name\n");
4567c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Create AUTH handle.  Save the necessary interface information
4617c478bd9Sstevel@tonic-gate 	 * so that the client can refresh the handle later if needed.
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 	if ((auth = (AUTH *) kmem_alloc(sizeof (*auth), KM_SLEEP)) != NULL)
4647c478bd9Sstevel@tonic-gate 		ap = (rpc_gss_data *) kmem_alloc(sizeof (*ap), KM_SLEEP);
4657c478bd9Sstevel@tonic-gate 	if (auth == NULL || ap == NULL) {
4667c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_seccreate: out of memory\n");
4677c478bd9Sstevel@tonic-gate 		if (auth != NULL)
4687c478bd9Sstevel@tonic-gate 			kmem_free((char *)auth, sizeof (*auth));
4697c478bd9Sstevel@tonic-gate 		(void) gss_release_name(&minor_stat, &target_name);
4707c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	bzero((char *)ap, sizeof (*ap));
4747c478bd9Sstevel@tonic-gate 	ap->clnt = clnt;
4757c478bd9Sstevel@tonic-gate 	ap->version = RPCSEC_GSS_VERSION;
4767c478bd9Sstevel@tonic-gate 	if (options_req != NULL) {
4777c478bd9Sstevel@tonic-gate 		ap->my_cred = options_req->my_cred;
4787c478bd9Sstevel@tonic-gate 		ap->req_flags = options_req->req_flags;
4797c478bd9Sstevel@tonic-gate 		ap->time_req = options_req->time_req;
4807c478bd9Sstevel@tonic-gate 		ap->icb = options_req->input_channel_bindings;
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		ap->my_cred = GSS_C_NO_CREDENTIAL;
4837c478bd9Sstevel@tonic-gate 		ap->req_flags = GSS_C_MUTUAL_FLAG;
4847c478bd9Sstevel@tonic-gate 		ap->time_req = 0;
4857c478bd9Sstevel@tonic-gate 		ap->icb = GSS_C_NO_CHANNEL_BINDINGS;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	if ((ap->service = service_type) == rpc_gss_svc_default)
4887c478bd9Sstevel@tonic-gate 		ap->service = rpc_gss_svc_integrity;
4897c478bd9Sstevel@tonic-gate 	ap->qop = qop;
4907c478bd9Sstevel@tonic-gate 	ap->target_name = target_name;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * Now invoke the real interface that sets up the context from
4947c478bd9Sstevel@tonic-gate 	 * the information stashed away in the private data.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	if (error = rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth, ap,
4978ffff9fdSgt 	    mechanism, &ap->mech_type, &ret_flags, &time_rec, cr, 0)) {
4987c478bd9Sstevel@tonic-gate 		if (ap->target_name) {
4997c478bd9Sstevel@tonic-gate 			(void) gss_release_name(&minor_stat, &ap->target_name);
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 		kmem_free((char *)ap, sizeof (*ap));
5027c478bd9Sstevel@tonic-gate 		kmem_free((char *)auth, sizeof (*auth));
5037c478bd9Sstevel@tonic-gate 		RPCGSS_LOG(1, "rpc_gss_seccreate: init context failed"
5048ffff9fdSgt 		    " errno=%d\n", error);
5057c478bd9Sstevel@tonic-gate 		return (error);
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Make sure that the requested service is supported.  In all
5107c478bd9Sstevel@tonic-gate 	 * cases, integrity service must be available.
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 	if ((ap->service == rpc_gss_svc_privacy &&
5138ffff9fdSgt 	    !(ret_flags & GSS_C_CONF_FLAG)) ||
5148ffff9fdSgt 	    !(ret_flags & GSS_C_INTEG_FLAG)) {
5157c478bd9Sstevel@tonic-gate 		rpc_gss_destroy(auth);
5167c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_seccreate: service not supported\n");
5177c478bd9Sstevel@tonic-gate 		return (EPROTONOSUPPORT);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * return option values if requested
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	if (options_ret != NULL) {
5247c478bd9Sstevel@tonic-gate 		options_ret->major_status = gssstat;
5257c478bd9Sstevel@tonic-gate 		options_ret->minor_status = minor_stat;
5267c478bd9Sstevel@tonic-gate 		options_ret->rpcsec_version = ap->version;
5277c478bd9Sstevel@tonic-gate 		options_ret->ret_flags = ret_flags;
5287c478bd9Sstevel@tonic-gate 		options_ret->time_ret = time_rec;
5297c478bd9Sstevel@tonic-gate 		options_ret->gss_context = ap->context;
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 *  Caller's responsibility to free this.
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 		NOT_NULL(ap->mech_type);
5347c478bd9Sstevel@tonic-gate 		__rpc_gss_dup_oid(ap->mech_type,
5358ffff9fdSgt 		    (gss_OID *)&options_ret->actual_mechanism);
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	*retauth = auth;
5397c478bd9Sstevel@tonic-gate 	return (0);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * Private interface to create a context.  This is the interface
5447c478bd9Sstevel@tonic-gate  * that's invoked when the context has to be refreshed.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate static int
rpc_gss_seccreate_pvt(gssstat,minor_stat,auth,ap,desired_mech_type,actual_mech_type,ret_flags,time_rec,cr,isrefresh)5477c478bd9Sstevel@tonic-gate rpc_gss_seccreate_pvt(gssstat, minor_stat, auth, ap, desired_mech_type,
5487c478bd9Sstevel@tonic-gate 			actual_mech_type, ret_flags, time_rec, cr, isrefresh)
5497c478bd9Sstevel@tonic-gate 	OM_uint32		*gssstat;
5507c478bd9Sstevel@tonic-gate 	OM_uint32		*minor_stat;
5517c478bd9Sstevel@tonic-gate 	AUTH			*auth;
5527c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap;
5537c478bd9Sstevel@tonic-gate 	gss_OID			desired_mech_type;
5547c478bd9Sstevel@tonic-gate 	gss_OID			*actual_mech_type;
5557c478bd9Sstevel@tonic-gate 	int			*ret_flags;
5567c478bd9Sstevel@tonic-gate 	OM_uint32		*time_rec;
5577c478bd9Sstevel@tonic-gate 	cred_t			*cr;
5587c478bd9Sstevel@tonic-gate 	int			isrefresh;
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	CLIENT			*clnt = ap->clnt;
5617c478bd9Sstevel@tonic-gate 	AUTH			*save_auth;
5627c478bd9Sstevel@tonic-gate 	enum clnt_stat		callstat;
5637c478bd9Sstevel@tonic-gate 	rpc_gss_init_arg	call_arg;
5647c478bd9Sstevel@tonic-gate 	rpc_gss_init_res	call_res;
5657c478bd9Sstevel@tonic-gate 	gss_buffer_desc		*input_token_p, input_token, process_token;
5667c478bd9Sstevel@tonic-gate 	int 			free_results = 0;
5677c478bd9Sstevel@tonic-gate 	k_sigset_t		smask;
5687c478bd9Sstevel@tonic-gate 	int			error = 0;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/*
5717c478bd9Sstevel@tonic-gate 	 * (re)initialize AUTH handle and private data.
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	bzero((char *)auth, sizeof (*auth));
5747c478bd9Sstevel@tonic-gate 	auth->ah_ops = &rpc_gss_ops;
5757c478bd9Sstevel@tonic-gate 	auth->ah_private = (caddr_t)ap;
5767c478bd9Sstevel@tonic-gate 	auth->ah_cred.oa_flavor = RPCSEC_GSS;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	ap->established = FALSE;
5797c478bd9Sstevel@tonic-gate 	ap->ctx_handle.length = 0;
5807c478bd9Sstevel@tonic-gate 	ap->ctx_handle.value = NULL;
5817c478bd9Sstevel@tonic-gate 	ap->context = NULL;
5827c478bd9Sstevel@tonic-gate 	ap->seq_num = 0;
5837c478bd9Sstevel@tonic-gate 	ap->gss_proc = RPCSEC_GSS_INIT;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * should not change clnt->cl_auth at this time, so save
5877c478bd9Sstevel@tonic-gate 	 * old handle
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 	save_auth = clnt->cl_auth;
5907c478bd9Sstevel@tonic-gate 	clnt->cl_auth = auth;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * set state for starting context setup
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	bzero((char *)&call_arg, sizeof (call_arg));
5967c478bd9Sstevel@tonic-gate 	input_token_p = GSS_C_NO_BUFFER;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate next_token:
5997c478bd9Sstevel@tonic-gate 	*gssstat = kgss_init_sec_context(minor_stat,
6007c478bd9Sstevel@tonic-gate 					ap->my_cred,
6017c478bd9Sstevel@tonic-gate 					&ap->context,
6027c478bd9Sstevel@tonic-gate 					ap->target_name,
6037c478bd9Sstevel@tonic-gate 					desired_mech_type,
6047c478bd9Sstevel@tonic-gate 					ap->req_flags,
6057c478bd9Sstevel@tonic-gate 					ap->time_req,
6067c478bd9Sstevel@tonic-gate 					NULL,
6077c478bd9Sstevel@tonic-gate 					input_token_p,
6087c478bd9Sstevel@tonic-gate 					actual_mech_type,
6097c478bd9Sstevel@tonic-gate 					&call_arg,
6107c478bd9Sstevel@tonic-gate 					ret_flags,
6117c478bd9Sstevel@tonic-gate 					time_rec,
6127c478bd9Sstevel@tonic-gate 					crgetuid(cr));
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (input_token_p != GSS_C_NO_BUFFER) {
6157c478bd9Sstevel@tonic-gate 		OM_uint32 minor_stat2;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat2, input_token_p);
6187c478bd9Sstevel@tonic-gate 		input_token_p = GSS_C_NO_BUFFER;
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (*gssstat != GSS_S_COMPLETE && *gssstat != GSS_S_CONTINUE_NEEDED) {
6227c478bd9Sstevel@tonic-gate 		rpc_gss_display_status(*gssstat, *minor_stat,
6237c478bd9Sstevel@tonic-gate 			desired_mech_type, crgetuid(cr),
6247c478bd9Sstevel@tonic-gate 			"rpcsec_gss_secreate_pvt:gss_init_sec_context");
6257c478bd9Sstevel@tonic-gate 		error = EACCES;
6267c478bd9Sstevel@tonic-gate 		goto cleanup;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * if we got a token, pass it on
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	if (call_arg.length != 0) {
6337c478bd9Sstevel@tonic-gate 		struct timeval timeout = {30, 0};
6347c478bd9Sstevel@tonic-gate 		int	 rpcsec_retry = isrefresh ?
6357c478bd9Sstevel@tonic-gate 			RPCSEC_GSS_REFRESH_ATTEMPTS : 1;
6367c478bd9Sstevel@tonic-gate 		uint32_t oldxid;
6377c478bd9Sstevel@tonic-gate 		uint32_t zeroxid = 0;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		bzero((char *)&call_res, sizeof (call_res));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(clnt, CLGET_XID, (char *)&oldxid);
6427c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(clnt, CLSET_XID, (char *)&zeroxid);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		while (rpcsec_retry > 0) {
6467c478bd9Sstevel@tonic-gate 			struct rpc_err rpcerr;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 			sigintr(&smask, INTERRUPT_OK);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 			callstat = clnt_call(clnt, NULLPROC,
6517c478bd9Sstevel@tonic-gate 				__xdr_rpc_gss_init_arg, (caddr_t)&call_arg,
6527c478bd9Sstevel@tonic-gate 				__xdr_rpc_gss_init_res, (caddr_t)&call_res,
6537c478bd9Sstevel@tonic-gate 				timeout);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 			sigunintr(&smask);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			if (callstat == RPC_SUCCESS) {
6587c478bd9Sstevel@tonic-gate 				error = 0;
6597c478bd9Sstevel@tonic-gate 				if (isrefresh &&
6607c478bd9Sstevel@tonic-gate 				    call_res.gss_major == GSS_S_FAILURE) {
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 					clock_t one_sec = drv_usectohz(1000000);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 					rpcsec_retry--;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 					/*
6677c478bd9Sstevel@tonic-gate 					 * Pause a little and try again.
6687c478bd9Sstevel@tonic-gate 					 */
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 					if (clnt->cl_nosignal == TRUE) {
6717c478bd9Sstevel@tonic-gate 						delay(one_sec);
6727c478bd9Sstevel@tonic-gate 					} else {
6737c478bd9Sstevel@tonic-gate 						if (delay_sig(one_sec)) {
6747c478bd9Sstevel@tonic-gate 							error = EINTR;
6757c478bd9Sstevel@tonic-gate 							break;
6767c478bd9Sstevel@tonic-gate 						}
6777c478bd9Sstevel@tonic-gate 					}
6787c478bd9Sstevel@tonic-gate 					continue;
6797c478bd9Sstevel@tonic-gate 				}
6807c478bd9Sstevel@tonic-gate 				break;
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 			if (callstat == RPC_TIMEDOUT) {
6847c478bd9Sstevel@tonic-gate 				error = ETIMEDOUT;
6857c478bd9Sstevel@tonic-gate 				break;
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 			if (callstat == RPC_XPRTFAILED) {
6897c478bd9Sstevel@tonic-gate 				error = ECONNRESET;
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 			if (callstat == RPC_INTR) {
6947c478bd9Sstevel@tonic-gate 				error = EINTR;
6957c478bd9Sstevel@tonic-gate 				break;
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			if (callstat == RPC_INPROGRESS) {
6997c478bd9Sstevel@tonic-gate 				continue;
7007c478bd9Sstevel@tonic-gate 			}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 			clnt_geterr(clnt, &rpcerr);
7037c478bd9Sstevel@tonic-gate 			error = rpcerr.re_errno;
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(clnt, CLSET_XID, (char *)&oldxid);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &call_arg);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		if (callstat != RPC_SUCCESS) {
7127c478bd9Sstevel@tonic-gate 			RPCGSS_LOG(1,
7137c478bd9Sstevel@tonic-gate 			    "rpc_gss_seccreate_pvt: clnt_call failed %d\n",
7147c478bd9Sstevel@tonic-gate 			    callstat);
7157c478bd9Sstevel@tonic-gate 			goto cleanup;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		/*
7197c478bd9Sstevel@tonic-gate 		 * we have results - note that these need to be freed
7207c478bd9Sstevel@tonic-gate 		 */
7217c478bd9Sstevel@tonic-gate 		free_results = 1;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		if ((call_res.gss_major != GSS_S_COMPLETE) &&
7247c478bd9Sstevel@tonic-gate 		    (call_res.gss_major != GSS_S_CONTINUE_NEEDED)) {
7257c478bd9Sstevel@tonic-gate 			RPCGSS_LOG1(1, "rpc_gss_seccreate_pvt: "
7267c478bd9Sstevel@tonic-gate 				"call_res gss_major %x, gss_minor %x\n",
7277c478bd9Sstevel@tonic-gate 				call_res.gss_major, call_res.gss_minor);
7287c478bd9Sstevel@tonic-gate 			error = EACCES;
7297c478bd9Sstevel@tonic-gate 			goto cleanup;
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		ap->gss_proc = RPCSEC_GSS_CONTINUE_INIT;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/*
7357c478bd9Sstevel@tonic-gate 		 * check for ctx_handle
7367c478bd9Sstevel@tonic-gate 		 */
7377c478bd9Sstevel@tonic-gate 		if (ap->ctx_handle.length == 0) {
7387c478bd9Sstevel@tonic-gate 			if (call_res.ctx_handle.length == 0) {
7397c478bd9Sstevel@tonic-gate 				RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt: zero "
7407c478bd9Sstevel@tonic-gate 					"length handle in response\n");
7417c478bd9Sstevel@tonic-gate 				error = EACCES;
7427c478bd9Sstevel@tonic-gate 				goto cleanup;
7437c478bd9Sstevel@tonic-gate 			}
7447c478bd9Sstevel@tonic-gate 			GSS_DUP_BUFFER(ap->ctx_handle,
7457c478bd9Sstevel@tonic-gate 					call_res.ctx_handle);
7467c478bd9Sstevel@tonic-gate 		} else if (!GSS_BUFFERS_EQUAL(ap->ctx_handle,
7477c478bd9Sstevel@tonic-gate 						call_res.ctx_handle)) {
7487c478bd9Sstevel@tonic-gate 			RPCGSS_LOG0(1,
7497c478bd9Sstevel@tonic-gate 			"rpc_gss_seccreate_pvt: ctx_handle not the same\n");
7507c478bd9Sstevel@tonic-gate 			error = EACCES;
7517c478bd9Sstevel@tonic-gate 			goto cleanup;
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		/*
7557c478bd9Sstevel@tonic-gate 		 * check for token
7567c478bd9Sstevel@tonic-gate 		 */
7577c478bd9Sstevel@tonic-gate 		if (call_res.token.length != 0) {
7587c478bd9Sstevel@tonic-gate 			if (*gssstat == GSS_S_COMPLETE) {
7597c478bd9Sstevel@tonic-gate 				RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt: non "
7607c478bd9Sstevel@tonic-gate 					"zero length token in response, but "
7617c478bd9Sstevel@tonic-gate 					"gsstat == GSS_S_COMPLETE\n");
7627c478bd9Sstevel@tonic-gate 				error = EACCES;
7637c478bd9Sstevel@tonic-gate 				goto cleanup;
7647c478bd9Sstevel@tonic-gate 			}
7657c478bd9Sstevel@tonic-gate 			GSS_DUP_BUFFER(input_token, call_res.token);
7667c478bd9Sstevel@tonic-gate 			input_token_p = &input_token;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		} else if (*gssstat != GSS_S_COMPLETE) {
7697c478bd9Sstevel@tonic-gate 			RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt:zero length "
7707c478bd9Sstevel@tonic-gate 				"token in response, but "
7717c478bd9Sstevel@tonic-gate 				"gsstat != GSS_S_COMPLETE\n");
7727c478bd9Sstevel@tonic-gate 			error = EACCES;
7737c478bd9Sstevel@tonic-gate 			goto cleanup;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		/* save the sequence window value; validate later */
7777c478bd9Sstevel@tonic-gate 		ap->seq_window = call_res.seq_window;
7787c478bd9Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res);
7797c478bd9Sstevel@tonic-gate 		free_results = 0;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/*
7837c478bd9Sstevel@tonic-gate 	 * results were okay.. continue if necessary
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	if (*gssstat == GSS_S_CONTINUE_NEEDED) {
7867c478bd9Sstevel@tonic-gate 		goto next_token;
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * Context is established. Now use kgss_export_sec_context and
7917c478bd9Sstevel@tonic-gate 	 * kgss_import_sec_context to transfer the context from the user
7927c478bd9Sstevel@tonic-gate 	 * land to kernel if the mechanism specific kernel module is
7937c478bd9Sstevel@tonic-gate 	 * available.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 	*gssstat  = kgss_export_sec_context(minor_stat, ap->context,
7967c478bd9Sstevel@tonic-gate 						&process_token);
7977c478bd9Sstevel@tonic-gate 	if (*gssstat == GSS_S_NAME_NOT_MN) {
7987c478bd9Sstevel@tonic-gate 		RPCGSS_LOG(2, "rpc_gss_seccreate_pvt: export_sec_context "
7997c478bd9Sstevel@tonic-gate 			"Kernel Module unavailable  gssstat = 0x%x\n",
8007c478bd9Sstevel@tonic-gate 			*gssstat);
8017c478bd9Sstevel@tonic-gate 		goto done;
8027c478bd9Sstevel@tonic-gate 	} else if (*gssstat != GSS_S_COMPLETE) {
8037c478bd9Sstevel@tonic-gate 		(void) rpc_gss_display_status(*gssstat, *minor_stat,
804fd08154eSrg 			isrefresh ? GSS_C_NULL_OID : *actual_mech_type,
805fd08154eSrg 					crgetuid(cr),
8067c478bd9Sstevel@tonic-gate 			"rpcsec_gss_secreate_pvt:gss_export_sec_context");
8077c478bd9Sstevel@tonic-gate 		(void) kgss_delete_sec_context(minor_stat,
8087c478bd9Sstevel@tonic-gate 					&ap->context, NULL);
8097c478bd9Sstevel@tonic-gate 		error = EACCES;
8107c478bd9Sstevel@tonic-gate 		goto cleanup;
8117c478bd9Sstevel@tonic-gate 	} else if (process_token.length == 0) {
8127c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt:zero length "
8137c478bd9Sstevel@tonic-gate 				"token in response for export_sec_context, but "
8147c478bd9Sstevel@tonic-gate 				"gsstat == GSS_S_COMPLETE\n");
8157c478bd9Sstevel@tonic-gate 		(void) kgss_delete_sec_context(minor_stat,
8167c478bd9Sstevel@tonic-gate 					&ap->context, NULL);
8177c478bd9Sstevel@tonic-gate 		error = EACCES;
8187c478bd9Sstevel@tonic-gate 		goto cleanup;
8197c478bd9Sstevel@tonic-gate 	} else
8207c478bd9Sstevel@tonic-gate 		*gssstat = kgss_import_sec_context(minor_stat, &process_token,
8217c478bd9Sstevel@tonic-gate 							ap->context);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (*gssstat == GSS_S_COMPLETE) {
8247c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &process_token);
8257c478bd9Sstevel@tonic-gate 	} else {
8267c478bd9Sstevel@tonic-gate 		rpc_gss_display_status(*gssstat, *minor_stat,
8277c478bd9Sstevel@tonic-gate 			desired_mech_type, crgetuid(cr),
8287c478bd9Sstevel@tonic-gate 			"rpcsec_gss_secreate_pvt:gss_import_sec_context");
8297c478bd9Sstevel@tonic-gate 		(void) kgss_delete_sec_context(minor_stat,
8307c478bd9Sstevel@tonic-gate 					&ap->context, NULL);
8317c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &process_token);
8327c478bd9Sstevel@tonic-gate 		error = EACCES;
8337c478bd9Sstevel@tonic-gate 		goto cleanup;
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate done:
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * Validate the sequence window - RFC 2203 section 5.2.3.1
8397c478bd9Sstevel@tonic-gate 	 */
8407c478bd9Sstevel@tonic-gate 	if (!validate_seqwin(ap)) {
8417c478bd9Sstevel@tonic-gate 		error = EACCES;
8427c478bd9Sstevel@tonic-gate 		goto cleanup;
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	/*
8467c478bd9Sstevel@tonic-gate 	 * Done!  Security context creation is successful.
8477c478bd9Sstevel@tonic-gate 	 * Ready for exchanging data.
8487c478bd9Sstevel@tonic-gate 	 */
8497c478bd9Sstevel@tonic-gate 	ap->established = TRUE;
8507c478bd9Sstevel@tonic-gate 	ap->seq_num = 1;
8517c478bd9Sstevel@tonic-gate 	ap->gss_proc = RPCSEC_GSS_DATA;
8527c478bd9Sstevel@tonic-gate 	ap->invalid = FALSE;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	clnt->cl_auth = save_auth;	/* restore cl_auth */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	return (0);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate cleanup:
8597c478bd9Sstevel@tonic-gate 	if (free_results)
8607c478bd9Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res);
8617c478bd9Sstevel@tonic-gate 	clnt->cl_auth = save_auth;	/* restore cl_auth */
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * If need to retry for AUTH_REFRESH, do not cleanup the
8657c478bd9Sstevel@tonic-gate 	 * auth private data.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	if (isrefresh && (error == ETIMEDOUT || error == ECONNRESET)) {
8687c478bd9Sstevel@tonic-gate 		return (error);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (ap->context != NULL) {
8727c478bd9Sstevel@tonic-gate 		rpc_gss_free_pvt(auth);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	return (error? error : EACCES);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate  * Marshall credentials.
8807c478bd9Sstevel@tonic-gate  */
8817c478bd9Sstevel@tonic-gate static bool_t
marshall_creds(ap,xdrs,cred_buf_len)8827c478bd9Sstevel@tonic-gate marshall_creds(ap, xdrs, cred_buf_len)
8837c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap;
8847c478bd9Sstevel@tonic-gate 	XDR			*xdrs;
8857c478bd9Sstevel@tonic-gate 	uint_t			cred_buf_len;
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	rpc_gss_creds		ag_creds;
8887c478bd9Sstevel@tonic-gate 	char			*cred_buf;
8897c478bd9Sstevel@tonic-gate 	struct opaque_auth	creds;
8907c478bd9Sstevel@tonic-gate 	XDR			cred_xdrs;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	ag_creds.version = ap->version;
8937c478bd9Sstevel@tonic-gate 	ag_creds.gss_proc = ap->gss_proc;
8947c478bd9Sstevel@tonic-gate 	ag_creds.seq_num = ap->seq_num;
8957c478bd9Sstevel@tonic-gate 	ag_creds.service = ap->service;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/*
8987c478bd9Sstevel@tonic-gate 	 * If context has not been set up yet, use NULL handle.
8997c478bd9Sstevel@tonic-gate 	 */
9007c478bd9Sstevel@tonic-gate 	if (ap->ctx_handle.length > 0)
9017c478bd9Sstevel@tonic-gate 		ag_creds.ctx_handle = ap->ctx_handle;
9027c478bd9Sstevel@tonic-gate 	else {
9037c478bd9Sstevel@tonic-gate 		ag_creds.ctx_handle.length = 0;
9047c478bd9Sstevel@tonic-gate 		ag_creds.ctx_handle.value = NULL;
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	cred_buf = kmem_alloc(cred_buf_len, KM_SLEEP);
9087c478bd9Sstevel@tonic-gate 	xdrmem_create(&cred_xdrs, (caddr_t)cred_buf, cred_buf_len,
9097c478bd9Sstevel@tonic-gate 								XDR_ENCODE);
9107c478bd9Sstevel@tonic-gate 	if (!__xdr_rpc_gss_creds(&cred_xdrs, &ag_creds)) {
9117c478bd9Sstevel@tonic-gate 		kmem_free(cred_buf, MAX_AUTH_BYTES);
9127c478bd9Sstevel@tonic-gate 		XDR_DESTROY(&cred_xdrs);
9137c478bd9Sstevel@tonic-gate 		return (FALSE);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	creds.oa_flavor = RPCSEC_GSS;
9177c478bd9Sstevel@tonic-gate 	creds.oa_base = cred_buf;
9187c478bd9Sstevel@tonic-gate 	creds.oa_length = xdr_getpos(&cred_xdrs);
9197c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&cred_xdrs);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if (!xdr_opaque_auth(xdrs, &creds)) {
9227c478bd9Sstevel@tonic-gate 		kmem_free(cred_buf, cred_buf_len);
9237c478bd9Sstevel@tonic-gate 		return (FALSE);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	kmem_free(cred_buf, cred_buf_len);
9277c478bd9Sstevel@tonic-gate 	return (TRUE);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * Marshall verifier.  The verifier is the checksum of the RPC header
9327c478bd9Sstevel@tonic-gate  * up to and including the credential field.  The XDR handle that's
9337c478bd9Sstevel@tonic-gate  * passed in has the header up to and including the credential field
9347c478bd9Sstevel@tonic-gate  * encoded.  A pointer to the transmit buffer is also passed in.
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate static bool_t
marshall_verf(ap,xdrs,buf)9377c478bd9Sstevel@tonic-gate marshall_verf(ap, xdrs, buf)
9387c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap;
9397c478bd9Sstevel@tonic-gate 	XDR			*xdrs;	/* send XDR */
9407c478bd9Sstevel@tonic-gate 	char			*buf;	/* pointer of send buffer */
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	struct opaque_auth	verf;
9437c478bd9Sstevel@tonic-gate 	OM_uint32		major, minor;
9447c478bd9Sstevel@tonic-gate 	gss_buffer_desc		in_buf, out_buf;
9457c478bd9Sstevel@tonic-gate 	bool_t			ret = FALSE;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * If context is not established yet, use NULL verifier.
9497c478bd9Sstevel@tonic-gate 	 */
9507c478bd9Sstevel@tonic-gate 	if (!ap->established) {
9517c478bd9Sstevel@tonic-gate 		verf.oa_flavor = AUTH_NONE;
9527c478bd9Sstevel@tonic-gate 		verf.oa_base = NULL;
9537c478bd9Sstevel@tonic-gate 		verf.oa_length = 0;
9547c478bd9Sstevel@tonic-gate 		return (xdr_opaque_auth(xdrs, &verf));
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	verf.oa_flavor = RPCSEC_GSS;
9587c478bd9Sstevel@tonic-gate 	in_buf.length = xdr_getpos(xdrs);
9597c478bd9Sstevel@tonic-gate 	in_buf.value = buf;
9607c478bd9Sstevel@tonic-gate 	if ((major = kgss_sign(&minor, ap->context, ap->qop, &in_buf,
9617c478bd9Sstevel@tonic-gate 				&out_buf)) != GSS_S_COMPLETE) {
9627c478bd9Sstevel@tonic-gate 		if (major == GSS_S_CONTEXT_EXPIRED) {
9637c478bd9Sstevel@tonic-gate 			ap->invalid = TRUE;
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 		RPCGSS_LOG1(1,
9667c478bd9Sstevel@tonic-gate 		    "marshall_verf: kgss_sign failed GSS Major %x Minor %x\n",
9677c478bd9Sstevel@tonic-gate 		    major, minor);
9687c478bd9Sstevel@tonic-gate 		return (FALSE);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	verf.oa_base = out_buf.value;
9717c478bd9Sstevel@tonic-gate 	verf.oa_length = out_buf.length;
9727c478bd9Sstevel@tonic-gate 	ret = xdr_opaque_auth(xdrs, &verf);
9737c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&minor, &out_buf);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	return (ret);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate /*
9797c478bd9Sstevel@tonic-gate  * Validate sequence window upon a successful RPCSEC_GSS INIT session.
9807c478bd9Sstevel@tonic-gate  * The sequence window sent back by the server should be verifiable by
9817c478bd9Sstevel@tonic-gate  * the verifier which is a checksum of the sequence window.
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate static bool_t
validate_seqwin(rpc_gss_data * ap)9847c478bd9Sstevel@tonic-gate validate_seqwin(rpc_gss_data *ap)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	uint_t			seq_win_net;
9877c478bd9Sstevel@tonic-gate 	OM_uint32		major = 0, minor = 0;
9887c478bd9Sstevel@tonic-gate 	gss_buffer_desc		msg_buf, tok_buf;
9897c478bd9Sstevel@tonic-gate 	int			qop_state = 0;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	ASSERT(ap->verifier);
9927c478bd9Sstevel@tonic-gate 	ASSERT(ap->context);
9937c478bd9Sstevel@tonic-gate 	seq_win_net = (uint_t)htonl(ap->seq_window);
9947c478bd9Sstevel@tonic-gate 	msg_buf.length = sizeof (seq_win_net);
9957c478bd9Sstevel@tonic-gate 	msg_buf.value = (char *)&seq_win_net;
9967c478bd9Sstevel@tonic-gate 	tok_buf.length = ap->verifier->oa_length;
9977c478bd9Sstevel@tonic-gate 	tok_buf.value = ap->verifier->oa_base;
9987c478bd9Sstevel@tonic-gate 	major = kgss_verify(&minor, ap->context, &msg_buf, &tok_buf,
9998ffff9fdSgt 	    &qop_state);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
10028ffff9fdSgt 		RPCGSS_LOG1(1,
10038ffff9fdSgt 		    "validate_seqwin: kgss_verify failed GSS Major "
10048ffff9fdSgt 		    "%x Minor %x\n", major, minor);
10058ffff9fdSgt 		RPCGSS_LOG1(1, "seq_window %d, verf len %d ", ap->seq_window,
10068ffff9fdSgt 		    ap->verifier->oa_length);
10078ffff9fdSgt 		return (FALSE);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 	return (TRUE);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate /*
10137c478bd9Sstevel@tonic-gate  * Validate RPC response verifier from server.  The response verifier
10147c478bd9Sstevel@tonic-gate  * is the checksum of the request sequence number.
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate static bool_t
rpc_gss_validate(auth,verf)10177c478bd9Sstevel@tonic-gate rpc_gss_validate(auth, verf)
10187c478bd9Sstevel@tonic-gate 	AUTH			*auth;
10197c478bd9Sstevel@tonic-gate 	struct opaque_auth	*verf;
10207c478bd9Sstevel@tonic-gate {
10217c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
10227c478bd9Sstevel@tonic-gate 	uint_t			seq_num_net;
10237c478bd9Sstevel@tonic-gate 	OM_uint32		major, minor;
10247c478bd9Sstevel@tonic-gate 	gss_buffer_desc		msg_buf, tok_buf;
10257c478bd9Sstevel@tonic-gate 	int			qop_state;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * If context is not established yet, save the verifier for
10297c478bd9Sstevel@tonic-gate 	 * validating the sequence window later at the end of context
10307c478bd9Sstevel@tonic-gate 	 * creation session.
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	if (!ap->established) {
10337c478bd9Sstevel@tonic-gate 	    if (ap->verifier == NULL) {
10347c478bd9Sstevel@tonic-gate 		ap->verifier = kmem_zalloc(sizeof (struct opaque_auth),
10357c478bd9Sstevel@tonic-gate 						KM_SLEEP);
10367c478bd9Sstevel@tonic-gate 		if (verf->oa_length > 0)
10377c478bd9Sstevel@tonic-gate 		    ap->verifier->oa_base = kmem_zalloc(verf->oa_length,
10387c478bd9Sstevel@tonic-gate 						KM_SLEEP);
10397c478bd9Sstevel@tonic-gate 	    } else {
10407c478bd9Sstevel@tonic-gate 		if (ap->verifier->oa_length > 0)
10417c478bd9Sstevel@tonic-gate 		    kmem_free(ap->verifier->oa_base, ap->verifier->oa_length);
10427c478bd9Sstevel@tonic-gate 		if (verf->oa_length > 0)
10437c478bd9Sstevel@tonic-gate 		    ap->verifier->oa_base = kmem_zalloc(verf->oa_length,
10447c478bd9Sstevel@tonic-gate 						KM_SLEEP);
10457c478bd9Sstevel@tonic-gate 	    }
10467c478bd9Sstevel@tonic-gate 	    ap->verifier->oa_length = verf->oa_length;
10477c478bd9Sstevel@tonic-gate 	    bcopy(verf->oa_base, ap->verifier->oa_base, verf->oa_length);
10487c478bd9Sstevel@tonic-gate 	    return (TRUE);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	seq_num_net = (uint_t)htonl(ap->seq_num);
10527c478bd9Sstevel@tonic-gate 	msg_buf.length = sizeof (seq_num_net);
10537c478bd9Sstevel@tonic-gate 	msg_buf.value = (char *)&seq_num_net;
10547c478bd9Sstevel@tonic-gate 	tok_buf.length = verf->oa_length;
10557c478bd9Sstevel@tonic-gate 	tok_buf.value = verf->oa_base;
10567c478bd9Sstevel@tonic-gate 	major = kgss_verify(&minor, ap->context, &msg_buf, &tok_buf,
10577c478bd9Sstevel@tonic-gate 				&qop_state);
10587c478bd9Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
10597c478bd9Sstevel@tonic-gate 		RPCGSS_LOG1(1,
10607c478bd9Sstevel@tonic-gate 		"rpc_gss_validate: kgss_verify failed GSS Major %x Minor %x\n",
10617c478bd9Sstevel@tonic-gate 		major, minor);
10627c478bd9Sstevel@tonic-gate 		return (FALSE);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 	return (TRUE);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*
10687c478bd9Sstevel@tonic-gate  * Refresh client context.  This is necessary sometimes because the
10697c478bd9Sstevel@tonic-gate  * server will ocassionally destroy contexts based on LRU method, or
10707c478bd9Sstevel@tonic-gate  * because of expired credentials.
10717c478bd9Sstevel@tonic-gate  */
10727c478bd9Sstevel@tonic-gate static bool_t
rpc_gss_refresh(auth,msg,cr)10737c478bd9Sstevel@tonic-gate rpc_gss_refresh(auth, msg, cr)
10747c478bd9Sstevel@tonic-gate 	AUTH		*auth;
10757c478bd9Sstevel@tonic-gate 	struct rpc_msg	*msg;
10767c478bd9Sstevel@tonic-gate 	cred_t		*cr;
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
10797c478bd9Sstevel@tonic-gate 	gss_ctx_id_t	ctx_sav = NULL;
10807c478bd9Sstevel@tonic-gate 	gss_buffer_desc	ctx_hdle_sav = {0, NULL};
10817c478bd9Sstevel@tonic-gate 	uint_t		sn_sav, proc_sav;
10827c478bd9Sstevel@tonic-gate 	bool_t		est_sav;
10837c478bd9Sstevel@tonic-gate 	OM_uint32	gssstat, minor_stat;
10847c478bd9Sstevel@tonic-gate 	int error;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/*
10877c478bd9Sstevel@tonic-gate 	 * The context needs to be recreated only when the error status
10887c478bd9Sstevel@tonic-gate 	 * returned from the server is one of the following:
10897c478bd9Sstevel@tonic-gate 	 *	RPCSEC_GSS_NOCRED and RPCSEC_GSS_FAILED
10907c478bd9Sstevel@tonic-gate 	 * The existing context should not be destroyed unless the above
10917c478bd9Sstevel@tonic-gate 	 * error status codes are received or if the context has not
10927c478bd9Sstevel@tonic-gate 	 * been set up.
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	if (msg->rjcted_rply.rj_why == RPCSEC_GSS_NOCRED ||
10967c478bd9Sstevel@tonic-gate 			msg->rjcted_rply.rj_why == RPCSEC_GSS_FAILED ||
10977c478bd9Sstevel@tonic-gate 							!ap->established) {
10987c478bd9Sstevel@tonic-gate 		/*
10997c478bd9Sstevel@tonic-gate 		 * Destroy the context if necessary.  Use the same memory
11007c478bd9Sstevel@tonic-gate 		 * for the new context since we've already passed a pointer
11017c478bd9Sstevel@tonic-gate 		 * to it to the user.
11027c478bd9Sstevel@tonic-gate 		 */
11037c478bd9Sstevel@tonic-gate 		if (ap->context != NULL) {
11047c478bd9Sstevel@tonic-gate 			ctx_sav = ap->context;
11057c478bd9Sstevel@tonic-gate 			ap->context = NULL;
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 		if (ap->ctx_handle.length != 0) {
11087c478bd9Sstevel@tonic-gate 			ctx_hdle_sav.length = ap->ctx_handle.length;
11097c478bd9Sstevel@tonic-gate 			ctx_hdle_sav.value = ap->ctx_handle.value;
11107c478bd9Sstevel@tonic-gate 			ap->ctx_handle.length = 0;
11117c478bd9Sstevel@tonic-gate 			ap->ctx_handle.value = NULL;
11127c478bd9Sstevel@tonic-gate 		}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		/*
11157c478bd9Sstevel@tonic-gate 		 * If the context was not already established, don't try to
11167c478bd9Sstevel@tonic-gate 		 * recreate it.
11177c478bd9Sstevel@tonic-gate 		 */
11187c478bd9Sstevel@tonic-gate 		if (!ap->established) {
11197c478bd9Sstevel@tonic-gate 			ap->invalid = TRUE;
11207c478bd9Sstevel@tonic-gate 			RPCGSS_LOG0(1,
11217c478bd9Sstevel@tonic-gate 			"rpc_gss_refresh: context was not established\n");
11227c478bd9Sstevel@tonic-gate 			error = EINVAL;
11237c478bd9Sstevel@tonic-gate 			goto out;
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		est_sav = ap->established;
11277c478bd9Sstevel@tonic-gate 		sn_sav = ap->seq_num;
11287c478bd9Sstevel@tonic-gate 		proc_sav = ap->gss_proc;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		/*
11317c478bd9Sstevel@tonic-gate 		 * Recreate context.
11327c478bd9Sstevel@tonic-gate 		 */
11337c478bd9Sstevel@tonic-gate 		error = rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth,
11347c478bd9Sstevel@tonic-gate 				ap, ap->mech_type, (gss_OID *)NULL, (int *)NULL,
11357c478bd9Sstevel@tonic-gate 				(OM_uint32 *)NULL, cr, 1);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 		switch (error) {
11387c478bd9Sstevel@tonic-gate 		case 0:
11397c478bd9Sstevel@tonic-gate 			RPCGSS_LOG(1,
11407c478bd9Sstevel@tonic-gate 			"rpc_gss_refresh: auth %p refreshed\n", (void *)auth);
11417c478bd9Sstevel@tonic-gate 			goto out;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		case ETIMEDOUT:
11447c478bd9Sstevel@tonic-gate 		case ECONNRESET:
11457c478bd9Sstevel@tonic-gate 			RPCGSS_LOG0(1, "rpc_gss_refresh: try again\n");
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 			if (ap->context != NULL) {
11487c478bd9Sstevel@tonic-gate 			    (void) kgss_delete_sec_context(&minor_stat,
11497c478bd9Sstevel@tonic-gate 					&ap->context, NULL);
11507c478bd9Sstevel@tonic-gate 			}
11517c478bd9Sstevel@tonic-gate 			if (ap->ctx_handle.length != 0) {
11527c478bd9Sstevel@tonic-gate 			    (void) gss_release_buffer(&minor_stat,
11537c478bd9Sstevel@tonic-gate 					&ap->ctx_handle);
11547c478bd9Sstevel@tonic-gate 			}
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 			/*
11577c478bd9Sstevel@tonic-gate 			 * Restore the original value for the caller to
11587c478bd9Sstevel@tonic-gate 			 * try again later.
11597c478bd9Sstevel@tonic-gate 			 */
11607c478bd9Sstevel@tonic-gate 			ap->context = ctx_sav;
11617c478bd9Sstevel@tonic-gate 			ap->ctx_handle.length = ctx_hdle_sav.length;
11627c478bd9Sstevel@tonic-gate 			ap->ctx_handle.value = ctx_hdle_sav.value;
11637c478bd9Sstevel@tonic-gate 			ap->established = est_sav;
11647c478bd9Sstevel@tonic-gate 			ap->seq_num = sn_sav;
11657c478bd9Sstevel@tonic-gate 			ap->gss_proc = proc_sav;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			return (FALSE);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		default:
11707c478bd9Sstevel@tonic-gate 			ap->invalid = TRUE;
11717c478bd9Sstevel@tonic-gate 			RPCGSS_LOG(1, "rpc_gss_refresh: can't refresh this "
11727c478bd9Sstevel@tonic-gate 				"auth, error=%d\n", error);
11737c478bd9Sstevel@tonic-gate 			goto out;
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 	RPCGSS_LOG0(1, "rpc_gss_refresh: don't refresh");
11777c478bd9Sstevel@tonic-gate 	return (FALSE);
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate out:
11807c478bd9Sstevel@tonic-gate 	if (ctx_sav != NULL) {
11817c478bd9Sstevel@tonic-gate 		(void) kgss_delete_sec_context(&minor_stat,
11827c478bd9Sstevel@tonic-gate 				&ctx_sav, NULL);
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 	if (ctx_hdle_sav.length != 0) {
11857c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &ctx_hdle_sav);
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	return (error == 0);
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate /*
11927c478bd9Sstevel@tonic-gate  * Destroy a context.
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate static void
rpc_gss_destroy(auth)11957c478bd9Sstevel@tonic-gate rpc_gss_destroy(auth)
11967c478bd9Sstevel@tonic-gate 	AUTH		*auth;
11977c478bd9Sstevel@tonic-gate {
11987c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	/*
12017c478bd9Sstevel@tonic-gate 	 *  XXX Currently, we do not ping the server (rpc_gss_destroy_pvt)
12027c478bd9Sstevel@tonic-gate 	 *  to destroy the context in the server cache.
12037c478bd9Sstevel@tonic-gate 	 *  We assume there is a good LRU/aging mechanism for the
12047c478bd9Sstevel@tonic-gate 	 *  context cache on the server side.
12057c478bd9Sstevel@tonic-gate 	 */
12067c478bd9Sstevel@tonic-gate 	rpc_gss_free_pvt(auth);
12077c478bd9Sstevel@tonic-gate 	kmem_free((char *)ap, sizeof (*ap));
12087c478bd9Sstevel@tonic-gate 	kmem_free(auth, sizeof (*auth));
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate  * Private interface to free memory allocated in the rpcsec_gss private
12137c478bd9Sstevel@tonic-gate  * data structure (rpc_gss_data).
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@tonic-gate static void
rpc_gss_free_pvt(auth)12167c478bd9Sstevel@tonic-gate rpc_gss_free_pvt(auth)
12177c478bd9Sstevel@tonic-gate 	AUTH		*auth;
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	OM_uint32	minor_stat;
12207c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	if (ap->ctx_handle.length != 0) {
12237c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &ap->ctx_handle);
12247c478bd9Sstevel@tonic-gate 		ap->ctx_handle.length = 0;
12257c478bd9Sstevel@tonic-gate 		ap->ctx_handle.value = NULL;
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/*
12297c478bd9Sstevel@tonic-gate 	 * Destroy local GSS context.
12307c478bd9Sstevel@tonic-gate 	 */
12317c478bd9Sstevel@tonic-gate 	if (ap->context != NULL) {
12327c478bd9Sstevel@tonic-gate 		(void) kgss_delete_sec_context(&minor_stat, &ap->context, NULL);
12337c478bd9Sstevel@tonic-gate 		ap->context = NULL;
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Looks like we need to release default credentials if we use it.
12387c478bd9Sstevel@tonic-gate 	 * Non-default creds need to be released by user.
12397c478bd9Sstevel@tonic-gate 	 */
12407c478bd9Sstevel@tonic-gate 	if (ap->my_cred == GSS_C_NO_CREDENTIAL)
12417c478bd9Sstevel@tonic-gate 		(void) kgss_release_cred(&minor_stat, &ap->my_cred,
12427c478bd9Sstevel@tonic-gate 					crgetuid(CRED()));
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	/*
12457c478bd9Sstevel@tonic-gate 	 * Release any internal name structures.
12467c478bd9Sstevel@tonic-gate 	 */
12477c478bd9Sstevel@tonic-gate 	if (ap->target_name != NULL) {
12487c478bd9Sstevel@tonic-gate 		(void) gss_release_name(&minor_stat, &ap->target_name);
12497c478bd9Sstevel@tonic-gate 		ap->target_name = NULL;
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 * Free mech_type oid structure.
12547c478bd9Sstevel@tonic-gate 	 */
12557c478bd9Sstevel@tonic-gate 	if (ap->mech_type != NULL) {
12567c478bd9Sstevel@tonic-gate 		kgss_free_oid(ap->mech_type);
12577c478bd9Sstevel@tonic-gate 		ap->mech_type = NULL;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/*
12617c478bd9Sstevel@tonic-gate 	 * Free the verifier saved for sequence window checking.
12627c478bd9Sstevel@tonic-gate 	 */
12637c478bd9Sstevel@tonic-gate 	if (ap->verifier != NULL) {
12647c478bd9Sstevel@tonic-gate 	    if (ap->verifier->oa_length > 0) {
12657c478bd9Sstevel@tonic-gate 		kmem_free(ap->verifier->oa_base, ap->verifier->oa_length);
12667c478bd9Sstevel@tonic-gate 	    }
12677c478bd9Sstevel@tonic-gate 	    kmem_free(ap->verifier, sizeof (struct opaque_auth));
12687c478bd9Sstevel@tonic-gate 	    ap->verifier = NULL;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate #if 0
12737c478bd9Sstevel@tonic-gate /*
12747c478bd9Sstevel@tonic-gate  * XXX this function is not used right now.
12757c478bd9Sstevel@tonic-gate  * There is a client handle issue needs to be resolved.
12767c478bd9Sstevel@tonic-gate  *
12777c478bd9Sstevel@tonic-gate  * This is a private interface which will destroy a context
12787c478bd9Sstevel@tonic-gate  * without freeing up the memory used by it.  We need to do this when
12797c478bd9Sstevel@tonic-gate  * a refresh fails, for example, so the user will still have a handle.
12807c478bd9Sstevel@tonic-gate  */
12817c478bd9Sstevel@tonic-gate static void
12827c478bd9Sstevel@tonic-gate rpc_gss_destroy_pvt(auth)
12837c478bd9Sstevel@tonic-gate 	AUTH		*auth;
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	struct timeval	timeout;
12867c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * If we have a server context id, inform server that we are
12907c478bd9Sstevel@tonic-gate 	 * destroying the context.
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	if (ap->ctx_handle.length != 0) {
12937c478bd9Sstevel@tonic-gate 		uint32_t oldxid;
12947c478bd9Sstevel@tonic-gate 		uint32_t zeroxid = 0;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		ap->gss_proc = RPCSEC_GSS_DESTROY;
12977c478bd9Sstevel@tonic-gate 		timeout.tv_sec = 10;
12987c478bd9Sstevel@tonic-gate 		timeout.tv_usec = 0;
12997c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(ap->clnt, CLGET_XID, (char *)&oldxid);
13007c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(ap->clnt, CLSET_XID, (char *)&zeroxid);
13017c478bd9Sstevel@tonic-gate 		(void) clnt_call(ap->clnt, NULLPROC, xdr_void, NULL,
13027c478bd9Sstevel@tonic-gate 						xdr_void, NULL, timeout);
13037c478bd9Sstevel@tonic-gate 		(void) CLNT_CONTROL(ap->clnt, CLSET_XID, (char *)&oldxid);
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	rpc_gss_free_pvt(auth);
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate #endif
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate /*
13117c478bd9Sstevel@tonic-gate  * Wrap client side data.  The encoded header is passed in through
13127c478bd9Sstevel@tonic-gate  * buf and buflen.  The header is up to but not including the
13137c478bd9Sstevel@tonic-gate  * credential field.
13147c478bd9Sstevel@tonic-gate  */
13157c478bd9Sstevel@tonic-gate bool_t
rpc_gss_wrap(auth,buf,buflen,out_xdrs,xdr_func,xdr_ptr)13167c478bd9Sstevel@tonic-gate rpc_gss_wrap(auth, buf, buflen, out_xdrs, xdr_func, xdr_ptr)
13177c478bd9Sstevel@tonic-gate 	AUTH			*auth;
13187c478bd9Sstevel@tonic-gate 	char			*buf;		/* encoded header */
13197c478bd9Sstevel@tonic-gate /* has been changed to u_int in the user land */
13207c478bd9Sstevel@tonic-gate 	uint_t			buflen;		/* encoded header length */
13217c478bd9Sstevel@tonic-gate 	XDR			*out_xdrs;
13227c478bd9Sstevel@tonic-gate 	xdrproc_t		xdr_func;
13237c478bd9Sstevel@tonic-gate 	caddr_t			xdr_ptr;
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
13267c478bd9Sstevel@tonic-gate 	XDR			xdrs;
13277c478bd9Sstevel@tonic-gate 	char			*tmp_buf;
13287c478bd9Sstevel@tonic-gate 	uint_t			xdr_buf_len, cred_buf_len;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate /*
13317c478bd9Sstevel@tonic-gate  *  Here is how MAX_SIGNED_LEN is estimated.
13327c478bd9Sstevel@tonic-gate  *  Signing a 48 bytes buffer using des_cbc_md5 would end up with
13337c478bd9Sstevel@tonic-gate  *  a buffer length 33 (padded data + 16 bytes of seq_num/checksum).
13347c478bd9Sstevel@tonic-gate  *  Current known max seq_num/checksum size is 24 bytes.
13357c478bd9Sstevel@tonic-gate  *  88 is derived from RNDUP(33+(24-16)) * 2.
13367c478bd9Sstevel@tonic-gate  */
13377c478bd9Sstevel@tonic-gate #define	MAX_SIGNED_LEN	88
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	/*
13407c478bd9Sstevel@tonic-gate 	 * Reject an invalid context.
13417c478bd9Sstevel@tonic-gate 	 */
13427c478bd9Sstevel@tonic-gate 	if (ap->invalid) {
13437c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: reject an invalid context\n");
13447c478bd9Sstevel@tonic-gate 		return (FALSE);
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * If context is established, bump up sequence number.
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	if (ap->established)
13517c478bd9Sstevel@tonic-gate 		ap->seq_num++;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	/*
13547c478bd9Sstevel@tonic-gate 	 * Create the header in a temporary XDR context and buffer
13557c478bd9Sstevel@tonic-gate 	 * before putting it out.
13567c478bd9Sstevel@tonic-gate 	 */
13577c478bd9Sstevel@tonic-gate 	cred_buf_len = RNDUP(sizeof (ap->version) + sizeof (ap->gss_proc) +
13587c478bd9Sstevel@tonic-gate 			sizeof (ap->seq_num) + sizeof (ap->service) +
13597c478bd9Sstevel@tonic-gate 			sizeof (ap->ctx_handle) + ap->ctx_handle.length);
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	xdr_buf_len = buflen + cred_buf_len + sizeof (struct opaque_auth) +
13627c478bd9Sstevel@tonic-gate 			MAX_SIGNED_LEN;
13637c478bd9Sstevel@tonic-gate 	tmp_buf = kmem_alloc(xdr_buf_len, KM_SLEEP);
13647c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, tmp_buf, xdr_buf_len, XDR_ENCODE);
13657c478bd9Sstevel@tonic-gate 	if (!XDR_PUTBYTES(&xdrs, buf, buflen)) {
13667c478bd9Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13677c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: xdr putbytes failed\n");
13687c478bd9Sstevel@tonic-gate 		return (FALSE);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * create cred field
13737c478bd9Sstevel@tonic-gate 	 */
13747c478bd9Sstevel@tonic-gate 	if (!marshall_creds(ap, &xdrs, cred_buf_len)) {
13757c478bd9Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13767c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: marshall_creds failed\n");
13777c478bd9Sstevel@tonic-gate 		return (FALSE);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	/*
13817c478bd9Sstevel@tonic-gate 	 * create verifier
13827c478bd9Sstevel@tonic-gate 	 */
13837c478bd9Sstevel@tonic-gate 	if (!marshall_verf(ap, &xdrs, tmp_buf)) {
13847c478bd9Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13857c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: marshall_verf failed\n");
13867c478bd9Sstevel@tonic-gate 		return (FALSE);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * write out header and destroy temp structures
13917c478bd9Sstevel@tonic-gate 	 */
13927c478bd9Sstevel@tonic-gate 	if (!XDR_PUTBYTES(out_xdrs, tmp_buf, XDR_GETPOS(&xdrs))) {
13937c478bd9Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13947c478bd9Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: write out header failed\n");
13957c478bd9Sstevel@tonic-gate 		return (FALSE);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
13987c478bd9Sstevel@tonic-gate 	kmem_free(tmp_buf, xdr_buf_len);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	/*
14017c478bd9Sstevel@tonic-gate 	 * If context is not established, or if neither integrity
14027c478bd9Sstevel@tonic-gate 	 * nor privacy is used, just XDR encode data.
14037c478bd9Sstevel@tonic-gate 	 */
14047c478bd9Sstevel@tonic-gate 	if (!ap->established || ap->service == rpc_gss_svc_none) {
14057c478bd9Sstevel@tonic-gate 		return ((*xdr_func)(out_xdrs, xdr_ptr));
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	return (__rpc_gss_wrap_data(ap->service, ap->qop, ap->context,
14097c478bd9Sstevel@tonic-gate 				ap->seq_num, out_xdrs, xdr_func, xdr_ptr));
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate /*
14137c478bd9Sstevel@tonic-gate  * Unwrap received data.
14147c478bd9Sstevel@tonic-gate  */
14157c478bd9Sstevel@tonic-gate bool_t
rpc_gss_unwrap(auth,in_xdrs,xdr_func,xdr_ptr)14167c478bd9Sstevel@tonic-gate rpc_gss_unwrap(auth, in_xdrs, xdr_func, xdr_ptr)
14177c478bd9Sstevel@tonic-gate 	AUTH			*auth;
14187c478bd9Sstevel@tonic-gate 	XDR			*in_xdrs;
14197c478bd9Sstevel@tonic-gate 	bool_t			(*xdr_func)();
14207c478bd9Sstevel@tonic-gate 	caddr_t			xdr_ptr;
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	/*
14257c478bd9Sstevel@tonic-gate 	 * If context is not established, of if neither integrity
14267c478bd9Sstevel@tonic-gate 	 * nor privacy is used, just XDR encode data.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	if (!ap->established || ap->service == rpc_gss_svc_none)
14297c478bd9Sstevel@tonic-gate 		return ((*xdr_func)(in_xdrs, xdr_ptr));
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	return (__rpc_gss_unwrap_data(ap->service,
14327c478bd9Sstevel@tonic-gate 				ap->context,
14337c478bd9Sstevel@tonic-gate 				ap->seq_num,
14347c478bd9Sstevel@tonic-gate 				ap->qop,
14357c478bd9Sstevel@tonic-gate 				in_xdrs, xdr_func, xdr_ptr));
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate /*
14397c478bd9Sstevel@tonic-gate  *  Revoke an GSSAPI based security credentials
14407c478bd9Sstevel@tonic-gate  *  from the cache table.
14417c478bd9Sstevel@tonic-gate  */
14427c478bd9Sstevel@tonic-gate int
rpc_gss_revauth(uid_t uid,rpc_gss_OID mech)14437c478bd9Sstevel@tonic-gate rpc_gss_revauth(uid_t uid, rpc_gss_OID mech)
14447c478bd9Sstevel@tonic-gate {
14457c478bd9Sstevel@tonic-gate 	struct ga_cache_entry *next, *prev, *cur;
14467c478bd9Sstevel@tonic-gate 	rpc_gss_data *ap;
14477c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
14487c478bd9Sstevel@tonic-gate 	int i;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	/*
14517c478bd9Sstevel@tonic-gate 	 *  Check the cache table against the uid and the
14527c478bd9Sstevel@tonic-gate 	 *  mechanism type.
14537c478bd9Sstevel@tonic-gate 	 */
14547c478bd9Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
14557c478bd9Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
14567c478bd9Sstevel@tonic-gate 		prev = NULL;
14577c478bd9Sstevel@tonic-gate 		for (cur = ga_cache_table[i]; cur; cur = next) {
14587c478bd9Sstevel@tonic-gate 			NOT_DEAD(cur);
14597c478bd9Sstevel@tonic-gate 			next = cur->next;
14607c478bd9Sstevel@tonic-gate 			NOT_DEAD(next);
14617c478bd9Sstevel@tonic-gate 			ap = AUTH_PRIVATE(cur->auth);
14627c478bd9Sstevel@tonic-gate 			if (__rpc_gss_oids_equal(ap->mech_type,
14637c478bd9Sstevel@tonic-gate 			    (gss_OID) mech) && (cur->uid == uid) &&
14647c478bd9Sstevel@tonic-gate 			    (cur->zoneid == zoneid)) {
14657c478bd9Sstevel@tonic-gate 				if (cur->in_use) {
14667c478bd9Sstevel@tonic-gate 					RPCGSS_LOG(2, "rpc_gss_revauth:invalid "
14678ffff9fdSgt 					    "auth %p\n", (void *)cur->auth);
14687c478bd9Sstevel@tonic-gate 					ap->invalid = TRUE;
14697c478bd9Sstevel@tonic-gate 				} else {
14707c478bd9Sstevel@tonic-gate 					RPCGSS_LOG(2, "rpc_gss_revauth:destroy "
14718ffff9fdSgt 					    "auth %p\n", (void *)cur->auth);
14727c478bd9Sstevel@tonic-gate 					rpc_gss_destroy(cur->auth);
14737c478bd9Sstevel@tonic-gate 					kmem_cache_free(ga_cache_handle,
14748ffff9fdSgt 					    (void *)cur);
14757c478bd9Sstevel@tonic-gate 				}
14767c478bd9Sstevel@tonic-gate 				if (prev == NULL) {
14777c478bd9Sstevel@tonic-gate 					ga_cache_table[i] = next;
14787c478bd9Sstevel@tonic-gate 				} else {
14797c478bd9Sstevel@tonic-gate 					prev->next = next;
14807c478bd9Sstevel@tonic-gate 					NOT_DEAD(prev->next);
14817c478bd9Sstevel@tonic-gate 				}
14827c478bd9Sstevel@tonic-gate 			} else {
14837c478bd9Sstevel@tonic-gate 				prev = cur;
14847c478bd9Sstevel@tonic-gate 			}
14857c478bd9Sstevel@tonic-gate 		}
14867c478bd9Sstevel@tonic-gate 	}
14877c478bd9Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	return (0);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate /*
14947c478bd9Sstevel@tonic-gate  *  Delete all the entries indexed by the cache_key.
14957c478bd9Sstevel@tonic-gate  *
14967c478bd9Sstevel@tonic-gate  *  For example, the cache_key used for NFS is the address of the
14977c478bd9Sstevel@tonic-gate  *  security entry for each mount point.  When the file system is unmounted,
14987c478bd9Sstevel@tonic-gate  *  all the cache entries indexed by this key should be deleted.
14997c478bd9Sstevel@tonic-gate  */
15007c478bd9Sstevel@tonic-gate void
rpc_gss_secpurge(void * cache_key)15017c478bd9Sstevel@tonic-gate rpc_gss_secpurge(void *cache_key)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	struct ga_cache_entry *next, *prev, *cur;
15047c478bd9Sstevel@tonic-gate 	int i;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	/*
15077c478bd9Sstevel@tonic-gate 	 *  Check the cache table against the cache_key.
15087c478bd9Sstevel@tonic-gate 	 */
15097c478bd9Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
15107c478bd9Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
15117c478bd9Sstevel@tonic-gate 		prev = NULL;
15127c478bd9Sstevel@tonic-gate 		for (cur = ga_cache_table[i]; cur; cur = next) {
15137c478bd9Sstevel@tonic-gate 			NOT_DEAD(cur);
15147c478bd9Sstevel@tonic-gate 			next = cur->next;
15157c478bd9Sstevel@tonic-gate 			NOT_DEAD(next);
15167c478bd9Sstevel@tonic-gate 			if (cache_key == cur->cache_key) {
15177c478bd9Sstevel@tonic-gate 				RPCGSS_LOG(2, "rpc_gss_secpurge: destroy auth "
15188ffff9fdSgt 				    "%p\n", (void *)cur->auth);
15198ffff9fdSgt 				if (cur->in_use == FALSE)
15208ffff9fdSgt 					rpc_gss_destroy(cur->auth);
15217c478bd9Sstevel@tonic-gate 				kmem_cache_free(ga_cache_handle, (void *)cur);
15227c478bd9Sstevel@tonic-gate 				if (prev == NULL) {
15237c478bd9Sstevel@tonic-gate 					ga_cache_table[i] = next;
15247c478bd9Sstevel@tonic-gate 				} else {
15257c478bd9Sstevel@tonic-gate 					NOT_DEAD(prev->next);
15267c478bd9Sstevel@tonic-gate 					prev->next = next;
15277c478bd9Sstevel@tonic-gate 				}
15287c478bd9Sstevel@tonic-gate 			} else {
15297c478bd9Sstevel@tonic-gate 				prev = cur;
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate /*
15377c478bd9Sstevel@tonic-gate  * Function: rpc_gss_nextverf.  Not used.
15387c478bd9Sstevel@tonic-gate  */
15397c478bd9Sstevel@tonic-gate static void
rpc_gss_nextverf()15407c478bd9Sstevel@tonic-gate rpc_gss_nextverf()
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate }
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate /*
15457c478bd9Sstevel@tonic-gate  * Function: rpc_gss_marshall - no op routine.
15467c478bd9Sstevel@tonic-gate  *		rpc_gss_wrap() is doing the marshalling.
15477c478bd9Sstevel@tonic-gate  */
15487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15497c478bd9Sstevel@tonic-gate static bool_t
rpc_gss_marshall(auth,xdrs)15507c478bd9Sstevel@tonic-gate rpc_gss_marshall(auth, xdrs)
15517c478bd9Sstevel@tonic-gate 	AUTH		*auth;
15527c478bd9Sstevel@tonic-gate 	XDR		*xdrs;
15537c478bd9Sstevel@tonic-gate {
15547c478bd9Sstevel@tonic-gate 	return (TRUE);
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate /*
15587c478bd9Sstevel@tonic-gate  * Set service defaults.
15597c478bd9Sstevel@tonic-gate  * Not supported yet.
15607c478bd9Sstevel@tonic-gate  */
15617c478bd9Sstevel@tonic-gate /* ARGSUSED */
15627c478bd9Sstevel@tonic-gate bool_t
rpc_gss_set_defaults(auth,service,qop)15637c478bd9Sstevel@tonic-gate rpc_gss_set_defaults(auth, service, qop)
15647c478bd9Sstevel@tonic-gate 	AUTH			*auth;
15657c478bd9Sstevel@tonic-gate 	rpc_gss_service_t	service;
15667c478bd9Sstevel@tonic-gate 	uint_t			qop;
15677c478bd9Sstevel@tonic-gate {
15687c478bd9Sstevel@tonic-gate 	return (FALSE);
15697c478bd9Sstevel@tonic-gate }
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate /* ARGSUSED */
15727c478bd9Sstevel@tonic-gate int
rpc_gss_max_data_length(AUTH * rpcgss_handle,int max_tp_unit_len)15737c478bd9Sstevel@tonic-gate rpc_gss_max_data_length(AUTH *rpcgss_handle, int max_tp_unit_len)
15747c478bd9Sstevel@tonic-gate {
15757c478bd9Sstevel@tonic-gate 	return (0);
15767c478bd9Sstevel@tonic-gate }
15770a701b1eSRobert Gordon 
15780a701b1eSRobert Gordon rpc_gss_service_t
rpc_gss_get_service_type(AUTH * auth)15790a701b1eSRobert Gordon rpc_gss_get_service_type(AUTH *auth)
15800a701b1eSRobert Gordon {
15810a701b1eSRobert Gordon 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
15820a701b1eSRobert Gordon 
15830a701b1eSRobert Gordon 	return (ap->service);
15840a701b1eSRobert Gordon }
1585