xref: /netbsd-src/lib/libc/yp/xdryp.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1992, 1993 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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Theo de Raadt.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef LINT
33 static char *rcsid = "$Id: xdryp.c,v 1.7 1994/08/06 23:07:47 jtc Exp $";
34 #endif
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <rpc/rpc.h>
44 #include <rpc/xdr.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 
48 extern int (*ypresp_allfn)();
49 extern void *ypresp_data;
50 
51 struct ypresp_all {
52 	bool_t more;
53 	union {
54 		struct ypresp_key_val val;
55 	} ypresp_all_u;
56 };
57 
58 bool_t
59 xdr_domainname(xdrs, objp)
60 XDR *xdrs;
61 char *objp;
62 {
63 	return xdr_string(xdrs, &objp, YPMAXDOMAIN);
64 }
65 
66 bool_t
67 xdr_peername(xdrs, objp)
68 XDR *xdrs;
69 char *objp;
70 {
71 	return xdr_string(xdrs, &objp, YPMAXPEER);
72 }
73 
74 bool_t
75 xdr_datum(xdrs, objp)
76 XDR *xdrs;
77 datum *objp;
78 {
79 	return xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD);
80 }
81 
82 bool_t
83 xdr_mapname(xdrs, objp)
84 XDR *xdrs;
85 char *objp;
86 {
87 	return xdr_string(xdrs, &objp, YPMAXMAP);
88 }
89 
90 bool_t
91 xdr_ypreq_key(xdrs, objp)
92 XDR *xdrs;
93 struct ypreq_key *objp;
94 {
95 	if (!xdr_domainname(xdrs, objp->domain)) {
96 		return (FALSE);
97 	}
98 	if (!xdr_mapname(xdrs, objp->map)) {
99 		return (FALSE);
100 	}
101 	return xdr_datum(xdrs, &objp->keydat);
102 }
103 
104 bool_t
105 xdr_ypreq_nokey(xdrs, objp)
106 XDR *xdrs;
107 struct ypreq_nokey *objp;
108 {
109 	if (!xdr_domainname(xdrs, objp->domain)) {
110 		return (FALSE);
111 	}
112 	return xdr_mapname(xdrs, objp->map);
113 }
114 
115 bool_t
116 xdr_yp_inaddr(xdrs, objp)
117 XDR *xdrs;
118 struct in_addr *objp;
119 {
120 	return xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr);
121 }
122 
123 bool_t
124 xdr_ypbind_binding(xdrs, objp)
125 XDR *xdrs;
126 struct ypbind_binding *objp;
127 {
128 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr)) {
129 		return (FALSE);
130 	}
131 	return xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
132 	    sizeof objp->ypbind_binding_port);
133 }
134 
135 bool_t
136 xdr_ypbind_resptype(xdrs, objp)
137 XDR *xdrs;
138 enum ypbind_resptype *objp;
139 {
140 	return xdr_enum(xdrs, (enum_t *)objp);
141 }
142 
143 bool_t
144 xdr_ypstat(xdrs, objp)
145 XDR *xdrs;
146 enum ypbind_resptype *objp;
147 {
148 	return xdr_enum(xdrs, (enum_t *)objp);
149 }
150 
151 bool_t
152 xdr_ypbind_resp(xdrs, objp)
153 XDR *xdrs;
154 struct ypbind_resp *objp;
155 {
156 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status)) {
157 		return (FALSE);
158 	}
159 	switch (objp->ypbind_status) {
160 	case YPBIND_FAIL_VAL:
161 		return xdr_u_int(xdrs, (u_int *)&objp->ypbind_respbody.ypbind_error);
162 	case YPBIND_SUCC_VAL:
163 		return xdr_ypbind_binding(xdrs, &objp->ypbind_respbody.ypbind_bindinfo);
164 	default:
165 		return (FALSE);
166 	}
167 	/* NOTREACHED */
168 }
169 
170 bool_t
171 xdr_ypresp_val(xdrs, objp)
172 XDR *xdrs;
173 struct ypresp_val *objp;
174 {
175 	if (!xdr_ypstat(xdrs, &objp->status)) {
176 		return (FALSE);
177 	}
178 	return xdr_datum(xdrs, &objp->valdat);
179 }
180 
181 bool_t
182 xdr_ypbind_setdom(xdrs, objp)
183 XDR *xdrs;
184 struct ypbind_setdom *objp;
185 {
186 	if (!xdr_domainname(xdrs, objp->ypsetdom_domain)) {
187 		return (FALSE);
188 	}
189 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding)) {
190 		return (FALSE);
191 	}
192 	return xdr_u_short(xdrs, &objp->ypsetdom_vers);
193 }
194 
195 bool_t
196 xdr_ypresp_key_val(xdrs, objp)
197 XDR *xdrs;
198 struct ypresp_key_val *objp;
199 {
200 	if (!xdr_ypstat(xdrs, &objp->status)) {
201 		return (FALSE);
202 	}
203 	if (!xdr_datum(xdrs, &objp->valdat)) {
204 		return (FALSE);
205 	}
206 	return xdr_datum(xdrs, &objp->keydat);
207 }
208 
209 bool_t
210 xdr_ypresp_all(xdrs, objp)
211 XDR *xdrs;
212 struct ypresp_all *objp;
213 {
214 	if (!xdr_bool(xdrs, &objp->more)) {
215 		return (FALSE);
216 	}
217 	switch (objp->more) {
218 	case TRUE:
219 		return xdr_ypresp_key_val(xdrs, &objp->ypresp_all_u.val);
220 	case FALSE:
221 		return (TRUE);
222 	default:
223 		return (FALSE);
224 	}
225 	/* NOTREACHED */
226 }
227 
228 bool_t
229 xdr_ypresp_all_seq(xdrs, objp)
230 XDR *xdrs;
231 u_long *objp;
232 {
233 	struct ypresp_all out;
234 	u_long status;
235 	char *key, *val;
236 	int r;
237 
238 	memset(&out, 0, sizeof out);
239 	while(1) {
240 		if( !xdr_ypresp_all(xdrs, &out)) {
241 			xdr_free(xdr_ypresp_all, (char *)&out);
242 			*objp = YP_YPERR;
243 			return FALSE;
244 		}
245 		if(out.more == 0) {
246 			xdr_free(xdr_ypresp_all, (char *)&out);
247 			return FALSE;
248 		}
249 		status = out.ypresp_all_u.val.status;
250 		switch(status) {
251 		case YP_TRUE:
252 			key = (char *)malloc(out.ypresp_all_u.val.keydat.dsize + 1);
253 			memcpy(key, out.ypresp_all_u.val.keydat.dptr,
254 				out.ypresp_all_u.val.keydat.dsize);
255 			key[out.ypresp_all_u.val.keydat.dsize] = '\0';
256 			val = (char *)malloc(out.ypresp_all_u.val.valdat.dsize + 1);
257 			memcpy(val, out.ypresp_all_u.val.valdat.dptr,
258 				out.ypresp_all_u.val.valdat.dsize);
259 			val[out.ypresp_all_u.val.valdat.dsize] = '\0';
260 			xdr_free(xdr_ypresp_all, (char *)&out);
261 
262 			r = (*ypresp_allfn)(status,
263 				key, out.ypresp_all_u.val.keydat.dsize,
264 				val, out.ypresp_all_u.val.valdat.dsize,
265 				ypresp_data);
266 			*objp = status;
267 			free(key);
268 			free(val);
269 			if(r)
270 				return TRUE;
271 			break;
272 		case YP_NOMORE:
273 			xdr_free(xdr_ypresp_all, (char *)&out);
274 			return TRUE;
275 		default:
276 			xdr_free(xdr_ypresp_all, (char *)&out);
277 			*objp = status;
278 			return TRUE;
279 		}
280 	}
281 }
282 
283 bool_t
284 xdr_ypresp_master(xdrs, objp)
285 XDR *xdrs;
286 struct ypresp_master *objp;
287 {
288 	if (!xdr_ypstat(xdrs, &objp->status)) {
289 		return (FALSE);
290 	}
291 	return xdr_string(xdrs, &objp->master, YPMAXPEER);
292 }
293 
294 bool_t
295 xdr_ypmaplist_str(xdrs, objp)
296 XDR *xdrs;
297 char *objp;
298 {
299 	return xdr_string(xdrs, &objp, YPMAXMAP+1);
300 }
301 
302 bool_t
303 xdr_ypmaplist(xdrs, objp)
304 XDR *xdrs;
305 struct ypmaplist *objp;
306 {
307 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name)) {
308 		return (FALSE);
309 	}
310 	return xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
311 	    sizeof(struct ypmaplist), xdr_ypmaplist);
312 }
313 
314 bool_t
315 xdr_ypresp_maplist(xdrs, objp)
316 XDR *xdrs;
317 struct ypresp_maplist *objp;
318 {
319 	if (!xdr_ypstat(xdrs, &objp->status)) {
320 		return (FALSE);
321 	}
322 	return xdr_pointer(xdrs, (caddr_t *)&objp->list,
323 	    sizeof(struct ypmaplist), xdr_ypmaplist);
324 }
325 
326 bool_t
327 xdr_ypresp_order(xdrs, objp)
328 XDR *xdrs;
329 struct ypresp_order *objp;
330 {
331 	if (!xdr_ypstat(xdrs, &objp->status)) {
332 		return (FALSE);
333 	}
334 	return xdr_u_long(xdrs, &objp->ordernum);
335 }
336