xref: /onnv-gate/usr/src/uts/common/rpc/sec_gss/rpcsec_gss.c (revision 7387:0b3a92e31fd8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54321Scasper  * Common Development and Distribution License (the "License").
64321Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226403Sgt29601  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * $Header:
300Sstevel@tonic-gate  * /afs/gza.com/product/secure/rel-eng/src/1.1/rpc/RCS/auth_gssapi.c,v
310Sstevel@tonic-gate  * 1.14 1995/03/22 22:07:55 jik Exp $
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include  <sys/systm.h>
350Sstevel@tonic-gate #include  <sys/types.h>
360Sstevel@tonic-gate #include  <gssapi/gssapi.h>
370Sstevel@tonic-gate #include  <rpc/rpc.h>
380Sstevel@tonic-gate #include  <rpc/rpcsec_defs.h>
390Sstevel@tonic-gate #include  <sys/debug.h>
400Sstevel@tonic-gate #include  <sys/cmn_err.h>
410Sstevel@tonic-gate #include  <sys/ddi.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static	void	rpc_gss_nextverf();
440Sstevel@tonic-gate static	bool_t	rpc_gss_marshall();
450Sstevel@tonic-gate static	bool_t	rpc_gss_validate();
460Sstevel@tonic-gate static	bool_t	rpc_gss_refresh();
470Sstevel@tonic-gate static	void	rpc_gss_destroy();
480Sstevel@tonic-gate #if 0
490Sstevel@tonic-gate static	void	rpc_gss_destroy_pvt();
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate static	void	rpc_gss_free_pvt();
520Sstevel@tonic-gate static	int	rpc_gss_seccreate_pvt();
530Sstevel@tonic-gate static  bool_t	rpc_gss_wrap();
540Sstevel@tonic-gate static  bool_t	rpc_gss_unwrap();
550Sstevel@tonic-gate static	bool_t	validate_seqwin();
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #ifdef	DEBUG
590Sstevel@tonic-gate #include <sys/promif.h>
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct auth_ops rpc_gss_ops = {
630Sstevel@tonic-gate 	rpc_gss_nextverf,
640Sstevel@tonic-gate 	rpc_gss_marshall,
650Sstevel@tonic-gate 	rpc_gss_validate,
660Sstevel@tonic-gate 	rpc_gss_refresh,
670Sstevel@tonic-gate 	rpc_gss_destroy,
680Sstevel@tonic-gate 	rpc_gss_wrap,
690Sstevel@tonic-gate 	rpc_gss_unwrap,
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Private data for RPCSEC_GSS.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate typedef struct _rpc_gss_data {
760Sstevel@tonic-gate 	bool_t		established;	/* TRUE when established */
770Sstevel@tonic-gate 	CLIENT		*clnt;		/* associated client handle */
780Sstevel@tonic-gate 	int		version;	/* RPCSEC version */
790Sstevel@tonic-gate 	gss_ctx_id_t	context;	/* GSS context id */
800Sstevel@tonic-gate 	gss_buffer_desc	ctx_handle;	/* RPCSEC GSS context handle */
810Sstevel@tonic-gate 	uint_t		seq_num;	/* last sequence number rcvd */
820Sstevel@tonic-gate 	gss_cred_id_t	my_cred;	/* caller's GSS credentials */
830Sstevel@tonic-gate 	OM_uint32	qop;		/* requested QOP */
840Sstevel@tonic-gate 	rpc_gss_service_t	service;	/* requested service */
850Sstevel@tonic-gate 	uint_t		gss_proc;	/* GSS control procedure */
860Sstevel@tonic-gate 	gss_name_t	target_name;	/* target server */
870Sstevel@tonic-gate 	int		req_flags;	/* GSS request bits */
880Sstevel@tonic-gate 	gss_OID		mech_type;	/* GSS mechanism */
890Sstevel@tonic-gate 	OM_uint32	time_req;	/* requested cred lifetime */
900Sstevel@tonic-gate 	bool_t		invalid;	/* can't use this any more */
910Sstevel@tonic-gate 	OM_uint32	seq_window;	/* server sequence window */
920Sstevel@tonic-gate 	struct opaque_auth *verifier;	/* rpc reply verifier saved for */
930Sstevel@tonic-gate 					/* validating the sequence window */
940Sstevel@tonic-gate 	gss_channel_bindings_t	icb;
950Sstevel@tonic-gate } rpc_gss_data;
960Sstevel@tonic-gate #define	AUTH_PRIVATE(auth)	((rpc_gss_data *)auth->ah_private)
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #define	INTERRUPT_OK	1	/* allow interrupt */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  *  RPCSEC_GSS auth cache definitions.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* The table size must be a power of two. */
1050Sstevel@tonic-gate #define	GSSAUTH_TABLESIZE 16
1060Sstevel@tonic-gate #define	HASH(keynum, uid_num) \
1074321Scasper 	((((intptr_t)(keynum)) ^ ((int)uid_num)) & (GSSAUTH_TABLESIZE - 1))
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * gss auth cache entry.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate typedef struct ga_cache_entry {
1130Sstevel@tonic-gate 	void	*cache_key;
1140Sstevel@tonic-gate 	uid_t	uid;
1150Sstevel@tonic-gate 	zoneid_t zoneid;
1160Sstevel@tonic-gate 	bool_t	in_use;
1170Sstevel@tonic-gate 	time_t	ref_time; /* the time referenced previously */
1180Sstevel@tonic-gate 	time_t	ctx_expired_time; /* when the context will be expired */
1190Sstevel@tonic-gate 	AUTH	*auth;
1200Sstevel@tonic-gate 	struct ga_cache_entry *next;
1210Sstevel@tonic-gate } *ga_cache_list;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate struct ga_cache_entry	*ga_cache_table[GSSAUTH_TABLESIZE];
1240Sstevel@tonic-gate static krwlock_t	ga_cache_table_lock;
1250Sstevel@tonic-gate static struct kmem_cache *ga_cache_handle;
1260Sstevel@tonic-gate static void gssauth_cache_reclaim(void *);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static void gssauth_zone_fini(zoneid_t, void *);
1290Sstevel@tonic-gate static zone_key_t	gssauth_zone_key;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate int ga_cache_hit;
1320Sstevel@tonic-gate int ga_cache_miss;
1330Sstevel@tonic-gate int ga_cache_reclaim;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate #define	NOT_DEAD(ptr)	ASSERT((((intptr_t)(ptr)) != 0xdeadbeef))
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate void
gssauth_init(void)1380Sstevel@tonic-gate gssauth_init(void)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Initialize gss auth cache table lock
1420Sstevel@tonic-gate 	 */
1430Sstevel@tonic-gate 	rw_init(&ga_cache_table_lock, NULL, RW_DEFAULT, NULL);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Allocate gss auth cache handle
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	ga_cache_handle = kmem_cache_create("ga_cache_handle",
1496403Sgt29601 	    sizeof (struct ga_cache_entry), 0, NULL, NULL,
1506403Sgt29601 	    gssauth_cache_reclaim, NULL, NULL, 0);
1510Sstevel@tonic-gate 	zone_key_create(&gssauth_zone_key, NULL, NULL, gssauth_zone_fini);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Destroy the structures previously initialized in gssauth_init()
1560Sstevel@tonic-gate  * This routine is called by _init() if mod_install() failed.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate void
gssauth_fini(void)1590Sstevel@tonic-gate gssauth_fini(void)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	(void) zone_key_delete(gssauth_zone_key);
1620Sstevel@tonic-gate 	kmem_cache_destroy(ga_cache_handle);
1630Sstevel@tonic-gate 	rw_destroy(&ga_cache_table_lock);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * This is a cleanup routine to release cached entries when a zone is being
1680Sstevel@tonic-gate  * destroyed.  The code is also used when kmem calls us to free up memory, at
1690Sstevel@tonic-gate  * which point ``zoneid'' will be ALL_ZONES.  We don't honor the cache timeout
1700Sstevel@tonic-gate  * when the zone is going away, since the zoneid (and all associated cached
1710Sstevel@tonic-gate  * entries) are invalid.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate time_t rpc_gss_cache_time = 60 * 60;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /* ARGSUSED */
1760Sstevel@tonic-gate static void
gssauth_zone_fini(zoneid_t zoneid,void * unused)1770Sstevel@tonic-gate gssauth_zone_fini(zoneid_t zoneid, void *unused)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	struct ga_cache_entry *p, *prev, *next;
1800Sstevel@tonic-gate 	int i;
1810Sstevel@tonic-gate 	time_t now;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
1860Sstevel@tonic-gate 		prev = NULL;
1870Sstevel@tonic-gate 		for (p = ga_cache_table[i]; p; p = next) {
1880Sstevel@tonic-gate 			NOT_DEAD(p->next);
1890Sstevel@tonic-gate 			next = p->next;
1900Sstevel@tonic-gate 			NOT_DEAD(next);
1910Sstevel@tonic-gate 			if (zoneid == ALL_ZONES) {	/* kmem callback */
1920Sstevel@tonic-gate 				/*
1930Sstevel@tonic-gate 				 * Free entries that have not been
1940Sstevel@tonic-gate 				 * used for rpc_gss_cache_time seconds.
1950Sstevel@tonic-gate 				 */
1960Sstevel@tonic-gate 				now = gethrestime_sec();
1970Sstevel@tonic-gate 				if ((p->ref_time + rpc_gss_cache_time >
1986403Sgt29601 				    now) || p->in_use) {
1990Sstevel@tonic-gate 					if ((p->ref_time + rpc_gss_cache_time <=
2006403Sgt29601 					    now) && p->in_use) {
2010Sstevel@tonic-gate 						RPCGSS_LOG0(2, "gssauth_cache_"
2020Sstevel@tonic-gate 						    "reclaim: in_use\n");
2030Sstevel@tonic-gate 					}
2040Sstevel@tonic-gate 					prev = p;
2050Sstevel@tonic-gate 					continue;
2060Sstevel@tonic-gate 				}
2070Sstevel@tonic-gate 			} else {
2080Sstevel@tonic-gate 				if (p->zoneid != zoneid) {
2090Sstevel@tonic-gate 					prev = p;
2100Sstevel@tonic-gate 					continue;
2110Sstevel@tonic-gate 				}
2120Sstevel@tonic-gate 				ASSERT(!p->in_use);
2130Sstevel@tonic-gate 			}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 			RPCGSS_LOG(2, "gssauth_cache_reclaim: destroy auth "
2166403Sgt29601 			    "%p\n", (void *)p->auth);
2170Sstevel@tonic-gate 			rpc_gss_destroy(p->auth);
2180Sstevel@tonic-gate 			kmem_cache_free(ga_cache_handle, (void *)p);
2190Sstevel@tonic-gate 			if (prev == NULL) {
2200Sstevel@tonic-gate 				ga_cache_table[i] = next;
2210Sstevel@tonic-gate 			} else {
2220Sstevel@tonic-gate 				NOT_DEAD(prev->next);
2230Sstevel@tonic-gate 				prev->next = next;
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * Called by the kernel memory allocator when
2340Sstevel@tonic-gate  * memory is low. Free unused cache entries.
2350Sstevel@tonic-gate  * If that's not enough, the VM system will
2360Sstevel@tonic-gate  * call again for some more.
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate /*ARGSUSED*/
2390Sstevel@tonic-gate static void
gssauth_cache_reclaim(void * cdrarg)2400Sstevel@tonic-gate gssauth_cache_reclaim(void *cdrarg)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	gssauth_zone_fini(ALL_ZONES, NULL);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #define	NOT_NULL(ptr)	ASSERT(ptr)
2460Sstevel@tonic-gate #define	IS_ALIGNED(ptr)	ASSERT((((intptr_t)(ptr)) & 3) == 0)
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  *  Get the client gss security service handle.
2500Sstevel@tonic-gate  *  If it is in the cache table, get it, otherwise, create
2510Sstevel@tonic-gate  *  a new one by calling rpc_gss_seccreate().
2520Sstevel@tonic-gate  */
2530Sstevel@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)2540Sstevel@tonic-gate rpc_gss_secget(CLIENT *clnt,
2550Sstevel@tonic-gate 	char	*principal,
2560Sstevel@tonic-gate 	rpc_gss_OID	mechanism,
2570Sstevel@tonic-gate 	rpc_gss_service_t service_type,
2580Sstevel@tonic-gate 	uint_t	qop,
2590Sstevel@tonic-gate 	rpc_gss_options_req_t *options_req,
2600Sstevel@tonic-gate 	rpc_gss_options_ret_t *options_ret,
2610Sstevel@tonic-gate 	void *cache_key,
2620Sstevel@tonic-gate 	cred_t *cr,
2630Sstevel@tonic-gate 	AUTH **retauth)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	struct ga_cache_entry **head, *current, *new, *prev;
2660Sstevel@tonic-gate 	AUTH *auth = NULL;
2670Sstevel@tonic-gate 	rpc_gss_data	*ap;
2680Sstevel@tonic-gate 	rpc_gss_options_ret_t opt_ret;
2690Sstevel@tonic-gate 	int status = 0;
2700Sstevel@tonic-gate 	uid_t uid = crgetuid(cr);
2710Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (retauth == NULL)
2740Sstevel@tonic-gate 		return (EINVAL);
2750Sstevel@tonic-gate 	*retauth = NULL;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	NOT_NULL(cr);
2780Sstevel@tonic-gate 	IS_ALIGNED(cr);
2790Sstevel@tonic-gate #ifdef DEBUG
2800Sstevel@tonic-gate if (HASH(cache_key, uid) < 0) {
2810Sstevel@tonic-gate 	prom_printf("cache_key %p, cr %p\n", cache_key, (void *)cr);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate #endif
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/*
2860Sstevel@tonic-gate 	 *  Get a valid gss auth handle from the cache table.
2870Sstevel@tonic-gate 	 *  If auth in cache is invalid and not in use, destroy it.
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 	prev = NULL;
2900Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	ASSERT(HASH(cache_key, uid) >= 0);
2930Sstevel@tonic-gate 	head = &ga_cache_table[HASH(cache_key, uid)];
2940Sstevel@tonic-gate 	NOT_NULL(head);
2950Sstevel@tonic-gate 	IS_ALIGNED(head);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	for (current = *head; current; current = current->next) {
2980Sstevel@tonic-gate 		NOT_NULL(current);
2990Sstevel@tonic-gate 		IS_ALIGNED(current);
3000Sstevel@tonic-gate 		if ((cache_key == current->cache_key) &&
3010Sstevel@tonic-gate 			(uid == current->uid) && (zoneid == current->zoneid) &&
3020Sstevel@tonic-gate 			!current->in_use) {
3030Sstevel@tonic-gate 			current->in_use = TRUE;
3040Sstevel@tonic-gate 			current->ref_time = gethrestime_sec();
3050Sstevel@tonic-gate 			ap = AUTH_PRIVATE(current->auth);
3060Sstevel@tonic-gate 			ap->clnt = clnt;
3070Sstevel@tonic-gate 			ga_cache_hit++;
3080Sstevel@tonic-gate 			if (ap->invalid ||
3090Sstevel@tonic-gate 			    ((current->ctx_expired_time != GSS_C_INDEFINITE) &&
3106636Sgtb 			    (gethrestime_sec() >=
3110Sstevel@tonic-gate 			    current->ctx_expired_time))) {
3120Sstevel@tonic-gate 			    RPCGSS_LOG0(1, "NOTICE: rpc_gss_secget: time to "
3130Sstevel@tonic-gate 					"refresh the auth\n");
3140Sstevel@tonic-gate 			    if (prev == NULL) {
3150Sstevel@tonic-gate 				*head = current->next;
3160Sstevel@tonic-gate 			    } else {
3170Sstevel@tonic-gate 				prev->next = current->next;
3180Sstevel@tonic-gate 			    }
3190Sstevel@tonic-gate 			    rpc_gss_destroy(current->auth);
3200Sstevel@tonic-gate 			    kmem_cache_free(ga_cache_handle, (void *) current);
3210Sstevel@tonic-gate 			    auth = NULL;
3220Sstevel@tonic-gate 			} else {
3230Sstevel@tonic-gate 			    auth = current->auth;
3240Sstevel@tonic-gate 			}
3250Sstevel@tonic-gate 			break;
3260Sstevel@tonic-gate 		} else {
3270Sstevel@tonic-gate 			prev = current;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 *  If no valid gss auth handle can be found in the cache, create
3340Sstevel@tonic-gate 	 *  a new one.
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	if (!auth) {
3370Sstevel@tonic-gate 		ga_cache_miss++;
3380Sstevel@tonic-gate 		if (options_ret == NULL)
3390Sstevel@tonic-gate 			options_ret = &opt_ret;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		status = rpc_gss_seccreate(clnt, principal, mechanism,
3420Sstevel@tonic-gate 			service_type, qop, options_req, options_ret, cr, &auth);
3430Sstevel@tonic-gate 		if (status == 0) {
3440Sstevel@tonic-gate 			RPCGSS_LOG(2, "rpc_gss_secget: new auth %p\n",
3450Sstevel@tonic-gate 					(void *)auth);
3460Sstevel@tonic-gate 			new = kmem_cache_alloc(ga_cache_handle, KM_NOSLEEP);
3470Sstevel@tonic-gate 			IS_ALIGNED(new);
3480Sstevel@tonic-gate 			NOT_DEAD(new);
3490Sstevel@tonic-gate 			if (new) {
3500Sstevel@tonic-gate 				new->cache_key = cache_key;
3510Sstevel@tonic-gate 				new->uid = uid;
3520Sstevel@tonic-gate 				new->zoneid = zoneid;
3530Sstevel@tonic-gate 				new->in_use = TRUE;
3540Sstevel@tonic-gate 				new->ref_time = gethrestime_sec();
3550Sstevel@tonic-gate 				if (options_ret->time_ret != GSS_C_INDEFINITE) {
3560Sstevel@tonic-gate 				    new->ctx_expired_time = new->ref_time +
3570Sstevel@tonic-gate 					options_ret->time_ret;
3580Sstevel@tonic-gate 				} else {
3590Sstevel@tonic-gate 				    new->ctx_expired_time = GSS_C_INDEFINITE;
3600Sstevel@tonic-gate 				}
3610Sstevel@tonic-gate 				new->auth = auth;
3620Sstevel@tonic-gate 				rw_enter(&ga_cache_table_lock, RW_WRITER);
3630Sstevel@tonic-gate 				NOT_DEAD(*head);
3640Sstevel@tonic-gate 				NOT_DEAD(new->next);
3650Sstevel@tonic-gate 				new->next = *head;
3660Sstevel@tonic-gate 				*head = new;
3670Sstevel@tonic-gate 				rw_exit(&ga_cache_table_lock);
3680Sstevel@tonic-gate 			}
3690Sstevel@tonic-gate 			/* done with opt_ret */
3700Sstevel@tonic-gate 			if (options_ret == &opt_ret) {
3710Sstevel@tonic-gate 			    kgss_free_oid((gss_OID) opt_ret.actual_mechanism);
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	*retauth = auth;
3770Sstevel@tonic-gate 	return (status);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  *  rpc_gss_secfree will destroy a rpcsec_gss context only if
3840Sstevel@tonic-gate  *  the auth handle is not in the cache table.
3850Sstevel@tonic-gate  */
3860Sstevel@tonic-gate void
rpc_gss_secfree(AUTH * auth)3870Sstevel@tonic-gate rpc_gss_secfree(AUTH *auth)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	struct ga_cache_entry *next, *cur;
3900Sstevel@tonic-gate 	int i;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 *  Check the cache table to find the auth.
3940Sstevel@tonic-gate 	 *  Marked it unused.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
3970Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
3980Sstevel@tonic-gate 		for (cur = ga_cache_table[i]; cur; cur = next) {
3990Sstevel@tonic-gate 			NOT_DEAD(cur);
4000Sstevel@tonic-gate 			next = cur->next;
4010Sstevel@tonic-gate 			NOT_DEAD(next);
4020Sstevel@tonic-gate 			if (cur->auth == auth) {
4036403Sgt29601 				ASSERT(cur->in_use == TRUE);
4046403Sgt29601 				cur->in_use = FALSE;
4056403Sgt29601 				rw_exit(&ga_cache_table_lock);
4066403Sgt29601 				return;
4070Sstevel@tonic-gate 			}
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
4110Sstevel@tonic-gate 	RPCGSS_LOG(2, "rpc_gss_secfree: destroy auth %p\n", (void *)auth);
4120Sstevel@tonic-gate 	rpc_gss_destroy(auth);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  *  Create a gss security service context.
4180Sstevel@tonic-gate  */
4190Sstevel@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)4200Sstevel@tonic-gate rpc_gss_seccreate(CLIENT *clnt,
4210Sstevel@tonic-gate 	char			*principal,	/* target service@server */
4220Sstevel@tonic-gate 	rpc_gss_OID		mechanism,	/* security mechanism */
4230Sstevel@tonic-gate 	rpc_gss_service_t	service_type,	/* security service */
4240Sstevel@tonic-gate 	uint_t			qop,		/* requested QOP */
4250Sstevel@tonic-gate 	rpc_gss_options_req_t	*options_req,	/* requested options */
4260Sstevel@tonic-gate 	rpc_gss_options_ret_t	*options_ret,	/* returned options */
4270Sstevel@tonic-gate 	cred_t			*cr,		/* client's unix cred */
4280Sstevel@tonic-gate 	AUTH			**retauth)	/* auth handle */
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	OM_uint32		gssstat;
4310Sstevel@tonic-gate 	OM_uint32		minor_stat;
4320Sstevel@tonic-gate 	gss_name_t		target_name;
4330Sstevel@tonic-gate 	int			ret_flags;
4340Sstevel@tonic-gate 	OM_uint32		time_rec;
4350Sstevel@tonic-gate 	gss_buffer_desc		input_name;
4360Sstevel@tonic-gate 	AUTH			*auth = NULL;
4370Sstevel@tonic-gate 	rpc_gss_data		*ap = NULL;
4380Sstevel@tonic-gate 	int			error;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * convert name to GSS internal type
4420Sstevel@tonic-gate 	 */
4430Sstevel@tonic-gate 	input_name.value = principal;
4440Sstevel@tonic-gate 	input_name.length = strlen(principal);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	gssstat = gss_import_name(&minor_stat, &input_name,
4476403Sgt29601 	    (gss_OID)GSS_C_NT_HOSTBASED_SERVICE, &target_name);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (gssstat != GSS_S_COMPLETE) {
4500Sstevel@tonic-gate 		RPCGSS_LOG0(1,
4516403Sgt29601 		    "rpc_gss_seccreate: unable to import gss name\n");
4520Sstevel@tonic-gate 		return (ENOMEM);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * Create AUTH handle.  Save the necessary interface information
4570Sstevel@tonic-gate 	 * so that the client can refresh the handle later if needed.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	if ((auth = (AUTH *) kmem_alloc(sizeof (*auth), KM_SLEEP)) != NULL)
4600Sstevel@tonic-gate 		ap = (rpc_gss_data *) kmem_alloc(sizeof (*ap), KM_SLEEP);
4610Sstevel@tonic-gate 	if (auth == NULL || ap == NULL) {
4620Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_seccreate: out of memory\n");
4630Sstevel@tonic-gate 		if (auth != NULL)
4640Sstevel@tonic-gate 			kmem_free((char *)auth, sizeof (*auth));
4650Sstevel@tonic-gate 		(void) gss_release_name(&minor_stat, &target_name);
4660Sstevel@tonic-gate 		return (ENOMEM);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	bzero((char *)ap, sizeof (*ap));
4700Sstevel@tonic-gate 	ap->clnt = clnt;
4710Sstevel@tonic-gate 	ap->version = RPCSEC_GSS_VERSION;
4720Sstevel@tonic-gate 	if (options_req != NULL) {
4730Sstevel@tonic-gate 		ap->my_cred = options_req->my_cred;
4740Sstevel@tonic-gate 		ap->req_flags = options_req->req_flags;
4750Sstevel@tonic-gate 		ap->time_req = options_req->time_req;
4760Sstevel@tonic-gate 		ap->icb = options_req->input_channel_bindings;
4770Sstevel@tonic-gate 	} else {
4780Sstevel@tonic-gate 		ap->my_cred = GSS_C_NO_CREDENTIAL;
4790Sstevel@tonic-gate 		ap->req_flags = GSS_C_MUTUAL_FLAG;
4800Sstevel@tonic-gate 		ap->time_req = 0;
4810Sstevel@tonic-gate 		ap->icb = GSS_C_NO_CHANNEL_BINDINGS;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	if ((ap->service = service_type) == rpc_gss_svc_default)
4840Sstevel@tonic-gate 		ap->service = rpc_gss_svc_integrity;
4850Sstevel@tonic-gate 	ap->qop = qop;
4860Sstevel@tonic-gate 	ap->target_name = target_name;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	/*
4890Sstevel@tonic-gate 	 * Now invoke the real interface that sets up the context from
4900Sstevel@tonic-gate 	 * the information stashed away in the private data.
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	if (error = rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth, ap,
4936403Sgt29601 	    mechanism, &ap->mech_type, &ret_flags, &time_rec, cr, 0)) {
4940Sstevel@tonic-gate 		if (ap->target_name) {
4950Sstevel@tonic-gate 			(void) gss_release_name(&minor_stat, &ap->target_name);
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 		kmem_free((char *)ap, sizeof (*ap));
4980Sstevel@tonic-gate 		kmem_free((char *)auth, sizeof (*auth));
4990Sstevel@tonic-gate 		RPCGSS_LOG(1, "rpc_gss_seccreate: init context failed"
5006403Sgt29601 		    " errno=%d\n", error);
5010Sstevel@tonic-gate 		return (error);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/*
5050Sstevel@tonic-gate 	 * Make sure that the requested service is supported.  In all
5060Sstevel@tonic-gate 	 * cases, integrity service must be available.
5070Sstevel@tonic-gate 	 */
5080Sstevel@tonic-gate 	if ((ap->service == rpc_gss_svc_privacy &&
5096403Sgt29601 	    !(ret_flags & GSS_C_CONF_FLAG)) ||
5106403Sgt29601 	    !(ret_flags & GSS_C_INTEG_FLAG)) {
5110Sstevel@tonic-gate 		rpc_gss_destroy(auth);
5120Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_seccreate: service not supported\n");
5130Sstevel@tonic-gate 		return (EPROTONOSUPPORT);
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * return option values if requested
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	if (options_ret != NULL) {
5200Sstevel@tonic-gate 		options_ret->major_status = gssstat;
5210Sstevel@tonic-gate 		options_ret->minor_status = minor_stat;
5220Sstevel@tonic-gate 		options_ret->rpcsec_version = ap->version;
5230Sstevel@tonic-gate 		options_ret->ret_flags = ret_flags;
5240Sstevel@tonic-gate 		options_ret->time_ret = time_rec;
5250Sstevel@tonic-gate 		options_ret->gss_context = ap->context;
5260Sstevel@tonic-gate 		/*
5270Sstevel@tonic-gate 		 *  Caller's responsibility to free this.
5280Sstevel@tonic-gate 		 */
5290Sstevel@tonic-gate 		NOT_NULL(ap->mech_type);
5300Sstevel@tonic-gate 		__rpc_gss_dup_oid(ap->mech_type,
5316403Sgt29601 		    (gss_OID *)&options_ret->actual_mechanism);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	*retauth = auth;
5350Sstevel@tonic-gate 	return (0);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * Private interface to create a context.  This is the interface
5400Sstevel@tonic-gate  * that's invoked when the context has to be refreshed.
5410Sstevel@tonic-gate  */
5420Sstevel@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)5430Sstevel@tonic-gate rpc_gss_seccreate_pvt(gssstat, minor_stat, auth, ap, desired_mech_type,
5440Sstevel@tonic-gate 			actual_mech_type, ret_flags, time_rec, cr, isrefresh)
5450Sstevel@tonic-gate 	OM_uint32		*gssstat;
5460Sstevel@tonic-gate 	OM_uint32		*minor_stat;
5470Sstevel@tonic-gate 	AUTH			*auth;
5480Sstevel@tonic-gate 	rpc_gss_data		*ap;
5490Sstevel@tonic-gate 	gss_OID			desired_mech_type;
5500Sstevel@tonic-gate 	gss_OID			*actual_mech_type;
5510Sstevel@tonic-gate 	int			*ret_flags;
5520Sstevel@tonic-gate 	OM_uint32		*time_rec;
5530Sstevel@tonic-gate 	cred_t			*cr;
5540Sstevel@tonic-gate 	int			isrefresh;
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	CLIENT			*clnt = ap->clnt;
5570Sstevel@tonic-gate 	AUTH			*save_auth;
5580Sstevel@tonic-gate 	enum clnt_stat		callstat;
5590Sstevel@tonic-gate 	rpc_gss_init_arg	call_arg;
5600Sstevel@tonic-gate 	rpc_gss_init_res	call_res;
5610Sstevel@tonic-gate 	gss_buffer_desc		*input_token_p, input_token, process_token;
5620Sstevel@tonic-gate 	int 			free_results = 0;
5630Sstevel@tonic-gate 	k_sigset_t		smask;
5640Sstevel@tonic-gate 	int			error = 0;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * (re)initialize AUTH handle and private data.
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	bzero((char *)auth, sizeof (*auth));
5700Sstevel@tonic-gate 	auth->ah_ops = &rpc_gss_ops;
5710Sstevel@tonic-gate 	auth->ah_private = (caddr_t)ap;
5720Sstevel@tonic-gate 	auth->ah_cred.oa_flavor = RPCSEC_GSS;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	ap->established = FALSE;
5750Sstevel@tonic-gate 	ap->ctx_handle.length = 0;
5760Sstevel@tonic-gate 	ap->ctx_handle.value = NULL;
5770Sstevel@tonic-gate 	ap->context = NULL;
5780Sstevel@tonic-gate 	ap->seq_num = 0;
5790Sstevel@tonic-gate 	ap->gss_proc = RPCSEC_GSS_INIT;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * should not change clnt->cl_auth at this time, so save
5830Sstevel@tonic-gate 	 * old handle
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	save_auth = clnt->cl_auth;
5860Sstevel@tonic-gate 	clnt->cl_auth = auth;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * set state for starting context setup
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	bzero((char *)&call_arg, sizeof (call_arg));
5920Sstevel@tonic-gate 	input_token_p = GSS_C_NO_BUFFER;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate next_token:
5950Sstevel@tonic-gate 	*gssstat = kgss_init_sec_context(minor_stat,
5960Sstevel@tonic-gate 					ap->my_cred,
5970Sstevel@tonic-gate 					&ap->context,
5980Sstevel@tonic-gate 					ap->target_name,
5990Sstevel@tonic-gate 					desired_mech_type,
6000Sstevel@tonic-gate 					ap->req_flags,
6010Sstevel@tonic-gate 					ap->time_req,
6020Sstevel@tonic-gate 					NULL,
6030Sstevel@tonic-gate 					input_token_p,
6040Sstevel@tonic-gate 					actual_mech_type,
6050Sstevel@tonic-gate 					&call_arg,
6060Sstevel@tonic-gate 					ret_flags,
6070Sstevel@tonic-gate 					time_rec,
6080Sstevel@tonic-gate 					crgetuid(cr));
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (input_token_p != GSS_C_NO_BUFFER) {
6110Sstevel@tonic-gate 		OM_uint32 minor_stat2;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat2, input_token_p);
6140Sstevel@tonic-gate 		input_token_p = GSS_C_NO_BUFFER;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	if (*gssstat != GSS_S_COMPLETE && *gssstat != GSS_S_CONTINUE_NEEDED) {
6180Sstevel@tonic-gate 		rpc_gss_display_status(*gssstat, *minor_stat,
6190Sstevel@tonic-gate 			desired_mech_type, crgetuid(cr),
6200Sstevel@tonic-gate 			"rpcsec_gss_secreate_pvt:gss_init_sec_context");
6210Sstevel@tonic-gate 		error = EACCES;
6220Sstevel@tonic-gate 		goto cleanup;
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * if we got a token, pass it on
6270Sstevel@tonic-gate 	 */
6280Sstevel@tonic-gate 	if (call_arg.length != 0) {
6290Sstevel@tonic-gate 		struct timeval timeout = {30, 0};
6300Sstevel@tonic-gate 		int	 rpcsec_retry = isrefresh ?
6310Sstevel@tonic-gate 			RPCSEC_GSS_REFRESH_ATTEMPTS : 1;
6320Sstevel@tonic-gate 		uint32_t oldxid;
6330Sstevel@tonic-gate 		uint32_t zeroxid = 0;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		bzero((char *)&call_res, sizeof (call_res));
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		(void) CLNT_CONTROL(clnt, CLGET_XID, (char *)&oldxid);
6380Sstevel@tonic-gate 		(void) CLNT_CONTROL(clnt, CLSET_XID, (char *)&zeroxid);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		while (rpcsec_retry > 0) {
6420Sstevel@tonic-gate 			struct rpc_err rpcerr;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 			sigintr(&smask, INTERRUPT_OK);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 			callstat = clnt_call(clnt, NULLPROC,
6470Sstevel@tonic-gate 				__xdr_rpc_gss_init_arg, (caddr_t)&call_arg,
6480Sstevel@tonic-gate 				__xdr_rpc_gss_init_res, (caddr_t)&call_res,
6490Sstevel@tonic-gate 				timeout);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 			sigunintr(&smask);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 			if (callstat == RPC_SUCCESS) {
6540Sstevel@tonic-gate 				error = 0;
6550Sstevel@tonic-gate 				if (isrefresh &&
6560Sstevel@tonic-gate 				    call_res.gss_major == GSS_S_FAILURE) {
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 					clock_t one_sec = drv_usectohz(1000000);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 					rpcsec_retry--;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 					/*
6630Sstevel@tonic-gate 					 * Pause a little and try again.
6640Sstevel@tonic-gate 					 */
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 					if (clnt->cl_nosignal == TRUE) {
6670Sstevel@tonic-gate 						delay(one_sec);
6680Sstevel@tonic-gate 					} else {
6690Sstevel@tonic-gate 						if (delay_sig(one_sec)) {
6700Sstevel@tonic-gate 							error = EINTR;
6710Sstevel@tonic-gate 							break;
6720Sstevel@tonic-gate 						}
6730Sstevel@tonic-gate 					}
6740Sstevel@tonic-gate 					continue;
6750Sstevel@tonic-gate 				}
6760Sstevel@tonic-gate 				break;
6770Sstevel@tonic-gate 			}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			if (callstat == RPC_TIMEDOUT) {
6800Sstevel@tonic-gate 				error = ETIMEDOUT;
6810Sstevel@tonic-gate 				break;
6820Sstevel@tonic-gate 			}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 			if (callstat == RPC_XPRTFAILED) {
6850Sstevel@tonic-gate 				error = ECONNRESET;
6860Sstevel@tonic-gate 				break;
6870Sstevel@tonic-gate 			}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 			if (callstat == RPC_INTR) {
6900Sstevel@tonic-gate 				error = EINTR;
6910Sstevel@tonic-gate 				break;
6920Sstevel@tonic-gate 			}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 			if (callstat == RPC_INPROGRESS) {
6950Sstevel@tonic-gate 				continue;
6960Sstevel@tonic-gate 			}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 			clnt_geterr(clnt, &rpcerr);
6990Sstevel@tonic-gate 			error = rpcerr.re_errno;
7000Sstevel@tonic-gate 			break;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		(void) CLNT_CONTROL(clnt, CLSET_XID, (char *)&oldxid);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &call_arg);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 		if (callstat != RPC_SUCCESS) {
7080Sstevel@tonic-gate 			RPCGSS_LOG(1,
7090Sstevel@tonic-gate 			    "rpc_gss_seccreate_pvt: clnt_call failed %d\n",
7100Sstevel@tonic-gate 			    callstat);
7110Sstevel@tonic-gate 			goto cleanup;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		/*
7150Sstevel@tonic-gate 		 * we have results - note that these need to be freed
7160Sstevel@tonic-gate 		 */
7170Sstevel@tonic-gate 		free_results = 1;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		if ((call_res.gss_major != GSS_S_COMPLETE) &&
7200Sstevel@tonic-gate 		    (call_res.gss_major != GSS_S_CONTINUE_NEEDED)) {
7210Sstevel@tonic-gate 			RPCGSS_LOG1(1, "rpc_gss_seccreate_pvt: "
7220Sstevel@tonic-gate 				"call_res gss_major %x, gss_minor %x\n",
7230Sstevel@tonic-gate 				call_res.gss_major, call_res.gss_minor);
7240Sstevel@tonic-gate 			error = EACCES;
7250Sstevel@tonic-gate 			goto cleanup;
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		ap->gss_proc = RPCSEC_GSS_CONTINUE_INIT;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		/*
7310Sstevel@tonic-gate 		 * check for ctx_handle
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		if (ap->ctx_handle.length == 0) {
7340Sstevel@tonic-gate 			if (call_res.ctx_handle.length == 0) {
7350Sstevel@tonic-gate 				RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt: zero "
7360Sstevel@tonic-gate 					"length handle in response\n");
7370Sstevel@tonic-gate 				error = EACCES;
7380Sstevel@tonic-gate 				goto cleanup;
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 			GSS_DUP_BUFFER(ap->ctx_handle,
7410Sstevel@tonic-gate 					call_res.ctx_handle);
7420Sstevel@tonic-gate 		} else if (!GSS_BUFFERS_EQUAL(ap->ctx_handle,
7430Sstevel@tonic-gate 						call_res.ctx_handle)) {
7440Sstevel@tonic-gate 			RPCGSS_LOG0(1,
7450Sstevel@tonic-gate 			"rpc_gss_seccreate_pvt: ctx_handle not the same\n");
7460Sstevel@tonic-gate 			error = EACCES;
7470Sstevel@tonic-gate 			goto cleanup;
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		/*
7510Sstevel@tonic-gate 		 * check for token
7520Sstevel@tonic-gate 		 */
7530Sstevel@tonic-gate 		if (call_res.token.length != 0) {
7540Sstevel@tonic-gate 			if (*gssstat == GSS_S_COMPLETE) {
7550Sstevel@tonic-gate 				RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt: non "
7560Sstevel@tonic-gate 					"zero length token in response, but "
7570Sstevel@tonic-gate 					"gsstat == GSS_S_COMPLETE\n");
7580Sstevel@tonic-gate 				error = EACCES;
7590Sstevel@tonic-gate 				goto cleanup;
7600Sstevel@tonic-gate 			}
7610Sstevel@tonic-gate 			GSS_DUP_BUFFER(input_token, call_res.token);
7620Sstevel@tonic-gate 			input_token_p = &input_token;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		} else if (*gssstat != GSS_S_COMPLETE) {
7650Sstevel@tonic-gate 			RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt:zero length "
7660Sstevel@tonic-gate 				"token in response, but "
7670Sstevel@tonic-gate 				"gsstat != GSS_S_COMPLETE\n");
7680Sstevel@tonic-gate 			error = EACCES;
7690Sstevel@tonic-gate 			goto cleanup;
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		/* save the sequence window value; validate later */
7730Sstevel@tonic-gate 		ap->seq_window = call_res.seq_window;
7740Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res);
7750Sstevel@tonic-gate 		free_results = 0;
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * results were okay.. continue if necessary
7800Sstevel@tonic-gate 	 */
7810Sstevel@tonic-gate 	if (*gssstat == GSS_S_CONTINUE_NEEDED) {
7820Sstevel@tonic-gate 		goto next_token;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * Context is established. Now use kgss_export_sec_context and
7870Sstevel@tonic-gate 	 * kgss_import_sec_context to transfer the context from the user
7880Sstevel@tonic-gate 	 * land to kernel if the mechanism specific kernel module is
7890Sstevel@tonic-gate 	 * available.
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	*gssstat  = kgss_export_sec_context(minor_stat, ap->context,
7920Sstevel@tonic-gate 						&process_token);
7930Sstevel@tonic-gate 	if (*gssstat == GSS_S_NAME_NOT_MN) {
7940Sstevel@tonic-gate 		RPCGSS_LOG(2, "rpc_gss_seccreate_pvt: export_sec_context "
7950Sstevel@tonic-gate 			"Kernel Module unavailable  gssstat = 0x%x\n",
7960Sstevel@tonic-gate 			*gssstat);
7970Sstevel@tonic-gate 		goto done;
7980Sstevel@tonic-gate 	} else if (*gssstat != GSS_S_COMPLETE) {
7990Sstevel@tonic-gate 		(void) rpc_gss_display_status(*gssstat, *minor_stat,
80075Srg137905 			isrefresh ? GSS_C_NULL_OID : *actual_mech_type,
80175Srg137905 					crgetuid(cr),
8020Sstevel@tonic-gate 			"rpcsec_gss_secreate_pvt:gss_export_sec_context");
8030Sstevel@tonic-gate 		(void) kgss_delete_sec_context(minor_stat,
8040Sstevel@tonic-gate 					&ap->context, NULL);
8050Sstevel@tonic-gate 		error = EACCES;
8060Sstevel@tonic-gate 		goto cleanup;
8070Sstevel@tonic-gate 	} else if (process_token.length == 0) {
8080Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_seccreate_pvt:zero length "
8090Sstevel@tonic-gate 				"token in response for export_sec_context, but "
8100Sstevel@tonic-gate 				"gsstat == GSS_S_COMPLETE\n");
8110Sstevel@tonic-gate 		(void) kgss_delete_sec_context(minor_stat,
8120Sstevel@tonic-gate 					&ap->context, NULL);
8130Sstevel@tonic-gate 		error = EACCES;
8140Sstevel@tonic-gate 		goto cleanup;
8150Sstevel@tonic-gate 	} else
8160Sstevel@tonic-gate 		*gssstat = kgss_import_sec_context(minor_stat, &process_token,
8170Sstevel@tonic-gate 							ap->context);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (*gssstat == GSS_S_COMPLETE) {
8200Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &process_token);
8210Sstevel@tonic-gate 	} else {
8220Sstevel@tonic-gate 		rpc_gss_display_status(*gssstat, *minor_stat,
8230Sstevel@tonic-gate 			desired_mech_type, crgetuid(cr),
8240Sstevel@tonic-gate 			"rpcsec_gss_secreate_pvt:gss_import_sec_context");
8250Sstevel@tonic-gate 		(void) kgss_delete_sec_context(minor_stat,
8260Sstevel@tonic-gate 					&ap->context, NULL);
8270Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &process_token);
8280Sstevel@tonic-gate 		error = EACCES;
8290Sstevel@tonic-gate 		goto cleanup;
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate done:
8330Sstevel@tonic-gate 	/*
8340Sstevel@tonic-gate 	 * Validate the sequence window - RFC 2203 section 5.2.3.1
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	if (!validate_seqwin(ap)) {
8370Sstevel@tonic-gate 		error = EACCES;
8380Sstevel@tonic-gate 		goto cleanup;
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * Done!  Security context creation is successful.
8430Sstevel@tonic-gate 	 * Ready for exchanging data.
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	ap->established = TRUE;
8460Sstevel@tonic-gate 	ap->seq_num = 1;
8470Sstevel@tonic-gate 	ap->gss_proc = RPCSEC_GSS_DATA;
8480Sstevel@tonic-gate 	ap->invalid = FALSE;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	clnt->cl_auth = save_auth;	/* restore cl_auth */
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	return (0);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate cleanup:
8550Sstevel@tonic-gate 	if (free_results)
8560Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res);
8570Sstevel@tonic-gate 	clnt->cl_auth = save_auth;	/* restore cl_auth */
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * If need to retry for AUTH_REFRESH, do not cleanup the
8610Sstevel@tonic-gate 	 * auth private data.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	if (isrefresh && (error == ETIMEDOUT || error == ECONNRESET)) {
8640Sstevel@tonic-gate 		return (error);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if (ap->context != NULL) {
8680Sstevel@tonic-gate 		rpc_gss_free_pvt(auth);
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	return (error? error : EACCES);
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate  * Marshall credentials.
8760Sstevel@tonic-gate  */
8770Sstevel@tonic-gate static bool_t
marshall_creds(ap,xdrs,cred_buf_len)8780Sstevel@tonic-gate marshall_creds(ap, xdrs, cred_buf_len)
8790Sstevel@tonic-gate 	rpc_gss_data		*ap;
8800Sstevel@tonic-gate 	XDR			*xdrs;
8810Sstevel@tonic-gate 	uint_t			cred_buf_len;
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate 	rpc_gss_creds		ag_creds;
8840Sstevel@tonic-gate 	char			*cred_buf;
8850Sstevel@tonic-gate 	struct opaque_auth	creds;
8860Sstevel@tonic-gate 	XDR			cred_xdrs;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	ag_creds.version = ap->version;
8890Sstevel@tonic-gate 	ag_creds.gss_proc = ap->gss_proc;
8900Sstevel@tonic-gate 	ag_creds.seq_num = ap->seq_num;
8910Sstevel@tonic-gate 	ag_creds.service = ap->service;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	/*
8940Sstevel@tonic-gate 	 * If context has not been set up yet, use NULL handle.
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	if (ap->ctx_handle.length > 0)
8970Sstevel@tonic-gate 		ag_creds.ctx_handle = ap->ctx_handle;
8980Sstevel@tonic-gate 	else {
8990Sstevel@tonic-gate 		ag_creds.ctx_handle.length = 0;
9000Sstevel@tonic-gate 		ag_creds.ctx_handle.value = NULL;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	cred_buf = kmem_alloc(cred_buf_len, KM_SLEEP);
9040Sstevel@tonic-gate 	xdrmem_create(&cred_xdrs, (caddr_t)cred_buf, cred_buf_len,
9050Sstevel@tonic-gate 								XDR_ENCODE);
9060Sstevel@tonic-gate 	if (!__xdr_rpc_gss_creds(&cred_xdrs, &ag_creds)) {
9070Sstevel@tonic-gate 		kmem_free(cred_buf, MAX_AUTH_BYTES);
9080Sstevel@tonic-gate 		XDR_DESTROY(&cred_xdrs);
9090Sstevel@tonic-gate 		return (FALSE);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	creds.oa_flavor = RPCSEC_GSS;
9130Sstevel@tonic-gate 	creds.oa_base = cred_buf;
9140Sstevel@tonic-gate 	creds.oa_length = xdr_getpos(&cred_xdrs);
9150Sstevel@tonic-gate 	XDR_DESTROY(&cred_xdrs);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (!xdr_opaque_auth(xdrs, &creds)) {
9180Sstevel@tonic-gate 		kmem_free(cred_buf, cred_buf_len);
9190Sstevel@tonic-gate 		return (FALSE);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	kmem_free(cred_buf, cred_buf_len);
9230Sstevel@tonic-gate 	return (TRUE);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate  * Marshall verifier.  The verifier is the checksum of the RPC header
9280Sstevel@tonic-gate  * up to and including the credential field.  The XDR handle that's
9290Sstevel@tonic-gate  * passed in has the header up to and including the credential field
9300Sstevel@tonic-gate  * encoded.  A pointer to the transmit buffer is also passed in.
9310Sstevel@tonic-gate  */
9320Sstevel@tonic-gate static bool_t
marshall_verf(ap,xdrs,buf)9330Sstevel@tonic-gate marshall_verf(ap, xdrs, buf)
9340Sstevel@tonic-gate 	rpc_gss_data		*ap;
9350Sstevel@tonic-gate 	XDR			*xdrs;	/* send XDR */
9360Sstevel@tonic-gate 	char			*buf;	/* pointer of send buffer */
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	struct opaque_auth	verf;
9390Sstevel@tonic-gate 	OM_uint32		major, minor;
9400Sstevel@tonic-gate 	gss_buffer_desc		in_buf, out_buf;
9410Sstevel@tonic-gate 	bool_t			ret = FALSE;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * If context is not established yet, use NULL verifier.
9450Sstevel@tonic-gate 	 */
9460Sstevel@tonic-gate 	if (!ap->established) {
9470Sstevel@tonic-gate 		verf.oa_flavor = AUTH_NONE;
9480Sstevel@tonic-gate 		verf.oa_base = NULL;
9490Sstevel@tonic-gate 		verf.oa_length = 0;
9500Sstevel@tonic-gate 		return (xdr_opaque_auth(xdrs, &verf));
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	verf.oa_flavor = RPCSEC_GSS;
9540Sstevel@tonic-gate 	in_buf.length = xdr_getpos(xdrs);
9550Sstevel@tonic-gate 	in_buf.value = buf;
9560Sstevel@tonic-gate 	if ((major = kgss_sign(&minor, ap->context, ap->qop, &in_buf,
9570Sstevel@tonic-gate 				&out_buf)) != GSS_S_COMPLETE) {
9580Sstevel@tonic-gate 		if (major == GSS_S_CONTEXT_EXPIRED) {
9590Sstevel@tonic-gate 			ap->invalid = TRUE;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 		RPCGSS_LOG1(1,
9620Sstevel@tonic-gate 		    "marshall_verf: kgss_sign failed GSS Major %x Minor %x\n",
9630Sstevel@tonic-gate 		    major, minor);
9640Sstevel@tonic-gate 		return (FALSE);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 	verf.oa_base = out_buf.value;
9670Sstevel@tonic-gate 	verf.oa_length = out_buf.length;
9680Sstevel@tonic-gate 	ret = xdr_opaque_auth(xdrs, &verf);
9690Sstevel@tonic-gate 	(void) gss_release_buffer(&minor, &out_buf);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	return (ret);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate /*
9750Sstevel@tonic-gate  * Validate sequence window upon a successful RPCSEC_GSS INIT session.
9760Sstevel@tonic-gate  * The sequence window sent back by the server should be verifiable by
9770Sstevel@tonic-gate  * the verifier which is a checksum of the sequence window.
9780Sstevel@tonic-gate  */
9790Sstevel@tonic-gate static bool_t
validate_seqwin(rpc_gss_data * ap)9800Sstevel@tonic-gate validate_seqwin(rpc_gss_data *ap)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	uint_t			seq_win_net;
9830Sstevel@tonic-gate 	OM_uint32		major = 0, minor = 0;
9840Sstevel@tonic-gate 	gss_buffer_desc		msg_buf, tok_buf;
9850Sstevel@tonic-gate 	int			qop_state = 0;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	ASSERT(ap->verifier);
9880Sstevel@tonic-gate 	ASSERT(ap->context);
9890Sstevel@tonic-gate 	seq_win_net = (uint_t)htonl(ap->seq_window);
9900Sstevel@tonic-gate 	msg_buf.length = sizeof (seq_win_net);
9910Sstevel@tonic-gate 	msg_buf.value = (char *)&seq_win_net;
9920Sstevel@tonic-gate 	tok_buf.length = ap->verifier->oa_length;
9930Sstevel@tonic-gate 	tok_buf.value = ap->verifier->oa_base;
9940Sstevel@tonic-gate 	major = kgss_verify(&minor, ap->context, &msg_buf, &tok_buf,
9956403Sgt29601 	    &qop_state);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
9986403Sgt29601 		RPCGSS_LOG1(1,
9996403Sgt29601 		    "validate_seqwin: kgss_verify failed GSS Major "
10006403Sgt29601 		    "%x Minor %x\n", major, minor);
10016403Sgt29601 		RPCGSS_LOG1(1, "seq_window %d, verf len %d ", ap->seq_window,
10026403Sgt29601 		    ap->verifier->oa_length);
10036403Sgt29601 		return (FALSE);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 	return (TRUE);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate /*
10090Sstevel@tonic-gate  * Validate RPC response verifier from server.  The response verifier
10100Sstevel@tonic-gate  * is the checksum of the request sequence number.
10110Sstevel@tonic-gate  */
10120Sstevel@tonic-gate static bool_t
rpc_gss_validate(auth,verf)10130Sstevel@tonic-gate rpc_gss_validate(auth, verf)
10140Sstevel@tonic-gate 	AUTH			*auth;
10150Sstevel@tonic-gate 	struct opaque_auth	*verf;
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
10180Sstevel@tonic-gate 	uint_t			seq_num_net;
10190Sstevel@tonic-gate 	OM_uint32		major, minor;
10200Sstevel@tonic-gate 	gss_buffer_desc		msg_buf, tok_buf;
10210Sstevel@tonic-gate 	int			qop_state;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	/*
10240Sstevel@tonic-gate 	 * If context is not established yet, save the verifier for
10250Sstevel@tonic-gate 	 * validating the sequence window later at the end of context
10260Sstevel@tonic-gate 	 * creation session.
10270Sstevel@tonic-gate 	 */
10280Sstevel@tonic-gate 	if (!ap->established) {
10290Sstevel@tonic-gate 	    if (ap->verifier == NULL) {
10300Sstevel@tonic-gate 		ap->verifier = kmem_zalloc(sizeof (struct opaque_auth),
10310Sstevel@tonic-gate 						KM_SLEEP);
10320Sstevel@tonic-gate 		if (verf->oa_length > 0)
10330Sstevel@tonic-gate 		    ap->verifier->oa_base = kmem_zalloc(verf->oa_length,
10340Sstevel@tonic-gate 						KM_SLEEP);
10350Sstevel@tonic-gate 	    } else {
10360Sstevel@tonic-gate 		if (ap->verifier->oa_length > 0)
10370Sstevel@tonic-gate 		    kmem_free(ap->verifier->oa_base, ap->verifier->oa_length);
10380Sstevel@tonic-gate 		if (verf->oa_length > 0)
10390Sstevel@tonic-gate 		    ap->verifier->oa_base = kmem_zalloc(verf->oa_length,
10400Sstevel@tonic-gate 						KM_SLEEP);
10410Sstevel@tonic-gate 	    }
10420Sstevel@tonic-gate 	    ap->verifier->oa_length = verf->oa_length;
10430Sstevel@tonic-gate 	    bcopy(verf->oa_base, ap->verifier->oa_base, verf->oa_length);
10440Sstevel@tonic-gate 	    return (TRUE);
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	seq_num_net = (uint_t)htonl(ap->seq_num);
10480Sstevel@tonic-gate 	msg_buf.length = sizeof (seq_num_net);
10490Sstevel@tonic-gate 	msg_buf.value = (char *)&seq_num_net;
10500Sstevel@tonic-gate 	tok_buf.length = verf->oa_length;
10510Sstevel@tonic-gate 	tok_buf.value = verf->oa_base;
10520Sstevel@tonic-gate 	major = kgss_verify(&minor, ap->context, &msg_buf, &tok_buf,
10530Sstevel@tonic-gate 				&qop_state);
10540Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
10550Sstevel@tonic-gate 		RPCGSS_LOG1(1,
10560Sstevel@tonic-gate 		"rpc_gss_validate: kgss_verify failed GSS Major %x Minor %x\n",
10570Sstevel@tonic-gate 		major, minor);
10580Sstevel@tonic-gate 		return (FALSE);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 	return (TRUE);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /*
10640Sstevel@tonic-gate  * Refresh client context.  This is necessary sometimes because the
10650Sstevel@tonic-gate  * server will ocassionally destroy contexts based on LRU method, or
10660Sstevel@tonic-gate  * because of expired credentials.
10670Sstevel@tonic-gate  */
10680Sstevel@tonic-gate static bool_t
rpc_gss_refresh(auth,msg,cr)10690Sstevel@tonic-gate rpc_gss_refresh(auth, msg, cr)
10700Sstevel@tonic-gate 	AUTH		*auth;
10710Sstevel@tonic-gate 	struct rpc_msg	*msg;
10720Sstevel@tonic-gate 	cred_t		*cr;
10730Sstevel@tonic-gate {
10740Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
10750Sstevel@tonic-gate 	gss_ctx_id_t	ctx_sav = NULL;
10760Sstevel@tonic-gate 	gss_buffer_desc	ctx_hdle_sav = {0, NULL};
10770Sstevel@tonic-gate 	uint_t		sn_sav, proc_sav;
10780Sstevel@tonic-gate 	bool_t		est_sav;
10790Sstevel@tonic-gate 	OM_uint32	gssstat, minor_stat;
10800Sstevel@tonic-gate 	int error;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * The context needs to be recreated only when the error status
10840Sstevel@tonic-gate 	 * returned from the server is one of the following:
10850Sstevel@tonic-gate 	 *	RPCSEC_GSS_NOCRED and RPCSEC_GSS_FAILED
10860Sstevel@tonic-gate 	 * The existing context should not be destroyed unless the above
10870Sstevel@tonic-gate 	 * error status codes are received or if the context has not
10880Sstevel@tonic-gate 	 * been set up.
10890Sstevel@tonic-gate 	 */
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	if (msg->rjcted_rply.rj_why == RPCSEC_GSS_NOCRED ||
10920Sstevel@tonic-gate 			msg->rjcted_rply.rj_why == RPCSEC_GSS_FAILED ||
10930Sstevel@tonic-gate 							!ap->established) {
10940Sstevel@tonic-gate 		/*
10950Sstevel@tonic-gate 		 * Destroy the context if necessary.  Use the same memory
10960Sstevel@tonic-gate 		 * for the new context since we've already passed a pointer
10970Sstevel@tonic-gate 		 * to it to the user.
10980Sstevel@tonic-gate 		 */
10990Sstevel@tonic-gate 		if (ap->context != NULL) {
11000Sstevel@tonic-gate 			ctx_sav = ap->context;
11010Sstevel@tonic-gate 			ap->context = NULL;
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 		if (ap->ctx_handle.length != 0) {
11040Sstevel@tonic-gate 			ctx_hdle_sav.length = ap->ctx_handle.length;
11050Sstevel@tonic-gate 			ctx_hdle_sav.value = ap->ctx_handle.value;
11060Sstevel@tonic-gate 			ap->ctx_handle.length = 0;
11070Sstevel@tonic-gate 			ap->ctx_handle.value = NULL;
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		/*
11110Sstevel@tonic-gate 		 * If the context was not already established, don't try to
11120Sstevel@tonic-gate 		 * recreate it.
11130Sstevel@tonic-gate 		 */
11140Sstevel@tonic-gate 		if (!ap->established) {
11150Sstevel@tonic-gate 			ap->invalid = TRUE;
11160Sstevel@tonic-gate 			RPCGSS_LOG0(1,
11170Sstevel@tonic-gate 			"rpc_gss_refresh: context was not established\n");
11180Sstevel@tonic-gate 			error = EINVAL;
11190Sstevel@tonic-gate 			goto out;
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		est_sav = ap->established;
11230Sstevel@tonic-gate 		sn_sav = ap->seq_num;
11240Sstevel@tonic-gate 		proc_sav = ap->gss_proc;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		/*
11270Sstevel@tonic-gate 		 * Recreate context.
11280Sstevel@tonic-gate 		 */
11290Sstevel@tonic-gate 		error = rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth,
11300Sstevel@tonic-gate 				ap, ap->mech_type, (gss_OID *)NULL, (int *)NULL,
11310Sstevel@tonic-gate 				(OM_uint32 *)NULL, cr, 1);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 		switch (error) {
11340Sstevel@tonic-gate 		case 0:
11350Sstevel@tonic-gate 			RPCGSS_LOG(1,
11360Sstevel@tonic-gate 			"rpc_gss_refresh: auth %p refreshed\n", (void *)auth);
11370Sstevel@tonic-gate 			goto out;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 		case ETIMEDOUT:
11400Sstevel@tonic-gate 		case ECONNRESET:
11410Sstevel@tonic-gate 			RPCGSS_LOG0(1, "rpc_gss_refresh: try again\n");
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 			if (ap->context != NULL) {
11440Sstevel@tonic-gate 			    (void) kgss_delete_sec_context(&minor_stat,
11450Sstevel@tonic-gate 					&ap->context, NULL);
11460Sstevel@tonic-gate 			}
11470Sstevel@tonic-gate 			if (ap->ctx_handle.length != 0) {
11480Sstevel@tonic-gate 			    (void) gss_release_buffer(&minor_stat,
11490Sstevel@tonic-gate 					&ap->ctx_handle);
11500Sstevel@tonic-gate 			}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 			/*
11530Sstevel@tonic-gate 			 * Restore the original value for the caller to
11540Sstevel@tonic-gate 			 * try again later.
11550Sstevel@tonic-gate 			 */
11560Sstevel@tonic-gate 			ap->context = ctx_sav;
11570Sstevel@tonic-gate 			ap->ctx_handle.length = ctx_hdle_sav.length;
11580Sstevel@tonic-gate 			ap->ctx_handle.value = ctx_hdle_sav.value;
11590Sstevel@tonic-gate 			ap->established = est_sav;
11600Sstevel@tonic-gate 			ap->seq_num = sn_sav;
11610Sstevel@tonic-gate 			ap->gss_proc = proc_sav;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 			return (FALSE);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 		default:
11660Sstevel@tonic-gate 			ap->invalid = TRUE;
11670Sstevel@tonic-gate 			RPCGSS_LOG(1, "rpc_gss_refresh: can't refresh this "
11680Sstevel@tonic-gate 				"auth, error=%d\n", error);
11690Sstevel@tonic-gate 			goto out;
11700Sstevel@tonic-gate 		}
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 	RPCGSS_LOG0(1, "rpc_gss_refresh: don't refresh");
11730Sstevel@tonic-gate 	return (FALSE);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate out:
11760Sstevel@tonic-gate 	if (ctx_sav != NULL) {
11770Sstevel@tonic-gate 		(void) kgss_delete_sec_context(&minor_stat,
11780Sstevel@tonic-gate 				&ctx_sav, NULL);
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 	if (ctx_hdle_sav.length != 0) {
11810Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &ctx_hdle_sav);
11820Sstevel@tonic-gate 	}
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	return (error == 0);
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate /*
11880Sstevel@tonic-gate  * Destroy a context.
11890Sstevel@tonic-gate  */
11900Sstevel@tonic-gate static void
rpc_gss_destroy(auth)11910Sstevel@tonic-gate rpc_gss_destroy(auth)
11920Sstevel@tonic-gate 	AUTH		*auth;
11930Sstevel@tonic-gate {
11940Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/*
11970Sstevel@tonic-gate 	 *  XXX Currently, we do not ping the server (rpc_gss_destroy_pvt)
11980Sstevel@tonic-gate 	 *  to destroy the context in the server cache.
11990Sstevel@tonic-gate 	 *  We assume there is a good LRU/aging mechanism for the
12000Sstevel@tonic-gate 	 *  context cache on the server side.
12010Sstevel@tonic-gate 	 */
12020Sstevel@tonic-gate 	rpc_gss_free_pvt(auth);
12030Sstevel@tonic-gate 	kmem_free((char *)ap, sizeof (*ap));
12040Sstevel@tonic-gate 	kmem_free(auth, sizeof (*auth));
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate /*
12080Sstevel@tonic-gate  * Private interface to free memory allocated in the rpcsec_gss private
12090Sstevel@tonic-gate  * data structure (rpc_gss_data).
12100Sstevel@tonic-gate  */
12110Sstevel@tonic-gate static void
rpc_gss_free_pvt(auth)12120Sstevel@tonic-gate rpc_gss_free_pvt(auth)
12130Sstevel@tonic-gate 	AUTH		*auth;
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	OM_uint32	minor_stat;
12160Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (ap->ctx_handle.length != 0) {
12190Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &ap->ctx_handle);
12200Sstevel@tonic-gate 		ap->ctx_handle.length = 0;
12210Sstevel@tonic-gate 		ap->ctx_handle.value = NULL;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * Destroy local GSS context.
12260Sstevel@tonic-gate 	 */
12270Sstevel@tonic-gate 	if (ap->context != NULL) {
12280Sstevel@tonic-gate 		(void) kgss_delete_sec_context(&minor_stat, &ap->context, NULL);
12290Sstevel@tonic-gate 		ap->context = NULL;
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	/*
12330Sstevel@tonic-gate 	 * Looks like we need to release default credentials if we use it.
12340Sstevel@tonic-gate 	 * Non-default creds need to be released by user.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	if (ap->my_cred == GSS_C_NO_CREDENTIAL)
12370Sstevel@tonic-gate 		(void) kgss_release_cred(&minor_stat, &ap->my_cred,
12380Sstevel@tonic-gate 					crgetuid(CRED()));
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	/*
12410Sstevel@tonic-gate 	 * Release any internal name structures.
12420Sstevel@tonic-gate 	 */
12430Sstevel@tonic-gate 	if (ap->target_name != NULL) {
12440Sstevel@tonic-gate 		(void) gss_release_name(&minor_stat, &ap->target_name);
12450Sstevel@tonic-gate 		ap->target_name = NULL;
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/*
12490Sstevel@tonic-gate 	 * Free mech_type oid structure.
12500Sstevel@tonic-gate 	 */
12510Sstevel@tonic-gate 	if (ap->mech_type != NULL) {
12520Sstevel@tonic-gate 		kgss_free_oid(ap->mech_type);
12530Sstevel@tonic-gate 		ap->mech_type = NULL;
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	/*
12570Sstevel@tonic-gate 	 * Free the verifier saved for sequence window checking.
12580Sstevel@tonic-gate 	 */
12590Sstevel@tonic-gate 	if (ap->verifier != NULL) {
12600Sstevel@tonic-gate 	    if (ap->verifier->oa_length > 0) {
12610Sstevel@tonic-gate 		kmem_free(ap->verifier->oa_base, ap->verifier->oa_length);
12620Sstevel@tonic-gate 	    }
12630Sstevel@tonic-gate 	    kmem_free(ap->verifier, sizeof (struct opaque_auth));
12640Sstevel@tonic-gate 	    ap->verifier = NULL;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate #if 0
12690Sstevel@tonic-gate /*
12700Sstevel@tonic-gate  * XXX this function is not used right now.
12710Sstevel@tonic-gate  * There is a client handle issue needs to be resolved.
12720Sstevel@tonic-gate  *
12730Sstevel@tonic-gate  * This is a private interface which will destroy a context
12740Sstevel@tonic-gate  * without freeing up the memory used by it.  We need to do this when
12750Sstevel@tonic-gate  * a refresh fails, for example, so the user will still have a handle.
12760Sstevel@tonic-gate  */
12770Sstevel@tonic-gate static void
12780Sstevel@tonic-gate rpc_gss_destroy_pvt(auth)
12790Sstevel@tonic-gate 	AUTH		*auth;
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	struct timeval	timeout;
12820Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	/*
12850Sstevel@tonic-gate 	 * If we have a server context id, inform server that we are
12860Sstevel@tonic-gate 	 * destroying the context.
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	if (ap->ctx_handle.length != 0) {
12890Sstevel@tonic-gate 		uint32_t oldxid;
12900Sstevel@tonic-gate 		uint32_t zeroxid = 0;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		ap->gss_proc = RPCSEC_GSS_DESTROY;
12930Sstevel@tonic-gate 		timeout.tv_sec = 10;
12940Sstevel@tonic-gate 		timeout.tv_usec = 0;
12950Sstevel@tonic-gate 		(void) CLNT_CONTROL(ap->clnt, CLGET_XID, (char *)&oldxid);
12960Sstevel@tonic-gate 		(void) CLNT_CONTROL(ap->clnt, CLSET_XID, (char *)&zeroxid);
12970Sstevel@tonic-gate 		(void) clnt_call(ap->clnt, NULLPROC, xdr_void, NULL,
12980Sstevel@tonic-gate 						xdr_void, NULL, timeout);
12990Sstevel@tonic-gate 		(void) CLNT_CONTROL(ap->clnt, CLSET_XID, (char *)&oldxid);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	rpc_gss_free_pvt(auth);
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate #endif
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate /*
13070Sstevel@tonic-gate  * Wrap client side data.  The encoded header is passed in through
13080Sstevel@tonic-gate  * buf and buflen.  The header is up to but not including the
13090Sstevel@tonic-gate  * credential field.
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate bool_t
rpc_gss_wrap(auth,buf,buflen,out_xdrs,xdr_func,xdr_ptr)13120Sstevel@tonic-gate rpc_gss_wrap(auth, buf, buflen, out_xdrs, xdr_func, xdr_ptr)
13130Sstevel@tonic-gate 	AUTH			*auth;
13140Sstevel@tonic-gate 	char			*buf;		/* encoded header */
13150Sstevel@tonic-gate /* has been changed to u_int in the user land */
13160Sstevel@tonic-gate 	uint_t			buflen;		/* encoded header length */
13170Sstevel@tonic-gate 	XDR			*out_xdrs;
13180Sstevel@tonic-gate 	xdrproc_t		xdr_func;
13190Sstevel@tonic-gate 	caddr_t			xdr_ptr;
13200Sstevel@tonic-gate {
13210Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
13220Sstevel@tonic-gate 	XDR			xdrs;
13230Sstevel@tonic-gate 	char			*tmp_buf;
13240Sstevel@tonic-gate 	uint_t			xdr_buf_len, cred_buf_len;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate  *  Here is how MAX_SIGNED_LEN is estimated.
13280Sstevel@tonic-gate  *  Signing a 48 bytes buffer using des_cbc_md5 would end up with
13290Sstevel@tonic-gate  *  a buffer length 33 (padded data + 16 bytes of seq_num/checksum).
13300Sstevel@tonic-gate  *  Current known max seq_num/checksum size is 24 bytes.
13310Sstevel@tonic-gate  *  88 is derived from RNDUP(33+(24-16)) * 2.
13320Sstevel@tonic-gate  */
13330Sstevel@tonic-gate #define	MAX_SIGNED_LEN	88
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * Reject an invalid context.
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 	if (ap->invalid) {
13390Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: reject an invalid context\n");
13400Sstevel@tonic-gate 		return (FALSE);
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * If context is established, bump up sequence number.
13450Sstevel@tonic-gate 	 */
13460Sstevel@tonic-gate 	if (ap->established)
13470Sstevel@tonic-gate 		ap->seq_num++;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/*
13500Sstevel@tonic-gate 	 * Create the header in a temporary XDR context and buffer
13510Sstevel@tonic-gate 	 * before putting it out.
13520Sstevel@tonic-gate 	 */
13530Sstevel@tonic-gate 	cred_buf_len = RNDUP(sizeof (ap->version) + sizeof (ap->gss_proc) +
13540Sstevel@tonic-gate 			sizeof (ap->seq_num) + sizeof (ap->service) +
13550Sstevel@tonic-gate 			sizeof (ap->ctx_handle) + ap->ctx_handle.length);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	xdr_buf_len = buflen + cred_buf_len + sizeof (struct opaque_auth) +
13580Sstevel@tonic-gate 			MAX_SIGNED_LEN;
13590Sstevel@tonic-gate 	tmp_buf = kmem_alloc(xdr_buf_len, KM_SLEEP);
13600Sstevel@tonic-gate 	xdrmem_create(&xdrs, tmp_buf, xdr_buf_len, XDR_ENCODE);
13610Sstevel@tonic-gate 	if (!XDR_PUTBYTES(&xdrs, buf, buflen)) {
13620Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13630Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: xdr putbytes failed\n");
13640Sstevel@tonic-gate 		return (FALSE);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * create cred field
13690Sstevel@tonic-gate 	 */
13700Sstevel@tonic-gate 	if (!marshall_creds(ap, &xdrs, cred_buf_len)) {
13710Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13720Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: marshall_creds failed\n");
13730Sstevel@tonic-gate 		return (FALSE);
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	/*
13770Sstevel@tonic-gate 	 * create verifier
13780Sstevel@tonic-gate 	 */
13790Sstevel@tonic-gate 	if (!marshall_verf(ap, &xdrs, tmp_buf)) {
13800Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13810Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: marshall_verf failed\n");
13820Sstevel@tonic-gate 		return (FALSE);
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	/*
13860Sstevel@tonic-gate 	 * write out header and destroy temp structures
13870Sstevel@tonic-gate 	 */
13880Sstevel@tonic-gate 	if (!XDR_PUTBYTES(out_xdrs, tmp_buf, XDR_GETPOS(&xdrs))) {
13890Sstevel@tonic-gate 		kmem_free(tmp_buf, xdr_buf_len);
13900Sstevel@tonic-gate 		RPCGSS_LOG0(1, "rpc_gss_wrap: write out header failed\n");
13910Sstevel@tonic-gate 		return (FALSE);
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
13940Sstevel@tonic-gate 	kmem_free(tmp_buf, xdr_buf_len);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/*
13970Sstevel@tonic-gate 	 * If context is not established, or if neither integrity
13980Sstevel@tonic-gate 	 * nor privacy is used, just XDR encode data.
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	if (!ap->established || ap->service == rpc_gss_svc_none) {
14010Sstevel@tonic-gate 		return ((*xdr_func)(out_xdrs, xdr_ptr));
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	return (__rpc_gss_wrap_data(ap->service, ap->qop, ap->context,
14050Sstevel@tonic-gate 				ap->seq_num, out_xdrs, xdr_func, xdr_ptr));
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * Unwrap received data.
14100Sstevel@tonic-gate  */
14110Sstevel@tonic-gate bool_t
rpc_gss_unwrap(auth,in_xdrs,xdr_func,xdr_ptr)14120Sstevel@tonic-gate rpc_gss_unwrap(auth, in_xdrs, xdr_func, xdr_ptr)
14130Sstevel@tonic-gate 	AUTH			*auth;
14140Sstevel@tonic-gate 	XDR			*in_xdrs;
14150Sstevel@tonic-gate 	bool_t			(*xdr_func)();
14160Sstevel@tonic-gate 	caddr_t			xdr_ptr;
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * If context is not established, of if neither integrity
14220Sstevel@tonic-gate 	 * nor privacy is used, just XDR encode data.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	if (!ap->established || ap->service == rpc_gss_svc_none)
14250Sstevel@tonic-gate 		return ((*xdr_func)(in_xdrs, xdr_ptr));
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	return (__rpc_gss_unwrap_data(ap->service,
14280Sstevel@tonic-gate 				ap->context,
14290Sstevel@tonic-gate 				ap->seq_num,
14300Sstevel@tonic-gate 				ap->qop,
14310Sstevel@tonic-gate 				in_xdrs, xdr_func, xdr_ptr));
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate  *  Revoke an GSSAPI based security credentials
14360Sstevel@tonic-gate  *  from the cache table.
14370Sstevel@tonic-gate  */
14380Sstevel@tonic-gate int
rpc_gss_revauth(uid_t uid,rpc_gss_OID mech)14390Sstevel@tonic-gate rpc_gss_revauth(uid_t uid, rpc_gss_OID mech)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate 	struct ga_cache_entry *next, *prev, *cur;
14420Sstevel@tonic-gate 	rpc_gss_data *ap;
14430Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
14440Sstevel@tonic-gate 	int i;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/*
14470Sstevel@tonic-gate 	 *  Check the cache table against the uid and the
14480Sstevel@tonic-gate 	 *  mechanism type.
14490Sstevel@tonic-gate 	 */
14500Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
14510Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
14520Sstevel@tonic-gate 		prev = NULL;
14530Sstevel@tonic-gate 		for (cur = ga_cache_table[i]; cur; cur = next) {
14540Sstevel@tonic-gate 			NOT_DEAD(cur);
14550Sstevel@tonic-gate 			next = cur->next;
14560Sstevel@tonic-gate 			NOT_DEAD(next);
14570Sstevel@tonic-gate 			ap = AUTH_PRIVATE(cur->auth);
14580Sstevel@tonic-gate 			if (__rpc_gss_oids_equal(ap->mech_type,
14590Sstevel@tonic-gate 			    (gss_OID) mech) && (cur->uid == uid) &&
14600Sstevel@tonic-gate 			    (cur->zoneid == zoneid)) {
14610Sstevel@tonic-gate 				if (cur->in_use) {
14620Sstevel@tonic-gate 					RPCGSS_LOG(2, "rpc_gss_revauth:invalid "
14636403Sgt29601 					    "auth %p\n", (void *)cur->auth);
14640Sstevel@tonic-gate 					ap->invalid = TRUE;
14650Sstevel@tonic-gate 				} else {
14660Sstevel@tonic-gate 					RPCGSS_LOG(2, "rpc_gss_revauth:destroy "
14676403Sgt29601 					    "auth %p\n", (void *)cur->auth);
14680Sstevel@tonic-gate 					rpc_gss_destroy(cur->auth);
14690Sstevel@tonic-gate 					kmem_cache_free(ga_cache_handle,
14706403Sgt29601 					    (void *)cur);
14710Sstevel@tonic-gate 				}
14720Sstevel@tonic-gate 				if (prev == NULL) {
14730Sstevel@tonic-gate 					ga_cache_table[i] = next;
14740Sstevel@tonic-gate 				} else {
14750Sstevel@tonic-gate 					prev->next = next;
14760Sstevel@tonic-gate 					NOT_DEAD(prev->next);
14770Sstevel@tonic-gate 				}
14780Sstevel@tonic-gate 			} else {
14790Sstevel@tonic-gate 				prev = cur;
14800Sstevel@tonic-gate 			}
14810Sstevel@tonic-gate 		}
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	return (0);
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate  *  Delete all the entries indexed by the cache_key.
14910Sstevel@tonic-gate  *
14920Sstevel@tonic-gate  *  For example, the cache_key used for NFS is the address of the
14930Sstevel@tonic-gate  *  security entry for each mount point.  When the file system is unmounted,
14940Sstevel@tonic-gate  *  all the cache entries indexed by this key should be deleted.
14950Sstevel@tonic-gate  */
14960Sstevel@tonic-gate void
rpc_gss_secpurge(void * cache_key)14970Sstevel@tonic-gate rpc_gss_secpurge(void *cache_key)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	struct ga_cache_entry *next, *prev, *cur;
15000Sstevel@tonic-gate 	int i;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 *  Check the cache table against the cache_key.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	rw_enter(&ga_cache_table_lock, RW_WRITER);
15060Sstevel@tonic-gate 	for (i = 0; i < GSSAUTH_TABLESIZE; i++) {
15070Sstevel@tonic-gate 		prev = NULL;
15080Sstevel@tonic-gate 		for (cur = ga_cache_table[i]; cur; cur = next) {
15090Sstevel@tonic-gate 			NOT_DEAD(cur);
15100Sstevel@tonic-gate 			next = cur->next;
15110Sstevel@tonic-gate 			NOT_DEAD(next);
15120Sstevel@tonic-gate 			if (cache_key == cur->cache_key) {
15130Sstevel@tonic-gate 				RPCGSS_LOG(2, "rpc_gss_secpurge: destroy auth "
15146403Sgt29601 				    "%p\n", (void *)cur->auth);
15156403Sgt29601 				if (cur->in_use == FALSE)
15166403Sgt29601 					rpc_gss_destroy(cur->auth);
15170Sstevel@tonic-gate 				kmem_cache_free(ga_cache_handle, (void *)cur);
15180Sstevel@tonic-gate 				if (prev == NULL) {
15190Sstevel@tonic-gate 					ga_cache_table[i] = next;
15200Sstevel@tonic-gate 				} else {
15210Sstevel@tonic-gate 					NOT_DEAD(prev->next);
15220Sstevel@tonic-gate 					prev->next = next;
15230Sstevel@tonic-gate 				}
15240Sstevel@tonic-gate 			} else {
15250Sstevel@tonic-gate 				prev = cur;
15260Sstevel@tonic-gate 			}
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 	rw_exit(&ga_cache_table_lock);
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate  * Function: rpc_gss_nextverf.  Not used.
15340Sstevel@tonic-gate  */
15350Sstevel@tonic-gate static void
rpc_gss_nextverf()15360Sstevel@tonic-gate rpc_gss_nextverf()
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate }
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate /*
15410Sstevel@tonic-gate  * Function: rpc_gss_marshall - no op routine.
15420Sstevel@tonic-gate  *		rpc_gss_wrap() is doing the marshalling.
15430Sstevel@tonic-gate  */
15440Sstevel@tonic-gate /*ARGSUSED*/
15450Sstevel@tonic-gate static bool_t
rpc_gss_marshall(auth,xdrs)15460Sstevel@tonic-gate rpc_gss_marshall(auth, xdrs)
15470Sstevel@tonic-gate 	AUTH		*auth;
15480Sstevel@tonic-gate 	XDR		*xdrs;
15490Sstevel@tonic-gate {
15500Sstevel@tonic-gate 	return (TRUE);
15510Sstevel@tonic-gate }
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate /*
15540Sstevel@tonic-gate  * Set service defaults.
15550Sstevel@tonic-gate  * Not supported yet.
15560Sstevel@tonic-gate  */
15570Sstevel@tonic-gate /* ARGSUSED */
15580Sstevel@tonic-gate bool_t
rpc_gss_set_defaults(auth,service,qop)15590Sstevel@tonic-gate rpc_gss_set_defaults(auth, service, qop)
15600Sstevel@tonic-gate 	AUTH			*auth;
15610Sstevel@tonic-gate 	rpc_gss_service_t	service;
15620Sstevel@tonic-gate 	uint_t			qop;
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	return (FALSE);
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate /* ARGSUSED */
15680Sstevel@tonic-gate int
rpc_gss_max_data_length(AUTH * rpcgss_handle,int max_tp_unit_len)15690Sstevel@tonic-gate rpc_gss_max_data_length(AUTH *rpcgss_handle, int max_tp_unit_len)
15700Sstevel@tonic-gate {
15710Sstevel@tonic-gate 	return (0);
15720Sstevel@tonic-gate }
1573*7387SRobert.Gordon@Sun.COM 
1574*7387SRobert.Gordon@Sun.COM rpc_gss_service_t
rpc_gss_get_service_type(AUTH * auth)1575*7387SRobert.Gordon@Sun.COM rpc_gss_get_service_type(AUTH *auth)
1576*7387SRobert.Gordon@Sun.COM {
1577*7387SRobert.Gordon@Sun.COM 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
1578*7387SRobert.Gordon@Sun.COM 
1579*7387SRobert.Gordon@Sun.COM 	return (ap->service);
1580*7387SRobert.Gordon@Sun.COM }
1581