xref: /titanic_41/usr/src/cmd/fm/fmd/common/fmd_rpc.c (revision 99ebb4ca412cb0a19d77a3899a87c055b9c30fa8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/fm/util.h>
31 
32 #include <netdir.h>
33 #include <strings.h>
34 #include <alloca.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <ucred.h>
38 #include <priv.h>
39 
40 #include <fmd_rpc_api.h>
41 #include <fmd_rpc_adm.h>
42 #include <rpc/svc_mt.h>
43 
44 #include <fmd_subr.h>
45 #include <fmd_error.h>
46 #include <fmd_thread.h>
47 #include <fmd_conf.h>
48 #include <fmd_api.h>
49 #include <fmd.h>
50 
51 /*
52  * Define range of transient RPC program numbers to use for transient bindings.
53  * These are defined in the Solaris ONC+ Developer's Guide, Appendix B, but
54  * are cleverly not defined in any ONC+ standard system header file.
55  */
56 #define	RPC_TRANS_MIN	0x40000000
57 #define	RPC_TRANS_MAX	0x5fffffff
58 
59 /*
60  * We use our own private version of svc_create() which registers our services
61  * only on loopback transports and enables an option whereby Solaris ucreds
62  * are associated with each connection, permitting us to check privilege bits.
63  */
64 static int
65 fmd_rpc_svc_create_local(void (*disp)(struct svc_req *, SVCXPRT *),
66     rpcprog_t prog, rpcvers_t vers, uint_t ssz, uint_t rsz, int force)
67 {
68 	struct netconfig *ncp;
69 	struct netbuf buf;
70 	SVCXPRT *xprt;
71 	void *hdl;
72 	int fd, n = 0;
73 
74 	char door[PATH_MAX];
75 	time_t tm;
76 
77 	if ((hdl = setnetconfig()) == NULL) {
78 		fmd_error(EFMD_RPC_REG, "failed to iterate over "
79 		    "netconfig database: %s\n", nc_sperror());
80 		return (fmd_set_errno(EFMD_RPC_REG));
81 	}
82 
83 	if (force)
84 		svc_unreg(prog, vers); /* clear stale rpcbind registrations */
85 
86 	buf.buf = alloca(_SS_MAXSIZE);
87 	buf.maxlen = _SS_MAXSIZE;
88 	buf.len = 0;
89 
90 	while ((ncp = getnetconfig(hdl)) != NULL) {
91 		if (strcmp(ncp->nc_protofmly, NC_LOOPBACK) != 0)
92 			continue;
93 
94 		if (!force && rpcb_getaddr(prog, vers, ncp, &buf, HOST_SELF)) {
95 			(void) endnetconfig(hdl);
96 			return (fmd_set_errno(EFMD_RPC_BOUND));
97 		}
98 
99 		if ((fd = t_open(ncp->nc_device, O_RDWR, NULL)) == -1) {
100 			fmd_error(EFMD_RPC_REG, "failed to open %s: %s\n",
101 			    ncp->nc_device, t_strerror(t_errno));
102 			continue;
103 		}
104 
105 		svc_fd_negotiate_ucred(fd); /* enable ucred option on xprt */
106 
107 		if ((xprt = svc_tli_create(fd, ncp, NULL, ssz, rsz)) == NULL) {
108 			(void) t_close(fd);
109 			continue;
110 		}
111 
112 		if (svc_reg(xprt, prog, vers, disp, ncp) == FALSE) {
113 			fmd_error(EFMD_RPC_REG, "failed to register "
114 			    "rpc service on %s\n", ncp->nc_netid);
115 			svc_destroy(xprt);
116 			continue;
117 		}
118 
119 		n++;
120 	}
121 
122 	(void) endnetconfig(hdl);
123 
124 	/*
125 	 * If we failed to register services (n == 0) because rpcbind is down,
126 	 * then check to see if the RPC door file exists before attempting an
127 	 * svc_door_create(), which cleverly destroys any existing door file.
128 	 * The RPC APIs have no stable errnos, so we use rpcb_gettime() as a
129 	 * hack to determine if rpcbind itself is down.
130 	 */
131 	if (!force && n == 0 && rpcb_gettime(HOST_SELF, &tm) == FALSE &&
132 	    snprintf(door, sizeof (door), RPC_DOOR_RENDEZVOUS,
133 	    prog, vers) > 0 && access(door, F_OK) == 0)
134 		return (fmd_set_errno(EFMD_RPC_BOUND));
135 
136 	/*
137 	 * Attempt to create a door server for the RPC program as well.  Limit
138 	 * the maximum request size for the door transport to the receive size.
139 	 */
140 	if ((xprt = svc_door_create(disp, prog, vers, ssz)) == NULL) {
141 		fmd_error(EFMD_RPC_REG, "failed to create door for "
142 		    "rpc service 0x%lx/0x%lx\n", prog, vers);
143 	} else {
144 		(void) svc_control(xprt, SVCSET_CONNMAXREC, &rsz);
145 		n++;
146 	}
147 
148 	return (n);
149 }
150 
151 static int
152 fmd_rpc_svc_init(void (*disp)(struct svc_req *, SVCXPRT *),
153     const char *name, const char *path, const char *prop,
154     rpcprog_t pmin, rpcprog_t pmax, rpcvers_t vers,
155     uint_t sndsize, uint_t rcvsize, int force)
156 {
157 	rpcprog_t prog;
158 	char buf[16];
159 	FILE *fp;
160 
161 	for (prog = pmin; prog <= pmax; prog++) {
162 		if (fmd_rpc_svc_create_local(disp, prog, vers,
163 		    sndsize, rcvsize, force) > 0) {
164 			fmd_dprintf(FMD_DBG_RPC, "registered %s rpc service "
165 			    "as 0x%lx.%lx\n", name, prog, vers);
166 
167 			/*
168 			 * To aid simulator scripts, save our RPC "digits" in
169 			 * the specified file for rendezvous with libfmd_adm.
170 			 */
171 			if (path != NULL && (fp = fopen(path, "w")) != NULL) {
172 				(void) fprintf(fp, "%ld\n", prog);
173 				(void) fclose(fp);
174 			}
175 
176 			(void) snprintf(buf, sizeof (buf), "%ld", prog);
177 			(void) fmd_conf_setprop(fmd.d_conf, prop, buf);
178 
179 			return (0);
180 		}
181 	}
182 
183 	return (-1); /* errno is set for us */
184 }
185 
186 void
187 fmd_rpc_init(void)
188 {
189 	int err, prog, mode = RPC_SVC_MT_USER;
190 	uint64_t sndsize = 0, rcvsize = 0;
191 	const char *s;
192 
193 	if (rpc_control(RPC_SVC_MTMODE_SET, &mode) == FALSE)
194 		fmd_panic("failed to enable user-MT rpc mode");
195 
196 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.sndsize", &sndsize);
197 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.rcvsize", &rcvsize);
198 
199 	/*
200 	 * Infer whether we are the "default" fault manager or an alternate one
201 	 * based on whether the initial setting of rpc.adm.prog is non-zero.
202 	 */
203 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
204 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.path", &s);
205 
206 	if (prog != 0) {
207 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
208 		    FMD_ADM, FMD_ADM, FMD_ADM_VERSION_1,
209 		    (uint_t)sndsize, (uint_t)rcvsize, TRUE);
210 	} else {
211 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
212 		    RPC_TRANS_MIN, RPC_TRANS_MAX, FMD_ADM_VERSION_1,
213 		    (uint_t)sndsize, (uint_t)rcvsize, FALSE);
214 	}
215 
216 	if (err != 0)
217 		fmd_error(EFMD_EXIT, "failed to create rpc server bindings");
218 
219 	if (fmd_thread_create(fmd.d_rmod, (fmd_thread_f *)svc_run, 0) == NULL)
220 		fmd_error(EFMD_EXIT, "failed to create rpc server thread");
221 }
222 
223 void
224 fmd_rpc_fini(void)
225 {
226 	rpcprog_t prog;
227 
228 	svc_exit(); /* force svc_run() threads to exit */
229 
230 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
231 	svc_unreg(prog, FMD_ADM_VERSION_1);
232 
233 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.api.prog", &prog);
234 	svc_unreg(prog, FMD_API_VERSION_1);
235 }
236 
237 /*
238  * Utillity function to fetch the XPRT's ucred and determine if we should deny
239  * the request.  For now, we implement a simple policy of rejecting any caller
240  * who does not have the PRIV_SYS_CONFIG bit in their Effective privilege set,
241  * unless the caller is loading a module, which requires all privileges.
242  */
243 int
244 fmd_rpc_deny(struct svc_req *rqp)
245 {
246 	ucred_t *ucp = alloca(ucred_size());
247 	const priv_set_t *psp;
248 
249 	if (svc_getcallerucred(rqp->rq_xprt, &ucp) != 0 ||
250 	    (psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) == NULL)
251 		return (1); /* deny access if we can't get credentials */
252 
253 #ifndef DEBUG
254 	/*
255 	 * For convenience of testing, we only require all privileges for a
256 	 * module load when running a non-DEBUG fault management daemon.
257 	 */
258 	if (rqp->rq_proc == FMD_ADM_MODLOAD)
259 		return (!priv_isfullset(psp));
260 #endif
261 	return (!priv_ismember(psp, PRIV_SYS_CONFIG));
262 }
263