summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rpcbind/rpcb_svc_4.c
blob: 3fc73fb102f59b089fcadcada1633895338b5619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * rpcb_svc_4.c
 * The server procedure for the version 4 rpcbind.
 *
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <netconfig.h>
#include <syslog.h>
#include <netdir.h>
#include <string.h>
#include <stdlib.h>
#include "rpcbind.h"

static void free_rpcb_entry_list(rpcb_entry_list_ptr);
static bool_t xdr_rpcb_entry_list_ptr_wrap(XDR *, rpcb_entry_list_ptr *);
static bool_t rpcbproc_getaddrlist(rpcb *, rpcb_entry_list_ptr *,
    struct svc_req *);

/*
 * Called by svc_getreqset. There is a separate server handle for
 * every transport that it waits on.
 */
void
rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp)
{
	union {
		rpcb rpcbproc_set_4_arg;
		rpcb rpcbproc_unset_4_arg;
		rpcb rpcbproc_getaddr_4_arg;
		char *rpcbproc_uaddr2taddr_4_arg;
		struct netbuf rpcbproc_taddr2uaddr_4_arg;
		rpcb rpcbproc_getversaddr_4_arg;
		rpcb rpcbproc_getaddrlist_4_arg;
	} argument;
	union {
		bool_t rpcbproc_set_4_res;
		bool_t rpcbproc_unset_4_res;
		char *rpcbproc_getaddr_4_res;
		rpcblist_ptr *rpcbproc_dump_4_res;
		ulong_t rpcbproc_gettime_4_res;
		struct netbuf rpcbproc_uaddr2taddr_4_res;
		char *rpcbproc_taddr2uaddr_4_res;
		char *rpcbproc_getversaddr_4_res;
		rpcb_entry_list_ptr rpcbproc_getaddrlist_4_res;
		rpcb_stat_byvers *rpcbproc_getstat_4_res;
	} result;
	bool_t retval;
	xdrproc_t xdr_argument, xdr_result;
	bool_t (*local)();

	rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc);

	RPCB_CHECK(transp, rqstp->rq_proc);

	switch (rqstp->rq_proc) {
	case NULLPROC:
		/*
		 * Null proc call
		 */
		(void) svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
		return;

	case RPCBPROC_SET:
		/*
		 * Check to see whether the message came from
		 * loopback transports (for security reasons)
		 */
		if (strcasecmp(transp->xp_netid, loopback_dg) &&
		    strcasecmp(transp->xp_netid, loopback_vc) &&
		    strcasecmp(transp->xp_netid, loopback_vc_ord)) {
			syslog(LOG_ERR, "non-local attempt to set");
			svcerr_weakauth(transp);
			return;
		}
		xdr_argument = xdr_rpcb;
		xdr_result = xdr_bool;
		local = (bool_t (*)()) rpcbproc_set_com;
		break;

	case RPCBPROC_UNSET:
		/*
		 * Check to see whether the message came from
		 * loopback transports (for security reasons)
		 */
		if (strcasecmp(transp->xp_netid, loopback_dg) &&
		    strcasecmp(transp->xp_netid, loopback_vc) &&
		    strcasecmp(transp->xp_netid, loopback_vc_ord)) {
			syslog(LOG_ERR, "non-local attempt to unset");
			svcerr_weakauth(transp);
			return;
		}
		xdr_argument = xdr_rpcb;
		xdr_result = xdr_bool;
		local = (bool_t (*)()) rpcbproc_unset_com;
		break;

	case RPCBPROC_GETADDR:
		xdr_argument = xdr_rpcb;
		xdr_result = xdr_wrapstring;
		local = (bool_t (*)()) rpcbproc_getaddr_com;
		break;

	case RPCBPROC_DUMP:
		xdr_argument = xdr_void;
		xdr_result = xdr_rpcblist_ptr_ptr;
		local = (bool_t (*)()) rpcbproc_dump_com;
		break;

	case RPCBPROC_BCAST:
		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
		return;

	case RPCBPROC_GETTIME:
		xdr_argument = xdr_void;
		xdr_result = xdr_u_long;
		local = (bool_t (*)()) rpcbproc_gettime_com;
		break;

	case RPCBPROC_UADDR2TADDR:
		xdr_argument = xdr_wrapstring;
		xdr_result = xdr_netbuf;
		local = (bool_t (*)()) rpcbproc_uaddr2taddr_com;
		break;

	case RPCBPROC_TADDR2UADDR:
		xdr_argument = xdr_netbuf;
		xdr_result = xdr_wrapstring;
		local = (bool_t (*)()) rpcbproc_taddr2uaddr_com;
		break;

	case RPCBPROC_GETVERSADDR:
		xdr_argument = xdr_rpcb;
		xdr_result = xdr_wrapstring;
		local = (bool_t (*)()) rpcbproc_getaddr_com;
		break;

	case RPCBPROC_INDIRECT:
		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
		return;

	case RPCBPROC_GETADDRLIST:
		xdr_argument = xdr_rpcb;
		xdr_result = xdr_rpcb_entry_list_ptr_wrap;
		local = (bool_t (*)()) rpcbproc_getaddrlist;
		break;

	case RPCBPROC_GETSTAT:
		xdr_argument = xdr_void;
		xdr_result = xdr_rpcb_stat_byvers_ptr;
		local = (bool_t (*)()) rpcbproc_getstat;
		break;

	default:
		svcerr_noproc(transp);
		return;
	}
	(void) memset((char *)&argument, 0, sizeof (argument));
	if (!svc_getargs(transp, xdr_argument, (char *)&argument)) {
		svcerr_decode(transp);
		if (debugging)
			(void) fprintf(stderr, "rpcbind: could not decode\n");
		return;
	}
	retval = (*local)(&argument, &result, rqstp, RPCBVERS4);
	if (retval > 0 && !svc_sendreply(transp, xdr_result, (char *)&result)) {
		svcerr_systemerr(transp);
		if (debugging) {
			(void) fprintf(stderr, "rpcbind: svc_sendreply\n");
			if (doabort) {
				rpcbind_abort();
			}
		}
	}
	if (!svc_freeargs(transp, xdr_argument, (char *)&argument)) {
		if (debugging) {
			(void) fprintf(stderr, "unable to free arguments\n");
			if (doabort) {
				rpcbind_abort();
			}
		}
	}

	xdr_free(xdr_result, (char *)&result);
}

