xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_xdr.c (revision 1da57d55)
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 2006 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 /*
38  * This contains ALL xdr routines used by the YP rpc interface.
39  */
40 
41 #include "mt.h"
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <rpc/rpc.h>
45 #include "yp_b.h"
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
48 #include <sys/types.h>
49 #include <limits.h>
50 
51 static bool xdr_ypmaplist(XDR *, struct ypmaplist **);
52 static bool xdr_ypmaplist_wrap_string(XDR *, char *);
53 
54 typedef struct xdr_discrim XDR_DISCRIM;
55 extern bool xdr_ypreq_key(XDR *, struct ypreq_key *);
56 extern bool xdr_ypreq_nokey(XDR *, struct ypreq_nokey *);
57 extern bool xdr_ypresp_val(XDR *, struct ypresp_val *);
58 extern bool xdr_ypresp_key_val(XDR *, struct ypresp_key_val *);
59 extern bool xdr_ypmap_parms(XDR *, struct ypmap_parms *);
60 extern bool xdr_ypowner_wrap_string(XDR *, char **);
61 extern bool xdr_ypreq_newname_string(XDR *, char **);
62 
63 
64 /*
65  * Serializes/deserializes a dbm datum data structure.
66  */
67 bool
xdr_datum(XDR * xdrs,datum * pdatum)68 xdr_datum(XDR *xdrs, datum *pdatum)
69 {
70 	bool res;
71 	uint_t dsize;
72 
73 	/*
74 	 * LP64 case :
75 	 * xdr_bytes() expects a uint_t for the 3rd argument. Since
76 	 * datum.dsize is a long, we need a new temporary to pass to
77 	 * xdr_bytes()
78 	 */
79 	if (xdrs->x_op == XDR_ENCODE) {
80 		if (pdatum->dsize > UINT_MAX)
81 			return (FALSE);
82 	}
83 	dsize = (uint_t)pdatum->dsize;
84 	res = (bool)xdr_bytes(xdrs, (char **)&(pdatum->dptr), &dsize,
85 								YPMAXRECORD);
86 	if (xdrs->x_op == XDR_DECODE) {
87 		pdatum->dsize = dsize;
88 	}
89 
90 	return (res);
91 }
92 
93 
94 /*
95  * Serializes/deserializes a domain name string.  This is a "wrapper" for
96  * xdr_string which knows about the maximum domain name size.
97  */
98 bool
xdr_ypdomain_wrap_string(XDR * xdrs,char ** ppstring)99 xdr_ypdomain_wrap_string(XDR *xdrs, char **ppstring)
100 {
101 	return ((bool)xdr_string(xdrs, ppstring, YPMAXDOMAIN));
102 }
103 
104 /*
105  * Serializes/deserializes a map name string.  This is a "wrapper" for
106  * xdr_string which knows about the maximum map name size.
107  */
108 bool
xdr_ypmap_wrap_string(XDR * xdrs,char ** ppstring)109 xdr_ypmap_wrap_string(XDR *xdrs, char **ppstring)
110 {
111 	return ((bool)xdr_string(xdrs, ppstring, YPMAXMAP));
112 }
113 
114 /*
115  * Serializes/deserializes a ypreq_key structure.
116  */
117 bool
xdr_ypreq_key(XDR * xdrs,struct ypreq_key * ps)118 xdr_ypreq_key(XDR *xdrs, struct ypreq_key *ps)
119 {
120 	return ((bool)(xdr_ypdomain_wrap_string(xdrs, &ps->domain) &&
121 		    xdr_ypmap_wrap_string(xdrs, &ps->map) &&
122 		    xdr_datum(xdrs, &ps->keydat)));
123 }
124 
125 /*
126  * Serializes/deserializes a ypreq_nokey structure.
127  */
128 bool
xdr_ypreq_nokey(XDR * xdrs,struct ypreq_nokey * ps)129 xdr_ypreq_nokey(XDR *xdrs, struct ypreq_nokey *ps)
130 {
131 	return ((bool)(xdr_ypdomain_wrap_string(xdrs, &ps->domain) &&
132 		    xdr_ypmap_wrap_string(xdrs, &ps->map)));
133 }
134 
135 /*
136  * Serializes/deserializes a ypresp_val structure.
137  */
138 bool
xdr_ypresp_val(XDR * xdrs,struct ypresp_val * ps)139 xdr_ypresp_val(XDR *xdrs, struct ypresp_val *ps)
140 {
141 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
142 		    xdr_datum(xdrs, &ps->valdat)));
143 }
144 
145 /*
146  * Serializes/deserializes a ypresp_key_val structure.
147  */
148 bool
xdr_ypresp_key_val(XDR * xdrs,struct ypresp_key_val * ps)149 xdr_ypresp_key_val(XDR *xdrs, struct ypresp_key_val *ps)
150 {
151 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
152 	    xdr_datum(xdrs, &ps->valdat) &&
153 	    xdr_datum(xdrs, &ps->keydat)));
154 }
155 
156 /*
157  * Serializes/deserializes a peer server's node name
158  */
159 bool
xdr_ypowner_wrap_string(XDR * xdrs,char ** ppstring)160 xdr_ypowner_wrap_string(XDR *xdrs, char **ppstring)
161 {
162 	return ((bool)xdr_string(xdrs, ppstring, YPMAXPEER));
163 }
164 
165 /*
166  * Serializes/deserializes a ypmap_parms structure.
167  */
168 bool
xdr_ypmap_parms(XDR * xdrs,struct ypmap_parms * ps)169 xdr_ypmap_parms(XDR *xdrs, struct ypmap_parms *ps)
170 {
171 	return ((bool)(xdr_ypdomain_wrap_string(xdrs, &ps->domain) &&
172 	    xdr_ypmap_wrap_string(xdrs, &ps->map) &&
173 	    xdr_u_int(xdrs, &ps->ordernum) &&
174 	    xdr_ypowner_wrap_string(xdrs, &ps->owner)));
175 }
176 
177 /*
178  * Serializes/deserializes a ypreq_newxfr name
179  */
180 bool
xdr_ypreq_newname_string(XDR * xdrs,char ** ppstring)181 xdr_ypreq_newname_string(XDR *xdrs, char **ppstring)
182 {
183 	return ((bool)xdr_string(xdrs, ppstring, 256));
184 }
185 
186 /*
187  * Serializes/deserializes a ypresp_master structure.
188  */
189 bool
xdr_ypresp_master(XDR * xdrs,struct ypresp_master * ps)190 xdr_ypresp_master(XDR *xdrs, struct ypresp_master *ps)
191 {
192 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
193 	    xdr_ypowner_wrap_string(xdrs, &ps->master)));
194 }
195 
196 /*
197  * Serializes/deserializes a ypresp_order structure.
198  */
199 bool
xdr_ypresp_order(XDR * xdrs,struct ypresp_order * ps)200 xdr_ypresp_order(XDR *xdrs, struct ypresp_order *ps)
201 {
202 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
203 	    xdr_u_int(xdrs, &ps->ordernum)));
204 }
205 
206 /*
207  * This is like xdr_ypmap_wrap_string except that it serializes/deserializes
208  * an array, instead of a pointer, so xdr_reference can work on the structure
209  * containing the char array itself.
210  */
211 static bool
xdr_ypmaplist_wrap_string(XDR * xdrs,char * pstring)212 xdr_ypmaplist_wrap_string(XDR *xdrs, char *pstring)
213 {
214 	char *s;
215 
216 	s = pstring;
217 	return ((bool)xdr_string(xdrs, &s, YPMAXMAP));
218 }
219 
220 /*
221  * Serializes/deserializes a ypmaplist.
222  */
223 static bool
xdr_ypmaplist(XDR * xdrs,struct ypmaplist ** lst)224 xdr_ypmaplist(XDR *xdrs, struct ypmaplist **lst)
225 {
226 	bool_t more_elements;
227 	int freeing = (xdrs->x_op == XDR_FREE);
228 	struct ypmaplist **next;
229 
230 	for (;;) {
231 		more_elements = (*lst != NULL);
232 
233 		if (!xdr_bool(xdrs, &more_elements))
234 			return (FALSE);
235 
236 		if (!more_elements)
237 			return (TRUE);  /* All done */
238 
239 		if (freeing)
240 			next = &((*lst)->ypml_next);
241 
242 		if (!xdr_reference(xdrs, (caddr_t *)lst,
243 			(uint_t)sizeof (struct ypmaplist),
244 		    (xdrproc_t)xdr_ypmaplist_wrap_string))
245 			return (FALSE);
246 
247 		lst = (freeing) ? next : &((*lst)->ypml_next);
248 	}
249 	/*NOTREACHED*/
250 }
251 
252 /*
253  * Serializes/deserializes a ypresp_maplist.
254  */
255 bool
xdr_ypresp_maplist(XDR * xdrs,struct ypresp_maplist * ps)256 xdr_ypresp_maplist(XDR *xdrs, struct ypresp_maplist *ps)
257 {
258 	return ((bool)(xdr_u_int(xdrs, &ps->status) &&
259 		xdr_ypmaplist(xdrs, &ps->list)));
260 }
261 
262 /*
263  * Serializes/deserializes a yppushresp_xfr structure.
264  */
265 bool
xdr_yppushresp_xfr(XDR * xdrs,struct yppushresp_xfr * ps)266 xdr_yppushresp_xfr(XDR *xdrs, struct yppushresp_xfr *ps)
267 {
268 	return ((bool)(xdr_u_int(xdrs, &ps->transid) &&
269 	    xdr_u_int(xdrs, &ps->status)));
270 }
271 
272 
273 /*
274  * Serializes/deserializes a ypreq_xfr structure.
275  */
276 bool
xdr_ypreq_newxfr(XDR * xdrs,struct ypreq_newxfr * ps)277 xdr_ypreq_newxfr(XDR *xdrs, struct ypreq_newxfr *ps)
278 {
279 	return ((bool)(xdr_ypmap_parms(xdrs, &ps->map_parms) &&
280 	    xdr_u_int(xdrs, &ps->transid) &&
281 	    xdr_u_int(xdrs, &ps->proto) &&
282 	    xdr_string(xdrs, &ps->name, 256)));
283 }
284 
285 /*
286  * Serializes/deserializes a ypreq_xfr structure.
287  */
288 bool
xdr_ypreq_xfr(XDR * xdrs,struct ypreq_xfr * ps)289 xdr_ypreq_xfr(XDR *xdrs, struct ypreq_xfr *ps)
290 {
291 	return ((bool)(xdr_ypmap_parms(xdrs, &ps->map_parms) &&
292 	    xdr_u_int(xdrs, &ps->transid) &&
293 	    xdr_u_int(xdrs, &ps->proto) &&
294 	    xdr_u_short(xdrs, &ps->port)));
295 }
296 
297 
298 /*
299  * Serializes/deserializes a stream of struct ypresp_key_val's.  This is used
300  * only by the client side of the batch enumerate operation.
301  */
302 bool
xdr_ypall(XDR * xdrs,struct ypall_callback * callback)303 xdr_ypall(XDR *xdrs, struct ypall_callback *callback)
304 {
305 	bool_t more;
306 	struct ypresp_key_val kv;
307 	char keybuf[YPMAXRECORD];
308 	char valbuf[YPMAXRECORD];
309 
310 	if (xdrs->x_op == XDR_ENCODE)
311 		return (FALSE);
312 
313 	if (xdrs->x_op == XDR_FREE)
314 		return (TRUE);
315 
316 	kv.keydat.dptr = keybuf;
317 	kv.valdat.dptr = valbuf;
318 	kv.keydat.dsize = YPMAXRECORD;
319 	kv.valdat.dsize = YPMAXRECORD;
320 
321 	for (;;) {
322 		if (!xdr_bool(xdrs, &more))
323 			return (FALSE);
324 
325 		if (!more)
326 			return (TRUE);
327 
328 		if (!xdr_ypresp_key_val(xdrs, &kv))
329 			return (FALSE);
330 		if ((*callback->foreach)(kv.status, kv.keydat.dptr,
331 			    kv.keydat.dsize, kv.valdat.dptr, kv.valdat.dsize,
332 			    callback->data))
333 			return (TRUE);
334 	}
335 }
336 
337 bool_t
xdr_netconfig(XDR * xdrs,struct netconfig * objp)338 xdr_netconfig(XDR *xdrs, struct netconfig *objp)
339 {
340 	if (!xdr_string(xdrs, &objp->nc_netid, ~0))
341 		return (FALSE);
342 	if (!xdr_u_int(xdrs, &objp->nc_semantics))
343 		return (FALSE);
344 	if (!xdr_u_int(xdrs, &objp->nc_flag))
345 		return (FALSE);
346 	if (!xdr_string(xdrs, &objp->nc_protofmly, ~0))
347 		return (FALSE);
348 	if (!xdr_string(xdrs, &objp->nc_proto, ~0))
349 		return (FALSE);
350 	if (!xdr_string(xdrs, &objp->nc_device, ~0))
351 		return (FALSE);
352 	if (!xdr_array(xdrs, (char **)&objp->nc_lookups,
353 		(uint_t *)&objp->nc_nlookups, 100, sizeof (char *),
354 		xdr_wrapstring))
355 		return (FALSE);
356 	return ((bool)xdr_vector(xdrs, (char *)objp->nc_unused,
357 		8, sizeof (uint_t), xdr_u_int));
358 }
359