xref: /titanic_44/usr/src/lib/libnisdb/ldap_ruleval.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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
5*36e852a1SRaja Andra  * Common Development and Distribution License (the "License").
6*36e852a1SRaja Andra  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*36e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <lber.h>
287c478bd9Sstevel@tonic-gate #include <ldap.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "ldap_util.h"
347c478bd9Sstevel@tonic-gate #include "ldap_val.h"
357c478bd9Sstevel@tonic-gate #include "ldap_attr.h"
367c478bd9Sstevel@tonic-gate #include "ldap_ldap.h"
377c478bd9Sstevel@tonic-gate #include "ldap_ruleval.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Free an array of 'count' rule-value elements.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate void
freeRuleValue(__nis_rule_value_t * rv,int count)447c478bd9Sstevel@tonic-gate freeRuleValue(__nis_rule_value_t *rv, int count) {
457c478bd9Sstevel@tonic-gate 	int	n, i, j;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (rv == 0)
487c478bd9Sstevel@tonic-gate 		return;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	for (n = 0; n < count; n++) {
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 		if (rv[n].colName != 0) {
537c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numColumns; i++) {
547c478bd9Sstevel@tonic-gate 				sfree(rv[n].colName[i]);
557c478bd9Sstevel@tonic-gate 			}
567c478bd9Sstevel@tonic-gate 			free(rv[n].colName);
577c478bd9Sstevel@tonic-gate 		}
587c478bd9Sstevel@tonic-gate 		if (rv[n].colVal != 0) {
597c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numColumns; i++) {
607c478bd9Sstevel@tonic-gate 				for (j = 0; j < rv[n].colVal[i].numVals; j++) {
617c478bd9Sstevel@tonic-gate 					sfree(rv[n].colVal[i].val[j].value);
627c478bd9Sstevel@tonic-gate 				}
637c478bd9Sstevel@tonic-gate 				if (rv[n].colVal[i].numVals > 0)
647c478bd9Sstevel@tonic-gate 					sfree(rv[n].colVal[i].val);
657c478bd9Sstevel@tonic-gate 			}
667c478bd9Sstevel@tonic-gate 			free(rv[n].colVal);
677c478bd9Sstevel@tonic-gate 		}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		if (rv[n].attrName != 0) {
707c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numAttrs; i++) {
717c478bd9Sstevel@tonic-gate 				sfree(rv[n].attrName[i]);
727c478bd9Sstevel@tonic-gate 			}
737c478bd9Sstevel@tonic-gate 			free(rv[n].attrName);
747c478bd9Sstevel@tonic-gate 		}
757c478bd9Sstevel@tonic-gate 		if (rv[n].attrVal != 0) {
767c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numAttrs; i++) {
777c478bd9Sstevel@tonic-gate 				for (j = 0; j < rv[n].attrVal[i].numVals;
787c478bd9Sstevel@tonic-gate 						j++) {
797c478bd9Sstevel@tonic-gate 					sfree(rv[n].attrVal[i].val[j].value);
807c478bd9Sstevel@tonic-gate 				}
817c478bd9Sstevel@tonic-gate 				if (rv[n].attrVal[i].numVals > 0)
827c478bd9Sstevel@tonic-gate 					sfree(rv[n].attrVal[i].val);
837c478bd9Sstevel@tonic-gate 			}
847c478bd9Sstevel@tonic-gate 			free(rv[n].attrVal);
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	sfree(rv);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Return an array of 'count' __nis_rule_value_t elements, initialized
937c478bd9Sstevel@tonic-gate  * to be copies of 'rvIn' if supplied; empty otherwise.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate __nis_rule_value_t *
initRuleValue(int count,__nis_rule_value_t * rvIn)967c478bd9Sstevel@tonic-gate initRuleValue(int count, __nis_rule_value_t *rvIn) {
977c478bd9Sstevel@tonic-gate 	return (growRuleValue(0, count, 0, rvIn));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static const __nis_rule_value_t	rvZero = {0};
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Grow 'old' from 'oldCount' to 'newCount' elements, initialize the
1047c478bd9Sstevel@tonic-gate  * new portion to 'rvIn' (empty if not supplied), and return a pointer
1057c478bd9Sstevel@tonic-gate  * to the result. Following a call to this function, the caller must
1067c478bd9Sstevel@tonic-gate  * refer only to the returned array, not to 'old'.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate __nis_rule_value_t *
growRuleValue(int oldCount,int newCount,__nis_rule_value_t * old,__nis_rule_value_t * rvIn)1097c478bd9Sstevel@tonic-gate growRuleValue(int oldCount, int newCount, __nis_rule_value_t *old,
1107c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rvIn) {
1117c478bd9Sstevel@tonic-gate 	__nis_rule_value_t	*rv;
1127c478bd9Sstevel@tonic-gate 	int			i, j;
1137c478bd9Sstevel@tonic-gate 	char			*myself = "growRuleValue";
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (newCount <= 0 || newCount <= oldCount)
1167c478bd9Sstevel@tonic-gate 		return (old);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (oldCount <= 0) {
1197c478bd9Sstevel@tonic-gate 		oldCount = 0;
1207c478bd9Sstevel@tonic-gate 		old = 0;
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (rvIn == 0)
1247c478bd9Sstevel@tonic-gate 		rvIn = (__nis_rule_value_t *)&rvZero;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	rv = realloc(old, newCount * sizeof (rv[0]));
1277c478bd9Sstevel@tonic-gate 	if (rv == 0) {
1287c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOMEM, LOG_ERR,
1297c478bd9Sstevel@tonic-gate 			"%s: realloc(%d ((%d+%d)*%d)) => 0",
1307c478bd9Sstevel@tonic-gate 			myself, (oldCount+newCount) * sizeof (rv[0]),
1317c478bd9Sstevel@tonic-gate 			oldCount, newCount, sizeof (rv[0]));
1327c478bd9Sstevel@tonic-gate 		freeRuleValue(old, oldCount);
1337c478bd9Sstevel@tonic-gate 		return (0);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	(void) memset(&rv[oldCount], 0, (newCount-oldCount)*sizeof (rv[0]));
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	for (i = oldCount; i < newCount; i++) {
1397c478bd9Sstevel@tonic-gate 		rv[i].numColumns = rvIn->numColumns;
1407c478bd9Sstevel@tonic-gate 		if (rv[i].numColumns > 0) {
1417c478bd9Sstevel@tonic-gate 			rv[i].colName = cloneName(rvIn->colName,
1427c478bd9Sstevel@tonic-gate 					rv[i].numColumns);
1437c478bd9Sstevel@tonic-gate 			rv[i].colVal = cloneValue(rvIn->colVal,
1447c478bd9Sstevel@tonic-gate 					rv[i].numColumns);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		if (rv[i].numColumns > 0 &&
1477c478bd9Sstevel@tonic-gate 				(rv[i].colName == 0 || rv[i].colVal == 0)) {
1487c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, i);
1497c478bd9Sstevel@tonic-gate 			return (0);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		rv[i].numAttrs = rvIn->numAttrs;
1527c478bd9Sstevel@tonic-gate 		rv[i].attrName = cloneName(rvIn->attrName, rv[i].numAttrs);
1537c478bd9Sstevel@tonic-gate 		rv[i].attrVal = cloneValue(rvIn->attrVal, rv[i].numAttrs);
1547c478bd9Sstevel@tonic-gate 		if (rv[i].numAttrs > 0 &&
1557c478bd9Sstevel@tonic-gate 			(rv[i].attrName == 0 || rv[i].attrVal == 0)) {
1567c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, i);
1577c478bd9Sstevel@tonic-gate 			return (0);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return (rv);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Merge the source rule-value 's' into the target rule-value 't'.
1667c478bd9Sstevel@tonic-gate  * If successful, unless 's' is a sub-set of 't', 't' will be changed
1677c478bd9Sstevel@tonic-gate  * on exit, and will contain the values from 's' as well.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate int
mergeRuleValue(__nis_rule_value_t * t,__nis_rule_value_t * s)1707c478bd9Sstevel@tonic-gate mergeRuleValue(__nis_rule_value_t *t, __nis_rule_value_t *s) {
1717c478bd9Sstevel@tonic-gate 	int	i, j;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (s == 0)
1747c478bd9Sstevel@tonic-gate 		return (0);
1757c478bd9Sstevel@tonic-gate 	else if (t == 0)
1767c478bd9Sstevel@tonic-gate 		return (-1);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	for (i = 0; i < s->numColumns; i++) {
1797c478bd9Sstevel@tonic-gate 		for (j = 0; j < s->colVal[i].numVals; j++) {
1807c478bd9Sstevel@tonic-gate 			if (addCol2RuleValue(s->colVal[i].type, s->colName[i],
1817c478bd9Sstevel@tonic-gate 					s->colVal[i].val[j].value,
1827c478bd9Sstevel@tonic-gate 					s->colVal[i].val[j].length,
1837c478bd9Sstevel@tonic-gate 					t))
1847c478bd9Sstevel@tonic-gate 				return (-1);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	for (i = 0; i < s->numAttrs; i++) {
1897c478bd9Sstevel@tonic-gate 		for (j = 0; j < s->attrVal[i].numVals; j++) {
1907c478bd9Sstevel@tonic-gate 			if (addAttr2RuleValue(s->attrVal[i].type,
1917c478bd9Sstevel@tonic-gate 					s->attrName[i],
1927c478bd9Sstevel@tonic-gate 					s->attrVal[i].val[j].value,
1937c478bd9Sstevel@tonic-gate 					s->attrVal[i].val[j].length,
1947c478bd9Sstevel@tonic-gate 					t))
1957c478bd9Sstevel@tonic-gate 				return (-1);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static int
addVal2RuleValue(char * msg,int caseSens,int snipNul,__nis_value_type_t type,char * name,void * value,int valueLen,int * numP,char *** inNameP,__nis_value_t ** inValP)2037c478bd9Sstevel@tonic-gate addVal2RuleValue(char *msg, int caseSens, int snipNul, __nis_value_type_t type,
2047c478bd9Sstevel@tonic-gate 		char *name, void *value, int valueLen,
2057c478bd9Sstevel@tonic-gate 		int *numP, char ***inNameP, __nis_value_t **inValP) {
2067c478bd9Sstevel@tonic-gate 	int			i, j, copyLen = valueLen;
2077c478bd9Sstevel@tonic-gate 	__nis_single_value_t	*v;
2087c478bd9Sstevel@tonic-gate 	char			**inName = *inNameP;
2097c478bd9Sstevel@tonic-gate 	__nis_value_t		*inVal = *inValP;
2107c478bd9Sstevel@tonic-gate 	int			num = *numP;
2117c478bd9Sstevel@tonic-gate 	int			(*comp)(const char *s1, const char *s2);
2127c478bd9Sstevel@tonic-gate 	char			*myself = "addVal2RuleValue";
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* Internal function, so assume arguments OK */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (msg == 0)
2177c478bd9Sstevel@tonic-gate 		msg = myself;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* Should we match the 'inName' value case sensitive or not ? */
2207c478bd9Sstevel@tonic-gate 	if (caseSens)
2217c478bd9Sstevel@tonic-gate 		comp = strcmp;
2227c478bd9Sstevel@tonic-gate 	else
2237c478bd9Sstevel@tonic-gate 		comp = strcasecmp;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * String-valued NIS+ entries count the concluding NUL in the
2277c478bd9Sstevel@tonic-gate 	 * length, while LDAP entries don't. In order to support this,
2287c478bd9Sstevel@tonic-gate 	 * we implement the following for vt_string value types:
2297c478bd9Sstevel@tonic-gate 	 *
2307c478bd9Sstevel@tonic-gate 	 * If the last byte of the value isn't a NUL, add one to the
2317c478bd9Sstevel@tonic-gate 	 * allocated length, so that there always is a NUL after the
2327c478bd9Sstevel@tonic-gate 	 * value, making it safe to pass to strcmp() etc.
2337c478bd9Sstevel@tonic-gate 	 *
2347c478bd9Sstevel@tonic-gate 	 * If 'snipNul' is set (presumably meaning we're inserting a
2357c478bd9Sstevel@tonic-gate 	 * value derived from a NIS+ entry), and the last byte of the
2367c478bd9Sstevel@tonic-gate 	 * value already is a NUL, decrement the length to be copied by
2377c478bd9Sstevel@tonic-gate 	 * one. This (a) doesn't count the NUL in the value length, but
2387c478bd9Sstevel@tonic-gate 	 * (b) still leaves a NUL following the value.
2397c478bd9Sstevel@tonic-gate 	 *
2407c478bd9Sstevel@tonic-gate 	 * In N2L, for all cases we set 'copyLen' to the number of non-0
2417c478bd9Sstevel@tonic-gate 	 * characters in 'value'.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	if (type == vt_string && valueLen > 0) {
2447c478bd9Sstevel@tonic-gate 		char	*charval = value;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		if (charval[valueLen-1] != '\0')
2477c478bd9Sstevel@tonic-gate 			valueLen += 1;
2487c478bd9Sstevel@tonic-gate 		else if (yp2ldap || snipNul)
2497c478bd9Sstevel@tonic-gate 			copyLen -= 1;
2507c478bd9Sstevel@tonic-gate 	} else if (valueLen == 0) {
2517c478bd9Sstevel@tonic-gate 		/*
2527c478bd9Sstevel@tonic-gate 		 * If the 'value' pointer is non-NULL, we create a zero-
2537c478bd9Sstevel@tonic-gate 		 * length value with one byte allocated. This takes care
2547c478bd9Sstevel@tonic-gate 		 * of empty strings.
2557c478bd9Sstevel@tonic-gate 		 */
2567c478bd9Sstevel@tonic-gate 		valueLen += 1;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* If we already have values for this attribute, add another one */
2607c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
2617c478bd9Sstevel@tonic-gate 		if ((*comp)(inName[i], name) == 0) {
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			/*
2647c478bd9Sstevel@tonic-gate 			 * Our caller often doesn't know the type of the
2657c478bd9Sstevel@tonic-gate 			 * value; this happens because the type (vt_string
2667c478bd9Sstevel@tonic-gate 			 * or vt_ber) is determined by the format in the
2677c478bd9Sstevel@tonic-gate 			 * rule sets, and we may be invoked as a preparation
2687c478bd9Sstevel@tonic-gate 			 * for evaluating the rules. Hence, we only use the
2697c478bd9Sstevel@tonic-gate 			 * supplied 'type' if we need to create a value.
2707c478bd9Sstevel@tonic-gate 			 * Otherwise, we accept mixed types.
2717c478bd9Sstevel@tonic-gate 			 *
2727c478bd9Sstevel@tonic-gate 			 * Strings are OK in any case, since we always make
2737c478bd9Sstevel@tonic-gate 			 * sure to have a zero byte at the end of any value,
2747c478bd9Sstevel@tonic-gate 			 * whatever the type.
2757c478bd9Sstevel@tonic-gate 			 */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			if (inVal[i].numVals < 0) {
2787c478bd9Sstevel@tonic-gate 				/*
2797c478bd9Sstevel@tonic-gate 				 * Used to indicate deletion of attribute,
2807c478bd9Sstevel@tonic-gate 				 * so we honor that and don't add a value.
2817c478bd9Sstevel@tonic-gate 				 */
2827c478bd9Sstevel@tonic-gate 				return (0);
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 			/*
2867c478bd9Sstevel@tonic-gate 			 * If 'value' is NULL, we should delete, so
2877c478bd9Sstevel@tonic-gate 			 * remove any existing values, and set the
2887c478bd9Sstevel@tonic-gate 			 * 'numVals' field to -1.
2897c478bd9Sstevel@tonic-gate 			 */
2907c478bd9Sstevel@tonic-gate 			if (value == 0) {
2917c478bd9Sstevel@tonic-gate 				for (j = 0; j < inVal[i].numVals; j++) {
2927c478bd9Sstevel@tonic-gate 					sfree(inVal[i].val[j].value);
2937c478bd9Sstevel@tonic-gate 				}
2947c478bd9Sstevel@tonic-gate 				sfree(inVal[i].val);
2957c478bd9Sstevel@tonic-gate 				inVal[i].val = 0;
2967c478bd9Sstevel@tonic-gate 				inVal[i].numVals = -1;
2977c478bd9Sstevel@tonic-gate 				return (0);
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			/* Is the value a duplicate ? */
3017c478bd9Sstevel@tonic-gate 			for (j = 0; j < inVal[i].numVals; j++) {
3027c478bd9Sstevel@tonic-gate 				if (copyLen == inVal[i].val[j].length &&
3037c478bd9Sstevel@tonic-gate 					memcmp(value, inVal[i].val[j].value,
3047c478bd9Sstevel@tonic-gate 						copyLen) == 0) {
3057c478bd9Sstevel@tonic-gate 					break;
3067c478bd9Sstevel@tonic-gate 				}
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			if (j < inVal[i].numVals)
3097c478bd9Sstevel@tonic-gate 				return (0);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 			/* Not a duplicate, so add the name/value pair */
3127c478bd9Sstevel@tonic-gate 			v = realloc(inVal[i].val,
3137c478bd9Sstevel@tonic-gate 					(inVal[i].numVals+1) *
3147c478bd9Sstevel@tonic-gate 					sizeof (inVal[i].val[0]));
3157c478bd9Sstevel@tonic-gate 			if (v == 0)
3167c478bd9Sstevel@tonic-gate 				return (-1);
3177c478bd9Sstevel@tonic-gate 			inVal[i].val = v;
3187c478bd9Sstevel@tonic-gate 			v[inVal[i].numVals].length = copyLen;
3197c478bd9Sstevel@tonic-gate 			v[inVal[i].numVals].value = am(msg, valueLen);
3207c478bd9Sstevel@tonic-gate 			if (v[inVal[i].numVals].value == 0 &&
3217c478bd9Sstevel@tonic-gate 					value != 0) {
3227c478bd9Sstevel@tonic-gate 				sfree(v);
3237c478bd9Sstevel@tonic-gate 				return (-1);
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate 			memcpy(v[inVal[i].numVals].value, value, copyLen);
3267c478bd9Sstevel@tonic-gate 			inVal[i].numVals++;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 			return (0);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/* No previous value for this attribute */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * value == 0 means deletion, in which case we create a
3367c478bd9Sstevel@tonic-gate 	 * __nis_value_t with the numVals field set to -1.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	if (value != 0) {
3397c478bd9Sstevel@tonic-gate 		if ((v = am(msg, sizeof (*v))) == 0)
3407c478bd9Sstevel@tonic-gate 			return (-1);
3417c478bd9Sstevel@tonic-gate 		v->length = copyLen;
3427c478bd9Sstevel@tonic-gate 		v->value = am(msg, valueLen);
3437c478bd9Sstevel@tonic-gate 		if (v->value == 0 && value != 0) {
3447c478bd9Sstevel@tonic-gate 			sfree(v);
3457c478bd9Sstevel@tonic-gate 			return (-1);
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 		memcpy(v->value, value, copyLen);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	inVal = realloc(inVal, (num+1)*sizeof (inVal[0]));
3517c478bd9Sstevel@tonic-gate 	if (inVal == 0) {
3527c478bd9Sstevel@tonic-gate 		if (value != 0) {
3537c478bd9Sstevel@tonic-gate 			sfree(v->value);
3547c478bd9Sstevel@tonic-gate 			sfree(v);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		return (-1);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	*inValP = inVal;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	inName = realloc(inName,
3617c478bd9Sstevel@tonic-gate 		(num+1)*sizeof (inName[0]));
3627c478bd9Sstevel@tonic-gate 	if (inName == 0 || (inName[num] =
3637c478bd9Sstevel@tonic-gate 			sdup(msg, T, name)) == 0) {
3647c478bd9Sstevel@tonic-gate 		sfree(v->value);
3657c478bd9Sstevel@tonic-gate 		sfree(v);
3667c478bd9Sstevel@tonic-gate 		return (-1);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	*inNameP = inName;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	inVal[num].type = type;
3717c478bd9Sstevel@tonic-gate 	inVal[num].repeat = 0;
3727c478bd9Sstevel@tonic-gate 	if (value != 0) {
3737c478bd9Sstevel@tonic-gate 		inVal[num].numVals = 1;
3747c478bd9Sstevel@tonic-gate 		inVal[num].val = v;
3757c478bd9Sstevel@tonic-gate 	} else {
3767c478bd9Sstevel@tonic-gate 		inVal[num].numVals = -1;
3777c478bd9Sstevel@tonic-gate 		inVal[num].val = 0;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	*numP += 1;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	return (0);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate int
addAttr2RuleValue(__nis_value_type_t type,char * name,void * value,int valueLen,__nis_rule_value_t * rv)3867c478bd9Sstevel@tonic-gate addAttr2RuleValue(__nis_value_type_t type, char *name, void *value,
3877c478bd9Sstevel@tonic-gate 		int valueLen, __nis_rule_value_t *rv) {
3887c478bd9Sstevel@tonic-gate 	char			*myself = "addAttr2RuleValue";
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (name == 0 || rv == 0)
3917c478bd9Sstevel@tonic-gate 		return (-1);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	return (addVal2RuleValue(myself, 0, 0, type, name, value, valueLen,
3947c478bd9Sstevel@tonic-gate 				&rv->numAttrs, &rv->attrName, &rv->attrVal));
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate int
addSAttr2RuleValue(char * name,char * value,__nis_rule_value_t * rv)3987c478bd9Sstevel@tonic-gate addSAttr2RuleValue(char *name, char *value, __nis_rule_value_t *rv) {
3997c478bd9Sstevel@tonic-gate 	return (addAttr2RuleValue(vt_string, name, value, slen(value), rv));
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate int
addCol2RuleValue(__nis_value_type_t type,char * name,void * value,int valueLen,__nis_rule_value_t * rv)4037c478bd9Sstevel@tonic-gate addCol2RuleValue(__nis_value_type_t type, char *name, void *value,
4047c478bd9Sstevel@tonic-gate 		int valueLen, __nis_rule_value_t *rv) {
4057c478bd9Sstevel@tonic-gate 	char *myself = "addCol2RuleValue";
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (name == 0 || rv == 0)
4087c478bd9Sstevel@tonic-gate 		return (-1);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (addVal2RuleValue(myself, 1, 1, type, name, value, valueLen,
4117c478bd9Sstevel@tonic-gate 				&rv->numColumns, &rv->colName, &rv->colVal));
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate int
addSCol2RuleValue(char * name,char * value,__nis_rule_value_t * rv)4157c478bd9Sstevel@tonic-gate addSCol2RuleValue(char *name, char *value, __nis_rule_value_t *rv) {
4167c478bd9Sstevel@tonic-gate 	return (addCol2RuleValue(vt_string, name, value, slen(value), rv));
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * Given a table mapping, a NIS+ DB query, and (optionally) an existing
4217c478bd9Sstevel@tonic-gate  * and compatible __nis_rule_value_t, return a new __nis_rule_value_t
4227c478bd9Sstevel@tonic-gate  * with the values from the query added.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate __nis_rule_value_t *
buildNisPlusRuleValue(__nis_table_mapping_t * t,db_query * q,__nis_rule_value_t * rv)4257c478bd9Sstevel@tonic-gate buildNisPlusRuleValue(__nis_table_mapping_t *t, db_query *q,
4267c478bd9Sstevel@tonic-gate 			__nis_rule_value_t *rv) {
4277c478bd9Sstevel@tonic-gate 	int			i;
4287c478bd9Sstevel@tonic-gate 	__nis_single_value_t	*sv;
4297c478bd9Sstevel@tonic-gate 	char			*myself = "buildNisPlusRuleValue";
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (t == 0 || q == 0)
4327c478bd9Sstevel@tonic-gate 		return (0);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	rv = initRuleValue(1, rv);
4357c478bd9Sstevel@tonic-gate 	if (rv == 0)
4367c478bd9Sstevel@tonic-gate 		return (0);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (i = 0; i < q->components.components_len; i++) {
4397c478bd9Sstevel@tonic-gate 		int	ic;
4407c478bd9Sstevel@tonic-gate 		int	iv, v, dup;
4417c478bd9Sstevel@tonic-gate 		int	len;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		/* Ignore out-of-range column index */
4447c478bd9Sstevel@tonic-gate 		if (q->components.components_val[i].which_index >=
4457c478bd9Sstevel@tonic-gate 				t->numColumns)
4467c478bd9Sstevel@tonic-gate 			continue;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		/*
4497c478bd9Sstevel@tonic-gate 		 * Add the query value. A NULL value indicates deletion,
4507c478bd9Sstevel@tonic-gate 		 * but addCol2RuleValue() takes care of that for us.
4517c478bd9Sstevel@tonic-gate 		 */
4527c478bd9Sstevel@tonic-gate 		if (addCol2RuleValue(vt_string,
4537c478bd9Sstevel@tonic-gate 				t->column[q->components.components_val[i].
4547c478bd9Sstevel@tonic-gate 						which_index],
4557c478bd9Sstevel@tonic-gate 				q->components.components_val[i].index_value->
4567c478bd9Sstevel@tonic-gate 					itemvalue.itemvalue_val,
4577c478bd9Sstevel@tonic-gate 				q->components.components_val[i].index_value->
4587c478bd9Sstevel@tonic-gate 					itemvalue.itemvalue_len, rv) != 0) {
4597c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, 1);
4607c478bd9Sstevel@tonic-gate 			rv = 0;
4617c478bd9Sstevel@tonic-gate 			break;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	return (rv);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate  * Given a LHS rule 'rl', return an array containing the item names,
4717c478bd9Sstevel@tonic-gate  * and the number of elements in the array in '*numItems'.
4727c478bd9Sstevel@tonic-gate  *
4737c478bd9Sstevel@tonic-gate  * If there are 'me_match' __nis_mapping_element_t's, we use the
4747c478bd9Sstevel@tonic-gate  * supplied '*rval' (if any) to derive values for the items in
4757c478bd9Sstevel@tonic-gate  * the 'me_match', and add the values thus derived to '*rval' (in
4767c478bd9Sstevel@tonic-gate  * which case the '*rval' pointer will change; the old '*rval'
4777c478bd9Sstevel@tonic-gate  * is deleted).
4787c478bd9Sstevel@tonic-gate  */
4797c478bd9Sstevel@tonic-gate __nis_mapping_item_t *
buildLvalue(__nis_mapping_rlhs_t * rl,__nis_value_t ** rval,int * numItems)4807c478bd9Sstevel@tonic-gate buildLvalue(__nis_mapping_rlhs_t *rl, __nis_value_t **rval, int *numItems) {
4817c478bd9Sstevel@tonic-gate 	__nis_value_t		*val, *r;
4827c478bd9Sstevel@tonic-gate 	__nis_mapping_item_t	*item = 0;
4837c478bd9Sstevel@tonic-gate 	int			i, n, ni = 0, nv = 0;
4847c478bd9Sstevel@tonic-gate 	int			repeat = 0;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (rl == 0)
4877c478bd9Sstevel@tonic-gate 		return (0);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (rval != 0) {
4907c478bd9Sstevel@tonic-gate 		r = *rval;
4917c478bd9Sstevel@tonic-gate 		repeat = r->repeat;
4927c478bd9Sstevel@tonic-gate 	} else
4937c478bd9Sstevel@tonic-gate 		r = 0;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/* If there is more than one element, we concatenate the items */
4967c478bd9Sstevel@tonic-gate 	for (i = 0; i < rl->numElements; i++) {
4977c478bd9Sstevel@tonic-gate 		__nis_mapping_element_t	*e = &rl->element[i];
4987c478bd9Sstevel@tonic-gate 		__nis_mapping_item_t	*olditem, *tmpitem = 0;
4997c478bd9Sstevel@tonic-gate 		__nis_value_t		**tmp;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		switch (e->type) {
5027c478bd9Sstevel@tonic-gate 		case me_item:
5037c478bd9Sstevel@tonic-gate 			tmpitem = cloneItem(&e->element.item);
5047c478bd9Sstevel@tonic-gate 			break;
5057c478bd9Sstevel@tonic-gate 		case me_match:
5067c478bd9Sstevel@tonic-gate 			/*
5077c478bd9Sstevel@tonic-gate 			 * Obtain values for the items in the 'me_match'
5087c478bd9Sstevel@tonic-gate 			 * element.
5097c478bd9Sstevel@tonic-gate 			 */
5107c478bd9Sstevel@tonic-gate 			tmp = matchMappingItem(e->element.match.fmt, r, &nv,
5117c478bd9Sstevel@tonic-gate 				0, 0);
5127c478bd9Sstevel@tonic-gate 			if (tmp != 0) {
5137c478bd9Sstevel@tonic-gate 				freeValue(r, 1);
5147c478bd9Sstevel@tonic-gate 				val = 0;
5157c478bd9Sstevel@tonic-gate 				for (n = 0; n < nv; n++) {
5167c478bd9Sstevel@tonic-gate 					r = concatenateValues(val, tmp[n]);
5177c478bd9Sstevel@tonic-gate 					freeValue(val, 1);
5187c478bd9Sstevel@tonic-gate 					freeValue(tmp[n], 1);
5197c478bd9Sstevel@tonic-gate 					val = r;
5207c478bd9Sstevel@tonic-gate 					if (val == 0) {
5217c478bd9Sstevel@tonic-gate 						for (n++; n < nv; n++) {
5227c478bd9Sstevel@tonic-gate 							freeValue(tmp[n], 1);
5237c478bd9Sstevel@tonic-gate 						}
5247c478bd9Sstevel@tonic-gate 						break;
5257c478bd9Sstevel@tonic-gate 					}
5267c478bd9Sstevel@tonic-gate 				}
5277c478bd9Sstevel@tonic-gate 				free(tmp);
5287c478bd9Sstevel@tonic-gate 				if (rval != 0) {
5297c478bd9Sstevel@tonic-gate 					if (repeat && val != 0)
5307c478bd9Sstevel@tonic-gate 						val->repeat = repeat;
5317c478bd9Sstevel@tonic-gate 					*rval = val;
5327c478bd9Sstevel@tonic-gate 				}
5337c478bd9Sstevel@tonic-gate 				for (n = 0; n < e->element.match.numItems;
5347c478bd9Sstevel@tonic-gate 						n++) {
5357c478bd9Sstevel@tonic-gate 					olditem = item;
5367c478bd9Sstevel@tonic-gate 					item = concatenateMappingItem(item, ni,
5377c478bd9Sstevel@tonic-gate 						&e->element.match.item[n]);
5387c478bd9Sstevel@tonic-gate 					freeMappingItem(olditem, ni);
5397c478bd9Sstevel@tonic-gate 					if (item == 0) {
5407c478bd9Sstevel@tonic-gate 						ni = 0;
5417c478bd9Sstevel@tonic-gate 						break;
5427c478bd9Sstevel@tonic-gate 					}
5437c478bd9Sstevel@tonic-gate 					ni++;
5447c478bd9Sstevel@tonic-gate 				}
5457c478bd9Sstevel@tonic-gate 			}
5467c478bd9Sstevel@tonic-gate 			break;
5477c478bd9Sstevel@tonic-gate 		case me_print:
5487c478bd9Sstevel@tonic-gate 		case me_split:
5497c478bd9Sstevel@tonic-gate 		case me_extract:
5507c478bd9Sstevel@tonic-gate 		default:
5517c478bd9Sstevel@tonic-gate 			/* These shouldn't show up on the LHS; ignore */
5527c478bd9Sstevel@tonic-gate 			break;
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (tmpitem != 0) {
5567c478bd9Sstevel@tonic-gate 			olditem = item;
5577c478bd9Sstevel@tonic-gate 			item = concatenateMappingItem(item, ni, tmpitem);
5587c478bd9Sstevel@tonic-gate 			freeMappingItem(olditem, ni);
5597c478bd9Sstevel@tonic-gate 			freeMappingItem(tmpitem, 1);
5607c478bd9Sstevel@tonic-gate 			ni++;
5617c478bd9Sstevel@tonic-gate 			if (item == 0) {
5627c478bd9Sstevel@tonic-gate 				ni = 0;
5637c478bd9Sstevel@tonic-gate 				break;
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (numItems != 0)
5697c478bd9Sstevel@tonic-gate 		*numItems = ni;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	return (item);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate __nis_value_t *
buildRvalue(__nis_mapping_rlhs_t * rl,__nis_mapping_item_type_t native,__nis_rule_value_t * rv,int * stat)5757c478bd9Sstevel@tonic-gate buildRvalue(__nis_mapping_rlhs_t *rl, __nis_mapping_item_type_t native,
5767c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv, int *stat) {
5777c478bd9Sstevel@tonic-gate 	__nis_value_t	*val, *vold = 0, *vnew;
5787c478bd9Sstevel@tonic-gate 	int		i;
5797c478bd9Sstevel@tonic-gate 	char		*myself = "buildRvalue";
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (rl == 0 || rl->numElements <= 0) {
5827c478bd9Sstevel@tonic-gate 		/*
5837c478bd9Sstevel@tonic-gate 		 * No RHS indicates deletion, as does a __nis_value_t
5847c478bd9Sstevel@tonic-gate 		 * with numVals == -1, so we return such a creature.
5857c478bd9Sstevel@tonic-gate 		 */
5867c478bd9Sstevel@tonic-gate 		val = am(myself, sizeof (*val));
5877c478bd9Sstevel@tonic-gate 		if (val != 0) {
5887c478bd9Sstevel@tonic-gate 			val->type = vt_string;
5897c478bd9Sstevel@tonic-gate 			val->numVals = -1;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		return (val);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/* If there is more than one element, we concatenate the values */
5957c478bd9Sstevel@tonic-gate 	for (i = 0; i < rl->numElements; i++) {
5967c478bd9Sstevel@tonic-gate 		vnew = getMappingElement(&rl->element[i], native, rv, stat);
5977c478bd9Sstevel@tonic-gate 		val = concatenateValues(vold, vnew);
5987c478bd9Sstevel@tonic-gate 		freeValue(vnew, 1);
5997c478bd9Sstevel@tonic-gate 		freeValue(vold, 1);
6007c478bd9Sstevel@tonic-gate 		vold = val;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	return (val);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Derive values for the LDAP attributes specified by the rule 'r',
6077c478bd9Sstevel@tonic-gate  * and add them to the rule-value 'rv'.
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * If 'doAssign' is set, out-of-context assignments are performed,
6107c478bd9Sstevel@tonic-gate  * otherwise not.
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate __nis_rule_value_t *
addLdapRuleValue(__nis_table_mapping_t * t,__nis_mapping_rule_t * r,__nis_mapping_item_type_t lnative,__nis_mapping_item_type_t rnative,__nis_rule_value_t * rv,int doAssign,int * stat)6137c478bd9Sstevel@tonic-gate addLdapRuleValue(__nis_table_mapping_t *t,
6147c478bd9Sstevel@tonic-gate 			__nis_mapping_rule_t *r,
6157c478bd9Sstevel@tonic-gate 			__nis_mapping_item_type_t lnative,
6167c478bd9Sstevel@tonic-gate 			__nis_mapping_item_type_t rnative,
6177c478bd9Sstevel@tonic-gate 			__nis_rule_value_t *rv,
6187c478bd9Sstevel@tonic-gate 			int doAssign, int *stat) {
6197c478bd9Sstevel@tonic-gate 	int			i, j;
6207c478bd9Sstevel@tonic-gate 	char			**new;
6217c478bd9Sstevel@tonic-gate 	__nis_value_t		*rval, *lval;
6227c478bd9Sstevel@tonic-gate 	__nis_buffer_t		b = {0, 0};
6237c478bd9Sstevel@tonic-gate 	__nis_mapping_item_t	*litem;
6247c478bd9Sstevel@tonic-gate 	int			numItems;
6257c478bd9Sstevel@tonic-gate 	char			**dn = 0;
6267c478bd9Sstevel@tonic-gate 	int			numDN = 0;
6277c478bd9Sstevel@tonic-gate 	char			*myself = "addLdapRuleValue";
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* Do we have the required values ? */
6317c478bd9Sstevel@tonic-gate 	if (rv == 0)
6327c478bd9Sstevel@tonic-gate 		return (0);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/*
6357c478bd9Sstevel@tonic-gate 	 * Establish appropriate search base. For rnative == mit_nisplus,
6367c478bd9Sstevel@tonic-gate 	 * we're deriving LDAP attribute values from NIS+ columns; in other
6377c478bd9Sstevel@tonic-gate 	 * words, we're writing to LDAP, and should use the write.base value.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	__nisdb_get_tsd()->searchBase = (rnative == mit_nisplus) ?
6407c478bd9Sstevel@tonic-gate 		t->objectDN->write.base : t->objectDN->read.base;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/* Set escapeFlag if LHS is "dn" to escape special chars */
6437c478bd9Sstevel@tonic-gate 	if (yp2ldap && r->lhs.numElements == 1 &&
6447c478bd9Sstevel@tonic-gate 		r->lhs.element->type == me_item &&
6457c478bd9Sstevel@tonic-gate 		r->lhs.element->element.item.type == mit_ldap &&
6467c478bd9Sstevel@tonic-gate 		strcasecmp(r->lhs.element->element.item.name, "dn") == 0) {
6477c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->escapeFlag = '1';
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/* Build the RHS value */
6517c478bd9Sstevel@tonic-gate 	rval = buildRvalue(&r->rhs, rnative, rv, stat);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/* Reset escapeFlag */
6547c478bd9Sstevel@tonic-gate 	__nisdb_get_tsd()->escapeFlag = '\0';
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (rval == 0)
6577c478bd9Sstevel@tonic-gate 		return (rv);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * Special case: If we got no value for the RHS (presumably because
6617c478bd9Sstevel@tonic-gate 	 * we're missing one or more item values), we don't produce an lval.
6627c478bd9Sstevel@tonic-gate 	 * Note that this isn't the same thing as an empty value, which we
6637c478bd9Sstevel@tonic-gate 	 * faithfully try to transmit to LDAP.
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 	if (rval->numVals == 1 && rval->val[0].value == 0) {
6667c478bd9Sstevel@tonic-gate 		freeValue(rval, 1);
6677c478bd9Sstevel@tonic-gate 		return (rv);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* Obtain the LHS item names */
6717c478bd9Sstevel@tonic-gate 	litem = buildLvalue(&r->lhs, &rval, &numItems);
6727c478bd9Sstevel@tonic-gate 	if (litem == 0) {
6737c478bd9Sstevel@tonic-gate 		freeValue(rval, 1);
6747c478bd9Sstevel@tonic-gate 		return (rv);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* Get string representations of the LHS item names */
6787c478bd9Sstevel@tonic-gate 	lval = 0;
6797c478bd9Sstevel@tonic-gate 	for (i = 0; i < numItems; i++) {
6807c478bd9Sstevel@tonic-gate 		__nis_value_t	*tmpval, *old;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		tmpval = getMappingItem(&litem[i], lnative, 0, 0, NULL);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * If the LHS item is out-of-context, we do the
6867c478bd9Sstevel@tonic-gate 		 * assignment right here.
6877c478bd9Sstevel@tonic-gate 		 */
688*36e852a1SRaja Andra 		if (doAssign && litem[i].type == mit_ldap &&
6897c478bd9Sstevel@tonic-gate 				litem[i].searchSpec.triple.scope !=
6907c478bd9Sstevel@tonic-gate 					LDAP_SCOPE_UNKNOWN &&
6917c478bd9Sstevel@tonic-gate 				slen(litem[i].searchSpec.triple.base) > 0 &&
6927c478bd9Sstevel@tonic-gate 				(slen(litem[i].searchSpec.triple.attrs) > 0 ||
6937c478bd9Sstevel@tonic-gate 				litem[i].searchSpec.triple.element != 0)) {
6947c478bd9Sstevel@tonic-gate 			int	stat;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 			if (dn == 0)
6977c478bd9Sstevel@tonic-gate 				dn = findDNs(myself, rv, 1,
6987c478bd9Sstevel@tonic-gate 					t->objectDN->write.base,
6997c478bd9Sstevel@tonic-gate 					&numDN);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			stat = storeLDAP(&litem[i], i, numItems, rval,
7027c478bd9Sstevel@tonic-gate 				t->objectDN, dn, numDN);
7037c478bd9Sstevel@tonic-gate 			if (stat != LDAP_SUCCESS) {
7047c478bd9Sstevel@tonic-gate 				char	*iname = "<unknown>";
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 				if (tmpval != 0 &&
7077c478bd9Sstevel@tonic-gate 						tmpval->numVals == 1)
7087c478bd9Sstevel@tonic-gate 					iname = tmpval->val[0].value;
7097c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
7107c478bd9Sstevel@tonic-gate 					"%s: LDAP store \"%s\": %s",
7117c478bd9Sstevel@tonic-gate 					myself, iname,
7127c478bd9Sstevel@tonic-gate 					ldap_err2string(stat));
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			freeValue(tmpval, 1);
7167c478bd9Sstevel@tonic-gate 			continue;
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		old = lval;
7207c478bd9Sstevel@tonic-gate 		lval = concatenateValues(old, tmpval);
7217c478bd9Sstevel@tonic-gate 		freeValue(tmpval, 1);
7227c478bd9Sstevel@tonic-gate 		freeValue(old, 1);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/* Don't need the LHS items themselves anymore */
7267c478bd9Sstevel@tonic-gate 	freeMappingItem(litem, numItems);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * If we don't have an 'lval' (probably because all litem[i]:s
7307c478bd9Sstevel@tonic-gate 	 * were out-of-context assignments), we're done.
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 	if (lval == 0 || lval->numVals <= 0) {
7337c478bd9Sstevel@tonic-gate 		freeValue(lval, 1);
7347c478bd9Sstevel@tonic-gate 		freeValue(rval, 1);
7357c478bd9Sstevel@tonic-gate 		return (rv);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < lval->numVals; i++) {
7397c478bd9Sstevel@tonic-gate 		/* Special case: rval->numVals < 0 means deletion */
7407c478bd9Sstevel@tonic-gate 		if (rval->numVals < 0) {
7417c478bd9Sstevel@tonic-gate 			(void) addAttr2RuleValue(rval->type,
7427c478bd9Sstevel@tonic-gate 				lval->val[i].value, 0, 0, rv);
7437c478bd9Sstevel@tonic-gate 			continue;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		/* If we're out of values, repeat the last one */
7467c478bd9Sstevel@tonic-gate 		if (j >= rval->numVals)
7477c478bd9Sstevel@tonic-gate 			j = (rval->numVals > 0) ? rval->numVals-1 : 0;
7487c478bd9Sstevel@tonic-gate 		for (0; j < rval->numVals; j++) {
7497c478bd9Sstevel@tonic-gate 			/*
7507c478bd9Sstevel@tonic-gate 			 * If this is the 'dn', and the value ends in a
7517c478bd9Sstevel@tonic-gate 			 * comma, append the appropriate search base.
7527c478bd9Sstevel@tonic-gate 			 */
7537c478bd9Sstevel@tonic-gate 			if (strcasecmp("dn", lval->val[i].value) == 0 &&
7547c478bd9Sstevel@tonic-gate 					lastChar(&rval->val[j]) == ',' &&
7557c478bd9Sstevel@tonic-gate 					t->objectDN->write.scope !=
7567c478bd9Sstevel@tonic-gate 						LDAP_SCOPE_UNKNOWN) {
7577c478bd9Sstevel@tonic-gate 				void	*nval;
7587c478bd9Sstevel@tonic-gate 				int	nlen = -1;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 				nval = appendString2SingleVal(
7617c478bd9Sstevel@tonic-gate 					t->objectDN->write.base, &rval->val[j],
7627c478bd9Sstevel@tonic-gate 					&nlen);
7637c478bd9Sstevel@tonic-gate 				if (nval != 0 && nlen >= 0) {
7647c478bd9Sstevel@tonic-gate 					sfree(rval->val[j].value);
7657c478bd9Sstevel@tonic-gate 					rval->val[j].value = nval;
7667c478bd9Sstevel@tonic-gate 					rval->val[j].length = nlen;
7677c478bd9Sstevel@tonic-gate 				}
7687c478bd9Sstevel@tonic-gate 			}
7697c478bd9Sstevel@tonic-gate 			(void) addAttr2RuleValue(rval->type,
7707c478bd9Sstevel@tonic-gate 				lval->val[i].value, rval->val[j].value,
7717c478bd9Sstevel@tonic-gate 				rval->val[j].length, rv);
7727c478bd9Sstevel@tonic-gate 			/*
7737c478bd9Sstevel@tonic-gate 			 * If the lval is multi-valued, go on to the
7747c478bd9Sstevel@tonic-gate 			 * other values; otherwise, quit (but increment
7757c478bd9Sstevel@tonic-gate 			 * the 'rval' value index).
7767c478bd9Sstevel@tonic-gate 			 */
7777c478bd9Sstevel@tonic-gate 			if (!lval->repeat) {
7787c478bd9Sstevel@tonic-gate 				j++;
7797c478bd9Sstevel@tonic-gate 				break;
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/* Clean up */
7857c478bd9Sstevel@tonic-gate 	freeValue(lval, 1);
7867c478bd9Sstevel@tonic-gate 	freeValue(rval, 1);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	return (rv);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate  * Remove the indicated attribute, and any values for it, from the
7937c478bd9Sstevel@tonic-gate  * rule-value.
7947c478bd9Sstevel@tonic-gate  */
7957c478bd9Sstevel@tonic-gate void
delAttrFromRuleValue(__nis_rule_value_t * rv,char * attrName)7967c478bd9Sstevel@tonic-gate delAttrFromRuleValue(__nis_rule_value_t *rv, char *attrName) {
7977c478bd9Sstevel@tonic-gate 	int	i;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	if (rv == 0 || attrName == 0)
8007c478bd9Sstevel@tonic-gate 		return;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	for (i = 0; i < rv->numAttrs; i++) {
8037c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrName, rv->attrName[i]) == 0) {
8047c478bd9Sstevel@tonic-gate 			int	j;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 			for (j = 0; j < rv->attrVal[i].numVals; j++)
8077c478bd9Sstevel@tonic-gate 				sfree(rv->attrVal[i].val[j].value);
8087c478bd9Sstevel@tonic-gate 			if (rv->attrVal[i].numVals > 0)
8097c478bd9Sstevel@tonic-gate 				sfree(rv->attrVal[i].val);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 			sfree(rv->attrName[i]);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 			/* Move up the rest of the attribute names/values */
8147c478bd9Sstevel@tonic-gate 			for (j = i+1; j < rv->numAttrs; j++) {
8157c478bd9Sstevel@tonic-gate 				rv->attrName[j-1] = rv->attrName[j];
8167c478bd9Sstevel@tonic-gate 				rv->attrVal[j-1] = rv->attrVal[j];
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 			rv->numAttrs -= 1;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * Remove the indicated column, and any values for it, from the
8287c478bd9Sstevel@tonic-gate  * rule-value.
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate void
delColFromRuleValue(__nis_rule_value_t * rv,char * colName)8317c478bd9Sstevel@tonic-gate delColFromRuleValue(__nis_rule_value_t *rv, char *colName) {
8327c478bd9Sstevel@tonic-gate 	int	i;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (rv == 0 || colName == 0)
8357c478bd9Sstevel@tonic-gate 		return;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	for (i = 0; i < rv->numColumns; i++) {
8387c478bd9Sstevel@tonic-gate 		if (strcmp(colName, rv->colName[i]) == 0) {
8397c478bd9Sstevel@tonic-gate 			int	j;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 			for (j = 0; j < rv->colVal[i].numVals; j++)
8427c478bd9Sstevel@tonic-gate 				sfree(rv->colVal[i].val[j].value);
8437c478bd9Sstevel@tonic-gate 			if (rv->colVal[i].numVals > 0)
8447c478bd9Sstevel@tonic-gate 				sfree(rv->colVal[i].val);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			sfree(rv->colName[i]);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 			/* Move up the rest of the column names/values */
8497c478bd9Sstevel@tonic-gate 			for (j = i+1; j < rv->numColumns; j++) {
8507c478bd9Sstevel@tonic-gate 				rv->colName[j-1] = rv->colName[j];
8517c478bd9Sstevel@tonic-gate 				rv->colVal[j-1] = rv->colVal[j];
8527c478bd9Sstevel@tonic-gate 			}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 			rv->numColumns -= 1;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 			break;
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /*
8627c478bd9Sstevel@tonic-gate  * Add the write-mode object classes specified by 'objClassAttrs' to the
8637c478bd9Sstevel@tonic-gate  * rule-value 'rv'.
8647c478bd9Sstevel@tonic-gate  * If there's an error, 'rv' is deleted, and NULL returned.
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate __nis_rule_value_t *
addObjectClasses(__nis_rule_value_t * rv,char * objClassAttrs)8677c478bd9Sstevel@tonic-gate addObjectClasses(__nis_rule_value_t *rv, char *objClassAttrs) {
8687c478bd9Sstevel@tonic-gate 	char	*filter = 0, **fc = 0;
8697c478bd9Sstevel@tonic-gate 	int	i, nfc = 0;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/*
8727c478bd9Sstevel@tonic-gate 	 * Expect to only use this for existing rule-values, so rv == 0 is
8737c478bd9Sstevel@tonic-gate 	 * an error.
8747c478bd9Sstevel@tonic-gate 	 */
8757c478bd9Sstevel@tonic-gate 	if (rv == 0)
8767c478bd9Sstevel@tonic-gate 		return (0);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	/*
8797c478bd9Sstevel@tonic-gate 	 * If 'objClassAttrs' is NULL, we trivially have nothing to do.
8807c478bd9Sstevel@tonic-gate 	 * Assume the caller knows what it's doing, and return success.
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 	if (objClassAttrs == 0)
8837c478bd9Sstevel@tonic-gate 		return (rv);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
8867c478bd9Sstevel@tonic-gate 	 * Make an AND-filter of the object classes, and split into
8877c478bd9Sstevel@tonic-gate 	 * components. (Yes, this is a bit round-about, but leverages
8887c478bd9Sstevel@tonic-gate 	 * existing functions.)
8897c478bd9Sstevel@tonic-gate 	 */
8907c478bd9Sstevel@tonic-gate 	filter = makeFilter(objClassAttrs);
8917c478bd9Sstevel@tonic-gate 	if (filter == 0) {
8927c478bd9Sstevel@tonic-gate 		freeRuleValue(rv, 1);
8937c478bd9Sstevel@tonic-gate 		return (0);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	fc = makeFilterComp(filter, &nfc);
8977c478bd9Sstevel@tonic-gate 	if (fc == 0 || nfc <= 0) {
8987c478bd9Sstevel@tonic-gate 		free(filter);
8997c478bd9Sstevel@tonic-gate 		freeRuleValue(rv, 1);
9007c478bd9Sstevel@tonic-gate 		return (0);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/* Add the objectClass attributes to the rule-value */
9047c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfc; i++) {
9057c478bd9Sstevel@tonic-gate 		char	*name, *value;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 		name = fc[i];
9087c478bd9Sstevel@tonic-gate 		/* Skip if not of the "name=value" form */
9097c478bd9Sstevel@tonic-gate 		if ((value = strchr(name, '=')) == 0)
9107c478bd9Sstevel@tonic-gate 			continue;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		*value = '\0';
9137c478bd9Sstevel@tonic-gate 		value++;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		/* Skip if the attribute name isn't "objectClass" */
9167c478bd9Sstevel@tonic-gate 		if (strcasecmp("objectClass", name) != 0)
9177c478bd9Sstevel@tonic-gate 			continue;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		if (addSAttr2RuleValue(name, value, rv) != 0) {
9207c478bd9Sstevel@tonic-gate 			free(filter);
9217c478bd9Sstevel@tonic-gate 			freeFilterComp(fc, nfc);
9227c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, 1);
9237c478bd9Sstevel@tonic-gate 			return (0);
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	free(filter);
9287c478bd9Sstevel@tonic-gate 	freeFilterComp(fc, nfc);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	return (rv);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static char *
valString(__nis_value_t * val)9357c478bd9Sstevel@tonic-gate valString(__nis_value_t *val) {
9367c478bd9Sstevel@tonic-gate 	int	i;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (val == 0 || val->type != vt_string)
9397c478bd9Sstevel@tonic-gate 		return (0);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	for (i = 0; i < val->numVals; i++) {
9427c478bd9Sstevel@tonic-gate 		/* Look for a non-NULL, non-zero length value */
9437c478bd9Sstevel@tonic-gate 		if (val->val[i].value != 0 && val->val[i].length > 0) {
9447c478bd9Sstevel@tonic-gate 			char	*v = val->val[i].value;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 			/*
9477c478bd9Sstevel@tonic-gate 			 * Check that there's a NUL at the end. True,
9487c478bd9Sstevel@tonic-gate 			 * if there isn't, we may be looking beyond
9497c478bd9Sstevel@tonic-gate 			 * allocated memory. However, we would have done
9507c478bd9Sstevel@tonic-gate 			 * so in any case when the supposed string was
9517c478bd9Sstevel@tonic-gate 			 * traversed (printed, etc.), very possibly by
9527c478bd9Sstevel@tonic-gate 			 * a lot more than one byte. So, it's better to
9537c478bd9Sstevel@tonic-gate 			 * take a small risk here than a large one later.
9547c478bd9Sstevel@tonic-gate 			 */
9557c478bd9Sstevel@tonic-gate 			if (v[val->val[i].length-1] == '\0' ||
9567c478bd9Sstevel@tonic-gate 					v[val->val[i].length] == '\0')
9577c478bd9Sstevel@tonic-gate 				return (v);
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	return (0);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate char *
findVal(char * name,__nis_rule_value_t * rv,__nis_mapping_item_type_t type)9657c478bd9Sstevel@tonic-gate findVal(char *name, __nis_rule_value_t *rv, __nis_mapping_item_type_t type) {
9667c478bd9Sstevel@tonic-gate 	int	i;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (type == mit_nisplus) {
9697c478bd9Sstevel@tonic-gate 		for (i = 0; i < rv->numColumns; i++) {
9707c478bd9Sstevel@tonic-gate 			if (rv->colName[i] == 0)
9717c478bd9Sstevel@tonic-gate 				continue;
9727c478bd9Sstevel@tonic-gate 			if (strcmp(name, rv->colName[i]) == 0) {
9737c478bd9Sstevel@tonic-gate 				return (valString(&rv->colVal[i]));
9747c478bd9Sstevel@tonic-gate 			}
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 	} else if (type == mit_ldap) {
9777c478bd9Sstevel@tonic-gate 		for (i = 0; i < rv->numAttrs; i++) {
9787c478bd9Sstevel@tonic-gate 			if (rv->attrName[i] == 0)
9797c478bd9Sstevel@tonic-gate 				continue;
9807c478bd9Sstevel@tonic-gate 			if (strcasecmp(name, rv->attrName[i]) == 0) {
9817c478bd9Sstevel@tonic-gate 				return (valString(&rv->attrVal[i]));
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	return (0);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate static char	*norv = "<NIL>";
9907c478bd9Sstevel@tonic-gate static char	*unknown = "<unknown>";
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * Attempt to derive a string identifying the rule-value 'rv'. The
9947c478bd9Sstevel@tonic-gate  * returned string is a pointer, either into 'rv', or to static
9957c478bd9Sstevel@tonic-gate  * storage, and must not be freed.
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate char *
rvId(__nis_rule_value_t * rv,__nis_mapping_item_type_t type)9987c478bd9Sstevel@tonic-gate rvId(__nis_rule_value_t *rv, __nis_mapping_item_type_t type) {
9997c478bd9Sstevel@tonic-gate 	char	*v;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (rv == 0)
10027c478bd9Sstevel@tonic-gate 		return (norv);
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if (rv->numColumns > 0 && type == mit_nisplus) {
10057c478bd9Sstevel@tonic-gate 		/*
10067c478bd9Sstevel@tonic-gate 		 * Look for a column called "cname" or "name".
10077c478bd9Sstevel@tonic-gate 		 * If that fails, try "key" or "alias".
10087c478bd9Sstevel@tonic-gate 		 */
10097c478bd9Sstevel@tonic-gate 		if ((v = findVal("cname", rv, type)) != 0)
10107c478bd9Sstevel@tonic-gate 			return (v);
10117c478bd9Sstevel@tonic-gate 		else if ((v = findVal("name", rv, type)) != 0)
10127c478bd9Sstevel@tonic-gate 			return (v);
10137c478bd9Sstevel@tonic-gate 		else if ((v = findVal("key", rv, type)) != 0)
10147c478bd9Sstevel@tonic-gate 			return (v);
10157c478bd9Sstevel@tonic-gate 		else if ((v = findVal("alias", rv, type)) != 0)
10167c478bd9Sstevel@tonic-gate 			return (v);
10177c478bd9Sstevel@tonic-gate 	} else if (rv->numAttrs > 0 && type == mit_ldap) {
10187c478bd9Sstevel@tonic-gate 		/*
10197c478bd9Sstevel@tonic-gate 		 * Look for "dn", or "cn".
10207c478bd9Sstevel@tonic-gate 		 */
10217c478bd9Sstevel@tonic-gate 		if ((v = findVal("dn", rv, type)) != 0)
10227c478bd9Sstevel@tonic-gate 			return (v);
10237c478bd9Sstevel@tonic-gate 		else if ((v = findVal("cn", rv, type)) != 0)
10247c478bd9Sstevel@tonic-gate 			return (v);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	return (unknown);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate /*
10317c478bd9Sstevel@tonic-gate  * Merge the rule-values with the same DN into one. Each rule-value
10327c478bd9Sstevel@tonic-gate  * in the returned array will have unique 'dn'. On entry, *numVals
10337c478bd9Sstevel@tonic-gate  * contains the number of rule-values in 'rv'. On exit, it contains
10347c478bd9Sstevel@tonic-gate  * the number of rule-values in the returned array or -1 on error.
10357c478bd9Sstevel@tonic-gate  */
10367c478bd9Sstevel@tonic-gate __nis_rule_value_t *
mergeRuleValueWithSameDN(__nis_rule_value_t * rv,int * numVals)10377c478bd9Sstevel@tonic-gate mergeRuleValueWithSameDN(__nis_rule_value_t *rv, int *numVals) {
10387c478bd9Sstevel@tonic-gate 	__nis_rule_value_t	*rvq = 0;
10397c478bd9Sstevel@tonic-gate 	char			*dn, *odn;
10407c478bd9Sstevel@tonic-gate 	int			count = 0;
10417c478bd9Sstevel@tonic-gate 	int			i, j;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (numVals == 0)
10447c478bd9Sstevel@tonic-gate 		return (0);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	for (i = 0; i < *numVals; i++) {
10477c478bd9Sstevel@tonic-gate 		if ((dn = findVal("dn", &rv[i], mit_ldap)) != 0) {
10487c478bd9Sstevel@tonic-gate 			for (j = 0; j < count; j++) {
10497c478bd9Sstevel@tonic-gate 				if ((odn = findVal("dn", &rvq[j],
10507c478bd9Sstevel@tonic-gate 						mit_ldap)) != 0) {
10517c478bd9Sstevel@tonic-gate 					/* case sensitive compare */
10527c478bd9Sstevel@tonic-gate 					if (strcmp(dn, odn) != 0)
10537c478bd9Sstevel@tonic-gate 						continue;
10547c478bd9Sstevel@tonic-gate 					if (mergeRuleValue(&rvq[j],
10557c478bd9Sstevel@tonic-gate 							&rv[i]) == -1) {
10567c478bd9Sstevel@tonic-gate 						freeRuleValue(rvq, count);
10577c478bd9Sstevel@tonic-gate 						*numVals = -1;
10587c478bd9Sstevel@tonic-gate 						return (0);
10597c478bd9Sstevel@tonic-gate 					}
10607c478bd9Sstevel@tonic-gate 					break;
10617c478bd9Sstevel@tonic-gate 				} else {
10627c478bd9Sstevel@tonic-gate 					freeRuleValue(rvq, count);
10637c478bd9Sstevel@tonic-gate 					*numVals = -1;
10647c478bd9Sstevel@tonic-gate 					return (0);
10657c478bd9Sstevel@tonic-gate 				}
10667c478bd9Sstevel@tonic-gate 			}
10677c478bd9Sstevel@tonic-gate 			/* if no match, then add it to the rulevalue array */
10687c478bd9Sstevel@tonic-gate 			if (j == count) {
10697c478bd9Sstevel@tonic-gate 				rvq = growRuleValue(count, count + 1, rvq,
10707c478bd9Sstevel@tonic-gate 									&rv[i]);
10717c478bd9Sstevel@tonic-gate 				if (rvq == 0) {
10727c478bd9Sstevel@tonic-gate 					*numVals = -1;
10737c478bd9Sstevel@tonic-gate 					return (0);
10747c478bd9Sstevel@tonic-gate 				}
10757c478bd9Sstevel@tonic-gate 				count++;
10767c478bd9Sstevel@tonic-gate 			}
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	*numVals = count;
10817c478bd9Sstevel@tonic-gate 	return (rvq);
10827c478bd9Sstevel@tonic-gate }
1083