xref: /onnv-gate/usr/src/lib/libnsl/rpc/rpcsec_gss_if.c (revision 3864:2ae506652d11)
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
5*3864Sraf  * Common Development and Distribution License (the "License").
6*3864Sraf  * 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  */
21132Srobinson 
220Sstevel@tonic-gate /*
23*3864Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * 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 #include "mt.h"
300Sstevel@tonic-gate #include "rpc_mt.h"
310Sstevel@tonic-gate #include <stdio.h>
32*3864Sraf #include <atomic.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <dlfcn.h>
350Sstevel@tonic-gate #include <rpc/rpc.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	RPCSEC	"rpcsec.so.1"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate typedef struct {
400Sstevel@tonic-gate 	AUTH		*(*rpc_gss_seccreate)();
410Sstevel@tonic-gate 	bool_t		(*rpc_gss_set_defaults)();
420Sstevel@tonic-gate 	bool_t		(*rpc_gss_get_principal_name)();
430Sstevel@tonic-gate 	char		**(*rpc_gss_get_mechanisms)();
440Sstevel@tonic-gate 	char		**(*rpc_gss_get_mech_info)();
450Sstevel@tonic-gate 	bool_t		(*rpc_gss_get_versions)();
460Sstevel@tonic-gate 	bool_t		(*rpc_gss_is_installed)();
470Sstevel@tonic-gate 	bool_t		(*rpc_gss_set_svc_name)();
480Sstevel@tonic-gate 	bool_t		(*rpc_gss_set_callback)();
490Sstevel@tonic-gate 	bool_t		(*rpc_gss_getcred)();
500Sstevel@tonic-gate 	bool_t		(*rpc_gss_mech_to_oid)();
510Sstevel@tonic-gate 	bool_t		(*rpc_gss_qop_to_num)();
520Sstevel@tonic-gate 	enum auth_stat	(*__svcrpcsec_gss)();
530Sstevel@tonic-gate 	bool_t		(*__rpc_gss_wrap)();
540Sstevel@tonic-gate 	bool_t		(*__rpc_gss_unwrap)();
550Sstevel@tonic-gate 	int		(*rpc_gss_max_data_length)();
560Sstevel@tonic-gate 	int		(*rpc_gss_svc_max_data_length)();
570Sstevel@tonic-gate 	void		(*rpc_gss_get_error)();
580Sstevel@tonic-gate } rpcgss_calls_t;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static rpcgss_calls_t calls;
610Sstevel@tonic-gate static mutex_t rpcgss_calls_mutex = DEFAULTMUTEX;
620Sstevel@tonic-gate static bool_t initialized = FALSE;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static bool_t
rpcgss_calls_init(void)65132Srobinson rpcgss_calls_init(void)
660Sstevel@tonic-gate {
67*3864Sraf 	void	*handle;
680Sstevel@tonic-gate 	bool_t	ret = FALSE;
690Sstevel@tonic-gate 
70*3864Sraf 	if (initialized) {
71*3864Sraf 		membar_consumer();
72*3864Sraf 		return (TRUE);
73*3864Sraf 	}
74132Srobinson 	(void) mutex_lock(&rpcgss_calls_mutex);
750Sstevel@tonic-gate 	if (initialized) {
76*3864Sraf 		(void) mutex_unlock(&rpcgss_calls_mutex);
77*3864Sraf 		membar_consumer();
78*3864Sraf 		return (TRUE);
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if ((handle = dlopen(RPCSEC, RTLD_LAZY)) == NULL)
820Sstevel@tonic-gate 		goto done;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if ((calls.rpc_gss_seccreate = (AUTH *(*)()) dlsym(handle,
850Sstevel@tonic-gate 					"__rpc_gss_seccreate")) == NULL)
860Sstevel@tonic-gate 		goto done;
870Sstevel@tonic-gate 	if ((calls.rpc_gss_set_defaults = (bool_t (*)()) dlsym(handle,
880Sstevel@tonic-gate 					"__rpc_gss_set_defaults")) == NULL)
890Sstevel@tonic-gate 		goto done;
900Sstevel@tonic-gate 	if ((calls.rpc_gss_get_principal_name = (bool_t (*)()) dlsym(handle,
910Sstevel@tonic-gate 				"__rpc_gss_get_principal_name")) == NULL)
920Sstevel@tonic-gate 		goto done;
930Sstevel@tonic-gate 	if ((calls.rpc_gss_get_mechanisms = (char **(*)()) dlsym(handle,
940Sstevel@tonic-gate 					"__rpc_gss_get_mechanisms")) == NULL)
950Sstevel@tonic-gate 		goto done;
960Sstevel@tonic-gate 	if ((calls.rpc_gss_get_mech_info = (char **(*)()) dlsym(handle,
970Sstevel@tonic-gate 					"__rpc_gss_get_mech_info")) == NULL)
980Sstevel@tonic-gate 		goto done;
990Sstevel@tonic-gate 	if ((calls.rpc_gss_get_versions = (bool_t (*)()) dlsym(handle,
1000Sstevel@tonic-gate 					"__rpc_gss_get_versions")) == NULL)
1010Sstevel@tonic-gate 		goto done;
1020Sstevel@tonic-gate 	if ((calls.rpc_gss_is_installed = (bool_t (*)()) dlsym(handle,
1030Sstevel@tonic-gate 					"__rpc_gss_is_installed")) == NULL)
1040Sstevel@tonic-gate 		goto done;
1050Sstevel@tonic-gate 	if ((calls.rpc_gss_set_svc_name = (bool_t (*)()) dlsym(handle,
1060Sstevel@tonic-gate 					"__rpc_gss_set_svc_name")) == NULL)
1070Sstevel@tonic-gate 		goto done;
1080Sstevel@tonic-gate 	if ((calls.rpc_gss_set_callback = (bool_t (*)()) dlsym(handle,
1090Sstevel@tonic-gate 					"__rpc_gss_set_callback")) == NULL)
1100Sstevel@tonic-gate 		goto done;
1110Sstevel@tonic-gate 	if ((calls.rpc_gss_getcred = (bool_t (*)()) dlsym(handle,
1120Sstevel@tonic-gate 					"__rpc_gss_getcred")) == NULL)
1130Sstevel@tonic-gate 		goto done;
1140Sstevel@tonic-gate 	if ((calls.rpc_gss_mech_to_oid = (bool_t (*)()) dlsym(handle,
1150Sstevel@tonic-gate 					"__rpc_gss_mech_to_oid")) == NULL)
1160Sstevel@tonic-gate 		goto done;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if ((calls.rpc_gss_qop_to_num = (bool_t (*)()) dlsym(handle,
1190Sstevel@tonic-gate 					"__rpc_gss_qop_to_num")) == NULL)
1200Sstevel@tonic-gate 		goto done;
1210Sstevel@tonic-gate 	if ((calls.__svcrpcsec_gss = (enum auth_stat (*)()) dlsym(handle,
1220Sstevel@tonic-gate 					"__svcrpcsec_gss")) == NULL)
1230Sstevel@tonic-gate 		goto done;
1240Sstevel@tonic-gate 	if ((calls.__rpc_gss_wrap = (bool_t (*)()) dlsym(handle,
1250Sstevel@tonic-gate 					"__rpc_gss_wrap")) == NULL)
1260Sstevel@tonic-gate 		goto done;
1270Sstevel@tonic-gate 	if ((calls.__rpc_gss_unwrap = (bool_t (*)()) dlsym(handle,
1280Sstevel@tonic-gate 					"__rpc_gss_unwrap")) == NULL)
1290Sstevel@tonic-gate 		goto done;
1300Sstevel@tonic-gate 	if ((calls.rpc_gss_max_data_length = (int (*)()) dlsym(handle,
1310Sstevel@tonic-gate 					"__rpc_gss_max_data_length")) == NULL)
1320Sstevel@tonic-gate 		goto done;
1330Sstevel@tonic-gate 	if ((calls.rpc_gss_svc_max_data_length = (int (*)()) dlsym(handle,
1340Sstevel@tonic-gate 				"__rpc_gss_svc_max_data_length")) == NULL)
1350Sstevel@tonic-gate 		goto done;
1360Sstevel@tonic-gate 	if ((calls.rpc_gss_get_error = (void (*)()) dlsym(handle,
1370Sstevel@tonic-gate 					"__rpc_gss_get_error")) == NULL)
1380Sstevel@tonic-gate 		goto done;
1390Sstevel@tonic-gate 	ret = TRUE;
1400Sstevel@tonic-gate done:
1410Sstevel@tonic-gate 	if (!ret) {
1420Sstevel@tonic-gate 		if (handle != NULL)
143132Srobinson 			(void) dlclose(handle);
1440Sstevel@tonic-gate 	}
145*3864Sraf 	membar_producer();
1460Sstevel@tonic-gate 	initialized = ret;
147132Srobinson 	(void) mutex_unlock(&rpcgss_calls_mutex);
1480Sstevel@tonic-gate 	return (ret);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate AUTH *
rpc_gss_seccreate(CLIENT * clnt,char * principal,char * mechanism,rpc_gss_service_t service_type,char * qop,rpc_gss_options_req_t * options_req,rpc_gss_options_ret_t * options_ret)1520Sstevel@tonic-gate rpc_gss_seccreate(
1530Sstevel@tonic-gate 	CLIENT			*clnt,		/* associated client handle */
1540Sstevel@tonic-gate 	char			*principal,	/* server service principal */
1550Sstevel@tonic-gate 	char			*mechanism,	/* security mechanism */
1560Sstevel@tonic-gate 	rpc_gss_service_t	service_type,	/* security service */
1570Sstevel@tonic-gate 	char			*qop,		/* requested QOP */
1580Sstevel@tonic-gate 	rpc_gss_options_req_t	*options_req,	/* requested options */
1590Sstevel@tonic-gate 	rpc_gss_options_ret_t	*options_ret)	/* returned options */
1600Sstevel@tonic-gate {
161*3864Sraf 	if (!rpcgss_calls_init())
162132Srobinson 		return (NULL);
1630Sstevel@tonic-gate 	return ((*calls.rpc_gss_seccreate)(clnt, principal, mechanism,
1640Sstevel@tonic-gate 				service_type, qop, options_req, options_ret));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate bool_t
rpc_gss_set_defaults(AUTH * auth,rpc_gss_service_t service,char * qop)168132Srobinson rpc_gss_set_defaults(AUTH *auth, rpc_gss_service_t service, char *qop)
1690Sstevel@tonic-gate {
170*3864Sraf 	if (!rpcgss_calls_init())
1710Sstevel@tonic-gate 		return (FALSE);
1720Sstevel@tonic-gate 	return ((*calls.rpc_gss_set_defaults)(auth, service, qop));
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate bool_t
rpc_gss_get_principal_name(rpc_gss_principal_t * principal,char * mechanism,char * user_name,char * node,char * secdomain)1760Sstevel@tonic-gate rpc_gss_get_principal_name(
1770Sstevel@tonic-gate 	rpc_gss_principal_t	*principal,
1780Sstevel@tonic-gate 	char			*mechanism,
1790Sstevel@tonic-gate 	char			*user_name,
1800Sstevel@tonic-gate 	char			*node,
1810Sstevel@tonic-gate 	char			*secdomain)
1820Sstevel@tonic-gate {
183*3864Sraf 	if (!rpcgss_calls_init())
1840Sstevel@tonic-gate 		return (FALSE);
1850Sstevel@tonic-gate 	return ((*calls.rpc_gss_get_principal_name)(principal, mechanism,
1860Sstevel@tonic-gate 					user_name, node, secdomain));
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate char **
rpc_gss_get_mechanisms(void)190132Srobinson rpc_gss_get_mechanisms(void)
1910Sstevel@tonic-gate {
192*3864Sraf 	if (!rpcgss_calls_init())
193132Srobinson 		return (NULL);
1940Sstevel@tonic-gate 	return ((*calls.rpc_gss_get_mechanisms)());
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate char **
rpc_gss_get_mech_info(char * mechanism,rpc_gss_service_t * service)198132Srobinson rpc_gss_get_mech_info(char *mechanism, rpc_gss_service_t *service)
1990Sstevel@tonic-gate {
200*3864Sraf 	if (!rpcgss_calls_init())
201132Srobinson 		return (NULL);
2020Sstevel@tonic-gate 	return ((*calls.rpc_gss_get_mech_info)(mechanism, service));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate bool_t
rpc_gss_get_versions(uint_t * vers_hi,uint_t * vers_lo)206132Srobinson rpc_gss_get_versions(uint_t *vers_hi, uint_t *vers_lo)
2070Sstevel@tonic-gate {
208*3864Sraf 	if (!rpcgss_calls_init())
2090Sstevel@tonic-gate 		return (FALSE);
2100Sstevel@tonic-gate 	return ((*calls.rpc_gss_get_versions)(vers_hi, vers_lo));
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate bool_t
rpc_gss_is_installed(char * mechanism)214132Srobinson rpc_gss_is_installed(char *mechanism)
2150Sstevel@tonic-gate {
216*3864Sraf 	if (!rpcgss_calls_init())
2170Sstevel@tonic-gate 		return (FALSE);
2180Sstevel@tonic-gate 	return ((*calls.rpc_gss_is_installed)(mechanism));
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate bool_t
rpc_gss_set_svc_name(char * principal,char * mechanism,uint_t req_time,uint_t program,uint_t version)2220Sstevel@tonic-gate rpc_gss_set_svc_name(
2230Sstevel@tonic-gate 	char			*principal, /* server service principal name */
2240Sstevel@tonic-gate 	char			*mechanism,
2250Sstevel@tonic-gate 	uint_t			req_time,
2260Sstevel@tonic-gate 	uint_t			program,
2270Sstevel@tonic-gate 	uint_t			version)
2280Sstevel@tonic-gate {
229*3864Sraf 	if (!rpcgss_calls_init())
2300Sstevel@tonic-gate 		return (FALSE);
2310Sstevel@tonic-gate 	return ((*calls.rpc_gss_set_svc_name)(principal, mechanism, req_time,
2320Sstevel@tonic-gate 						program, version));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate bool_t
rpc_gss_set_callback(rpc_gss_callback_t * cb)236132Srobinson rpc_gss_set_callback(rpc_gss_callback_t *cb)
2370Sstevel@tonic-gate {
238*3864Sraf 	if (!rpcgss_calls_init())
2390Sstevel@tonic-gate 		return (FALSE);
2400Sstevel@tonic-gate 	return ((*calls.rpc_gss_set_callback)(cb));
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate bool_t
rpc_gss_getcred(struct svc_req * req,rpc_gss_rawcred_t ** rcred,rpc_gss_ucred_t ** ucred,void ** cookie)244132Srobinson rpc_gss_getcred(struct svc_req *req, rpc_gss_rawcred_t **rcred,
245132Srobinson 					rpc_gss_ucred_t **ucred, void **cookie)
2460Sstevel@tonic-gate {
247*3864Sraf 	if (!rpcgss_calls_init())
2480Sstevel@tonic-gate 		return (FALSE);
2490Sstevel@tonic-gate 	return ((*calls.rpc_gss_getcred)(req, rcred, ucred, cookie));
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate bool_t
rpc_gss_mech_to_oid(char * mech,rpc_gss_OID * oid)253132Srobinson rpc_gss_mech_to_oid(char *mech, rpc_gss_OID *oid)
2540Sstevel@tonic-gate {
255*3864Sraf 	if (!rpcgss_calls_init())
2560Sstevel@tonic-gate 		return (FALSE);
2570Sstevel@tonic-gate 	return ((*calls.rpc_gss_mech_to_oid)(mech, oid));
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate bool_t
rpc_gss_qop_to_num(char * qop,char * mech,uint_t * num)261132Srobinson rpc_gss_qop_to_num(char *qop, char *mech, uint_t *num)
2620Sstevel@tonic-gate {
263*3864Sraf 	if (!rpcgss_calls_init())
2640Sstevel@tonic-gate 		return (FALSE);
2650Sstevel@tonic-gate 	return ((*calls.rpc_gss_qop_to_num)(qop, mech, num));
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate enum auth_stat
__svcrpcsec_gss(struct svc_req * rqst,struct rpc_msg * msg,bool_t * no_dispatch)269132Srobinson __svcrpcsec_gss(struct svc_req *rqst, struct rpc_msg *msg, bool_t *no_dispatch)
2700Sstevel@tonic-gate {
271*3864Sraf 	if (!rpcgss_calls_init())
2720Sstevel@tonic-gate 		return (AUTH_FAILED);
2730Sstevel@tonic-gate 	return ((*calls.__svcrpcsec_gss)(rqst, msg, no_dispatch));
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate bool_t
__rpc_gss_wrap(AUTH * auth,char * buf,uint_t buflen,XDR * out_xdrs,bool_t (* xdr_func)(),caddr_t xdr_ptr)277132Srobinson __rpc_gss_wrap(AUTH *auth, char *buf, uint_t buflen, XDR *out_xdrs,
278132Srobinson 					bool_t (*xdr_func)(), caddr_t xdr_ptr)
2790Sstevel@tonic-gate {
280*3864Sraf 	if (!rpcgss_calls_init())
2810Sstevel@tonic-gate 		return (FALSE);
2820Sstevel@tonic-gate 	return ((*calls.__rpc_gss_wrap)(auth, buf, buflen, out_xdrs,
2830Sstevel@tonic-gate 							xdr_func, xdr_ptr));
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate bool_t
__rpc_gss_unwrap(AUTH * auth,XDR * in_xdrs,bool_t (* xdr_func)(),caddr_t xdr_ptr)287132Srobinson __rpc_gss_unwrap(AUTH *auth, XDR *in_xdrs, bool_t (*xdr_func)(),
288132Srobinson 								caddr_t xdr_ptr)
2890Sstevel@tonic-gate {
290*3864Sraf 	if (!rpcgss_calls_init())
2910Sstevel@tonic-gate 		return (FALSE);
2920Sstevel@tonic-gate 	return ((*calls.__rpc_gss_unwrap)(auth, in_xdrs, xdr_func, xdr_ptr));
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate int
rpc_gss_max_data_length(AUTH * rpcgss_handle,int max_tp_unit_len)296132Srobinson rpc_gss_max_data_length(AUTH *rpcgss_handle, int max_tp_unit_len)
2970Sstevel@tonic-gate {
298*3864Sraf 	if (!rpcgss_calls_init())
2990Sstevel@tonic-gate 		return (0);
3000Sstevel@tonic-gate 	return ((*calls.rpc_gss_max_data_length)(rpcgss_handle,
3010Sstevel@tonic-gate 					max_tp_unit_len));
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate int
rpc_gss_svc_max_data_length(struct svc_req * req,int max_tp_unit_len)305132Srobinson rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len)
3060Sstevel@tonic-gate {
307*3864Sraf 	if (!rpcgss_calls_init())
3080Sstevel@tonic-gate 		return (0);
3090Sstevel@tonic-gate 	return ((*calls.rpc_gss_svc_max_data_length)(req, max_tp_unit_len));
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate void
rpc_gss_get_error(rpc_gss_error_t * error)313132Srobinson rpc_gss_get_error(rpc_gss_error_t *error)
3140Sstevel@tonic-gate {
315*3864Sraf 	if (!rpcgss_calls_init()) {
3160Sstevel@tonic-gate 		error->rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
3170Sstevel@tonic-gate 		error->system_error = ENOTSUP;
3180Sstevel@tonic-gate 		return;
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	(*calls.rpc_gss_get_error)(error);
3210Sstevel@tonic-gate }
322