xref: /dragonfly/lib/libc/yp/xdryp.c (revision ec21d9fb)
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/lib/libc/yp/xdryp.c,v 1.15 2008/03/26 07:32:06 brueffer Exp $
30  */
31 
32 #include <rpc/rpc.h>
33 #include <rpcsvc/yp.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 extern int (*ypresp_allfn)(unsigned long, char *, int, char *, int, void *);
38 extern void *ypresp_data;
39 
40 /*
41  * I'm leaving the xdr_datum() function in purely for backwards
42  * compatibility. yplib.c doesn't actually use it, but it's listed
43  * in yp_prot.h as being available, so it's probably a good idea to
44  * leave it in case somebody goes looking for it.
45  */
46 typedef struct {
47 	char   *dptr;
48 	size_t dsize;
49 } datum;
50 
51 bool_t
52 xdr_datum(XDR *xdrs, datum *objp)
53 {
54 	if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) {
55 		return (FALSE);
56 	}
57 	return (TRUE);
58 }
59 
60 bool_t
61 xdr_ypresp_all_seq(XDR *xdrs, u_long *objp)
62 {
63 	struct ypresp_all out;
64 	u_long status;
65 	char *key, *val;
66 	int r;
67 
68 	bzero(&out, sizeof out);
69 	while (1) {
70 		if (!xdr_ypresp_all(xdrs, &out)) {
71 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
72 			*objp = YP_YPERR;
73 			return (FALSE);
74 		}
75 		if (out.more == 0) {
76 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
77 			*objp = YP_NOMORE;
78 			return (TRUE);
79 		}
80 		status = out.ypresp_all_u.val.stat;
81 		switch (status) {
82 		case YP_TRUE:
83 			key = (char *)malloc(out.ypresp_all_u.val.key.keydat_len + 1);
84 			bcopy(out.ypresp_all_u.val.key.keydat_val, key,
85 				out.ypresp_all_u.val.key.keydat_len);
86 			key[out.ypresp_all_u.val.key.keydat_len] = '\0';
87 			val = (char *)malloc(out.ypresp_all_u.val.val.valdat_len + 1);
88 			bcopy(out.ypresp_all_u.val.val.valdat_val, val,
89 				out.ypresp_all_u.val.val.valdat_len);
90 			val[out.ypresp_all_u.val.val.valdat_len] = '\0';
91 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
92 
93 			r = (*ypresp_allfn)(status,
94 				key, out.ypresp_all_u.val.key.keydat_len,
95 				val, out.ypresp_all_u.val.val.valdat_len,
96 				ypresp_data);
97 			*objp = status;
98 			free(key);
99 			free(val);
100 			if (r)
101 				return (TRUE);
102 			break;
103 		case YP_NOMORE:
104 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
105 			*objp = YP_NOMORE;
106 			return (TRUE);
107 		default:
108 			xdr_free((xdrproc_t)xdr_ypresp_all, &out);
109 			*objp = status;
110 			return (TRUE);
111 		}
112 	}
113 }
114