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