xref: /illumos-gate/usr/src/lib/libnisdb/ldap_xdr.c (revision a3451765)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23a87701e9SGary Mills  * Copyright 2015 Gary Mills
247c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
257c478bd9Sstevel@tonic-gate  * All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <rpc/types.h>
327c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
337c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "db_mindex_c.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "ldap_xdr.h"
387c478bd9Sstevel@tonic-gate #include "ldap_util.h"
397c478bd9Sstevel@tonic-gate 
40a87701e9SGary Mills #include "nis_clnt.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * In order not to change the on-disk NIS+ DB format, we need make sure
447c478bd9Sstevel@tonic-gate  * that XDR does nothing for the new structures added to various C++
457c478bd9Sstevel@tonic-gate  * classes.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate bool_t
xdr___nis_table_mapping_t(XDR * xdrs,void * t)497c478bd9Sstevel@tonic-gate xdr___nis_table_mapping_t(XDR *xdrs, void *t) {
507c478bd9Sstevel@tonic-gate 	return (TRUE);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate bool_t
xdr___nisdb_ptr_t(XDR * xdrs,void * ptr)547c478bd9Sstevel@tonic-gate xdr___nisdb_ptr_t(XDR *xdrs, void *ptr) {
557c478bd9Sstevel@tonic-gate 	return (TRUE);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate bool_t
xdr___nisdb_dictionary_defer_t(XDR * xdrs,void * defer)597c478bd9Sstevel@tonic-gate xdr___nisdb_dictionary_defer_t(XDR *xdrs, void *defer) {
607c478bd9Sstevel@tonic-gate 	return (TRUE);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate bool_t
xdr___nisdb_rwlock_t(XDR * xdrs,void * rw)647c478bd9Sstevel@tonic-gate xdr___nisdb_rwlock_t(XDR *xdrs, void *rw) {
657c478bd9Sstevel@tonic-gate 	return (TRUE);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate bool_t
xdr___nisdb_flag_t(XDR * xdrs,void * flag)697c478bd9Sstevel@tonic-gate xdr___nisdb_flag_t(XDR *xdrs, void *flag) {
707c478bd9Sstevel@tonic-gate 	return (TRUE);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Imported from rpc.nisd/nis_db.c
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * Special abbreviated XDR string which knows that the namep parameter (mainly
777c478bd9Sstevel@tonic-gate  * owner and group) has a trailing end which matches the last 'n' characters
787c478bd9Sstevel@tonic-gate  * in the domainname part.  It makes use of those common characters to
797c478bd9Sstevel@tonic-gate  * encode/decode this information.  We append an integer string to the
807c478bd9Sstevel@tonic-gate  * name to be encoded which denotes the place in the domainname from where the
817c478bd9Sstevel@tonic-gate  * common string starts.  For example, if the name was "foo.my.domain." and the
827c478bd9Sstevel@tonic-gate  * domainname was "my.domain.", the name would be encoded as "foo.10" because
837c478bd9Sstevel@tonic-gate  * the length of the common part "my.domain." is 10.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate bool_t
xdr_nis_name_abbrev(XDR * xdrs,nis_name * namep,nis_name domainname)867c478bd9Sstevel@tonic-gate xdr_nis_name_abbrev(
877c478bd9Sstevel@tonic-gate 	XDR		*xdrs,
887c478bd9Sstevel@tonic-gate 	nis_name	*namep,
897c478bd9Sstevel@tonic-gate 	nis_name	domainname)	/* domainname field from the table */
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	size_t	name_len, dom_len, min_len;
927c478bd9Sstevel@tonic-gate 	char 	buf[NIS_MAXNAMELEN];
937c478bd9Sstevel@tonic-gate 	char 	*name;
947c478bd9Sstevel@tonic-gate 	char	*lenstr, *tmp;
957c478bd9Sstevel@tonic-gate 	int	i;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	switch (xdrs->x_op) {
987c478bd9Sstevel@tonic-gate 	case XDR_ENCODE:
997c478bd9Sstevel@tonic-gate 		/* Get the start of the common part */
1007c478bd9Sstevel@tonic-gate 		name = *namep;
1017c478bd9Sstevel@tonic-gate 		name_len = strlen(name);
1027c478bd9Sstevel@tonic-gate 		if (name_len == 0)
1037c478bd9Sstevel@tonic-gate 			return (xdr_nis_name(xdrs, namep));
1047c478bd9Sstevel@tonic-gate 		dom_len = strlen(domainname);
1057c478bd9Sstevel@tonic-gate 		min_len = (name_len < dom_len) ? name_len : dom_len;
1067c478bd9Sstevel@tonic-gate 		for (i = 1; i <= min_len; i++) {
1077c478bd9Sstevel@tonic-gate 			if (name[name_len - i] != domainname[dom_len - i])
1087c478bd9Sstevel@tonic-gate 				break;
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		i--;
1117c478bd9Sstevel@tonic-gate 		memcpy(buf, name, name_len - i);
1127c478bd9Sstevel@tonic-gate 		sprintf(buf + name_len - i, ".%d", dom_len - i);
1137c478bd9Sstevel@tonic-gate 		tmp = buf;
1147c478bd9Sstevel@tonic-gate 		return (xdr_nis_name(xdrs, &tmp));
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	case XDR_DECODE:
1177c478bd9Sstevel@tonic-gate 		tmp = buf;
1187c478bd9Sstevel@tonic-gate 		if (!xdr_nis_name(xdrs, &tmp))
1197c478bd9Sstevel@tonic-gate 		    return (FALSE);
120*a3451765SToomas Soome 		if ((buf[0] == '\0') || buf[strlen(buf) - 1] == '.') {
1217c478bd9Sstevel@tonic-gate 			/* It is either a FQN or a NULL string */
1227c478bd9Sstevel@tonic-gate 			if (*namep) {
1237c478bd9Sstevel@tonic-gate 				strcpy(*namep, buf);
1247c478bd9Sstevel@tonic-gate 				return (TRUE);
1257c478bd9Sstevel@tonic-gate 			} else {
1267c478bd9Sstevel@tonic-gate 				if ((*namep = strdup(buf)) == NULL)
1277c478bd9Sstevel@tonic-gate 					return (FALSE);
1287c478bd9Sstevel@tonic-gate 				else
1297c478bd9Sstevel@tonic-gate 					return (TRUE);
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		/* Now concoct the new name */
1337c478bd9Sstevel@tonic-gate 		if ((lenstr = strrchr(buf, '.')) == NULL) {
1347c478bd9Sstevel@tonic-gate 			/* something went wrong here */
1357c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1367c478bd9Sstevel@tonic-gate 				"xdr_nis_name_abbrev: no dot found in %s", buf);
1377c478bd9Sstevel@tonic-gate 			return (FALSE);
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 		i = atoi(lenstr + 1);
1407c478bd9Sstevel@tonic-gate 		strcpy(lenstr, domainname + i);
1417c478bd9Sstevel@tonic-gate 		if (*namep) {
1427c478bd9Sstevel@tonic-gate 			strcpy(*namep, buf);
1437c478bd9Sstevel@tonic-gate 		} else {
1447c478bd9Sstevel@tonic-gate 			if ((*namep = strdup(buf)) == NULL)
1457c478bd9Sstevel@tonic-gate 				return (FALSE);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		return (TRUE);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	default:
1507c478bd9Sstevel@tonic-gate 		return (xdr_nis_name(xdrs, namep));
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Imported from rpc.nisd/nis_db.c
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  * special XDR for fetus object.  We create the actual object from the
1587c478bd9Sstevel@tonic-gate  * "forming" object plus the table object.  We create this special object to
1597c478bd9Sstevel@tonic-gate  * save the following components of the nis_object:
1607c478bd9Sstevel@tonic-gate  *	zo_name and zo_domain: replaced by just the length field of 0.  We had
1617c478bd9Sstevel@tonic-gate  *		to keep the length field for backward compatibility.  If we
1627c478bd9Sstevel@tonic-gate  *		ever change the object format, we should fix this.
1637c478bd9Sstevel@tonic-gate  *	zo_owner and zo_group: we condensed it by abbreviating the common part
1647c478bd9Sstevel@tonic-gate  *		shared between the table object and the entry object
1657c478bd9Sstevel@tonic-gate  *	en_type: Avoided altogether
1667c478bd9Sstevel@tonic-gate  *	zo_type and other en_data: Avoided altogether.
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  * XXX: If the definition of nis_object ever changes, this should be changed.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate bool_t
xdr_nis_fetus_object(XDR * xdrs,nis_object * objp,nis_object * tobj)1717c478bd9Sstevel@tonic-gate xdr_nis_fetus_object(
1727c478bd9Sstevel@tonic-gate 	XDR		*xdrs,
1737c478bd9Sstevel@tonic-gate 	nis_object	*objp,	/* Entry object */
1747c478bd9Sstevel@tonic-gate 	nis_object	*tobj)	/* Table object */
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	uint_t	size;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE)
1797c478bd9Sstevel@tonic-gate 		return (xdr_nis_object(xdrs, objp));
1807c478bd9Sstevel@tonic-gate 	if (!xdr_nis_oid(xdrs, &objp->zo_oid))
1817c478bd9Sstevel@tonic-gate 		return (FALSE);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * While encoding of zo_name, we put 0 in the length field, while for
1857c478bd9Sstevel@tonic-gate 	 * decoding, we get the name from the table object.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE) {
1887c478bd9Sstevel@tonic-gate 		size = 0;
1897c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
1907c478bd9Sstevel@tonic-gate 			return (FALSE);
1917c478bd9Sstevel@tonic-gate 	} else {
1927c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
1937c478bd9Sstevel@tonic-gate 			return (FALSE);
1947c478bd9Sstevel@tonic-gate 		if (size == 0) {	/* shrinked format */
1957c478bd9Sstevel@tonic-gate 			/* get the name from the table object */
1967c478bd9Sstevel@tonic-gate 			if ((objp->zo_name = strdup(tobj->zo_name)) == NULL)
1977c478bd9Sstevel@tonic-gate 				return (FALSE);
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			/*
2007c478bd9Sstevel@tonic-gate 			 * We are opening up the xdr_string implementation here
2017c478bd9Sstevel@tonic-gate 			 * because we called xdr_u_int() earlier.
2027c478bd9Sstevel@tonic-gate 			 */
2037c478bd9Sstevel@tonic-gate 			if ((objp->zo_name = (char *)malloc(size + 1)) == NULL)
2047c478bd9Sstevel@tonic-gate 				return (FALSE);
2057c478bd9Sstevel@tonic-gate 			if (!xdr_opaque(xdrs, objp->zo_name, size))
2067c478bd9Sstevel@tonic-gate 				return (FALSE);
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * We use the xdr_nis_name_abbrev() function for both owner
2127c478bd9Sstevel@tonic-gate 	 * and group which constructs the name from the domain name.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	if (!xdr_nis_name_abbrev(xdrs, &objp->zo_owner, tobj->zo_domain))
2157c478bd9Sstevel@tonic-gate 		return (FALSE);
2167c478bd9Sstevel@tonic-gate 	if (!xdr_nis_name_abbrev(xdrs, &objp->zo_group, tobj->zo_domain))
2177c478bd9Sstevel@tonic-gate 		return (FALSE);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * While encoding of zo_domain, we put 0 in the length field, while for
2217c478bd9Sstevel@tonic-gate 	 * decoding, we get the name from the table object.  Same as above for
2227c478bd9Sstevel@tonic-gate 	 * the name.  Could have used a function instead.
2237c478bd9Sstevel@tonic-gate 	 */
2247c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE) {
2257c478bd9Sstevel@tonic-gate 		size = 0;
2267c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
2277c478bd9Sstevel@tonic-gate 			return (FALSE);
2287c478bd9Sstevel@tonic-gate 	} else {
2297c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, &size))
2307c478bd9Sstevel@tonic-gate 			return (FALSE);
2317c478bd9Sstevel@tonic-gate 		if (size == 0) {	/* shrinked format */
2327c478bd9Sstevel@tonic-gate 			/* get the name from the table object */
2337c478bd9Sstevel@tonic-gate 			if ((objp->zo_domain = strdup(tobj->zo_domain)) == NULL)
2347c478bd9Sstevel@tonic-gate 				return (FALSE);
2357c478bd9Sstevel@tonic-gate 		} else {
2367c478bd9Sstevel@tonic-gate 			/*
2377c478bd9Sstevel@tonic-gate 			 * We are opening up the xdr_string implementation here
2387c478bd9Sstevel@tonic-gate 			 * because we called xdr_u_int() earlier.
2397c478bd9Sstevel@tonic-gate 			 */
2407c478bd9Sstevel@tonic-gate 			if ((objp->zo_domain = (char *)malloc(size + 1))
2417c478bd9Sstevel@tonic-gate 				== NULL)
2427c478bd9Sstevel@tonic-gate 				return (FALSE);
2437c478bd9Sstevel@tonic-gate 			if (!xdr_opaque(xdrs, objp->zo_domain, size))
2447c478bd9Sstevel@tonic-gate 				return (FALSE);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (!xdr_u_int(xdrs, &objp->zo_access))
2497c478bd9Sstevel@tonic-gate 		return (FALSE);
2507c478bd9Sstevel@tonic-gate 	if (!xdr_u_int(xdrs, &objp->zo_ttl))
2517c478bd9Sstevel@tonic-gate 		return (FALSE);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * We know that this is an entry object, so we'll save all the entry_obj
2557c478bd9Sstevel@tonic-gate 	 * space because we can recreate it later.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE)
2587c478bd9Sstevel@tonic-gate 		return (TRUE);
2597c478bd9Sstevel@tonic-gate 	/* Now for the DECODE case, just handcraft the entries and ignore XDR */
2607c478bd9Sstevel@tonic-gate 	objp->zo_data.zo_type = NIS_ENTRY_OBJ;
2617c478bd9Sstevel@tonic-gate 	if ((objp->zo_data.objdata_u.en_data.en_type =
2627c478bd9Sstevel@tonic-gate 		strdup(tobj->zo_data.objdata_u.ta_data.ta_type)) == NULL)
2637c478bd9Sstevel@tonic-gate 		return (FALSE);
2647c478bd9Sstevel@tonic-gate 	objp->zo_data.objdata_u.en_data.en_cols.en_cols_val = NULL;
2657c478bd9Sstevel@tonic-gate 	objp->zo_data.objdata_u.en_data.en_cols.en_cols_len = 0;
2667c478bd9Sstevel@tonic-gate 	return (TRUE);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static const char	*in_directory = "IN_DIRECTORY";
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Given an input NIS+ object, create the kind
2737c478bd9Sstevel@tonic-gate  * of pseudo-entry_obj (with an XDR-encoded nis_object in the
2747c478bd9Sstevel@tonic-gate  * first column) that's stored in the DB. Note that:
2757c478bd9Sstevel@tonic-gate  *
2767c478bd9Sstevel@tonic-gate  *	If the input object is an entry, it's assumed to have the
2777c478bd9Sstevel@tonic-gate  *	columns moved up one step (col 0 in en_cols.en_cols_val[1],
2787c478bd9Sstevel@tonic-gate  *	etc.). en_cols.en_cols_val[0] will be overwritten. The
2797c478bd9Sstevel@tonic-gate  *	input object will be changed (some pointers set to zero,
2807c478bd9Sstevel@tonic-gate  *	etc.) on exit.
2817c478bd9Sstevel@tonic-gate  *
2827c478bd9Sstevel@tonic-gate  *	'eo' is assumed to be a pointer to an empty entry_obj (or,
2837c478bd9Sstevel@tonic-gate  *	at least, one that can be overwritten). It must not be a
2847c478bd9Sstevel@tonic-gate  *	pointer to the entry_obj in 'obj'. If the input object is
2857c478bd9Sstevel@tonic-gate  *	of a type other than entry, the 'eo' pointer must have
2867c478bd9Sstevel@tonic-gate  *	en_cols.en_cols_val appropriately initialized to an array of
2877c478bd9Sstevel@tonic-gate  *	(at least) length one.
2887c478bd9Sstevel@tonic-gate  *
2897c478bd9Sstevel@tonic-gate  *	'tobj' is a pointer to the table object for the table for
2907c478bd9Sstevel@tonic-gate  *	which the entry_obj is destined. It's needed for entry objects,
2917c478bd9Sstevel@tonic-gate  *	but unused for other object types.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate entry_obj *
makePseudoEntryObj(nis_object * obj,entry_obj * eo,nis_object * tobj)2947c478bd9Sstevel@tonic-gate makePseudoEntryObj(nis_object *obj, entry_obj *eo, nis_object *tobj) {
2957c478bd9Sstevel@tonic-gate 	int		bufsize;
2967c478bd9Sstevel@tonic-gate 	char		*buf;
2977c478bd9Sstevel@tonic-gate 	XDR		xdrs;
2987c478bd9Sstevel@tonic-gate 	bool_t		xret;
2997c478bd9Sstevel@tonic-gate 	uint_t		ecl;
3007c478bd9Sstevel@tonic-gate 	entry_col	*ecv;
3017c478bd9Sstevel@tonic-gate 	char		*myself = "makePseudoEntryObj";
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (obj == 0 || eo == 0)
3047c478bd9Sstevel@tonic-gate 		return (0);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
3077c478bd9Sstevel@tonic-gate 		*eo = obj->zo_data.objdata_u.en_data;
3087c478bd9Sstevel@tonic-gate 		eo->en_type = 0;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		/*
3117c478bd9Sstevel@tonic-gate 		 * To prevent the XDR function from making a copy of
3127c478bd9Sstevel@tonic-gate 		 * the entry columns, we set the columns structure to
3137c478bd9Sstevel@tonic-gate 		 * 0 (ie no column data)
3147c478bd9Sstevel@tonic-gate 		 */
3157c478bd9Sstevel@tonic-gate 		ecl = obj->EN_data.en_cols.en_cols_len;
3167c478bd9Sstevel@tonic-gate 		ecv = obj->EN_data.en_cols.en_cols_val;
3177c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_len  = 0;
3187c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val  = 0;
3197c478bd9Sstevel@tonic-gate 	} else {
3207c478bd9Sstevel@tonic-gate 		eo->en_type = (char *)in_directory;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	bufsize = xdr_sizeof(xdr_nis_object, obj);
3247c478bd9Sstevel@tonic-gate 	buf = am(myself, bufsize);
3257c478bd9Sstevel@tonic-gate 	if (buf == 0) {
3267c478bd9Sstevel@tonic-gate 		if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
3277c478bd9Sstevel@tonic-gate 			obj->EN_data.en_cols.en_cols_len = ecl;
3287c478bd9Sstevel@tonic-gate 			obj->EN_data.en_cols.en_cols_val = ecv;
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 		return (0);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, (char *)buf, bufsize, XDR_ENCODE);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
3367c478bd9Sstevel@tonic-gate 		xret = xdr_nis_fetus_object(&xdrs, obj, tobj);
3377c478bd9Sstevel@tonic-gate 	} else {
3387c478bd9Sstevel@tonic-gate 		xret = xdr_nis_object(&xdrs, obj);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* Restore the 'obj' */
3427c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_ENTRY_OBJ) {
3437c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_len = ecl;
3447c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val = ecv;
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (!xret) {
3487c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
3497c478bd9Sstevel@tonic-gate 			"%s: XDR encode failure", myself);
3507c478bd9Sstevel@tonic-gate 		sfree(buf);
3517c478bd9Sstevel@tonic-gate 		return (0);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val[0].ec_value.ec_value_val = buf;
3557c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val[0].ec_value.ec_value_len = xdr_getpos(&xdrs);
3567c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val[0].ec_flags = EN_BINARY+EN_XDR;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	return (eo);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate nis_object *
unmakePseudoEntryObj(entry_obj * e,nis_object * tobj)3627c478bd9Sstevel@tonic-gate unmakePseudoEntryObj(entry_obj *e, nis_object *tobj) {
3637c478bd9Sstevel@tonic-gate 	nis_object	*o;
3647c478bd9Sstevel@tonic-gate 	XDR		xdrs;
3657c478bd9Sstevel@tonic-gate 	bool_t		stat;
3667c478bd9Sstevel@tonic-gate 	char		*myself = "unmakePseudoEntryObj";
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (e == 0 || e->en_cols.en_cols_val == 0 ||
3697c478bd9Sstevel@tonic-gate 			e->en_cols.en_cols_len == 0)
3707c478bd9Sstevel@tonic-gate 		return (0);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	o = am(myself, sizeof (*o));
3737c478bd9Sstevel@tonic-gate 	if (o == 0)
3747c478bd9Sstevel@tonic-gate 		return (0);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, e->en_cols.en_cols_val[0].ec_value.ec_value_val,
3777c478bd9Sstevel@tonic-gate 			e->en_cols.en_cols_val[0].ec_value.ec_value_len,
3787c478bd9Sstevel@tonic-gate 			XDR_DECODE);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (tobj != 0 && (e->en_type == 0 || e->en_type[0] == '\0')) {
3817c478bd9Sstevel@tonic-gate 		stat = xdr_nis_fetus_object(&xdrs, o, tobj);
3827c478bd9Sstevel@tonic-gate 	} else {
3837c478bd9Sstevel@tonic-gate 		stat = xdr_nis_object(&xdrs, o);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (!stat) {
3877c478bd9Sstevel@tonic-gate 		sfree(o);
3887c478bd9Sstevel@tonic-gate 		o = 0;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * If it's an entry object, construct the column information.
3937c478bd9Sstevel@tonic-gate 	 * We make this a copy, so that 'o' can be freed using
3947c478bd9Sstevel@tonic-gate 	 * nis_destroy_object().
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	if (o != 0 && o->zo_data.zo_type == NIS_ENTRY_OBJ &&
3977c478bd9Sstevel@tonic-gate 			o->zo_data.objdata_u.en_data.en_cols.en_cols_val == 0 &&
3987c478bd9Sstevel@tonic-gate 			e->en_cols.en_cols_len > 1) {
3997c478bd9Sstevel@tonic-gate 		entry_col	*ec, *oec;
4007c478bd9Sstevel@tonic-gate 		uint_t		i, *ocl;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		ec = am(myself, (e->en_cols.en_cols_len - 1) * sizeof (ec[0]));
4037c478bd9Sstevel@tonic-gate 		if (ec == 0) {
4047c478bd9Sstevel@tonic-gate 			nis_destroy_object(o);
4057c478bd9Sstevel@tonic-gate 			return (0);
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		o->zo_data.objdata_u.en_data.en_cols.en_cols_val = ec;
4097c478bd9Sstevel@tonic-gate 		o->zo_data.objdata_u.en_data.en_cols.en_cols_len = 0;
4107c478bd9Sstevel@tonic-gate 		ocl = &o->zo_data.objdata_u.en_data.en_cols.en_cols_len;
4117c478bd9Sstevel@tonic-gate 		oec = e->en_cols.en_cols_val;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		for (i = 1; i < e->en_cols.en_cols_len; i++) {
4147c478bd9Sstevel@tonic-gate 			uint_t	len;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			if (oec[i].ec_value.ec_value_val != 0) {
4177c478bd9Sstevel@tonic-gate 				len = oec[i].ec_value.ec_value_len;
4187c478bd9Sstevel@tonic-gate 				if (len == 0)
4197c478bd9Sstevel@tonic-gate 					len++;
4207c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_val = am(myself, len);
4217c478bd9Sstevel@tonic-gate 				if (ec[i-1].ec_value.ec_value_val == 0) {
4227c478bd9Sstevel@tonic-gate 					nis_destroy_object(o);
4237c478bd9Sstevel@tonic-gate 					return (0);
4247c478bd9Sstevel@tonic-gate 				}
4257c478bd9Sstevel@tonic-gate 				(void) memcpy(ec[i-1].ec_value.ec_value_val,
4267c478bd9Sstevel@tonic-gate 						oec[i].ec_value.ec_value_val,
4277c478bd9Sstevel@tonic-gate 						oec[i].ec_value.ec_value_len);
4287c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_len =
4297c478bd9Sstevel@tonic-gate 						oec[i].ec_value.ec_value_len;
4307c478bd9Sstevel@tonic-gate 			} else {
4317c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_val = 0;
4327c478bd9Sstevel@tonic-gate 				ec[i-1].ec_value.ec_value_len = 0;
4337c478bd9Sstevel@tonic-gate 			}
4347c478bd9Sstevel@tonic-gate 			*ocl += 1;
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * If it's an entry, and we have the table object, make sure
4407c478bd9Sstevel@tonic-gate 	 * zo_name and en_type either already are set, or get them
4417c478bd9Sstevel@tonic-gate 	 * from the table.
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 	if (o != 0 && o->zo_data.zo_type == NIS_ENTRY_OBJ && tobj != 0) {
4447c478bd9Sstevel@tonic-gate 		if (o->zo_name == 0)
4457c478bd9Sstevel@tonic-gate 			o->zo_name = sdup(myself, T, tobj->zo_name);
4467c478bd9Sstevel@tonic-gate 		if (o->zo_data.objdata_u.en_data.en_type == 0)
4477c478bd9Sstevel@tonic-gate 			o->zo_data.objdata_u.en_data.en_type = sdup(myself, T,
4487c478bd9Sstevel@tonic-gate 				tobj->zo_data.objdata_u.ta_data.ta_type);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	return (o);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Input:  A (nis_object *), and (optionally) an (entry_obj *) array.
4567c478bd9Sstevel@tonic-gate  * Output: Pointer to an XDR:ed version of an (xdr_nis_object_t).
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate void *
xdrNisObject(nis_object * obj,entry_obj ** ea,int numEa,int * xdrLenP)4597c478bd9Sstevel@tonic-gate xdrNisObject(nis_object *obj, entry_obj **ea, int numEa, int *xdrLenP) {
4607c478bd9Sstevel@tonic-gate 	xdr_nis_object_t	xno;
4617c478bd9Sstevel@tonic-gate 	void			*buf;
4627c478bd9Sstevel@tonic-gate 	int			xdrLen;
4637c478bd9Sstevel@tonic-gate 	XDR			xdrs;
4647c478bd9Sstevel@tonic-gate 	bool_t			xret;
4657c478bd9Sstevel@tonic-gate 	char			*myself = "xdrNisObject";
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (obj == 0)
4687c478bd9Sstevel@tonic-gate 		return (0);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * The version tells us what the XDR:ed buffer contains.
4727c478bd9Sstevel@tonic-gate 	 * Should be incremented whenever xdr_nis_object_t changes
4737c478bd9Sstevel@tonic-gate 	 * incompatibly.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	xno.xversion = 1;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	xno.obj = obj;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (obj->zo_data.zo_type == NIS_DIRECTORY_OBJ &&
4807c478bd9Sstevel@tonic-gate 			ea != 0 && numEa > 0) {
4817c478bd9Sstevel@tonic-gate 		int	i;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		/*
4847c478bd9Sstevel@tonic-gate 		 * The ea[] array is expected to contain the kind of
4857c478bd9Sstevel@tonic-gate 		 * pseudo-entry object stored in the nisdb incarnation
4867c478bd9Sstevel@tonic-gate 		 * of a NIS+ directory. Column zero contains the XDR:ed
4877c478bd9Sstevel@tonic-gate 		 * directory entry object (which we ignore), while column
4887c478bd9Sstevel@tonic-gate 		 * one contains the name of said entry. It's the latter
4897c478bd9Sstevel@tonic-gate 		 * that we borrow for use in the dirEntry[] list of the
4907c478bd9Sstevel@tonic-gate 		 * xdr_nis_object_t.
4917c478bd9Sstevel@tonic-gate 		 */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_len = 0;
4947c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_val = am(myself, numEa *
4957c478bd9Sstevel@tonic-gate 			sizeof (xno.dirEntry.dirEntry_val[0]));
4967c478bd9Sstevel@tonic-gate 		if (xno.dirEntry.dirEntry_val == 0)
4977c478bd9Sstevel@tonic-gate 			return (0);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		for (i = 0; i < numEa; i++) {
5007c478bd9Sstevel@tonic-gate 			if (ea[i] == 0 || ea[i]->en_cols.en_cols_val == 0 ||
5017c478bd9Sstevel@tonic-gate 					ea[i]->en_cols.en_cols_len != 2 ||
5027c478bd9Sstevel@tonic-gate 					ea[i]->en_cols.en_cols_val[1].
5037c478bd9Sstevel@tonic-gate 						ec_value.ec_value_len == 0)
5047c478bd9Sstevel@tonic-gate 				continue;
5057c478bd9Sstevel@tonic-gate 			/*
5067c478bd9Sstevel@tonic-gate 			 * Yes, there's a NUL at the end of the dir entry
5077c478bd9Sstevel@tonic-gate 			 * name.
5087c478bd9Sstevel@tonic-gate 			 */
5097c478bd9Sstevel@tonic-gate 			xno.dirEntry.dirEntry_val[xno.dirEntry.dirEntry_len] =
5107c478bd9Sstevel@tonic-gate 				ea[i]->en_cols.en_cols_val[1].
5117c478bd9Sstevel@tonic-gate 					ec_value.ec_value_val;
5127c478bd9Sstevel@tonic-gate 			xno.dirEntry.dirEntry_len++;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	} else {
5157c478bd9Sstevel@tonic-gate 		/* No directory entries */
5167c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_len = 0;
5177c478bd9Sstevel@tonic-gate 		xno.dirEntry.dirEntry_val = 0;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	xdrLen = xdr_sizeof(xdr_xdr_nis_object_t, &xno);
5217c478bd9Sstevel@tonic-gate 	buf = am(myself, xdrLen);
5227c478bd9Sstevel@tonic-gate 	if (buf == 0)
5237c478bd9Sstevel@tonic-gate 		return (0);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, (char *)buf, xdrLen, XDR_ENCODE);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	xret = xdr_xdr_nis_object_t(&xdrs, &xno);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	sfree(xno.dirEntry.dirEntry_val);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (!xret) {
5327c478bd9Sstevel@tonic-gate 		sfree(buf);
5337c478bd9Sstevel@tonic-gate 		return (0);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (xdrLenP != 0)
5377c478bd9Sstevel@tonic-gate 		*xdrLenP = xdrLen;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	return (buf);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * Input:  Pointer to an XDR:ed version of an (xdr_nis_object_t).
5447c478bd9Sstevel@tonic-gate  * Output: Pointer to a (nis_object *) and (if the object is a
5457c478bd9Sstevel@tonic-gate  *         directory) a pointer to an array of (entry_obj *).
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate nis_object *
unXdrNisObject(void * buf,int bufLen,entry_obj *** eaP,int * numEaP)5487c478bd9Sstevel@tonic-gate unXdrNisObject(void *buf, int bufLen, entry_obj ***eaP, int *numEaP) {
5497c478bd9Sstevel@tonic-gate 	xdr_nis_object_t	*xno;
5507c478bd9Sstevel@tonic-gate 	XDR			xdrs;
5517c478bd9Sstevel@tonic-gate 	bool_t			xret;
5527c478bd9Sstevel@tonic-gate 	entry_obj		**ea;
5537c478bd9Sstevel@tonic-gate 	int			numEa;
5547c478bd9Sstevel@tonic-gate 	nis_object		*o;
5557c478bd9Sstevel@tonic-gate 	char			*myself = "unXdrNisObject";
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (buf == 0 || bufLen <= 0)
5587c478bd9Sstevel@tonic-gate 		return (0);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	xno = am(myself, sizeof (*xno));
5617c478bd9Sstevel@tonic-gate 	if (xno == 0)
5627c478bd9Sstevel@tonic-gate 		return (0);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, buf, bufLen, XDR_DECODE);
5657c478bd9Sstevel@tonic-gate 	xret = xdr_xdr_nis_object_t(&xdrs, xno);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (!xret) {
5687c478bd9Sstevel@tonic-gate 		sfree(xno);
5697c478bd9Sstevel@tonic-gate 		return (0);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	switch (xno->xversion) {
5737c478bd9Sstevel@tonic-gate 	case 1:
5747c478bd9Sstevel@tonic-gate 		break;
5757c478bd9Sstevel@tonic-gate 	default:
5767c478bd9Sstevel@tonic-gate 		xdr_free(xdr_xdr_nis_object_t, (char *)xno);
5777c478bd9Sstevel@tonic-gate 		sfree(xno);
5787c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
5797c478bd9Sstevel@tonic-gate 			"%s: Unknown xdr_nis_object_t version %d",
5807c478bd9Sstevel@tonic-gate 			myself, xno->xversion);
5817c478bd9Sstevel@tonic-gate 		return (0);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (eaP != 0 && numEaP != 0 && xno->dirEntry.dirEntry_len > 0 &&
5857c478bd9Sstevel@tonic-gate 			xno->dirEntry.dirEntry_val != 0) {
5867c478bd9Sstevel@tonic-gate 		ea = am(myself, xno->dirEntry.dirEntry_len * sizeof (ea[0]));
5877c478bd9Sstevel@tonic-gate 		if (ea == 0) {
5887c478bd9Sstevel@tonic-gate 			xdr_free(xdr_xdr_nis_object_t, (char *)xno);
5897c478bd9Sstevel@tonic-gate 			sfree(xno);
5907c478bd9Sstevel@tonic-gate 			return (0);
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		for (numEa = 0; numEa < xno->dirEntry.dirEntry_len; numEa++) {
5937c478bd9Sstevel@tonic-gate 			ea[numEa] = am(myself, sizeof (*ea[numEa]));
5947c478bd9Sstevel@tonic-gate 			if (ea[numEa] != 0) {
5957c478bd9Sstevel@tonic-gate 				ea[numEa]->en_cols.en_cols_len = 2;
5967c478bd9Sstevel@tonic-gate 				ea[numEa]->en_cols.en_cols_val = am(myself,
5977c478bd9Sstevel@tonic-gate 					ea[numEa]->en_cols.en_cols_len *
5987c478bd9Sstevel@tonic-gate 				sizeof (ea[numEa]->en_cols.en_cols_val[0]));
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 			if (ea[numEa] == 0 ||
6017c478bd9Sstevel@tonic-gate 					ea[numEa]->en_cols.en_cols_val == 0) {
6027c478bd9Sstevel@tonic-gate 				int	i;
6037c478bd9Sstevel@tonic-gate 				for (i = 0; i < numEa; i++) {
6047c478bd9Sstevel@tonic-gate 					sfree(ea[i]->en_cols.en_cols_val);
6057c478bd9Sstevel@tonic-gate 					sfree(ea[i]);
6067c478bd9Sstevel@tonic-gate 				}
6077c478bd9Sstevel@tonic-gate 				sfree(ea);
6087c478bd9Sstevel@tonic-gate 				xdr_free(xdr_xdr_nis_object_t, (char *)xno);
6097c478bd9Sstevel@tonic-gate 				sfree(xno);
6107c478bd9Sstevel@tonic-gate 				return (0);
6117c478bd9Sstevel@tonic-gate 			}
6127c478bd9Sstevel@tonic-gate 			/* Leave column 0 (XDR:ed object) empty */
6137c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[0].
6147c478bd9Sstevel@tonic-gate 				ec_value.ec_value_len = 0;
6157c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[0].
6167c478bd9Sstevel@tonic-gate 				ec_value.ec_value_val = 0;
6177c478bd9Sstevel@tonic-gate 			/*
6187c478bd9Sstevel@tonic-gate 			 * Fill in name of dir entry. The DB counts the NUL
6197c478bd9Sstevel@tonic-gate 			 * as part of the dir entry name; hence, add one
6207c478bd9Sstevel@tonic-gate 			 * to the string length.
6217c478bd9Sstevel@tonic-gate 			 */
6227c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[1].
6237c478bd9Sstevel@tonic-gate 				ec_value.ec_value_len = slen(xno->dirEntry.
6247c478bd9Sstevel@tonic-gate 					dirEntry_val[numEa]) + 1;
6257c478bd9Sstevel@tonic-gate 			ea[numEa]->en_cols.en_cols_val[1].
6267c478bd9Sstevel@tonic-gate 				ec_value.ec_value_val =
6277c478bd9Sstevel@tonic-gate 					xno->dirEntry.dirEntry_val[numEa];
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 		*eaP = ea;
6307c478bd9Sstevel@tonic-gate 		*numEaP = numEa;
6317c478bd9Sstevel@tonic-gate 		/*
6327c478bd9Sstevel@tonic-gate 		 * The xno->dirEntry.dirEntry_val[] pointers are duplicated
6337c478bd9Sstevel@tonic-gate 		 * in 'ea'. Set the xno pointers to zero, so that the xdr_free
6347c478bd9Sstevel@tonic-gate 		 * doesn't free the 'ea' data.
6357c478bd9Sstevel@tonic-gate 		 */
6367c478bd9Sstevel@tonic-gate 		if (numEa > 0) {
6377c478bd9Sstevel@tonic-gate 			int	i;
6387c478bd9Sstevel@tonic-gate 			for (i = 0; i < numEa; i++) {
6397c478bd9Sstevel@tonic-gate 				xno->dirEntry.dirEntry_val[i] = 0;
6407c478bd9Sstevel@tonic-gate 			}
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 	} else {
6437c478bd9Sstevel@tonic-gate 		if (eaP != 0)
6447c478bd9Sstevel@tonic-gate 			*eaP = 0;
6457c478bd9Sstevel@tonic-gate 		if (numEaP != 0)
6467c478bd9Sstevel@tonic-gate 			*numEaP = 0;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	o = xno->obj;
6507c478bd9Sstevel@tonic-gate 	xno->obj = 0;
6517c478bd9Sstevel@tonic-gate 	xdr_free(xdr_xdr_nis_object_t, (char *)xno);
6527c478bd9Sstevel@tonic-gate 	sfree(xno);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	return (o);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate void
freeEntryObjArray(entry_obj ** ea,int numEa)6587c478bd9Sstevel@tonic-gate freeEntryObjArray(entry_obj **ea, int numEa) {
6597c478bd9Sstevel@tonic-gate 	int	i;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (ea == 0)
6627c478bd9Sstevel@tonic-gate 		return;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	for (i = 0; i < numEa; i++) {
6657c478bd9Sstevel@tonic-gate 		int	j;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		for (j = 0; j < ea[i]->en_cols.en_cols_len; j++) {
6687c478bd9Sstevel@tonic-gate 			sfree(ea[i]->en_cols.en_cols_val[j].
6697c478bd9Sstevel@tonic-gate 				ec_value.ec_value_val);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		sfree(ea[i]->en_cols.en_cols_val);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	sfree(ea);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate /*
6797c478bd9Sstevel@tonic-gate  * Return TRUE if 'o1' and 'o2' are the same, FALSE otherwise.
6807c478bd9Sstevel@tonic-gate  * We perform the comparison by XDR encoding the objects, and then
6817c478bd9Sstevel@tonic-gate  * checking the XDR buffers for equality. However, we don't want to
6827c478bd9Sstevel@tonic-gate  * include the zo_oid (i.e., ctime and mtime) in the comparison.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate bool_t
sameNisPlusObj(nis_object * o1,nis_object * o2)6857c478bd9Sstevel@tonic-gate sameNisPlusObj(nis_object *o1, nis_object *o2) {
6867c478bd9Sstevel@tonic-gate 	XDR		x1, x2;
6877c478bd9Sstevel@tonic-gate 	void		*b1, *b2;
6887c478bd9Sstevel@tonic-gate 	int		l1, l2;
6897c478bd9Sstevel@tonic-gate 	bool_t		ret;
6907c478bd9Sstevel@tonic-gate 	nis_object	obj1, obj2;
6917c478bd9Sstevel@tonic-gate 	char		*myself = "sameNisPlusObj";
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (o1 == o2)
6947c478bd9Sstevel@tonic-gate 		return (TRUE);
6957c478bd9Sstevel@tonic-gate 	else if (o1 == 0 || o2 == 0)
6967c478bd9Sstevel@tonic-gate 		return (FALSE);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * We want to exclude the zo_oid from the comparison. In order
7007c478bd9Sstevel@tonic-gate 	 * not to modify the objects (even very briefly), we do this by
7017c478bd9Sstevel@tonic-gate 	 * making copies (nis_object itself only, not the underlying
7027c478bd9Sstevel@tonic-gate 	 * structures accessed through pointers), and setting the zo_oid
7037c478bd9Sstevel@tonic-gate 	 * to zero in the copies.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	obj1 = *o1;
7067c478bd9Sstevel@tonic-gate 	obj2 = *o2;
7077c478bd9Sstevel@tonic-gate 	obj1.zo_oid.ctime = obj1.zo_oid.mtime = 0;
7087c478bd9Sstevel@tonic-gate 	obj2.zo_oid.ctime = obj2.zo_oid.mtime = 0;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	l1 = xdr_sizeof(xdr_nis_object, &obj1);
7117c478bd9Sstevel@tonic-gate 	l2 = xdr_sizeof(xdr_nis_object, &obj2);
7127c478bd9Sstevel@tonic-gate 	if (l1 != l2)
7137c478bd9Sstevel@tonic-gate 		return (FALSE);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	b1 = am(myself, l1);
7167c478bd9Sstevel@tonic-gate 	b2 = am(myself, l2);
7177c478bd9Sstevel@tonic-gate 	if (b1 == 0 || b2 == 0) {
7187c478bd9Sstevel@tonic-gate 		sfree(b1);
7197c478bd9Sstevel@tonic-gate 		sfree(b2);
7207c478bd9Sstevel@tonic-gate 		return (FALSE);
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	xdrmem_create(&x1, (char *)b1, l1, XDR_ENCODE);
7247c478bd9Sstevel@tonic-gate 	xdrmem_create(&x2, (char *)b2, l2, XDR_ENCODE);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (xdr_nis_object(&x1, &obj1) && xdr_nis_object(&x2, &obj2)) {
7277c478bd9Sstevel@tonic-gate 		ret = (memcmp(b1, b2, l1) == 0);
7287c478bd9Sstevel@tonic-gate 	} else {
7297c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
7307c478bd9Sstevel@tonic-gate 			"%s: xdr_nis_object() error",
7317c478bd9Sstevel@tonic-gate 			myself);
7327c478bd9Sstevel@tonic-gate 		ret = FALSE;
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	sfree(b1);
7367c478bd9Sstevel@tonic-gate 	sfree(b2);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	return (ret);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * A wrapper/convenience function for sameNisPlusObj() that extracts
7437c478bd9Sstevel@tonic-gate  * the object in column zero of 'e2'.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate bool_t
sameNisPlusPseudoObj(nis_object * o1,entry_obj * e2)7467c478bd9Sstevel@tonic-gate sameNisPlusPseudoObj(nis_object *o1, entry_obj *e2) {
7477c478bd9Sstevel@tonic-gate 	nis_object	*o2;
7487c478bd9Sstevel@tonic-gate 	bool_t		res;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (o1 == 0 && e2 == 0)
7517c478bd9Sstevel@tonic-gate 		return (TRUE);
7527c478bd9Sstevel@tonic-gate 	else if (e2 == 0)
7537c478bd9Sstevel@tonic-gate 		return (FALSE);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	o2 = unmakePseudoEntryObj(e2, 0);
7567c478bd9Sstevel@tonic-gate 	if (o2 == 0)
7577c478bd9Sstevel@tonic-gate 		return ((o1 == 0) ? TRUE : FALSE);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	res = sameNisPlusObj(o1, o2);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	nis_destroy_object(o2);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	return (res);
7647c478bd9Sstevel@tonic-gate }
765