xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_rpc.c (revision 12967:ab9ae749152f)
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
56559Sstephh  * Common Development and Distribution License (the "License").
66559Sstephh  * 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*12967Sgavin.maltby@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/fm/util.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <netdir.h>
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <alloca.h>
310Sstevel@tonic-gate #include <limits.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <ucred.h>
340Sstevel@tonic-gate #include <priv.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <fmd_rpc_api.h>
370Sstevel@tonic-gate #include <fmd_rpc_adm.h>
380Sstevel@tonic-gate #include <rpc/svc_mt.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <fmd_subr.h>
410Sstevel@tonic-gate #include <fmd_error.h>
420Sstevel@tonic-gate #include <fmd_thread.h>
430Sstevel@tonic-gate #include <fmd_conf.h>
440Sstevel@tonic-gate #include <fmd_api.h>
450Sstevel@tonic-gate #include <fmd.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Define range of transient RPC program numbers to use for transient bindings.
490Sstevel@tonic-gate  * These are defined in the Solaris ONC+ Developer's Guide, Appendix B, but
500Sstevel@tonic-gate  * are cleverly not defined in any ONC+ standard system header file.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate #define	RPC_TRANS_MIN	0x40000000
530Sstevel@tonic-gate #define	RPC_TRANS_MAX	0x5fffffff
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * We use our own private version of svc_create() which registers our services
570Sstevel@tonic-gate  * only on loopback transports and enables an option whereby Solaris ucreds
580Sstevel@tonic-gate  * are associated with each connection, permitting us to check privilege bits.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate static int
fmd_rpc_svc_create_local(void (* disp)(struct svc_req *,SVCXPRT *),rpcprog_t prog,rpcvers_t vers,uint_t ssz,uint_t rsz,int force)610Sstevel@tonic-gate fmd_rpc_svc_create_local(void (*disp)(struct svc_req *, SVCXPRT *),
620Sstevel@tonic-gate     rpcprog_t prog, rpcvers_t vers, uint_t ssz, uint_t rsz, int force)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	struct netconfig *ncp;
650Sstevel@tonic-gate 	struct netbuf buf;
660Sstevel@tonic-gate 	SVCXPRT *xprt;
670Sstevel@tonic-gate 	void *hdl;
680Sstevel@tonic-gate 	int fd, n = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	char door[PATH_MAX];
710Sstevel@tonic-gate 	time_t tm;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if ((hdl = setnetconfig()) == NULL) {
740Sstevel@tonic-gate 		fmd_error(EFMD_RPC_REG, "failed to iterate over "
750Sstevel@tonic-gate 		    "netconfig database: %s\n", nc_sperror());
760Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_RPC_REG));
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (force)
800Sstevel@tonic-gate 		svc_unreg(prog, vers); /* clear stale rpcbind registrations */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	buf.buf = alloca(_SS_MAXSIZE);
830Sstevel@tonic-gate 	buf.maxlen = _SS_MAXSIZE;
840Sstevel@tonic-gate 	buf.len = 0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	while ((ncp = getnetconfig(hdl)) != NULL) {
870Sstevel@tonic-gate 		if (strcmp(ncp->nc_protofmly, NC_LOOPBACK) != 0)
880Sstevel@tonic-gate 			continue;
890Sstevel@tonic-gate 
901193Smws 		if (!force && rpcb_getaddr(prog, vers, ncp, &buf, HOST_SELF)) {
911193Smws 			(void) endnetconfig(hdl);
920Sstevel@tonic-gate 			return (fmd_set_errno(EFMD_RPC_BOUND));
931193Smws 		}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 		if ((fd = t_open(ncp->nc_device, O_RDWR, NULL)) == -1) {
960Sstevel@tonic-gate 			fmd_error(EFMD_RPC_REG, "failed to open %s: %s\n",
970Sstevel@tonic-gate 			    ncp->nc_device, t_strerror(t_errno));
980Sstevel@tonic-gate 			continue;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		svc_fd_negotiate_ucred(fd); /* enable ucred option on xprt */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		if ((xprt = svc_tli_create(fd, ncp, NULL, ssz, rsz)) == NULL) {
1040Sstevel@tonic-gate 			(void) t_close(fd);
1050Sstevel@tonic-gate 			continue;
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		if (svc_reg(xprt, prog, vers, disp, ncp) == FALSE) {
1090Sstevel@tonic-gate 			fmd_error(EFMD_RPC_REG, "failed to register "
1100Sstevel@tonic-gate 			    "rpc service on %s\n", ncp->nc_netid);
1110Sstevel@tonic-gate 			svc_destroy(xprt);
1120Sstevel@tonic-gate 			continue;
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		n++;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	(void) endnetconfig(hdl);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/*
1210Sstevel@tonic-gate 	 * If we failed to register services (n == 0) because rpcbind is down,
1220Sstevel@tonic-gate 	 * then check to see if the RPC door file exists before attempting an
1230Sstevel@tonic-gate 	 * svc_door_create(), which cleverly destroys any existing door file.
1240Sstevel@tonic-gate 	 * The RPC APIs have no stable errnos, so we use rpcb_gettime() as a
1250Sstevel@tonic-gate 	 * hack to determine if rpcbind itself is down.
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	if (!force && n == 0 && rpcb_gettime(HOST_SELF, &tm) == FALSE &&
1280Sstevel@tonic-gate 	    snprintf(door, sizeof (door), RPC_DOOR_RENDEZVOUS,
1290Sstevel@tonic-gate 	    prog, vers) > 0 && access(door, F_OK) == 0)
1300Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_RPC_BOUND));
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/*
1331193Smws 	 * Attempt to create a door server for the RPC program as well.  Limit
1341193Smws 	 * the maximum request size for the door transport to the receive size.
1350Sstevel@tonic-gate 	 */
1360Sstevel@tonic-gate 	if ((xprt = svc_door_create(disp, prog, vers, ssz)) == NULL) {
1370Sstevel@tonic-gate 		fmd_error(EFMD_RPC_REG, "failed to create door for "
1380Sstevel@tonic-gate 		    "rpc service 0x%lx/0x%lx\n", prog, vers);
1390Sstevel@tonic-gate 	} else {
1400Sstevel@tonic-gate 		(void) svc_control(xprt, SVCSET_CONNMAXREC, &rsz);
1410Sstevel@tonic-gate 		n++;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	return (n);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static int
fmd_rpc_svc_init(void (* disp)(struct svc_req *,SVCXPRT *),const char * name,const char * path,const char * prop,rpcprog_t pmin,rpcprog_t pmax,rpcvers_t vers,uint_t sndsize,uint_t rcvsize,int force)1480Sstevel@tonic-gate fmd_rpc_svc_init(void (*disp)(struct svc_req *, SVCXPRT *),
1490Sstevel@tonic-gate     const char *name, const char *path, const char *prop,
1500Sstevel@tonic-gate     rpcprog_t pmin, rpcprog_t pmax, rpcvers_t vers,
1510Sstevel@tonic-gate     uint_t sndsize, uint_t rcvsize, int force)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	rpcprog_t prog;
1540Sstevel@tonic-gate 	char buf[16];
1550Sstevel@tonic-gate 	FILE *fp;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	for (prog = pmin; prog <= pmax; prog++) {
1580Sstevel@tonic-gate 		if (fmd_rpc_svc_create_local(disp, prog, vers,
1590Sstevel@tonic-gate 		    sndsize, rcvsize, force) > 0) {
1600Sstevel@tonic-gate 			fmd_dprintf(FMD_DBG_RPC, "registered %s rpc service "
1610Sstevel@tonic-gate 			    "as 0x%lx.%lx\n", name, prog, vers);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 			/*
1640Sstevel@tonic-gate 			 * To aid simulator scripts, save our RPC "digits" in
1650Sstevel@tonic-gate 			 * the specified file for rendezvous with libfmd_adm.
1660Sstevel@tonic-gate 			 */
1670Sstevel@tonic-gate 			if (path != NULL && (fp = fopen(path, "w")) != NULL) {
1680Sstevel@tonic-gate 				(void) fprintf(fp, "%ld\n", prog);
1690Sstevel@tonic-gate 				(void) fclose(fp);
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%ld", prog);
1730Sstevel@tonic-gate 			(void) fmd_conf_setprop(fmd.d_conf, prop, buf);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 			return (0);
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	return (-1); /* errno is set for us */
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate void
fmd_rpc_init(void)1830Sstevel@tonic-gate fmd_rpc_init(void)
1840Sstevel@tonic-gate {
1851193Smws 	int err, prog, mode = RPC_SVC_MT_USER;
1860Sstevel@tonic-gate 	uint64_t sndsize = 0, rcvsize = 0;
1871193Smws 	const char *s;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (rpc_control(RPC_SVC_MTMODE_SET, &mode) == FALSE)
1900Sstevel@tonic-gate 		fmd_panic("failed to enable user-MT rpc mode");
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.sndsize", &sndsize);
1930Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.rcvsize", &rcvsize);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * Infer whether we are the "default" fault manager or an alternate one
1971193Smws 	 * based on whether the initial setting of rpc.adm.prog is non-zero.
1980Sstevel@tonic-gate 	 */
1991193Smws 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
2000Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.path", &s);
2010Sstevel@tonic-gate 
2021193Smws 	if (prog != 0) {
2030Sstevel@tonic-gate 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
2040Sstevel@tonic-gate 		    FMD_ADM, FMD_ADM, FMD_ADM_VERSION_1,
2050Sstevel@tonic-gate 		    (uint_t)sndsize, (uint_t)rcvsize, TRUE);
2060Sstevel@tonic-gate 	} else {
2070Sstevel@tonic-gate 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
2080Sstevel@tonic-gate 		    RPC_TRANS_MIN, RPC_TRANS_MAX, FMD_ADM_VERSION_1,
2090Sstevel@tonic-gate 		    (uint_t)sndsize, (uint_t)rcvsize, FALSE);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (err != 0)
2130Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create rpc server bindings");
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (fmd_thread_create(fmd.d_rmod, (fmd_thread_f *)svc_run, 0) == NULL)
2160Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create rpc server thread");
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate void
fmd_rpc_fini(void)2200Sstevel@tonic-gate fmd_rpc_fini(void)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	rpcprog_t prog;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	svc_exit(); /* force svc_run() threads to exit */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
2270Sstevel@tonic-gate 	svc_unreg(prog, FMD_ADM_VERSION_1);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.api.prog", &prog);
2300Sstevel@tonic-gate 	svc_unreg(prog, FMD_API_VERSION_1);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * Utillity function to fetch the XPRT's ucred and determine if we should deny
2350Sstevel@tonic-gate  * the request.  For now, we implement a simple policy of rejecting any caller
236*12967Sgavin.maltby@oracle.com  * who does not have the PRIV_SYS_ADMIN bit in their Effective privilege set,
2370Sstevel@tonic-gate  * unless the caller is loading a module, which requires all privileges.
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate int
fmd_rpc_deny(struct svc_req * rqp)2400Sstevel@tonic-gate fmd_rpc_deny(struct svc_req *rqp)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	ucred_t *ucp = alloca(ucred_size());
2430Sstevel@tonic-gate 	const priv_set_t *psp;
2440Sstevel@tonic-gate 
2456559Sstephh 	if (!fmd.d_booted) {
2466559Sstephh 		(void) pthread_mutex_lock(&fmd.d_fmd_lock);
2476559Sstephh 		while (!fmd.d_booted)
2486559Sstephh 			(void) pthread_cond_wait(&fmd.d_fmd_cv,
2496559Sstephh 			    &fmd.d_fmd_lock);
2506559Sstephh 		(void) pthread_mutex_unlock(&fmd.d_fmd_lock);
2516559Sstephh 	}
2526559Sstephh 
2530Sstevel@tonic-gate 	if (svc_getcallerucred(rqp->rq_xprt, &ucp) != 0 ||
2540Sstevel@tonic-gate 	    (psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) == NULL)
2550Sstevel@tonic-gate 		return (1); /* deny access if we can't get credentials */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate #ifndef DEBUG
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * For convenience of testing, we only require all privileges for a
2600Sstevel@tonic-gate 	 * module load when running a non-DEBUG fault management daemon.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (rqp->rq_proc == FMD_ADM_MODLOAD)
2630Sstevel@tonic-gate 		return (!priv_isfullset(psp));
2640Sstevel@tonic-gate #endif
265*12967Sgavin.maltby@oracle.com 	return (!priv_ismember(psp, PRIV_SYS_ADMIN));
2660Sstevel@tonic-gate }
267