xref: /titanic_41/usr/src/cmd/fm/fmd/common/fmd_rpc.c (revision d58fda4376e4bf67072ce2e69f6f47036f9dbb68)
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 			return (fmd_set_errno(EFMD_RPC_BOUND));
96 
97 		if ((fd = t_open(ncp->nc_device, O_RDWR, NULL)) == -1) {
98 			fmd_error(EFMD_RPC_REG, "failed to open %s: %s\n",
99 			    ncp->nc_device, t_strerror(t_errno));
100 			continue;
101 		}
102 
103 		svc_fd_negotiate_ucred(fd); /* enable ucred option on xprt */
104 
105 		if ((xprt = svc_tli_create(fd, ncp, NULL, ssz, rsz)) == NULL) {
106 			(void) t_close(fd);
107 			continue;
108 		}
109 
110 		if (svc_reg(xprt, prog, vers, disp, ncp) == FALSE) {
111 			fmd_error(EFMD_RPC_REG, "failed to register "
112 			    "rpc service on %s\n", ncp->nc_netid);
113 			svc_destroy(xprt);
114 			continue;
115 		}
116 
117 		n++;
118 	}
119 
120 	(void) endnetconfig(hdl);
121 
122 	/*
123 	 * If we failed to register services (n == 0) because rpcbind is down,
124 	 * then check to see if the RPC door file exists before attempting an
125 	 * svc_door_create(), which cleverly destroys any existing door file.
126 	 * The RPC APIs have no stable errnos, so we use rpcb_gettime() as a
127 	 * hack to determine if rpcbind itself is down.
128 	 */
129 	if (!force && n == 0 && rpcb_gettime(HOST_SELF, &tm) == FALSE &&
130 	    snprintf(door, sizeof (door), RPC_DOOR_RENDEZVOUS,
131 	    prog, vers) > 0 && access(door, F_OK) == 0)
132 		return (fmd_set_errno(EFMD_RPC_BOUND));
133 
134 	/*
135 	 * Attempt to create a door server for the RPC program as well.  Door
136 	 * servers in fmd must be setup as fmd threads, and fmd_transport_init()
137 	 * already calls door_server_create() for us. We therefore rely on and
138 	 * assert that fmd_transport_init() is called before fmd_rpc_init().
139 	 */
140 	ASSERT(fmd.d_xprt_chan != NULL);
141 
142 	if ((xprt = svc_door_create(disp, prog, vers, ssz)) == NULL) {
143 		fmd_error(EFMD_RPC_REG, "failed to create door for "
144 		    "rpc service 0x%lx/0x%lx\n", prog, vers);
145 	} else {
146 		/* set the maximum request size for the door transport */
147 		(void) svc_control(xprt, SVCSET_CONNMAXREC, &rsz);
148 		n++;
149 	}
150 
151 	return (n);
152 }
153 
154 static int
155 fmd_rpc_svc_init(void (*disp)(struct svc_req *, SVCXPRT *),
156     const char *name, const char *path, const char *prop,
157     rpcprog_t pmin, rpcprog_t pmax, rpcvers_t vers,
158     uint_t sndsize, uint_t rcvsize, int force)
159 {
160 	rpcprog_t prog;
161 	char buf[16];
162 	FILE *fp;
163 
164 	for (prog = pmin; prog <= pmax; prog++) {
165 		if (fmd_rpc_svc_create_local(disp, prog, vers,
166 		    sndsize, rcvsize, force) > 0) {
167 			fmd_dprintf(FMD_DBG_RPC, "registered %s rpc service "
168 			    "as 0x%lx.%lx\n", name, prog, vers);
169 
170 			/*
171 			 * To aid simulator scripts, save our RPC "digits" in
172 			 * the specified file for rendezvous with libfmd_adm.
173 			 */
174 			if (path != NULL && (fp = fopen(path, "w")) != NULL) {
175 				(void) fprintf(fp, "%ld\n", prog);
176 				(void) fclose(fp);
177 			}
178 
179 			(void) snprintf(buf, sizeof (buf), "%ld", prog);
180 			(void) fmd_conf_setprop(fmd.d_conf, prop, buf);
181 
182 			return (0);
183 		}
184 	}
185 
186 	return (-1); /* errno is set for us */
187 }
188 
189 void
190 fmd_rpc_init(void)
191 {
192 	int err, mode = RPC_SVC_MT_USER;
193 	const char *errchan, *s;
194 	uint64_t sndsize = 0, rcvsize = 0;
195 
196 	if (rpc_control(RPC_SVC_MTMODE_SET, &mode) == FALSE)
197 		fmd_panic("failed to enable user-MT rpc mode");
198 
199 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.sndsize", &sndsize);
200 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.rcvsize", &rcvsize);
201 
202 	/*
203 	 * Infer whether we are the "default" fault manager or an alternate one
204 	 * based on whether we are subscribed to the default transport or not.
205 	 */
206 	(void) fmd_conf_getprop(fmd.d_conf, "errchan", &errchan);
207 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.path", &s);
208 
209 	if (errchan != NULL && strcmp(errchan, FM_ERROR_CHAN) == 0) {
210 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
211 		    FMD_ADM, FMD_ADM, FMD_ADM_VERSION_1,
212 		    (uint_t)sndsize, (uint_t)rcvsize, TRUE);
213 	} else {
214 		err = fmd_rpc_svc_init(fmd_adm_1, "FMD_ADM", s, "rpc.adm.prog",
215 		    RPC_TRANS_MIN, RPC_TRANS_MAX, FMD_ADM_VERSION_1,
216 		    (uint_t)sndsize, (uint_t)rcvsize, FALSE);
217 	}
218 
219 	if (err != 0)
220 		fmd_error(EFMD_EXIT, "failed to create rpc server bindings");
221 
222 	if (fmd_thread_create(fmd.d_rmod, (fmd_thread_f *)svc_run, 0) == NULL)
223 		fmd_error(EFMD_EXIT, "failed to create rpc server thread");
224 }
225 
226 void
227 fmd_rpc_fini(void)
228 {
229 	rpcprog_t prog;
230 
231 	svc_exit(); /* force svc_run() threads to exit */
232 
233 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.adm.prog", &prog);
234 	svc_unreg(prog, FMD_ADM_VERSION_1);
235 
236 	(void) fmd_conf_getprop(fmd.d_conf, "rpc.api.prog", &prog);
237 	svc_unreg(prog, FMD_API_VERSION_1);
238 }
239 
240 /*
241  * Utillity function to fetch the XPRT's ucred and determine if we should deny
242  * the request.  For now, we implement a simple policy of rejecting any caller
243  * who does not have the PRIV_SYS_CONFIG bit in their Effective privilege set,
244  * unless the caller is loading a module, which requires all privileges.
245  */
246 int
247 fmd_rpc_deny(struct svc_req *rqp)
248 {
249 	ucred_t *ucp = alloca(ucred_size());
250 	const priv_set_t *psp;
251 
252 	if (svc_getcallerucred(rqp->rq_xprt, &ucp) != 0 ||
253 	    (psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) == NULL)
254 		return (1); /* deny access if we can't get credentials */
255 
256 #ifndef DEBUG
257 	/*
258 	 * For convenience of testing, we only require all privileges for a
259 	 * module load when running a non-DEBUG fault management daemon.
260 	 */
261 	if (rqp->rq_proc == FMD_ADM_MODLOAD)
262 		return (!priv_isfullset(psp));
263 #endif
264 	return (!priv_ismember(psp, PRIV_SYS_CONFIG));
265 }
266