xdryp.c revision 1.16 1 1.16 christos /* $NetBSD: xdryp.c,v 1.16 1997/07/13 20:28:10 christos Exp $ */
2 1.8 cgd
3 1.3 deraadt /*
4 1.12 thorpej * Copyright (c) 1996 Jason R. Thorpe <thorpej (at) NetBSD.ORG>.
5 1.12 thorpej * All rights reserved.
6 1.12 thorpej *
7 1.6 deraadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
8 1.3 deraadt * All rights reserved.
9 1.3 deraadt *
10 1.3 deraadt * Redistribution and use in source and binary forms, with or without
11 1.3 deraadt * modification, are permitted provided that the following conditions
12 1.3 deraadt * are met:
13 1.3 deraadt * 1. Redistributions of source code must retain the above copyright
14 1.3 deraadt * notice, this list of conditions and the following disclaimer.
15 1.3 deraadt * 2. Redistributions in binary form must reproduce the above copyright
16 1.3 deraadt * notice, this list of conditions and the following disclaimer in the
17 1.3 deraadt * documentation and/or other materials provided with the distribution.
18 1.6 deraadt * 3. All advertising materials mentioning features or use of this software
19 1.6 deraadt * must display the following acknowledgement:
20 1.6 deraadt * This product includes software developed by Theo de Raadt.
21 1.12 thorpej * This product includes software developed for the NetBSD Project
22 1.12 thorpej * by Jason R. Thorpe.
23 1.6 deraadt * 4. The name of the author may not be used to endorse or promote products
24 1.6 deraadt * derived from this software without specific prior written permission.
25 1.3 deraadt *
26 1.3 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
27 1.3 deraadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 1.3 deraadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.3 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
30 1.3 deraadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.3 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.3 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.3 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.3 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.3 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.3 deraadt * SUCH DAMAGE.
37 1.3 deraadt */
38 1.3 deraadt
39 1.16 christos #include <sys/cdefs.h>
40 1.10 jtc #if defined(LIBC_SCCS) && !defined(lint)
41 1.16 christos __RCSID("$NetBSD: xdryp.c,v 1.16 1997/07/13 20:28:10 christos Exp $");
42 1.3 deraadt #endif
43 1.3 deraadt
44 1.12 thorpej /*
45 1.12 thorpej * XDR routines used by the YP protocol. Note that these routines do
46 1.12 thorpej * not strictly conform to the RPC definition in yp.x. This file
47 1.12 thorpej * replicates the functions exported by the Sun YP API; reality is
48 1.12 thorpej * often inaccurate.
49 1.12 thorpej */
50 1.12 thorpej
51 1.1 deraadt #include <sys/param.h>
52 1.1 deraadt #include <sys/types.h>
53 1.1 deraadt #include <sys/socket.h>
54 1.1 deraadt #include <stdio.h>
55 1.7 jtc #include <stdlib.h>
56 1.7 jtc #include <string.h>
57 1.1 deraadt #include <ctype.h>
58 1.1 deraadt #include <rpc/rpc.h>
59 1.1 deraadt #include <rpc/xdr.h>
60 1.1 deraadt #include <rpcsvc/yp_prot.h>
61 1.1 deraadt #include <rpcsvc/ypclnt.h>
62 1.1 deraadt
63 1.12 thorpej /*
64 1.12 thorpej * Functions used only within this file.
65 1.12 thorpej */
66 1.12 thorpej static bool_t xdr_ypbind_binding __P((XDR *, struct ypbind_binding *));
67 1.12 thorpej static bool_t xdr_ypbind_resptype __P((XDR *, enum ypbind_resptype *));
68 1.12 thorpej static bool_t xdr_ypstat __P((XDR *, enum ypbind_resptype *));
69 1.12 thorpej static bool_t xdr_ypmaplist_str __P((XDR *, char *));
70 1.12 thorpej
71 1.12 thorpej __warn_references(xdr_domainname,
72 1.12 thorpej "warning: this program uses xdr_domainname(), which is deprecated and buggy.");
73 1.1 deraadt
74 1.1 deraadt bool_t
75 1.1 deraadt xdr_domainname(xdrs, objp)
76 1.12 thorpej XDR *xdrs;
77 1.12 thorpej char *objp;
78 1.1 deraadt {
79 1.7 jtc return xdr_string(xdrs, &objp, YPMAXDOMAIN);
80 1.1 deraadt }
81 1.1 deraadt
82 1.12 thorpej __warn_references(xdr_peername,
83 1.12 thorpej "warning: this program uses xdr_peername(), which is deprecated and buggy.");
84 1.12 thorpej
85 1.1 deraadt bool_t
86 1.1 deraadt xdr_peername(xdrs, objp)
87 1.12 thorpej XDR *xdrs;
88 1.12 thorpej char *objp;
89 1.1 deraadt {
90 1.7 jtc return xdr_string(xdrs, &objp, YPMAXPEER);
91 1.1 deraadt }
92 1.1 deraadt
93 1.12 thorpej __warn_references(xdr_mapname,
94 1.12 thorpej "warning: this program uses xdr_mapname(), which is deprecated and buggy.");
95 1.12 thorpej
96 1.12 thorpej bool_t
97 1.12 thorpej xdr_mapname(xdrs, objp)
98 1.12 thorpej XDR *xdrs;
99 1.12 thorpej char *objp;
100 1.12 thorpej {
101 1.12 thorpej return xdr_string(xdrs, &objp, YPMAXMAP);
102 1.12 thorpej }
103 1.12 thorpej
104 1.12 thorpej bool_t
105 1.12 thorpej xdr_ypdomain_wrap_string(xdrs, objp)
106 1.12 thorpej XDR *xdrs;
107 1.12 thorpej char **objp;
108 1.12 thorpej {
109 1.12 thorpej return xdr_string(xdrs, objp, YPMAXDOMAIN);
110 1.12 thorpej }
111 1.12 thorpej
112 1.12 thorpej bool_t
113 1.12 thorpej xdr_ypmap_wrap_string(xdrs, objp)
114 1.12 thorpej XDR *xdrs;
115 1.12 thorpej char **objp;
116 1.12 thorpej {
117 1.12 thorpej return xdr_string(xdrs, objp, YPMAXMAP);
118 1.12 thorpej }
119 1.12 thorpej
120 1.1 deraadt bool_t
121 1.12 thorpej xdr_ypowner_wrap_string(xdrs, objp)
122 1.12 thorpej XDR *xdrs;
123 1.12 thorpej char **objp;
124 1.1 deraadt {
125 1.12 thorpej return xdr_string(xdrs, objp, YPMAXPEER);
126 1.1 deraadt }
127 1.1 deraadt
128 1.1 deraadt bool_t
129 1.12 thorpej xdr_datum(xdrs, objp)
130 1.12 thorpej XDR *xdrs;
131 1.12 thorpej datum *objp;
132 1.1 deraadt {
133 1.12 thorpej return xdr_bytes(xdrs, (char **)&objp->dptr,
134 1.12 thorpej (u_int *)&objp->dsize, YPMAXRECORD);
135 1.1 deraadt }
136 1.1 deraadt
137 1.1 deraadt bool_t
138 1.1 deraadt xdr_ypreq_key(xdrs, objp)
139 1.12 thorpej XDR *xdrs;
140 1.12 thorpej struct ypreq_key *objp;
141 1.1 deraadt {
142 1.13 christos if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
143 1.9 christos return FALSE;
144 1.12 thorpej
145 1.13 christos if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
146 1.9 christos return FALSE;
147 1.12 thorpej
148 1.13 christos if (!xdr_datum(xdrs, &objp->keydat))
149 1.13 christos return FALSE;
150 1.13 christos
151 1.13 christos return TRUE;
152 1.1 deraadt }
153 1.1 deraadt
154 1.1 deraadt bool_t
155 1.1 deraadt xdr_ypreq_nokey(xdrs, objp)
156 1.12 thorpej XDR *xdrs;
157 1.12 thorpej struct ypreq_nokey *objp;
158 1.1 deraadt {
159 1.13 christos if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
160 1.13 christos return FALSE;
161 1.13 christos
162 1.13 christos if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
163 1.9 christos return FALSE;
164 1.12 thorpej
165 1.13 christos return TRUE;
166 1.1 deraadt }
167 1.1 deraadt
168 1.1 deraadt bool_t
169 1.1 deraadt xdr_yp_inaddr(xdrs, objp)
170 1.12 thorpej XDR *xdrs;
171 1.12 thorpej struct in_addr *objp;
172 1.1 deraadt {
173 1.7 jtc return xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr);
174 1.1 deraadt }
175 1.1 deraadt
176 1.12 thorpej static bool_t
177 1.1 deraadt xdr_ypbind_binding(xdrs, objp)
178 1.12 thorpej XDR *xdrs;
179 1.12 thorpej struct ypbind_binding *objp;
180 1.1 deraadt {
181 1.12 thorpej if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr))
182 1.9 christos return FALSE;
183 1.12 thorpej
184 1.13 christos if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
185 1.13 christos sizeof objp->ypbind_binding_port))
186 1.13 christos return FALSE;
187 1.13 christos
188 1.13 christos return TRUE;
189 1.1 deraadt }
190 1.1 deraadt
191 1.12 thorpej static bool_t
192 1.1 deraadt xdr_ypbind_resptype(xdrs, objp)
193 1.12 thorpej XDR *xdrs;
194 1.12 thorpej enum ypbind_resptype *objp;
195 1.1 deraadt {
196 1.7 jtc return xdr_enum(xdrs, (enum_t *)objp);
197 1.1 deraadt }
198 1.1 deraadt
199 1.12 thorpej static bool_t
200 1.1 deraadt xdr_ypstat(xdrs, objp)
201 1.12 thorpej XDR *xdrs;
202 1.12 thorpej enum ypbind_resptype *objp;
203 1.1 deraadt {
204 1.7 jtc return xdr_enum(xdrs, (enum_t *)objp);
205 1.1 deraadt }
206 1.1 deraadt
207 1.1 deraadt bool_t
208 1.1 deraadt xdr_ypbind_resp(xdrs, objp)
209 1.12 thorpej XDR *xdrs;
210 1.12 thorpej struct ypbind_resp *objp;
211 1.1 deraadt {
212 1.12 thorpej if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status))
213 1.9 christos return FALSE;
214 1.9 christos
215 1.1 deraadt switch (objp->ypbind_status) {
216 1.1 deraadt case YPBIND_FAIL_VAL:
217 1.9 christos return xdr_u_int(xdrs,
218 1.12 thorpej (u_int *)&objp->ypbind_respbody.ypbind_error);
219 1.12 thorpej
220 1.1 deraadt case YPBIND_SUCC_VAL:
221 1.12 thorpej return xdr_ypbind_binding(xdrs,
222 1.12 thorpej &objp->ypbind_respbody.ypbind_bindinfo);
223 1.12 thorpej
224 1.1 deraadt default:
225 1.9 christos return FALSE;
226 1.1 deraadt }
227 1.7 jtc /* NOTREACHED */
228 1.1 deraadt }
229 1.1 deraadt
230 1.1 deraadt bool_t
231 1.1 deraadt xdr_ypresp_val(xdrs, objp)
232 1.12 thorpej XDR *xdrs;
233 1.12 thorpej struct ypresp_val *objp;
234 1.1 deraadt {
235 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
236 1.9 christos return FALSE;
237 1.12 thorpej
238 1.13 christos if (!xdr_datum(xdrs, &objp->valdat))
239 1.13 christos return FALSE;
240 1.13 christos
241 1.13 christos return TRUE;
242 1.1 deraadt }
243 1.1 deraadt
244 1.1 deraadt bool_t
245 1.1 deraadt xdr_ypbind_setdom(xdrs, objp)
246 1.12 thorpej XDR *xdrs;
247 1.12 thorpej struct ypbind_setdom *objp;
248 1.1 deraadt {
249 1.12 thorpej char *cp = objp->ypsetdom_domain;
250 1.12 thorpej
251 1.13 christos if (!xdr_ypdomain_wrap_string(xdrs, &cp))
252 1.9 christos return FALSE;
253 1.12 thorpej
254 1.12 thorpej if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding))
255 1.9 christos return FALSE;
256 1.12 thorpej
257 1.14 thorpej if (!xdr_u_int(xdrs, &objp->ypsetdom_vers))
258 1.13 christos return FALSE;
259 1.13 christos
260 1.13 christos return TRUE;
261 1.1 deraadt }
262 1.1 deraadt
263 1.1 deraadt bool_t
264 1.1 deraadt xdr_ypresp_key_val(xdrs, objp)
265 1.12 thorpej XDR *xdrs;
266 1.12 thorpej struct ypresp_key_val *objp;
267 1.1 deraadt {
268 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
269 1.9 christos return FALSE;
270 1.12 thorpej
271 1.12 thorpej if (!xdr_datum(xdrs, &objp->valdat))
272 1.9 christos return FALSE;
273 1.12 thorpej
274 1.13 christos if (!xdr_datum(xdrs, &objp->keydat))
275 1.13 christos return FALSE;
276 1.13 christos
277 1.13 christos return TRUE;
278 1.1 deraadt }
279 1.1 deraadt
280 1.1 deraadt bool_t
281 1.12 thorpej xdr_ypall(xdrs, incallback)
282 1.12 thorpej XDR *xdrs;
283 1.12 thorpej struct ypall_callback *incallback;
284 1.12 thorpej {
285 1.12 thorpej struct ypresp_key_val out;
286 1.12 thorpej char key[YPMAXRECORD], val[YPMAXRECORD];
287 1.12 thorpej bool_t more, status;
288 1.1 deraadt
289 1.12 thorpej /*
290 1.12 thorpej * Set up key/val struct to be used during the transaction.
291 1.12 thorpej */
292 1.7 jtc memset(&out, 0, sizeof out);
293 1.12 thorpej out.keydat.dptr = key;
294 1.12 thorpej out.keydat.dsize = sizeof(key);
295 1.12 thorpej out.valdat.dptr = val;
296 1.12 thorpej out.valdat.dsize = sizeof(val);
297 1.12 thorpej
298 1.12 thorpej for (;;) {
299 1.12 thorpej /* Values pending? */
300 1.12 thorpej if (!xdr_bool(xdrs, &more))
301 1.12 thorpej return FALSE; /* can't tell! */
302 1.13 christos if (!more)
303 1.12 thorpej return TRUE; /* no more */
304 1.12 thorpej
305 1.12 thorpej /* Transfer key/value pair. */
306 1.12 thorpej status = xdr_ypresp_key_val(xdrs, &out);
307 1.12 thorpej
308 1.12 thorpej /*
309 1.12 thorpej * If we succeeded, call the callback function.
310 1.12 thorpej * The callback will return TRUE when it wants
311 1.12 thorpej * no more values. If we fail, indicate the
312 1.12 thorpej * error.
313 1.12 thorpej */
314 1.13 christos if (status) {
315 1.12 thorpej if ((*incallback->foreach)(out.status,
316 1.12 thorpej (char *)out.keydat.dptr, out.keydat.dsize,
317 1.12 thorpej (char *)out.valdat.dptr, out.valdat.dsize,
318 1.12 thorpej incallback->data))
319 1.12 thorpej return TRUE;
320 1.12 thorpej } else
321 1.1 deraadt return FALSE;
322 1.1 deraadt }
323 1.1 deraadt }
324 1.1 deraadt
325 1.1 deraadt bool_t
326 1.1 deraadt xdr_ypresp_master(xdrs, objp)
327 1.12 thorpej XDR *xdrs;
328 1.12 thorpej struct ypresp_master *objp;
329 1.1 deraadt {
330 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
331 1.9 christos return FALSE;
332 1.12 thorpej
333 1.13 christos if (!xdr_string(xdrs, &objp->master, YPMAXPEER))
334 1.13 christos return FALSE;
335 1.13 christos
336 1.13 christos return TRUE;
337 1.1 deraadt }
338 1.1 deraadt
339 1.12 thorpej static bool_t
340 1.1 deraadt xdr_ypmaplist_str(xdrs, objp)
341 1.12 thorpej XDR *xdrs;
342 1.12 thorpej char *objp;
343 1.1 deraadt {
344 1.7 jtc return xdr_string(xdrs, &objp, YPMAXMAP+1);
345 1.1 deraadt }
346 1.1 deraadt
347 1.1 deraadt bool_t
348 1.1 deraadt xdr_ypmaplist(xdrs, objp)
349 1.12 thorpej XDR *xdrs;
350 1.12 thorpej struct ypmaplist *objp;
351 1.1 deraadt {
352 1.12 thorpej if (!xdr_ypmaplist_str(xdrs, objp->ypml_name))
353 1.9 christos return FALSE;
354 1.12 thorpej
355 1.13 christos if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
356 1.13 christos sizeof(struct ypmaplist), xdr_ypmaplist))
357 1.13 christos return FALSE;
358 1.13 christos
359 1.13 christos return TRUE;
360 1.1 deraadt }
361 1.1 deraadt
362 1.1 deraadt bool_t
363 1.1 deraadt xdr_ypresp_maplist(xdrs, objp)
364 1.12 thorpej XDR *xdrs;
365 1.12 thorpej struct ypresp_maplist *objp;
366 1.1 deraadt {
367 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
368 1.9 christos return FALSE;
369 1.12 thorpej
370 1.13 christos if (!xdr_pointer(xdrs, (caddr_t *)&objp->list,
371 1.13 christos sizeof(struct ypmaplist), xdr_ypmaplist))
372 1.13 christos return FALSE;
373 1.13 christos
374 1.13 christos return TRUE;
375 1.1 deraadt }
376 1.1 deraadt
377 1.1 deraadt bool_t
378 1.1 deraadt xdr_ypresp_order(xdrs, objp)
379 1.12 thorpej XDR *xdrs;
380 1.12 thorpej struct ypresp_order *objp;
381 1.1 deraadt {
382 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
383 1.9 christos return FALSE;
384 1.12 thorpej
385 1.14 thorpej if (!xdr_u_int(xdrs, &objp->ordernum))
386 1.13 christos return FALSE;
387 1.13 christos
388 1.13 christos return TRUE;
389 1.12 thorpej }
390 1.12 thorpej
391 1.12 thorpej bool_t
392 1.12 thorpej xdr_ypreq_xfr(xdrs, objp)
393 1.12 thorpej XDR *xdrs;
394 1.12 thorpej struct ypreq_xfr *objp;
395 1.12 thorpej {
396 1.13 christos if (!xdr_ypmap_parms(xdrs, &objp->map_parms))
397 1.12 thorpej return FALSE;
398 1.12 thorpej
399 1.14 thorpej if (!xdr_u_int(xdrs, &objp->transid))
400 1.12 thorpej return FALSE;
401 1.12 thorpej
402 1.14 thorpej if (!xdr_u_int(xdrs, &objp->proto))
403 1.12 thorpej return FALSE;
404 1.12 thorpej
405 1.14 thorpej if (!xdr_u_int(xdrs, &objp->port))
406 1.12 thorpej return FALSE;
407 1.12 thorpej
408 1.12 thorpej return TRUE;
409 1.12 thorpej }
410 1.12 thorpej
411 1.12 thorpej bool_t
412 1.12 thorpej xdr_ypmap_parms(xdrs, objp)
413 1.12 thorpej XDR *xdrs;
414 1.12 thorpej struct ypmap_parms *objp;
415 1.12 thorpej {
416 1.15 chuck if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
417 1.12 thorpej return FALSE;
418 1.12 thorpej
419 1.15 chuck if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
420 1.12 thorpej return FALSE;
421 1.12 thorpej
422 1.14 thorpej if (!xdr_u_int(xdrs, &objp->ordernum))
423 1.12 thorpej return FALSE;
424 1.12 thorpej
425 1.13 christos if (!xdr_ypowner_wrap_string(xdrs, &objp->owner))
426 1.12 thorpej return FALSE;
427 1.12 thorpej
428 1.12 thorpej return TRUE;
429 1.12 thorpej }
430 1.12 thorpej
431 1.12 thorpej bool_t
432 1.12 thorpej xdr_yppushresp_xfr(xdrs, objp)
433 1.12 thorpej XDR *xdrs;
434 1.12 thorpej struct yppushresp_xfr *objp;
435 1.12 thorpej {
436 1.14 thorpej if (!xdr_u_int(xdrs, &objp->transid))
437 1.12 thorpej return FALSE;
438 1.12 thorpej
439 1.14 thorpej if (!xdr_enum(xdrs, (enum_t *)&objp->status))
440 1.12 thorpej return FALSE;
441 1.12 thorpej
442 1.12 thorpej return TRUE;
443 1.1 deraadt }
444