xref: /titanic_52/usr/src/lib/libnsl/yp/yp_xdr.c (revision 8ad60789b9913d5b2118fe76d8cd7a661d8ae8b6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /*	  All Rights Reserved   */
30 
31 /*
32  * Portions of this source code were derived from Berkeley
33  * under license from the Regents of the University of
34  * California.
35  */
36 
37 #pragma ident	"%Z%%M%	%I%	%E% SMI"
38 
39 /*
40  * This contains ALL xdr routines used by the YP rpc interface.
41  */
42 
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <rpc/rpc.h>
46 #include "yp_b.h"
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
49 #include <sys/types.h>
50 #include <limits.h>
51 
52 static bool xdr_ypmaplist(XDR *, struct ypmaplist **);
53 static bool xdr_ypmaplist_wrap_string(XDR *, char *);
54 
55 typedef struct xdr_discrim XDR_DISCRIM;
56 extern bool xdr_ypreq_key(XDR *, struct ypreq_key *);
57 extern bool xdr_ypreq_nokey(XDR *, struct ypreq_nokey *);
58 extern bool xdr_ypresp_val(XDR *, struct ypresp_val *);
59 extern bool xdr_ypresp_key_val(XDR *, struct ypresp_key_val *);
60 extern bool xdr_ypmap_parms(XDR *, struct ypmap_parms *);
61 extern bool xdr_ypowner_wrap_string(XDR *, char **);
62 extern bool xdr_ypreq_newname_string(XDR *, char **);
63 
64 
65 /*
66  * Serializes/deserializes a dbm datum data structure.
67  */
68 bool
69 xdr_datum(XDR *xdrs, datum *pdatum)
70 {
71 	bool res;
72 	uint_t dsize;
73 
74 	/*
75 	 * LP64 case :
76 	 * xdr_bytes() expects a uint_t for the 3rd argument. Since
77 	 * datum.dsize is a long, we need a new temporary to pass to
78 	 * xdr_bytes()
79 	 */
80 	if (xdrs->x_op == XDR_ENCODE) {
81 		if (pdatum->dsize > UINT_MAX)
82 			return (FALSE);
83 	}
84 	dsize = (uint_t)pdatum->dsize;
85 	res = (bool)xdr_bytes(xdrs, (char **)&(pdatum->dptr), &dsize,
86 								YPMAXRECORD);
87 	if (xdrs->x_op == XDR_DECODE) {
88 		pdatum->dsize = dsize;
89 	}
90 
91 	return (res);
92 }
93 
94 
95 /*
96  * Serializes/deserializes a domain name string.  This is a "wrapper" for
97  * xdr_string which knows about the maximum domain name size.
98  */
99 bool
100 xdr_ypdomain_wrap_string(XDR *xdrs, char **ppstring)
101 {
102 	return ((bool)xdr_string(xdrs, ppstring, YPMAXDOMAIN));
103 }
104 
105 /*
106  * Serializes/deserializes a map name string.  This is a "wrapper" for
107  * xdr_string which knows about the maximum map name size.
108  */
109 bool
110 xdr_ypmap_wrap_string(XDR *xdrs, char **ppstring)
111 {
112 	return ((bool)xdr_string(xdrs, ppstring, YPMAXMAP));
113 }
114 
115 /*
116  * Serializes/deserializes a ypreq_key structure.
117  */
118 bool
119 xdr_ypreq_key(XDR *xdrs, struct ypreq_key *ps)
120 {
121 	return ((bool)(xdr_ypdomain_wrap_string(xdrs, &ps->domain) &&
122 		    xdr_ypmap_wrap_string(xdrs, &ps->map) &&
123 		    xdr_datum(xdrs, &ps->keydat)));
124 }
125 
126 /*
127  * Serializes/deserializes a ypreq_nokey structure.
128  */
129 bool
130 xdr_ypreq_nokey(XDR *xdrs, struct ypreq_nokey *ps)
131 {
132 	return ((bool)(xdr_ypdomain_wrap_string(xdrs, &ps->domain) &&
133 		    xdr_ypmap_wrap_string(xdrs, &ps->map)));
134 }
135 
136 /*
137  * Serializes/deserializes a ypresp_val structure.
138  */
139 bool
140 xdr_ypresp_val(XDR *xdrs, struct ypresp_val *ps)
141 {
142 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
143 		    xdr_datum(xdrs, &ps->valdat)));
144 }
145 
146 /*
147  * Serializes/deserializes a ypresp_key_val structure.
148  */
149 bool
150 xdr_ypresp_key_val(XDR *xdrs, struct ypresp_key_val *ps)
151 {
152 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
153 	    xdr_datum(xdrs, &ps->valdat) &&
154 	    xdr_datum(xdrs, &ps->keydat)));
155 }
156 
157 /*
158  * Serializes/deserializes a peer server's node name
159  */
160 bool
161 xdr_ypowner_wrap_string(XDR *xdrs, char **ppstring)
162 {
163 	return ((bool)xdr_string(xdrs, ppstring, YPMAXPEER));
164 }
165 
166 /*
167  * Serializes/deserializes a ypmap_parms structure.
168  */
169 bool
170 xdr_ypmap_parms(XDR *xdrs, struct ypmap_parms *ps)
171 {
172 	return ((bool)(xdr_ypdomain_wrap_string(xdrs, &ps->domain) &&
173 	    xdr_ypmap_wrap_string(xdrs, &ps->map) &&
174 	    xdr_u_int(xdrs, &ps->ordernum) &&
175 	    xdr_ypowner_wrap_string(xdrs, &ps->owner)));
176 }
177 
178 /*
179  * Serializes/deserializes a ypreq_newxfr name
180  */
181 bool
182 xdr_ypreq_newname_string(XDR *xdrs, char **ppstring)
183 {
184 	return ((bool)xdr_string(xdrs, ppstring, 256));
185 }
186 
187 /*
188  * Serializes/deserializes a ypresp_master structure.
189  */
190 bool
191 xdr_ypresp_master(XDR *xdrs, struct ypresp_master *ps)
192 {
193 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
194 	    xdr_ypowner_wrap_string(xdrs, &ps->master)));
195 }
196 
197 /*
198  * Serializes/deserializes a ypresp_order structure.
199  */
200 bool
201 xdr_ypresp_order(XDR *xdrs, struct ypresp_order *ps)
202 {
203 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
204 	    xdr_u_int(xdrs, &ps->ordernum)));
205 }
206 
207 /*
208  * This is like xdr_ypmap_wrap_string except that it serializes/deserializes
209  * an array, instead of a pointer, so xdr_reference can work on the structure
210  * containing the char array itself.
211  */
212 static bool
213 xdr_ypmaplist_wrap_string(XDR *xdrs, char *pstring)
214 {
215 	char *s;
216 
217 	s = pstring;
218 	return ((bool)xdr_string(xdrs, &s, YPMAXMAP));
219 }
220 
221 /*
222  * Serializes/deserializes a ypmaplist.
223  */
224 static bool
225 xdr_ypmaplist(XDR *xdrs, struct ypmaplist **lst)
226 {
227 	bool_t more_elements;
228 	int freeing = (xdrs->x_op == XDR_FREE);
229 	struct ypmaplist **next;
230 
231 	for (;;) {
232 		more_elements = (*lst != NULL);
233 
234 		if (!xdr_bool(xdrs, &more_elements))
235 			return (FALSE);
236 
237 		if (!more_elements)
238 			return (TRUE);  /* All done */
239 
240 		if (freeing)
241 			next = &((*lst)->ypml_next);
242 
243 		if (!xdr_reference(xdrs, (caddr_t *)lst,
244 			(uint_t)sizeof (struct ypmaplist),
245 		    (xdrproc_t)xdr_ypmaplist_wrap_string))
246 			return (FALSE);
247 
248 		lst = (freeing) ? next : &((*lst)->ypml_next);
249 	}
250 	/*NOTREACHED*/
251 }
252 
253 /*
254  * Serializes/deserializes a ypresp_maplist.
255  */
256 bool
257 xdr_ypresp_maplist(XDR *xdrs, struct ypresp_maplist *ps)
258 {
259 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
260 		xdr_ypmaplist(xdrs, &ps->list)));
261 }
262 
263 /*
264  * Serializes/deserializes a yppushresp_xfr structure.
265  */
266 bool
267 xdr_yppushresp_xfr(XDR *xdrs, struct yppushresp_xfr *ps)
268 {
269 	return ((bool)(xdr_u_int(xdrs, &ps->transid) &&
270 	    xdr_u_int(xdrs, &ps->status)));
271 }
272 
273 
274 /*
275  * Serializes/deserializes a ypreq_xfr structure.
276  */
277 bool
278 xdr_ypreq_newxfr(XDR *xdrs, struct ypreq_newxfr *ps)
279 {
280 	return ((bool)(xdr_ypmap_parms(xdrs, &ps->map_parms) &&
281 	    xdr_u_int(xdrs, &ps->transid) &&
282 	    xdr_u_int(xdrs, &ps->proto) &&
283 	    xdr_string(xdrs, &ps->name, 256)));
284 }
285 
286 /*
287  * Serializes/deserializes a ypreq_xfr structure.
288  */
289 bool
290 xdr_ypreq_xfr(XDR *xdrs, struct ypreq_xfr *ps)
291 {
292 	return ((bool)(xdr_ypmap_parms(xdrs, &ps->map_parms) &&
293 	    xdr_u_int(xdrs, &ps->transid) &&
294 	    xdr_u_int(xdrs, &ps->proto) &&
295 	    xdr_u_short(xdrs, &ps->port)));
296 }
297 
298 
299 /*
300  * Serializes/deserializes a stream of struct ypresp_key_val's.  This is used
301  * only by the client side of the batch enumerate operation.
302  */
303 bool
304 xdr_ypall(XDR *xdrs, struct ypall_callback *callback)
305 {
306 	bool_t more;
307 	struct ypresp_key_val kv;
308 	char keybuf[YPMAXRECORD];
309 	char valbuf[YPMAXRECORD];
310 
311 	if (xdrs->x_op == XDR_ENCODE)
312 		return (FALSE);
313 
314 	if (xdrs->x_op == XDR_FREE)
315 		return (TRUE);
316 
317 	kv.keydat.dptr = keybuf;
318 	kv.valdat.dptr = valbuf;
319 	kv.keydat.dsize = YPMAXRECORD;
320 	kv.valdat.dsize = YPMAXRECORD;
321 
322 	for (;;) {
323 		if (!xdr_bool(xdrs, &more))
324 			return (FALSE);
325 
326 		if (!more)
327 			return (TRUE);
328 
329 		if (!xdr_ypresp_key_val(xdrs, &kv))
330 			return (FALSE);
331 		if ((*callback->foreach)(kv.status, kv.keydat.dptr,
332 			    kv.keydat.dsize, kv.valdat.dptr, kv.valdat.dsize,
333 			    callback->data))
334 			return (TRUE);
335 	}
336 }
337 
338 bool_t
339 xdr_netconfig(XDR *xdrs, struct netconfig *objp)
340 {
341 	if (!xdr_string(xdrs, &objp->nc_netid, ~0))
342 		return (FALSE);
343 	if (!xdr_u_int(xdrs, &objp->nc_semantics))
344 		return (FALSE);
345 	if (!xdr_u_int(xdrs, &objp->nc_flag))
346 		return (FALSE);
347 	if (!xdr_string(xdrs, &objp->nc_protofmly, ~0))
348 		return (FALSE);
349 	if (!xdr_string(xdrs, &objp->nc_proto, ~0))
350 		return (FALSE);
351 	if (!xdr_string(xdrs, &objp->nc_device, ~0))
352 		return (FALSE);
353 	if (!xdr_array(xdrs, (char **)&objp->nc_lookups,
354 		(uint_t *)&objp->nc_nlookups, 100, sizeof (char *),
355 		xdr_wrapstring))
356 		return (FALSE);
357 	return ((bool)xdr_vector(xdrs, (char *)objp->nc_unused,
358 		8, sizeof (uint_t), xdr_u_int));
359 }
360