xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_rpc.c (revision d9638e547d8811f2c689977f8dd2a353938b61fd)
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  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <netdir.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <alloca.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <ucred.h>
387c478bd9Sstevel@tonic-gate #include <priv.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <fmd_rpc_api.h>
417c478bd9Sstevel@tonic-gate #include <fmd_rpc_adm.h>
427c478bd9Sstevel@tonic-gate #include <rpc/svc_mt.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
457c478bd9Sstevel@tonic-gate #include <fmd_error.h>
467c478bd9Sstevel@tonic-gate #include <fmd_thread.h>
477c478bd9Sstevel@tonic-gate #include <fmd_conf.h>
487c478bd9Sstevel@tonic-gate #include <fmd_api.h>
497c478bd9Sstevel@tonic-gate #include <fmd.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Define range of transient RPC program numbers to use for transient bindings.
537c478bd9Sstevel@tonic-gate  * These are defined in the Solaris ONC+ Developer's Guide, Appendix B, but
547c478bd9Sstevel@tonic-gate  * are cleverly not defined in any ONC+ standard system header file.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #define	RPC_TRANS_MIN	0x40000000
577c478bd9Sstevel@tonic-gate #define	RPC_TRANS_MAX	0x5fffffff
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * We use our own private version of svc_create() which registers our services
617c478bd9Sstevel@tonic-gate  * only on loopback transports and enables an option whereby Solaris ucreds
627c478bd9Sstevel@tonic-gate  * are associated with each connection, permitting us to check privilege bits.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static int
657c478bd9Sstevel@tonic-gate fmd_rpc_svc_create_local(void (*disp)(struct svc_req *, SVCXPRT *),
667c478bd9Sstevel@tonic-gate     rpcprog_t prog, rpcvers_t vers, uint_t ssz, uint_t rsz, int force)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	struct netconfig *ncp;
697c478bd9Sstevel@tonic-gate 	struct netbuf buf;
707c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
717c478bd9Sstevel@tonic-gate 	void *hdl;
727c478bd9Sstevel@tonic-gate 	int fd, n = 0;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	char door[PATH_MAX];
757c478bd9Sstevel@tonic-gate 	time_t tm;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if ((hdl = setnetconfig()) == NULL) {
787c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_RPC_REG, "failed to iterate over "
797c478bd9Sstevel@tonic-gate 		    "netconfig database: %s\n", nc_sperror());
807c478bd9Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_RPC_REG));
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (force)
847c478bd9Sstevel@tonic-gate 		svc_unreg(prog, vers); /* clear stale rpcbind registrations */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	buf.buf = alloca(_SS_MAXSIZE);
877c478bd9Sstevel@tonic-gate 	buf.maxlen = _SS_MAXSIZE;
887c478bd9Sstevel@tonic-gate 	buf.len = 0;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	while ((ncp = getnetconfig(hdl)) != NULL) {
917c478bd9Sstevel@tonic-gate 		if (strcmp(ncp->nc_protofmly, NC_LOOPBACK) != 0)
927c478bd9Sstevel@tonic-gate 			continue;
937c478bd9Sstevel@tonic-gate 
94*d9638e54Smws 		if (!force && rpcb_getaddr(prog, vers, ncp, &buf, HOST_SELF)) {
95*d9638e54Smws 			(void) endnetconfig(hdl);
967c478bd9Sstevel@tonic-gate 			return (fmd_set_errno(EFMD_RPC_BOUND));
97*d9638e54Smws 		}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		if ((fd = t_open(ncp->nc_device, O_RDWR, NULL)) == -1) {
1007c478bd9Sstevel@tonic-gate 			fmd_error(EFMD_RPC_REG, "failed to open %s: %s\n",
1017c478bd9Sstevel@tonic-gate 			    ncp->nc_device, t_strerror(t_errno));
1027c478bd9Sstevel@tonic-gate 			continue;
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		svc_fd_negotiate_ucred(fd); /* enable ucred option on xprt */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		if ((xprt = svc_tli_create(fd, ncp, NULL, ssz, rsz)) == NULL) {
1087c478bd9Sstevel@tonic-gate 			(void) t_close(fd);
1097c478bd9Sstevel@tonic-gate 			continue;
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		if (svc_reg(xprt, prog, vers, disp, ncp) == FALSE) {
1137c478bd9Sstevel@tonic-gate 			fmd_error(EFMD_RPC_REG, "failed to register "
1147c478bd9Sstevel@tonic-gate 			    "rpc service on %s\n", ncp->nc_netid);
1157c478bd9Sstevel@tonic-gate 			svc_destroy(xprt);
1167c478bd9Sstevel@tonic-gate 			continue;
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		n++;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) endnetconfig(hdl);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * If we failed to register services (n == 0) because rpcbind is down,
1267c478bd9Sstevel@tonic-gate 	 * then check to see if the RPC door file exists before attempting an
1277c478bd9Sstevel@tonic-gate 	 * svc_door_create(), which cleverly destroys any existing door file.
1287c478bd9Sstevel@tonic-gate 	 * The RPC APIs have no stable errnos, so we use rpcb_gettime() as a
1297c478bd9Sstevel@tonic-gate 	 * hack to determine if rpcbind itself is down.
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 	if (!force && n == 0 && rpcb_gettime(HOST_SELF, &tm) == FALSE &&
1327c478bd9Sstevel@tonic-gate 	    snprintf(door, sizeof (door), RPC_DOOR_RENDEZVOUS,
1337c478bd9Sstevel@tonic-gate 	    prog, vers) > 0 && access(door, F_OK) == 0)
1347c478bd9Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_RPC_BOUND));
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
137*d9638e54Smws 	 * Attempt to create a door server for the RPC program as well.  Limit
138*d9638e54Smws 	 * the maximum request size for the door transport to the receive size.
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	if ((xprt = svc_door_create(disp, prog, vers, ssz)) == NULL) {
1417c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_RPC_REG, "failed to create door for "
1427c478bd9Sstevel@tonic-gate 		    "rpc service 0x%lx/0x%lx\n", prog, vers);
1437c478bd9Sstevel@tonic-gate 	} else {
1447c478bd9Sstevel@tonic-gate 		(void) svc_control(xprt, SVCSET_CONNMAXREC, &rsz);
1457c478bd9Sstevel@tonic-gate 		n++;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	return (n);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static int
1527c478bd9Sstevel@tonic-gate fmd_rpc_svc_init(void (*disp)(struct svc_req *, SVCXPRT *),
1537c478bd9Sstevel@tonic-gate     const char *name, const char *path, const char *prop,
1547c478bd9Sstevel@tonic-gate     rpcprog_t pmin, rpcprog_t pmax, rpcvers_t vers,
1557c478bd9Sstevel@tonic-gate     uint_t sndsize, uint_t rcvsize, int force)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	rpcprog_t prog;
1587c478bd9Sstevel@tonic-gate 	char buf[16];
1597c478bd9Sstevel@tonic-gate 	FILE *fp;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	for (prog = pmin; prog <= pmax; prog++) {
1627c478bd9Sstevel@tonic-gate 		if (fmd_rpc_svc_create_local(disp, prog, vers,
1637c478bd9Sstevel@tonic-gate 		    sndsize, rcvsize, force) > 0) {
1647c478bd9Sstevel@tonic-gate 			fmd_dprintf(FMD_DBG_RPC, "registered %s rpc service "
1657c478bd9Sstevel@tonic-gate 			    "as 0x%lx.%lx\n", name, prog, vers);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 			/*
1687c478bd9Sstevel@tonic-gate 			 * To aid simulator scripts, save our RPC "digits" in
1697c478bd9Sstevel@tonic-gate 			 * the specified file for rendezvous with libfmd_adm.
1707c478bd9Sstevel@tonic-gate 			 */
1717c478bd9Sstevel@tonic-gate 			if (path != NULL && (fp = fopen(path, "w")) != NULL) {
1727c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "%ld\n", prog);
1737c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%ld", prog);
1777c478bd9Sstevel@tonic-gate 			(void) fmd_conf_setprop(fmd.d_conf, prop, buf);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 			return (0);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (-1); /* errno is set for us */
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate void
1877c478bd9Sstevel@tonic-gate fmd_rpc_init(void)
1887c478bd9Sstevel@tonic-gate {
189*d9638e54Smws 	int err, prog, mode = RPC_SVC_MT_USER;
1907c478bd9Sstevel@tonic-gate 	uint64_t sndsize = 0, rcvsize = 0;
191*d9638e54Smws 	const char *s;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (rpc_control(RPC_SVC_MTMODE_SET, &mode) == FALSE)
1947c478bd9Sstevel@tonic-gate 		fmd_panic("failed to enable user-MT rpc mode");
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.sndsize", &sndsize);
1977c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.rcvsize", &rcvsize);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * Infer whether we are the "default" fault manager or an alternate one
201*d9638e54Smws 	 * based on whether the initial setting of rpc.adm.prog is non-zero.
2027c478bd9Sstevel@tonic-gate 	 */
203*d9638e54Smws 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
2047c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.path", &s);
2057c478bd9Sstevel@tonic-gate 
206*d9638e54Smws 	if (prog != 0) {
2077c478bd9Sstevel@tonic-gate 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
2087c478bd9Sstevel@tonic-gate 		    FMD_ADM, FMD_ADM, FMD_ADM_VERSION_1,
2097c478bd9Sstevel@tonic-gate 		    (uint_t)sndsize, (uint_t)rcvsize, TRUE);
2107c478bd9Sstevel@tonic-gate 	} else {
2117c478bd9Sstevel@tonic-gate 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
2127c478bd9Sstevel@tonic-gate 		    RPC_TRANS_MIN, RPC_TRANS_MAX, FMD_ADM_VERSION_1,
2137c478bd9Sstevel@tonic-gate 		    (uint_t)sndsize, (uint_t)rcvsize, FALSE);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (err != 0)
2177c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create rpc server bindings");
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (fmd_thread_create(fmd.d_rmod, (fmd_thread_f *)svc_run, 0) == NULL)
2207c478bd9Sstevel@tonic-gate 		fmd_error(EFMD_EXIT, "failed to create rpc server thread");
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate void
2247c478bd9Sstevel@tonic-gate fmd_rpc_fini(void)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	rpcprog_t prog;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	svc_exit(); /* force svc_run() threads to exit */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
2317c478bd9Sstevel@tonic-gate 	svc_unreg(prog, FMD_ADM_VERSION_1);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.api.prog", &prog);
2347c478bd9Sstevel@tonic-gate 	svc_unreg(prog, FMD_API_VERSION_1);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * Utillity function to fetch the XPRT's ucred and determine if we should deny
2397c478bd9Sstevel@tonic-gate  * the request.  For now, we implement a simple policy of rejecting any caller
2407c478bd9Sstevel@tonic-gate  * who does not have the PRIV_SYS_CONFIG bit in their Effective privilege set,
2417c478bd9Sstevel@tonic-gate  * unless the caller is loading a module, which requires all privileges.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate int
2447c478bd9Sstevel@tonic-gate fmd_rpc_deny(struct svc_req *rqp)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	ucred_t *ucp = alloca(ucred_size());
2477c478bd9Sstevel@tonic-gate 	const priv_set_t *psp;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (svc_getcallerucred(rqp->rq_xprt, &ucp) != 0 ||
2507c478bd9Sstevel@tonic-gate 	    (psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) == NULL)
2517c478bd9Sstevel@tonic-gate 		return (1); /* deny access if we can't get credentials */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate #ifndef DEBUG
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * For convenience of testing, we only require all privileges for a
2567c478bd9Sstevel@tonic-gate 	 * module load when running a non-DEBUG fault management daemon.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	if (rqp->rq_proc == FMD_ADM_MODLOAD)
2597c478bd9Sstevel@tonic-gate 		return (!priv_isfullset(psp));
2607c478bd9Sstevel@tonic-gate #endif
2617c478bd9Sstevel@tonic-gate 	return (!priv_ismember(psp, PRIV_SYS_CONFIG));
2627c478bd9Sstevel@tonic-gate }
263