/*
 * Lookup the mapping for a program, version and return the
 * addresses for all transports in the current transport family.
 * We return a merged address.
 */
static bool_t
rpcbproc_getaddrlist(rpcb *regp, rpcb_entry_list_ptr *result,
    struct svc_req *rqstp)
{
	rpcb_entry_list_ptr rlist = *result = NULL;
	rpcblist_ptr rbl, next, prev;
	rpcb_entry_list_ptr rp, tail = NULL;
	ulong_t prog, vers;
	rpcb_entry *a;
	struct netconfig *nconf;
	struct netconfig *reg_nconf;
	char *saddr, *maddr = NULL;
	struct netconfig *trans_conf;	/* transport netconfig */
	SVCXPRT *transp = rqstp->rq_xprt;

	/*
	 * Deal with a possible window during which we could return an IPv6
	 * address when the caller wanted IPv4.  See the comments in
	 * rpcbproc_getaddr_com() for more details.
	 */
	trans_conf = rpcbind_get_conf(transp->xp_netid);
	if (strcmp(trans_conf->nc_protofmly, NC_INET6) == 0) {
		struct sockaddr_in6 *rmtaddr;

		rmtaddr = (struct sockaddr_in6 *)transp->xp_rtaddr.buf;
		if (IN6_IS_ADDR_V4MAPPED(&rmtaddr->sin6_addr)) {
			syslog(LOG_DEBUG,
			    "IPv4 GETADDRLIST request mapped "
			    "to IPv6: ignoring");
			return (FALSE);
		}
	}

	prog = regp->r_prog;
	vers = regp->r_vers;
	reg_nconf = rpcbind_get_conf(transp->xp_netid);
	if (reg_nconf == NULL)
		return (FALSE);
	if (*(regp->r_addr) != '\0') {
		saddr = regp->r_addr;
	} else {
		saddr = NULL;
	}

	prev = NULL;
	(void) rw_wrlock(&list_rbl_lock);
	for (rbl = list_rbl; rbl != NULL; rbl = next) {
		next = rbl->rpcb_next;
		if ((rbl->rpcb_map.r_prog == prog) &&
		    (rbl->rpcb_map.r_vers == vers)) {
			nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid);
			if (nconf == NULL) {
				(void) rw_unlock(&list_rbl_lock);
				goto fail;
			}
			if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly)
			    != 0) {
				prev = rbl;
				continue;	/* not same proto family */
			}
			if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
			    rbl->rpcb_map.r_addr, saddr)) == NULL) {
				prev = rbl;
				continue;
			} else if (!maddr[0]) {
				/*
				 * The server died, remove this rpcb_map element
				 * from the list and free it.
				 */
#ifdef PORTMAP
				(void) rw_wrlock(&list_pml_lock);
				(void) del_pmaplist(&rbl->rpcb_map);
				(void) rw_unlock(&list_pml_lock);
#endif
				(void) delete_rbl(rbl);

				if (prev == NULL)
					list_rbl = next;
				else
					prev->rpcb_next = next;
				continue;
			}
			/*
			 * Add it to rlist.
			 */
			rp = (rpcb_entry_list_ptr)
			    malloc((uint_t)sizeof (rpcb_entry_list));
			if (rp == NULL) {
				(void) rw_unlock(&list_rbl_lock);
				goto fail;
			}
			a = &rp->rpcb_entry_map;
			a->r_maddr = maddr;
			a->r_nc_netid = nconf->nc_netid;
			a->r_nc_semantics = nconf->nc_semantics;
			a->r_nc_protofmly = nconf->nc_protofmly;
			a->r_nc_proto = nconf->nc_proto;
			rp->rpcb_entry_next = NULL;
			if (rlist == NULL) {
				rlist = rp;
				tail = rp;
			} else {
				tail->rpcb_entry_next = rp;
				tail = rp;
			}
			rp = NULL;
		}
		prev = rbl;
	}
	(void) rw_unlock(&list_rbl_lock);

	/*
	 * XXX: getaddrlist info is also being stuffed into getaddr.
	 * Perhaps wrong, but better than it not getting counted at all.
	 */
	rpcbs_getaddr(RPCBVERS_4_STAT, prog, vers, transp->xp_netid, maddr);

	*result = rlist;
	return (TRUE);

fail:
	free_rpcb_entry_list(rlist);
	return (FALSE);
}

/*
 * Free only the allocated structure, rest is all a pointer to some
 * other data somewhere else.
 */
static void
free_rpcb_entry_list(rpcb_entry_list_ptr rlist)
{
	while (rlist != NULL) {
		rpcb_entry_list_ptr tmp = rlist;
		rlist = rlist->rpcb_entry_next;
		free(tmp->rpcb_entry_map.r_maddr);
		free(tmp);
	}
}

static bool_t
xdr_rpcb_entry_list_ptr_wrap(XDR *xdrs, rpcb_entry_list_ptr *rp)
{
	if (xdrs->x_op == XDR_FREE) {
		free_rpcb_entry_list(*rp);
		return (TRUE);
	}

	return (xdr_rpcb_entry_list_ptr(xdrs, rp));
}