xdryp.c revision 1.18 1 1.18 christos /* $NetBSD: xdryp.c,v 1.18 1997/10/16 23:22:31 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.18 christos __RCSID("$NetBSD: xdryp.c,v 1.18 1997/10/16 23:22:31 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.17 jtc #include "namespace.h"
52 1.1 deraadt #include <sys/param.h>
53 1.1 deraadt #include <sys/types.h>
54 1.1 deraadt #include <sys/socket.h>
55 1.1 deraadt #include <stdio.h>
56 1.7 jtc #include <stdlib.h>
57 1.7 jtc #include <string.h>
58 1.1 deraadt #include <ctype.h>
59 1.1 deraadt #include <rpc/rpc.h>
60 1.1 deraadt #include <rpc/xdr.h>
61 1.1 deraadt #include <rpcsvc/yp_prot.h>
62 1.1 deraadt #include <rpcsvc/ypclnt.h>
63 1.17 jtc
64 1.17 jtc #ifdef __weak_alias
65 1.17 jtc __weak_alias(xdr_datum,_xdr_datum);
66 1.17 jtc __weak_alias(xdr_domainname,_xdr_domainname);
67 1.17 jtc __weak_alias(xdr_mapname,_xdr_mapname);
68 1.17 jtc __weak_alias(xdr_peername,_xdr_peername);
69 1.17 jtc __weak_alias(xdr_yp_inaddr,_xdr_yp_inaddr);
70 1.17 jtc __weak_alias(xdr_ypall,_xdr_ypall);
71 1.17 jtc __weak_alias(xdr_ypbind_resp,_xdr_ypbind_resp);
72 1.17 jtc __weak_alias(xdr_ypbind_setdom,_xdr_ypbind_setdom);
73 1.17 jtc __weak_alias(xdr_ypdomain_wrap_string,_xdr_ypdomain_wrap_string);
74 1.17 jtc __weak_alias(xdr_ypmap_parms,_xdr_ypmap_parms);
75 1.17 jtc __weak_alias(xdr_ypmap_wrap_string,_xdr_ypmap_wrap_string);
76 1.17 jtc __weak_alias(xdr_ypmaplist,_xdr_ypmaplist);
77 1.17 jtc __weak_alias(xdr_ypowner_wrap_string,_xdr_ypowner_wrap_string);
78 1.17 jtc __weak_alias(xdr_yppushresp_xfr,_xdr_yppushresp_xfr);
79 1.17 jtc __weak_alias(xdr_ypreq_key,_xdr_ypreq_key);
80 1.17 jtc __weak_alias(xdr_ypreq_nokey,_xdr_ypreq_nokey);
81 1.17 jtc __weak_alias(xdr_ypreq_xfr,_xdr_ypreq_xfr);
82 1.17 jtc __weak_alias(xdr_ypresp_key_val,_xdr_ypresp_key_val);
83 1.17 jtc __weak_alias(xdr_ypresp_maplist,_xdr_ypresp_maplist);
84 1.17 jtc __weak_alias(xdr_ypresp_master,_xdr_ypresp_master);
85 1.17 jtc __weak_alias(xdr_ypresp_order,_xdr_ypresp_order);
86 1.17 jtc __weak_alias(xdr_ypresp_val,_xdr_ypresp_val);
87 1.17 jtc #endif
88 1.1 deraadt
89 1.12 thorpej /*
90 1.12 thorpej * Functions used only within this file.
91 1.12 thorpej */
92 1.12 thorpej static bool_t xdr_ypbind_binding __P((XDR *, struct ypbind_binding *));
93 1.12 thorpej static bool_t xdr_ypbind_resptype __P((XDR *, enum ypbind_resptype *));
94 1.12 thorpej static bool_t xdr_ypstat __P((XDR *, enum ypbind_resptype *));
95 1.12 thorpej static bool_t xdr_ypmaplist_str __P((XDR *, char *));
96 1.12 thorpej
97 1.12 thorpej bool_t
98 1.12 thorpej xdr_ypdomain_wrap_string(xdrs, objp)
99 1.12 thorpej XDR *xdrs;
100 1.12 thorpej char **objp;
101 1.12 thorpej {
102 1.12 thorpej return xdr_string(xdrs, objp, YPMAXDOMAIN);
103 1.12 thorpej }
104 1.12 thorpej
105 1.12 thorpej bool_t
106 1.12 thorpej xdr_ypmap_wrap_string(xdrs, objp)
107 1.12 thorpej XDR *xdrs;
108 1.12 thorpej char **objp;
109 1.12 thorpej {
110 1.12 thorpej return xdr_string(xdrs, objp, YPMAXMAP);
111 1.12 thorpej }
112 1.12 thorpej
113 1.1 deraadt bool_t
114 1.12 thorpej xdr_ypowner_wrap_string(xdrs, objp)
115 1.12 thorpej XDR *xdrs;
116 1.12 thorpej char **objp;
117 1.1 deraadt {
118 1.12 thorpej return xdr_string(xdrs, objp, YPMAXPEER);
119 1.1 deraadt }
120 1.1 deraadt
121 1.1 deraadt bool_t
122 1.12 thorpej xdr_datum(xdrs, objp)
123 1.12 thorpej XDR *xdrs;
124 1.12 thorpej datum *objp;
125 1.1 deraadt {
126 1.12 thorpej return xdr_bytes(xdrs, (char **)&objp->dptr,
127 1.12 thorpej (u_int *)&objp->dsize, YPMAXRECORD);
128 1.1 deraadt }
129 1.1 deraadt
130 1.1 deraadt bool_t
131 1.1 deraadt xdr_ypreq_key(xdrs, objp)
132 1.12 thorpej XDR *xdrs;
133 1.12 thorpej struct ypreq_key *objp;
134 1.1 deraadt {
135 1.13 christos if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
136 1.9 christos return FALSE;
137 1.12 thorpej
138 1.13 christos if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
139 1.9 christos return FALSE;
140 1.12 thorpej
141 1.13 christos if (!xdr_datum(xdrs, &objp->keydat))
142 1.13 christos return FALSE;
143 1.13 christos
144 1.13 christos return TRUE;
145 1.1 deraadt }
146 1.1 deraadt
147 1.1 deraadt bool_t
148 1.1 deraadt xdr_ypreq_nokey(xdrs, objp)
149 1.12 thorpej XDR *xdrs;
150 1.12 thorpej struct ypreq_nokey *objp;
151 1.1 deraadt {
152 1.13 christos if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
153 1.13 christos return FALSE;
154 1.13 christos
155 1.13 christos if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
156 1.9 christos return FALSE;
157 1.12 thorpej
158 1.13 christos return TRUE;
159 1.1 deraadt }
160 1.1 deraadt
161 1.1 deraadt bool_t
162 1.1 deraadt xdr_yp_inaddr(xdrs, objp)
163 1.12 thorpej XDR *xdrs;
164 1.12 thorpej struct in_addr *objp;
165 1.1 deraadt {
166 1.7 jtc return xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr);
167 1.1 deraadt }
168 1.1 deraadt
169 1.12 thorpej static bool_t
170 1.1 deraadt xdr_ypbind_binding(xdrs, objp)
171 1.12 thorpej XDR *xdrs;
172 1.12 thorpej struct ypbind_binding *objp;
173 1.1 deraadt {
174 1.12 thorpej if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr))
175 1.9 christos return FALSE;
176 1.12 thorpej
177 1.13 christos if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
178 1.13 christos sizeof objp->ypbind_binding_port))
179 1.13 christos return FALSE;
180 1.13 christos
181 1.13 christos return TRUE;
182 1.1 deraadt }
183 1.1 deraadt
184 1.12 thorpej static bool_t
185 1.1 deraadt xdr_ypbind_resptype(xdrs, objp)
186 1.12 thorpej XDR *xdrs;
187 1.12 thorpej enum ypbind_resptype *objp;
188 1.1 deraadt {
189 1.7 jtc return xdr_enum(xdrs, (enum_t *)objp);
190 1.1 deraadt }
191 1.1 deraadt
192 1.12 thorpej static bool_t
193 1.1 deraadt xdr_ypstat(xdrs, objp)
194 1.12 thorpej XDR *xdrs;
195 1.12 thorpej enum ypbind_resptype *objp;
196 1.1 deraadt {
197 1.7 jtc return xdr_enum(xdrs, (enum_t *)objp);
198 1.1 deraadt }
199 1.1 deraadt
200 1.1 deraadt bool_t
201 1.1 deraadt xdr_ypbind_resp(xdrs, objp)
202 1.12 thorpej XDR *xdrs;
203 1.12 thorpej struct ypbind_resp *objp;
204 1.1 deraadt {
205 1.12 thorpej if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status))
206 1.9 christos return FALSE;
207 1.9 christos
208 1.1 deraadt switch (objp->ypbind_status) {
209 1.1 deraadt case YPBIND_FAIL_VAL:
210 1.9 christos return xdr_u_int(xdrs,
211 1.12 thorpej (u_int *)&objp->ypbind_respbody.ypbind_error);
212 1.12 thorpej
213 1.1 deraadt case YPBIND_SUCC_VAL:
214 1.12 thorpej return xdr_ypbind_binding(xdrs,
215 1.12 thorpej &objp->ypbind_respbody.ypbind_bindinfo);
216 1.12 thorpej
217 1.1 deraadt default:
218 1.9 christos return FALSE;
219 1.1 deraadt }
220 1.7 jtc /* NOTREACHED */
221 1.1 deraadt }
222 1.1 deraadt
223 1.1 deraadt bool_t
224 1.1 deraadt xdr_ypresp_val(xdrs, objp)
225 1.12 thorpej XDR *xdrs;
226 1.12 thorpej struct ypresp_val *objp;
227 1.1 deraadt {
228 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
229 1.9 christos return FALSE;
230 1.12 thorpej
231 1.13 christos if (!xdr_datum(xdrs, &objp->valdat))
232 1.13 christos return FALSE;
233 1.13 christos
234 1.13 christos return TRUE;
235 1.1 deraadt }
236 1.1 deraadt
237 1.1 deraadt bool_t
238 1.1 deraadt xdr_ypbind_setdom(xdrs, objp)
239 1.12 thorpej XDR *xdrs;
240 1.12 thorpej struct ypbind_setdom *objp;
241 1.1 deraadt {
242 1.12 thorpej char *cp = objp->ypsetdom_domain;
243 1.12 thorpej
244 1.13 christos if (!xdr_ypdomain_wrap_string(xdrs, &cp))
245 1.9 christos return FALSE;
246 1.12 thorpej
247 1.12 thorpej if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding))
248 1.9 christos return FALSE;
249 1.12 thorpej
250 1.14 thorpej if (!xdr_u_int(xdrs, &objp->ypsetdom_vers))
251 1.13 christos return FALSE;
252 1.13 christos
253 1.13 christos return TRUE;
254 1.1 deraadt }
255 1.1 deraadt
256 1.1 deraadt bool_t
257 1.1 deraadt xdr_ypresp_key_val(xdrs, objp)
258 1.12 thorpej XDR *xdrs;
259 1.12 thorpej struct ypresp_key_val *objp;
260 1.1 deraadt {
261 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
262 1.9 christos return FALSE;
263 1.12 thorpej
264 1.12 thorpej if (!xdr_datum(xdrs, &objp->valdat))
265 1.9 christos return FALSE;
266 1.12 thorpej
267 1.13 christos if (!xdr_datum(xdrs, &objp->keydat))
268 1.13 christos return FALSE;
269 1.13 christos
270 1.13 christos return TRUE;
271 1.1 deraadt }
272 1.1 deraadt
273 1.1 deraadt bool_t
274 1.12 thorpej xdr_ypall(xdrs, incallback)
275 1.12 thorpej XDR *xdrs;
276 1.12 thorpej struct ypall_callback *incallback;
277 1.12 thorpej {
278 1.12 thorpej struct ypresp_key_val out;
279 1.12 thorpej char key[YPMAXRECORD], val[YPMAXRECORD];
280 1.12 thorpej bool_t more, status;
281 1.1 deraadt
282 1.12 thorpej /*
283 1.12 thorpej * Set up key/val struct to be used during the transaction.
284 1.12 thorpej */
285 1.7 jtc memset(&out, 0, sizeof out);
286 1.12 thorpej out.keydat.dptr = key;
287 1.12 thorpej out.keydat.dsize = sizeof(key);
288 1.12 thorpej out.valdat.dptr = val;
289 1.12 thorpej out.valdat.dsize = sizeof(val);
290 1.12 thorpej
291 1.12 thorpej for (;;) {
292 1.12 thorpej /* Values pending? */
293 1.12 thorpej if (!xdr_bool(xdrs, &more))
294 1.12 thorpej return FALSE; /* can't tell! */
295 1.13 christos if (!more)
296 1.12 thorpej return TRUE; /* no more */
297 1.12 thorpej
298 1.12 thorpej /* Transfer key/value pair. */
299 1.12 thorpej status = xdr_ypresp_key_val(xdrs, &out);
300 1.12 thorpej
301 1.12 thorpej /*
302 1.12 thorpej * If we succeeded, call the callback function.
303 1.12 thorpej * The callback will return TRUE when it wants
304 1.12 thorpej * no more values. If we fail, indicate the
305 1.12 thorpej * error.
306 1.12 thorpej */
307 1.13 christos if (status) {
308 1.12 thorpej if ((*incallback->foreach)(out.status,
309 1.12 thorpej (char *)out.keydat.dptr, out.keydat.dsize,
310 1.12 thorpej (char *)out.valdat.dptr, out.valdat.dsize,
311 1.12 thorpej incallback->data))
312 1.12 thorpej return TRUE;
313 1.12 thorpej } else
314 1.1 deraadt return FALSE;
315 1.1 deraadt }
316 1.1 deraadt }
317 1.1 deraadt
318 1.1 deraadt bool_t
319 1.1 deraadt xdr_ypresp_master(xdrs, objp)
320 1.12 thorpej XDR *xdrs;
321 1.12 thorpej struct ypresp_master *objp;
322 1.1 deraadt {
323 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
324 1.9 christos return FALSE;
325 1.12 thorpej
326 1.13 christos if (!xdr_string(xdrs, &objp->master, YPMAXPEER))
327 1.13 christos return FALSE;
328 1.13 christos
329 1.13 christos return TRUE;
330 1.1 deraadt }
331 1.1 deraadt
332 1.12 thorpej static bool_t
333 1.1 deraadt xdr_ypmaplist_str(xdrs, objp)
334 1.12 thorpej XDR *xdrs;
335 1.12 thorpej char *objp;
336 1.1 deraadt {
337 1.7 jtc return xdr_string(xdrs, &objp, YPMAXMAP+1);
338 1.1 deraadt }
339 1.1 deraadt
340 1.1 deraadt bool_t
341 1.1 deraadt xdr_ypmaplist(xdrs, objp)
342 1.12 thorpej XDR *xdrs;
343 1.12 thorpej struct ypmaplist *objp;
344 1.1 deraadt {
345 1.12 thorpej if (!xdr_ypmaplist_str(xdrs, objp->ypml_name))
346 1.9 christos return FALSE;
347 1.12 thorpej
348 1.13 christos if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
349 1.13 christos sizeof(struct ypmaplist), xdr_ypmaplist))
350 1.13 christos return FALSE;
351 1.13 christos
352 1.13 christos return TRUE;
353 1.1 deraadt }
354 1.1 deraadt
355 1.1 deraadt bool_t
356 1.1 deraadt xdr_ypresp_maplist(xdrs, objp)
357 1.12 thorpej XDR *xdrs;
358 1.12 thorpej struct ypresp_maplist *objp;
359 1.1 deraadt {
360 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
361 1.9 christos return FALSE;
362 1.12 thorpej
363 1.13 christos if (!xdr_pointer(xdrs, (caddr_t *)&objp->list,
364 1.13 christos sizeof(struct ypmaplist), xdr_ypmaplist))
365 1.13 christos return FALSE;
366 1.13 christos
367 1.13 christos return TRUE;
368 1.1 deraadt }
369 1.1 deraadt
370 1.1 deraadt bool_t
371 1.1 deraadt xdr_ypresp_order(xdrs, objp)
372 1.12 thorpej XDR *xdrs;
373 1.12 thorpej struct ypresp_order *objp;
374 1.1 deraadt {
375 1.12 thorpej if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
376 1.9 christos return FALSE;
377 1.12 thorpej
378 1.14 thorpej if (!xdr_u_int(xdrs, &objp->ordernum))
379 1.13 christos return FALSE;
380 1.13 christos
381 1.13 christos return TRUE;
382 1.12 thorpej }
383 1.12 thorpej
384 1.12 thorpej bool_t
385 1.12 thorpej xdr_ypreq_xfr(xdrs, objp)
386 1.12 thorpej XDR *xdrs;
387 1.12 thorpej struct ypreq_xfr *objp;
388 1.12 thorpej {
389 1.13 christos if (!xdr_ypmap_parms(xdrs, &objp->map_parms))
390 1.12 thorpej return FALSE;
391 1.12 thorpej
392 1.14 thorpej if (!xdr_u_int(xdrs, &objp->transid))
393 1.12 thorpej return FALSE;
394 1.12 thorpej
395 1.14 thorpej if (!xdr_u_int(xdrs, &objp->proto))
396 1.12 thorpej return FALSE;
397 1.12 thorpej
398 1.14 thorpej if (!xdr_u_int(xdrs, &objp->port))
399 1.12 thorpej return FALSE;
400 1.12 thorpej
401 1.12 thorpej return TRUE;
402 1.12 thorpej }
403 1.12 thorpej
404 1.12 thorpej bool_t
405 1.12 thorpej xdr_ypmap_parms(xdrs, objp)
406 1.12 thorpej XDR *xdrs;
407 1.12 thorpej struct ypmap_parms *objp;
408 1.12 thorpej {
409 1.15 chuck if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
410 1.12 thorpej return FALSE;
411 1.12 thorpej
412 1.15 chuck if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
413 1.12 thorpej return FALSE;
414 1.12 thorpej
415 1.14 thorpej if (!xdr_u_int(xdrs, &objp->ordernum))
416 1.12 thorpej return FALSE;
417 1.12 thorpej
418 1.13 christos if (!xdr_ypowner_wrap_string(xdrs, &objp->owner))
419 1.12 thorpej return FALSE;
420 1.12 thorpej
421 1.12 thorpej return TRUE;
422 1.12 thorpej }
423 1.12 thorpej
424 1.12 thorpej bool_t
425 1.12 thorpej xdr_yppushresp_xfr(xdrs, objp)
426 1.12 thorpej XDR *xdrs;
427 1.12 thorpej struct yppushresp_xfr *objp;
428 1.12 thorpej {
429 1.14 thorpej if (!xdr_u_int(xdrs, &objp->transid))
430 1.12 thorpej return FALSE;
431 1.12 thorpej
432 1.14 thorpej if (!xdr_enum(xdrs, (enum_t *)&objp->status))
433 1.12 thorpej return FALSE;
434 1.12 thorpej
435 1.12 thorpej return TRUE;
436 1.1 deraadt }
437