xref: /netbsd-src/lib/libc/yp/xdryp.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: xdryp.c,v 1.30 2006/05/11 17:11:57 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Jason R. Thorpe <thorpej@NetBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project
18  *	by Jason R. Thorpe.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  *
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
50  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
53  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  */
61 
62 #include <sys/cdefs.h>
63 #if defined(LIBC_SCCS) && !defined(lint)
64 __RCSID("$NetBSD: xdryp.c,v 1.30 2006/05/11 17:11:57 mrg Exp $");
65 #endif
66 
67 /*
68  * XDR routines used by the YP protocol.  Note that these routines do
69  * not strictly conform to the RPC definition in yp.x.  This file
70  * replicates the functions exported by the Sun YP API; reality is
71  * often inaccurate.
72  */
73 
74 #include "namespace.h"
75 
76 #include <sys/param.h>
77 #include <sys/socket.h>
78 
79 #include <assert.h>
80 #include <ctype.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 
85 #include <rpc/rpc.h>
86 #include <rpc/xdr.h>
87 #include <rpcsvc/yp_prot.h>
88 #include <rpcsvc/ypclnt.h>
89 
90 #ifdef __weak_alias
91 __weak_alias(xdr_datum,_xdr_datum)
92 __weak_alias(xdr_domainname,_xdr_domainname)
93 __weak_alias(xdr_mapname,_xdr_mapname)
94 __weak_alias(xdr_peername,_xdr_peername)
95 __weak_alias(xdr_yp_inaddr,_xdr_yp_inaddr)
96 __weak_alias(xdr_ypall,_xdr_ypall)
97 __weak_alias(xdr_ypbind_resp,_xdr_ypbind_resp)
98 __weak_alias(xdr_ypbind_setdom,_xdr_ypbind_setdom)
99 __weak_alias(xdr_ypdomain_wrap_string,_xdr_ypdomain_wrap_string)
100 __weak_alias(xdr_ypmap_parms,_xdr_ypmap_parms)
101 __weak_alias(xdr_ypmap_wrap_string,_xdr_ypmap_wrap_string)
102 __weak_alias(xdr_ypmaplist,_xdr_ypmaplist)
103 __weak_alias(xdr_ypowner_wrap_string,_xdr_ypowner_wrap_string)
104 __weak_alias(xdr_yppushresp_xfr,_xdr_yppushresp_xfr)
105 __weak_alias(xdr_ypreq_key,_xdr_ypreq_key)
106 __weak_alias(xdr_ypreq_nokey,_xdr_ypreq_nokey)
107 __weak_alias(xdr_ypreq_xfr,_xdr_ypreq_xfr)
108 __weak_alias(xdr_ypresp_key_val,_xdr_ypresp_key_val)
109 __weak_alias(xdr_ypresp_maplist,_xdr_ypresp_maplist)
110 __weak_alias(xdr_ypresp_master,_xdr_ypresp_master)
111 __weak_alias(xdr_ypresp_order,_xdr_ypresp_order)
112 __weak_alias(xdr_ypresp_val,_xdr_ypresp_val)
113 #endif
114 
115 /*
116  * Functions used only within this file.
117  */
118 static	bool_t xdr_ypbind_binding __P((XDR *, struct ypbind_binding *));
119 static	bool_t xdr_ypbind_resptype __P((XDR *, enum ypbind_resptype *));
120 static	bool_t xdr_ypstat __P((XDR *, enum ypbind_resptype *));
121 static	bool_t xdr_ypmaplist_str __P((XDR *, char *));
122 
123 __warn_references(xdr_domainname,
124     "warning: this program uses xdr_domainname(), which is deprecated and buggy.")
125 
126 bool_t
127 xdr_domainname(xdrs, objp)
128 	XDR *xdrs;
129 	char *objp;
130 {
131 
132 	_DIAGASSERT(xdrs != NULL);
133 	_DIAGASSERT(objp != NULL);
134 
135 	return xdr_string(xdrs, &objp, YPMAXDOMAIN);
136 }
137 
138 __warn_references(xdr_peername,
139     "warning: this program uses xdr_peername(), which is deprecated and buggy.")
140 
141 bool_t
142 xdr_peername(xdrs, objp)
143 	XDR *xdrs;
144 	char *objp;
145 {
146 
147 	_DIAGASSERT(xdrs != NULL);
148 	_DIAGASSERT(objp != NULL);
149 
150 	return xdr_string(xdrs, &objp, YPMAXPEER);
151 }
152 
153 __warn_references(xdr_mapname,
154     "warning: this program uses xdr_mapname(), which is deprecated and buggy.")
155 
156 bool_t
157 xdr_mapname(xdrs, objp)
158 	XDR *xdrs;
159 	char *objp;
160 {
161 
162 	_DIAGASSERT(xdrs != NULL);
163 	_DIAGASSERT(objp != NULL);
164 
165 	return xdr_string(xdrs, &objp, YPMAXMAP);
166 }
167 
168 bool_t
169 xdr_ypdomain_wrap_string(xdrs, objp)
170 	XDR *xdrs;
171 	char **objp;
172 {
173 
174 	_DIAGASSERT(xdrs != NULL);
175 	_DIAGASSERT(objp != NULL);
176 
177 	return xdr_string(xdrs, objp, YPMAXDOMAIN);
178 }
179 
180 bool_t
181 xdr_ypmap_wrap_string(xdrs, objp)
182 	XDR *xdrs;
183 	char **objp;
184 {
185 
186 	_DIAGASSERT(xdrs != NULL);
187 	_DIAGASSERT(objp != NULL);
188 
189 	return xdr_string(xdrs, objp, YPMAXMAP);
190 }
191 
192 bool_t
193 xdr_ypowner_wrap_string(xdrs, objp)
194 	XDR *xdrs;
195 	char **objp;
196 {
197 
198 	_DIAGASSERT(xdrs != NULL);
199 	_DIAGASSERT(objp != NULL);
200 
201 	return xdr_string(xdrs, objp, YPMAXPEER);
202 }
203 
204 bool_t
205 xdr_datum(xdrs, objp)
206 	XDR *xdrs;
207 	datum *objp;
208 {
209 
210 	_DIAGASSERT(xdrs != NULL);
211 	_DIAGASSERT(objp != NULL);
212 
213 	return xdr_bytes(xdrs, __UNCONST(&objp->dptr),
214 	    (u_int *)&objp->dsize, YPMAXRECORD);
215 }
216 
217 bool_t
218 xdr_ypreq_key(xdrs, objp)
219 	XDR *xdrs;
220 	struct ypreq_key *objp;
221 {
222 
223 	_DIAGASSERT(xdrs != NULL);
224 	_DIAGASSERT(objp != NULL);
225 
226 	if (!xdr_ypdomain_wrap_string(xdrs, __UNCONST(&objp->domain)))
227 		return FALSE;
228 
229 	if (!xdr_ypmap_wrap_string(xdrs, __UNCONST(&objp->map)))
230 		return FALSE;
231 
232 	if (!xdr_datum(xdrs, &objp->keydat))
233 		return FALSE;
234 
235 	return TRUE;
236 }
237 
238 bool_t
239 xdr_ypreq_nokey(xdrs, objp)
240 	XDR *xdrs;
241 	struct ypreq_nokey *objp;
242 {
243 
244 	_DIAGASSERT(xdrs != NULL);
245 	_DIAGASSERT(objp != NULL);
246 
247 	if (!xdr_ypdomain_wrap_string(xdrs, __UNCONST(&objp->domain)))
248 		return FALSE;
249 
250 	if (!xdr_ypmap_wrap_string(xdrs, __UNCONST(&objp->map)))
251 		return FALSE;
252 
253 	return TRUE;
254 }
255 
256 bool_t
257 xdr_yp_inaddr(xdrs, objp)
258 	XDR *xdrs;
259 	struct in_addr *objp;
260 {
261 
262 	_DIAGASSERT(xdrs != NULL);
263 	_DIAGASSERT(objp != NULL);
264 
265 	return xdr_opaque(xdrs, (caddr_t)(void *)&objp->s_addr,
266 	    sizeof objp->s_addr);
267 }
268 
269 static bool_t
270 xdr_ypbind_binding(xdrs, objp)
271 	XDR *xdrs;
272 	struct ypbind_binding *objp;
273 {
274 
275 	_DIAGASSERT(xdrs != NULL);
276 	_DIAGASSERT(objp != NULL);
277 
278 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr))
279 		return FALSE;
280 
281 	if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
282 	    sizeof objp->ypbind_binding_port))
283 		return FALSE;
284 
285 	return TRUE;
286 }
287 
288 static bool_t
289 xdr_ypbind_resptype(xdrs, objp)
290 	XDR *xdrs;
291 	enum ypbind_resptype *objp;
292 {
293 
294 	_DIAGASSERT(xdrs != NULL);
295 	_DIAGASSERT(objp != NULL);
296 
297 	return xdr_enum(xdrs, (enum_t *)(void *)objp);
298 }
299 
300 static bool_t
301 xdr_ypstat(xdrs, objp)
302 	XDR *xdrs;
303 	enum ypbind_resptype *objp;
304 {
305 
306 	_DIAGASSERT(xdrs != NULL);
307 	_DIAGASSERT(objp != NULL);
308 
309 	return xdr_enum(xdrs, (enum_t *)(void *)objp);
310 }
311 
312 bool_t
313 xdr_ypbind_resp(xdrs, objp)
314 	XDR *xdrs;
315 	struct ypbind_resp *objp;
316 {
317 
318 	_DIAGASSERT(xdrs != NULL);
319 	_DIAGASSERT(objp != NULL);
320 
321 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status))
322 		return FALSE;
323 
324 	switch (objp->ypbind_status) {
325 	case YPBIND_FAIL_VAL:
326 		return xdr_u_int(xdrs,
327 		    (u_int *)&objp->ypbind_respbody.ypbind_error);
328 
329 	case YPBIND_SUCC_VAL:
330 		return xdr_ypbind_binding(xdrs,
331 		    &objp->ypbind_respbody.ypbind_bindinfo);
332 
333 	default:
334 		return FALSE;
335 	}
336 	/* NOTREACHED */
337 }
338 
339 bool_t
340 xdr_ypresp_val(xdrs, objp)
341 	XDR *xdrs;
342 	struct ypresp_val *objp;
343 {
344 
345 	_DIAGASSERT(xdrs != NULL);
346 	_DIAGASSERT(objp != NULL);
347 
348 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)(void *)&objp->status))
349 		return FALSE;
350 
351 	if (!xdr_datum(xdrs, &objp->valdat))
352 		return FALSE;
353 
354 	return TRUE;
355 }
356 
357 bool_t
358 xdr_ypbind_setdom(xdrs, objp)
359 	XDR *xdrs;
360 	struct ypbind_setdom *objp;
361 {
362 	char *cp;
363 
364 	_DIAGASSERT(xdrs != NULL);
365 	_DIAGASSERT(objp != NULL);
366 
367 	cp = objp->ypsetdom_domain;
368 
369 	if (!xdr_ypdomain_wrap_string(xdrs, &cp))
370 		return FALSE;
371 
372 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding))
373 		return FALSE;
374 
375 	if (!xdr_u_int(xdrs, &objp->ypsetdom_vers))
376 		return FALSE;
377 
378 	return TRUE;
379 }
380 
381 bool_t
382 xdr_ypresp_key_val(xdrs, objp)
383 	XDR *xdrs;
384 	struct ypresp_key_val *objp;
385 {
386 
387 	_DIAGASSERT(xdrs != NULL);
388 	_DIAGASSERT(objp != NULL);
389 
390 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)(void *)&objp->status))
391 		return FALSE;
392 
393 	if (!xdr_datum(xdrs, &objp->valdat))
394 		return FALSE;
395 
396 	if (!xdr_datum(xdrs, &objp->keydat))
397 		return FALSE;
398 
399 	return TRUE;
400 }
401 
402 bool_t
403 xdr_ypall(xdrs, incallback)
404 	XDR *xdrs;
405 	struct ypall_callback *incallback;
406 {
407 	struct ypresp_key_val out;
408 	char key[YPMAXRECORD], val[YPMAXRECORD];
409 	bool_t more, status;
410 
411 	_DIAGASSERT(xdrs != NULL);
412 	_DIAGASSERT(incallback != NULL);
413 
414 	/*
415 	 * Set up key/val struct to be used during the transaction.
416 	 */
417 	memset(&out, 0, sizeof out);
418 	out.keydat.dptr = key;
419 	out.keydat.dsize = sizeof(key);
420 	out.valdat.dptr = val;
421 	out.valdat.dsize = sizeof(val);
422 
423 	for (;;) {
424 		/* Values pending? */
425 		if (!xdr_bool(xdrs, &more))
426 			return FALSE;		/* can't tell! */
427 		if (!more)
428 			return TRUE;		/* no more */
429 
430 		/* Transfer key/value pair. */
431 		status = xdr_ypresp_key_val(xdrs, &out);
432 
433 		/*
434 		 * If we succeeded, call the callback function.
435 		 * The callback will return TRUE when it wants
436 		 * no more values.  If we fail, indicate the
437 		 * error.
438 		 */
439 		if (status) {
440 			if ((*incallback->foreach)((int)out.status,
441 			    __UNCONST(out.keydat.dptr), out.keydat.dsize,
442 			    __UNCONST(out.valdat.dptr), out.valdat.dsize,
443 			    incallback->data))
444 				return TRUE;
445 		} else
446 			return FALSE;
447 	}
448 }
449 
450 bool_t
451 xdr_ypresp_master(xdrs, objp)
452 	XDR *xdrs;
453 	struct ypresp_master *objp;
454 {
455 
456 	_DIAGASSERT(xdrs != NULL);
457 	_DIAGASSERT(objp != NULL);
458 
459 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)(void *)&objp->status))
460 		return FALSE;
461 
462 	if (!xdr_string(xdrs, &objp->master, YPMAXPEER))
463 		return FALSE;
464 
465 	return TRUE;
466 }
467 
468 static bool_t
469 xdr_ypmaplist_str(xdrs, objp)
470 	XDR *xdrs;
471 	char *objp;
472 {
473 
474 	_DIAGASSERT(xdrs != NULL);
475 	_DIAGASSERT(objp != NULL);
476 
477 	return xdr_string(xdrs, &objp, YPMAXMAP+1);
478 }
479 
480 bool_t
481 xdr_ypmaplist(xdrs, objp)
482 	XDR *xdrs;
483 	struct ypmaplist *objp;
484 {
485 
486 	_DIAGASSERT(xdrs != NULL);
487 	_DIAGASSERT(objp != NULL);
488 
489 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name))
490 		return FALSE;
491 
492 	if (!xdr_pointer(xdrs, (char **)(void *)&objp->ypml_next,
493 	    sizeof(struct ypmaplist), (xdrproc_t)xdr_ypmaplist))
494 		return FALSE;
495 
496 	return TRUE;
497 }
498 
499 bool_t
500 xdr_ypresp_maplist(xdrs, objp)
501 	XDR *xdrs;
502 	struct ypresp_maplist *objp;
503 {
504 
505 	_DIAGASSERT(xdrs != NULL);
506 	_DIAGASSERT(objp != NULL);
507 
508 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)(void *)&objp->status))
509 		return FALSE;
510 
511 	if (!xdr_pointer(xdrs, (char **)(void *)&objp->list,
512 	    sizeof(struct ypmaplist), (xdrproc_t)xdr_ypmaplist))
513 		return FALSE;
514 
515 	return TRUE;
516 }
517 
518 bool_t
519 xdr_ypresp_order(xdrs, objp)
520 	XDR *xdrs;
521 	struct ypresp_order *objp;
522 {
523 
524 	_DIAGASSERT(xdrs != NULL);
525 	_DIAGASSERT(objp != NULL);
526 
527 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)(void *)&objp->status))
528 		return FALSE;
529 
530 	if (!xdr_u_int(xdrs, &objp->ordernum))
531 		return FALSE;
532 
533 	return TRUE;
534 }
535 
536 bool_t
537 xdr_ypreq_xfr(xdrs, objp)
538 	XDR *xdrs;
539 	struct ypreq_xfr *objp;
540 {
541 
542 	_DIAGASSERT(xdrs != NULL);
543 	_DIAGASSERT(objp != NULL);
544 
545 	if (!xdr_ypmap_parms(xdrs, &objp->map_parms))
546 		return FALSE;
547 
548 	if (!xdr_u_int(xdrs, &objp->transid))
549 		return FALSE;
550 
551 	if (!xdr_u_int(xdrs, &objp->proto))
552 		return FALSE;
553 
554 	if (!xdr_u_int(xdrs, &objp->port))
555 		return FALSE;
556 
557 	return TRUE;
558 }
559 
560 bool_t
561 xdr_ypmap_parms(xdrs, objp)
562 	XDR *xdrs;
563 	struct ypmap_parms *objp;
564 {
565 
566 	_DIAGASSERT(xdrs != NULL);
567 	_DIAGASSERT(objp != NULL);
568 
569 	if (!xdr_ypdomain_wrap_string(xdrs, __UNCONST(&objp->domain)))
570 		return FALSE;
571 
572 	if (!xdr_ypmap_wrap_string(xdrs, __UNCONST(&objp->map)))
573 		return FALSE;
574 
575 	if (!xdr_u_int(xdrs, &objp->ordernum))
576 		return FALSE;
577 
578 	if (!xdr_ypowner_wrap_string(xdrs, &objp->owner))
579 		return FALSE;
580 
581 	return TRUE;
582 }
583 
584 bool_t
585 xdr_yppushresp_xfr(xdrs, objp)
586 	XDR *xdrs;
587 	struct yppushresp_xfr *objp;
588 {
589 
590 	_DIAGASSERT(xdrs != NULL);
591 	_DIAGASSERT(objp != NULL);
592 
593 	if (!xdr_u_int(xdrs, &objp->transid))
594 		return FALSE;
595 
596 	if (!xdr_enum(xdrs, (enum_t *)&objp->status))
597 		return FALSE;
598 
599 	return TRUE;
600 }
601