xref: /titanic_50/usr/src/lib/libnisdb/ldap_xdr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <rpc/types.h>
33*7c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
34*7c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include "db_mindex_c.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "ldap_xdr.h"
39*7c478bd9Sstevel@tonic-gate #include "ldap_util.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate extern bool_t xdr_nis_object();
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * In order not to change the on-disk NIS+ DB format, we need make sure
45*7c478bd9Sstevel@tonic-gate  * that XDR does nothing for the new structures added to various C++
46*7c478bd9Sstevel@tonic-gate  * classes.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate bool_t
50*7c478bd9Sstevel@tonic-gate xdr___nis_table_mapping_t(XDR *xdrs, void *t) {
51*7c478bd9Sstevel@tonic-gate 	return (TRUE);
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate bool_t
55*7c478bd9Sstevel@tonic-gate xdr___nisdb_ptr_t(XDR *xdrs, void *ptr) {
56*7c478bd9Sstevel@tonic-gate 	return (TRUE);
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate bool_t
60*7c478bd9Sstevel@tonic-gate xdr___nisdb_dictionary_defer_t(XDR *xdrs, void *defer) {
61*7c478bd9Sstevel@tonic-gate 	return (TRUE);
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate bool_t
65*7c478bd9Sstevel@tonic-gate xdr___nisdb_rwlock_t(XDR *xdrs, void *rw) {
66*7c478bd9Sstevel@tonic-gate 	return (TRUE);
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate bool_t
70*7c478bd9Sstevel@tonic-gate xdr___nisdb_flag_t(XDR *xdrs, void *flag) {
71*7c478bd9Sstevel@tonic-gate 	return (TRUE);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * Imported from rpc.nisd/nis_db.c
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * Special abbreviated XDR string which knows that the namep parameter (mainly
78*7c478bd9Sstevel@tonic-gate  * owner and group) has a trailing end which matches the last 'n' characters
79*7c478bd9Sstevel@tonic-gate  * in the domainname part.  It makes use of those common characters to
80*7c478bd9Sstevel@tonic-gate  * encode/decode this information.  We append an integer string to the
81*7c478bd9Sstevel@tonic-gate  * name to be encoded which denotes the place in the domainname from where the
82*7c478bd9Sstevel@tonic-gate  * common string starts.  For example, if the name was "foo.my.domain." and the
83*7c478bd9Sstevel@tonic-gate  * domainname was "my.domain.", the name would be encoded as "foo.10" because
84*7c478bd9Sstevel@tonic-gate  * the length of the common part "my.domain." is 10.
85*7c478bd9Sstevel@tonic-gate  */
86*7c478bd9Sstevel@tonic-gate bool_t
87*7c478bd9Sstevel@tonic-gate xdr_nis_name_abbrev(
88*7c478bd9Sstevel@tonic-gate 	XDR		*xdrs,
89*7c478bd9Sstevel@tonic-gate 	nis_name	*namep,
90*7c478bd9Sstevel@tonic-gate 	nis_name	domainname)	/* domainname field from the table */
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	size_t	name_len, dom_len, min_len;
93*7c478bd9Sstevel@tonic-gate 	char 	buf[NIS_MAXNAMELEN];
94*7c478bd9Sstevel@tonic-gate 	char 	*name;
95*7c478bd9Sstevel@tonic-gate 	char	*lenstr, *tmp;
96*7c478bd9Sstevel@tonic-gate 	int	i;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	switch (xdrs->x_op) {
99*7c478bd9Sstevel@tonic-gate 	case XDR_ENCODE:
100*7c478bd9Sstevel@tonic-gate 		/* Get the start of the common part */
101*7c478bd9Sstevel@tonic-gate 		name = *namep;
102*7c478bd9Sstevel@tonic-gate 		name_len = strlen(name);
103*7c478bd9Sstevel@tonic-gate 		if (name_len == 0)
104*7c478bd9Sstevel@tonic-gate 			return (xdr_nis_name(xdrs, namep));
105*7c478bd9Sstevel@tonic-gate 		dom_len = strlen(domainname);
106*7c478bd9Sstevel@tonic-gate 		min_len = (name_len < dom_len) ? name_len : dom_len;
107*7c478bd9Sstevel@tonic-gate 		for (i = 1; i <= min_len; i++) {
108*7c478bd9Sstevel@tonic-gate 			if (name[name_len - i] != domainname[dom_len - i])
109*7c478bd9Sstevel@tonic-gate 				break;
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 		i--;
112*7c478bd9Sstevel@tonic-gate 		memcpy(buf, name, name_len - i);
113*7c478bd9Sstevel@tonic-gate 		sprintf(buf + name_len - i, ".%d", dom_len - i);
114*7c478bd9Sstevel@tonic-gate 		tmp = buf;
115*7c478bd9Sstevel@tonic-gate 		return (xdr_nis_name(xdrs, &tmp));
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	case XDR_DECODE:
118*7c478bd9Sstevel@tonic-gate 		tmp = buf;
119*7c478bd9Sstevel@tonic-gate 		if (!xdr_nis_name(xdrs, &tmp))
120*7c478bd9Sstevel@tonic-gate 		    return (FALSE);
121*7c478bd9Sstevel@tonic-gate 		if ((buf[0] == NULL) || buf[strlen(buf) - 1] == '.') {
122*7c478bd9Sstevel@tonic-gate 			/* It is either a FQN or a NULL string */
123*7c478bd9Sstevel@tonic-gate 			if (*namep) {
124*7c478bd9Sstevel@tonic-gate 				strcpy(*namep, buf);
125*7c478bd9Sstevel@tonic-gate 				return (TRUE);
126*7c478bd9Sstevel@tonic-gate 			} else {
127*7c478bd9Sstevel@tonic-gate 				if ((*namep = strdup(buf)) == NULL)
128*7c478bd9Sstevel@tonic-gate 					return (FALSE);
129*7c478bd9Sstevel@tonic-gate 				else
130*7c478bd9Sstevel@tonic-gate 					return (TRUE);
131*7c478bd9Sstevel@tonic-gate 			}
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 		/* Now concoct the new name */
134*7c478bd9Sstevel@tonic-gate 		if ((lenstr = strrchr(buf, '.')) == NULL) {
135*7c478bd9Sstevel@tonic-gate 			/* something went wrong here */
136*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
137*7c478bd9Sstevel@tonic-gate 				"xdr_nis_name_abbrev: no dot found in %s", buf);
138*7c478bd9Sstevel@tonic-gate 			return (FALSE);
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 		i = atoi(lenstr + 1);
141*7c478bd9Sstevel@tonic-gate 		strcpy(lenstr, domainname + i);
142*7c478bd9Sstevel@tonic-gate 		if (*namep) {
143*7c478bd9Sstevel@tonic-gate 			strcpy(*namep, buf);
144*7c478bd9Sstevel@tonic-gate 		} else {
145*7c478bd9Sstevel@tonic-gate 			if ((*namep = strdup(buf)) == NULL)
146*7c478bd9Sstevel@tonic-gate 				return (FALSE);
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 		return (TRUE);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	default:
151*7c478bd9Sstevel@tonic-gate 		return (xdr_nis_name(xdrs, namep));
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * Imported from rpc.nisd/nis_db.c
157*7c478bd9Sstevel@tonic-gate  *
158*7c478bd9Sstevel@tonic-gate  * special XDR for fetus object.  We create the actual object from the
159*7c478bd9Sstevel@tonic-gate  * "forming" object plus the table object.  We create this special object to
160*7c478bd9Sstevel@tonic-gate  * save the following components of the nis_object:
161*7c478bd9Sstevel@tonic-gate  *	zo_name and zo_domain: replaced by just the length field of 0.  We had
162*7c478bd9Sstevel@tonic-gate  *		to keep the length field for backward compatibility.  If we
163*7c478bd9Sstevel@tonic-gate  *		ever change the object format, we should fix this.
164*7c478bd9Sstevel@tonic-gate  *	zo_owner and zo_group: we condensed it by abbreviating the common part
165*7c478bd9Sstevel@tonic-gate  *		shared between the table object and the entry object
166*7c478bd9Sstevel@tonic-gate  *	en_type: Avoided altogether
167*7c478bd9Sstevel@tonic-gate  *	zo_type and other en_data: Avoided altogether.
168*7c478bd9Sstevel@tonic-gate  *
169*7c478bd9Sstevel@tonic-gate  * XXX: If the definition of nis_object ever changes, this should be changed.
170*7c478bd9Sstevel@tonic-gate  */
171*7c478bd9Sstevel@tonic-gate bool_t
172*7c478bd9Sstevel@tonic-gate xdr_nis_fetus_object(
173*7c478bd9Sstevel@tonic-gate 	XDR		*xdrs,
174*7c478bd9Sstevel@tonic-gate 	nis_object	*objp,	/* Entry object */
175*7c478bd9Sstevel@tonic-gate 	nis_object	*tobj)	/* Table object */
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	uint_t	size;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE)
180*7c478bd9Sstevel@tonic-gate 		return (xdr_nis_object(xdrs, objp));
181*7c478bd9Sstevel@tonic-gate 	if (!xdr_nis_oid(xdrs, &objp->zo_oid))
182*7c478bd9Sstevel@tonic-gate 		return (FALSE);
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	/*
185*7c478bd9Sstevel@tonic-gate 	 * While encoding of zo_name, we put 0 in the length field, while for
186*7c478bd9Sstevel@tonic-gate 	 * decoding, we get the name from the table object.
187*7c478bd9Sstevel@tonic-gate 	 */
188*7c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE) {
189*7c478bd9Sstevel@tonic-gate 		size = 0;
190*7c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
191*7c478bd9Sstevel@tonic-gate 			return (FALSE);
192*7c478bd9Sstevel@tonic-gate 	} else {
193*7c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
194*7c478bd9Sstevel@tonic-gate 			return (FALSE);
195*7c478bd9Sstevel@tonic-gate 		if (size == 0) {	/* shrinked format */
196*7c478bd9Sstevel@tonic-gate 			/* get the name from the table object */
197*7c478bd9Sstevel@tonic-gate 			if ((objp->zo_name = strdup(tobj->zo_name)) == NULL)
198*7c478bd9Sstevel@tonic-gate 				return (FALSE);
199*7c478bd9Sstevel@tonic-gate 		} else {
200*7c478bd9Sstevel@tonic-gate 			/*
201*7c478bd9Sstevel@tonic-gate 			 * We are opening up the xdr_string implementation here
202*7c478bd9Sstevel@tonic-gate 			 * because we called xdr_u_int() earlier.
203*7c478bd9Sstevel@tonic-gate 			 */
204*7c478bd9Sstevel@tonic-gate 			if ((objp->zo_name = (char *)malloc(size + 1)) == NULL)
205*7c478bd9Sstevel@tonic-gate 				return (FALSE);
206*7c478bd9Sstevel@tonic-gate 			if (!xdr_opaque(xdrs, objp->zo_name, size))
207*7c478bd9Sstevel@tonic-gate 				return (FALSE);
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/*
212*7c478bd9Sstevel@tonic-gate 	 * We use the xdr_nis_name_abbrev() function for both owner
213*7c478bd9Sstevel@tonic-gate 	 * and group which constructs the name from the domain name.
214*7c478bd9Sstevel@tonic-gate 	 */
215*7c478bd9Sstevel@tonic-gate 	if (!xdr_nis_name_abbrev(xdrs, &objp->zo_owner, tobj->zo_domain))
216*7c478bd9Sstevel@tonic-gate 		return (FALSE);
217*7c478bd9Sstevel@tonic-gate 	if (!xdr_nis_name_abbrev(xdrs, &objp->zo_group, tobj->zo_domain))
218*7c478bd9Sstevel@tonic-gate 		return (FALSE);
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	/*
221*7c478bd9Sstevel@tonic-gate 	 * While encoding of zo_domain, we put 0 in the length field, while for
222*7c478bd9Sstevel@tonic-gate 	 * decoding, we get the name from the table object.  Same as above for
223*7c478bd9Sstevel@tonic-gate 	 * the name.  Could have used a function instead.
224*7c478bd9Sstevel@tonic-gate 	 */
225*7c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE) {
226*7c478bd9Sstevel@tonic-gate 		size = 0;
227*7c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
228*7c478bd9Sstevel@tonic-gate 			return (FALSE);
229*7c478bd9Sstevel@tonic-gate 	} else {
230*7c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
231*7c478bd9Sstevel@tonic-gate 			return (FALSE);
232*7c478bd9Sstevel@tonic-gate 		if (size == 0) {	/* shrinked format */
233*7c478bd9Sstevel@tonic-gate 			/* get the name from the table object */
234*7c478bd9Sstevel@tonic-gate 			if ((objp->zo_domain = strdup(tobj->zo_domain)) == NULL)
235*7c478bd9Sstevel@tonic-gate 				return (FALSE);
236*7c478bd9Sstevel@tonic-gate 		} else {
237*7c478bd9Sstevel@tonic-gate 			/*
238*7c478bd9Sstevel@tonic-gate 			 * We are opening up the xdr_string implementation here
239*7c478bd9Sstevel@tonic-gate 			 * because we called xdr_u_int() earlier.
240*7c478bd9Sstevel@tonic-gate 			 */
241*7c478bd9Sstevel@tonic-gate 			if ((objp->zo_domain = (char *)malloc(size + 1))
242*7c478bd9Sstevel@tonic-gate 				== NULL)
243*7c478bd9Sstevel@tonic-gate 				return (FALSE);
244*7c478bd9Sstevel@tonic-gate 			if (!xdr_opaque(xdrs, objp->zo_domain, size))
245*7c478bd9Sstevel@tonic-gate 				return (FALSE);
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	if (!xdr_u_int(xdrs, &objp->zo_access))
250*7c478bd9Sstevel@tonic-gate 		return (FALSE);
251*7c478bd9Sstevel@tonic-gate 	if (!xdr_u_int(xdrs, &objp->zo_ttl))
252*7c478bd9Sstevel@tonic-gate 		return (FALSE);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	/*
255*7c478bd9Sstevel@tonic-gate 	 * We know that this is an entry object, so we'll save all the entry_obj
256*7c478bd9Sstevel@tonic-gate 	 * space because we can recreate it later.
257*7c478bd9Sstevel@tonic-gate 	 */
258*7c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE)
259*7c478bd9Sstevel@tonic-gate 		return (TRUE);
260*7c478bd9Sstevel@tonic-gate 	/* Now for the DECODE case, just handcraft the entries and ignore XDR */
261*7c478bd9Sstevel@tonic-gate 	objp->zo_data.zo_type = NIS_ENTRY_OBJ;
262*7c478bd9Sstevel@tonic-gate 	if ((objp->zo_data.objdata_u.en_data.en_type =
263*7c478bd9Sstevel@tonic-gate 		strdup(tobj->zo_data.objdata_u.ta_data.ta_type)) == NULL)
264*7c478bd9Sstevel@tonic-gate 		return (FALSE);
265*7c478bd9Sstevel@tonic-gate 	objp->zo_data.objdata_u.en_data.en_cols.en_cols_val = NULL;
266*7c478bd9Sstevel@tonic-gate 	objp->zo_data.objdata_u.en_data.en_cols.en_cols_len = 0;
267*7c478bd9Sstevel@tonic-gate 	return (TRUE);
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate static const char	*in_directory = "IN_DIRECTORY";
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /*
273*7c478bd9Sstevel@tonic-gate  * Given an input NIS+ object, create the kind
274*7c478bd9Sstevel@tonic-gate  * of pseudo-entry_obj (with an XDR-encoded nis_object in the
275*7c478bd9Sstevel@tonic-gate  * first column) that's stored in the DB. Note that:
276*7c478bd9Sstevel@tonic-gate  *
277*7c478bd9Sstevel@tonic-gate  *	If the input object is an entry, it's assumed to have the
278*7c478bd9Sstevel@tonic-gate  *	columns moved up one step (col 0 in en_cols.en_cols_val[1],
279*7c478bd9Sstevel@tonic-gate  *	etc.). en_cols.en_cols_val[0] will be overwritten. The
280*7c478bd9Sstevel@tonic-gate  *	input object will be changed (some pointers set to zero,
281*7c478bd9Sstevel@tonic-gate  *	etc.) on exit.
282*7c478bd9Sstevel@tonic-gate  *
283*7c478bd9Sstevel@tonic-gate  *	'eo' is assumed to be a pointer to an empty entry_obj (or,
284*7c478bd9Sstevel@tonic-gate  *	at least, one that can be overwritten). It must not be a
285*7c478bd9Sstevel@tonic-gate  *	pointer to the entry_obj in 'obj'. If the input object is
286*7c478bd9Sstevel@tonic-gate  *	of a type other than entry, the 'eo' pointer must have
287*7c478bd9Sstevel@tonic-gate  *	en_cols.en_cols_val appropriately initialized to an array of
288*7c478bd9Sstevel@tonic-gate  *	(at least) length one.
289*7c478bd9Sstevel@tonic-gate  *
290*7c478bd9Sstevel@tonic-gate  *	'tobj' is a pointer to the table object for the table for
291*7c478bd9Sstevel@tonic-gate  *	which the entry_obj is destined. It's needed for entry objects,
292*7c478bd9Sstevel@tonic-gate  *	but unused for other object types.
293*7c478bd9Sstevel@tonic-gate  */
294*7c478bd9Sstevel@tonic-gate entry_obj *
295*7c478bd9Sstevel@tonic-gate makePseudoEntryObj(nis_object *obj, entry_obj *eo, nis_object *tobj) {
296*7c478bd9Sstevel@tonic-gate 	int		bufsize;
297*7c478bd9Sstevel@tonic-gate 	char		*buf;
298*7c478bd9Sstevel@tonic-gate 	XDR		xdrs;
299*7c478bd9Sstevel@tonic-gate 	bool_t		xret;
300*7c478bd9Sstevel@tonic-gate 	uint_t		ecl;
301*7c478bd9Sstevel@tonic-gate 	entry_col	*ecv;
302*7c478bd9Sstevel@tonic-gate 	char		*myself = "makePseudoEntryObj";
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	if (obj == 0 || eo == 0)
305*7c478bd9Sstevel@tonic-gate 		return (0);
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
308*7c478bd9Sstevel@tonic-gate 		*eo = obj->zo_data.objdata_u.en_data;
309*7c478bd9Sstevel@tonic-gate 		eo->en_type = 0;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 		/*
312*7c478bd9Sstevel@tonic-gate 		 * To prevent the XDR function from making a copy of
313*7c478bd9Sstevel@tonic-gate 		 * the entry columns, we set the columns structure to
314*7c478bd9Sstevel@tonic-gate 		 * 0 (ie no column data)
315*7c478bd9Sstevel@tonic-gate 		 */
316*7c478bd9Sstevel@tonic-gate 		ecl = obj->EN_data.en_cols.en_cols_len;
317*7c478bd9Sstevel@tonic-gate 		ecv = obj->EN_data.en_cols.en_cols_val;
318*7c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_len  = 0;
319*7c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val  = 0;
320*7c478bd9Sstevel@tonic-gate 	} else {
321*7c478bd9Sstevel@tonic-gate 		eo->en_type = (char *)in_directory;
322*7c478bd9Sstevel@tonic-gate 	}
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	bufsize = xdr_sizeof(xdr_nis_object, obj);
325*7c478bd9Sstevel@tonic-gate 	buf = am(myself, bufsize);
326*7c478bd9Sstevel@tonic-gate 	if (buf == 0) {
327*7c478bd9Sstevel@tonic-gate 		if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
328*7c478bd9Sstevel@tonic-gate 			obj->EN_data.en_cols.en_cols_len = ecl;
329*7c478bd9Sstevel@tonic-gate 			obj->EN_data.en_cols.en_cols_val = ecv;
330*7c478bd9Sstevel@tonic-gate 		}
331*7c478bd9Sstevel@tonic-gate 		return (0);
332*7c478bd9Sstevel@tonic-gate 	}
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, (char *)buf, bufsize, XDR_ENCODE);
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
337*7c478bd9Sstevel@tonic-gate 		xret = xdr_nis_fetus_object(&xdrs, obj, tobj);
338*7c478bd9Sstevel@tonic-gate 	} else {
339*7c478bd9Sstevel@tonic-gate 		xret = xdr_nis_object(&xdrs, obj);
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	/* Restore the 'obj' */
343*7c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
344*7c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_len = ecl;
345*7c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val = ecv;
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	if (!xret) {
349*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
350*7c478bd9Sstevel@tonic-gate 			"%s: XDR encode failure", myself);
351*7c478bd9Sstevel@tonic-gate 		sfree(buf);
352*7c478bd9Sstevel@tonic-gate 		return (0);
353*7c478bd9Sstevel@tonic-gate 	}
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val[0].ec_value.ec_value_val = buf;
356*7c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val[0].ec_value.ec_value_len = xdr_getpos(&xdrs);
357*7c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val[0].ec_flags = EN_BINARY+EN_XDR;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	return (eo);
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate nis_object *
363*7c478bd9Sstevel@tonic-gate unmakePseudoEntryObj(entry_obj *e, nis_object *tobj) {
364*7c478bd9Sstevel@tonic-gate 	nis_object	*o;
365*7c478bd9Sstevel@tonic-gate 	XDR		xdrs;
366*7c478bd9Sstevel@tonic-gate 	bool_t		stat;
367*7c478bd9Sstevel@tonic-gate 	char		*myself = "unmakePseudoEntryObj";
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	if (e == 0 || e->en_cols.en_cols_val == 0 ||
370*7c478bd9Sstevel@tonic-gate 			e->en_cols.en_cols_len == 0)
371*7c478bd9Sstevel@tonic-gate 		return (0);
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	o = am(myself, sizeof (*o));
374*7c478bd9Sstevel@tonic-gate 	if (o == 0)
375*7c478bd9Sstevel@tonic-gate 		return (0);
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, e->en_cols.en_cols_val[0].ec_value.ec_value_val,
378*7c478bd9Sstevel@tonic-gate 			e->en_cols.en_cols_val[0].ec_value.ec_value_len,
379*7c478bd9Sstevel@tonic-gate 			XDR_DECODE);
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (tobj != 0 && (e->en_type == 0 || e->en_type[0] == '\0')) {
382*7c478bd9Sstevel@tonic-gate 		stat = xdr_nis_fetus_object(&xdrs, o, tobj);
383*7c478bd9Sstevel@tonic-gate 	} else {
384*7c478bd9Sstevel@tonic-gate 		stat = xdr_nis_object(&xdrs, o);
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 	if (!stat) {
388*7c478bd9Sstevel@tonic-gate 		sfree(o);
389*7c478bd9Sstevel@tonic-gate 		o = 0;
390*7c478bd9Sstevel@tonic-gate 	}
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/*
393*7c478bd9Sstevel@tonic-gate 	 * If it's an entry object, construct the column information.
394*7c478bd9Sstevel@tonic-gate 	 * We make this a copy, so that 'o' can be freed using
395*7c478bd9Sstevel@tonic-gate 	 * nis_destroy_object().
396*7c478bd9Sstevel@tonic-gate 	 */
397*7c478bd9Sstevel@tonic-gate 	if (o != 0 && o->zo_data.zo_type == NIS_ENTRY_OBJ &&
398*7c478bd9Sstevel@tonic-gate 			o->zo_data.objdata_u.en_data.en_cols.en_cols_val == 0 &&
399*7c478bd9Sstevel@tonic-gate 			e->en_cols.en_cols_len > 1) {
400*7c478bd9Sstevel@tonic-gate 		entry_col	*ec, *oec;
401*7c478bd9Sstevel@tonic-gate 		uint_t		i, *ocl;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 		ec = am(myself, (e->en_cols.en_cols_len - 1) * sizeof (ec[0]));
404*7c478bd9Sstevel@tonic-gate 		if (ec == 0) {
405*7c478bd9Sstevel@tonic-gate 			nis_destroy_object(o);
406*7c478bd9Sstevel@tonic-gate 			return (0);
407*7c478bd9Sstevel@tonic-gate 		}
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		o->zo_data.objdata_u.en_data.en_cols.en_cols_val = ec;
410*7c478bd9Sstevel@tonic-gate 		o->zo_data.objdata_u.en_data.en_cols.en_cols_len = 0;
411*7c478bd9Sstevel@tonic-gate 		ocl = &o->zo_data.objdata_u.en_data.en_cols.en_cols_len;
412*7c478bd9Sstevel@tonic-gate 		oec = e->en_cols.en_cols_val;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 		for (i = 1; i < e->en_cols.en_cols_len; i++) {
415*7c478bd9Sstevel@tonic-gate 			uint_t	len;
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 			if (oec[i].ec_value.ec_value_val != 0) {
418*7c478bd9Sstevel@tonic-gate 				len = oec[i].ec_value.ec_value_len;
419*7c478bd9Sstevel@tonic-gate 				if (len == 0)
420*7c478bd9Sstevel@tonic-gate 					len++;
421*7c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_val = am(myself, len);
422*7c478bd9Sstevel@tonic-gate 				if (ec[i-1].ec_value.ec_value_val == 0) {
423*7c478bd9Sstevel@tonic-gate 					nis_destroy_object(o);
424*7c478bd9Sstevel@tonic-gate 					return (0);
425*7c478bd9Sstevel@tonic-gate 				}
426*7c478bd9Sstevel@tonic-gate 				(void) memcpy(ec[i-1].ec_value.ec_value_val,
427*7c478bd9Sstevel@tonic-gate 						oec[i].ec_value.ec_value_val,
428*7c478bd9Sstevel@tonic-gate 						oec[i].ec_value.ec_value_len);
429*7c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_len =
430*7c478bd9Sstevel@tonic-gate 						oec[i].ec_value.ec_value_len;
431*7c478bd9Sstevel@tonic-gate 			} else {
432*7c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_val = 0;
433*7c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_len = 0;
434*7c478bd9Sstevel@tonic-gate 			}
435*7c478bd9Sstevel@tonic-gate 			*ocl += 1;
436*7c478bd9Sstevel@tonic-gate 		}
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	/*
440*7c478bd9Sstevel@tonic-gate 	 * If it's an entry, and we have the table object, make sure
441*7c478bd9Sstevel@tonic-gate 	 * zo_name and en_type either already are set, or get them
442*7c478bd9Sstevel@tonic-gate 	 * from the table.
443*7c478bd9Sstevel@tonic-gate 	 */
444*7c478bd9Sstevel@tonic-gate 	if (o != 0 && o->zo_data.zo_type == NIS_ENTRY_OBJ && tobj != 0) {
445*7c478bd9Sstevel@tonic-gate 		if (o->zo_name == 0)
446*7c478bd9Sstevel@tonic-gate 			o->zo_name = sdup(myself, T, tobj->zo_name);
447*7c478bd9Sstevel@tonic-gate 		if (o->zo_data.objdata_u.en_data.en_type == 0)
448*7c478bd9Sstevel@tonic-gate 			o->zo_data.objdata_u.en_data.en_type = sdup(myself, T,
449*7c478bd9Sstevel@tonic-gate 				tobj->zo_data.objdata_u.ta_data.ta_type);
450*7c478bd9Sstevel@tonic-gate 	}
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	return (o);
453*7c478bd9Sstevel@tonic-gate }
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate /*
456*7c478bd9Sstevel@tonic-gate  * Input:  A (nis_object *), and (optionally) an (entry_obj *) array.
457*7c478bd9Sstevel@tonic-gate  * Output: Pointer to an XDR:ed version of an (xdr_nis_object_t).
458*7c478bd9Sstevel@tonic-gate  */
459*7c478bd9Sstevel@tonic-gate void *
460*7c478bd9Sstevel@tonic-gate xdrNisObject(nis_object *obj, entry_obj **ea, int numEa, int *xdrLenP) {
461*7c478bd9Sstevel@tonic-gate 	xdr_nis_object_t	xno;
462*7c478bd9Sstevel@tonic-gate 	void			*buf;
463*7c478bd9Sstevel@tonic-gate 	int			xdrLen;
464*7c478bd9Sstevel@tonic-gate 	XDR			xdrs;
465*7c478bd9Sstevel@tonic-gate 	bool_t			xret;
466*7c478bd9Sstevel@tonic-gate 	char			*myself = "xdrNisObject";
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	if (obj == 0)
469*7c478bd9Sstevel@tonic-gate 		return (0);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	/*
472*7c478bd9Sstevel@tonic-gate 	 * The version tells us what the XDR:ed buffer contains.
473*7c478bd9Sstevel@tonic-gate 	 * Should be incremented whenever xdr_nis_object_t changes
474*7c478bd9Sstevel@tonic-gate 	 * incompatibly.
475*7c478bd9Sstevel@tonic-gate 	 */
476*7c478bd9Sstevel@tonic-gate 	xno.xversion = 1;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	xno.obj = obj;
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_DIRECTORY_OBJ &&
481*7c478bd9Sstevel@tonic-gate 			ea != 0 && numEa > 0) {
482*7c478bd9Sstevel@tonic-gate 		int	i;
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 		/*
485*7c478bd9Sstevel@tonic-gate 		 * The ea[] array is expected to contain the kind of
486*7c478bd9Sstevel@tonic-gate 		 * pseudo-entry object stored in the nisdb incarnation
487*7c478bd9Sstevel@tonic-gate 		 * of a NIS+ directory. Column zero contains the XDR:ed
488*7c478bd9Sstevel@tonic-gate 		 * directory entry object (which we ignore), while column
489*7c478bd9Sstevel@tonic-gate 		 * one contains the name of said entry. It's the latter
490*7c478bd9Sstevel@tonic-gate 		 * that we borrow for use in the dirEntry[] list of the
491*7c478bd9Sstevel@tonic-gate 		 * xdr_nis_object_t.
492*7c478bd9Sstevel@tonic-gate 		 */
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_len = 0;
495*7c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_val = am(myself, numEa *
496*7c478bd9Sstevel@tonic-gate 			sizeof (xno.dirEntry.dirEntry_val[0]));
497*7c478bd9Sstevel@tonic-gate 		if (xno.dirEntry.dirEntry_val == 0)
498*7c478bd9Sstevel@tonic-gate 			return (0);
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < numEa; i++) {
501*7c478bd9Sstevel@tonic-gate 			if (ea[i] == 0 || ea[i]->en_cols.en_cols_val == 0 ||
502*7c478bd9Sstevel@tonic-gate 					ea[i]->en_cols.en_cols_len != 2 ||
503*7c478bd9Sstevel@tonic-gate 					ea[i]->en_cols.en_cols_val[1].
504*7c478bd9Sstevel@tonic-gate 						ec_value.ec_value_len == 0)
505*7c478bd9Sstevel@tonic-gate 				continue;
506*7c478bd9Sstevel@tonic-gate 			/*
507*7c478bd9Sstevel@tonic-gate 			 * Yes, there's a NUL at the end of the dir entry
508*7c478bd9Sstevel@tonic-gate 			 * name.
509*7c478bd9Sstevel@tonic-gate 			 */
510*7c478bd9Sstevel@tonic-gate 			xno.dirEntry.dirEntry_val[xno.dirEntry.dirEntry_len] =
511*7c478bd9Sstevel@tonic-gate 				ea[i]->en_cols.en_cols_val[1].
512*7c478bd9Sstevel@tonic-gate 					ec_value.ec_value_val;
513*7c478bd9Sstevel@tonic-gate 			xno.dirEntry.dirEntry_len++;
514*7c478bd9Sstevel@tonic-gate 		}
515*7c478bd9Sstevel@tonic-gate 	} else {
516*7c478bd9Sstevel@tonic-gate 		/* No directory entries */
517*7c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_len = 0;
518*7c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_val = 0;
519*7c478bd9Sstevel@tonic-gate 	}
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 	xdrLen = xdr_sizeof(xdr_xdr_nis_object_t, &xno);
522*7c478bd9Sstevel@tonic-gate 	buf = am(myself, xdrLen);
523*7c478bd9Sstevel@tonic-gate 	if (buf == 0)
524*7c478bd9Sstevel@tonic-gate 		return (0);
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, (char *)buf, xdrLen, XDR_ENCODE);
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	xret = xdr_xdr_nis_object_t(&xdrs, &xno);
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	sfree(xno.dirEntry.dirEntry_val);
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	if (!xret) {
533*7c478bd9Sstevel@tonic-gate 		sfree(buf);
534*7c478bd9Sstevel@tonic-gate 		return (0);
535*7c478bd9Sstevel@tonic-gate 	}
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	if (xdrLenP != 0)
538*7c478bd9Sstevel@tonic-gate 		*xdrLenP = xdrLen;
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	return (buf);
541*7c478bd9Sstevel@tonic-gate }
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate /*
544*7c478bd9Sstevel@tonic-gate  * Input:  Pointer to an XDR:ed version of an (xdr_nis_object_t).
545*7c478bd9Sstevel@tonic-gate  * Output: Pointer to a (nis_object *) and (if the object is a
546*7c478bd9Sstevel@tonic-gate  *         directory) a pointer to an array of (entry_obj *).
547*7c478bd9Sstevel@tonic-gate  */
548*7c478bd9Sstevel@tonic-gate nis_object *
549*7c478bd9Sstevel@tonic-gate unXdrNisObject(void *buf, int bufLen, entry_obj ***eaP, int *numEaP) {
550*7c478bd9Sstevel@tonic-gate 	xdr_nis_object_t	*xno;
551*7c478bd9Sstevel@tonic-gate 	XDR			xdrs;
552*7c478bd9Sstevel@tonic-gate 	bool_t			xret;
553*7c478bd9Sstevel@tonic-gate 	entry_obj		**ea;
554*7c478bd9Sstevel@tonic-gate 	int			numEa;
555*7c478bd9Sstevel@tonic-gate 	nis_object		*o;
556*7c478bd9Sstevel@tonic-gate 	char			*myself = "unXdrNisObject";
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	if (buf == 0 || bufLen <= 0)
559*7c478bd9Sstevel@tonic-gate 		return (0);
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	xno = am(myself, sizeof (*xno));
562*7c478bd9Sstevel@tonic-gate 	if (xno == 0)
563*7c478bd9Sstevel@tonic-gate 		return (0);
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, buf, bufLen, XDR_DECODE);
566*7c478bd9Sstevel@tonic-gate 	xret = xdr_xdr_nis_object_t(&xdrs, xno);
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	if (!xret) {
569*7c478bd9Sstevel@tonic-gate 		sfree(xno);
570*7c478bd9Sstevel@tonic-gate 		return (0);
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	switch (xno->xversion) {
574*7c478bd9Sstevel@tonic-gate 	case 1:
575*7c478bd9Sstevel@tonic-gate 		break;
576*7c478bd9Sstevel@tonic-gate 	default:
577*7c478bd9Sstevel@tonic-gate 		xdr_free(xdr_xdr_nis_object_t, (char *)xno);
578*7c478bd9Sstevel@tonic-gate 		sfree(xno);
579*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
580*7c478bd9Sstevel@tonic-gate 			"%s: Unknown xdr_nis_object_t version %d",
581*7c478bd9Sstevel@tonic-gate 			myself, xno->xversion);
582*7c478bd9Sstevel@tonic-gate 		return (0);
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	if (eaP != 0 && numEaP != 0 && xno->dirEntry.dirEntry_len > 0 &&
586*7c478bd9Sstevel@tonic-gate 			xno->dirEntry.dirEntry_val != 0) {
587*7c478bd9Sstevel@tonic-gate 		ea = am(myself, xno->dirEntry.dirEntry_len * sizeof (ea[0]));
588*7c478bd9Sstevel@tonic-gate 		if (ea == 0) {
589*7c478bd9Sstevel@tonic-gate 			xdr_free(xdr_xdr_nis_object_t, (char *)xno);
590*7c478bd9Sstevel@tonic-gate 			sfree(xno);
591*7c478bd9Sstevel@tonic-gate 			return (0);
592*7c478bd9Sstevel@tonic-gate 		}
593*7c478bd9Sstevel@tonic-gate 		for (numEa = 0; numEa < xno->dirEntry.dirEntry_len; numEa++) {
594*7c478bd9Sstevel@tonic-gate 			ea[numEa] = am(myself, sizeof (*ea[numEa]));
595*7c478bd9Sstevel@tonic-gate 			if (ea[numEa] != 0) {
596*7c478bd9Sstevel@tonic-gate 				ea[numEa]->en_cols.en_cols_len = 2;
597*7c478bd9Sstevel@tonic-gate 				ea[numEa]->en_cols.en_cols_val = am(myself,
598*7c478bd9Sstevel@tonic-gate 					ea[numEa]->en_cols.en_cols_len *
599*7c478bd9Sstevel@tonic-gate 				sizeof (ea[numEa]->en_cols.en_cols_val[0]));
600*7c478bd9Sstevel@tonic-gate 			}
601*7c478bd9Sstevel@tonic-gate 			if (ea[numEa] == 0 ||
602*7c478bd9Sstevel@tonic-gate 					ea[numEa]->en_cols.en_cols_val == 0) {
603*7c478bd9Sstevel@tonic-gate 				int	i;
604*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < numEa; i++) {
605*7c478bd9Sstevel@tonic-gate 					sfree(ea[i]->en_cols.en_cols_val);
606*7c478bd9Sstevel@tonic-gate 					sfree(ea[i]);
607*7c478bd9Sstevel@tonic-gate 				}
608*7c478bd9Sstevel@tonic-gate 				sfree(ea);
609*7c478bd9Sstevel@tonic-gate 				xdr_free(xdr_xdr_nis_object_t, (char *)xno);
610*7c478bd9Sstevel@tonic-gate 				sfree(xno);
611*7c478bd9Sstevel@tonic-gate 				return (0);
612*7c478bd9Sstevel@tonic-gate 			}
613*7c478bd9Sstevel@tonic-gate 			/* Leave column 0 (XDR:ed object) empty */
614*7c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[0].
615*7c478bd9Sstevel@tonic-gate 				ec_value.ec_value_len = 0;
616*7c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[0].
617*7c478bd9Sstevel@tonic-gate 				ec_value.ec_value_val = 0;
618*7c478bd9Sstevel@tonic-gate 			/*
619*7c478bd9Sstevel@tonic-gate 			 * Fill in name of dir entry. The DB counts the NUL
620*7c478bd9Sstevel@tonic-gate 			 * as part of the dir entry name; hence, add one
621*7c478bd9Sstevel@tonic-gate 			 * to the string length.
622*7c478bd9Sstevel@tonic-gate 			 */
623*7c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[1].
624*7c478bd9Sstevel@tonic-gate 				ec_value.ec_value_len = slen(xno->dirEntry.
625*7c478bd9Sstevel@tonic-gate 					dirEntry_val[numEa]) + 1;
626*7c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[1].
627*7c478bd9Sstevel@tonic-gate 				ec_value.ec_value_val =
628*7c478bd9Sstevel@tonic-gate 					xno->dirEntry.dirEntry_val[numEa];
629*7c478bd9Sstevel@tonic-gate 		}
630*7c478bd9Sstevel@tonic-gate 		*eaP = ea;
631*7c478bd9Sstevel@tonic-gate 		*numEaP = numEa;
632*7c478bd9Sstevel@tonic-gate 		/*
633*7c478bd9Sstevel@tonic-gate 		 * The xno->dirEntry.dirEntry_val[] pointers are duplicated
634*7c478bd9Sstevel@tonic-gate 		 * in 'ea'. Set the xno pointers to zero, so that the xdr_free
635*7c478bd9Sstevel@tonic-gate 		 * doesn't free the 'ea' data.
636*7c478bd9Sstevel@tonic-gate 		 */
637*7c478bd9Sstevel@tonic-gate 		if (numEa > 0) {
638*7c478bd9Sstevel@tonic-gate 			int	i;
639*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < numEa; i++) {
640*7c478bd9Sstevel@tonic-gate 				xno->dirEntry.dirEntry_val[i] = 0;
641*7c478bd9Sstevel@tonic-gate 			}
642*7c478bd9Sstevel@tonic-gate 		}
643*7c478bd9Sstevel@tonic-gate 	} else {
644*7c478bd9Sstevel@tonic-gate 		if (eaP != 0)
645*7c478bd9Sstevel@tonic-gate 			*eaP = 0;
646*7c478bd9Sstevel@tonic-gate 		if (numEaP != 0)
647*7c478bd9Sstevel@tonic-gate 			*numEaP = 0;
648*7c478bd9Sstevel@tonic-gate 	}
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	o = xno->obj;
651*7c478bd9Sstevel@tonic-gate 	xno->obj = 0;
652*7c478bd9Sstevel@tonic-gate 	xdr_free(xdr_xdr_nis_object_t, (char *)xno);
653*7c478bd9Sstevel@tonic-gate 	sfree(xno);
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 	return (o);
656*7c478bd9Sstevel@tonic-gate }
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate void
659*7c478bd9Sstevel@tonic-gate freeEntryObjArray(entry_obj **ea, int numEa) {
660*7c478bd9Sstevel@tonic-gate 	int	i;
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	if (ea == 0)
663*7c478bd9Sstevel@tonic-gate 		return;
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numEa; i++) {
666*7c478bd9Sstevel@tonic-gate 		int	j;
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < ea[i]->en_cols.en_cols_len; j++) {
669*7c478bd9Sstevel@tonic-gate 			sfree(ea[i]->en_cols.en_cols_val[j].
670*7c478bd9Sstevel@tonic-gate 				ec_value.ec_value_val);
671*7c478bd9Sstevel@tonic-gate 		}
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 		sfree(ea[i]->en_cols.en_cols_val);
674*7c478bd9Sstevel@tonic-gate 	}
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 	sfree(ea);
677*7c478bd9Sstevel@tonic-gate }
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate /*
680*7c478bd9Sstevel@tonic-gate  * Return TRUE if 'o1' and 'o2' are the same, FALSE otherwise.
681*7c478bd9Sstevel@tonic-gate  * We perform the comparison by XDR encoding the objects, and then
682*7c478bd9Sstevel@tonic-gate  * checking the XDR buffers for equality. However, we don't want to
683*7c478bd9Sstevel@tonic-gate  * include the zo_oid (i.e., ctime and mtime) in the comparison.
684*7c478bd9Sstevel@tonic-gate  */
685*7c478bd9Sstevel@tonic-gate bool_t
686*7c478bd9Sstevel@tonic-gate sameNisPlusObj(nis_object *o1, nis_object *o2) {
687*7c478bd9Sstevel@tonic-gate 	XDR		x1, x2;
688*7c478bd9Sstevel@tonic-gate 	void		*b1, *b2;
689*7c478bd9Sstevel@tonic-gate 	int		l1, l2;
690*7c478bd9Sstevel@tonic-gate 	bool_t		ret;
691*7c478bd9Sstevel@tonic-gate 	nis_object	obj1, obj2;
692*7c478bd9Sstevel@tonic-gate 	char		*myself = "sameNisPlusObj";
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	if (o1 == o2)
695*7c478bd9Sstevel@tonic-gate 		return (TRUE);
696*7c478bd9Sstevel@tonic-gate 	else if (o1 == 0 || o2 == 0)
697*7c478bd9Sstevel@tonic-gate 		return (FALSE);
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 	/*
700*7c478bd9Sstevel@tonic-gate 	 * We want to exclude the zo_oid from the comparison. In order
701*7c478bd9Sstevel@tonic-gate 	 * not to modify the objects (even very briefly), we do this by
702*7c478bd9Sstevel@tonic-gate 	 * making copies (nis_object itself only, not the underlying
703*7c478bd9Sstevel@tonic-gate 	 * structures accessed through pointers), and setting the zo_oid
704*7c478bd9Sstevel@tonic-gate 	 * to zero in the copies.
705*7c478bd9Sstevel@tonic-gate 	 */
706*7c478bd9Sstevel@tonic-gate 	obj1 = *o1;
707*7c478bd9Sstevel@tonic-gate 	obj2 = *o2;
708*7c478bd9Sstevel@tonic-gate 	obj1.zo_oid.ctime = obj1.zo_oid.mtime = 0;
709*7c478bd9Sstevel@tonic-gate 	obj2.zo_oid.ctime = obj2.zo_oid.mtime = 0;
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate 	l1 = xdr_sizeof(xdr_nis_object, &obj1);
712*7c478bd9Sstevel@tonic-gate 	l2 = xdr_sizeof(xdr_nis_object, &obj2);
713*7c478bd9Sstevel@tonic-gate 	if (l1 != l2)
714*7c478bd9Sstevel@tonic-gate 		return (FALSE);
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	b1 = am(myself, l1);
717*7c478bd9Sstevel@tonic-gate 	b2 = am(myself, l2);
718*7c478bd9Sstevel@tonic-gate 	if (b1 == 0 || b2 == 0) {
719*7c478bd9Sstevel@tonic-gate 		sfree(b1);
720*7c478bd9Sstevel@tonic-gate 		sfree(b2);
721*7c478bd9Sstevel@tonic-gate 		return (FALSE);
722*7c478bd9Sstevel@tonic-gate 	}
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&x1, (char *)b1, l1, XDR_ENCODE);
725*7c478bd9Sstevel@tonic-gate 	xdrmem_create(&x2, (char *)b2, l2, XDR_ENCODE);
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	if (xdr_nis_object(&x1, &obj1) && xdr_nis_object(&x2, &obj2)) {
728*7c478bd9Sstevel@tonic-gate 		ret = (memcmp(b1, b2, l1) == 0);
729*7c478bd9Sstevel@tonic-gate 	} else {
730*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
731*7c478bd9Sstevel@tonic-gate 			"%s: xdr_nis_object() error",
732*7c478bd9Sstevel@tonic-gate 			myself);
733*7c478bd9Sstevel@tonic-gate 		ret = FALSE;
734*7c478bd9Sstevel@tonic-gate 	}
735*7c478bd9Sstevel@tonic-gate 
736*7c478bd9Sstevel@tonic-gate 	sfree(b1);
737*7c478bd9Sstevel@tonic-gate 	sfree(b2);
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	return (ret);
740*7c478bd9Sstevel@tonic-gate }
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate /*
743*7c478bd9Sstevel@tonic-gate  * A wrapper/convenience function for sameNisPlusObj() that extracts
744*7c478bd9Sstevel@tonic-gate  * the object in column zero of 'e2'.
745*7c478bd9Sstevel@tonic-gate  */
746*7c478bd9Sstevel@tonic-gate bool_t
747*7c478bd9Sstevel@tonic-gate sameNisPlusPseudoObj(nis_object *o1, entry_obj *e2) {
748*7c478bd9Sstevel@tonic-gate 	nis_object	*o2;
749*7c478bd9Sstevel@tonic-gate 	bool_t		res;
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	if (o1 == 0 && e2 == 0)
752*7c478bd9Sstevel@tonic-gate 		return (TRUE);
753*7c478bd9Sstevel@tonic-gate 	else if (e2 == 0)
754*7c478bd9Sstevel@tonic-gate 		return (FALSE);
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	o2 = unmakePseudoEntryObj(e2, 0);
757*7c478bd9Sstevel@tonic-gate 	if (o2 == 0)
758*7c478bd9Sstevel@tonic-gate 		return ((o1 == 0) ? TRUE : FALSE);
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	res = sameNisPlusObj(o1, o2);
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	nis_destroy_object(o2);
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	return (res);
765*7c478bd9Sstevel@tonic-gate }
766