xref: /onnv-gate/usr/src/lib/rpcsec_gss/svc_rpcsec_gss.c (revision 6636:1e56d9885568)
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
54554Sps57422  * Common Development and Distribution License (the "License").
64554Sps57422  * 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  */
214554Sps57422 
220Sstevel@tonic-gate /*
23*6636Sgtb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
244554Sps57422  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * $Id: svc_auth_gssapi.c,v 1.19 1994/10/27 12:38:51 jik Exp $
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Server side handling of RPCSEC_GSS flavor.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <strings.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/time.h>
440Sstevel@tonic-gate #include <gssapi/gssapi.h>
450Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
460Sstevel@tonic-gate #include <rpc/rpc.h>
470Sstevel@tonic-gate #include <rpc/rpcsec_defs.h>
480Sstevel@tonic-gate #include <sys/file.h>
490Sstevel@tonic-gate #include <fcntl.h>
500Sstevel@tonic-gate #include <pwd.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <syslog.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Sequence window definitions.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate #define	SEQ_ARR_SIZE	4
580Sstevel@tonic-gate #define	SEQ_WIN		(SEQ_ARR_SIZE*32)
590Sstevel@tonic-gate #define	SEQ_HI_BIT	0x80000000
600Sstevel@tonic-gate #define	SEQ_LO_BIT	1
610Sstevel@tonic-gate #define	DIV_BY_32	5
620Sstevel@tonic-gate #define	SEQ_MASK	0x1f
630Sstevel@tonic-gate #define	SEQ_MAX		0x80000000
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* cache retransmit data */
670Sstevel@tonic-gate typedef struct _retrans_entry {
680Sstevel@tonic-gate 	uint32_t	xid;
690Sstevel@tonic-gate 	rpc_gss_init_res result;
700Sstevel@tonic-gate 	struct _retrans_entry *next, *prev;
710Sstevel@tonic-gate } retrans_entry;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Server side RPCSEC_GSS context information.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate typedef struct _svc_rpc_gss_data {
770Sstevel@tonic-gate 	struct _svc_rpc_gss_data	*next, *prev;
780Sstevel@tonic-gate 	struct _svc_rpc_gss_data	*lru_next, *lru_prev;
790Sstevel@tonic-gate 	bool_t				established;
800Sstevel@tonic-gate 	gss_ctx_id_t			context;
810Sstevel@tonic-gate 	gss_name_t			client_name;
820Sstevel@tonic-gate 	gss_cred_id_t			server_creds;
830Sstevel@tonic-gate 	uint_t				expiration;
840Sstevel@tonic-gate 	uint_t				seq_num;
850Sstevel@tonic-gate 	uint_t				seq_bits[SEQ_ARR_SIZE];
860Sstevel@tonic-gate 	uint_t				key;
870Sstevel@tonic-gate 	OM_uint32			qop;
880Sstevel@tonic-gate 	bool_t				done_docallback;
890Sstevel@tonic-gate 	bool_t				locked;
900Sstevel@tonic-gate 	rpc_gss_rawcred_t		raw_cred;
910Sstevel@tonic-gate 	rpc_gss_ucred_t			u_cred;
920Sstevel@tonic-gate 	bool_t				u_cred_set;
930Sstevel@tonic-gate 	void				*cookie;
940Sstevel@tonic-gate 	gss_cred_id_t			deleg;
950Sstevel@tonic-gate 	mutex_t				clm;
960Sstevel@tonic-gate 	int				ref_cnt;
970Sstevel@tonic-gate 	bool_t				stale;
980Sstevel@tonic-gate 	time_t				time_secs_set;
990Sstevel@tonic-gate 	retrans_entry			*retrans_data;
1000Sstevel@tonic-gate } svc_rpc_gss_data;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * Data structures used for LRU based context management.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate #define	HASHMOD			256
1060Sstevel@tonic-gate #define	HASHMASK		255
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static svc_rpc_gss_data		*clients[HASHMOD];
1090Sstevel@tonic-gate static svc_rpc_gss_data		*lru_first, *lru_last;
1100Sstevel@tonic-gate static int			num_gss_contexts = 0;
1110Sstevel@tonic-gate static int			max_gss_contexts = 128;
1120Sstevel@tonic-gate static int			sweep_interval = 10;
1130Sstevel@tonic-gate static int			last_swept = 0;
1140Sstevel@tonic-gate static uint_t			max_lifetime = GSS_C_INDEFINITE;
115*6636Sgtb static int			init_lifetime = 0;
1160Sstevel@tonic-gate static uint_t			gid_timeout = 43200; /* 43200 secs = 12 hours */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * lock used with context/lru variables
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate static mutex_t			ctx_mutex = DEFAULTMUTEX;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * server credential management data and structures
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate typedef struct svc_creds_list_s {
1270Sstevel@tonic-gate 	struct svc_creds_list_s	*next;
1280Sstevel@tonic-gate 	gss_cred_id_t		cred;
1290Sstevel@tonic-gate 	gss_name_t		name;
1300Sstevel@tonic-gate 	rpcprog_t		program;
1310Sstevel@tonic-gate 	rpcvers_t		version;
1320Sstevel@tonic-gate 	gss_OID_set		oid_set;
1330Sstevel@tonic-gate 	OM_uint32		req_time;
1340Sstevel@tonic-gate 	char			*server_name;
1350Sstevel@tonic-gate 	mutex_t			refresh_mutex;
1360Sstevel@tonic-gate } svc_creds_list_t;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static svc_creds_list_t		*svc_creds_list;
1400Sstevel@tonic-gate static int			svc_creds_count = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * lock used with server credential variables list
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * server cred list locking guidelines:
1460Sstevel@tonic-gate  * - Writer's lock holder has exclusive access to the list
1470Sstevel@tonic-gate  * - Reader's lock holder(s) must also lock (refresh_mutex) each node
1480Sstevel@tonic-gate  *   before accessing that node's elements (ie. cred)
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static rwlock_t			cred_lock = DEFAULTRWLOCK;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * server callback list
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate typedef struct cblist_s {
1560Sstevel@tonic-gate 	struct cblist_s		*next;
1570Sstevel@tonic-gate 	rpc_gss_callback_t	cb;
1580Sstevel@tonic-gate } cblist_t;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate cblist_t			*cblist = NULL;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * lock used with callback variables
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate static mutex_t			cb_mutex = DEFAULTMUTEX;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * forward declarations
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate static bool_t			svc_rpc_gss_wrap();
1710Sstevel@tonic-gate static bool_t			svc_rpc_gss_unwrap();
1720Sstevel@tonic-gate static svc_rpc_gss_data		*create_client();
1730Sstevel@tonic-gate static svc_rpc_gss_data		*get_client();
1740Sstevel@tonic-gate static svc_rpc_gss_data		*find_client();
1750Sstevel@tonic-gate static void			destroy_client();
1760Sstevel@tonic-gate static void			sweep_clients();
1770Sstevel@tonic-gate static void			drop_lru_client();
1780Sstevel@tonic-gate static void			insert_client();
1790Sstevel@tonic-gate static bool_t			check_verf();
1800Sstevel@tonic-gate static bool_t			rpc_gss_refresh_svc_cred();
1810Sstevel@tonic-gate static bool_t			set_response_verf();
1820Sstevel@tonic-gate static void			retrans_add(svc_rpc_gss_data *, uint32_t,
1830Sstevel@tonic-gate 					rpc_gss_init_res *);
1840Sstevel@tonic-gate static void			retrans_del(struct _svc_rpc_gss_data *);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * server side wrap/unwrap routines
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate struct svc_auth_ops svc_rpc_gss_ops = {
1910Sstevel@tonic-gate 	svc_rpc_gss_wrap,
1920Sstevel@tonic-gate 	svc_rpc_gss_unwrap,
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * Fetch server side authentication structure.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate extern SVCAUTH *__svc_get_svcauth();
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * Cleanup routine for destroying context, called after service
2020Sstevel@tonic-gate  * procedure is executed, for MT safeness.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate extern void *__svc_set_proc_cleanup_cb();
2050Sstevel@tonic-gate static void (*old_cleanup_cb)() = NULL;
2060Sstevel@tonic-gate static bool_t cleanup_cb_set = FALSE;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static void
ctx_cleanup(xprt)2090Sstevel@tonic-gate ctx_cleanup(xprt)
2100Sstevel@tonic-gate 	SVCXPRT *xprt;
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	svc_rpc_gss_data	*cl;
2130Sstevel@tonic-gate 	SVCAUTH			*svcauth;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (old_cleanup_cb != NULL)
2160Sstevel@tonic-gate 		(*old_cleanup_cb)(xprt);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * First check if current context needs to be cleaned up.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	svcauth = __svc_get_svcauth(xprt);
2220Sstevel@tonic-gate 	/*LINTED*/
2230Sstevel@tonic-gate 	if ((cl = (svc_rpc_gss_data *)svcauth->svc_ah_private) != NULL) {
2240Sstevel@tonic-gate 		mutex_lock(&cl->clm);
2250Sstevel@tonic-gate 		if (--cl->ref_cnt == 0 && cl->stale) {
2260Sstevel@tonic-gate 			mutex_unlock(&cl->clm);
2270Sstevel@tonic-gate 			mutex_lock(&ctx_mutex);
2280Sstevel@tonic-gate 			destroy_client(cl);
2290Sstevel@tonic-gate 			mutex_unlock(&ctx_mutex);
2300Sstevel@tonic-gate 		} else
2310Sstevel@tonic-gate 			mutex_unlock(&cl->clm);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Check for other expired contexts.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	if ((time(0) - last_swept) > sweep_interval) {
2380Sstevel@tonic-gate 		mutex_lock(&ctx_mutex);
2390Sstevel@tonic-gate 		/*
2400Sstevel@tonic-gate 		 * Check again, in case some other thread got in.
2410Sstevel@tonic-gate 		 */
2420Sstevel@tonic-gate 		if ((time(0) - last_swept) > sweep_interval)
2430Sstevel@tonic-gate 			sweep_clients();
2440Sstevel@tonic-gate 		mutex_unlock(&ctx_mutex);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * Set server parameters.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate void
__rpc_gss_set_server_parms(init_cred_lifetime,max_cred_lifetime,cache_size)2520Sstevel@tonic-gate __rpc_gss_set_server_parms(init_cred_lifetime, max_cred_lifetime, cache_size)
2530Sstevel@tonic-gate 	int	init_cred_lifetime;
2540Sstevel@tonic-gate 	int	max_cred_lifetime;
2550Sstevel@tonic-gate 	int	cache_size;
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Ignore parameters unless greater than zero.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	mutex_lock(&ctx_mutex);
2610Sstevel@tonic-gate 	if (cache_size > 0)
2620Sstevel@tonic-gate 		max_gss_contexts = cache_size;
2630Sstevel@tonic-gate 	if (max_cred_lifetime > 0)
2640Sstevel@tonic-gate 		max_lifetime = (uint_t)max_cred_lifetime;
2650Sstevel@tonic-gate 	if (init_cred_lifetime > 0)
2660Sstevel@tonic-gate 		init_lifetime = init_cred_lifetime;
2670Sstevel@tonic-gate 	mutex_unlock(&ctx_mutex);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * Shift the array arr of length arrlen right by nbits bits.
2720Sstevel@tonic-gate  */
2730Sstevel@tonic-gate static void
shift_bits(arr,arrlen,nbits)2740Sstevel@tonic-gate shift_bits(arr, arrlen, nbits)
2750Sstevel@tonic-gate 	uint_t	*arr;
2760Sstevel@tonic-gate 	int	arrlen;
2770Sstevel@tonic-gate 	int	nbits;
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	int	i, j;
2800Sstevel@tonic-gate 	uint_t	lo, hi;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * If the number of bits to be shifted exceeds SEQ_WIN, just
2840Sstevel@tonic-gate 	 * zero out the array.
2850Sstevel@tonic-gate 	 */
2860Sstevel@tonic-gate 	if (nbits < SEQ_WIN) {
2870Sstevel@tonic-gate 		for (i = 0; i < nbits; i++) {
2880Sstevel@tonic-gate 			hi = 0;
2890Sstevel@tonic-gate 			for (j = 0; j < arrlen; j++) {
2900Sstevel@tonic-gate 				lo = arr[j] & SEQ_LO_BIT;
2910Sstevel@tonic-gate 				arr[j] >>= 1;
2920Sstevel@tonic-gate 				if (hi)
2930Sstevel@tonic-gate 					arr[j] |= SEQ_HI_BIT;
2940Sstevel@tonic-gate 				hi = lo;
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 	} else {
2980Sstevel@tonic-gate 		for (j = 0; j < arrlen; j++)
2990Sstevel@tonic-gate 			arr[j] = 0;
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Check that the received sequence number seq_num is valid.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate static bool_t
check_seq(cl,seq_num,kill_context)3070Sstevel@tonic-gate check_seq(cl, seq_num, kill_context)
3080Sstevel@tonic-gate 	svc_rpc_gss_data	*cl;
3090Sstevel@tonic-gate 	uint_t			seq_num;
3100Sstevel@tonic-gate 	bool_t			*kill_context;
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	int			i, j;
3130Sstevel@tonic-gate 	uint_t			bit;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * If it exceeds the maximum, kill context.
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	if (seq_num >= SEQ_MAX) {
3190Sstevel@tonic-gate 		*kill_context = TRUE;
3200Sstevel@tonic-gate 		return (FALSE);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * If greater than the last seen sequence number, just shift
3250Sstevel@tonic-gate 	 * the sequence window so that it starts at the new sequence
3260Sstevel@tonic-gate 	 * number and extends downwards by SEQ_WIN.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	if (seq_num > cl->seq_num) {
3290Sstevel@tonic-gate 		shift_bits(cl->seq_bits, SEQ_ARR_SIZE, seq_num - cl->seq_num);
3300Sstevel@tonic-gate 		cl->seq_bits[0] |= SEQ_HI_BIT;
3310Sstevel@tonic-gate 		cl->seq_num = seq_num;
3320Sstevel@tonic-gate 		return (TRUE);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * If it is outside the sequence window, return failure.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	i = cl->seq_num - seq_num;
3390Sstevel@tonic-gate 	if (i >= SEQ_WIN)
3400Sstevel@tonic-gate 		return (FALSE);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * If within sequence window, set the bit corresponding to it
3440Sstevel@tonic-gate 	 * if not already seen;  if already seen, return failure.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	j = SEQ_MASK - (i & SEQ_MASK);
3470Sstevel@tonic-gate 	bit = j > 0 ? (1 << j) : 1;
3480Sstevel@tonic-gate 	i >>= DIV_BY_32;
3490Sstevel@tonic-gate 	if (cl->seq_bits[i] & bit)
3500Sstevel@tonic-gate 		return (FALSE);
3510Sstevel@tonic-gate 	cl->seq_bits[i] |= bit;
3520Sstevel@tonic-gate 	return (TRUE);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate  * Convert a name in gss exported type to rpc_gss_principal_t type.
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate static bool_t
__rpc_gss_make_principal(principal,name)3590Sstevel@tonic-gate __rpc_gss_make_principal(principal, name)
3600Sstevel@tonic-gate 	rpc_gss_principal_t	*principal;
3610Sstevel@tonic-gate 	gss_buffer_desc		*name;
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	int			plen;
3640Sstevel@tonic-gate 	char			*s;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	plen = RNDUP(name->length) + sizeof (int);
3670Sstevel@tonic-gate 	(*principal) = (rpc_gss_principal_t)malloc(plen);
3680Sstevel@tonic-gate 	if ((*principal) == NULL)
3690Sstevel@tonic-gate 		return (FALSE);
3700Sstevel@tonic-gate 	bzero((caddr_t)(*principal), plen);
3710Sstevel@tonic-gate 	(*principal)->len = RNDUP(name->length);
3720Sstevel@tonic-gate 	s = (*principal)->name;
3730Sstevel@tonic-gate 	memcpy(s, name->value, name->length);
3740Sstevel@tonic-gate 	return (TRUE);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * Convert a name in internal form to the exported type.
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate static bool_t
set_client_principal(g_name,r_name)3810Sstevel@tonic-gate set_client_principal(g_name, r_name)
3820Sstevel@tonic-gate 	gss_name_t		g_name;
3830Sstevel@tonic-gate 	rpc_gss_principal_t	*r_name;
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	gss_buffer_desc		name;
3860Sstevel@tonic-gate 	OM_uint32		major, minor;
3870Sstevel@tonic-gate 	bool_t			ret = FALSE;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	major = gss_export_name(&minor, g_name, &name);
3900Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE)
3910Sstevel@tonic-gate 		return (FALSE);
3920Sstevel@tonic-gate 	ret = __rpc_gss_make_principal(r_name, &name);
3930Sstevel@tonic-gate 	(void) gss_release_buffer(&minor, &name);
3940Sstevel@tonic-gate 	return (ret);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Set server callback.
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate bool_t
__rpc_gss_set_callback(cb)4010Sstevel@tonic-gate __rpc_gss_set_callback(cb)
4020Sstevel@tonic-gate 	rpc_gss_callback_t	*cb;
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	cblist_t		*cbl;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (cb->callback == NULL)
4070Sstevel@tonic-gate 		return (FALSE);
4080Sstevel@tonic-gate 	if ((cbl = (cblist_t *)malloc(sizeof (*cbl))) == NULL)
4090Sstevel@tonic-gate 		return (FALSE);
4100Sstevel@tonic-gate 	cbl->cb = *cb;
4110Sstevel@tonic-gate 	mutex_lock(&cb_mutex);
4120Sstevel@tonic-gate 	cbl->next = cblist;
4130Sstevel@tonic-gate 	cblist = cbl;
4140Sstevel@tonic-gate 	mutex_unlock(&cb_mutex);
4150Sstevel@tonic-gate 	return (TRUE);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate  * Locate callback (if specified) and call server.  Release any
4200Sstevel@tonic-gate  * delegated credentials unless passed to server and the server
4210Sstevel@tonic-gate  * accepts the context.  If a callback is not specified, accept
4220Sstevel@tonic-gate  * the incoming context.
4230Sstevel@tonic-gate  */
4240Sstevel@tonic-gate static bool_t
do_callback(req,client_data)4250Sstevel@tonic-gate do_callback(req, client_data)
4260Sstevel@tonic-gate 	struct svc_req		*req;
4270Sstevel@tonic-gate 	svc_rpc_gss_data	*client_data;
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	cblist_t		*cbl;
4300Sstevel@tonic-gate 	bool_t			ret = TRUE, found = FALSE;
4310Sstevel@tonic-gate 	rpc_gss_lock_t		lock;
4320Sstevel@tonic-gate 	OM_uint32		minor;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	mutex_lock(&cb_mutex);
4350Sstevel@tonic-gate 	for (cbl = cblist; cbl != NULL; cbl = cbl->next) {
4360Sstevel@tonic-gate 		if (req->rq_prog != cbl->cb.program ||
4370Sstevel@tonic-gate 					req->rq_vers != cbl->cb.version)
4380Sstevel@tonic-gate 			continue;
4390Sstevel@tonic-gate 		found = TRUE;
4400Sstevel@tonic-gate 		lock.locked = FALSE;
4410Sstevel@tonic-gate 		lock.raw_cred = &client_data->raw_cred;
4420Sstevel@tonic-gate 		ret = (*cbl->cb.callback)(req, client_data->deleg,
4430Sstevel@tonic-gate 			client_data->context, &lock, &client_data->cookie);
4440Sstevel@tonic-gate 		if (ret) {
4450Sstevel@tonic-gate 			client_data->locked = lock.locked;
4460Sstevel@tonic-gate 			client_data->deleg = GSS_C_NO_CREDENTIAL;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		break;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 	if (!found) {
4510Sstevel@tonic-gate 		if (client_data->deleg != GSS_C_NO_CREDENTIAL) {
4520Sstevel@tonic-gate 			(void) gss_release_cred(&minor, &client_data->deleg);
4530Sstevel@tonic-gate 			client_data->deleg = GSS_C_NO_CREDENTIAL;
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	mutex_unlock(&cb_mutex);
4570Sstevel@tonic-gate 	return (ret);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Return caller credentials.
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate bool_t
__rpc_gss_getcred(req,rcred,ucred,cookie)4640Sstevel@tonic-gate __rpc_gss_getcred(req, rcred, ucred, cookie)
4650Sstevel@tonic-gate 	struct svc_req		*req;
4660Sstevel@tonic-gate 	rpc_gss_rawcred_t	**rcred;
4670Sstevel@tonic-gate 	rpc_gss_ucred_t		**ucred;
4680Sstevel@tonic-gate 	void			**cookie;
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	SVCAUTH			*svcauth;
4710Sstevel@tonic-gate 	svc_rpc_gss_data	*client_data;
4720Sstevel@tonic-gate 	svc_rpc_gss_parms_t	*gss_parms;
4730Sstevel@tonic-gate 	gss_OID			oid;
4740Sstevel@tonic-gate 	OM_uint32		status;
4750Sstevel@tonic-gate 	int			len = 0;
4760Sstevel@tonic-gate 	struct timeval		now;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	svcauth = __svc_get_svcauth(req->rq_xprt);
4790Sstevel@tonic-gate 	/*LINTED*/
4800Sstevel@tonic-gate 	client_data = (svc_rpc_gss_data *)svcauth->svc_ah_private;
4810Sstevel@tonic-gate 	gss_parms = &svcauth->svc_gss_parms;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	mutex_lock(&client_data->clm);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	if (rcred != NULL) {
4860Sstevel@tonic-gate 		svcauth->raw_cred = client_data->raw_cred;
4870Sstevel@tonic-gate 		svcauth->raw_cred.service = gss_parms->service;
4880Sstevel@tonic-gate 		svcauth->raw_cred.qop = __rpc_gss_num_to_qop(
4890Sstevel@tonic-gate 			svcauth->raw_cred.mechanism, gss_parms->qop_rcvd);
4900Sstevel@tonic-gate 		*rcred = &svcauth->raw_cred;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	if (ucred != NULL) {
4930Sstevel@tonic-gate 		if (!client_data->u_cred_set) {
4940Sstevel@tonic-gate 			/*
4950Sstevel@tonic-gate 			 * Double check making sure ucred is not set
4960Sstevel@tonic-gate 			 * after acquiring the lock.
4970Sstevel@tonic-gate 			 */
4980Sstevel@tonic-gate 			if (!client_data->u_cred_set) {
4990Sstevel@tonic-gate 				if (!__rpc_gss_mech_to_oid(
5000Sstevel@tonic-gate 					(*rcred)->mechanism, &oid)) {
5010Sstevel@tonic-gate 					fprintf(stderr, dgettext(TEXT_DOMAIN,
5020Sstevel@tonic-gate 					"mech_to_oid failed in getcred.\n"));
5030Sstevel@tonic-gate 					*ucred = NULL;
5040Sstevel@tonic-gate 				} else {
5050Sstevel@tonic-gate 					status = gsscred_name_to_unix_cred(
5060Sstevel@tonic-gate 						client_data->client_name, oid,
5070Sstevel@tonic-gate 						&client_data->u_cred.uid,
5080Sstevel@tonic-gate 						&client_data->u_cred.gid,
5090Sstevel@tonic-gate 						&client_data->u_cred.gidlist,
5100Sstevel@tonic-gate 						&len);
5110Sstevel@tonic-gate 					if (status == GSS_S_COMPLETE) {
5120Sstevel@tonic-gate 						client_data->u_cred_set = TRUE;
5130Sstevel@tonic-gate 						client_data->u_cred.gidlen =
5140Sstevel@tonic-gate 							(short)len;
5150Sstevel@tonic-gate 						gettimeofday(&now,
5160Sstevel@tonic-gate 						(struct timezone *)NULL);
5170Sstevel@tonic-gate 						client_data->time_secs_set =
5180Sstevel@tonic-gate 						now.tv_sec;
5190Sstevel@tonic-gate 						*ucred = &client_data->u_cred;
5200Sstevel@tonic-gate 					} else
5210Sstevel@tonic-gate 						*ucred = NULL;
5220Sstevel@tonic-gate 				}
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 		} else {
5250Sstevel@tonic-gate 			/*
5260Sstevel@tonic-gate 			 * gid's already set;
5270Sstevel@tonic-gate 			 * check if they have expired.
5280Sstevel@tonic-gate 			 */
5290Sstevel@tonic-gate 			gettimeofday(&now, (struct timezone *)NULL);
5300Sstevel@tonic-gate 			if ((now.tv_sec - client_data->time_secs_set)
5310Sstevel@tonic-gate 				> gid_timeout) {
5320Sstevel@tonic-gate 				/* Refresh gid's */
5330Sstevel@tonic-gate 				status = gss_get_group_info(
5340Sstevel@tonic-gate 					client_data->u_cred.uid,
5350Sstevel@tonic-gate 					&client_data->u_cred.gid,
5360Sstevel@tonic-gate 					&client_data->u_cred.gidlist,
5370Sstevel@tonic-gate 					&len);
5380Sstevel@tonic-gate 				if (status == GSS_S_COMPLETE) {
5390Sstevel@tonic-gate 					client_data->u_cred.gidlen =
5400Sstevel@tonic-gate 						(short)len;
5410Sstevel@tonic-gate 					gettimeofday(&now,
5420Sstevel@tonic-gate 					(struct timezone *)NULL);
5430Sstevel@tonic-gate 					client_data->time_secs_set = now.tv_sec;
5440Sstevel@tonic-gate 					*ucred = &client_data->u_cred;
5450Sstevel@tonic-gate 				} else {
5460Sstevel@tonic-gate 					client_data->u_cred_set = FALSE;
5470Sstevel@tonic-gate 					*ucred = NULL;
5480Sstevel@tonic-gate 				}
5490Sstevel@tonic-gate 			}
5500Sstevel@tonic-gate 			else
5510Sstevel@tonic-gate 				*ucred = &client_data->u_cred;
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	if (cookie != NULL)
5550Sstevel@tonic-gate 		*cookie = client_data->cookie;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	mutex_unlock(&client_data->clm);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	return (TRUE);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate  * Server side authentication for RPCSEC_GSS.
5640Sstevel@tonic-gate  */
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate enum auth_stat
__svcrpcsec_gss(rqst,msg,no_dispatch)5670Sstevel@tonic-gate __svcrpcsec_gss(rqst, msg, no_dispatch)
5680Sstevel@tonic-gate 	struct svc_req		*rqst;
5690Sstevel@tonic-gate 	struct rpc_msg		*msg;
5700Sstevel@tonic-gate 	bool_t			*no_dispatch;
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	XDR			xdrs;
5730Sstevel@tonic-gate 	rpc_gss_creds		creds;
5740Sstevel@tonic-gate 	rpc_gss_init_arg	call_arg;
5750Sstevel@tonic-gate 	rpc_gss_init_res	call_res, *retrans_result;
5760Sstevel@tonic-gate 	gss_buffer_desc		output_token;
5770Sstevel@tonic-gate 	OM_uint32		gssstat, minor_stat, time_rec, ret_flags;
5780Sstevel@tonic-gate 	struct opaque_auth	*cred;
5790Sstevel@tonic-gate 	svc_rpc_gss_data	*client_data;
5800Sstevel@tonic-gate 	int			ret;
5810Sstevel@tonic-gate 	svc_creds_list_t	*sc;
5820Sstevel@tonic-gate 	SVCAUTH			*svcauth;
5830Sstevel@tonic-gate 	svc_rpc_gss_parms_t	*gss_parms;
5840Sstevel@tonic-gate 	gss_OID			mech_type = GSS_C_NULL_OID;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * Initialize response verifier to NULL verifier.  If
5880Sstevel@tonic-gate 	 * necessary, this will be changed later.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NONE;
5910Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_base = NULL;
5920Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_length = 0;
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Need to null out results to start with.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	memset((char *)&call_res, 0, sizeof (call_res));
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Pull out and check credential and verifier.
6000Sstevel@tonic-gate 	 */
6010Sstevel@tonic-gate 	cred = &msg->rm_call.cb_cred;
6020Sstevel@tonic-gate 	if (cred->oa_length == 0) {
6034554Sps57422 		return (AUTH_BADCRED);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	xdrmem_create(&xdrs, cred->oa_base, cred->oa_length, XDR_DECODE);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	memset((char *)&creds, 0, sizeof (creds));
6090Sstevel@tonic-gate 	if (!__xdr_rpc_gss_creds(&xdrs, &creds)) {
6100Sstevel@tonic-gate 		XDR_DESTROY(&xdrs);
6110Sstevel@tonic-gate 		ret = AUTH_BADCRED;
6120Sstevel@tonic-gate 		goto error;
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * If this is a control message and proc is GSSAPI_INIT, then
6180Sstevel@tonic-gate 	 * create a client handle for this client.  Otherwise, look up
6190Sstevel@tonic-gate 	 * the existing handle.
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	if (creds.gss_proc == RPCSEC_GSS_INIT) {
6220Sstevel@tonic-gate 		if (creds.ctx_handle.length != 0) {
6230Sstevel@tonic-gate 			ret = AUTH_BADCRED;
6240Sstevel@tonic-gate 			goto error;
6250Sstevel@tonic-gate 		}
6260Sstevel@tonic-gate 		if ((client_data = create_client()) == NULL) {
6270Sstevel@tonic-gate 			ret = AUTH_FAILED;
6280Sstevel@tonic-gate 			goto error;
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 	} else {
6310Sstevel@tonic-gate 		/*
6320Sstevel@tonic-gate 		 * Only verify values for service parameter when proc
6330Sstevel@tonic-gate 		 * not RPCSEC_GSS_INIT or RPCSEC_GSS_CONTINUE_INIT.
6340Sstevel@tonic-gate 		 * RFC2203 says contents for sequence and service args
6350Sstevel@tonic-gate 		 * are undefined for creation procs.
6360Sstevel@tonic-gate 		 *
6370Sstevel@tonic-gate 		 * Note: only need to check for *CONTINUE_INIT here because
6380Sstevel@tonic-gate 		 *	if() clause already checked for RPCSEC_GSS_INIT
6390Sstevel@tonic-gate 		 */
6400Sstevel@tonic-gate 		if (creds.gss_proc != RPCSEC_GSS_CONTINUE_INIT) {
6410Sstevel@tonic-gate 			switch (creds.service) {
6420Sstevel@tonic-gate 			case rpc_gss_svc_none:
6430Sstevel@tonic-gate 			case rpc_gss_svc_integrity:
6440Sstevel@tonic-gate 			case rpc_gss_svc_privacy:
6450Sstevel@tonic-gate 				break;
6460Sstevel@tonic-gate 			default:
6470Sstevel@tonic-gate 				ret = AUTH_BADCRED;
6480Sstevel@tonic-gate 				goto error;
6490Sstevel@tonic-gate 			}
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 		if (creds.ctx_handle.length == 0) {
6520Sstevel@tonic-gate 			ret = AUTH_BADCRED;
6530Sstevel@tonic-gate 			goto error;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 		if ((client_data = get_client(&creds.ctx_handle)) == NULL) {
6560Sstevel@tonic-gate 			ret = RPCSEC_GSS_NOCRED;
6570Sstevel@tonic-gate 			goto error;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/*
6620Sstevel@tonic-gate 	 * lock the client data until it's safe; if it's already stale,
6630Sstevel@tonic-gate 	 * no more processing is possible
6640Sstevel@tonic-gate 	 */
6650Sstevel@tonic-gate 	mutex_lock(&client_data->clm);
6660Sstevel@tonic-gate 	if (client_data->stale) {
6670Sstevel@tonic-gate 		ret = RPCSEC_GSS_NOCRED;
6680Sstevel@tonic-gate 		goto error2;
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	/*
6720Sstevel@tonic-gate 	 * Any response we send will use ctx_handle, so set it now;
6730Sstevel@tonic-gate 	 * also set seq_window since this won't change.
6740Sstevel@tonic-gate 	 */
6750Sstevel@tonic-gate 	call_res.ctx_handle.length = sizeof (client_data->key);
6760Sstevel@tonic-gate 	call_res.ctx_handle.value = (char *)&client_data->key;
6770Sstevel@tonic-gate 	call_res.seq_window = SEQ_WIN;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/*
6800Sstevel@tonic-gate 	 * Set the appropriate wrap/unwrap routine for RPCSEC_GSS.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	svcauth = __svc_get_svcauth(rqst->rq_xprt);
6830Sstevel@tonic-gate 	svcauth->svc_ah_ops = svc_rpc_gss_ops;
6840Sstevel@tonic-gate 	svcauth->svc_ah_private = (caddr_t)client_data;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	/*
6870Sstevel@tonic-gate 	 * Keep copy of parameters we'll need for response, for the
6880Sstevel@tonic-gate 	 * sake of reentrancy (we don't want to look in the context
6890Sstevel@tonic-gate 	 * data because when we are sending a response, another
6900Sstevel@tonic-gate 	 * request may have come in.
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	gss_parms = &svcauth->svc_gss_parms;
6930Sstevel@tonic-gate 	gss_parms->established = client_data->established;
6940Sstevel@tonic-gate 	gss_parms->service = creds.service;
6950Sstevel@tonic-gate 	gss_parms->qop_rcvd = (uint_t)client_data->qop;
6960Sstevel@tonic-gate 	gss_parms->context = (void *)client_data->context;
6970Sstevel@tonic-gate 	gss_parms->seq_num = creds.seq_num;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	if (!client_data->established) {
7000Sstevel@tonic-gate 		if (creds.gss_proc == RPCSEC_GSS_DATA) {
7010Sstevel@tonic-gate 			ret = RPCSEC_GSS_FAILED;
7020Sstevel@tonic-gate 			client_data->stale = TRUE;
7030Sstevel@tonic-gate 			goto error2;
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		/*
7070Sstevel@tonic-gate 		 * If the context is not established, then only GSSAPI_INIT
7080Sstevel@tonic-gate 		 * and _CONTINUE requests are valid.
7090Sstevel@tonic-gate 		 */
7100Sstevel@tonic-gate 		if (creds.gss_proc != RPCSEC_GSS_INIT && creds.gss_proc !=
7110Sstevel@tonic-gate 						RPCSEC_GSS_CONTINUE_INIT) {
7120Sstevel@tonic-gate 			ret = RPCSEC_GSS_FAILED;
7130Sstevel@tonic-gate 			client_data->stale = TRUE;
7140Sstevel@tonic-gate 			goto error2;
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		/*
7180Sstevel@tonic-gate 		 * call is for us, deserialize arguments
7190Sstevel@tonic-gate 		 */
7200Sstevel@tonic-gate 		memset(&call_arg, 0, sizeof (call_arg));
7210Sstevel@tonic-gate 		if (!svc_getargs(rqst->rq_xprt, __xdr_rpc_gss_init_arg,
7220Sstevel@tonic-gate 							(caddr_t)&call_arg)) {
7230Sstevel@tonic-gate 			ret = RPCSEC_GSS_FAILED;
7240Sstevel@tonic-gate 			client_data->stale = TRUE;
7250Sstevel@tonic-gate 			goto error2;
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		gssstat = GSS_S_FAILURE;
7290Sstevel@tonic-gate 		minor_stat = 0;
7300Sstevel@tonic-gate 		rw_rdlock(&cred_lock);
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * set next sc to point to the server cred
7330Sstevel@tonic-gate 		 * if the  client_data contains server_creds
7340Sstevel@tonic-gate 		 */
7350Sstevel@tonic-gate 		for (sc = svc_creds_list; sc != NULL; sc = sc->next) {
7360Sstevel@tonic-gate 			if (rqst->rq_prog != sc->program ||
7370Sstevel@tonic-gate 					rqst->rq_vers != sc->version)
7380Sstevel@tonic-gate 				continue;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 			mutex_lock(&sc->refresh_mutex);
7410Sstevel@tonic-gate 			gssstat = gss_accept_sec_context(&minor_stat,
7420Sstevel@tonic-gate 				&client_data->context,
7430Sstevel@tonic-gate 				sc->cred,
7440Sstevel@tonic-gate 				&call_arg,
7450Sstevel@tonic-gate 				GSS_C_NO_CHANNEL_BINDINGS,
7460Sstevel@tonic-gate 				&client_data->client_name,
7470Sstevel@tonic-gate 				&mech_type,
7480Sstevel@tonic-gate 				&output_token,
7490Sstevel@tonic-gate 				&ret_flags,
7500Sstevel@tonic-gate 				&time_rec,
7510Sstevel@tonic-gate 				NULL);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 			if (gssstat == GSS_S_CREDENTIALS_EXPIRED) {
7540Sstevel@tonic-gate 				if (rpc_gss_refresh_svc_cred(sc)) {
7550Sstevel@tonic-gate 					gssstat = gss_accept_sec_context(
7560Sstevel@tonic-gate 						&minor_stat,
7570Sstevel@tonic-gate 						&client_data->context,
7580Sstevel@tonic-gate 						sc->cred,
7590Sstevel@tonic-gate 						&call_arg,
7600Sstevel@tonic-gate 						GSS_C_NO_CHANNEL_BINDINGS,
7610Sstevel@tonic-gate 						&client_data->client_name,
7620Sstevel@tonic-gate 						&mech_type,
7630Sstevel@tonic-gate 						&output_token,
7640Sstevel@tonic-gate 						&ret_flags,
7650Sstevel@tonic-gate 						&time_rec,
7660Sstevel@tonic-gate 						NULL);
7670Sstevel@tonic-gate 					mutex_unlock(&sc->refresh_mutex);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 				} else {
7700Sstevel@tonic-gate 					mutex_unlock(&sc->refresh_mutex);
7710Sstevel@tonic-gate 					gssstat = GSS_S_NO_CRED;
7720Sstevel@tonic-gate 					break;
7730Sstevel@tonic-gate 				}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 			} else
7760Sstevel@tonic-gate 				mutex_unlock(&sc->refresh_mutex);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 			if (gssstat == GSS_S_COMPLETE) {
7790Sstevel@tonic-gate 				/*
7800Sstevel@tonic-gate 				 * Server_creds was right - set it.  Also
7810Sstevel@tonic-gate 				 * set the raw and unix credentials at this
7820Sstevel@tonic-gate 				 * point.  This saves a lot of computation
7830Sstevel@tonic-gate 				 * later when credentials are retrieved.
7840Sstevel@tonic-gate 				 */
7850Sstevel@tonic-gate 				/*
7860Sstevel@tonic-gate 				 * XXX server_creds will prob be stale
7870Sstevel@tonic-gate 				 * after rpc_gss_refresh_svc_cred(), but
7880Sstevel@tonic-gate 				 * it appears not to ever be referenced
7890Sstevel@tonic-gate 				 * anyways.
7900Sstevel@tonic-gate 				 */
7910Sstevel@tonic-gate 				mutex_lock(&sc->refresh_mutex);
7920Sstevel@tonic-gate 				client_data->server_creds = sc->cred;
7930Sstevel@tonic-gate 				client_data->raw_cred.version = creds.version;
7940Sstevel@tonic-gate 				client_data->raw_cred.service = creds.service;
7950Sstevel@tonic-gate 				client_data->raw_cred.svc_principal =
7960Sstevel@tonic-gate 						sc->server_name;
7970Sstevel@tonic-gate 				mutex_unlock(&sc->refresh_mutex);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 				if ((client_data->raw_cred.mechanism
8000Sstevel@tonic-gate 					= __rpc_gss_oid_to_mech(mech_type))
8010Sstevel@tonic-gate 					== NULL) {
8020Sstevel@tonic-gate 					gssstat = GSS_S_FAILURE;
8030Sstevel@tonic-gate 					(void) gss_release_buffer(&minor_stat,
8040Sstevel@tonic-gate 								&output_token);
8050Sstevel@tonic-gate 				} else if (!set_client_principal(client_data->
8060Sstevel@tonic-gate 					client_name, &client_data->
8070Sstevel@tonic-gate 					raw_cred.client_principal)) {
8080Sstevel@tonic-gate 					gssstat = GSS_S_FAILURE;
8090Sstevel@tonic-gate 					(void) gss_release_buffer(&minor_stat,
8100Sstevel@tonic-gate 								&output_token);
8110Sstevel@tonic-gate 				}
8120Sstevel@tonic-gate 				break;
8130Sstevel@tonic-gate 			}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 			if (gssstat == GSS_S_CONTINUE_NEEDED) {
8160Sstevel@tonic-gate 				/*
8170Sstevel@tonic-gate 				 * XXX server_creds will prob be stale
8180Sstevel@tonic-gate 				 * after rpc_gss_refresh_svc_cred(), but
8190Sstevel@tonic-gate 				 * it appears not to ever be referenced
8200Sstevel@tonic-gate 				 * anyways.
8210Sstevel@tonic-gate 				 */
8220Sstevel@tonic-gate 				mutex_lock(&sc->refresh_mutex);
8230Sstevel@tonic-gate 				client_data->server_creds = sc->cred;
8240Sstevel@tonic-gate 				mutex_unlock(&sc->refresh_mutex);
8250Sstevel@tonic-gate 				break;
8260Sstevel@tonic-gate 			}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 		rw_unlock(&cred_lock);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		call_res.gss_major = gssstat;
8320Sstevel@tonic-gate 		call_res.gss_minor = minor_stat;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_arg, (caddr_t)&call_arg);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		if (gssstat != GSS_S_COMPLETE &&
8370Sstevel@tonic-gate 					gssstat != GSS_S_CONTINUE_NEEDED) {
8380Sstevel@tonic-gate 			/*
8390Sstevel@tonic-gate 			 * We have a failure - send response and delete
8400Sstevel@tonic-gate 			 * the context.  Don't dispatch. Set ctx_handle
8410Sstevel@tonic-gate 			 * to NULL and seq_window to 0.
8420Sstevel@tonic-gate 			 */
8430Sstevel@tonic-gate 			call_res.ctx_handle.length = 0;
8440Sstevel@tonic-gate 			call_res.ctx_handle.value = NULL;
8450Sstevel@tonic-gate 			call_res.seq_window = 0;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 			svc_sendreply(rqst->rq_xprt, __xdr_rpc_gss_init_res,
8480Sstevel@tonic-gate 						(caddr_t)&call_res);
8490Sstevel@tonic-gate 			*no_dispatch = TRUE;
8500Sstevel@tonic-gate 			ret = AUTH_OK;
8510Sstevel@tonic-gate 			client_data->stale = TRUE;
8520Sstevel@tonic-gate 			goto error2;
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		/*
8560Sstevel@tonic-gate 		 * This step succeeded.  Send a response, along with
8570Sstevel@tonic-gate 		 * a token if there's one.  Don't dispatch.
8580Sstevel@tonic-gate 		 */
8590Sstevel@tonic-gate 		if (output_token.length != 0) {
8600Sstevel@tonic-gate 			GSS_COPY_BUFFER(call_res.token, output_token);
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		/*
8640Sstevel@tonic-gate 		 * set response verifier: checksum of SEQ_WIN
8650Sstevel@tonic-gate 		 */
8660Sstevel@tonic-gate 		if (gssstat == GSS_S_COMPLETE) {
8670Sstevel@tonic-gate 			if (!set_response_verf(rqst, msg, client_data,
8680Sstevel@tonic-gate 				(uint_t)SEQ_WIN)) {
8690Sstevel@tonic-gate 				ret = RPCSEC_GSS_FAILED;
8700Sstevel@tonic-gate 				client_data->stale = TRUE;
8710Sstevel@tonic-gate 				(void) gss_release_buffer(&minor_stat,
8720Sstevel@tonic-gate 							&output_token);
8730Sstevel@tonic-gate 				goto error2;
8740Sstevel@tonic-gate 			}
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		svc_sendreply(rqst->rq_xprt, __xdr_rpc_gss_init_res,
8780Sstevel@tonic-gate 							(caddr_t)&call_res);
8790Sstevel@tonic-gate 		/*
8800Sstevel@tonic-gate 		 * Cache last response in case it is lost and the client
8810Sstevel@tonic-gate 		 * retries on an established context.
8820Sstevel@tonic-gate 		 */
8830Sstevel@tonic-gate 		(void) retrans_add(client_data, msg->rm_xid, &call_res);
8840Sstevel@tonic-gate 		*no_dispatch = TRUE;
8850Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &output_token);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		/*
8880Sstevel@tonic-gate 		 * If appropriate, set established to TRUE *after* sending
8890Sstevel@tonic-gate 		 * response (otherwise, the client will receive the final
8900Sstevel@tonic-gate 		 * token encrypted)
8910Sstevel@tonic-gate 		 */
8920Sstevel@tonic-gate 		if (gssstat == GSS_S_COMPLETE) {
8930Sstevel@tonic-gate 			/*
8940Sstevel@tonic-gate 			 * Context is established.  Set expiry time for
8950Sstevel@tonic-gate 			 * context (the minimum of time_rec and max_lifetime).
8960Sstevel@tonic-gate 			 */
8970Sstevel@tonic-gate 			client_data->seq_num = 1;
8980Sstevel@tonic-gate 			if (time_rec == GSS_C_INDEFINITE) {
8990Sstevel@tonic-gate 				if (max_lifetime != GSS_C_INDEFINITE)
9000Sstevel@tonic-gate 					client_data->expiration =
9010Sstevel@tonic-gate 							max_lifetime + time(0);
9020Sstevel@tonic-gate 				else
9030Sstevel@tonic-gate 					client_data->expiration =
9040Sstevel@tonic-gate 							GSS_C_INDEFINITE;
9050Sstevel@tonic-gate 			} else if (max_lifetime == GSS_C_INDEFINITE ||
9060Sstevel@tonic-gate 							max_lifetime > time_rec)
9070Sstevel@tonic-gate 				client_data->expiration = time_rec + time(0);
9080Sstevel@tonic-gate 			else
9090Sstevel@tonic-gate 				client_data->expiration = max_lifetime +
9100Sstevel@tonic-gate 								time(0);
9110Sstevel@tonic-gate 			client_data->established = TRUE;
9120Sstevel@tonic-gate 		}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	} else {
9150Sstevel@tonic-gate 		if ((creds.gss_proc != RPCSEC_GSS_DATA) &&
9160Sstevel@tonic-gate 			(creds.gss_proc != RPCSEC_GSS_DESTROY)) {
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		    switch (creds.gss_proc) {
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		    case RPCSEC_GSS_CONTINUE_INIT:
9210Sstevel@tonic-gate 			/*
9220Sstevel@tonic-gate 			 * This is an established context. Continue to
9230Sstevel@tonic-gate 			 * satisfy retried continue init requests out of
9240Sstevel@tonic-gate 			 * the retransmit cache.  Throw away any that don't
9250Sstevel@tonic-gate 			 * have a matching xid or the cach is empty.
9260Sstevel@tonic-gate 			 * Delete the retransmit cache once the client sends
9270Sstevel@tonic-gate 			 * a data request.
9280Sstevel@tonic-gate 			 */
9290Sstevel@tonic-gate 			if (client_data->retrans_data &&
9300Sstevel@tonic-gate 			    (client_data->retrans_data->xid == msg->rm_xid)) {
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 			    retrans_result = &client_data->retrans_data->result;
9330Sstevel@tonic-gate 			    if (set_response_verf(rqst, msg, client_data,
9340Sstevel@tonic-gate 				(uint_t)retrans_result->seq_window)) {
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 				gss_parms->established = FALSE;
9370Sstevel@tonic-gate 				svc_sendreply(rqst->rq_xprt,
9380Sstevel@tonic-gate 					__xdr_rpc_gss_init_res,
9390Sstevel@tonic-gate 					(caddr_t)retrans_result);
9400Sstevel@tonic-gate 				*no_dispatch = TRUE;
9410Sstevel@tonic-gate 				goto success;
9420Sstevel@tonic-gate 			    }
9430Sstevel@tonic-gate 			}
9440Sstevel@tonic-gate 			/* fall thru to default */
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		    default:
9470Sstevel@tonic-gate 			syslog(LOG_ERR, "_svcrpcsec_gss: non-data request "
9480Sstevel@tonic-gate 				"on an established context");
9490Sstevel@tonic-gate 			ret = AUTH_FAILED;
9500Sstevel@tonic-gate 			goto error2;
9510Sstevel@tonic-gate 		    }
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		/*
9550Sstevel@tonic-gate 		 * Once the context is established and there is no more
9560Sstevel@tonic-gate 		 * retransmission of last continue init request, it is safe
9570Sstevel@tonic-gate 		 * to delete the retransmit cache entry.
9580Sstevel@tonic-gate 		 */
9590Sstevel@tonic-gate 		if (client_data->retrans_data)
9600Sstevel@tonic-gate 			retrans_del(client_data);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		/*
9630Sstevel@tonic-gate 		 * Context is already established.  Check verifier, and
9640Sstevel@tonic-gate 		 * note parameters we will need for response in gss_parms.
9650Sstevel@tonic-gate 		 */
9660Sstevel@tonic-gate 		if (!check_verf(msg, client_data->context,
9670Sstevel@tonic-gate 						&gss_parms->qop_rcvd)) {
9680Sstevel@tonic-gate 			ret = RPCSEC_GSS_NOCRED;
9690Sstevel@tonic-gate 			goto error2;
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 		/*
9720Sstevel@tonic-gate 		 *  Check and invoke callback if necessary.
9730Sstevel@tonic-gate 		 */
9740Sstevel@tonic-gate 		if (!client_data->done_docallback) {
9750Sstevel@tonic-gate 			client_data->done_docallback = TRUE;
9760Sstevel@tonic-gate 			client_data->qop = gss_parms->qop_rcvd;
9770Sstevel@tonic-gate 			client_data->raw_cred.qop = __rpc_gss_num_to_qop(
9780Sstevel@tonic-gate 						client_data->raw_cred.mechanism,
9790Sstevel@tonic-gate 							gss_parms->qop_rcvd);
9800Sstevel@tonic-gate 			client_data->raw_cred.service = creds.service;
9810Sstevel@tonic-gate 			if (!do_callback(rqst, client_data)) {
9820Sstevel@tonic-gate 				ret = AUTH_FAILED;
9830Sstevel@tonic-gate 				client_data->stale = TRUE;
9840Sstevel@tonic-gate 				goto error2;
9850Sstevel@tonic-gate 			}
9860Sstevel@tonic-gate 		}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 		/*
9890Sstevel@tonic-gate 		 * If the context was locked, make sure that the client
9900Sstevel@tonic-gate 		 * has not changed QOP.
9910Sstevel@tonic-gate 		 */
9920Sstevel@tonic-gate 		if (client_data->locked &&
9930Sstevel@tonic-gate 				gss_parms->qop_rcvd != client_data->qop) {
9940Sstevel@tonic-gate 			ret = AUTH_BADVERF;
9950Sstevel@tonic-gate 			goto error2;
9960Sstevel@tonic-gate 		}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 		/*
9990Sstevel@tonic-gate 		 * Validate sequence number.
10000Sstevel@tonic-gate 		 */
10010Sstevel@tonic-gate 		if (!check_seq(client_data, creds.seq_num,
10020Sstevel@tonic-gate 						&client_data->stale)) {
10030Sstevel@tonic-gate 			if (client_data->stale)
10040Sstevel@tonic-gate 				ret = RPCSEC_GSS_FAILED;
10050Sstevel@tonic-gate 			else {
10060Sstevel@tonic-gate 				/*
10070Sstevel@tonic-gate 				 * Operational error, drop packet silently.
10080Sstevel@tonic-gate 				 * The client will recover after timing out,
10090Sstevel@tonic-gate 				 * assuming this is a client error and not
10100Sstevel@tonic-gate 				 * a relpay attack.  Don't dispatch.
10110Sstevel@tonic-gate 				 */
10120Sstevel@tonic-gate 				ret = AUTH_OK;
10130Sstevel@tonic-gate 				*no_dispatch = TRUE;
10140Sstevel@tonic-gate 			}
10150Sstevel@tonic-gate 			goto error2;
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 		/*
10190Sstevel@tonic-gate 		 * set response verifier
10200Sstevel@tonic-gate 		 */
10210Sstevel@tonic-gate 		if (!set_response_verf(rqst, msg, client_data, creds.seq_num)) {
10220Sstevel@tonic-gate 			ret = RPCSEC_GSS_FAILED;
10230Sstevel@tonic-gate 			client_data->stale = TRUE;
10240Sstevel@tonic-gate 			goto error2;
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		/*
10280Sstevel@tonic-gate 		 * If this is a control message RPCSEC_GSS_DESTROY, process
10290Sstevel@tonic-gate 		 * the call; otherwise, return AUTH_OK so it will be
10300Sstevel@tonic-gate 		 * dispatched to the application server.
10310Sstevel@tonic-gate 		 */
10320Sstevel@tonic-gate 		if (creds.gss_proc == RPCSEC_GSS_DESTROY) {
10330Sstevel@tonic-gate 			svc_sendreply(rqst->rq_xprt, xdr_void, NULL);
10340Sstevel@tonic-gate 			*no_dispatch = TRUE;
10350Sstevel@tonic-gate 			client_data->stale = TRUE;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		} else {
10380Sstevel@tonic-gate 			/*
10390Sstevel@tonic-gate 			 * This should be an RPCSEC_GSS_DATA request.
10400Sstevel@tonic-gate 			 * If context is locked, make sure that the client
10410Sstevel@tonic-gate 			 * has not changed the security service.
10420Sstevel@tonic-gate 			 */
10430Sstevel@tonic-gate 			if (client_data->locked &&
10440Sstevel@tonic-gate 			    client_data->raw_cred.service != creds.service) {
10450Sstevel@tonic-gate 				ret = AUTH_FAILED;
10460Sstevel@tonic-gate 				goto error2;
10470Sstevel@tonic-gate 			}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 			/*
10500Sstevel@tonic-gate 			 * Set client credentials to raw credential
10510Sstevel@tonic-gate 			 * structure in context.  This is okay, since
10520Sstevel@tonic-gate 			 * this will not change during the lifetime of
10530Sstevel@tonic-gate 			 * the context (so it's MT safe).
10540Sstevel@tonic-gate 			 */
10550Sstevel@tonic-gate 			rqst->rq_clntcred = (char *)&client_data->raw_cred;
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate success:
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * Success.
10620Sstevel@tonic-gate 	 */
10630Sstevel@tonic-gate 	if (creds.ctx_handle.length != 0)
10640Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_creds, (caddr_t)&creds);
10650Sstevel@tonic-gate 	mutex_unlock(&client_data->clm);
10660Sstevel@tonic-gate 	return (AUTH_OK);
10670Sstevel@tonic-gate error2:
10680Sstevel@tonic-gate 	mutex_unlock(&client_data->clm);
10690Sstevel@tonic-gate error:
10700Sstevel@tonic-gate 	/*
10710Sstevel@tonic-gate 	 * Failure.
10720Sstevel@tonic-gate 	 */
10730Sstevel@tonic-gate 	if (creds.ctx_handle.length != 0)
10740Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_creds, (caddr_t)&creds);
10750Sstevel@tonic-gate 	return (ret);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate /*
10790Sstevel@tonic-gate  * Check verifier.  The verifier is the checksum of the RPC header
10800Sstevel@tonic-gate  * upto and including the credentials field.
10810Sstevel@tonic-gate  */
10820Sstevel@tonic-gate static bool_t
check_verf(msg,context,qop_state)10830Sstevel@tonic-gate check_verf(msg, context, qop_state)
10840Sstevel@tonic-gate 	struct rpc_msg		*msg;
10850Sstevel@tonic-gate 	gss_ctx_id_t		context;
10860Sstevel@tonic-gate 	int			*qop_state;
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate 	int			*buf, *tmp;
10890Sstevel@tonic-gate 	int			hdr[32];
10900Sstevel@tonic-gate 	struct opaque_auth	*oa;
10910Sstevel@tonic-gate 	int			len;
10920Sstevel@tonic-gate 	gss_buffer_desc		msg_buf;
10930Sstevel@tonic-gate 	gss_buffer_desc		tok_buf;
10940Sstevel@tonic-gate 	OM_uint32		gssstat, minor_stat;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	/*
10970Sstevel@tonic-gate 	 * We have to reconstruct the RPC header from the previously
10980Sstevel@tonic-gate 	 * parsed information, since we haven't kept the header intact.
10990Sstevel@tonic-gate 	 */
11005101Spk193450 
11015101Spk193450 	oa = &msg->rm_call.cb_cred;
11025101Spk193450 	if (oa->oa_length > MAX_AUTH_BYTES)
11035101Spk193450 		return (FALSE);
11045101Spk193450 
11055101Spk193450 	/* 8 XDR units from the IXDR macro calls. */
11065101Spk193450 	if (sizeof (hdr) < (8 * BYTES_PER_XDR_UNIT +
11075101Spk193450 			    RNDUP(oa->oa_length)))
11085101Spk193450 		return (FALSE);
11090Sstevel@tonic-gate 	buf = hdr;
11105101Spk193450 
11110Sstevel@tonic-gate 	IXDR_PUT_U_INT32(buf, msg->rm_xid);
11120Sstevel@tonic-gate 	IXDR_PUT_ENUM(buf, msg->rm_direction);
11130Sstevel@tonic-gate 	IXDR_PUT_U_INT32(buf, msg->rm_call.cb_rpcvers);
11140Sstevel@tonic-gate 	IXDR_PUT_U_INT32(buf, msg->rm_call.cb_prog);
11150Sstevel@tonic-gate 	IXDR_PUT_U_INT32(buf, msg->rm_call.cb_vers);
11160Sstevel@tonic-gate 	IXDR_PUT_U_INT32(buf, msg->rm_call.cb_proc);
11170Sstevel@tonic-gate 	IXDR_PUT_ENUM(buf, oa->oa_flavor);
11180Sstevel@tonic-gate 	IXDR_PUT_U_INT32(buf, oa->oa_length);
11190Sstevel@tonic-gate 	if (oa->oa_length) {
11200Sstevel@tonic-gate 		len = RNDUP(oa->oa_length);
11210Sstevel@tonic-gate 		tmp = buf;
11220Sstevel@tonic-gate 		buf += len / sizeof (int);
11230Sstevel@tonic-gate 		*(buf - 1) = 0;
11240Sstevel@tonic-gate 		(void) memcpy((caddr_t)tmp, oa->oa_base, oa->oa_length);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	len = ((char *)buf) - (char *)hdr;
11270Sstevel@tonic-gate 	msg_buf.length = len;
11280Sstevel@tonic-gate 	msg_buf.value = (char *)hdr;
11290Sstevel@tonic-gate 	oa = &msg->rm_call.cb_verf;
11300Sstevel@tonic-gate 	tok_buf.length = oa->oa_length;
11310Sstevel@tonic-gate 	tok_buf.value = oa->oa_base;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	gssstat = gss_verify(&minor_stat, context, &msg_buf, &tok_buf,
11340Sstevel@tonic-gate 				qop_state);
11350Sstevel@tonic-gate 	if (gssstat != GSS_S_COMPLETE)
11360Sstevel@tonic-gate 		return (FALSE);
11370Sstevel@tonic-gate 	return (TRUE);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate  * Set response verifier.  This is the checksum of the given number.
11420Sstevel@tonic-gate  * (e.g. sequence number or sequence window)
11430Sstevel@tonic-gate  */
11440Sstevel@tonic-gate static bool_t
set_response_verf(rqst,msg,cl,num)11450Sstevel@tonic-gate set_response_verf(rqst, msg, cl, num)
11460Sstevel@tonic-gate 	struct svc_req		*rqst;
11470Sstevel@tonic-gate 	struct rpc_msg		*msg;
11480Sstevel@tonic-gate 	svc_rpc_gss_data	*cl;
11490Sstevel@tonic-gate 	uint_t			num;
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate 	OM_uint32		minor;
11520Sstevel@tonic-gate 	gss_buffer_desc		in_buf, out_buf;
11530Sstevel@tonic-gate 	uint_t			num_net;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	num_net = (uint_t)htonl(num);
11560Sstevel@tonic-gate 	in_buf.length = sizeof (num);
11570Sstevel@tonic-gate 	in_buf.value = (char *)&num_net;
11580Sstevel@tonic-gate 	if (gss_sign(&minor, cl->context, cl->qop, &in_buf,
11590Sstevel@tonic-gate 					&out_buf) != GSS_S_COMPLETE)
11600Sstevel@tonic-gate 		return (FALSE);
11610Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_flavor = RPCSEC_GSS;
11620Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
11630Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_length = out_buf.length;
11640Sstevel@tonic-gate 	memcpy(rqst->rq_xprt->xp_verf.oa_base, out_buf.value,
11650Sstevel@tonic-gate 		out_buf.length);
11660Sstevel@tonic-gate 	(void) gss_release_buffer(&minor, &out_buf);
11670Sstevel@tonic-gate 	return (TRUE);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate  * Create client context.
11720Sstevel@tonic-gate  */
11730Sstevel@tonic-gate static svc_rpc_gss_data *
create_client()11740Sstevel@tonic-gate create_client()
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate 	svc_rpc_gss_data	*client_data;
11770Sstevel@tonic-gate 	static uint_t		key = 1;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	client_data = (svc_rpc_gss_data *) malloc(sizeof (*client_data));
11800Sstevel@tonic-gate 	if (client_data == NULL)
11810Sstevel@tonic-gate 		return (NULL);
11820Sstevel@tonic-gate 	memset((char *)client_data, 0, sizeof (*client_data));
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	/*
11850Sstevel@tonic-gate 	 * set up client data structure
11860Sstevel@tonic-gate 	 */
11870Sstevel@tonic-gate 	client_data->established = FALSE;
11880Sstevel@tonic-gate 	client_data->locked = FALSE;
11890Sstevel@tonic-gate 	client_data->u_cred_set = FALSE;
11900Sstevel@tonic-gate 	client_data->context = GSS_C_NO_CONTEXT;
11910Sstevel@tonic-gate 	client_data->expiration = init_lifetime + time(0);
11920Sstevel@tonic-gate 	client_data->ref_cnt = 1;
11930Sstevel@tonic-gate 	client_data->qop = GSS_C_QOP_DEFAULT;
11940Sstevel@tonic-gate 	client_data->done_docallback = FALSE;
11950Sstevel@tonic-gate 	client_data->stale = FALSE;
11960Sstevel@tonic-gate 	client_data->time_secs_set = 0;
11970Sstevel@tonic-gate 	client_data->retrans_data = NULL;
11980Sstevel@tonic-gate 	mutex_init(&client_data->clm, USYNC_THREAD, NULL);
11990Sstevel@tonic-gate 	/*
12000Sstevel@tonic-gate 	 * Check totals.  If we've hit the limit, we destroy a context
12010Sstevel@tonic-gate 	 * based on LRU method.
12020Sstevel@tonic-gate 	 */
12030Sstevel@tonic-gate 	mutex_lock(&ctx_mutex);
12040Sstevel@tonic-gate 	if (num_gss_contexts >= max_gss_contexts) {
12050Sstevel@tonic-gate 		/*
12060Sstevel@tonic-gate 		 * now try on LRU basis
12070Sstevel@tonic-gate 		 */
12080Sstevel@tonic-gate 		drop_lru_client();
12090Sstevel@tonic-gate 		if (num_gss_contexts >= max_gss_contexts) {
12100Sstevel@tonic-gate 			mutex_unlock(&ctx_mutex);
12110Sstevel@tonic-gate 			free((char *)client_data);
12120Sstevel@tonic-gate 			return (NULL);
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
12170Sstevel@tonic-gate 	 * The client context handle is a 32-bit key (unsigned int).
12180Sstevel@tonic-gate 	 * The key is incremented until there is no duplicate for it.
12190Sstevel@tonic-gate 	 */
12200Sstevel@tonic-gate 	for (;;) {
12210Sstevel@tonic-gate 		client_data->key = key++;
12220Sstevel@tonic-gate 		if (find_client(client_data->key) == NULL) {
12230Sstevel@tonic-gate 			insert_client(client_data);
12240Sstevel@tonic-gate 			/*
12250Sstevel@tonic-gate 			 * Set cleanup callback if we haven't.
12260Sstevel@tonic-gate 			 */
12270Sstevel@tonic-gate 			if (!cleanup_cb_set) {
12280Sstevel@tonic-gate 				old_cleanup_cb =
1229*6636Sgtb 				    (void (*)()) __svc_set_proc_cleanup_cb(
1230*6636Sgtb 				    (void *)ctx_cleanup);
12310Sstevel@tonic-gate 				cleanup_cb_set = TRUE;
12320Sstevel@tonic-gate 			}
12330Sstevel@tonic-gate 			mutex_unlock(&ctx_mutex);
12340Sstevel@tonic-gate 			return (client_data);
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate 	/*NOTREACHED*/
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * Insert client context into hash list and LRU list.
12420Sstevel@tonic-gate  */
12430Sstevel@tonic-gate static void
insert_client(client_data)12440Sstevel@tonic-gate insert_client(client_data)
12450Sstevel@tonic-gate 	svc_rpc_gss_data	*client_data;
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	svc_rpc_gss_data	*cl;
12480Sstevel@tonic-gate 	int			index = (client_data->key & HASHMASK);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	client_data->prev = NULL;
12510Sstevel@tonic-gate 	cl = clients[index];
12520Sstevel@tonic-gate 	if ((client_data->next = cl) != NULL)
12530Sstevel@tonic-gate 		cl->prev = client_data;
12540Sstevel@tonic-gate 	clients[index] = client_data;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	client_data->lru_prev = NULL;
12570Sstevel@tonic-gate 	if ((client_data->lru_next = lru_first) != NULL)
12580Sstevel@tonic-gate 		lru_first->lru_prev = client_data;
12590Sstevel@tonic-gate 	else
12600Sstevel@tonic-gate 		lru_last = client_data;
12610Sstevel@tonic-gate 	lru_first = client_data;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	num_gss_contexts++;
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate  * Fetch a client, given the client context handle.  Move it to the
12680Sstevel@tonic-gate  * top of the LRU list since this is the most recently used context.
12690Sstevel@tonic-gate  */
12700Sstevel@tonic-gate static svc_rpc_gss_data *
get_client(ctx_handle)12710Sstevel@tonic-gate get_client(ctx_handle)
12720Sstevel@tonic-gate 	gss_buffer_t		ctx_handle;
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate 	uint_t			key = *(uint_t *)ctx_handle->value;
12750Sstevel@tonic-gate 	svc_rpc_gss_data	*cl;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	mutex_lock(&ctx_mutex);
12780Sstevel@tonic-gate 	if ((cl = find_client(key)) != NULL) {
12790Sstevel@tonic-gate 		mutex_lock(&cl->clm);
12800Sstevel@tonic-gate 		if (cl->stale) {
12810Sstevel@tonic-gate 			mutex_unlock(&cl->clm);
12820Sstevel@tonic-gate 			mutex_unlock(&ctx_mutex);
12830Sstevel@tonic-gate 			return (NULL);
12840Sstevel@tonic-gate 		}
12850Sstevel@tonic-gate 		cl->ref_cnt++;
12860Sstevel@tonic-gate 		mutex_unlock(&cl->clm);
12870Sstevel@tonic-gate 		if (cl != lru_first) {
12880Sstevel@tonic-gate 			cl->lru_prev->lru_next = cl->lru_next;
12890Sstevel@tonic-gate 			if (cl->lru_next != NULL)
12900Sstevel@tonic-gate 				cl->lru_next->lru_prev = cl->lru_prev;
12910Sstevel@tonic-gate 			else
12920Sstevel@tonic-gate 				lru_last = cl->lru_prev;
12930Sstevel@tonic-gate 			cl->lru_prev = NULL;
12940Sstevel@tonic-gate 			cl->lru_next = lru_first;
12950Sstevel@tonic-gate 			lru_first->lru_prev = cl;
12960Sstevel@tonic-gate 			lru_first = cl;
12970Sstevel@tonic-gate 		}
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 	mutex_unlock(&ctx_mutex);
13000Sstevel@tonic-gate 	return (cl);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate /*
13040Sstevel@tonic-gate  * Given the client context handle, find the context corresponding to it.
13050Sstevel@tonic-gate  * Don't change its LRU state since it may not be used.
13060Sstevel@tonic-gate  */
13070Sstevel@tonic-gate static svc_rpc_gss_data *
find_client(key)13080Sstevel@tonic-gate find_client(key)
13090Sstevel@tonic-gate 	uint_t			key;
13100Sstevel@tonic-gate {
13110Sstevel@tonic-gate 	int			index = (key & HASHMASK);
13120Sstevel@tonic-gate 	svc_rpc_gss_data	*cl;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	for (cl = clients[index]; cl != NULL; cl = cl->next) {
13150Sstevel@tonic-gate 		if (cl->key == key)
13160Sstevel@tonic-gate 			break;
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 	return (cl);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate  * Destroy a client context.
13230Sstevel@tonic-gate  */
13240Sstevel@tonic-gate static void
destroy_client(client_data)13250Sstevel@tonic-gate destroy_client(client_data)
13260Sstevel@tonic-gate 	svc_rpc_gss_data	*client_data;
13270Sstevel@tonic-gate {
13280Sstevel@tonic-gate 	OM_uint32		minor;
13290Sstevel@tonic-gate 	int			index = (client_data->key & HASHMASK);
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	/*
13320Sstevel@tonic-gate 	 * remove from hash list
13330Sstevel@tonic-gate 	 */
13340Sstevel@tonic-gate 	if (client_data->prev == NULL)
13350Sstevel@tonic-gate 		clients[index] = client_data->next;
13360Sstevel@tonic-gate 	else
13370Sstevel@tonic-gate 		client_data->prev->next = client_data->next;
13380Sstevel@tonic-gate 	if (client_data->next != NULL)
13390Sstevel@tonic-gate 		client_data->next->prev = client_data->prev;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	/*
13420Sstevel@tonic-gate 	 * remove from LRU list
13430Sstevel@tonic-gate 	 */
13440Sstevel@tonic-gate 	if (client_data->lru_prev == NULL)
13450Sstevel@tonic-gate 		lru_first = client_data->lru_next;
13460Sstevel@tonic-gate 	else
13470Sstevel@tonic-gate 		client_data->lru_prev->lru_next = client_data->lru_next;
13480Sstevel@tonic-gate 	if (client_data->lru_next != NULL)
13490Sstevel@tonic-gate 		client_data->lru_next->lru_prev = client_data->lru_prev;
13500Sstevel@tonic-gate 	else
13510Sstevel@tonic-gate 		lru_last = client_data->lru_prev;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * If there is a GSS context, clean up GSS state.
13550Sstevel@tonic-gate 	 */
13560Sstevel@tonic-gate 	if (client_data->context != GSS_C_NO_CONTEXT) {
13570Sstevel@tonic-gate 		(void) gss_delete_sec_context(&minor, &client_data->context,
13580Sstevel@tonic-gate 								NULL);
13590Sstevel@tonic-gate 		if (client_data->client_name)
13600Sstevel@tonic-gate 		    (void) gss_release_name(&minor, &client_data->client_name);
13610Sstevel@tonic-gate 		if (client_data->raw_cred.client_principal)
13620Sstevel@tonic-gate 		    free((char *)client_data->raw_cred.client_principal);
13630Sstevel@tonic-gate 		if (client_data->u_cred.gidlist != NULL)
13640Sstevel@tonic-gate 		    free((char *)client_data->u_cred.gidlist);
13650Sstevel@tonic-gate 		if (client_data->deleg != GSS_C_NO_CREDENTIAL)
13660Sstevel@tonic-gate 		    (void) gss_release_cred(&minor, &client_data->deleg);
13670Sstevel@tonic-gate 	}
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	if (client_data->retrans_data != NULL)
13700Sstevel@tonic-gate 		retrans_del(client_data);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	free(client_data);
13730Sstevel@tonic-gate 	num_gss_contexts--;
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate /*
13770Sstevel@tonic-gate  * Check for expired client contexts.
13780Sstevel@tonic-gate  */
13790Sstevel@tonic-gate static void
sweep_clients()13800Sstevel@tonic-gate sweep_clients()
13810Sstevel@tonic-gate {
13820Sstevel@tonic-gate 	svc_rpc_gss_data	*cl, *next;
13830Sstevel@tonic-gate 	int			index;
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	for (index = 0; index < HASHMOD; index++) {
13860Sstevel@tonic-gate 		cl = clients[index];
13870Sstevel@tonic-gate 		while (cl) {
13880Sstevel@tonic-gate 			next = cl->next;
13890Sstevel@tonic-gate 			mutex_lock(&cl->clm);
13900Sstevel@tonic-gate 			if ((cl->expiration != GSS_C_INDEFINITE &&
13910Sstevel@tonic-gate 			    cl->expiration <= time(0)) || cl->stale) {
13920Sstevel@tonic-gate 				cl->stale = TRUE;
13930Sstevel@tonic-gate 				if (cl->ref_cnt == 0) {
13940Sstevel@tonic-gate 					mutex_unlock(&cl->clm);
13950Sstevel@tonic-gate 					destroy_client(cl);
13960Sstevel@tonic-gate 				} else
13970Sstevel@tonic-gate 					mutex_unlock(&cl->clm);
13980Sstevel@tonic-gate 			} else
13990Sstevel@tonic-gate 				mutex_unlock(&cl->clm);
14000Sstevel@tonic-gate 			cl = next;
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 	last_swept = time(0);
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /*
14070Sstevel@tonic-gate  * Drop the least recently used client context, if possible.
14080Sstevel@tonic-gate  */
14090Sstevel@tonic-gate static void
drop_lru_client()14100Sstevel@tonic-gate drop_lru_client()
14110Sstevel@tonic-gate {
14120Sstevel@tonic-gate 	mutex_lock(&lru_last->clm);
14130Sstevel@tonic-gate 	lru_last->stale = TRUE;
14140Sstevel@tonic-gate 	mutex_unlock(&lru_last->clm);
14150Sstevel@tonic-gate 	if (lru_last->ref_cnt == 0)
14160Sstevel@tonic-gate 		destroy_client(lru_last);
14170Sstevel@tonic-gate 	else
14180Sstevel@tonic-gate 		sweep_clients();
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate /*
14220Sstevel@tonic-gate  * find service credentials
14230Sstevel@tonic-gate  * return cred if found,
14240Sstevel@tonic-gate  * other wise, NULL
14250Sstevel@tonic-gate  */
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate svc_creds_list_t *
find_svc_cred(char * service_name,uint_t program,uint_t version)14280Sstevel@tonic-gate find_svc_cred(char *service_name, uint_t program, uint_t version) {
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	svc_creds_list_t	*sc;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if (!svc_creds_list)
14330Sstevel@tonic-gate 		return (NULL);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	for (sc = svc_creds_list; sc != NULL; sc = sc->next) {
14360Sstevel@tonic-gate 		if (program != sc->program || version != sc->version)
14370Sstevel@tonic-gate 			continue;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 		if (strcmp(service_name, sc->server_name) != 0)
14400Sstevel@tonic-gate 			continue;
14410Sstevel@tonic-gate 		return (sc);
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 	return (NULL);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate  * Set the server principal name.
14480Sstevel@tonic-gate  */
14490Sstevel@tonic-gate bool_t
__rpc_gss_set_svc_name(server_name,mech,req_time,program,version)14500Sstevel@tonic-gate __rpc_gss_set_svc_name(server_name, mech, req_time, program, version)
14510Sstevel@tonic-gate 	char			*server_name;
14520Sstevel@tonic-gate 	char			*mech;
14530Sstevel@tonic-gate 	OM_uint32		req_time;
14540Sstevel@tonic-gate 	uint_t			program;
14550Sstevel@tonic-gate 	uint_t			version;
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	gss_name_t		name;
14580Sstevel@tonic-gate 	svc_creds_list_t	*svc_cred;
14590Sstevel@tonic-gate 	gss_OID			mechanism;
14600Sstevel@tonic-gate 	gss_OID_set_desc	oid_set_desc;
14610Sstevel@tonic-gate 	gss_OID_set		oid_set;
14620Sstevel@tonic-gate 	OM_uint32		ret_time;
14630Sstevel@tonic-gate 	OM_uint32		major, minor;
14640Sstevel@tonic-gate 	gss_buffer_desc		name_buf;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	if (!__rpc_gss_mech_to_oid(mech, &mechanism)) {
14670Sstevel@tonic-gate 		return (FALSE);
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	name_buf.value = server_name;
14710Sstevel@tonic-gate 	name_buf.length = strlen(server_name);
14720Sstevel@tonic-gate 	major = gss_import_name(&minor, &name_buf,
14730Sstevel@tonic-gate 				(gss_OID) GSS_C_NT_HOSTBASED_SERVICE, &name);
14740Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
14750Sstevel@tonic-gate 		return (FALSE);
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	/* Check if there is already an entry in the svc_creds_list. */
14790Sstevel@tonic-gate 	rw_wrlock(&cred_lock);
14800Sstevel@tonic-gate 	if (svc_cred = find_svc_cred(server_name, program, version)) {
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 		major = gss_add_cred(&minor, svc_cred->cred, name,
14830Sstevel@tonic-gate 					mechanism, GSS_C_ACCEPT,
14840Sstevel@tonic-gate 					0, req_time, NULL,
14850Sstevel@tonic-gate 					&oid_set, NULL,
14860Sstevel@tonic-gate 					&ret_time);
14870Sstevel@tonic-gate 		(void) gss_release_name(&minor, &name);
14880Sstevel@tonic-gate 		if (major == GSS_S_COMPLETE) {
14890Sstevel@tonic-gate 			/*
14900Sstevel@tonic-gate 			 * Successfully added the mech to the cred handle
14910Sstevel@tonic-gate 			 * free the existing oid_set in svc_cred
14920Sstevel@tonic-gate 			 */
14930Sstevel@tonic-gate 			gss_release_oid_set(&minor, &svc_cred->oid_set);
14940Sstevel@tonic-gate 			svc_cred->oid_set = oid_set;
14950Sstevel@tonic-gate 			rw_unlock(&cred_lock);
14960Sstevel@tonic-gate 			return (TRUE);
14970Sstevel@tonic-gate 		} else if (major == GSS_S_DUPLICATE_ELEMENT) {
14980Sstevel@tonic-gate 			rw_unlock(&cred_lock);
14990Sstevel@tonic-gate 			return (TRUE);
15000Sstevel@tonic-gate 		} else if (major == GSS_S_CREDENTIALS_EXPIRED) {
15010Sstevel@tonic-gate 			if (rpc_gss_refresh_svc_cred(svc_cred)) {
15020Sstevel@tonic-gate 				rw_unlock(&cred_lock);
15030Sstevel@tonic-gate 				return (TRUE);
15040Sstevel@tonic-gate 			} else {
15050Sstevel@tonic-gate 				rw_unlock(&cred_lock);
15060Sstevel@tonic-gate 				return (FALSE);
15070Sstevel@tonic-gate 			}
15080Sstevel@tonic-gate 		} else {
15090Sstevel@tonic-gate 			rw_unlock(&cred_lock);
15100Sstevel@tonic-gate 			return (FALSE);
15110Sstevel@tonic-gate 		}
15120Sstevel@tonic-gate 	} else {
15130Sstevel@tonic-gate 		svc_cred = (svc_creds_list_t *)malloc(sizeof (*svc_cred));
15140Sstevel@tonic-gate 		if (svc_cred == NULL) {
15150Sstevel@tonic-gate 			(void) gss_release_name(&minor, &name);
15160Sstevel@tonic-gate 			rw_unlock(&cred_lock);
15170Sstevel@tonic-gate 			return (FALSE);
15180Sstevel@tonic-gate 		}
15190Sstevel@tonic-gate 		oid_set_desc.count = 1;
15200Sstevel@tonic-gate 		oid_set_desc.elements = mechanism;
15210Sstevel@tonic-gate 		major = gss_acquire_cred(&minor, name, req_time,
15220Sstevel@tonic-gate 						&oid_set_desc,
15230Sstevel@tonic-gate 						GSS_C_ACCEPT,
15240Sstevel@tonic-gate 						&svc_cred->cred,
15250Sstevel@tonic-gate 						&oid_set, &ret_time);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 		if (major != GSS_S_COMPLETE) {
15280Sstevel@tonic-gate 			(void) gss_release_name(&minor, &name);
15290Sstevel@tonic-gate 			free(svc_cred);
15300Sstevel@tonic-gate 			rw_unlock(&cred_lock);
15310Sstevel@tonic-gate 			return (FALSE);
15320Sstevel@tonic-gate 		}
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 		svc_cred->name = name;
15350Sstevel@tonic-gate 		svc_cred->program = program;
15360Sstevel@tonic-gate 		svc_cred->version = version;
15370Sstevel@tonic-gate 		svc_cred->req_time = req_time;
15380Sstevel@tonic-gate 		svc_cred->oid_set = oid_set;
15390Sstevel@tonic-gate 		svc_cred->server_name = strdup(server_name);
15400Sstevel@tonic-gate 		if (svc_cred->server_name == NULL) {
15410Sstevel@tonic-gate 			(void) gss_release_name(&minor, &name);
15420Sstevel@tonic-gate 			free((char *)svc_cred);
15430Sstevel@tonic-gate 			rw_unlock(&cred_lock);
15440Sstevel@tonic-gate 			return (FALSE);
15450Sstevel@tonic-gate 		}
15460Sstevel@tonic-gate 		mutex_init(&svc_cred->refresh_mutex, USYNC_THREAD, NULL);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 		svc_cred->next = svc_creds_list;
15490Sstevel@tonic-gate 		svc_creds_list = svc_cred;
15500Sstevel@tonic-gate 		svc_creds_count++;
15510Sstevel@tonic-gate 		rw_unlock(&cred_lock);
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 		return (TRUE);
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate /*
15570Sstevel@tonic-gate  * Refresh server credentials.
15580Sstevel@tonic-gate  */
15590Sstevel@tonic-gate static bool_t
rpc_gss_refresh_svc_cred(svc_cred)15600Sstevel@tonic-gate rpc_gss_refresh_svc_cred(svc_cred)
15610Sstevel@tonic-gate 	svc_creds_list_t	*svc_cred;
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	OM_uint32		major, minor;
15640Sstevel@tonic-gate 	gss_OID_set		oid_set;
15650Sstevel@tonic-gate 	OM_uint32		ret_time;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	(void) gss_release_cred(&minor, &svc_cred->cred);
15680Sstevel@tonic-gate 	svc_cred->cred = GSS_C_NO_CREDENTIAL;
15690Sstevel@tonic-gate 	major = gss_acquire_cred(&minor, svc_cred->name, svc_cred->req_time,
15700Sstevel@tonic-gate 		svc_cred->oid_set, GSS_C_ACCEPT, &svc_cred->cred, &oid_set,
15710Sstevel@tonic-gate 		&ret_time);
15720Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
15730Sstevel@tonic-gate 		return (FALSE);
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	gss_release_oid_set(&minor, &svc_cred->oid_set);
15760Sstevel@tonic-gate 	svc_cred->oid_set = oid_set;
15770Sstevel@tonic-gate 	return (TRUE);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate /*
15810Sstevel@tonic-gate  * Encrypt the serialized arguments from xdr_func applied to xdr_ptr
15820Sstevel@tonic-gate  * and write the result to xdrs.
15830Sstevel@tonic-gate  */
15840Sstevel@tonic-gate static bool_t
svc_rpc_gss_wrap(auth,out_xdrs,xdr_func,xdr_ptr)15850Sstevel@tonic-gate svc_rpc_gss_wrap(auth, out_xdrs, xdr_func, xdr_ptr)
15860Sstevel@tonic-gate 	SVCAUTH			*auth;
15870Sstevel@tonic-gate 	XDR			*out_xdrs;
15880Sstevel@tonic-gate 	bool_t			(*xdr_func)();
15890Sstevel@tonic-gate 	caddr_t			xdr_ptr;
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	svc_rpc_gss_parms_t	*gss_parms = &auth->svc_gss_parms;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	/*
15940Sstevel@tonic-gate 	 * If context is not established, or if neither integrity nor
15950Sstevel@tonic-gate 	 * privacy service is used, don't wrap - just XDR encode.
15960Sstevel@tonic-gate 	 * Otherwise, wrap data using service and QOP parameters.
15970Sstevel@tonic-gate 	 */
15980Sstevel@tonic-gate 	if (!gss_parms->established ||
15990Sstevel@tonic-gate 				gss_parms->service == rpc_gss_svc_none)
16000Sstevel@tonic-gate 		return ((*xdr_func)(out_xdrs, xdr_ptr));
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	return (__rpc_gss_wrap_data(gss_parms->service,
16030Sstevel@tonic-gate 				(OM_uint32)gss_parms->qop_rcvd,
16040Sstevel@tonic-gate 				(gss_ctx_id_t)gss_parms->context,
16050Sstevel@tonic-gate 				gss_parms->seq_num,
16060Sstevel@tonic-gate 				out_xdrs, xdr_func, xdr_ptr));
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate  * Decrypt the serialized arguments and XDR decode them.
16110Sstevel@tonic-gate  */
16120Sstevel@tonic-gate static bool_t
svc_rpc_gss_unwrap(auth,in_xdrs,xdr_func,xdr_ptr)16130Sstevel@tonic-gate svc_rpc_gss_unwrap(auth, in_xdrs, xdr_func, xdr_ptr)
16140Sstevel@tonic-gate 	SVCAUTH			*auth;
16150Sstevel@tonic-gate 	XDR			*in_xdrs;
16160Sstevel@tonic-gate 	bool_t			(*xdr_func)();
16170Sstevel@tonic-gate 	caddr_t			xdr_ptr;
16180Sstevel@tonic-gate {
16190Sstevel@tonic-gate 	svc_rpc_gss_parms_t	*gss_parms = &auth->svc_gss_parms;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	/*
16220Sstevel@tonic-gate 	 * If context is not established, or if neither integrity nor
16230Sstevel@tonic-gate 	 * privacy service is used, don't unwrap - just XDR decode.
16240Sstevel@tonic-gate 	 * Otherwise, unwrap data.
16250Sstevel@tonic-gate 	 */
16260Sstevel@tonic-gate 	if (!gss_parms->established ||
16270Sstevel@tonic-gate 				gss_parms->service == rpc_gss_svc_none)
16280Sstevel@tonic-gate 		return ((*xdr_func)(in_xdrs, xdr_ptr));
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	return (__rpc_gss_unwrap_data(gss_parms->service,
16310Sstevel@tonic-gate 				(gss_ctx_id_t)gss_parms->context,
16320Sstevel@tonic-gate 				gss_parms->seq_num,
16330Sstevel@tonic-gate 				gss_parms->qop_rcvd,
16340Sstevel@tonic-gate 				in_xdrs, xdr_func, xdr_ptr));
16350Sstevel@tonic-gate }
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate int
__rpc_gss_svc_max_data_length(req,max_tp_unit_len)16380Sstevel@tonic-gate __rpc_gss_svc_max_data_length(req, max_tp_unit_len)
16390Sstevel@tonic-gate 	struct svc_req	*req;
16400Sstevel@tonic-gate 	int		max_tp_unit_len;
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate 	SVCAUTH			*svcauth;
16430Sstevel@tonic-gate 	svc_rpc_gss_parms_t	*gss_parms;
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	svcauth = __svc_get_svcauth(req->rq_xprt);
16460Sstevel@tonic-gate 	gss_parms = &svcauth->svc_gss_parms;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	if (!gss_parms->established || max_tp_unit_len <= 0)
16490Sstevel@tonic-gate 		return (0);
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	return (__find_max_data_length(gss_parms->service,
16520Sstevel@tonic-gate 			(gss_ctx_id_t)gss_parms->context,
16530Sstevel@tonic-gate 			gss_parms->qop_rcvd, max_tp_unit_len));
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate /*
16570Sstevel@tonic-gate  * Add retransmit entry to the context cache entry for a new xid.
16580Sstevel@tonic-gate  * If there is already an entry, delete it before adding the new one.
16590Sstevel@tonic-gate  */
retrans_add(client,xid,result)16600Sstevel@tonic-gate static void retrans_add(client, xid, result)
16610Sstevel@tonic-gate 	svc_rpc_gss_data *client;
16620Sstevel@tonic-gate 	uint32_t	xid;
16630Sstevel@tonic-gate 	rpc_gss_init_res *result;
16640Sstevel@tonic-gate {
16650Sstevel@tonic-gate 	retrans_entry	*rdata;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	if (client->retrans_data && client->retrans_data->xid == xid)
16680Sstevel@tonic-gate 		return;
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	rdata = (retrans_entry *) malloc(sizeof (*rdata));
16710Sstevel@tonic-gate 	if (rdata == NULL)
16720Sstevel@tonic-gate 		return;
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	rdata->xid = xid;
16750Sstevel@tonic-gate 	rdata->result = *result;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	if (result->token.length != 0) {
16780Sstevel@tonic-gate 		GSS_DUP_BUFFER(rdata->result.token, result->token);
16790Sstevel@tonic-gate 	}
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if (client->retrans_data)
16820Sstevel@tonic-gate 		retrans_del(client);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	client->retrans_data = rdata;
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * Delete the retransmit data from the context cache entry.
16890Sstevel@tonic-gate  */
retrans_del(client)16900Sstevel@tonic-gate static void retrans_del(client)
16910Sstevel@tonic-gate 	svc_rpc_gss_data *client;
16920Sstevel@tonic-gate {
16930Sstevel@tonic-gate 	retrans_entry *rdata;
16940Sstevel@tonic-gate 	OM_uint32 minor_stat;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	if (client->retrans_data == NULL)
16970Sstevel@tonic-gate 		return;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	rdata = client->retrans_data;
17000Sstevel@tonic-gate 	if (rdata->result.token.length != 0) {
17010Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &rdata->result.token);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	free((caddr_t)rdata);
17050Sstevel@tonic-gate 	client->retrans_data = NULL;
17060Sstevel@tonic-gate }
1707