xref: /titanic_41/usr/src/cmd/lvm/rpc.metad/metad_init.c (revision 80148899834a4078a2bd348504aa2d6de9752837)
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
5*80148899SSurya Prakki  * Common Development and Distribution License (the "License").
6*80148899SSurya Prakki  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*80148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include "metad_local.h"
277c478bd9Sstevel@tonic-gate #include <metad.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <grp.h>
307c478bd9Sstevel@tonic-gate #include <pwd.h>
317c478bd9Sstevel@tonic-gate #include <synch.h>
327c478bd9Sstevel@tonic-gate #include <netdir.h>
337c478bd9Sstevel@tonic-gate #include <netdb.h>
347c478bd9Sstevel@tonic-gate #include <sdssc.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate extern	void	nc_perror(const char *msg);
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
397c478bd9Sstevel@tonic-gate void
sigalarmhandler(int sig)407c478bd9Sstevel@tonic-gate sigalarmhandler(int sig)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	md_exit(NULL, 0);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * check for trusted host and user
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate static int
check_host(struct svc_req * rqstp)507c478bd9Sstevel@tonic-gate check_host(
517c478bd9Sstevel@tonic-gate 	struct svc_req		*rqstp		/* RPC stuff */
527c478bd9Sstevel@tonic-gate )
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	struct authsys_parms	*sys_credp;
557c478bd9Sstevel@tonic-gate 	SVCXPRT			*transp = rqstp->rq_xprt;
567c478bd9Sstevel@tonic-gate 	struct netconfig	*nconfp = NULL;
577c478bd9Sstevel@tonic-gate 	struct nd_hostservlist	*hservlistp = NULL;
587c478bd9Sstevel@tonic-gate 	int			i;
597c478bd9Sstevel@tonic-gate 	int			rval = -1;
607c478bd9Sstevel@tonic-gate 	char			*inplace = NULL;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/* check for root */
637c478bd9Sstevel@tonic-gate 	/*LINTED*/
647c478bd9Sstevel@tonic-gate 	sys_credp = (struct authsys_parms *)rqstp->rq_clntcred;
657c478bd9Sstevel@tonic-gate 	assert(sys_credp != NULL);
667c478bd9Sstevel@tonic-gate 	if (sys_credp->aup_uid != 0)
677c478bd9Sstevel@tonic-gate 		goto out;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/* get hostnames */
707c478bd9Sstevel@tonic-gate 	if (transp->xp_netid == NULL) {
717c478bd9Sstevel@tonic-gate 		md_eprintf("transp->xp_netid == NULL\n");
727c478bd9Sstevel@tonic-gate 		goto out;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if ((nconfp = getnetconfigent(transp->xp_netid)) == NULL) {
757c478bd9Sstevel@tonic-gate #ifdef	DEBUG
767c478bd9Sstevel@tonic-gate 		nc_perror("getnetconfigent(transp->xp_netid)");
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate 		goto out;
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	if ((__netdir_getbyaddr_nosrv(nconfp, &hservlistp, &transp->xp_rtaddr)
817c478bd9Sstevel@tonic-gate 	    != 0) || (hservlistp == NULL)) {
827c478bd9Sstevel@tonic-gate #ifdef	DEBUG
837c478bd9Sstevel@tonic-gate 		netdir_perror("netdir_getbyaddr(transp->xp_rtaddr)");
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 		goto out;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/* check hostnames */
897c478bd9Sstevel@tonic-gate 	for (i = 0; (i < hservlistp->h_cnt); ++i) {
907c478bd9Sstevel@tonic-gate 		struct nd_hostserv	*hservp = &hservlistp->h_hostservs[i];
917c478bd9Sstevel@tonic-gate 		char			*hostname = hservp->h_host;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		inplace = strdup(hostname);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		/* localhost is OK */
967c478bd9Sstevel@tonic-gate 		if (strcmp(hostname, mynode()) == 0) {
977c478bd9Sstevel@tonic-gate 			rval = 0;
987c478bd9Sstevel@tonic-gate 			goto out;
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		/* check for remote root access */
1027c478bd9Sstevel@tonic-gate 		if (ruserok(hostname, 1, "root", "root") == 0) {
1037c478bd9Sstevel@tonic-gate 			rval = 0;
1047c478bd9Sstevel@tonic-gate 			goto out;
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		sdssc_cm_nm2nid(inplace);
1087c478bd9Sstevel@tonic-gate 		if (strcmp(inplace, hostname)) {
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 			/*
1117c478bd9Sstevel@tonic-gate 			 * If the names are now different it indicates
1127c478bd9Sstevel@tonic-gate 			 * that hostname was converted to a nodeid. This
1137c478bd9Sstevel@tonic-gate 			 * will only occur if hostname is part of the same
1147c478bd9Sstevel@tonic-gate 			 * cluster that the current node is in.
1157c478bd9Sstevel@tonic-gate 			 * If the machine is not running in a cluster than
1167c478bd9Sstevel@tonic-gate 			 * sdssc_cm_nm2nid is a noop which leaves inplace
1177c478bd9Sstevel@tonic-gate 			 * alone.
1187c478bd9Sstevel@tonic-gate 			 */
1197c478bd9Sstevel@tonic-gate 			rval = 0;
1207c478bd9Sstevel@tonic-gate 			goto out;
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* cleanup, return success */
1257c478bd9Sstevel@tonic-gate out:
1267c478bd9Sstevel@tonic-gate 	if (inplace)
1277c478bd9Sstevel@tonic-gate 		free(inplace);
1287c478bd9Sstevel@tonic-gate 	if (hservlistp != NULL)
1297c478bd9Sstevel@tonic-gate 		netdir_free(hservlistp, ND_HOSTSERVLIST);
1307c478bd9Sstevel@tonic-gate 	if (nconfp != NULL)
1317c478bd9Sstevel@tonic-gate 		Free(nconfp);
1327c478bd9Sstevel@tonic-gate 	return (rval);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * check for user in local group 14
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static int
check_gid14(uid_t uid)1397c478bd9Sstevel@tonic-gate check_gid14(
1407c478bd9Sstevel@tonic-gate 	uid_t		uid
1417c478bd9Sstevel@tonic-gate )
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	struct passwd	*pwp;
1447c478bd9Sstevel@tonic-gate 	struct group	*grp;
1457c478bd9Sstevel@tonic-gate 	char		**namep;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* get user info, check default GID */
1487c478bd9Sstevel@tonic-gate 	if ((pwp = getpwuid(uid)) == NULL)
1497c478bd9Sstevel@tonic-gate 		return (-1);
1507c478bd9Sstevel@tonic-gate 	if (pwp->pw_gid == METAD_GID)
1517c478bd9Sstevel@tonic-gate 		return (0);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* check in group */
1547c478bd9Sstevel@tonic-gate 	if ((grp = getgrgid(METAD_GID)) == NULL)
1557c478bd9Sstevel@tonic-gate 		return (-1);
1567c478bd9Sstevel@tonic-gate 	for (namep = grp->gr_mem; ((*namep != NULL) && (**namep != '\0'));
1577c478bd9Sstevel@tonic-gate 	    ++namep) {
1587c478bd9Sstevel@tonic-gate 		if (strcmp(*namep, pwp->pw_name) == 0)
1597c478bd9Sstevel@tonic-gate 			return (0);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	return (-1);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * check AUTH_SYS
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static int
check_sys(struct svc_req * rqstp,int amode,md_error_t * ep)1687c478bd9Sstevel@tonic-gate check_sys(
1697c478bd9Sstevel@tonic-gate 	struct svc_req		*rqstp,		/* RPC stuff */
1707c478bd9Sstevel@tonic-gate 	int			amode,		/* R_OK | W_OK */
1717c478bd9Sstevel@tonic-gate 	md_error_t		*ep		/* returned status */
1727c478bd9Sstevel@tonic-gate )
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	static mutex_t		mx = DEFAULTMUTEX;
1757c478bd9Sstevel@tonic-gate 	struct authsys_parms	*sys_credp;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* for read, anything is OK */
1787c478bd9Sstevel@tonic-gate 	if (! (amode & W_OK))
1797c478bd9Sstevel@tonic-gate 		return (0);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* single thread (not really needed if daemon stays single threaded) */
182*80148899SSurya Prakki 	(void) mutex_lock(&mx);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* check for remote root or METAD_GID */
1857c478bd9Sstevel@tonic-gate 	/*LINTED*/
1867c478bd9Sstevel@tonic-gate 	sys_credp = (struct authsys_parms *)rqstp->rq_clntcred;
1877c478bd9Sstevel@tonic-gate 	if ((check_gid14(sys_credp->aup_uid) == 0) ||
1887c478bd9Sstevel@tonic-gate 	    (check_host(rqstp) == 0)) {
189*80148899SSurya Prakki 		(void) mutex_unlock(&mx);
1907c478bd9Sstevel@tonic-gate 		return (0);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* return failure */
194*80148899SSurya Prakki 	(void) mutex_unlock(&mx);
1957c478bd9Sstevel@tonic-gate 	return (mdsyserror(ep, EACCES, "rpc.metad"));
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * setup RPC service
2007c478bd9Sstevel@tonic-gate  *
2017c478bd9Sstevel@tonic-gate  * if can't authenticate return < 0
2027c478bd9Sstevel@tonic-gate  * any other error return > 0
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate int
svc_init(struct svc_req * rqstp,int amode,md_error_t * ep)2057c478bd9Sstevel@tonic-gate svc_init(
2067c478bd9Sstevel@tonic-gate 	struct svc_req	*rqstp,	/* RPC stuff */
2077c478bd9Sstevel@tonic-gate 	int		amode,	/* R_OK | W_OK */
2087c478bd9Sstevel@tonic-gate 	md_error_t	*ep	/* returned status */
2097c478bd9Sstevel@tonic-gate )
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	SVCXPRT		*transp;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (sdssc_bind_library() == SDSSC_ERROR) {
214*80148899SSurya Prakki 		(void) mdsyserror(ep, EACCES, "can't bind to cluster library");
2157c478bd9Sstevel@tonic-gate 		return (1);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * if we have no rpc service info, we must have been
2207c478bd9Sstevel@tonic-gate 	 * called recursively from within the daemon
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	if (rqstp == NULL) {
2237c478bd9Sstevel@tonic-gate 		mdclrerror(ep);
2247c478bd9Sstevel@tonic-gate 		return (0);		/* OK */
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * initialize
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	transp = rqstp->rq_xprt;
2317c478bd9Sstevel@tonic-gate 	assert(transp != NULL);
2327c478bd9Sstevel@tonic-gate 	*ep = mdnullerror;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * check credentials
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	switch (rqstp->rq_cred.oa_flavor) {
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* UNIX flavor */
2407c478bd9Sstevel@tonic-gate 	case AUTH_SYS:
2417c478bd9Sstevel@tonic-gate 	{
2427c478bd9Sstevel@tonic-gate 		if (check_sys(rqstp, amode, ep) != 0)
2437c478bd9Sstevel@tonic-gate 			return (1);	/* error */
2447c478bd9Sstevel@tonic-gate 		break;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* can't authenticate anything else */
2487c478bd9Sstevel@tonic-gate 	default:
2497c478bd9Sstevel@tonic-gate 		svcerr_weakauth(transp);
2507c478bd9Sstevel@tonic-gate 		return (-1);		/* weak authentication */
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * (re)initialize
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	if (md_init_daemon("rpc.metad", ep) != 0)
2577c478bd9Sstevel@tonic-gate 		return (1);		/* error */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (set_snarf(ep))
2607c478bd9Sstevel@tonic-gate 		return (1);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	sr_validate();
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* success */
2657c478bd9Sstevel@tonic-gate 	return (0);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2697c478bd9Sstevel@tonic-gate int
svc_fini(md_error_t * ep)2707c478bd9Sstevel@tonic-gate svc_fini(md_error_t *ep)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	return (0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate int
check_set_lock(int amode,md_setkey_t * cl_sk,md_error_t * ep)2767c478bd9Sstevel@tonic-gate check_set_lock(
2777c478bd9Sstevel@tonic-gate 	int		amode,	/* R_OK | W_OK */
2787c478bd9Sstevel@tonic-gate 	md_setkey_t	*cl_sk,	/* clients idea of set locked */
2797c478bd9Sstevel@tonic-gate 	md_error_t	*ep	/* returned status */
2807c478bd9Sstevel@tonic-gate )
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	md_setkey_t	*svc_sk;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (cl_sk == NULL)
2857c478bd9Sstevel@tonic-gate 		return (0);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	svc_sk = svc_get_setkey(cl_sk->sk_setno);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* The set is not locked */
2907c478bd9Sstevel@tonic-gate 	if (svc_sk == NULL) {
2917c478bd9Sstevel@tonic-gate 		if ((amode & W_OK) == W_OK) {
2927c478bd9Sstevel@tonic-gate 			(void) mddserror(ep, MDE_DS_WRITEWITHSULK,
2937c478bd9Sstevel@tonic-gate 			    cl_sk->sk_setno, mynode(), NULL, cl_sk->sk_setname);
2947c478bd9Sstevel@tonic-gate 			return (1);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 		return (0);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* The set is locked, do we have the key? */
3007c478bd9Sstevel@tonic-gate 	if (cl_sk->sk_key.tv_sec == svc_sk->sk_key.tv_sec &&
3017c478bd9Sstevel@tonic-gate 	    cl_sk->sk_key.tv_usec == svc_sk->sk_key.tv_usec)
3027c478bd9Sstevel@tonic-gate 		return (0);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	(void) mddserror(ep, MDE_DS_SETLOCKED, MD_SET_BAD, mynode(),
3057c478bd9Sstevel@tonic-gate 	    svc_sk->sk_host, svc_sk->sk_setname);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	return (1);
3087c478bd9Sstevel@tonic-gate }
309