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