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