xref: /titanic_50/usr/src/lib/libnisdb/ldap_ruleval.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <lber.h>
30*7c478bd9Sstevel@tonic-gate #include <ldap.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "ldap_util.h"
36*7c478bd9Sstevel@tonic-gate #include "ldap_val.h"
37*7c478bd9Sstevel@tonic-gate #include "ldap_attr.h"
38*7c478bd9Sstevel@tonic-gate #include "ldap_ldap.h"
39*7c478bd9Sstevel@tonic-gate #include "ldap_nisplus.h"
40*7c478bd9Sstevel@tonic-gate #include "ldap_ruleval.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * Free an array of 'count' rule-value elements.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate void
47*7c478bd9Sstevel@tonic-gate freeRuleValue(__nis_rule_value_t *rv, int count) {
48*7c478bd9Sstevel@tonic-gate 	int	n, i, j;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	if (rv == 0)
51*7c478bd9Sstevel@tonic-gate 		return;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	for (n = 0; n < count; n++) {
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 		if (rv[n].colName != 0) {
56*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numColumns; i++) {
57*7c478bd9Sstevel@tonic-gate 				sfree(rv[n].colName[i]);
58*7c478bd9Sstevel@tonic-gate 			}
59*7c478bd9Sstevel@tonic-gate 			free(rv[n].colName);
60*7c478bd9Sstevel@tonic-gate 		}
61*7c478bd9Sstevel@tonic-gate 		if (rv[n].colVal != 0) {
62*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numColumns; i++) {
63*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < rv[n].colVal[i].numVals; j++) {
64*7c478bd9Sstevel@tonic-gate 					sfree(rv[n].colVal[i].val[j].value);
65*7c478bd9Sstevel@tonic-gate 				}
66*7c478bd9Sstevel@tonic-gate 				if (rv[n].colVal[i].numVals > 0)
67*7c478bd9Sstevel@tonic-gate 					sfree(rv[n].colVal[i].val);
68*7c478bd9Sstevel@tonic-gate 			}
69*7c478bd9Sstevel@tonic-gate 			free(rv[n].colVal);
70*7c478bd9Sstevel@tonic-gate 		}
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 		if (rv[n].attrName != 0) {
73*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numAttrs; i++) {
74*7c478bd9Sstevel@tonic-gate 				sfree(rv[n].attrName[i]);
75*7c478bd9Sstevel@tonic-gate 			}
76*7c478bd9Sstevel@tonic-gate 			free(rv[n].attrName);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 		if (rv[n].attrVal != 0) {
79*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < rv[n].numAttrs; i++) {
80*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < rv[n].attrVal[i].numVals;
81*7c478bd9Sstevel@tonic-gate 						j++) {
82*7c478bd9Sstevel@tonic-gate 					sfree(rv[n].attrVal[i].val[j].value);
83*7c478bd9Sstevel@tonic-gate 				}
84*7c478bd9Sstevel@tonic-gate 				if (rv[n].attrVal[i].numVals > 0)
85*7c478bd9Sstevel@tonic-gate 					sfree(rv[n].attrVal[i].val);
86*7c478bd9Sstevel@tonic-gate 			}
87*7c478bd9Sstevel@tonic-gate 			free(rv[n].attrVal);
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	sfree(rv);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * Return an array of 'count' __nis_rule_value_t elements, initialized
96*7c478bd9Sstevel@tonic-gate  * to be copies of 'rvIn' if supplied; empty otherwise.
97*7c478bd9Sstevel@tonic-gate  */
98*7c478bd9Sstevel@tonic-gate __nis_rule_value_t *
99*7c478bd9Sstevel@tonic-gate initRuleValue(int count, __nis_rule_value_t *rvIn) {
100*7c478bd9Sstevel@tonic-gate 	return (growRuleValue(0, count, 0, rvIn));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static const __nis_rule_value_t	rvZero = {0};
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * Grow 'old' from 'oldCount' to 'newCount' elements, initialize the
107*7c478bd9Sstevel@tonic-gate  * new portion to 'rvIn' (empty if not supplied), and return a pointer
108*7c478bd9Sstevel@tonic-gate  * to the result. Following a call to this function, the caller must
109*7c478bd9Sstevel@tonic-gate  * refer only to the returned array, not to 'old'.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate __nis_rule_value_t *
112*7c478bd9Sstevel@tonic-gate growRuleValue(int oldCount, int newCount, __nis_rule_value_t *old,
113*7c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rvIn) {
114*7c478bd9Sstevel@tonic-gate 	__nis_rule_value_t	*rv;
115*7c478bd9Sstevel@tonic-gate 	int			i, j;
116*7c478bd9Sstevel@tonic-gate 	char			*myself = "growRuleValue";
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (newCount <= 0 || newCount <= oldCount)
119*7c478bd9Sstevel@tonic-gate 		return (old);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (oldCount <= 0) {
122*7c478bd9Sstevel@tonic-gate 		oldCount = 0;
123*7c478bd9Sstevel@tonic-gate 		old = 0;
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (rvIn == 0)
127*7c478bd9Sstevel@tonic-gate 		rvIn = (__nis_rule_value_t *)&rvZero;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	rv = realloc(old, newCount * sizeof (rv[0]));
130*7c478bd9Sstevel@tonic-gate 	if (rv == 0) {
131*7c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOMEM, LOG_ERR,
132*7c478bd9Sstevel@tonic-gate 			"%s: realloc(%d ((%d+%d)*%d)) => 0",
133*7c478bd9Sstevel@tonic-gate 			myself, (oldCount+newCount) * sizeof (rv[0]),
134*7c478bd9Sstevel@tonic-gate 			oldCount, newCount, sizeof (rv[0]));
135*7c478bd9Sstevel@tonic-gate 		freeRuleValue(old, oldCount);
136*7c478bd9Sstevel@tonic-gate 		return (0);
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	(void) memset(&rv[oldCount], 0, (newCount-oldCount)*sizeof (rv[0]));
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	for (i = oldCount; i < newCount; i++) {
142*7c478bd9Sstevel@tonic-gate 		rv[i].numColumns = rvIn->numColumns;
143*7c478bd9Sstevel@tonic-gate 		if (rv[i].numColumns > 0) {
144*7c478bd9Sstevel@tonic-gate 			rv[i].colName = cloneName(rvIn->colName,
145*7c478bd9Sstevel@tonic-gate 					rv[i].numColumns);
146*7c478bd9Sstevel@tonic-gate 			rv[i].colVal = cloneValue(rvIn->colVal,
147*7c478bd9Sstevel@tonic-gate 					rv[i].numColumns);
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 		if (rv[i].numColumns > 0 &&
150*7c478bd9Sstevel@tonic-gate 				(rv[i].colName == 0 || rv[i].colVal == 0)) {
151*7c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, i);
152*7c478bd9Sstevel@tonic-gate 			return (0);
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 		rv[i].numAttrs = rvIn->numAttrs;
155*7c478bd9Sstevel@tonic-gate 		rv[i].attrName = cloneName(rvIn->attrName, rv[i].numAttrs);
156*7c478bd9Sstevel@tonic-gate 		rv[i].attrVal = cloneValue(rvIn->attrVal, rv[i].numAttrs);
157*7c478bd9Sstevel@tonic-gate 		if (rv[i].numAttrs > 0 &&
158*7c478bd9Sstevel@tonic-gate 			(rv[i].attrName == 0 || rv[i].attrVal == 0)) {
159*7c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, i);
160*7c478bd9Sstevel@tonic-gate 			return (0);
161*7c478bd9Sstevel@tonic-gate 		}
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	return (rv);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate /*
168*7c478bd9Sstevel@tonic-gate  * Merge the source rule-value 's' into the target rule-value 't'.
169*7c478bd9Sstevel@tonic-gate  * If successful, unless 's' is a sub-set of 't', 't' will be changed
170*7c478bd9Sstevel@tonic-gate  * on exit, and will contain the values from 's' as well.
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate int
173*7c478bd9Sstevel@tonic-gate mergeRuleValue(__nis_rule_value_t *t, __nis_rule_value_t *s) {
174*7c478bd9Sstevel@tonic-gate 	int	i, j;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	if (s == 0)
177*7c478bd9Sstevel@tonic-gate 		return (0);
178*7c478bd9Sstevel@tonic-gate 	else if (t == 0)
179*7c478bd9Sstevel@tonic-gate 		return (-1);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < s->numColumns; i++) {
182*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < s->colVal[i].numVals; j++) {
183*7c478bd9Sstevel@tonic-gate 			if (addCol2RuleValue(s->colVal[i].type, s->colName[i],
184*7c478bd9Sstevel@tonic-gate 					s->colVal[i].val[j].value,
185*7c478bd9Sstevel@tonic-gate 					s->colVal[i].val[j].length,
186*7c478bd9Sstevel@tonic-gate 					t))
187*7c478bd9Sstevel@tonic-gate 				return (-1);
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < s->numAttrs; i++) {
192*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < s->attrVal[i].numVals; j++) {
193*7c478bd9Sstevel@tonic-gate 			if (addAttr2RuleValue(s->attrVal[i].type,
194*7c478bd9Sstevel@tonic-gate 					s->attrName[i],
195*7c478bd9Sstevel@tonic-gate 					s->attrVal[i].val[j].value,
196*7c478bd9Sstevel@tonic-gate 					s->attrVal[i].val[j].length,
197*7c478bd9Sstevel@tonic-gate 					t))
198*7c478bd9Sstevel@tonic-gate 				return (-1);
199*7c478bd9Sstevel@tonic-gate 		}
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	return (0);
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate static int
206*7c478bd9Sstevel@tonic-gate addVal2RuleValue(char *msg, int caseSens, int snipNul, __nis_value_type_t type,
207*7c478bd9Sstevel@tonic-gate 		char *name, void *value, int valueLen,
208*7c478bd9Sstevel@tonic-gate 		int *numP, char ***inNameP, __nis_value_t **inValP) {
209*7c478bd9Sstevel@tonic-gate 	int			i, j, copyLen = valueLen;
210*7c478bd9Sstevel@tonic-gate 	__nis_single_value_t	*v;
211*7c478bd9Sstevel@tonic-gate 	char			**inName = *inNameP;
212*7c478bd9Sstevel@tonic-gate 	__nis_value_t		*inVal = *inValP;
213*7c478bd9Sstevel@tonic-gate 	int			num = *numP;
214*7c478bd9Sstevel@tonic-gate 	int			(*comp)(const char *s1, const char *s2);
215*7c478bd9Sstevel@tonic-gate 	char			*myself = "addVal2RuleValue";
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	/* Internal function, so assume arguments OK */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (msg == 0)
220*7c478bd9Sstevel@tonic-gate 		msg = myself;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	/* Should we match the 'inName' value case sensitive or not ? */
223*7c478bd9Sstevel@tonic-gate 	if (caseSens)
224*7c478bd9Sstevel@tonic-gate 		comp = strcmp;
225*7c478bd9Sstevel@tonic-gate 	else
226*7c478bd9Sstevel@tonic-gate 		comp = strcasecmp;
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	/*
229*7c478bd9Sstevel@tonic-gate 	 * String-valued NIS+ entries count the concluding NUL in the
230*7c478bd9Sstevel@tonic-gate 	 * length, while LDAP entries don't. In order to support this,
231*7c478bd9Sstevel@tonic-gate 	 * we implement the following for vt_string value types:
232*7c478bd9Sstevel@tonic-gate 	 *
233*7c478bd9Sstevel@tonic-gate 	 * If the last byte of the value isn't a NUL, add one to the
234*7c478bd9Sstevel@tonic-gate 	 * allocated length, so that there always is a NUL after the
235*7c478bd9Sstevel@tonic-gate 	 * value, making it safe to pass to strcmp() etc.
236*7c478bd9Sstevel@tonic-gate 	 *
237*7c478bd9Sstevel@tonic-gate 	 * If 'snipNul' is set (presumably meaning we're inserting a
238*7c478bd9Sstevel@tonic-gate 	 * value derived from a NIS+ entry), and the last byte of the
239*7c478bd9Sstevel@tonic-gate 	 * value already is a NUL, decrement the length to be copied by
240*7c478bd9Sstevel@tonic-gate 	 * one. This (a) doesn't count the NUL in the value length, but
241*7c478bd9Sstevel@tonic-gate 	 * (b) still leaves a NUL following the value.
242*7c478bd9Sstevel@tonic-gate 	 *
243*7c478bd9Sstevel@tonic-gate 	 * In N2L, for all cases we set 'copyLen' to the number of non-0
244*7c478bd9Sstevel@tonic-gate 	 * characters in 'value'.
245*7c478bd9Sstevel@tonic-gate 	 */
246*7c478bd9Sstevel@tonic-gate 	if (type == vt_string && valueLen > 0) {
247*7c478bd9Sstevel@tonic-gate 		char	*charval = value;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		if (charval[valueLen-1] != '\0')
250*7c478bd9Sstevel@tonic-gate 			valueLen += 1;
251*7c478bd9Sstevel@tonic-gate 		else if (yp2ldap || snipNul)
252*7c478bd9Sstevel@tonic-gate 			copyLen -= 1;
253*7c478bd9Sstevel@tonic-gate 	} else if (valueLen == 0) {
254*7c478bd9Sstevel@tonic-gate 		/*
255*7c478bd9Sstevel@tonic-gate 		 * If the 'value' pointer is non-NULL, we create a zero-
256*7c478bd9Sstevel@tonic-gate 		 * length value with one byte allocated. This takes care
257*7c478bd9Sstevel@tonic-gate 		 * of empty strings.
258*7c478bd9Sstevel@tonic-gate 		 */
259*7c478bd9Sstevel@tonic-gate 		valueLen += 1;
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	/* If we already have values for this attribute, add another one */
263*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
264*7c478bd9Sstevel@tonic-gate 		if ((*comp)(inName[i], name) == 0) {
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 			/*
267*7c478bd9Sstevel@tonic-gate 			 * Our caller often doesn't know the type of the
268*7c478bd9Sstevel@tonic-gate 			 * value; this happens because the type (vt_string
269*7c478bd9Sstevel@tonic-gate 			 * or vt_ber) is determined by the format in the
270*7c478bd9Sstevel@tonic-gate 			 * rule sets, and we may be invoked as a preparation
271*7c478bd9Sstevel@tonic-gate 			 * for evaluating the rules. Hence, we only use the
272*7c478bd9Sstevel@tonic-gate 			 * supplied 'type' if we need to create a value.
273*7c478bd9Sstevel@tonic-gate 			 * Otherwise, we accept mixed types.
274*7c478bd9Sstevel@tonic-gate 			 *
275*7c478bd9Sstevel@tonic-gate 			 * Strings are OK in any case, since we always make
276*7c478bd9Sstevel@tonic-gate 			 * sure to have a zero byte at the end of any value,
277*7c478bd9Sstevel@tonic-gate 			 * whatever the type.
278*7c478bd9Sstevel@tonic-gate 			 */
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 			if (inVal[i].numVals < 0) {
281*7c478bd9Sstevel@tonic-gate 				/*
282*7c478bd9Sstevel@tonic-gate 				 * Used to indicate deletion of attribute,
283*7c478bd9Sstevel@tonic-gate 				 * so we honor that and don't add a value.
284*7c478bd9Sstevel@tonic-gate 				 */
285*7c478bd9Sstevel@tonic-gate 				return (0);
286*7c478bd9Sstevel@tonic-gate 			}
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 			/*
289*7c478bd9Sstevel@tonic-gate 			 * If 'value' is NULL, we should delete, so
290*7c478bd9Sstevel@tonic-gate 			 * remove any existing values, and set the
291*7c478bd9Sstevel@tonic-gate 			 * 'numVals' field to -1.
292*7c478bd9Sstevel@tonic-gate 			 */
293*7c478bd9Sstevel@tonic-gate 			if (value == 0) {
294*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < inVal[i].numVals; j++) {
295*7c478bd9Sstevel@tonic-gate 					sfree(inVal[i].val[j].value);
296*7c478bd9Sstevel@tonic-gate 				}
297*7c478bd9Sstevel@tonic-gate 				sfree(inVal[i].val);
298*7c478bd9Sstevel@tonic-gate 				inVal[i].val = 0;
299*7c478bd9Sstevel@tonic-gate 				inVal[i].numVals = -1;
300*7c478bd9Sstevel@tonic-gate 				return (0);
301*7c478bd9Sstevel@tonic-gate 			}
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 			/* Is the value a duplicate ? */
304*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < inVal[i].numVals; j++) {
305*7c478bd9Sstevel@tonic-gate 				if (copyLen == inVal[i].val[j].length &&
306*7c478bd9Sstevel@tonic-gate 					memcmp(value, inVal[i].val[j].value,
307*7c478bd9Sstevel@tonic-gate 						copyLen) == 0) {
308*7c478bd9Sstevel@tonic-gate 					break;
309*7c478bd9Sstevel@tonic-gate 				}
310*7c478bd9Sstevel@tonic-gate 			}
311*7c478bd9Sstevel@tonic-gate 			if (j < inVal[i].numVals)
312*7c478bd9Sstevel@tonic-gate 				return (0);
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 			/* Not a duplicate, so add the name/value pair */
315*7c478bd9Sstevel@tonic-gate 			v = realloc(inVal[i].val,
316*7c478bd9Sstevel@tonic-gate 					(inVal[i].numVals+1) *
317*7c478bd9Sstevel@tonic-gate 					sizeof (inVal[i].val[0]));
318*7c478bd9Sstevel@tonic-gate 			if (v == 0)
319*7c478bd9Sstevel@tonic-gate 				return (-1);
320*7c478bd9Sstevel@tonic-gate 			inVal[i].val = v;
321*7c478bd9Sstevel@tonic-gate 			v[inVal[i].numVals].length = copyLen;
322*7c478bd9Sstevel@tonic-gate 			v[inVal[i].numVals].value = am(msg, valueLen);
323*7c478bd9Sstevel@tonic-gate 			if (v[inVal[i].numVals].value == 0 &&
324*7c478bd9Sstevel@tonic-gate 					value != 0) {
325*7c478bd9Sstevel@tonic-gate 				sfree(v);
326*7c478bd9Sstevel@tonic-gate 				return (-1);
327*7c478bd9Sstevel@tonic-gate 			}
328*7c478bd9Sstevel@tonic-gate 			memcpy(v[inVal[i].numVals].value, value, copyLen);
329*7c478bd9Sstevel@tonic-gate 			inVal[i].numVals++;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 			return (0);
332*7c478bd9Sstevel@tonic-gate 		}
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	/* No previous value for this attribute */
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	/*
338*7c478bd9Sstevel@tonic-gate 	 * value == 0 means deletion, in which case we create a
339*7c478bd9Sstevel@tonic-gate 	 * __nis_value_t with the numVals field set to -1.
340*7c478bd9Sstevel@tonic-gate 	 */
341*7c478bd9Sstevel@tonic-gate 	if (value != 0) {
342*7c478bd9Sstevel@tonic-gate 		if ((v = am(msg, sizeof (*v))) == 0)
343*7c478bd9Sstevel@tonic-gate 			return (-1);
344*7c478bd9Sstevel@tonic-gate 		v->length = copyLen;
345*7c478bd9Sstevel@tonic-gate 		v->value = am(msg, valueLen);
346*7c478bd9Sstevel@tonic-gate 		if (v->value == 0 && value != 0) {
347*7c478bd9Sstevel@tonic-gate 			sfree(v);
348*7c478bd9Sstevel@tonic-gate 			return (-1);
349*7c478bd9Sstevel@tonic-gate 		}
350*7c478bd9Sstevel@tonic-gate 		memcpy(v->value, value, copyLen);
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	inVal = realloc(inVal, (num+1)*sizeof (inVal[0]));
354*7c478bd9Sstevel@tonic-gate 	if (inVal == 0) {
355*7c478bd9Sstevel@tonic-gate 		if (value != 0) {
356*7c478bd9Sstevel@tonic-gate 			sfree(v->value);
357*7c478bd9Sstevel@tonic-gate 			sfree(v);
358*7c478bd9Sstevel@tonic-gate 		}
359*7c478bd9Sstevel@tonic-gate 		return (-1);
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 	*inValP = inVal;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	inName = realloc(inName,
364*7c478bd9Sstevel@tonic-gate 		(num+1)*sizeof (inName[0]));
365*7c478bd9Sstevel@tonic-gate 	if (inName == 0 || (inName[num] =
366*7c478bd9Sstevel@tonic-gate 			sdup(msg, T, name)) == 0) {
367*7c478bd9Sstevel@tonic-gate 		sfree(v->value);
368*7c478bd9Sstevel@tonic-gate 		sfree(v);
369*7c478bd9Sstevel@tonic-gate 		return (-1);
370*7c478bd9Sstevel@tonic-gate 	}
371*7c478bd9Sstevel@tonic-gate 	*inNameP = inName;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	inVal[num].type = type;
374*7c478bd9Sstevel@tonic-gate 	inVal[num].repeat = 0;
375*7c478bd9Sstevel@tonic-gate 	if (value != 0) {
376*7c478bd9Sstevel@tonic-gate 		inVal[num].numVals = 1;
377*7c478bd9Sstevel@tonic-gate 		inVal[num].val = v;
378*7c478bd9Sstevel@tonic-gate 	} else {
379*7c478bd9Sstevel@tonic-gate 		inVal[num].numVals = -1;
380*7c478bd9Sstevel@tonic-gate 		inVal[num].val = 0;
381*7c478bd9Sstevel@tonic-gate 	}
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	*numP += 1;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	return (0);
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate int
389*7c478bd9Sstevel@tonic-gate addAttr2RuleValue(__nis_value_type_t type, char *name, void *value,
390*7c478bd9Sstevel@tonic-gate 		int valueLen, __nis_rule_value_t *rv) {
391*7c478bd9Sstevel@tonic-gate 	char			*myself = "addAttr2RuleValue";
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	if (name == 0 || rv == 0)
394*7c478bd9Sstevel@tonic-gate 		return (-1);
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	return (addVal2RuleValue(myself, 0, 0, type, name, value, valueLen,
397*7c478bd9Sstevel@tonic-gate 				&rv->numAttrs, &rv->attrName, &rv->attrVal));
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate int
401*7c478bd9Sstevel@tonic-gate addSAttr2RuleValue(char *name, char *value, __nis_rule_value_t *rv) {
402*7c478bd9Sstevel@tonic-gate 	return (addAttr2RuleValue(vt_string, name, value, slen(value), rv));
403*7c478bd9Sstevel@tonic-gate }
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate int
406*7c478bd9Sstevel@tonic-gate addCol2RuleValue(__nis_value_type_t type, char *name, void *value,
407*7c478bd9Sstevel@tonic-gate 		int valueLen, __nis_rule_value_t *rv) {
408*7c478bd9Sstevel@tonic-gate 	char *myself = "addCol2RuleValue";
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	if (name == 0 || rv == 0)
411*7c478bd9Sstevel@tonic-gate 		return (-1);
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	return (addVal2RuleValue(myself, 1, 1, type, name, value, valueLen,
414*7c478bd9Sstevel@tonic-gate 				&rv->numColumns, &rv->colName, &rv->colVal));
415*7c478bd9Sstevel@tonic-gate }
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate int
418*7c478bd9Sstevel@tonic-gate addSCol2RuleValue(char *name, char *value, __nis_rule_value_t *rv) {
419*7c478bd9Sstevel@tonic-gate 	return (addCol2RuleValue(vt_string, name, value, slen(value), rv));
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate /*
423*7c478bd9Sstevel@tonic-gate  * Given a table mapping, a NIS+ DB query, and (optionally) an existing
424*7c478bd9Sstevel@tonic-gate  * and compatible __nis_rule_value_t, return a new __nis_rule_value_t
425*7c478bd9Sstevel@tonic-gate  * with the values from the query added.
426*7c478bd9Sstevel@tonic-gate  */
427*7c478bd9Sstevel@tonic-gate __nis_rule_value_t *
428*7c478bd9Sstevel@tonic-gate buildNisPlusRuleValue(__nis_table_mapping_t *t, db_query *q,
429*7c478bd9Sstevel@tonic-gate 			__nis_rule_value_t *rv) {
430*7c478bd9Sstevel@tonic-gate 	int			i;
431*7c478bd9Sstevel@tonic-gate 	__nis_single_value_t	*sv;
432*7c478bd9Sstevel@tonic-gate 	char			*myself = "buildNisPlusRuleValue";
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	if (t == 0 || q == 0)
435*7c478bd9Sstevel@tonic-gate 		return (0);
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	rv = initRuleValue(1, rv);
438*7c478bd9Sstevel@tonic-gate 	if (rv == 0)
439*7c478bd9Sstevel@tonic-gate 		return (0);
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < q->components.components_len; i++) {
442*7c478bd9Sstevel@tonic-gate 		int	ic;
443*7c478bd9Sstevel@tonic-gate 		int	iv, v, dup;
444*7c478bd9Sstevel@tonic-gate 		int	len;
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 		/* Ignore out-of-range column index */
447*7c478bd9Sstevel@tonic-gate 		if (q->components.components_val[i].which_index >=
448*7c478bd9Sstevel@tonic-gate 				t->numColumns)
449*7c478bd9Sstevel@tonic-gate 			continue;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 		/*
452*7c478bd9Sstevel@tonic-gate 		 * Add the query value. A NULL value indicates deletion,
453*7c478bd9Sstevel@tonic-gate 		 * but addCol2RuleValue() takes care of that for us.
454*7c478bd9Sstevel@tonic-gate 		 */
455*7c478bd9Sstevel@tonic-gate 		if (addCol2RuleValue(vt_string,
456*7c478bd9Sstevel@tonic-gate 				t->column[q->components.components_val[i].
457*7c478bd9Sstevel@tonic-gate 						which_index],
458*7c478bd9Sstevel@tonic-gate 				q->components.components_val[i].index_value->
459*7c478bd9Sstevel@tonic-gate 					itemvalue.itemvalue_val,
460*7c478bd9Sstevel@tonic-gate 				q->components.components_val[i].index_value->
461*7c478bd9Sstevel@tonic-gate 					itemvalue.itemvalue_len, rv) != 0) {
462*7c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, 1);
463*7c478bd9Sstevel@tonic-gate 			rv = 0;
464*7c478bd9Sstevel@tonic-gate 			break;
465*7c478bd9Sstevel@tonic-gate 		}
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	return (rv);
469*7c478bd9Sstevel@tonic-gate }
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate /*
473*7c478bd9Sstevel@tonic-gate  * Given a LHS rule 'rl', return an array containing the item names,
474*7c478bd9Sstevel@tonic-gate  * and the number of elements in the array in '*numItems'.
475*7c478bd9Sstevel@tonic-gate  *
476*7c478bd9Sstevel@tonic-gate  * If there are 'me_match' __nis_mapping_element_t's, we use the
477*7c478bd9Sstevel@tonic-gate  * supplied '*rval' (if any) to derive values for the items in
478*7c478bd9Sstevel@tonic-gate  * the 'me_match', and add the values thus derived to '*rval' (in
479*7c478bd9Sstevel@tonic-gate  * which case the '*rval' pointer will change; the old '*rval'
480*7c478bd9Sstevel@tonic-gate  * is deleted).
481*7c478bd9Sstevel@tonic-gate  */
482*7c478bd9Sstevel@tonic-gate __nis_mapping_item_t *
483*7c478bd9Sstevel@tonic-gate buildLvalue(__nis_mapping_rlhs_t *rl, __nis_value_t **rval, int *numItems) {
484*7c478bd9Sstevel@tonic-gate 	__nis_value_t		*val, *r;
485*7c478bd9Sstevel@tonic-gate 	__nis_mapping_item_t	*item = 0;
486*7c478bd9Sstevel@tonic-gate 	int			i, n, ni = 0, nv = 0;
487*7c478bd9Sstevel@tonic-gate 	int			repeat = 0;
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	if (rl == 0)
490*7c478bd9Sstevel@tonic-gate 		return (0);
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 	if (rval != 0) {
493*7c478bd9Sstevel@tonic-gate 		r = *rval;
494*7c478bd9Sstevel@tonic-gate 		repeat = r->repeat;
495*7c478bd9Sstevel@tonic-gate 	} else
496*7c478bd9Sstevel@tonic-gate 		r = 0;
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	/* If there is more than one element, we concatenate the items */
499*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < rl->numElements; i++) {
500*7c478bd9Sstevel@tonic-gate 		__nis_mapping_element_t	*e = &rl->element[i];
501*7c478bd9Sstevel@tonic-gate 		__nis_mapping_item_t	*olditem, *tmpitem = 0;
502*7c478bd9Sstevel@tonic-gate 		__nis_value_t		**tmp;
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 		switch (e->type) {
505*7c478bd9Sstevel@tonic-gate 		case me_item:
506*7c478bd9Sstevel@tonic-gate 			tmpitem = cloneItem(&e->element.item);
507*7c478bd9Sstevel@tonic-gate 			break;
508*7c478bd9Sstevel@tonic-gate 		case me_match:
509*7c478bd9Sstevel@tonic-gate 			/*
510*7c478bd9Sstevel@tonic-gate 			 * Obtain values for the items in the 'me_match'
511*7c478bd9Sstevel@tonic-gate 			 * element.
512*7c478bd9Sstevel@tonic-gate 			 */
513*7c478bd9Sstevel@tonic-gate 			tmp = matchMappingItem(e->element.match.fmt, r, &nv,
514*7c478bd9Sstevel@tonic-gate 				0, 0);
515*7c478bd9Sstevel@tonic-gate 			if (tmp != 0) {
516*7c478bd9Sstevel@tonic-gate 				freeValue(r, 1);
517*7c478bd9Sstevel@tonic-gate 				val = 0;
518*7c478bd9Sstevel@tonic-gate 				for (n = 0; n < nv; n++) {
519*7c478bd9Sstevel@tonic-gate 					r = concatenateValues(val, tmp[n]);
520*7c478bd9Sstevel@tonic-gate 					freeValue(val, 1);
521*7c478bd9Sstevel@tonic-gate 					freeValue(tmp[n], 1);
522*7c478bd9Sstevel@tonic-gate 					val = r;
523*7c478bd9Sstevel@tonic-gate 					if (val == 0) {
524*7c478bd9Sstevel@tonic-gate 						for (n++; n < nv; n++) {
525*7c478bd9Sstevel@tonic-gate 							freeValue(tmp[n], 1);
526*7c478bd9Sstevel@tonic-gate 						}
527*7c478bd9Sstevel@tonic-gate 						break;
528*7c478bd9Sstevel@tonic-gate 					}
529*7c478bd9Sstevel@tonic-gate 				}
530*7c478bd9Sstevel@tonic-gate 				free(tmp);
531*7c478bd9Sstevel@tonic-gate 				if (rval != 0) {
532*7c478bd9Sstevel@tonic-gate 					if (repeat && val != 0)
533*7c478bd9Sstevel@tonic-gate 						val->repeat = repeat;
534*7c478bd9Sstevel@tonic-gate 					*rval = val;
535*7c478bd9Sstevel@tonic-gate 				}
536*7c478bd9Sstevel@tonic-gate 				for (n = 0; n < e->element.match.numItems;
537*7c478bd9Sstevel@tonic-gate 						n++) {
538*7c478bd9Sstevel@tonic-gate 					olditem = item;
539*7c478bd9Sstevel@tonic-gate 					item = concatenateMappingItem(item, ni,
540*7c478bd9Sstevel@tonic-gate 						&e->element.match.item[n]);
541*7c478bd9Sstevel@tonic-gate 					freeMappingItem(olditem, ni);
542*7c478bd9Sstevel@tonic-gate 					if (item == 0) {
543*7c478bd9Sstevel@tonic-gate 						ni = 0;
544*7c478bd9Sstevel@tonic-gate 						break;
545*7c478bd9Sstevel@tonic-gate 					}
546*7c478bd9Sstevel@tonic-gate 					ni++;
547*7c478bd9Sstevel@tonic-gate 				}
548*7c478bd9Sstevel@tonic-gate 			}
549*7c478bd9Sstevel@tonic-gate 			break;
550*7c478bd9Sstevel@tonic-gate 		case me_print:
551*7c478bd9Sstevel@tonic-gate 		case me_split:
552*7c478bd9Sstevel@tonic-gate 		case me_extract:
553*7c478bd9Sstevel@tonic-gate 		default:
554*7c478bd9Sstevel@tonic-gate 			/* These shouldn't show up on the LHS; ignore */
555*7c478bd9Sstevel@tonic-gate 			break;
556*7c478bd9Sstevel@tonic-gate 		}
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 		if (tmpitem != 0) {
559*7c478bd9Sstevel@tonic-gate 			olditem = item;
560*7c478bd9Sstevel@tonic-gate 			item = concatenateMappingItem(item, ni, tmpitem);
561*7c478bd9Sstevel@tonic-gate 			freeMappingItem(olditem, ni);
562*7c478bd9Sstevel@tonic-gate 			freeMappingItem(tmpitem, 1);
563*7c478bd9Sstevel@tonic-gate 			ni++;
564*7c478bd9Sstevel@tonic-gate 			if (item == 0) {
565*7c478bd9Sstevel@tonic-gate 				ni = 0;
566*7c478bd9Sstevel@tonic-gate 				break;
567*7c478bd9Sstevel@tonic-gate 			}
568*7c478bd9Sstevel@tonic-gate 		}
569*7c478bd9Sstevel@tonic-gate 	}
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 	if (numItems != 0)
572*7c478bd9Sstevel@tonic-gate 		*numItems = ni;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 	return (item);
575*7c478bd9Sstevel@tonic-gate }
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate __nis_value_t *
578*7c478bd9Sstevel@tonic-gate buildRvalue(__nis_mapping_rlhs_t *rl, __nis_mapping_item_type_t native,
579*7c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv, int *stat) {
580*7c478bd9Sstevel@tonic-gate 	__nis_value_t	*val, *vold = 0, *vnew;
581*7c478bd9Sstevel@tonic-gate 	int		i;
582*7c478bd9Sstevel@tonic-gate 	char		*myself = "buildRvalue";
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	if (rl == 0 || rl->numElements <= 0) {
585*7c478bd9Sstevel@tonic-gate 		/*
586*7c478bd9Sstevel@tonic-gate 		 * No RHS indicates deletion, as does a __nis_value_t
587*7c478bd9Sstevel@tonic-gate 		 * with numVals == -1, so we return such a creature.
588*7c478bd9Sstevel@tonic-gate 		 */
589*7c478bd9Sstevel@tonic-gate 		val = am(myself, sizeof (*val));
590*7c478bd9Sstevel@tonic-gate 		if (val != 0) {
591*7c478bd9Sstevel@tonic-gate 			val->type = vt_string;
592*7c478bd9Sstevel@tonic-gate 			val->numVals = -1;
593*7c478bd9Sstevel@tonic-gate 		}
594*7c478bd9Sstevel@tonic-gate 		return (val);
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 	/* If there is more than one element, we concatenate the values */
598*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < rl->numElements; i++) {
599*7c478bd9Sstevel@tonic-gate 		vnew = getMappingElement(&rl->element[i], native, rv, stat);
600*7c478bd9Sstevel@tonic-gate 		val = concatenateValues(vold, vnew);
601*7c478bd9Sstevel@tonic-gate 		freeValue(vnew, 1);
602*7c478bd9Sstevel@tonic-gate 		freeValue(vold, 1);
603*7c478bd9Sstevel@tonic-gate 		vold = val;
604*7c478bd9Sstevel@tonic-gate 	}
605*7c478bd9Sstevel@tonic-gate 	return (val);
606*7c478bd9Sstevel@tonic-gate }
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate /*
609*7c478bd9Sstevel@tonic-gate  * Derive values for the LDAP attributes specified by the rule 'r',
610*7c478bd9Sstevel@tonic-gate  * and add them to the rule-value 'rv'.
611*7c478bd9Sstevel@tonic-gate  *
612*7c478bd9Sstevel@tonic-gate  * If 'doAssign' is set, out-of-context assignments are performed,
613*7c478bd9Sstevel@tonic-gate  * otherwise not.
614*7c478bd9Sstevel@tonic-gate  */
615*7c478bd9Sstevel@tonic-gate __nis_rule_value_t *
616*7c478bd9Sstevel@tonic-gate addLdapRuleValue(__nis_table_mapping_t *t,
617*7c478bd9Sstevel@tonic-gate 			__nis_mapping_rule_t *r,
618*7c478bd9Sstevel@tonic-gate 			__nis_mapping_item_type_t lnative,
619*7c478bd9Sstevel@tonic-gate 			__nis_mapping_item_type_t rnative,
620*7c478bd9Sstevel@tonic-gate 			__nis_rule_value_t *rv,
621*7c478bd9Sstevel@tonic-gate 			int doAssign, int *stat) {
622*7c478bd9Sstevel@tonic-gate 	int			i, j;
623*7c478bd9Sstevel@tonic-gate 	char			**new;
624*7c478bd9Sstevel@tonic-gate 	__nis_value_t		*rval, *lval;
625*7c478bd9Sstevel@tonic-gate 	__nis_buffer_t		b = {0, 0};
626*7c478bd9Sstevel@tonic-gate 	__nis_mapping_item_t	*litem;
627*7c478bd9Sstevel@tonic-gate 	int			numItems;
628*7c478bd9Sstevel@tonic-gate 	char			**dn = 0;
629*7c478bd9Sstevel@tonic-gate 	int			numDN = 0;
630*7c478bd9Sstevel@tonic-gate 	char			*myself = "addLdapRuleValue";
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	/* Do we have the required values ? */
634*7c478bd9Sstevel@tonic-gate 	if (rv == 0)
635*7c478bd9Sstevel@tonic-gate 		return (0);
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 	/*
638*7c478bd9Sstevel@tonic-gate 	 * Establish appropriate search base. For rnative == mit_nisplus,
639*7c478bd9Sstevel@tonic-gate 	 * we're deriving LDAP attribute values from NIS+ columns; in other
640*7c478bd9Sstevel@tonic-gate 	 * words, we're writing to LDAP, and should use the write.base value.
641*7c478bd9Sstevel@tonic-gate 	 */
642*7c478bd9Sstevel@tonic-gate 	__nisdb_get_tsd()->searchBase = (rnative == mit_nisplus) ?
643*7c478bd9Sstevel@tonic-gate 		t->objectDN->write.base : t->objectDN->read.base;
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 	/* Set escapeFlag if LHS is "dn" to escape special chars */
646*7c478bd9Sstevel@tonic-gate 	if (yp2ldap && r->lhs.numElements == 1 &&
647*7c478bd9Sstevel@tonic-gate 		r->lhs.element->type == me_item &&
648*7c478bd9Sstevel@tonic-gate 		r->lhs.element->element.item.type == mit_ldap &&
649*7c478bd9Sstevel@tonic-gate 		strcasecmp(r->lhs.element->element.item.name, "dn") == 0) {
650*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->escapeFlag = '1';
651*7c478bd9Sstevel@tonic-gate 	}
652*7c478bd9Sstevel@tonic-gate 
653*7c478bd9Sstevel@tonic-gate 	/* Build the RHS value */
654*7c478bd9Sstevel@tonic-gate 	rval = buildRvalue(&r->rhs, rnative, rv, stat);
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	/* Reset escapeFlag */
657*7c478bd9Sstevel@tonic-gate 	__nisdb_get_tsd()->escapeFlag = '\0';
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	if (rval == 0)
660*7c478bd9Sstevel@tonic-gate 		return (rv);
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	/*
663*7c478bd9Sstevel@tonic-gate 	 * Special case: If we got no value for the RHS (presumably because
664*7c478bd9Sstevel@tonic-gate 	 * we're missing one or more item values), we don't produce an lval.
665*7c478bd9Sstevel@tonic-gate 	 * Note that this isn't the same thing as an empty value, which we
666*7c478bd9Sstevel@tonic-gate 	 * faithfully try to transmit to LDAP.
667*7c478bd9Sstevel@tonic-gate 	 */
668*7c478bd9Sstevel@tonic-gate 	if (rval->numVals == 1 && rval->val[0].value == 0) {
669*7c478bd9Sstevel@tonic-gate 		freeValue(rval, 1);
670*7c478bd9Sstevel@tonic-gate 		return (rv);
671*7c478bd9Sstevel@tonic-gate 	}
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	/* Obtain the LHS item names */
674*7c478bd9Sstevel@tonic-gate 	litem = buildLvalue(&r->lhs, &rval, &numItems);
675*7c478bd9Sstevel@tonic-gate 	if (litem == 0) {
676*7c478bd9Sstevel@tonic-gate 		freeValue(rval, 1);
677*7c478bd9Sstevel@tonic-gate 		return (rv);
678*7c478bd9Sstevel@tonic-gate 	}
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 	/* Get string representations of the LHS item names */
681*7c478bd9Sstevel@tonic-gate 	lval = 0;
682*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numItems; i++) {
683*7c478bd9Sstevel@tonic-gate 		__nis_value_t	*tmpval, *old;
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 		tmpval = getMappingItem(&litem[i], lnative, 0, 0, NULL);
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 		/*
688*7c478bd9Sstevel@tonic-gate 		 * If the LHS item is out-of-context, we do the
689*7c478bd9Sstevel@tonic-gate 		 * assignment right here.
690*7c478bd9Sstevel@tonic-gate 		 */
691*7c478bd9Sstevel@tonic-gate 		if (doAssign && litem[i].type == mit_nisplus) {
692*7c478bd9Sstevel@tonic-gate 			int	err;
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 			err = storeNisPlus(&litem[i], i, numItems,
695*7c478bd9Sstevel@tonic-gate 						rv, t->objName, rval);
696*7c478bd9Sstevel@tonic-gate 			if (err != NIS_SUCCESS) {
697*7c478bd9Sstevel@tonic-gate 				char	*iname = "<unknown>";
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 				if (tmpval != 0 &&
700*7c478bd9Sstevel@tonic-gate 						tmpval->numVals == 1)
701*7c478bd9Sstevel@tonic-gate 					iname = tmpval->val[0].value;
702*7c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
703*7c478bd9Sstevel@tonic-gate 					"%s: NIS+ store \"%s\": %s",
704*7c478bd9Sstevel@tonic-gate 					myself, iname,
705*7c478bd9Sstevel@tonic-gate 					nis_sperrno(err));
706*7c478bd9Sstevel@tonic-gate 			}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 			freeValue(tmpval, 1);
709*7c478bd9Sstevel@tonic-gate 			continue;
710*7c478bd9Sstevel@tonic-gate 		} else if (doAssign && litem[i].type == mit_ldap &&
711*7c478bd9Sstevel@tonic-gate 				litem[i].searchSpec.triple.scope !=
712*7c478bd9Sstevel@tonic-gate 					LDAP_SCOPE_UNKNOWN &&
713*7c478bd9Sstevel@tonic-gate 				slen(litem[i].searchSpec.triple.base) > 0 &&
714*7c478bd9Sstevel@tonic-gate 				(slen(litem[i].searchSpec.triple.attrs) > 0 ||
715*7c478bd9Sstevel@tonic-gate 				litem[i].searchSpec.triple.element != 0)) {
716*7c478bd9Sstevel@tonic-gate 			int	stat;
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate 			if (dn == 0)
719*7c478bd9Sstevel@tonic-gate 				dn = findDNs(myself, rv, 1,
720*7c478bd9Sstevel@tonic-gate 					t->objectDN->write.base,
721*7c478bd9Sstevel@tonic-gate 					&numDN);
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 			stat = storeLDAP(&litem[i], i, numItems, rval,
724*7c478bd9Sstevel@tonic-gate 				t->objectDN, dn, numDN);
725*7c478bd9Sstevel@tonic-gate 			if (stat != LDAP_SUCCESS) {
726*7c478bd9Sstevel@tonic-gate 				char	*iname = "<unknown>";
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 				if (tmpval != 0 &&
729*7c478bd9Sstevel@tonic-gate 						tmpval->numVals == 1)
730*7c478bd9Sstevel@tonic-gate 					iname = tmpval->val[0].value;
731*7c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
732*7c478bd9Sstevel@tonic-gate 					"%s: LDAP store \"%s\": %s",
733*7c478bd9Sstevel@tonic-gate 					myself, iname,
734*7c478bd9Sstevel@tonic-gate 					ldap_err2string(stat));
735*7c478bd9Sstevel@tonic-gate 			}
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate 			freeValue(tmpval, 1);
738*7c478bd9Sstevel@tonic-gate 			continue;
739*7c478bd9Sstevel@tonic-gate 		}
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 		old = lval;
742*7c478bd9Sstevel@tonic-gate 		lval = concatenateValues(old, tmpval);
743*7c478bd9Sstevel@tonic-gate 		freeValue(tmpval, 1);
744*7c478bd9Sstevel@tonic-gate 		freeValue(old, 1);
745*7c478bd9Sstevel@tonic-gate 	}
746*7c478bd9Sstevel@tonic-gate 
747*7c478bd9Sstevel@tonic-gate 	/* Don't need the LHS items themselves anymore */
748*7c478bd9Sstevel@tonic-gate 	freeMappingItem(litem, numItems);
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 	/*
751*7c478bd9Sstevel@tonic-gate 	 * If we don't have an 'lval' (probably because all litem[i]:s
752*7c478bd9Sstevel@tonic-gate 	 * were out-of-context assignments), we're done.
753*7c478bd9Sstevel@tonic-gate 	 */
754*7c478bd9Sstevel@tonic-gate 	if (lval == 0 || lval->numVals <= 0) {
755*7c478bd9Sstevel@tonic-gate 		freeValue(lval, 1);
756*7c478bd9Sstevel@tonic-gate 		freeValue(rval, 1);
757*7c478bd9Sstevel@tonic-gate 		return (rv);
758*7c478bd9Sstevel@tonic-gate 	}
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < lval->numVals; i++) {
761*7c478bd9Sstevel@tonic-gate 		/* Special case: rval->numVals < 0 means deletion */
762*7c478bd9Sstevel@tonic-gate 		if (rval->numVals < 0) {
763*7c478bd9Sstevel@tonic-gate 			(void) addAttr2RuleValue(rval->type,
764*7c478bd9Sstevel@tonic-gate 				lval->val[i].value, 0, 0, rv);
765*7c478bd9Sstevel@tonic-gate 			continue;
766*7c478bd9Sstevel@tonic-gate 		}
767*7c478bd9Sstevel@tonic-gate 		/* If we're out of values, repeat the last one */
768*7c478bd9Sstevel@tonic-gate 		if (j >= rval->numVals)
769*7c478bd9Sstevel@tonic-gate 			j = (rval->numVals > 0) ? rval->numVals-1 : 0;
770*7c478bd9Sstevel@tonic-gate 		for (0; j < rval->numVals; j++) {
771*7c478bd9Sstevel@tonic-gate 			/*
772*7c478bd9Sstevel@tonic-gate 			 * If this is the 'dn', and the value ends in a
773*7c478bd9Sstevel@tonic-gate 			 * comma, append the appropriate search base.
774*7c478bd9Sstevel@tonic-gate 			 */
775*7c478bd9Sstevel@tonic-gate 			if (strcasecmp("dn", lval->val[i].value) == 0 &&
776*7c478bd9Sstevel@tonic-gate 					lastChar(&rval->val[j]) == ',' &&
777*7c478bd9Sstevel@tonic-gate 					t->objectDN->write.scope !=
778*7c478bd9Sstevel@tonic-gate 						LDAP_SCOPE_UNKNOWN) {
779*7c478bd9Sstevel@tonic-gate 				void	*nval;
780*7c478bd9Sstevel@tonic-gate 				int	nlen = -1;
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate 				nval = appendString2SingleVal(
783*7c478bd9Sstevel@tonic-gate 					t->objectDN->write.base, &rval->val[j],
784*7c478bd9Sstevel@tonic-gate 					&nlen);
785*7c478bd9Sstevel@tonic-gate 				if (nval != 0 && nlen >= 0) {
786*7c478bd9Sstevel@tonic-gate 					sfree(rval->val[j].value);
787*7c478bd9Sstevel@tonic-gate 					rval->val[j].value = nval;
788*7c478bd9Sstevel@tonic-gate 					rval->val[j].length = nlen;
789*7c478bd9Sstevel@tonic-gate 				}
790*7c478bd9Sstevel@tonic-gate 			}
791*7c478bd9Sstevel@tonic-gate 			(void) addAttr2RuleValue(rval->type,
792*7c478bd9Sstevel@tonic-gate 				lval->val[i].value, rval->val[j].value,
793*7c478bd9Sstevel@tonic-gate 				rval->val[j].length, rv);
794*7c478bd9Sstevel@tonic-gate 			/*
795*7c478bd9Sstevel@tonic-gate 			 * If the lval is multi-valued, go on to the
796*7c478bd9Sstevel@tonic-gate 			 * other values; otherwise, quit (but increment
797*7c478bd9Sstevel@tonic-gate 			 * the 'rval' value index).
798*7c478bd9Sstevel@tonic-gate 			 */
799*7c478bd9Sstevel@tonic-gate 			if (!lval->repeat) {
800*7c478bd9Sstevel@tonic-gate 				j++;
801*7c478bd9Sstevel@tonic-gate 				break;
802*7c478bd9Sstevel@tonic-gate 			}
803*7c478bd9Sstevel@tonic-gate 		}
804*7c478bd9Sstevel@tonic-gate 	}
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate 	/* Clean up */
807*7c478bd9Sstevel@tonic-gate 	freeValue(lval, 1);
808*7c478bd9Sstevel@tonic-gate 	freeValue(rval, 1);
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 	return (rv);
811*7c478bd9Sstevel@tonic-gate }
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate /*
814*7c478bd9Sstevel@tonic-gate  * Remove the indicated attribute, and any values for it, from the
815*7c478bd9Sstevel@tonic-gate  * rule-value.
816*7c478bd9Sstevel@tonic-gate  */
817*7c478bd9Sstevel@tonic-gate void
818*7c478bd9Sstevel@tonic-gate delAttrFromRuleValue(__nis_rule_value_t *rv, char *attrName) {
819*7c478bd9Sstevel@tonic-gate 	int	i;
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 	if (rv == 0 || attrName == 0)
822*7c478bd9Sstevel@tonic-gate 		return;
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < rv->numAttrs; i++) {
825*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrName, rv->attrName[i]) == 0) {
826*7c478bd9Sstevel@tonic-gate 			int	j;
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < rv->attrVal[i].numVals; j++)
829*7c478bd9Sstevel@tonic-gate 				sfree(rv->attrVal[i].val[j].value);
830*7c478bd9Sstevel@tonic-gate 			if (rv->attrVal[i].numVals > 0)
831*7c478bd9Sstevel@tonic-gate 				sfree(rv->attrVal[i].val);
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate 			sfree(rv->attrName[i]);
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 			/* Move up the rest of the attribute names/values */
836*7c478bd9Sstevel@tonic-gate 			for (j = i+1; j < rv->numAttrs; j++) {
837*7c478bd9Sstevel@tonic-gate 				rv->attrName[j-1] = rv->attrName[j];
838*7c478bd9Sstevel@tonic-gate 				rv->attrVal[j-1] = rv->attrVal[j];
839*7c478bd9Sstevel@tonic-gate 			}
840*7c478bd9Sstevel@tonic-gate 
841*7c478bd9Sstevel@tonic-gate 			rv->numAttrs -= 1;
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate 			break;
844*7c478bd9Sstevel@tonic-gate 		}
845*7c478bd9Sstevel@tonic-gate 	}
846*7c478bd9Sstevel@tonic-gate }
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate /*
849*7c478bd9Sstevel@tonic-gate  * Remove the indicated column, and any values for it, from the
850*7c478bd9Sstevel@tonic-gate  * rule-value.
851*7c478bd9Sstevel@tonic-gate  */
852*7c478bd9Sstevel@tonic-gate void
853*7c478bd9Sstevel@tonic-gate delColFromRuleValue(__nis_rule_value_t *rv, char *colName) {
854*7c478bd9Sstevel@tonic-gate 	int	i;
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	if (rv == 0 || colName == 0)
857*7c478bd9Sstevel@tonic-gate 		return;
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < rv->numColumns; i++) {
860*7c478bd9Sstevel@tonic-gate 		if (strcmp(colName, rv->colName[i]) == 0) {
861*7c478bd9Sstevel@tonic-gate 			int	j;
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < rv->colVal[i].numVals; j++)
864*7c478bd9Sstevel@tonic-gate 				sfree(rv->colVal[i].val[j].value);
865*7c478bd9Sstevel@tonic-gate 			if (rv->colVal[i].numVals > 0)
866*7c478bd9Sstevel@tonic-gate 				sfree(rv->colVal[i].val);
867*7c478bd9Sstevel@tonic-gate 
868*7c478bd9Sstevel@tonic-gate 			sfree(rv->colName[i]);
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate 			/* Move up the rest of the column names/values */
871*7c478bd9Sstevel@tonic-gate 			for (j = i+1; j < rv->numColumns; j++) {
872*7c478bd9Sstevel@tonic-gate 				rv->colName[j-1] = rv->colName[j];
873*7c478bd9Sstevel@tonic-gate 				rv->colVal[j-1] = rv->colVal[j];
874*7c478bd9Sstevel@tonic-gate 			}
875*7c478bd9Sstevel@tonic-gate 
876*7c478bd9Sstevel@tonic-gate 			rv->numColumns -= 1;
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate 			break;
879*7c478bd9Sstevel@tonic-gate 		}
880*7c478bd9Sstevel@tonic-gate 	}
881*7c478bd9Sstevel@tonic-gate }
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate /*
884*7c478bd9Sstevel@tonic-gate  * Add the write-mode object classes specified by 'objClassAttrs' to the
885*7c478bd9Sstevel@tonic-gate  * rule-value 'rv'.
886*7c478bd9Sstevel@tonic-gate  * If there's an error, 'rv' is deleted, and NULL returned.
887*7c478bd9Sstevel@tonic-gate  */
888*7c478bd9Sstevel@tonic-gate __nis_rule_value_t *
889*7c478bd9Sstevel@tonic-gate addObjectClasses(__nis_rule_value_t *rv, char *objClassAttrs) {
890*7c478bd9Sstevel@tonic-gate 	char	*filter = 0, **fc = 0;
891*7c478bd9Sstevel@tonic-gate 	int	i, nfc = 0;
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	/*
894*7c478bd9Sstevel@tonic-gate 	 * Expect to only use this for existing rule-values, so rv == 0 is
895*7c478bd9Sstevel@tonic-gate 	 * an error.
896*7c478bd9Sstevel@tonic-gate 	 */
897*7c478bd9Sstevel@tonic-gate 	if (rv == 0)
898*7c478bd9Sstevel@tonic-gate 		return (0);
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate 	/*
901*7c478bd9Sstevel@tonic-gate 	 * If 'objClassAttrs' is NULL, we trivially have nothing to do.
902*7c478bd9Sstevel@tonic-gate 	 * Assume the caller knows what it's doing, and return success.
903*7c478bd9Sstevel@tonic-gate 	 */
904*7c478bd9Sstevel@tonic-gate 	if (objClassAttrs == 0)
905*7c478bd9Sstevel@tonic-gate 		return (rv);
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate 	/*
908*7c478bd9Sstevel@tonic-gate 	 * Make an AND-filter of the object classes, and split into
909*7c478bd9Sstevel@tonic-gate 	 * components. (Yes, this is a bit round-about, but leverages
910*7c478bd9Sstevel@tonic-gate 	 * existing functions.)
911*7c478bd9Sstevel@tonic-gate 	 */
912*7c478bd9Sstevel@tonic-gate 	filter = makeFilter(objClassAttrs);
913*7c478bd9Sstevel@tonic-gate 	if (filter == 0) {
914*7c478bd9Sstevel@tonic-gate 		freeRuleValue(rv, 1);
915*7c478bd9Sstevel@tonic-gate 		return (0);
916*7c478bd9Sstevel@tonic-gate 	}
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 	fc = makeFilterComp(filter, &nfc);
919*7c478bd9Sstevel@tonic-gate 	if (fc == 0 || nfc <= 0) {
920*7c478bd9Sstevel@tonic-gate 		free(filter);
921*7c478bd9Sstevel@tonic-gate 		freeRuleValue(rv, 1);
922*7c478bd9Sstevel@tonic-gate 		return (0);
923*7c478bd9Sstevel@tonic-gate 	}
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate 	/* Add the objectClass attributes to the rule-value */
926*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfc; i++) {
927*7c478bd9Sstevel@tonic-gate 		char	*name, *value;
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate 		name = fc[i];
930*7c478bd9Sstevel@tonic-gate 		/* Skip if not of the "name=value" form */
931*7c478bd9Sstevel@tonic-gate 		if ((value = strchr(name, '=')) == 0)
932*7c478bd9Sstevel@tonic-gate 			continue;
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 		*value = '\0';
935*7c478bd9Sstevel@tonic-gate 		value++;
936*7c478bd9Sstevel@tonic-gate 
937*7c478bd9Sstevel@tonic-gate 		/* Skip if the attribute name isn't "objectClass" */
938*7c478bd9Sstevel@tonic-gate 		if (strcasecmp("objectClass", name) != 0)
939*7c478bd9Sstevel@tonic-gate 			continue;
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 		if (addSAttr2RuleValue(name, value, rv) != 0) {
942*7c478bd9Sstevel@tonic-gate 			free(filter);
943*7c478bd9Sstevel@tonic-gate 			freeFilterComp(fc, nfc);
944*7c478bd9Sstevel@tonic-gate 			freeRuleValue(rv, 1);
945*7c478bd9Sstevel@tonic-gate 			return (0);
946*7c478bd9Sstevel@tonic-gate 		}
947*7c478bd9Sstevel@tonic-gate 	}
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate 	free(filter);
950*7c478bd9Sstevel@tonic-gate 	freeFilterComp(fc, nfc);
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 	return (rv);
953*7c478bd9Sstevel@tonic-gate }
954*7c478bd9Sstevel@tonic-gate 
955*7c478bd9Sstevel@tonic-gate 
956*7c478bd9Sstevel@tonic-gate static char *
957*7c478bd9Sstevel@tonic-gate valString(__nis_value_t *val) {
958*7c478bd9Sstevel@tonic-gate 	int	i;
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate 	if (val == 0 || val->type != vt_string)
961*7c478bd9Sstevel@tonic-gate 		return (0);
962*7c478bd9Sstevel@tonic-gate 
963*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < val->numVals; i++) {
964*7c478bd9Sstevel@tonic-gate 		/* Look for a non-NULL, non-zero length value */
965*7c478bd9Sstevel@tonic-gate 		if (val->val[i].value != 0 && val->val[i].length > 0) {
966*7c478bd9Sstevel@tonic-gate 			char	*v = val->val[i].value;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 			/*
969*7c478bd9Sstevel@tonic-gate 			 * Check that there's a NUL at the end. True,
970*7c478bd9Sstevel@tonic-gate 			 * if there isn't, we may be looking beyond
971*7c478bd9Sstevel@tonic-gate 			 * allocated memory. However, we would have done
972*7c478bd9Sstevel@tonic-gate 			 * so in any case when the supposed string was
973*7c478bd9Sstevel@tonic-gate 			 * traversed (printed, etc.), very possibly by
974*7c478bd9Sstevel@tonic-gate 			 * a lot more than one byte. So, it's better to
975*7c478bd9Sstevel@tonic-gate 			 * take a small risk here than a large one later.
976*7c478bd9Sstevel@tonic-gate 			 */
977*7c478bd9Sstevel@tonic-gate 			if (v[val->val[i].length-1] == '\0' ||
978*7c478bd9Sstevel@tonic-gate 					v[val->val[i].length] == '\0')
979*7c478bd9Sstevel@tonic-gate 				return (v);
980*7c478bd9Sstevel@tonic-gate 		}
981*7c478bd9Sstevel@tonic-gate 	}
982*7c478bd9Sstevel@tonic-gate 
983*7c478bd9Sstevel@tonic-gate 	return (0);
984*7c478bd9Sstevel@tonic-gate }
985*7c478bd9Sstevel@tonic-gate 
986*7c478bd9Sstevel@tonic-gate char *
987*7c478bd9Sstevel@tonic-gate findVal(char *name, __nis_rule_value_t *rv, __nis_mapping_item_type_t type) {
988*7c478bd9Sstevel@tonic-gate 	int	i;
989*7c478bd9Sstevel@tonic-gate 
990*7c478bd9Sstevel@tonic-gate 	if (type == mit_nisplus) {
991*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < rv->numColumns; i++) {
992*7c478bd9Sstevel@tonic-gate 			if (rv->colName[i] == 0)
993*7c478bd9Sstevel@tonic-gate 				continue;
994*7c478bd9Sstevel@tonic-gate 			if (strcmp(name, rv->colName[i]) == 0) {
995*7c478bd9Sstevel@tonic-gate 				return (valString(&rv->colVal[i]));
996*7c478bd9Sstevel@tonic-gate 			}
997*7c478bd9Sstevel@tonic-gate 		}
998*7c478bd9Sstevel@tonic-gate 	} else if (type == mit_ldap) {
999*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < rv->numAttrs; i++) {
1000*7c478bd9Sstevel@tonic-gate 			if (rv->attrName[i] == 0)
1001*7c478bd9Sstevel@tonic-gate 				continue;
1002*7c478bd9Sstevel@tonic-gate 			if (strcasecmp(name, rv->attrName[i]) == 0) {
1003*7c478bd9Sstevel@tonic-gate 				return (valString(&rv->attrVal[i]));
1004*7c478bd9Sstevel@tonic-gate 			}
1005*7c478bd9Sstevel@tonic-gate 		}
1006*7c478bd9Sstevel@tonic-gate 	}
1007*7c478bd9Sstevel@tonic-gate 
1008*7c478bd9Sstevel@tonic-gate 	return (0);
1009*7c478bd9Sstevel@tonic-gate }
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate static char	*norv = "<NIL>";
1012*7c478bd9Sstevel@tonic-gate static char	*unknown = "<unknown>";
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate /*
1015*7c478bd9Sstevel@tonic-gate  * Attempt to derive a string identifying the rule-value 'rv'. The
1016*7c478bd9Sstevel@tonic-gate  * returned string is a pointer, either into 'rv', or to static
1017*7c478bd9Sstevel@tonic-gate  * storage, and must not be freed.
1018*7c478bd9Sstevel@tonic-gate  */
1019*7c478bd9Sstevel@tonic-gate char *
1020*7c478bd9Sstevel@tonic-gate rvId(__nis_rule_value_t *rv, __nis_mapping_item_type_t type) {
1021*7c478bd9Sstevel@tonic-gate 	char	*v;
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 	if (rv == 0)
1024*7c478bd9Sstevel@tonic-gate 		return (norv);
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate 	if (rv->numColumns > 0 && type == mit_nisplus) {
1027*7c478bd9Sstevel@tonic-gate 		/*
1028*7c478bd9Sstevel@tonic-gate 		 * Look for a column called "cname" or "name".
1029*7c478bd9Sstevel@tonic-gate 		 * If that fails, try "key" or "alias".
1030*7c478bd9Sstevel@tonic-gate 		 */
1031*7c478bd9Sstevel@tonic-gate 		if ((v = findVal("cname", rv, type)) != 0)
1032*7c478bd9Sstevel@tonic-gate 			return (v);
1033*7c478bd9Sstevel@tonic-gate 		else if ((v = findVal("name", rv, type)) != 0)
1034*7c478bd9Sstevel@tonic-gate 			return (v);
1035*7c478bd9Sstevel@tonic-gate 		else if ((v = findVal("key", rv, type)) != 0)
1036*7c478bd9Sstevel@tonic-gate 			return (v);
1037*7c478bd9Sstevel@tonic-gate 		else if ((v = findVal("alias", rv, type)) != 0)
1038*7c478bd9Sstevel@tonic-gate 			return (v);
1039*7c478bd9Sstevel@tonic-gate 	} else if (rv->numAttrs > 0 && type == mit_ldap) {
1040*7c478bd9Sstevel@tonic-gate 		/*
1041*7c478bd9Sstevel@tonic-gate 		 * Look for "dn", or "cn".
1042*7c478bd9Sstevel@tonic-gate 		 */
1043*7c478bd9Sstevel@tonic-gate 		if ((v = findVal("dn", rv, type)) != 0)
1044*7c478bd9Sstevel@tonic-gate 			return (v);
1045*7c478bd9Sstevel@tonic-gate 		else if ((v = findVal("cn", rv, type)) != 0)
1046*7c478bd9Sstevel@tonic-gate 			return (v);
1047*7c478bd9Sstevel@tonic-gate 	}
1048*7c478bd9Sstevel@tonic-gate 
1049*7c478bd9Sstevel@tonic-gate 	return (unknown);
1050*7c478bd9Sstevel@tonic-gate }
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate /*
1053*7c478bd9Sstevel@tonic-gate  * Merge the rule-values with the same DN into one. Each rule-value
1054*7c478bd9Sstevel@tonic-gate  * in the returned array will have unique 'dn'. On entry, *numVals
1055*7c478bd9Sstevel@tonic-gate  * contains the number of rule-values in 'rv'. On exit, it contains
1056*7c478bd9Sstevel@tonic-gate  * the number of rule-values in the returned array or -1 on error.
1057*7c478bd9Sstevel@tonic-gate  */
1058*7c478bd9Sstevel@tonic-gate __nis_rule_value_t *
1059*7c478bd9Sstevel@tonic-gate mergeRuleValueWithSameDN(__nis_rule_value_t *rv, int *numVals) {
1060*7c478bd9Sstevel@tonic-gate 	__nis_rule_value_t	*rvq = 0;
1061*7c478bd9Sstevel@tonic-gate 	char			*dn, *odn;
1062*7c478bd9Sstevel@tonic-gate 	int			count = 0;
1063*7c478bd9Sstevel@tonic-gate 	int			i, j;
1064*7c478bd9Sstevel@tonic-gate 
1065*7c478bd9Sstevel@tonic-gate 	if (numVals == 0)
1066*7c478bd9Sstevel@tonic-gate 		return (0);
1067*7c478bd9Sstevel@tonic-gate 
1068*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < *numVals; i++) {
1069*7c478bd9Sstevel@tonic-gate 		if ((dn = findVal("dn", &rv[i], mit_ldap)) != 0) {
1070*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < count; j++) {
1071*7c478bd9Sstevel@tonic-gate 				if ((odn = findVal("dn", &rvq[j],
1072*7c478bd9Sstevel@tonic-gate 						mit_ldap)) != 0) {
1073*7c478bd9Sstevel@tonic-gate 					/* case sensitive compare */
1074*7c478bd9Sstevel@tonic-gate 					if (strcmp(dn, odn) != 0)
1075*7c478bd9Sstevel@tonic-gate 						continue;
1076*7c478bd9Sstevel@tonic-gate 					if (mergeRuleValue(&rvq[j],
1077*7c478bd9Sstevel@tonic-gate 							&rv[i]) == -1) {
1078*7c478bd9Sstevel@tonic-gate 						freeRuleValue(rvq, count);
1079*7c478bd9Sstevel@tonic-gate 						*numVals = -1;
1080*7c478bd9Sstevel@tonic-gate 						return (0);
1081*7c478bd9Sstevel@tonic-gate 					}
1082*7c478bd9Sstevel@tonic-gate 					break;
1083*7c478bd9Sstevel@tonic-gate 				} else {
1084*7c478bd9Sstevel@tonic-gate 					freeRuleValue(rvq, count);
1085*7c478bd9Sstevel@tonic-gate 					*numVals = -1;
1086*7c478bd9Sstevel@tonic-gate 					return (0);
1087*7c478bd9Sstevel@tonic-gate 				}
1088*7c478bd9Sstevel@tonic-gate 			}
1089*7c478bd9Sstevel@tonic-gate 			/* if no match, then add it to the rulevalue array */
1090*7c478bd9Sstevel@tonic-gate 			if (j == count) {
1091*7c478bd9Sstevel@tonic-gate 				rvq = growRuleValue(count, count + 1, rvq,
1092*7c478bd9Sstevel@tonic-gate 									&rv[i]);
1093*7c478bd9Sstevel@tonic-gate 				if (rvq == 0) {
1094*7c478bd9Sstevel@tonic-gate 					*numVals = -1;
1095*7c478bd9Sstevel@tonic-gate 					return (0);
1096*7c478bd9Sstevel@tonic-gate 				}
1097*7c478bd9Sstevel@tonic-gate 				count++;
1098*7c478bd9Sstevel@tonic-gate 			}
1099*7c478bd9Sstevel@tonic-gate 		}
1100*7c478bd9Sstevel@tonic-gate 	}
1101*7c478bd9Sstevel@tonic-gate 
1102*7c478bd9Sstevel@tonic-gate 	*numVals = count;
1103*7c478bd9Sstevel@tonic-gate 	return (rvq);
1104*7c478bd9Sstevel@tonic-gate }
1105