xref: /titanic_51/usr/src/lib/rpcsec_gss/rpcsec_gss.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21cb620785Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24cb620785Sraf  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * $Header:
317c478bd9Sstevel@tonic-gate  * /afs/gza.com/product/secure/rel-eng/src/1.1/rpc/RCS/auth_gssapi.c,v
327c478bd9Sstevel@tonic-gate  * 1.14 1995/03/22 22:07:55 jik Exp $
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
35*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7257d1b4Sraf 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <pthread.h>
427c478bd9Sstevel@tonic-gate #include <thread.h>
437c478bd9Sstevel@tonic-gate #include <syslog.h>
447c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
467c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_defs.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void 	rpc_gss_nextverf();
497c478bd9Sstevel@tonic-gate static bool_t 	rpc_gss_marshall();
507c478bd9Sstevel@tonic-gate static bool_t	rpc_gss_validate();
517c478bd9Sstevel@tonic-gate static bool_t	rpc_gss_refresh();
527c478bd9Sstevel@tonic-gate static void	rpc_gss_destroy();
537c478bd9Sstevel@tonic-gate static void	rpc_gss_destroy_pvt();
547c478bd9Sstevel@tonic-gate static bool_t	rpc_gss_seccreate_pvt();
557c478bd9Sstevel@tonic-gate static bool_t	validate_seqwin();
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Globals that should have header files but don't.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate extern bool_t	xdr_opaque_auth(XDR *, struct opaque_auth *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static struct auth_ops rpc_gss_ops = {
647c478bd9Sstevel@tonic-gate 	rpc_gss_nextverf,
657c478bd9Sstevel@tonic-gate 	rpc_gss_marshall,
667c478bd9Sstevel@tonic-gate 	rpc_gss_validate,
677c478bd9Sstevel@tonic-gate 	rpc_gss_refresh,
687c478bd9Sstevel@tonic-gate 	rpc_gss_destroy
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Private data for RPCSEC_GSS.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate typedef struct _rpc_gss_data {
757c478bd9Sstevel@tonic-gate 	bool_t			established;	/* TRUE when established */
767c478bd9Sstevel@tonic-gate 	CLIENT			*clnt;		/* associated client handle */
777c478bd9Sstevel@tonic-gate 	uint_t			version;	/* RPCSEC version */
787c478bd9Sstevel@tonic-gate 	gss_ctx_id_t		context;	/* GSS context id */
797c478bd9Sstevel@tonic-gate 	gss_buffer_desc		ctx_handle;	/* RPCSEC context handle */
807c478bd9Sstevel@tonic-gate 	uint_t			seq_num;	/* last sequence number rcvd */
817c478bd9Sstevel@tonic-gate 	gss_cred_id_t		my_cred;	/* GSS credentials */
827c478bd9Sstevel@tonic-gate 	OM_uint32		qop;		/* requested QOP */
837c478bd9Sstevel@tonic-gate 	rpc_gss_service_t	service;	/* requested service */
847c478bd9Sstevel@tonic-gate 	uint_t			gss_proc;	/* GSS control procedure */
857c478bd9Sstevel@tonic-gate 	gss_name_t		target_name;	/* target server */
867c478bd9Sstevel@tonic-gate 	int			req_flags;	/* GSS request bits */
877c478bd9Sstevel@tonic-gate 	gss_OID			mech_type;	/* GSS mechanism */
887c478bd9Sstevel@tonic-gate 	OM_uint32		time_req;	/* requested cred lifetime */
897c478bd9Sstevel@tonic-gate 	bool_t			invalid;	/* can't use this any more */
907c478bd9Sstevel@tonic-gate 	OM_uint32		seq_window;	/* server sequence window */
917c478bd9Sstevel@tonic-gate 	struct opaque_auth	*verifier;  /* rpc reply verifier saved for */
927c478bd9Sstevel@tonic-gate 					    /* validating the sequence window */
937c478bd9Sstevel@tonic-gate 	gss_channel_bindings_t	icb;
947c478bd9Sstevel@tonic-gate } rpc_gss_data;
957c478bd9Sstevel@tonic-gate #define	AUTH_PRIVATE(auth) ((rpc_gss_data *)auth->ah_private)
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Create a context.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate AUTH *
1017c478bd9Sstevel@tonic-gate __rpc_gss_seccreate(clnt, server_name, mech, service, qop, options_req,
1027c478bd9Sstevel@tonic-gate 								options_ret)
1037c478bd9Sstevel@tonic-gate 	CLIENT			*clnt;		/* associated client handle */
1047c478bd9Sstevel@tonic-gate 	char			*server_name;	/* target server */
1057c478bd9Sstevel@tonic-gate 	char			*mech;		/* security mechanism */
1067c478bd9Sstevel@tonic-gate 	rpc_gss_service_t	service;	/* security service */
1077c478bd9Sstevel@tonic-gate 	char			*qop;		/* requested QOP */
1087c478bd9Sstevel@tonic-gate 	rpc_gss_options_req_t	*options_req;	/* requested options */
1097c478bd9Sstevel@tonic-gate 	rpc_gss_options_ret_t	*options_ret;	/* returned options */
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	OM_uint32		gssstat;
1127c478bd9Sstevel@tonic-gate 	OM_uint32		minor_stat;
1137c478bd9Sstevel@tonic-gate 	gss_name_t		target_name;
1147c478bd9Sstevel@tonic-gate 	gss_OID			mech_type;
1157c478bd9Sstevel@tonic-gate 	OM_uint32		ret_flags;
1167c478bd9Sstevel@tonic-gate 	OM_uint32		time_rec;
1177c478bd9Sstevel@tonic-gate 	gss_buffer_desc		input_name;
1187c478bd9Sstevel@tonic-gate 	AUTH			*auth = NULL;
1197c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = NULL;
1207c478bd9Sstevel@tonic-gate 	OM_uint32		qop_num;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * convert ascii strings to GSS values
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	if (!__rpc_gss_qop_to_num(qop, mech, &qop_num)) {
1267c478bd9Sstevel@tonic-gate 		return (NULL);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (!__rpc_gss_mech_to_oid(mech, &mech_type)) {
1307c478bd9Sstevel@tonic-gate 		return (NULL);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * convert name to GSS internal type
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	input_name.value = server_name;
1377c478bd9Sstevel@tonic-gate 	input_name.length = strlen(server_name);
1387c478bd9Sstevel@tonic-gate 	gssstat = gss_import_name(&minor_stat, &input_name,
1397c478bd9Sstevel@tonic-gate 				(gss_OID)GSS_C_NT_HOSTBASED_SERVICE,
1407c478bd9Sstevel@tonic-gate 				&target_name);
1417c478bd9Sstevel@tonic-gate 	if (gssstat != GSS_S_COMPLETE) {
1427c478bd9Sstevel@tonic-gate 		rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
1437c478bd9Sstevel@tonic-gate 		rpc_gss_err.system_error = ENOMEM;
1447c478bd9Sstevel@tonic-gate 		return (NULL);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * Create AUTH handle.  Save the necessary interface information
1497c478bd9Sstevel@tonic-gate 	 * so that the client can refresh the handle later if needed.
1507c478bd9Sstevel@tonic-gate 	 */
1517c478bd9Sstevel@tonic-gate 	if ((auth = (AUTH *) malloc(sizeof (*auth))) != NULL)
1527c478bd9Sstevel@tonic-gate 		ap = (rpc_gss_data *) malloc(sizeof (*ap));
1537c478bd9Sstevel@tonic-gate 	if (auth == NULL || ap == NULL) {
1547c478bd9Sstevel@tonic-gate 		rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
1557c478bd9Sstevel@tonic-gate 		rpc_gss_err.system_error = ENOMEM;
1567c478bd9Sstevel@tonic-gate 		if (auth != NULL)
1577c478bd9Sstevel@tonic-gate 			free((char *)auth);
1587c478bd9Sstevel@tonic-gate 		(void) gss_release_name(&minor_stat, &target_name);
1597c478bd9Sstevel@tonic-gate 		return (NULL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	memset((char *)ap, 0, sizeof (*ap));
1637c478bd9Sstevel@tonic-gate 	ap->clnt = clnt;
1647c478bd9Sstevel@tonic-gate 	ap->version = RPCSEC_GSS_VERSION;
1657c478bd9Sstevel@tonic-gate 	if (options_req != NULL) {
1667c478bd9Sstevel@tonic-gate 		ap->my_cred = options_req->my_cred;
1677c478bd9Sstevel@tonic-gate 		ap->req_flags = options_req->req_flags;
1687c478bd9Sstevel@tonic-gate 		ap->time_req = options_req->time_req;
1697c478bd9Sstevel@tonic-gate 		ap->icb = options_req->input_channel_bindings;
1707c478bd9Sstevel@tonic-gate 	} else {
1717c478bd9Sstevel@tonic-gate 		ap->my_cred = GSS_C_NO_CREDENTIAL;
1727c478bd9Sstevel@tonic-gate 		ap->req_flags = GSS_C_MUTUAL_FLAG;
1737c478bd9Sstevel@tonic-gate 		ap->time_req = 0;
1747c478bd9Sstevel@tonic-gate 		ap->icb = NULL;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	if ((ap->service = service) == rpc_gss_svc_default)
1777c478bd9Sstevel@tonic-gate 		ap->service = rpc_gss_svc_integrity;
1787c478bd9Sstevel@tonic-gate 	ap->qop = qop_num;
1797c478bd9Sstevel@tonic-gate 	ap->target_name = target_name;
1807c478bd9Sstevel@tonic-gate 	ap->mech_type = mech_type;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Now invoke the real interface that sets up the context from
1847c478bd9Sstevel@tonic-gate 	 * the information stashed away in the private data.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	if (!rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth, ap,
1877c478bd9Sstevel@tonic-gate 				&mech_type, &ret_flags, &time_rec)) {
1887c478bd9Sstevel@tonic-gate 		if (ap->target_name)
1897c478bd9Sstevel@tonic-gate 			(void) gss_release_name(&minor_stat, &ap->target_name);
1907c478bd9Sstevel@tonic-gate 		free((char *)ap);
1917c478bd9Sstevel@tonic-gate 		free((char *)auth);
1927c478bd9Sstevel@tonic-gate 		return (NULL);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Make sure that the requested service is supported.  In all
1977c478bd9Sstevel@tonic-gate 	 * cases, integrity service must be available.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	if ((ap->service == rpc_gss_svc_privacy &&
2007c478bd9Sstevel@tonic-gate 					!(ret_flags & GSS_C_CONF_FLAG)) ||
2017c478bd9Sstevel@tonic-gate 			!(ret_flags & GSS_C_INTEG_FLAG)) {
2027c478bd9Sstevel@tonic-gate 		rpc_gss_destroy(auth);
2037c478bd9Sstevel@tonic-gate 		rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
2047c478bd9Sstevel@tonic-gate 		rpc_gss_err.system_error = EPROTONOSUPPORT;
2057c478bd9Sstevel@tonic-gate 		return (NULL);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * return option values if requested
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if (options_ret != NULL) {
2127c478bd9Sstevel@tonic-gate 		char	*s;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		options_ret->major_status = gssstat;
2157c478bd9Sstevel@tonic-gate 		options_ret->minor_status = minor_stat;
2167c478bd9Sstevel@tonic-gate 		options_ret->rpcsec_version = ap->version;
2177c478bd9Sstevel@tonic-gate 		options_ret->ret_flags = ret_flags;
2187c478bd9Sstevel@tonic-gate 		options_ret->time_ret = time_rec;
2197c478bd9Sstevel@tonic-gate 		options_ret->gss_context = ap->context;
2207c478bd9Sstevel@tonic-gate 		if ((s = __rpc_gss_oid_to_mech(mech_type)) != NULL)
2217c478bd9Sstevel@tonic-gate 			strcpy(options_ret->actual_mechanism, s);
2227c478bd9Sstevel@tonic-gate 		else
2237c478bd9Sstevel@tonic-gate 			options_ret->actual_mechanism[0] = '\0';
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	return (auth);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Private interface to create a context.  This is the interface
2307c478bd9Sstevel@tonic-gate  * that's invoked when the context has to be refreshed.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate static bool_t
2337c478bd9Sstevel@tonic-gate rpc_gss_seccreate_pvt(gssstat, minor_stat, auth, ap, actual_mech_type,
2347c478bd9Sstevel@tonic-gate 						ret_flags, time_rec)
2357c478bd9Sstevel@tonic-gate 	OM_uint32		*gssstat;
2367c478bd9Sstevel@tonic-gate 	OM_uint32		*minor_stat;
2377c478bd9Sstevel@tonic-gate 	AUTH			*auth;
2387c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap;
2397c478bd9Sstevel@tonic-gate 	gss_OID			*actual_mech_type;
2407c478bd9Sstevel@tonic-gate 	OM_uint32		*ret_flags;
2417c478bd9Sstevel@tonic-gate 	OM_uint32		*time_rec;
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	CLIENT			*clnt = ap->clnt;
2447c478bd9Sstevel@tonic-gate 	AUTH			*save_auth;
2457c478bd9Sstevel@tonic-gate 	enum clnt_stat		callstat;
2467c478bd9Sstevel@tonic-gate 	rpc_gss_init_arg	call_arg;
2477c478bd9Sstevel@tonic-gate 	rpc_gss_init_res	call_res;
2487c478bd9Sstevel@tonic-gate 	gss_buffer_desc		*input_token_p, input_token;
2497c478bd9Sstevel@tonic-gate 	bool_t			free_results = FALSE;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * initialize error
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	memset(&rpc_createerr, 0, sizeof (rpc_createerr));
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * (re)initialize AUTH handle and private data.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	memset((char *)auth, 0, sizeof (*auth));
2607c478bd9Sstevel@tonic-gate 	auth->ah_ops = &rpc_gss_ops;
2617c478bd9Sstevel@tonic-gate 	auth->ah_private = (caddr_t)ap;
2627c478bd9Sstevel@tonic-gate 	auth->ah_cred.oa_flavor = RPCSEC_GSS;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	ap->established = FALSE;
2657c478bd9Sstevel@tonic-gate 	ap->ctx_handle.length = 0;
2667c478bd9Sstevel@tonic-gate 	ap->ctx_handle.value = NULL;
2677c478bd9Sstevel@tonic-gate 	ap->context = GSS_C_NO_CONTEXT;
2687c478bd9Sstevel@tonic-gate 	ap->seq_num = 0;
2697c478bd9Sstevel@tonic-gate 	ap->gss_proc = RPCSEC_GSS_INIT;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * should not change clnt->cl_auth at this time, so save
2737c478bd9Sstevel@tonic-gate 	 * old handle
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	save_auth = clnt->cl_auth;
2767c478bd9Sstevel@tonic-gate 	clnt->cl_auth = auth;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * set state for starting context setup
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	input_token_p = GSS_C_NO_BUFFER;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate next_token:
2847c478bd9Sstevel@tonic-gate 	*gssstat = gss_init_sec_context(minor_stat,
2857c478bd9Sstevel@tonic-gate 					ap->my_cred,
2867c478bd9Sstevel@tonic-gate 					&ap->context,
2877c478bd9Sstevel@tonic-gate 					ap->target_name,
2887c478bd9Sstevel@tonic-gate 					ap->mech_type,
2897c478bd9Sstevel@tonic-gate 					ap->req_flags,
2907c478bd9Sstevel@tonic-gate 					ap->time_req,
2917c478bd9Sstevel@tonic-gate 					NULL,
2927c478bd9Sstevel@tonic-gate 					input_token_p,
2937c478bd9Sstevel@tonic-gate 					actual_mech_type,
2947c478bd9Sstevel@tonic-gate 					&call_arg,
2957c478bd9Sstevel@tonic-gate 					ret_flags,
2967c478bd9Sstevel@tonic-gate 					time_rec);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (input_token_p != GSS_C_NO_BUFFER) {
2997c478bd9Sstevel@tonic-gate 		OM_uint32 minor_stat2;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat2, input_token_p);
3027c478bd9Sstevel@tonic-gate 		input_token_p = GSS_C_NO_BUFFER;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (*gssstat != GSS_S_COMPLETE && *gssstat != GSS_S_CONTINUE_NEEDED) {
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		goto cleanup;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * if we got a token, pass it on
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if (call_arg.length != 0) {
3147c478bd9Sstevel@tonic-gate 		struct timeval timeout = {30, 0};
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		memset((char *)&call_res, 0, sizeof (call_res));
3177c478bd9Sstevel@tonic-gate 		callstat = clnt_call(clnt, NULLPROC,
3187c478bd9Sstevel@tonic-gate 				__xdr_rpc_gss_init_arg, (caddr_t)&call_arg,
3197c478bd9Sstevel@tonic-gate 				__xdr_rpc_gss_init_res, (caddr_t)&call_res,
3207c478bd9Sstevel@tonic-gate 				timeout);
3217c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(minor_stat, &call_arg);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		if (callstat != RPC_SUCCESS) {
3247c478bd9Sstevel@tonic-gate 			goto cleanup;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		/*
3277c478bd9Sstevel@tonic-gate 		 * we have results - note that these need to be freed
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		free_results = TRUE;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		if (call_res.gss_major != GSS_S_COMPLETE &&
3327c478bd9Sstevel@tonic-gate 			call_res.gss_major != GSS_S_CONTINUE_NEEDED)
3337c478bd9Sstevel@tonic-gate 			goto cleanup;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		ap->gss_proc = RPCSEC_GSS_CONTINUE_INIT;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		/*
3387c478bd9Sstevel@tonic-gate 		 * check for ctx_handle
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		if (ap->ctx_handle.length == 0) {
3417c478bd9Sstevel@tonic-gate 			if (call_res.ctx_handle.length == 0)
3427c478bd9Sstevel@tonic-gate 				goto cleanup;
3437c478bd9Sstevel@tonic-gate 			GSS_DUP_BUFFER(ap->ctx_handle,
3447c478bd9Sstevel@tonic-gate 				call_res.ctx_handle);
3457c478bd9Sstevel@tonic-gate 		} else if (!GSS_BUFFERS_EQUAL(ap->ctx_handle,
3467c478bd9Sstevel@tonic-gate 						call_res.ctx_handle))
3477c478bd9Sstevel@tonic-gate 			goto cleanup;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * check for token
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		if (call_res.token.length != 0) {
3537c478bd9Sstevel@tonic-gate 			if (*gssstat == GSS_S_COMPLETE)
3547c478bd9Sstevel@tonic-gate 				goto cleanup;
3557c478bd9Sstevel@tonic-gate 			GSS_DUP_BUFFER(input_token, call_res.token);
3567c478bd9Sstevel@tonic-gate 			input_token_p = &input_token;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		} else if (*gssstat != GSS_S_COMPLETE)
3597c478bd9Sstevel@tonic-gate 			goto cleanup;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		/* save the sequence window value; validate later */
3627c478bd9Sstevel@tonic-gate 		ap->seq_window = call_res.seq_window;
3637c478bd9Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res);
3647c478bd9Sstevel@tonic-gate 		free_results = FALSE;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * results were okay.. continue if necessary
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	if (*gssstat == GSS_S_CONTINUE_NEEDED)
3717c478bd9Sstevel@tonic-gate 		goto next_token;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * Validate the sequence window - RFC 2203 section 5.2.3.1
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	if (!validate_seqwin(ap)) {
3777c478bd9Sstevel@tonic-gate 		goto cleanup;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * Done!  Security context creation is successful.
3827c478bd9Sstevel@tonic-gate 	 * Ready for exchanging data.
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 	ap->established = TRUE;
3857c478bd9Sstevel@tonic-gate 	ap->seq_num = 1;
3867c478bd9Sstevel@tonic-gate 	ap->gss_proc = RPCSEC_GSS_DATA;
3877c478bd9Sstevel@tonic-gate 	ap->invalid = FALSE;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	clnt->cl_auth = save_auth;	/* restore cl_auth */
3907c478bd9Sstevel@tonic-gate 	return (TRUE);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate cleanup:
3937c478bd9Sstevel@tonic-gate 	if (ap->context != GSS_C_NO_CONTEXT)
3947c478bd9Sstevel@tonic-gate 		rpc_gss_destroy_pvt(auth);
3957c478bd9Sstevel@tonic-gate 	if (free_results)
3967c478bd9Sstevel@tonic-gate 		xdr_free(__xdr_rpc_gss_init_res, (caddr_t)&call_res);
3977c478bd9Sstevel@tonic-gate 	clnt->cl_auth = save_auth;	/* restore cl_auth */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  *	if (rpc_createerr.cf_stat == 0)
4017c478bd9Sstevel@tonic-gate  *		rpc_createerr.cf_stat = RPC_AUTHERROR;
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate 	if (rpc_createerr.cf_stat == 0) {
4047c478bd9Sstevel@tonic-gate 		rpc_gss_err.rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
4057c478bd9Sstevel@tonic-gate 		rpc_gss_err.system_error = RPC_AUTHERROR;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	return (FALSE);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * Set service defaults.
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate bool_t
4157c478bd9Sstevel@tonic-gate __rpc_gss_set_defaults(auth, service, qop)
4167c478bd9Sstevel@tonic-gate 	AUTH			*auth;
4177c478bd9Sstevel@tonic-gate 	rpc_gss_service_t	service;
4187c478bd9Sstevel@tonic-gate 	char			*qop;
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	/*LINTED*/
4217c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
4227c478bd9Sstevel@tonic-gate 	char			*mech;
4237c478bd9Sstevel@tonic-gate 	OM_uint32		qop_num;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	switch (service) {
4267c478bd9Sstevel@tonic-gate 	case rpc_gss_svc_integrity:
4277c478bd9Sstevel@tonic-gate 	case rpc_gss_svc_privacy:
4287c478bd9Sstevel@tonic-gate 	case rpc_gss_svc_none:
4297c478bd9Sstevel@tonic-gate 		break;
4307c478bd9Sstevel@tonic-gate 	case rpc_gss_svc_default:
4317c478bd9Sstevel@tonic-gate 		service = rpc_gss_svc_integrity;
4327c478bd9Sstevel@tonic-gate 		break;
4337c478bd9Sstevel@tonic-gate 	default:
4347c478bd9Sstevel@tonic-gate 		return (FALSE);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if ((mech = __rpc_gss_oid_to_mech(ap->mech_type)) == NULL)
4387c478bd9Sstevel@tonic-gate 		return (FALSE);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (!__rpc_gss_qop_to_num(qop, mech, &qop_num))
4417c478bd9Sstevel@tonic-gate 		return (FALSE);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	ap->qop = qop_num;
4447c478bd9Sstevel@tonic-gate 	ap->service = service;
4457c478bd9Sstevel@tonic-gate 	return (TRUE);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate  * Marshall credentials.
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate static bool_t
4527c478bd9Sstevel@tonic-gate marshall_creds(ap, xdrs)
4537c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap;
4547c478bd9Sstevel@tonic-gate 	XDR			*xdrs;
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	rpc_gss_creds		ag_creds;
4577c478bd9Sstevel@tonic-gate 	char			cred_buf[MAX_AUTH_BYTES];
4587c478bd9Sstevel@tonic-gate 	struct opaque_auth	creds;
4597c478bd9Sstevel@tonic-gate 	XDR			cred_xdrs;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	ag_creds.version = ap->version;
4627c478bd9Sstevel@tonic-gate 	ag_creds.gss_proc = ap->gss_proc;
4637c478bd9Sstevel@tonic-gate 	ag_creds.seq_num = ap->seq_num;
4647c478bd9Sstevel@tonic-gate 	ag_creds.service = ap->service;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * If context has not been set up yet, use NULL handle.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	if (ap->ctx_handle.length > 0)
4707c478bd9Sstevel@tonic-gate 		ag_creds.ctx_handle = ap->ctx_handle;
4717c478bd9Sstevel@tonic-gate 	else {
4727c478bd9Sstevel@tonic-gate 		ag_creds.ctx_handle.length = 0;
4737c478bd9Sstevel@tonic-gate 		ag_creds.ctx_handle.value = NULL;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	xdrmem_create(&cred_xdrs, (caddr_t)cred_buf, MAX_AUTH_BYTES,
4777c478bd9Sstevel@tonic-gate 								XDR_ENCODE);
4787c478bd9Sstevel@tonic-gate 	if (!__xdr_rpc_gss_creds(&cred_xdrs, &ag_creds)) {
4797c478bd9Sstevel@tonic-gate 		XDR_DESTROY(&cred_xdrs);
4807c478bd9Sstevel@tonic-gate 		return (FALSE);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	creds.oa_flavor = RPCSEC_GSS;
4847c478bd9Sstevel@tonic-gate 	creds.oa_base = cred_buf;
4857c478bd9Sstevel@tonic-gate 	creds.oa_length = xdr_getpos(&cred_xdrs);
4867c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&cred_xdrs);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (!xdr_opaque_auth(xdrs, &creds))
4897c478bd9Sstevel@tonic-gate 		return (FALSE);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	return (TRUE);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate  * Marshall verifier.  The verifier is the checksum of the RPC header
4967c478bd9Sstevel@tonic-gate  * up to and including the credential field.  The XDR handle that's
4977c478bd9Sstevel@tonic-gate  * passed in has the header up to and including the credential field
4987c478bd9Sstevel@tonic-gate  * encoded.  A pointer to the transmit buffer is also passed in.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static bool_t
5017c478bd9Sstevel@tonic-gate marshall_verf(ap, xdrs, buf)
5027c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap;
5037c478bd9Sstevel@tonic-gate 	XDR			*xdrs;	/* send XDR */
5047c478bd9Sstevel@tonic-gate 	char			*buf;	/* pointer of send buffer */
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	struct opaque_auth	verf;
5077c478bd9Sstevel@tonic-gate 	OM_uint32		major, minor;
5087c478bd9Sstevel@tonic-gate 	gss_buffer_desc		in_buf, out_buf;
5097c478bd9Sstevel@tonic-gate 	bool_t			ret = FALSE;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * If context is not established yet, use NULL verifier.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	if (!ap->established) {
5157c478bd9Sstevel@tonic-gate 		verf.oa_flavor = AUTH_NONE;
5167c478bd9Sstevel@tonic-gate 		verf.oa_base = NULL;
5177c478bd9Sstevel@tonic-gate 		verf.oa_length = 0;
5187c478bd9Sstevel@tonic-gate 		return (xdr_opaque_auth(xdrs, &verf));
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	verf.oa_flavor = RPCSEC_GSS;
5227c478bd9Sstevel@tonic-gate 	in_buf.length = xdr_getpos(xdrs);
5237c478bd9Sstevel@tonic-gate 	in_buf.value = buf;
5247c478bd9Sstevel@tonic-gate 	if ((major = gss_sign(&minor, ap->context, ap->qop, &in_buf,
5257c478bd9Sstevel@tonic-gate 					&out_buf)) != GSS_S_COMPLETE) {
5267c478bd9Sstevel@tonic-gate 		if (major == GSS_S_CONTEXT_EXPIRED) {
5277c478bd9Sstevel@tonic-gate 			ap->invalid = TRUE;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		return (FALSE);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	verf.oa_base = out_buf.value;
5327c478bd9Sstevel@tonic-gate 	verf.oa_length = out_buf.length;
5337c478bd9Sstevel@tonic-gate 	ret = xdr_opaque_auth(xdrs, &verf);
5347c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&minor, &out_buf);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	return (ret);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * Function: rpc_gss_nextverf.  Not used.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate static void
5437c478bd9Sstevel@tonic-gate rpc_gss_nextverf()
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate  * Function: rpc_gss_marshall - not used.
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate static bool_t
5517c478bd9Sstevel@tonic-gate rpc_gss_marshall(auth, xdrs)
5527c478bd9Sstevel@tonic-gate 	AUTH		*auth;
5537c478bd9Sstevel@tonic-gate 	XDR		*xdrs;
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	if (!xdr_opaque_auth(xdrs, &auth->ah_cred) ||
5567c478bd9Sstevel@tonic-gate 				!xdr_opaque_auth(xdrs, &auth->ah_verf))
5577c478bd9Sstevel@tonic-gate 		return (FALSE);
5587c478bd9Sstevel@tonic-gate 	return (TRUE);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Validate sequence window upon a successful RPCSEC_GSS INIT session.
5637c478bd9Sstevel@tonic-gate  * The sequence window sent back by the server should be verifiable by
5647c478bd9Sstevel@tonic-gate  * the verifier which is a checksum of the sequence window.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate static bool_t
5677c478bd9Sstevel@tonic-gate validate_seqwin(rpc_gss_data *ap)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	uint_t			seq_win_net;
5707c478bd9Sstevel@tonic-gate 	OM_uint32		major = 0, minor = 0;
5717c478bd9Sstevel@tonic-gate 	gss_buffer_desc		msg_buf, tok_buf;
5727c478bd9Sstevel@tonic-gate 	int			qop_state = 0;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	seq_win_net = (uint_t)htonl(ap->seq_window);
5757c478bd9Sstevel@tonic-gate 	msg_buf.length = sizeof (seq_win_net);
5767c478bd9Sstevel@tonic-gate 	msg_buf.value = (char *)&seq_win_net;
5777c478bd9Sstevel@tonic-gate 	tok_buf.length = ap->verifier->oa_length;
5787c478bd9Sstevel@tonic-gate 	tok_buf.value = ap->verifier->oa_base;
5797c478bd9Sstevel@tonic-gate 	major = gss_verify(&minor, ap->context, &msg_buf, &tok_buf, &qop_state);
5807c478bd9Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE)
5817c478bd9Sstevel@tonic-gate 		return (FALSE);
5827c478bd9Sstevel@tonic-gate 	return (TRUE);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * Validate RPC response verifier from server.  The response verifier
5877c478bd9Sstevel@tonic-gate  * is the checksum of the request sequence number.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static bool_t
5907c478bd9Sstevel@tonic-gate rpc_gss_validate(auth, verf)
5917c478bd9Sstevel@tonic-gate 	AUTH			*auth;
5927c478bd9Sstevel@tonic-gate 	struct opaque_auth	*verf;
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	/*LINTED*/
5957c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
5967c478bd9Sstevel@tonic-gate 	uint_t			seq_num_net;
5977c478bd9Sstevel@tonic-gate 	OM_uint32		major, minor;
5987c478bd9Sstevel@tonic-gate 	gss_buffer_desc		msg_buf, tok_buf;
5997c478bd9Sstevel@tonic-gate 	int			qop_state;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * If context is not established yet, save the verifier for
6037c478bd9Sstevel@tonic-gate 	 * validating the sequence window later at the end of context
6047c478bd9Sstevel@tonic-gate 	 * creation session.
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	if (!ap->established) {
6077c478bd9Sstevel@tonic-gate 	    if (ap->verifier == NULL) {
6087c478bd9Sstevel@tonic-gate 		ap->verifier = malloc(sizeof (struct opaque_auth));
6097c478bd9Sstevel@tonic-gate 		memset(ap->verifier, 0, sizeof (struct opaque_auth));
6107c478bd9Sstevel@tonic-gate 		if (verf->oa_length > 0)
6117c478bd9Sstevel@tonic-gate 		    ap->verifier->oa_base = malloc(verf->oa_length);
6127c478bd9Sstevel@tonic-gate 	    } else {
6137c478bd9Sstevel@tonic-gate 		if (ap->verifier->oa_length > 0)
6147c478bd9Sstevel@tonic-gate 		    free(ap->verifier->oa_base);
6157c478bd9Sstevel@tonic-gate 		if (verf->oa_length > 0)
6167c478bd9Sstevel@tonic-gate 		    ap->verifier->oa_base = malloc(verf->oa_length);
6177c478bd9Sstevel@tonic-gate 	    }
6187c478bd9Sstevel@tonic-gate 	    ap->verifier->oa_length = verf->oa_length;
6197c478bd9Sstevel@tonic-gate 	    bcopy(verf->oa_base, ap->verifier->oa_base, verf->oa_length);
6207c478bd9Sstevel@tonic-gate 	    return (TRUE);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	seq_num_net = (uint_t)htonl(ap->seq_num);
6247c478bd9Sstevel@tonic-gate 	msg_buf.length = sizeof (seq_num_net);
6257c478bd9Sstevel@tonic-gate 	msg_buf.value = (char *)&seq_num_net;
6267c478bd9Sstevel@tonic-gate 	tok_buf.length = verf->oa_length;
6277c478bd9Sstevel@tonic-gate 	tok_buf.value = verf->oa_base;
6287c478bd9Sstevel@tonic-gate 	major = gss_verify(&minor, ap->context, &msg_buf, &tok_buf, &qop_state);
6297c478bd9Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE)
6307c478bd9Sstevel@tonic-gate 		return (FALSE);
6317c478bd9Sstevel@tonic-gate 	return (TRUE);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate /*
6357c478bd9Sstevel@tonic-gate  * Refresh client context.  This is necessary sometimes because the
6367c478bd9Sstevel@tonic-gate  * server will ocassionally destroy contexts based on LRU method, or
6377c478bd9Sstevel@tonic-gate  * because of expired credentials.
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate static bool_t
6407c478bd9Sstevel@tonic-gate rpc_gss_refresh(auth, msg)
6417c478bd9Sstevel@tonic-gate 	AUTH		*auth;
6427c478bd9Sstevel@tonic-gate 	struct rpc_msg	*msg;
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	/*LINTED*/
6457c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
6467c478bd9Sstevel@tonic-gate 	OM_uint32	gssstat, minor_stat;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * The context needs to be recreated only when the error status
6507c478bd9Sstevel@tonic-gate 	 * returned from the server is one of the following:
6517c478bd9Sstevel@tonic-gate 	 *	RPCSEC_GSS_NOCRED and RPCSEC_GSS_FAILED
6527c478bd9Sstevel@tonic-gate 	 * The existing context should not be destroyed unless the above
6537c478bd9Sstevel@tonic-gate 	 * error status codes are received or if the context has not
6547c478bd9Sstevel@tonic-gate 	 * been set up.
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (msg->rjcted_rply.rj_why == RPCSEC_GSS_NOCRED ||
6587c478bd9Sstevel@tonic-gate 			msg->rjcted_rply.rj_why == RPCSEC_GSS_FAILED ||
6597c478bd9Sstevel@tonic-gate 							!ap->established) {
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Destroy the context if necessary.  Use the same memory
6627c478bd9Sstevel@tonic-gate 		 * for the new context since we've already passed a pointer
6637c478bd9Sstevel@tonic-gate 		 * to it to the user.
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		if (ap->context != GSS_C_NO_CONTEXT) {
6667c478bd9Sstevel@tonic-gate 			(void) gss_delete_sec_context(&minor_stat, &ap->context,
6677c478bd9Sstevel@tonic-gate 								NULL);
6687c478bd9Sstevel@tonic-gate 			ap->context = GSS_C_NO_CONTEXT;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		if (ap->ctx_handle.length != 0) {
6717c478bd9Sstevel@tonic-gate 			(void) gss_release_buffer(&minor_stat,
6727c478bd9Sstevel@tonic-gate 							&ap->ctx_handle);
6737c478bd9Sstevel@tonic-gate 			ap->ctx_handle.length = 0;
6747c478bd9Sstevel@tonic-gate 			ap->ctx_handle.value = NULL;
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		/*
6787c478bd9Sstevel@tonic-gate 		 * If the context was not already established, don't try to
6797c478bd9Sstevel@tonic-gate 		 * recreate it.
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		if (!ap->established) {
6827c478bd9Sstevel@tonic-gate 			ap->invalid = TRUE;
6837c478bd9Sstevel@tonic-gate 			return (FALSE);
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * Recreate context.
6887c478bd9Sstevel@tonic-gate 		 */
6897c478bd9Sstevel@tonic-gate 		if (rpc_gss_seccreate_pvt(&gssstat, &minor_stat, auth, ap,
6907c478bd9Sstevel@tonic-gate 		    (gss_OID *)0, (OM_uint32 *)0, (OM_uint32 *)0))
6917c478bd9Sstevel@tonic-gate 			return (TRUE);
6927c478bd9Sstevel@tonic-gate 		else {
6937c478bd9Sstevel@tonic-gate 			ap->invalid = TRUE;
6947c478bd9Sstevel@tonic-gate 			return (FALSE);
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	return (FALSE);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * Destroy a context.
7027c478bd9Sstevel@tonic-gate  */
7037c478bd9Sstevel@tonic-gate static void
7047c478bd9Sstevel@tonic-gate rpc_gss_destroy(auth)
7057c478bd9Sstevel@tonic-gate 	AUTH		*auth;
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	/*LINTED*/
7087c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	rpc_gss_destroy_pvt(auth);
7117c478bd9Sstevel@tonic-gate 	free((char *)ap);
7127c478bd9Sstevel@tonic-gate 	free(auth);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate  * Private interface to destroy a context without freeing up
7177c478bd9Sstevel@tonic-gate  * the memory used by it.  We need to do this when a refresh
7187c478bd9Sstevel@tonic-gate  * fails, for example, so the user will still have a handle.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate static void
7217c478bd9Sstevel@tonic-gate rpc_gss_destroy_pvt(auth)
7227c478bd9Sstevel@tonic-gate 	AUTH		*auth;
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	struct timeval	timeout;
7257c478bd9Sstevel@tonic-gate 	OM_uint32	minor_stat;
7267c478bd9Sstevel@tonic-gate 	/*LINTED*/
7277c478bd9Sstevel@tonic-gate 	rpc_gss_data	*ap = AUTH_PRIVATE(auth);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	/*
7307c478bd9Sstevel@tonic-gate 	 * If we have a server context id, inform server that we are
7317c478bd9Sstevel@tonic-gate 	 * destroying the context.
7327c478bd9Sstevel@tonic-gate 	 */
7337c478bd9Sstevel@tonic-gate 	if (ap->ctx_handle.length != 0) {
7347c478bd9Sstevel@tonic-gate 		ap->gss_proc = RPCSEC_GSS_DESTROY;
7357c478bd9Sstevel@tonic-gate 		timeout.tv_sec = 1;
7367c478bd9Sstevel@tonic-gate 		timeout.tv_usec = 0;
7377c478bd9Sstevel@tonic-gate 		(void) clnt_call(ap->clnt, NULLPROC, xdr_void, NULL,
7387c478bd9Sstevel@tonic-gate 						xdr_void, NULL, timeout);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		(void) gss_release_buffer(&minor_stat, &ap->ctx_handle);
7417c478bd9Sstevel@tonic-gate 		ap->ctx_handle.length = 0;
7427c478bd9Sstevel@tonic-gate 		ap->ctx_handle.value = NULL;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	/*
7467c478bd9Sstevel@tonic-gate 	 * Destroy local GSS context.
7477c478bd9Sstevel@tonic-gate 	 */
7487c478bd9Sstevel@tonic-gate 	if (ap->context != GSS_C_NO_CONTEXT) {
7497c478bd9Sstevel@tonic-gate 		(void) gss_delete_sec_context(&minor_stat, &ap->context, NULL);
7507c478bd9Sstevel@tonic-gate 		ap->context = GSS_C_NO_CONTEXT;
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * Looks like we need to release default credentials if we use it.
7557c478bd9Sstevel@tonic-gate 	 * Non-default creds need to be released by user.
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	if (ap->my_cred == GSS_C_NO_CREDENTIAL)
7587c478bd9Sstevel@tonic-gate 		(void) gss_release_cred(&minor_stat, &ap->my_cred);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * Release any internal name structures.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	if (ap->target_name != NULL) {
7647c478bd9Sstevel@tonic-gate 		(void) gss_release_name(&minor_stat, &ap->target_name);
7657c478bd9Sstevel@tonic-gate 		ap->target_name = NULL;
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * Free the verifier saved for sequence window checking.
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 	if (ap->verifier != NULL) {
7727c478bd9Sstevel@tonic-gate 	    if (ap->verifier->oa_length > 0)
7737c478bd9Sstevel@tonic-gate 		free(ap->verifier->oa_base);
7747c478bd9Sstevel@tonic-gate 	    free(ap->verifier);
7757c478bd9Sstevel@tonic-gate 	    ap->verifier = NULL;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate /*
7807c478bd9Sstevel@tonic-gate  * Wrap client side data.  The encoded header is passed in through
7817c478bd9Sstevel@tonic-gate  * buf and buflen.  The header is up to but not including the
7827c478bd9Sstevel@tonic-gate  * credential field.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate bool_t
7857c478bd9Sstevel@tonic-gate __rpc_gss_wrap(auth, buf, buflen, out_xdrs, xdr_func, xdr_ptr)
7867c478bd9Sstevel@tonic-gate 	AUTH			*auth;
7877c478bd9Sstevel@tonic-gate 	char			*buf;		/* encoded header */
7887c478bd9Sstevel@tonic-gate 	uint_t			buflen;		/* encoded header length */
7897c478bd9Sstevel@tonic-gate 	XDR			*out_xdrs;
7907c478bd9Sstevel@tonic-gate 	bool_t			(*xdr_func)();
7917c478bd9Sstevel@tonic-gate 	caddr_t			xdr_ptr;
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	/*LINTED*/
7947c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
7957c478bd9Sstevel@tonic-gate 	XDR			xdrs;
7967c478bd9Sstevel@tonic-gate 	char			tmp_buf[512];
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * Reject an invalid context.
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	if (ap->invalid)
8037c478bd9Sstevel@tonic-gate 		return (FALSE);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * If context is established, bump up sequence number.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	if (ap->established)
8097c478bd9Sstevel@tonic-gate 		ap->seq_num++;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * Create the header in a temporary XDR context and buffer
8137c478bd9Sstevel@tonic-gate 	 * before putting it out.
8147c478bd9Sstevel@tonic-gate 	 */
8157c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, tmp_buf, sizeof (tmp_buf), XDR_ENCODE);
8167c478bd9Sstevel@tonic-gate 	if (!XDR_PUTBYTES(&xdrs, buf, buflen))
8177c478bd9Sstevel@tonic-gate 		return (FALSE);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	/*
8207c478bd9Sstevel@tonic-gate 	 * create cred field
8217c478bd9Sstevel@tonic-gate 	 */
8227c478bd9Sstevel@tonic-gate 	if (!marshall_creds(ap, &xdrs))
8237c478bd9Sstevel@tonic-gate 		return (FALSE);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * create verifier
8277c478bd9Sstevel@tonic-gate 	 */
8287c478bd9Sstevel@tonic-gate 	if (!marshall_verf(ap, &xdrs, tmp_buf))
8297c478bd9Sstevel@tonic-gate 		return (FALSE);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/*
8327c478bd9Sstevel@tonic-gate 	 * write out header and destroy temp structures
8337c478bd9Sstevel@tonic-gate 	 */
8347c478bd9Sstevel@tonic-gate 	if (!XDR_PUTBYTES(out_xdrs, tmp_buf, XDR_GETPOS(&xdrs)))
8357c478bd9Sstevel@tonic-gate 		return (FALSE);
8367c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * If context is not established, or if neither integrity
8407c478bd9Sstevel@tonic-gate 	 * nor privacy is used, just XDR encode data.
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	if (!ap->established || ap->service == rpc_gss_svc_none)
8437c478bd9Sstevel@tonic-gate 		return ((*xdr_func)(out_xdrs, xdr_ptr));
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	return (__rpc_gss_wrap_data(ap->service, ap->qop, ap->context,
8467c478bd9Sstevel@tonic-gate 				ap->seq_num, out_xdrs, xdr_func, xdr_ptr));
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * Unwrap received data.
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate bool_t
8537c478bd9Sstevel@tonic-gate __rpc_gss_unwrap(auth, in_xdrs, xdr_func, xdr_ptr)
8547c478bd9Sstevel@tonic-gate 	AUTH			*auth;
8557c478bd9Sstevel@tonic-gate 	XDR			*in_xdrs;
8567c478bd9Sstevel@tonic-gate 	bool_t			(*xdr_func)();
8577c478bd9Sstevel@tonic-gate 	caddr_t			xdr_ptr;
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	/*LINTED*/
8607c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/*
8637c478bd9Sstevel@tonic-gate 	 * If context is not established, of if neither integrity
8647c478bd9Sstevel@tonic-gate 	 * nor privacy is used, just XDR encode data.
8657c478bd9Sstevel@tonic-gate 	 */
8667c478bd9Sstevel@tonic-gate 	if (!ap->established || ap->service == rpc_gss_svc_none)
8677c478bd9Sstevel@tonic-gate 		return ((*xdr_func)(in_xdrs, xdr_ptr));
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	return (__rpc_gss_unwrap_data(ap->service,
8707c478bd9Sstevel@tonic-gate 				ap->context,
8717c478bd9Sstevel@tonic-gate 				ap->seq_num,
8727c478bd9Sstevel@tonic-gate 				ap->qop,
8737c478bd9Sstevel@tonic-gate 				in_xdrs, xdr_func, xdr_ptr));
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate int
8777c478bd9Sstevel@tonic-gate __rpc_gss_max_data_length(auth, max_tp_unit_len)
8787c478bd9Sstevel@tonic-gate 	AUTH		*auth;
8797c478bd9Sstevel@tonic-gate 	int		max_tp_unit_len;
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	/*LINTED*/
8827c478bd9Sstevel@tonic-gate 	rpc_gss_data		*ap = AUTH_PRIVATE(auth);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if (!ap->established || max_tp_unit_len <= 0)
8857c478bd9Sstevel@tonic-gate 		return (0);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	return (__find_max_data_length(ap->service,
8887c478bd9Sstevel@tonic-gate 			ap->context,
8897c478bd9Sstevel@tonic-gate 			ap->qop,
8907c478bd9Sstevel@tonic-gate 			max_tp_unit_len));
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
893cb620785Sraf void
894cb620785Sraf __rpc_gss_get_error(rpc_gss_error_t *error)
895cb620785Sraf {
896cb620785Sraf 	*error = rpc_gss_err;
897cb620785Sraf }
898cb620785Sraf 
8997c478bd9Sstevel@tonic-gate #undef  rpc_gss_err
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate rpc_gss_error_t	rpc_gss_err;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate rpc_gss_error_t *
9047c478bd9Sstevel@tonic-gate __rpc_gss_err()
9057c478bd9Sstevel@tonic-gate {
906cb620785Sraf 	static thread_key_t rpc_gss_err_key = THR_ONCE_KEY;
907cb620785Sraf 	rpc_gss_error_t *tsd;
9087c478bd9Sstevel@tonic-gate 
909*7257d1b4Sraf 	if (thr_main())
9107c478bd9Sstevel@tonic-gate 		return (&rpc_gss_err);
911*7257d1b4Sraf 	if (thr_keycreate_once(&rpc_gss_err_key, free) != 0)
9127c478bd9Sstevel@tonic-gate 		return (&rpc_gss_err);
913*7257d1b4Sraf 	tsd = pthread_getspecific(rpc_gss_err_key);
914cb620785Sraf 	if (tsd == NULL) {
915cb620785Sraf 		tsd = (rpc_gss_error_t *)calloc(1, sizeof (rpc_gss_error_t));
916*7257d1b4Sraf 		if (thr_setspecific(rpc_gss_err_key, tsd) != 0) {
9177c478bd9Sstevel@tonic-gate 			if (tsd)
9187c478bd9Sstevel@tonic-gate 				free(tsd);
9197c478bd9Sstevel@tonic-gate 			return (&rpc_gss_err);
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	return (tsd);
9237c478bd9Sstevel@tonic-gate }
924