xref: /onnv-gate/usr/src/cmd/lvm/rpc.metad/metad_init.c (revision 11053:f33a1c7f3155)
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*11053SSurya.Prakki@Sun.COM  * Common Development and Distribution License (the "License").
6*11053SSurya.Prakki@Sun.COM  * 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  */
210Sstevel@tonic-gate /*
22*11053SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "metad_local.h"
270Sstevel@tonic-gate #include <metad.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <grp.h>
300Sstevel@tonic-gate #include <pwd.h>
310Sstevel@tonic-gate #include <synch.h>
320Sstevel@tonic-gate #include <netdir.h>
330Sstevel@tonic-gate #include <netdb.h>
340Sstevel@tonic-gate #include <sdssc.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate extern	void	nc_perror(const char *msg);
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*ARGSUSED*/
390Sstevel@tonic-gate void
sigalarmhandler(int sig)400Sstevel@tonic-gate sigalarmhandler(int sig)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	md_exit(NULL, 0);
430Sstevel@tonic-gate }
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * check for trusted host and user
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate static int
check_host(struct svc_req * rqstp)500Sstevel@tonic-gate check_host(
510Sstevel@tonic-gate 	struct svc_req		*rqstp		/* RPC stuff */
520Sstevel@tonic-gate )
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	struct authsys_parms	*sys_credp;
550Sstevel@tonic-gate 	SVCXPRT			*transp = rqstp->rq_xprt;
560Sstevel@tonic-gate 	struct netconfig	*nconfp = NULL;
570Sstevel@tonic-gate 	struct nd_hostservlist	*hservlistp = NULL;
580Sstevel@tonic-gate 	int			i;
590Sstevel@tonic-gate 	int			rval = -1;
600Sstevel@tonic-gate 	char			*inplace = NULL;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/* check for root */
630Sstevel@tonic-gate 	/*LINTED*/
640Sstevel@tonic-gate 	sys_credp = (struct authsys_parms *)rqstp->rq_clntcred;
650Sstevel@tonic-gate 	assert(sys_credp != NULL);
660Sstevel@tonic-gate 	if (sys_credp->aup_uid != 0)
670Sstevel@tonic-gate 		goto out;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	/* get hostnames */
700Sstevel@tonic-gate 	if (transp->xp_netid == NULL) {
710Sstevel@tonic-gate 		md_eprintf("transp->xp_netid == NULL\n");
720Sstevel@tonic-gate 		goto out;
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 	if ((nconfp = getnetconfigent(transp->xp_netid)) == NULL) {
750Sstevel@tonic-gate #ifdef	DEBUG
760Sstevel@tonic-gate 		nc_perror("getnetconfigent(transp->xp_netid)");
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 		goto out;
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 	if ((__netdir_getbyaddr_nosrv(nconfp, &hservlistp, &transp->xp_rtaddr)
810Sstevel@tonic-gate 	    != 0) || (hservlistp == NULL)) {
820Sstevel@tonic-gate #ifdef	DEBUG
830Sstevel@tonic-gate 		netdir_perror("netdir_getbyaddr(transp->xp_rtaddr)");
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate 		goto out;
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/* check hostnames */
890Sstevel@tonic-gate 	for (i = 0; (i < hservlistp->h_cnt); ++i) {
900Sstevel@tonic-gate 		struct nd_hostserv	*hservp = &hservlistp->h_hostservs[i];
910Sstevel@tonic-gate 		char			*hostname = hservp->h_host;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		inplace = strdup(hostname);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 		/* localhost is OK */
960Sstevel@tonic-gate 		if (strcmp(hostname, mynode()) == 0) {
970Sstevel@tonic-gate 			rval = 0;
980Sstevel@tonic-gate 			goto out;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		/* check for remote root access */
1020Sstevel@tonic-gate 		if (ruserok(hostname, 1, "root", "root") == 0) {
1030Sstevel@tonic-gate 			rval = 0;
1040Sstevel@tonic-gate 			goto out;
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		sdssc_cm_nm2nid(inplace);
1080Sstevel@tonic-gate 		if (strcmp(inplace, hostname)) {
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 			/*
1110Sstevel@tonic-gate 			 * If the names are now different it indicates
1120Sstevel@tonic-gate 			 * that hostname was converted to a nodeid. This
1130Sstevel@tonic-gate 			 * will only occur if hostname is part of the same
1140Sstevel@tonic-gate 			 * cluster that the current node is in.
1150Sstevel@tonic-gate 			 * If the machine is not running in a cluster than
1160Sstevel@tonic-gate 			 * sdssc_cm_nm2nid is a noop which leaves inplace
1170Sstevel@tonic-gate 			 * alone.
1180Sstevel@tonic-gate 			 */
1190Sstevel@tonic-gate 			rval = 0;
1200Sstevel@tonic-gate 			goto out;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/* cleanup, return success */
1250Sstevel@tonic-gate out:
1260Sstevel@tonic-gate 	if (inplace)
1270Sstevel@tonic-gate 		free(inplace);
1280Sstevel@tonic-gate 	if (hservlistp != NULL)
1290Sstevel@tonic-gate 		netdir_free(hservlistp, ND_HOSTSERVLIST);
1300Sstevel@tonic-gate 	if (nconfp != NULL)
1310Sstevel@tonic-gate 		Free(nconfp);
1320Sstevel@tonic-gate 	return (rval);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * check for user in local group 14
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate static int
check_gid14(uid_t uid)1390Sstevel@tonic-gate check_gid14(
1400Sstevel@tonic-gate 	uid_t		uid
1410Sstevel@tonic-gate )
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	struct passwd	*pwp;
1440Sstevel@tonic-gate 	struct group	*grp;
1450Sstevel@tonic-gate 	char		**namep;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* get user info, check default GID */
1480Sstevel@tonic-gate 	if ((pwp = getpwuid(uid)) == NULL)
1490Sstevel@tonic-gate 		return (-1);
1500Sstevel@tonic-gate 	if (pwp->pw_gid == METAD_GID)
1510Sstevel@tonic-gate 		return (0);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* check in group */
1540Sstevel@tonic-gate 	if ((grp = getgrgid(METAD_GID)) == NULL)
1550Sstevel@tonic-gate 		return (-1);
1560Sstevel@tonic-gate 	for (namep = grp->gr_mem; ((*namep != NULL) && (**namep != '\0'));
1570Sstevel@tonic-gate 	    ++namep) {
1580Sstevel@tonic-gate 		if (strcmp(*namep, pwp->pw_name) == 0)
1590Sstevel@tonic-gate 			return (0);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 	return (-1);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * check AUTH_SYS
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate static int
check_sys(struct svc_req * rqstp,int amode,md_error_t * ep)1680Sstevel@tonic-gate check_sys(
1690Sstevel@tonic-gate 	struct svc_req		*rqstp,		/* RPC stuff */
1700Sstevel@tonic-gate 	int			amode,		/* R_OK | W_OK */
1710Sstevel@tonic-gate 	md_error_t		*ep		/* returned status */
1720Sstevel@tonic-gate )
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	static mutex_t		mx = DEFAULTMUTEX;
1750Sstevel@tonic-gate 	struct authsys_parms	*sys_credp;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/* for read, anything is OK */
1780Sstevel@tonic-gate 	if (! (amode & W_OK))
1790Sstevel@tonic-gate 		return (0);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/* single thread (not really needed if daemon stays single threaded) */
182*11053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mx);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/* check for remote root or METAD_GID */
1850Sstevel@tonic-gate 	/*LINTED*/
1860Sstevel@tonic-gate 	sys_credp = (struct authsys_parms *)rqstp->rq_clntcred;
1870Sstevel@tonic-gate 	if ((check_gid14(sys_credp->aup_uid) == 0) ||
1880Sstevel@tonic-gate 	    (check_host(rqstp) == 0)) {
189*11053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mx);
1900Sstevel@tonic-gate 		return (0);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/* return failure */
194*11053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mx);
1950Sstevel@tonic-gate 	return (mdsyserror(ep, EACCES, "rpc.metad"));
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * setup RPC service
2000Sstevel@tonic-gate  *
2010Sstevel@tonic-gate  * if can't authenticate return < 0
2020Sstevel@tonic-gate  * any other error return > 0
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate int
svc_init(struct svc_req * rqstp,int amode,md_error_t * ep)2050Sstevel@tonic-gate svc_init(
2060Sstevel@tonic-gate 	struct svc_req	*rqstp,	/* RPC stuff */
2070Sstevel@tonic-gate 	int		amode,	/* R_OK | W_OK */
2080Sstevel@tonic-gate 	md_error_t	*ep	/* returned status */
2090Sstevel@tonic-gate )
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	SVCXPRT		*transp;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (sdssc_bind_library() == SDSSC_ERROR) {
214*11053SSurya.Prakki@Sun.COM 		(void) mdsyserror(ep, EACCES, "can't bind to cluster library");
2150Sstevel@tonic-gate 		return (1);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * if we have no rpc service info, we must have been
2200Sstevel@tonic-gate 	 * called recursively from within the daemon
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	if (rqstp == NULL) {
2230Sstevel@tonic-gate 		mdclrerror(ep);
2240Sstevel@tonic-gate 		return (0);		/* OK */
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * initialize
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	transp = rqstp->rq_xprt;
2310Sstevel@tonic-gate 	assert(transp != NULL);
2320Sstevel@tonic-gate 	*ep = mdnullerror;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * check credentials
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	switch (rqstp->rq_cred.oa_flavor) {
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* UNIX flavor */
2400Sstevel@tonic-gate 	case AUTH_SYS:
2410Sstevel@tonic-gate 	{
2420Sstevel@tonic-gate 		if (check_sys(rqstp, amode, ep) != 0)
2430Sstevel@tonic-gate 			return (1);	/* error */
2440Sstevel@tonic-gate 		break;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/* can't authenticate anything else */
2480Sstevel@tonic-gate 	default:
2490Sstevel@tonic-gate 		svcerr_weakauth(transp);
2500Sstevel@tonic-gate 		return (-1);		/* weak authentication */
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/*
2540Sstevel@tonic-gate 	 * (re)initialize
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	if (md_init_daemon("rpc.metad", ep) != 0)
2570Sstevel@tonic-gate 		return (1);		/* error */
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if (set_snarf(ep))
2600Sstevel@tonic-gate 		return (1);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	sr_validate();
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/* success */
2650Sstevel@tonic-gate 	return (0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*ARGSUSED*/
2690Sstevel@tonic-gate int
svc_fini(md_error_t * ep)2700Sstevel@tonic-gate svc_fini(md_error_t *ep)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	return (0);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate int
check_set_lock(int amode,md_setkey_t * cl_sk,md_error_t * ep)2760Sstevel@tonic-gate check_set_lock(
2770Sstevel@tonic-gate 	int		amode,	/* R_OK | W_OK */
2780Sstevel@tonic-gate 	md_setkey_t	*cl_sk,	/* clients idea of set locked */
2790Sstevel@tonic-gate 	md_error_t	*ep	/* returned status */
2800Sstevel@tonic-gate )
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	md_setkey_t	*svc_sk;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (cl_sk == NULL)
2850Sstevel@tonic-gate 		return (0);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	svc_sk = svc_get_setkey(cl_sk->sk_setno);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/* The set is not locked */
2900Sstevel@tonic-gate 	if (svc_sk == NULL) {
2910Sstevel@tonic-gate 		if ((amode & W_OK) == W_OK) {
2920Sstevel@tonic-gate 			(void) mddserror(ep, MDE_DS_WRITEWITHSULK,
2930Sstevel@tonic-gate 			    cl_sk->sk_setno, mynode(), NULL, cl_sk->sk_setname);
2940Sstevel@tonic-gate 			return (1);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		return (0);
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/* The set is locked, do we have the key? */
3000Sstevel@tonic-gate 	if (cl_sk->sk_key.tv_sec == svc_sk->sk_key.tv_sec &&
3010Sstevel@tonic-gate 	    cl_sk->sk_key.tv_usec == svc_sk->sk_key.tv_usec)
3020Sstevel@tonic-gate 		return (0);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	(void) mddserror(ep, MDE_DS_SETLOCKED, MD_SET_BAD, mynode(),
3050Sstevel@tonic-gate 	    svc_sk->sk_host, svc_sk->sk_setname);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	return (1);
3080Sstevel@tonic-gate }
309