xref: /titanic_44/usr/src/cmd/rpcbind/rpcb_svc_4.c (revision 8f6d9dae92449b59bdafcb7777bc32f1b2726e48)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
27  */
28 
29 /*
30  * rpcb_svc_4.c
31  * The server procedure for the version 4 rpcbind.
32  *
33  */
34 
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <rpc/rpc.h>
40 #include <netconfig.h>
41 #include <syslog.h>
42 #include <netdir.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include "rpcbind.h"
46 
47 static void free_rpcb_entry_list(rpcb_entry_list_ptr);
48 static bool_t xdr_rpcb_entry_list_ptr_wrap(XDR *, rpcb_entry_list_ptr *);
49 static bool_t rpcbproc_getaddrlist(rpcb *, rpcb_entry_list_ptr *,
50     struct svc_req *);
51 
52 /*
53  * Called by svc_getreqset. There is a separate server handle for
54  * every transport that it waits on.
55  */
56 void
rpcb_service_4(struct svc_req * rqstp,SVCXPRT * transp)57 rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp)
58 {
59 	union {
60 		rpcb rpcbproc_set_4_arg;
61 		rpcb rpcbproc_unset_4_arg;
62 		rpcb rpcbproc_getaddr_4_arg;
63 		char *rpcbproc_uaddr2taddr_4_arg;
64 		struct netbuf rpcbproc_taddr2uaddr_4_arg;
65 		rpcb rpcbproc_getversaddr_4_arg;
66 		rpcb rpcbproc_getaddrlist_4_arg;
67 	} argument;
68 	union {
69 		bool_t rpcbproc_set_4_res;
70 		bool_t rpcbproc_unset_4_res;
71 		char *rpcbproc_getaddr_4_res;
72 		rpcblist_ptr *rpcbproc_dump_4_res;
73 		ulong_t rpcbproc_gettime_4_res;
74 		struct netbuf rpcbproc_uaddr2taddr_4_res;
75 		char *rpcbproc_taddr2uaddr_4_res;
76 		char *rpcbproc_getversaddr_4_res;
77 		rpcb_entry_list_ptr rpcbproc_getaddrlist_4_res;
78 		rpcb_stat_byvers *rpcbproc_getstat_4_res;
79 	} result;
80 	bool_t retval;
81 	xdrproc_t xdr_argument, xdr_result;
82 	bool_t (*local)();
83 
84 	rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc);
85 
86 	RPCB_CHECK(transp, rqstp->rq_proc);
87 
88 	switch (rqstp->rq_proc) {
89 	case NULLPROC:
90 		/*
91 		 * Null proc call
92 		 */
93 		(void) svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
94 		return;
95 
96 	case RPCBPROC_SET:
97 		/*
98 		 * Check to see whether the message came from
99 		 * loopback transports (for security reasons)
100 		 */
101 		if (strcasecmp(transp->xp_netid, loopback_dg) &&
102 		    strcasecmp(transp->xp_netid, loopback_vc) &&
103 		    strcasecmp(transp->xp_netid, loopback_vc_ord)) {
104 			syslog(LOG_ERR, "non-local attempt to set");
105 			svcerr_weakauth(transp);
106 			return;
107 		}
108 		xdr_argument = xdr_rpcb;
109 		xdr_result = xdr_bool;
110 		local = (bool_t (*)()) rpcbproc_set_com;
111 		break;
112 
113 	case RPCBPROC_UNSET:
114 		/*
115 		 * Check to see whether the message came from
116 		 * loopback transports (for security reasons)
117 		 */
118 		if (strcasecmp(transp->xp_netid, loopback_dg) &&
119 		    strcasecmp(transp->xp_netid, loopback_vc) &&
120 		    strcasecmp(transp->xp_netid, loopback_vc_ord)) {
121 			syslog(LOG_ERR, "non-local attempt to unset");
122 			svcerr_weakauth(transp);
123 			return;
124 		}
125 		xdr_argument = xdr_rpcb;
126 		xdr_result = xdr_bool;
127 		local = (bool_t (*)()) rpcbproc_unset_com;
128 		break;
129 
130 	case RPCBPROC_GETADDR:
131 		xdr_argument = xdr_rpcb;
132 		xdr_result = xdr_wrapstring;
133 		local = (bool_t (*)()) rpcbproc_getaddr_com;
134 		break;
135 
136 	case RPCBPROC_DUMP:
137 		xdr_argument = xdr_void;
138 		xdr_result = xdr_rpcblist_ptr_ptr;
139 		local = (bool_t (*)()) rpcbproc_dump_com;
140 		break;
141 
142 	case RPCBPROC_BCAST:
143 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
144 		return;
145 
146 	case RPCBPROC_GETTIME:
147 		xdr_argument = xdr_void;
148 		xdr_result = xdr_u_long;
149 		local = (bool_t (*)()) rpcbproc_gettime_com;
150 		break;
151 
152 	case RPCBPROC_UADDR2TADDR:
153 		xdr_argument = xdr_wrapstring;
154 		xdr_result = xdr_netbuf;
155 		local = (bool_t (*)()) rpcbproc_uaddr2taddr_com;
156 		break;
157 
158 	case RPCBPROC_TADDR2UADDR:
159 		xdr_argument = xdr_netbuf;
160 		xdr_result = xdr_wrapstring;
161 		local = (bool_t (*)()) rpcbproc_taddr2uaddr_com;
162 		break;
163 
164 	case RPCBPROC_GETVERSADDR:
165 		xdr_argument = xdr_rpcb;
166 		xdr_result = xdr_wrapstring;
167 		local = (bool_t (*)()) rpcbproc_getaddr_com;
168 		break;
169 
170 	case RPCBPROC_INDIRECT:
171 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
172 		return;
173 
174 	case RPCBPROC_GETADDRLIST:
175 		xdr_argument = xdr_rpcb;
176 		xdr_result = xdr_rpcb_entry_list_ptr_wrap;
177 		local = (bool_t (*)()) rpcbproc_getaddrlist;
178 		break;
179 
180 	case RPCBPROC_GETSTAT:
181 		xdr_argument = xdr_void;
182 		xdr_result = xdr_rpcb_stat_byvers_ptr;
183 		local = (bool_t (*)()) rpcbproc_getstat;
184 		break;
185 
186 	default:
187 		svcerr_noproc(transp);
188 		return;
189 	}
190 	(void) memset((char *)&argument, 0, sizeof (argument));
191 	if (!svc_getargs(transp, xdr_argument, (char *)&argument)) {
192 		svcerr_decode(transp);
193 		if (debugging)
194 			(void) fprintf(stderr, "rpcbind: could not decode\n");
195 		return;
196 	}
197 	retval = (*local)(&argument, &result, rqstp, RPCBVERS4);
198 	if (retval > 0 && !svc_sendreply(transp, xdr_result, (char *)&result)) {
199 		svcerr_systemerr(transp);
200 		if (debugging) {
201 			(void) fprintf(stderr, "rpcbind: svc_sendreply\n");
202 			if (doabort) {
203 				rpcbind_abort();
204 			}
205 		}
206 	}
207 	if (!svc_freeargs(transp, xdr_argument, (char *)&argument)) {
208 		if (debugging) {
209 			(void) fprintf(stderr, "unable to free arguments\n");
210 			if (doabort) {
211 				rpcbind_abort();
212 			}
213 		}
214 	}
215 
216 	xdr_free(xdr_result, (char *)&result);
217 }
218 
219 /*
220  * Lookup the mapping for a program, version and return the
221  * addresses for all transports in the current transport family.
222  * We return a merged address.
223  */
224 static bool_t
rpcbproc_getaddrlist(rpcb * regp,rpcb_entry_list_ptr * result,struct svc_req * rqstp)225 rpcbproc_getaddrlist(rpcb *regp, rpcb_entry_list_ptr *result,
226     struct svc_req *rqstp)
227 {
228 	rpcb_entry_list_ptr rlist = *result = NULL;
229 	rpcblist_ptr rbl, next, prev;
230 	rpcb_entry_list_ptr rp, tail = NULL;
231 	ulong_t prog, vers;
232 	rpcb_entry *a;
233 	struct netconfig *nconf;
234 	struct netconfig *reg_nconf;
235 	char *saddr, *maddr = NULL;
236 	struct netconfig *trans_conf;	/* transport netconfig */
237 	SVCXPRT *transp = rqstp->rq_xprt;
238 
239 	/*
240 	 * Deal with a possible window during which we could return an IPv6
241 	 * address when the caller wanted IPv4.  See the comments in
242 	 * rpcbproc_getaddr_com() for more details.
243 	 */
244 	trans_conf = rpcbind_get_conf(transp->xp_netid);
245 	if (strcmp(trans_conf->nc_protofmly, NC_INET6) == 0) {
246 		struct sockaddr_in6 *rmtaddr;
247 
248 		rmtaddr = (struct sockaddr_in6 *)transp->xp_rtaddr.buf;
249 		if (IN6_IS_ADDR_V4MAPPED(&rmtaddr->sin6_addr)) {
250 			syslog(LOG_DEBUG,
251 			    "IPv4 GETADDRLIST request mapped "
252 			    "to IPv6: ignoring");
253 			return (FALSE);
254 		}
255 	}
256 
257 	prog = regp->r_prog;
258 	vers = regp->r_vers;
259 	reg_nconf = rpcbind_get_conf(transp->xp_netid);
260 	if (reg_nconf == NULL)
261 		return (FALSE);
262 	if (*(regp->r_addr) != '\0') {
263 		saddr = regp->r_addr;
264 	} else {
265 		saddr = NULL;
266 	}
267 
268 	prev = NULL;
269 	(void) rw_wrlock(&list_rbl_lock);
270 	for (rbl = list_rbl; rbl != NULL; rbl = next) {
271 		next = rbl->rpcb_next;
272 		if ((rbl->rpcb_map.r_prog == prog) &&
273 		    (rbl->rpcb_map.r_vers == vers)) {
274 			nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid);
275 			if (nconf == NULL) {
276 				(void) rw_unlock(&list_rbl_lock);
277 				goto fail;
278 			}
279 			if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly)
280 			    != 0) {
281 				prev = rbl;
282 				continue;	/* not same proto family */
283 			}
284 			if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
285 			    rbl->rpcb_map.r_addr, saddr)) == NULL) {
286 				prev = rbl;
287 				continue;
288 			} else if (!maddr[0]) {
289 				/*
290 				 * The server died, remove this rpcb_map element
291 				 * from the list and free it.
292 				 */
293 #ifdef PORTMAP
294 				(void) rw_wrlock(&list_pml_lock);
295 				(void) del_pmaplist(&rbl->rpcb_map);
296 				(void) rw_unlock(&list_pml_lock);
297 #endif
298 				(void) delete_rbl(rbl);
299 
300 				if (prev == NULL)
301 					list_rbl = next;
302 				else
303 					prev->rpcb_next = next;
304 				continue;
305 			}
306 			/*
307 			 * Add it to rlist.
308 			 */
309 			rp = (rpcb_entry_list_ptr)
310 			    malloc((uint_t)sizeof (rpcb_entry_list));
311 			if (rp == NULL) {
312 				(void) rw_unlock(&list_rbl_lock);
313 				goto fail;
314 			}
315 			a = &rp->rpcb_entry_map;
316 			a->r_maddr = maddr;
317 			a->r_nc_netid = nconf->nc_netid;
318 			a->r_nc_semantics = nconf->nc_semantics;
319 			a->r_nc_protofmly = nconf->nc_protofmly;
320 			a->r_nc_proto = nconf->nc_proto;
321 			rp->rpcb_entry_next = NULL;
322 			if (rlist == NULL) {
323 				rlist = rp;
324 				tail = rp;
325 			} else {
326 				tail->rpcb_entry_next = rp;
327 				tail = rp;
328 			}
329 			rp = NULL;
330 		}
331 		prev = rbl;
332 	}
333 	(void) rw_unlock(&list_rbl_lock);
334 
335 	/*
336 	 * XXX: getaddrlist info is also being stuffed into getaddr.
337 	 * Perhaps wrong, but better than it not getting counted at all.
338 	 */
339 	rpcbs_getaddr(RPCBVERS_4_STAT, prog, vers, transp->xp_netid, maddr);
340 
341 	*result = rlist;
342 	return (TRUE);
343 
344 fail:
345 	free_rpcb_entry_list(rlist);
346 	return (FALSE);
347 }
348 
349 /*
350  * Free only the allocated structure, rest is all a pointer to some
351  * other data somewhere else.
352  */
353 static void
free_rpcb_entry_list(rpcb_entry_list_ptr rlist)354 free_rpcb_entry_list(rpcb_entry_list_ptr rlist)
355 {
356 	while (rlist != NULL) {
357 		rpcb_entry_list_ptr tmp = rlist;
358 		rlist = rlist->rpcb_entry_next;
359 		free(tmp->rpcb_entry_map.r_maddr);
360 		free(tmp);
361 	}
362 }
363 
364 static bool_t
xdr_rpcb_entry_list_ptr_wrap(XDR * xdrs,rpcb_entry_list_ptr * rp)365 xdr_rpcb_entry_list_ptr_wrap(XDR *xdrs, rpcb_entry_list_ptr *rp)
366 {
367 	if (xdrs->x_op == XDR_FREE) {
368 		free_rpcb_entry_list(*rp);
369 		return (TRUE);
370 	}
371 
372 	return (xdr_rpcb_entry_list_ptr(xdrs, rp));
373 }
374