xref: /freebsd/lib/libc/yp/xdryp.c (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
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 
30 #ifndef LINT
31 static char *rcsid = "$Id: xdryp.c,v 1.1 1993/11/01 23:56:28 paul Exp $";
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <rpc/rpc.h>
40 #include <rpc/xdr.h>
41 #include <rpcsvc/yp_prot.h>
42 #include <rpcsvc/ypclnt.h>
43 
44 extern int (*ypresp_allfn)();
45 extern void *ypresp_data;
46 
47 struct ypresp_all {
48 	bool_t more;
49 	union {
50 		struct ypresp_key_val val;
51 	} ypresp_all_u;
52 };
53 
54 bool_t
55 xdr_domainname(xdrs, objp)
56 XDR *xdrs;
57 char *objp;
58 {
59 	if (!xdr_string(xdrs, &objp, YPMAXDOMAIN)) {
60 		return (FALSE);
61 	}
62 	return (TRUE);
63 }
64 
65 bool_t
66 xdr_peername(xdrs, objp)
67 XDR *xdrs;
68 char *objp;
69 {
70 	if (!xdr_string(xdrs, &objp, YPMAXPEER)) {
71 		return (FALSE);
72 	}
73 	return (TRUE);
74 }
75 
76 bool_t
77 xdr_datum(xdrs, objp)
78 XDR *xdrs;
79 datum *objp;
80 {
81 	if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) {
82 		return (FALSE);
83 	}
84 	return (TRUE);
85 }
86 
87 bool_t
88 xdr_mapname(xdrs, objp)
89 XDR *xdrs;
90 char *objp;
91 {
92 	if (!xdr_string(xdrs, &objp, YPMAXMAP)) {
93 		return (FALSE);
94 	}
95 	return (TRUE);
96 }
97 
98 bool_t
99 xdr_ypreq_key(xdrs, objp)
100 XDR *xdrs;
101 struct ypreq_key *objp;
102 {
103 	if (!xdr_domainname(xdrs, objp->domain)) {
104 		return (FALSE);
105 	}
106 	if (!xdr_mapname(xdrs, objp->map)) {
107 		return (FALSE);
108 	}
109 	if (!xdr_datum(xdrs, &objp->keydat)) {
110 		return (FALSE);
111 	}
112 	return (TRUE);
113 }
114 
115 bool_t
116 xdr_ypreq_nokey(xdrs, objp)
117 XDR *xdrs;
118 struct ypreq_nokey *objp;
119 {
120 	if (!xdr_domainname(xdrs, objp->domain)) {
121 		return (FALSE);
122 	}
123 	if (!xdr_mapname(xdrs, objp->map)) {
124 		return (FALSE);
125 	}
126 	return (TRUE);
127 }
128 
129 bool_t
130 xdr_yp_inaddr(xdrs, objp)
131 XDR *xdrs;
132 struct in_addr *objp;
133 {
134 	if (!xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr)) {
135 		return (FALSE);
136 	}
137 	return (TRUE);
138 }
139 
140 bool_t
141 xdr_ypbind_binding(xdrs, objp)
142 XDR *xdrs;
143 struct ypbind_binding *objp;
144 {
145 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr)) {
146 		return (FALSE);
147 	}
148 	if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
149 	    sizeof objp->ypbind_binding_port)) {
150 		return (FALSE);
151 	}
152 	return (TRUE);
153 }
154 
155 bool_t
156 xdr_ypbind_resptype(xdrs, objp)
157 XDR *xdrs;
158 enum ypbind_resptype *objp;
159 {
160 	if (!xdr_enum(xdrs, (enum_t *)objp)) {
161 		return (FALSE);
162 	}
163 	return (TRUE);
164 }
165 
166 bool_t
167 xdr_ypstat(xdrs, objp)
168 XDR *xdrs;
169 enum ypbind_resptype *objp;
170 {
171 	if (!xdr_enum(xdrs, (enum_t *)objp)) {
172 		return (FALSE);
173 	}
174 	return (TRUE);
175 }
176 
177 bool_t
178 xdr_ypbind_resp(xdrs, objp)
179 XDR *xdrs;
180 struct ypbind_resp *objp;
181 {
182 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status)) {
183 		return (FALSE);
184 	}
185 	switch (objp->ypbind_status) {
186 	case YPBIND_FAIL_VAL:
187 		if (!xdr_u_int(xdrs, (u_int *)&objp->ypbind_respbody.ypbind_error)) {
188 			return (FALSE);
189 		}
190 		break;
191 	case YPBIND_SUCC_VAL:
192 		if (!xdr_ypbind_binding(xdrs, &objp->ypbind_respbody.ypbind_bindinfo)) {
193 			return (FALSE);
194 		}
195 		break;
196 	default:
197 		return (FALSE);
198 	}
199 	return (TRUE);
200 }
201 
202 bool_t
203 xdr_ypresp_val(xdrs, objp)
204 XDR *xdrs;
205 struct ypresp_val *objp;
206 {
207 	if (!xdr_ypstat(xdrs, &objp->status)) {
208 		return (FALSE);
209 	}
210 	if (!xdr_datum(xdrs, &objp->valdat)) {
211 		return (FALSE);
212 	}
213 	return (TRUE);
214 }
215 
216 bool_t
217 xdr_ypbind_setdom(xdrs, objp)
218 XDR *xdrs;
219 struct ypbind_setdom *objp;
220 {
221 	if (!xdr_domainname(xdrs, objp->ypsetdom_domain)) {
222 		return (FALSE);
223 	}
224 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding)) {
225 		return (FALSE);
226 	}
227 	if (!xdr_u_short(xdrs, &objp->ypsetdom_vers)) {
228 		return (FALSE);
229 	}
230 	return (TRUE);
231 }
232 
233 bool_t
234 xdr_ypresp_key_val(xdrs, objp)
235 XDR *xdrs;
236 struct ypresp_key_val *objp;
237 {
238 	if (!xdr_ypstat(xdrs, &objp->status)) {
239 		return (FALSE);
240 	}
241 	if (!xdr_datum(xdrs, &objp->valdat)) {
242 		return (FALSE);
243 	}
244 	if (!xdr_datum(xdrs, &objp->keydat)) {
245 		return (FALSE);
246 	}
247 	return (TRUE);
248 }
249 
250 bool_t
251 xdr_ypresp_all(xdrs, objp)
252 XDR *xdrs;
253 struct ypresp_all *objp;
254 {
255 	if (!xdr_bool(xdrs, &objp->more)) {
256 		return (FALSE);
257 	}
258 	switch (objp->more) {
259 	case TRUE:
260 		if (!xdr_ypresp_key_val(xdrs, &objp->ypresp_all_u.val)) {
261 			return (FALSE);
262 		}
263 		break;
264 	case FALSE:
265 		break;
266 	default:
267 		return (FALSE);
268 	}
269 	return (TRUE);
270 }
271 
272 bool_t
273 xdr_ypresp_all_seq(xdrs, objp)
274 XDR *xdrs;
275 u_long *objp;
276 {
277 	struct ypresp_all out;
278 	u_long status;
279 	char *key, *val;
280 	int r;
281 
282 	bzero(&out, sizeof out);
283 	while(1) {
284 		if( !xdr_ypresp_all(xdrs, &out)) {
285 			xdr_free(xdr_ypresp_all, (char *)&out);
286 			*objp = YP_YPERR;
287 			return FALSE;
288 		}
289 		if(out.more == 0) {
290 			xdr_free(xdr_ypresp_all, (char *)&out);
291 			return FALSE;
292 		}
293 		status = out.ypresp_all_u.val.status;
294 		switch(status) {
295 		case YP_TRUE:
296 			key = (char *)malloc(out.ypresp_all_u.val.keydat.dsize + 1);
297 			bcopy(out.ypresp_all_u.val.keydat.dptr, key,
298 				out.ypresp_all_u.val.keydat.dsize);
299 			key[out.ypresp_all_u.val.keydat.dsize] = '\0';
300 			val = (char *)malloc(out.ypresp_all_u.val.valdat.dsize + 1);
301 			bcopy(out.ypresp_all_u.val.valdat.dptr, val,
302 				out.ypresp_all_u.val.valdat.dsize);
303 			val[out.ypresp_all_u.val.valdat.dsize] = '\0';
304 			xdr_free(xdr_ypresp_all, (char *)&out);
305 
306 			r = (*ypresp_allfn)(status,
307 				key, out.ypresp_all_u.val.keydat.dsize,
308 				val, out.ypresp_all_u.val.valdat.dsize,
309 				ypresp_data);
310 			*objp = status;
311 			free(key);
312 			free(val);
313 			if(r)
314 				return TRUE;
315 			break;
316 		case YP_NOMORE:
317 			xdr_free(xdr_ypresp_all, (char *)&out);
318 			return TRUE;
319 		default:
320 			xdr_free(xdr_ypresp_all, (char *)&out);
321 			*objp = status;
322 			return TRUE;
323 		}
324 	}
325 }
326 
327 bool_t
328 xdr_ypresp_master(xdrs, objp)
329 XDR *xdrs;
330 struct ypresp_master *objp;
331 {
332 	if (!xdr_ypstat(xdrs, &objp->status)) {
333 		return (FALSE);
334 	}
335 	if (!xdr_string(xdrs, &objp->master, YPMAXPEER)) {
336 		return (FALSE);
337 	}
338 	return (TRUE);
339 }
340 
341 bool_t
342 xdr_ypmaplist_str(xdrs, objp)
343 XDR *xdrs;
344 char *objp;
345 {
346 	if (!xdr_string(xdrs, &objp, YPMAXMAP+1)) {
347 		return (FALSE);
348 	}
349 	return (TRUE);
350 }
351 
352 bool_t
353 xdr_ypmaplist(xdrs, objp)
354 XDR *xdrs;
355 struct ypmaplist *objp;
356 {
357 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name)) {
358 		return (FALSE);
359 	}
360 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
361 	    sizeof(struct ypmaplist), xdr_ypmaplist)) {
362 		return (FALSE);
363 	}
364 	return (TRUE);
365 }
366 
367 bool_t
368 xdr_ypresp_maplist(xdrs, objp)
369 XDR *xdrs;
370 struct ypresp_maplist *objp;
371 {
372 	if (!xdr_ypstat(xdrs, &objp->status)) {
373 		return (FALSE);
374 	}
375 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->list,
376 	    sizeof(struct ypmaplist), xdr_ypmaplist)) {
377 		return (FALSE);
378 	}
379 	return (TRUE);
380 }
381 
382 bool_t
383 xdr_ypresp_order(xdrs, objp)
384 XDR *xdrs;
385 struct ypresp_order *objp;
386 {
387 	if (!xdr_ypstat(xdrs, &objp->status)) {
388 		return (FALSE);
389 	}
390 	if (!xdr_u_long(xdrs, &objp->ordernum)) {
391 		return (FALSE);
392 	}
393 	return (TRUE);
394 }
395