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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*6935f61bSMarcel Telka 
23*6935f61bSMarcel Telka /*
24*6935f61bSMarcel Telka  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25*6935f61bSMarcel Telka  */
26*6935f61bSMarcel Telka 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
297c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *  Kernel code to obtain client handle to gssd server
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
387c478bd9Sstevel@tonic-gate #include <gssapi/gssd_prot.h>
397c478bd9Sstevel@tonic-gate #include <gssapi/kgssapi_defs.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/uio.h>
447c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	GSSD_RETRY 5
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate kmutex_t	gssrpcb_lock;
497c478bd9Sstevel@tonic-gate zone_key_t	gss_zone_key;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate struct gss_globals {
527c478bd9Sstevel@tonic-gate 	enum clnt_stat		gss_last_stat;
537c478bd9Sstevel@tonic-gate 	struct netbuf		gss_netaddr;
547c478bd9Sstevel@tonic-gate 	struct knetconfig	gss_config;
557c478bd9Sstevel@tonic-gate };
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* ARGSUSED */
587c478bd9Sstevel@tonic-gate void *
gss_zone_init(zoneid_t zoneid)597c478bd9Sstevel@tonic-gate gss_zone_init(zoneid_t zoneid)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	struct gss_globals *gssg;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	gssg = kmem_zalloc(sizeof (*gssg), KM_SLEEP);
647c478bd9Sstevel@tonic-gate 	return (gssg);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* ARGSUSED */
687c478bd9Sstevel@tonic-gate void
gss_zone_fini(zoneid_t zoneid,void * data)697c478bd9Sstevel@tonic-gate gss_zone_fini(zoneid_t zoneid, void *data)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	struct gss_globals *gssg = data;
727c478bd9Sstevel@tonic-gate 	struct netbuf *netaddrp = &gssg->gss_netaddr;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (netaddrp->len != 0)
757c478bd9Sstevel@tonic-gate 		kmem_free(netaddrp->buf, netaddrp->maxlen);
767c478bd9Sstevel@tonic-gate 	kmem_free(gssg, sizeof (*gssg));
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate void
killgssd_handle(CLIENT * client)807c478bd9Sstevel@tonic-gate killgssd_handle(CLIENT *client)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	struct rpc_err rpcerr;
837c478bd9Sstevel@tonic-gate 	struct gss_globals *gssg;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	gssg = zone_getspecific(gss_zone_key, curproc->p_zone);
867c478bd9Sstevel@tonic-gate 	CLNT_GETERR(client, &rpcerr);
877c478bd9Sstevel@tonic-gate 	gssg->gss_last_stat = rpcerr.re_status;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	AUTH_DESTROY(client->cl_auth);
907c478bd9Sstevel@tonic-gate 	CLNT_DESTROY(client);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate CLIENT *
getgssd_handle(void)947c478bd9Sstevel@tonic-gate getgssd_handle(void)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	struct vnode *vp;
977c478bd9Sstevel@tonic-gate 	int error;
987c478bd9Sstevel@tonic-gate 	CLIENT *clnt;
997c478bd9Sstevel@tonic-gate 	enum clnt_stat stat;
1007c478bd9Sstevel@tonic-gate 	struct netbuf tmpaddr;
1017c478bd9Sstevel@tonic-gate 	struct gss_globals *gssg;
1027c478bd9Sstevel@tonic-gate 	struct netbuf *netaddrp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	gssg = zone_getspecific(gss_zone_key, curproc->p_zone);
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * Cribbed from kerb_krpc.c. Really should do the config set up
1077c478bd9Sstevel@tonic-gate 	 * in the _init routine.
1087c478bd9Sstevel@tonic-gate 	 */
1097c478bd9Sstevel@tonic-gate 	if (gssg->gss_config.knc_rdev == 0) {
1107c478bd9Sstevel@tonic-gate 		if ((error = lookupname("/dev/ticotsord", UIO_SYSSPACE,
111*6935f61bSMarcel Telka 		    FOLLOW, NULLVPP, &vp)) != 0) {
1127c478bd9Sstevel@tonic-gate 			GSSLOG(1, "getgssd_handle: lookupname: %d\n", error);
1137c478bd9Sstevel@tonic-gate 			return (NULL);
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 		gssg->gss_config.knc_rdev = vp->v_rdev;
1167c478bd9Sstevel@tonic-gate 		gssg->gss_config.knc_protofmly = loopback_name;
1177c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1187c478bd9Sstevel@tonic-gate 		gssg->gss_config.knc_semantics = NC_TPI_COTS_ORD;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Contact rpcbind to get gssd's address only
1237c478bd9Sstevel@tonic-gate 	 * once and re-use the address.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	mutex_enter(&gssrpcb_lock);
1267c478bd9Sstevel@tonic-gate 	netaddrp = &gssg->gss_netaddr;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (netaddrp->len == 0 || gssg->gss_last_stat != RPC_SUCCESS) {
129*6935f61bSMarcel Telka 		if (netaddrp->buf != NULL)
1307c478bd9Sstevel@tonic-gate 			kmem_free(netaddrp->buf, netaddrp->maxlen);
1317c478bd9Sstevel@tonic-gate 
132*6935f61bSMarcel Telka 		/* Set up netaddr to be "localhost." (strlen is 10) */
133*6935f61bSMarcel Telka 		netaddrp->len = netaddrp->maxlen = 10;
134*6935f61bSMarcel Telka 		netaddrp->buf = kmem_alloc(netaddrp->len, KM_SLEEP);
135*6935f61bSMarcel Telka 		(void) strncpy(netaddrp->buf, "localhost.", netaddrp->len);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		/* Get address of gssd from rpcbind */
1387c478bd9Sstevel@tonic-gate 		stat = rpcbind_getaddr(&gssg->gss_config, GSSPROG, GSSVERS,
1397c478bd9Sstevel@tonic-gate 		    netaddrp);
1407c478bd9Sstevel@tonic-gate 		if (stat != RPC_SUCCESS) {
1417c478bd9Sstevel@tonic-gate 			kmem_free(netaddrp->buf, netaddrp->maxlen);
142*6935f61bSMarcel Telka 			netaddrp->buf = NULL;
1437c478bd9Sstevel@tonic-gate 			netaddrp->len = netaddrp->maxlen = 0;
1447c478bd9Sstevel@tonic-gate 			mutex_exit(&gssrpcb_lock);
1457c478bd9Sstevel@tonic-gate 			return (NULL);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Copy the netaddr information into a tmp location to
1517c478bd9Sstevel@tonic-gate 	 * be used by clnt_tli_kcreate.  The purpose of this
1527c478bd9Sstevel@tonic-gate 	 * is for MT race condition (ie. netaddr being modified
1537c478bd9Sstevel@tonic-gate 	 * while it is being used.)
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	tmpaddr.buf = kmem_zalloc(netaddrp->maxlen, KM_SLEEP);
1567c478bd9Sstevel@tonic-gate 	bcopy(netaddrp->buf, tmpaddr.buf, netaddrp->maxlen);
1577c478bd9Sstevel@tonic-gate 	tmpaddr.maxlen = netaddrp->maxlen;
1587c478bd9Sstevel@tonic-gate 	tmpaddr.len = netaddrp->len;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	mutex_exit(&gssrpcb_lock);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	error = clnt_tli_kcreate(&gssg->gss_config, &tmpaddr, GSSPROG,
163*6935f61bSMarcel Telka 	    GSSVERS, 0, GSSD_RETRY, kcred, &clnt);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	kmem_free(tmpaddr.buf, tmpaddr.maxlen);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (error != 0) {
1687c478bd9Sstevel@tonic-gate 		GSSLOG(1,
1697c478bd9Sstevel@tonic-gate 		"getgssd_handle: clnt_tli_kcreate: error %d\n", error);
1707c478bd9Sstevel@tonic-gate 		return (NULL);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	return (clnt);
1747c478bd9Sstevel@tonic-gate }
175