1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <strings.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/stat.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <rpcsvc/nis.h>
35*0Sstevel@tonic-gate #include <rpc/xdr.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "ldap_util.h"
38*0Sstevel@tonic-gate #include "ldap_attr.h"
39*0Sstevel@tonic-gate #include "ldap_ruleval.h"
40*0Sstevel@tonic-gate #include "ldap_op.h"
41*0Sstevel@tonic-gate #include "ldap_map.h"
42*0Sstevel@tonic-gate #include "ldap_nisplus.h"
43*0Sstevel@tonic-gate #include "ldap_glob.h"
44*0Sstevel@tonic-gate #include "ldap_xdr.h"
45*0Sstevel@tonic-gate #include "ldap_val.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* From yptol/dit_access_utils.h */
48*0Sstevel@tonic-gate #define	N2LKEY		"rf_key"
49*0Sstevel@tonic-gate #define	N2LIPKEY	"rf_ipkey"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate __nis_hash_table_mt	ldapMappingList = NIS_HASH_TABLE_MT_INIT;
52*0Sstevel@tonic-gate extern	int yp2ldap;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate int
56*0Sstevel@tonic-gate setColumnNames(__nis_table_mapping_t *t) {
57*0Sstevel@tonic-gate 	int	i, j, nic, noc, stat;
58*0Sstevel@tonic-gate 	char	**col;
59*0Sstevel@tonic-gate 	zotypes	type;
60*0Sstevel@tonic-gate 	char	*myself = "setColumnNames";
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	if (t == 0)
63*0Sstevel@tonic-gate 		return (0);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	type = t->objType;
66*0Sstevel@tonic-gate 	col = t->column;
67*0Sstevel@tonic-gate 	nic = (col != 0) ? t->numColumns : -1;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	t->objType = NIS_BOGUS_OBJ;
70*0Sstevel@tonic-gate 	t->obj = 0;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	stat = initializeColumnNames(t->objName, &t->column, &t->numColumns,
73*0Sstevel@tonic-gate 					&t->objType, &t->obj);
74*0Sstevel@tonic-gate 	if (stat == LDAP_OBJECT_CLASS_VIOLATION) {
75*0Sstevel@tonic-gate 		/* Not a table object; that's OK */
76*0Sstevel@tonic-gate 		sfree(col);
77*0Sstevel@tonic-gate 		return (0);
78*0Sstevel@tonic-gate 	} else if (justTesting && stat != LDAP_SUCCESS) {
79*0Sstevel@tonic-gate 		/*
80*0Sstevel@tonic-gate 		 * Restore the parser initialization. This will only work
81*0Sstevel@tonic-gate 		 * correctly if the config file is consistent in the ordering
82*0Sstevel@tonic-gate 		 * of column names, and either no NIS+ lookups are needed,
83*0Sstevel@tonic-gate 		 * or the ordering is the same as in NIS+.
84*0Sstevel@tonic-gate 		 */
85*0Sstevel@tonic-gate 		t->column = col;
86*0Sstevel@tonic-gate 		t->numColumns = nic;
87*0Sstevel@tonic-gate 		/* Make a guess at the object type, based on the name */
88*0Sstevel@tonic-gate 		if (strstr(t->objName, ".org_dir") != 0)
89*0Sstevel@tonic-gate 			t->objType = NIS_TABLE_OBJ;
90*0Sstevel@tonic-gate 		else if (strncmp(t->objName, "admin.groups_dir",
91*0Sstevel@tonic-gate 				sizeof ("admin.groups_dir")) != 0)
92*0Sstevel@tonic-gate 			t->objType = NIS_GROUP_OBJ;
93*0Sstevel@tonic-gate 		else
94*0Sstevel@tonic-gate 			t->objType = NIS_DIRECTORY_OBJ;
95*0Sstevel@tonic-gate 		return (0);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	/*
99*0Sstevel@tonic-gate 	 * If it's a table object, but there are no translation rules,
100*0Sstevel@tonic-gate 	 * this mapping is for the table object itself. In that case,
101*0Sstevel@tonic-gate 	 * we throw away the column names (if any).
102*0Sstevel@tonic-gate 	 */
103*0Sstevel@tonic-gate 	if (t->objType == NIS_TABLE_OBJ && t->numRulesFromLDAP == 0 &&
104*0Sstevel@tonic-gate 			t->numRulesToLDAP == 0) {
105*0Sstevel@tonic-gate 		for (i = 0; i < t->numColumns; i++)
106*0Sstevel@tonic-gate 			sfree(t->column[i]);
107*0Sstevel@tonic-gate 		sfree(t->column);
108*0Sstevel@tonic-gate 		t->column = 0;
109*0Sstevel@tonic-gate 		t->numColumns = 0;
110*0Sstevel@tonic-gate 		noc = 0;
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/*
114*0Sstevel@tonic-gate 	 * Verify that all column names found by the parser
115*0Sstevel@tonic-gate 	 * are present in the actual column list.
116*0Sstevel@tonic-gate 	 */
117*0Sstevel@tonic-gate 	if (verbose) {
118*0Sstevel@tonic-gate 		for (i = 0, noc = 0; i < nic; i++) {
119*0Sstevel@tonic-gate 			int	found = 0;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 			if (col[i] == 0)
122*0Sstevel@tonic-gate 				continue;
123*0Sstevel@tonic-gate 			/* Skip the 'zo_*' special column names */
124*0Sstevel@tonic-gate 			if (isObjAttrString(col[i]))
125*0Sstevel@tonic-gate 				continue;
126*0Sstevel@tonic-gate 			for (j = 0; j < t->numColumns; j++) {
127*0Sstevel@tonic-gate 				if (strcmp(col[i], t->column[j]) == 0) {
128*0Sstevel@tonic-gate 					noc++;
129*0Sstevel@tonic-gate 					found = 1;
130*0Sstevel@tonic-gate 					break;
131*0Sstevel@tonic-gate 				}
132*0Sstevel@tonic-gate 			}
133*0Sstevel@tonic-gate 			if (!found) {
134*0Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
135*0Sstevel@tonic-gate 					"%s: No column \"%s\" in \"%s\"",
136*0Sstevel@tonic-gate 					myself, NIL(col[i]), NIL(t->objName));
137*0Sstevel@tonic-gate 			}
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/* Remove any setup by the parser */
142*0Sstevel@tonic-gate 	for (i = 0; i < nic; i++) {
143*0Sstevel@tonic-gate 		sfree(col[i]);
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 	sfree(col);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	return (0);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate void
151*0Sstevel@tonic-gate freeSingleObjAttr(__nis_obj_attr_t *attr) {
152*0Sstevel@tonic-gate 	if (attr == 0)
153*0Sstevel@tonic-gate 		return;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	sfree(attr->zo_owner);
156*0Sstevel@tonic-gate 	sfree(attr->zo_group);
157*0Sstevel@tonic-gate 	sfree(attr->zo_domain);
158*0Sstevel@tonic-gate 	sfree(attr);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate void
162*0Sstevel@tonic-gate freeObjAttr(__nis_obj_attr_t **attr, int numAttr) {
163*0Sstevel@tonic-gate 	int	i;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (attr == 0)
166*0Sstevel@tonic-gate 		return;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	for (i = 0; i < numAttr; i++) {
169*0Sstevel@tonic-gate 		freeSingleObjAttr(attr[i]);
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	sfree(attr);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate __nis_obj_attr_t *
176*0Sstevel@tonic-gate cloneObjAttr(__nis_obj_attr_t *old) {
177*0Sstevel@tonic-gate 	__nis_obj_attr_t	*new;
178*0Sstevel@tonic-gate 	char			*myself = "cloneObjAttr";
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if (old == 0)
181*0Sstevel@tonic-gate 		return (0);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	new = am(myself, sizeof (*new));
184*0Sstevel@tonic-gate 	if (new == 0)
185*0Sstevel@tonic-gate 		return (0);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	new->zo_owner = sdup(myself, T, old->zo_owner);
188*0Sstevel@tonic-gate 	if (new->zo_owner == 0 && old->zo_owner != 0)
189*0Sstevel@tonic-gate 		goto cleanup;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	new->zo_group = sdup(myself, T, old->zo_group);
192*0Sstevel@tonic-gate 	if (new->zo_group == 0 && old->zo_group != 0)
193*0Sstevel@tonic-gate 		goto cleanup;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	new->zo_domain = sdup(myself, T, old->zo_domain);
196*0Sstevel@tonic-gate 	if (new->zo_domain == 0 && old->zo_domain != 0)
197*0Sstevel@tonic-gate 		goto cleanup;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	new->zo_access = old->zo_access;
200*0Sstevel@tonic-gate 	new->zo_ttl = old->zo_ttl;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	return (new);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate cleanup:
205*0Sstevel@tonic-gate 	freeSingleObjAttr(new);
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	return (0);
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate /*
212*0Sstevel@tonic-gate  * Obtain NIS+ entries (in the form of db_query's) from the supplied table
213*0Sstevel@tonic-gate  * mapping and db_query.
214*0Sstevel@tonic-gate  *
215*0Sstevel@tonic-gate  * If 'qin' is NULL, enumeration is desired.
216*0Sstevel@tonic-gate  *
217*0Sstevel@tonic-gate  * On exit, '*numQueries' contains the number of (db_query *)'s in the
218*0Sstevel@tonic-gate  * return array, '*ldapStat' the LDAP operation status, and '*objAttr'
219*0Sstevel@tonic-gate  * a pointer to an array (of '*numQueries elements) of object attributes
220*0Sstevel@tonic-gate  * (zo_owner, etc.). If no object attributes were retrieved, '*objAttr'
221*0Sstevel@tonic-gate  * is NULL; any and all of the (*objAttr)[i]'s may be NULL.
222*0Sstevel@tonic-gate  */
223*0Sstevel@tonic-gate db_query **
224*0Sstevel@tonic-gate mapFromLDAP(__nis_table_mapping_t *t, db_query *qin, int *numQueries,
225*0Sstevel@tonic-gate 		char *dbId, int *ldapStat, __nis_obj_attr_t ***objAttr) {
226*0Sstevel@tonic-gate 	__nis_table_mapping_t	**tp;
227*0Sstevel@tonic-gate 	db_query		**q;
228*0Sstevel@tonic-gate 	__nis_rule_value_t	*rv;
229*0Sstevel@tonic-gate 	__nis_ldap_search_t	*ls;
230*0Sstevel@tonic-gate 	int			n, numVals, numMatches = 0;
231*0Sstevel@tonic-gate 	int			stat;
232*0Sstevel@tonic-gate 	__nis_obj_attr_t	**attr;
233*0Sstevel@tonic-gate 	char			*myself = "mapFromLDAP";
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	if (ldapStat == 0)
236*0Sstevel@tonic-gate 		ldapStat = &stat;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	if (t == 0 || numQueries == 0) {
239*0Sstevel@tonic-gate 		*ldapStat = LDAP_PARAM_ERROR;
240*0Sstevel@tonic-gate 		return (0);
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/* Select the correct table mapping(s) */
244*0Sstevel@tonic-gate 	tp = selectTableMapping(t, qin, 0, 0, dbId, &numMatches);
245*0Sstevel@tonic-gate 	if (tp == 0 || numMatches <= 0) {
246*0Sstevel@tonic-gate 		/*
247*0Sstevel@tonic-gate 		 * Not really an error; just no matching mapping
248*0Sstevel@tonic-gate 		 * for the query.
249*0Sstevel@tonic-gate 		 */
250*0Sstevel@tonic-gate 		*ldapStat = LDAP_SUCCESS;
251*0Sstevel@tonic-gate 		return (0);
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	q = 0;
255*0Sstevel@tonic-gate 	attr = 0;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	/* For each mapping */
258*0Sstevel@tonic-gate 	for (numVals = 0, n = 0; n < numMatches; n++) {
259*0Sstevel@tonic-gate 		db_query		**qt;
260*0Sstevel@tonic-gate 		int			i, nqt = 0, filterOnQin, res = 0;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 		t = tp[n];
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 		if (qin != 0) {
265*0Sstevel@tonic-gate 			rv = buildNisPlusRuleValue(t, qin, 0);
266*0Sstevel@tonic-gate 			if (rv != 0) {
267*0Sstevel@tonic-gate 				/*
268*0Sstevel@tonic-gate 				 * Depending on the value of res, we shall
269*0Sstevel@tonic-gate 				 * proceed to next table mapping.
270*0Sstevel@tonic-gate 				 */
271*0Sstevel@tonic-gate 				ls = createLdapRequest(t, rv, 0, 1, &res, NULL);
272*0Sstevel@tonic-gate 			}
273*0Sstevel@tonic-gate 			else
274*0Sstevel@tonic-gate 				ls = 0;
275*0Sstevel@tonic-gate 		} else {
276*0Sstevel@tonic-gate 			/* Build enumeration request */
277*0Sstevel@tonic-gate 			rv = 0;
278*0Sstevel@tonic-gate 			ls = createLdapRequest(t, 0, 0, 1, NULL, NULL);
279*0Sstevel@tonic-gate 		}
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 		freeRuleValue(rv, 1);
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 		if (ls == 0) {
284*0Sstevel@tonic-gate 			/*
285*0Sstevel@tonic-gate 			 * if the res is NP_LDAP_RULES_NO_VALUE, that means we
286*0Sstevel@tonic-gate 			 * have enough NIS+ columns for the rules to produce
287*0Sstevel@tonic-gate 			 * values, but none of them did, so continue to the
288*0Sstevel@tonic-gate 			 * next table mapping. Otherwise do cleanup and return
289*0Sstevel@tonic-gate 			 * error.
290*0Sstevel@tonic-gate 			 */
291*0Sstevel@tonic-gate 			if (res == NP_LDAP_RULES_NO_VALUE)
292*0Sstevel@tonic-gate 				continue;
293*0Sstevel@tonic-gate 			for (i = 0; i < numVals; i++)
294*0Sstevel@tonic-gate 				freeQuery(q[i]);
295*0Sstevel@tonic-gate 			sfree(q);
296*0Sstevel@tonic-gate 			free(tp);
297*0Sstevel@tonic-gate 			*ldapStat = LDAP_OPERATIONS_ERROR;
298*0Sstevel@tonic-gate 			return (0);
299*0Sstevel@tonic-gate 		}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		/* Query LDAP */
302*0Sstevel@tonic-gate 		nqt = (ls->isDN || qin != 0) ? 0 : -1;
303*0Sstevel@tonic-gate 		rv = ldapSearch(ls, &nqt, 0, ldapStat);
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 		/*
306*0Sstevel@tonic-gate 		 * If qin != 0, then we need to make sure that the
307*0Sstevel@tonic-gate 		 * LDAP search is filtered so that only entries that
308*0Sstevel@tonic-gate 		 * are compatible with 'qin' are retained. This will
309*0Sstevel@tonic-gate 		 * happen automatically if we do a DN search (in which
310*0Sstevel@tonic-gate 		 * case, no need to filter on 'qin').
311*0Sstevel@tonic-gate 		 */
312*0Sstevel@tonic-gate 		if (ls->isDN || qin == 0)
313*0Sstevel@tonic-gate 			filterOnQin = 0;
314*0Sstevel@tonic-gate 		else
315*0Sstevel@tonic-gate 			filterOnQin = 1;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		freeLdapSearch(ls);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		/* Convert rule-values to db_query's */
320*0Sstevel@tonic-gate 		if (rv != 0 && nqt > 0) {
321*0Sstevel@tonic-gate 			int			nrv = nqt;
322*0Sstevel@tonic-gate 			__nis_obj_attr_t	**at = 0;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 			qt = ruleValue2Query(t, rv,
325*0Sstevel@tonic-gate 				(filterOnQin) ? qin : 0, &at, &nqt);
326*0Sstevel@tonic-gate 			freeRuleValue(rv, nrv);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 			if (qt != 0 && q == 0) {
329*0Sstevel@tonic-gate 				q = qt;
330*0Sstevel@tonic-gate 				attr = at;
331*0Sstevel@tonic-gate 				numVals = nqt;
332*0Sstevel@tonic-gate 			} else if (qt != 0) {
333*0Sstevel@tonic-gate 				db_query		**tmp;
334*0Sstevel@tonic-gate 				__nis_obj_attr_t	**atmp;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 				/* Extend the 'q' array */
337*0Sstevel@tonic-gate 				tmp = realloc(q,
338*0Sstevel@tonic-gate 					(numVals+nqt) * sizeof (q[0]));
339*0Sstevel@tonic-gate 				/* ... and the 'attr' array */
340*0Sstevel@tonic-gate 				atmp = realloc(attr,
341*0Sstevel@tonic-gate 					(numVals+nqt) * sizeof (attr[0]));
342*0Sstevel@tonic-gate 				if (tmp == 0 || atmp == 0) {
343*0Sstevel@tonic-gate 					logmsg(MSG_NOMEM, LOG_ERR,
344*0Sstevel@tonic-gate 						"%s: realloc(%d) => NULL",
345*0Sstevel@tonic-gate 						myself,
346*0Sstevel@tonic-gate 						(numVals+nqt) * sizeof (q[0]));
347*0Sstevel@tonic-gate 					for (i = 0; i < numVals; i++)
348*0Sstevel@tonic-gate 						freeQuery(q[i]);
349*0Sstevel@tonic-gate 					for (i = 0; i < nqt; i++)
350*0Sstevel@tonic-gate 						freeQuery(qt[i]);
351*0Sstevel@tonic-gate 					sfree(tmp);
352*0Sstevel@tonic-gate 					sfree(atmp);
353*0Sstevel@tonic-gate 					sfree(q);
354*0Sstevel@tonic-gate 					sfree(qt);
355*0Sstevel@tonic-gate 					sfree(tp);
356*0Sstevel@tonic-gate 					freeObjAttr(at, nqt);
357*0Sstevel@tonic-gate 					freeObjAttr(attr, numVals);
358*0Sstevel@tonic-gate 					*ldapStat = LDAP_NO_MEMORY;
359*0Sstevel@tonic-gate 					return (0);
360*0Sstevel@tonic-gate 				}
361*0Sstevel@tonic-gate 				q = tmp;
362*0Sstevel@tonic-gate 				attr = atmp;
363*0Sstevel@tonic-gate 				/* Add the results for this 't' */
364*0Sstevel@tonic-gate 				(void) memcpy(&q[numVals], qt,
365*0Sstevel@tonic-gate 						nqt * sizeof (qt[0]));
366*0Sstevel@tonic-gate 				(void) memcpy(&attr[numVals], at,
367*0Sstevel@tonic-gate 						nqt * sizeof (at[0]));
368*0Sstevel@tonic-gate 				numVals += nqt;
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 				sfree(qt);
371*0Sstevel@tonic-gate 				sfree(at);
372*0Sstevel@tonic-gate 			}
373*0Sstevel@tonic-gate 		}
374*0Sstevel@tonic-gate 	}
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	*numQueries = numVals;
377*0Sstevel@tonic-gate 	if (objAttr != 0)
378*0Sstevel@tonic-gate 		*objAttr = attr;
379*0Sstevel@tonic-gate 	else
380*0Sstevel@tonic-gate 		freeObjAttr(attr, numVals);
381*0Sstevel@tonic-gate 	sfree(tp);
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	return (q);
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*
387*0Sstevel@tonic-gate  * Add the object attributes (zo_owner, etc.) to the rule-value 'rv'.
388*0Sstevel@tonic-gate  * Returns a pointer to the (possibly newly allocated) rule-value,
389*0Sstevel@tonic-gate  * or NULL in case of failure. If not returning 'rvIn', the latter
390*0Sstevel@tonic-gate  * will have been freed.
391*0Sstevel@tonic-gate  */
392*0Sstevel@tonic-gate __nis_rule_value_t *
393*0Sstevel@tonic-gate addObjAttr2RuleValue(nis_object *obj, __nis_rule_value_t *rvIn) {
394*0Sstevel@tonic-gate 	__nis_rule_value_t	*rv;
395*0Sstevel@tonic-gate 	char			abuf[2 * sizeof (obj->zo_access) + 1];
396*0Sstevel@tonic-gate 	char			tbuf[2 * sizeof (obj->zo_ttl) + 1];
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (obj == 0)
399*0Sstevel@tonic-gate 		return (0);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (rvIn != 0) {
402*0Sstevel@tonic-gate 		rv = rvIn;
403*0Sstevel@tonic-gate 	} else {
404*0Sstevel@tonic-gate 		rv = initRuleValue(1, 0);
405*0Sstevel@tonic-gate 		if (rv == 0)
406*0Sstevel@tonic-gate 			return (0);
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	if (obj->zo_owner != 0) {
410*0Sstevel@tonic-gate 		if (addSCol2RuleValue("zo_owner", obj->zo_owner, rv) != 0) {
411*0Sstevel@tonic-gate 			freeRuleValue(rv, 1);
412*0Sstevel@tonic-gate 			return (0);
413*0Sstevel@tonic-gate 		}
414*0Sstevel@tonic-gate 	}
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	if (obj->zo_group != 0) {
417*0Sstevel@tonic-gate 		if (addSCol2RuleValue("zo_group", obj->zo_group, rv) != 0) {
418*0Sstevel@tonic-gate 			freeRuleValue(rv, 1);
419*0Sstevel@tonic-gate 			return (0);
420*0Sstevel@tonic-gate 		}
421*0Sstevel@tonic-gate 	}
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	if (obj->zo_domain != 0) {
424*0Sstevel@tonic-gate 		if (addSCol2RuleValue("zo_domain", obj->zo_domain, rv) != 0) {
425*0Sstevel@tonic-gate 			freeRuleValue(rv, 1);
426*0Sstevel@tonic-gate 			return (0);
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate 	}
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	(void) memset(abuf, 0, sizeof (abuf));
431*0Sstevel@tonic-gate 	(void) memset(tbuf, 0, sizeof (tbuf));
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	sprintf(abuf, "%x", obj->zo_access);
434*0Sstevel@tonic-gate 	sprintf(tbuf, "%x", obj->zo_ttl);
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (addSCol2RuleValue("zo_access", abuf, rv) != 0) {
437*0Sstevel@tonic-gate 		freeRuleValue(rv, 1);
438*0Sstevel@tonic-gate 		return (0);
439*0Sstevel@tonic-gate 	}
440*0Sstevel@tonic-gate 	if (addSCol2RuleValue("zo_ttl", tbuf, rv) != 0) {
441*0Sstevel@tonic-gate 		freeRuleValue(rv, 1);
442*0Sstevel@tonic-gate 		return (0);
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	return (rv);
446*0Sstevel@tonic-gate }
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate /*
449*0Sstevel@tonic-gate  * Returns a pointer to (NOT a copy of) the value for the specified
450*0Sstevel@tonic-gate  * column 'col' in the rule-value 'rv'.
451*0Sstevel@tonic-gate  */
452*0Sstevel@tonic-gate __nis_value_t *
453*0Sstevel@tonic-gate findColValue(char *col, __nis_rule_value_t *rv) {
454*0Sstevel@tonic-gate 	int		i;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	if (col == 0 || rv == 0 || rv->numColumns <= 0)
457*0Sstevel@tonic-gate 		return (0);
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	for (i = 0; i < rv->numColumns; i++) {
460*0Sstevel@tonic-gate 		if (strcmp(col, rv->colName[i]) == 0)
461*0Sstevel@tonic-gate 			return (&rv->colVal[i]);
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	return (0);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate /*
468*0Sstevel@tonic-gate  * Return the NIS+ object attributes (if any) in the rule-value 'rv'.
469*0Sstevel@tonic-gate  */
470*0Sstevel@tonic-gate __nis_obj_attr_t *
471*0Sstevel@tonic-gate ruleValue2ObjAttr(__nis_rule_value_t *rv) {
472*0Sstevel@tonic-gate 	__nis_obj_attr_t	*attr;
473*0Sstevel@tonic-gate 	__nis_value_t		*val;
474*0Sstevel@tonic-gate 	char			*myself = "ruleValue2ObjAttr";
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	if (rv == 0 || rv->numColumns <= 0)
477*0Sstevel@tonic-gate 		return (0);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	attr = am(myself, sizeof (*attr));
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	if ((val = findColValue("zo_owner", rv)) != 0 &&
482*0Sstevel@tonic-gate 			val->type == vt_string && val->numVals == 1 &&
483*0Sstevel@tonic-gate 			val->val[0].value != 0) {
484*0Sstevel@tonic-gate 		attr->zo_owner = sdup(myself, T, val->val[0].value);
485*0Sstevel@tonic-gate 		if (attr->zo_owner == 0) {
486*0Sstevel@tonic-gate 			freeSingleObjAttr(attr);
487*0Sstevel@tonic-gate 			return (0);
488*0Sstevel@tonic-gate 		}
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	if ((val = findColValue("zo_group", rv)) != 0 &&
492*0Sstevel@tonic-gate 			val->type == vt_string && val->numVals == 1 &&
493*0Sstevel@tonic-gate 			val->val[0].value != 0) {
494*0Sstevel@tonic-gate 		attr->zo_group = sdup(myself, T, val->val[0].value);
495*0Sstevel@tonic-gate 		if (attr->zo_group == 0) {
496*0Sstevel@tonic-gate 			freeSingleObjAttr(attr);
497*0Sstevel@tonic-gate 			return (0);
498*0Sstevel@tonic-gate 		}
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	if ((val = findColValue("zo_domain", rv)) != 0 &&
502*0Sstevel@tonic-gate 			val->type == vt_string && val->numVals == 1 &&
503*0Sstevel@tonic-gate 			val->val[0].value != 0) {
504*0Sstevel@tonic-gate 		attr->zo_domain = sdup(myself, T, val->val[0].value);
505*0Sstevel@tonic-gate 		if (attr->zo_domain == 0) {
506*0Sstevel@tonic-gate 			freeSingleObjAttr(attr);
507*0Sstevel@tonic-gate 			return (0);
508*0Sstevel@tonic-gate 		}
509*0Sstevel@tonic-gate 	}
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	if ((val = findColValue("zo_access", rv)) != 0 &&
512*0Sstevel@tonic-gate 			val->type == vt_string && val->numVals == 1 &&
513*0Sstevel@tonic-gate 			val->val[0].value != 0) {
514*0Sstevel@tonic-gate 		if (sscanf(val->val[0].value, "%x", &attr->zo_access) != 1) {
515*0Sstevel@tonic-gate 			freeSingleObjAttr(attr);
516*0Sstevel@tonic-gate 			return (0);
517*0Sstevel@tonic-gate 		}
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	if ((val = findColValue("zo_ttl", rv)) != 0 &&
521*0Sstevel@tonic-gate 			val->type == vt_string && val->numVals == 1 &&
522*0Sstevel@tonic-gate 			val->val[0].value != 0) {
523*0Sstevel@tonic-gate 		if (sscanf(val->val[0].value, "%x", &attr->zo_ttl) != 1) {
524*0Sstevel@tonic-gate 			freeSingleObjAttr(attr);
525*0Sstevel@tonic-gate 			return (0);
526*0Sstevel@tonic-gate 		}
527*0Sstevel@tonic-gate 	}
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	return (attr);
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate /*
533*0Sstevel@tonic-gate  * If the supplied string is one of the object attributes, return one.
534*0Sstevel@tonic-gate  * Otherwise, return zero.
535*0Sstevel@tonic-gate  */
536*0Sstevel@tonic-gate int
537*0Sstevel@tonic-gate isObjAttrString(char *str) {
538*0Sstevel@tonic-gate 	if (str == 0)
539*0Sstevel@tonic-gate 		return (0);
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	if (strcmp("zo_owner", str) == 0 ||
542*0Sstevel@tonic-gate 		strcmp("zo_group", str) == 0 ||
543*0Sstevel@tonic-gate 		strcmp("zo_domain", str) == 0 ||
544*0Sstevel@tonic-gate 		strcmp("zo_access", str) == 0 ||
545*0Sstevel@tonic-gate 		strcmp("zo_ttl", str) == 0)
546*0Sstevel@tonic-gate 		return (1);
547*0Sstevel@tonic-gate 	else
548*0Sstevel@tonic-gate 		return (0);
549*0Sstevel@tonic-gate }
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate /*
553*0Sstevel@tonic-gate  * If the supplied value is one of the object attribute strings, return
554*0Sstevel@tonic-gate  * a pointer to the string. Otherwise, return NULL.
555*0Sstevel@tonic-gate  */
556*0Sstevel@tonic-gate char *
557*0Sstevel@tonic-gate isObjAttr(__nis_single_value_t *val) {
558*0Sstevel@tonic-gate 	if (val == 0 || val->length <= 0 || val->value == 0)
559*0Sstevel@tonic-gate 		return (0);
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	if (isObjAttrString(val->value))
562*0Sstevel@tonic-gate 		return (val->value);
563*0Sstevel@tonic-gate 	else
564*0Sstevel@tonic-gate 		return (0);
565*0Sstevel@tonic-gate }
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate int
568*0Sstevel@tonic-gate setObjAttrField(char *attrName, __nis_single_value_t *val,
569*0Sstevel@tonic-gate 		__nis_obj_attr_t **objAttr) {
570*0Sstevel@tonic-gate 	__nis_obj_attr_t	*attr;
571*0Sstevel@tonic-gate 	char			*myself = "setObjAttrField";
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	if (attrName == 0 || val == 0 || objAttr == 0 ||
574*0Sstevel@tonic-gate 			val->value == 0 || val->length <= 0)
575*0Sstevel@tonic-gate 		return (-1);
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	if (*objAttr != 0) {
578*0Sstevel@tonic-gate 		attr = *objAttr;
579*0Sstevel@tonic-gate 	} else {
580*0Sstevel@tonic-gate 		attr = am(myself, sizeof (*attr));
581*0Sstevel@tonic-gate 		if (attr == 0)
582*0Sstevel@tonic-gate 			return (-2);
583*0Sstevel@tonic-gate 		*objAttr = attr;
584*0Sstevel@tonic-gate 	}
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	if (strcmp("zo_owner", attrName) == 0) {
587*0Sstevel@tonic-gate 		if (attr->zo_owner == 0) {
588*0Sstevel@tonic-gate 			attr->zo_owner = sdup(myself, T, val->value);
589*0Sstevel@tonic-gate 			if (attr->zo_owner == 0)
590*0Sstevel@tonic-gate 				return (-11);
591*0Sstevel@tonic-gate 		}
592*0Sstevel@tonic-gate 	} else if (strcmp("zo_group", attrName) == 0) {
593*0Sstevel@tonic-gate 		if (attr->zo_group == 0) {
594*0Sstevel@tonic-gate 			attr->zo_group = sdup(myself, T, val->value);
595*0Sstevel@tonic-gate 			if (attr->zo_group == 0)
596*0Sstevel@tonic-gate 				return (-12);
597*0Sstevel@tonic-gate 		}
598*0Sstevel@tonic-gate 	} else if (strcmp("zo_domain", attrName) == 0) {
599*0Sstevel@tonic-gate 		if (attr->zo_domain == 0) {
600*0Sstevel@tonic-gate 			attr->zo_domain = sdup(myself, T, val->value);
601*0Sstevel@tonic-gate 			if (attr->zo_domain == 0)
602*0Sstevel@tonic-gate 				return (-13);
603*0Sstevel@tonic-gate 		}
604*0Sstevel@tonic-gate 	} else if (strcmp("zo_access", attrName) == 0) {
605*0Sstevel@tonic-gate 		if (attr->zo_access == 0) {
606*0Sstevel@tonic-gate 			if (sscanf(val->value, "%x", &attr->zo_access) != 1)
607*0Sstevel@tonic-gate 				return (-14);
608*0Sstevel@tonic-gate 		}
609*0Sstevel@tonic-gate 	} else if (strcmp("zo_ttl", attrName) == 0) {
610*0Sstevel@tonic-gate 		if (attr->zo_ttl == 0) {
611*0Sstevel@tonic-gate 			if (sscanf(val->value, "%x", &attr->zo_ttl) != 1)
612*0Sstevel@tonic-gate 				return (-15);
613*0Sstevel@tonic-gate 		}
614*0Sstevel@tonic-gate 	}
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 	return (0);
617*0Sstevel@tonic-gate }
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate /*
620*0Sstevel@tonic-gate  * Return a DN and rule-value for the supplied mapping, db_query's, and
621*0Sstevel@tonic-gate  * input rule-value. This function only works on a single mapping. See
622*0Sstevel@tonic-gate  * mapToLDAP() below for a description of the action depending on the
623*0Sstevel@tonic-gate  * values of 'old' and 'new'.
624*0Sstevel@tonic-gate  *
625*0Sstevel@tonic-gate  * If both 'old' and 'new' are supplied, and the modify would result
626*0Sstevel@tonic-gate  * in a change to the DN, '*oldDN' will contain the old DN. Otherwise
627*0Sstevel@tonic-gate  * (and normally), '*oldDN' will be NULL.
628*0Sstevel@tonic-gate  */
629*0Sstevel@tonic-gate char *
630*0Sstevel@tonic-gate map1qToLDAP(__nis_table_mapping_t *t, db_query *old, db_query *new,
631*0Sstevel@tonic-gate 		__nis_rule_value_t *rvIn, __nis_rule_value_t **rvOutP,
632*0Sstevel@tonic-gate 		char **oldDnP) {
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	__nis_rule_value_t	*rv, *rvt;
635*0Sstevel@tonic-gate 	__nis_ldap_search_t	*ls;
636*0Sstevel@tonic-gate 	char			*dn = 0, *oldDn = 0;
637*0Sstevel@tonic-gate 	__nis_table_mapping_t	del;
638*0Sstevel@tonic-gate 	char			*myself = "map1qToLDAP";
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	if (t == 0 || (old == 0 && new == 0) || rvOutP == 0)
641*0Sstevel@tonic-gate 		return (0);
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	/*
644*0Sstevel@tonic-gate 	 * If entry should be deleted, we look at the delete
645*0Sstevel@tonic-gate 	 * policy in the table mapping. Should it specify a
646*0Sstevel@tonic-gate 	 * rule set, we use that rule set to build a rule-
647*0Sstevel@tonic-gate 	 * value, and the delete actually becomes a modify
648*0Sstevel@tonic-gate 	 * operation.
649*0Sstevel@tonic-gate 	 */
650*0Sstevel@tonic-gate 	if (old != 0 && new == 0) {
651*0Sstevel@tonic-gate 		if (t->objectDN->delDisp == dd_perDbId) {
652*0Sstevel@tonic-gate 			/*
653*0Sstevel@tonic-gate 			 * The functions that build a rule-value from a
654*0Sstevel@tonic-gate 			 * rule set expect a __nis_table_mapping_t, but the
655*0Sstevel@tonic-gate 			 * rule set in the __nis_object_dn_t isn't of that
656*0Sstevel@tonic-gate 			 * form. So, build a pseudo-__nis_table_mapping_t that
657*0Sstevel@tonic-gate 			 * borrows heavily from 't'.
658*0Sstevel@tonic-gate 			 */
659*0Sstevel@tonic-gate 			del = *t;
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 			del.numRulesToLDAP = del.objectDN->numDbIds;
662*0Sstevel@tonic-gate 			del.ruleToLDAP = del.objectDN->dbId;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 			/*
665*0Sstevel@tonic-gate 			 * Do a modify with the pseudo-table
666*0Sstevel@tonic-gate 			 * mapping, and the 'old' db_query
667*0Sstevel@tonic-gate 			 * supplying input to the delete rule
668*0Sstevel@tonic-gate 			 * set.
669*0Sstevel@tonic-gate 			 */
670*0Sstevel@tonic-gate 			t = &del;
671*0Sstevel@tonic-gate 			new = old;
672*0Sstevel@tonic-gate 		} else if (t->objectDN->delDisp == dd_always) {
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate 			/* Nothing to do here; all handled below */
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 		} else if (t->objectDN->delDisp == dd_never) {
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 			return (0);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 		} else {
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 			logmsg(MSG_INVALIDDELDISP, LOG_WARNING,
683*0Sstevel@tonic-gate 				"%s: Invalid delete disposition %d for \"%s\"",
684*0Sstevel@tonic-gate 				myself, t->objectDN->delDisp,
685*0Sstevel@tonic-gate 				NIL(t->dbId));
686*0Sstevel@tonic-gate 			return (0);
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 		}
689*0Sstevel@tonic-gate 	}
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	/* Make a copy of the input rule-value */
692*0Sstevel@tonic-gate 	if (rvIn != 0) {
693*0Sstevel@tonic-gate 		rv = initRuleValue(1, rvIn);
694*0Sstevel@tonic-gate 		if (rv == 0)
695*0Sstevel@tonic-gate 			return (0);
696*0Sstevel@tonic-gate 	} else {
697*0Sstevel@tonic-gate 		rv = 0;
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	/* First get a rule-value from the supplied NIS+ entry. */
701*0Sstevel@tonic-gate 	rvt = rv;
702*0Sstevel@tonic-gate 	rv = buildNisPlusRuleValue(t, ((old != 0) ? old : new), rvt);
703*0Sstevel@tonic-gate 	freeRuleValue(rvt, 1);
704*0Sstevel@tonic-gate 	if (rv == 0) {
705*0Sstevel@tonic-gate 		logmsg(MSG_NORULEVALUE, LOG_WARNING,
706*0Sstevel@tonic-gate 			"%s: No in-query rule-value derived for \"%s\"",
707*0Sstevel@tonic-gate 			myself, NIL(t->dbId));
708*0Sstevel@tonic-gate 		return (0);
709*0Sstevel@tonic-gate 	}
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 	/*
712*0Sstevel@tonic-gate 	 * Create a request (really only care about the DN) from the
713*0Sstevel@tonic-gate 	 * supplied NIS+ entry data.
714*0Sstevel@tonic-gate 	 */
715*0Sstevel@tonic-gate 	ls = createLdapRequest(t, rv, &dn, 0, NULL, NULL);
716*0Sstevel@tonic-gate 	if (ls == 0 || dn == 0) {
717*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
718*0Sstevel@tonic-gate 			"%s: Unable to create LDAP request for %s: %s",
719*0Sstevel@tonic-gate 			myself, NIL(t->dbId),
720*0Sstevel@tonic-gate 			(dn != 0) ? dn : rvId(rv, mit_nisplus));
721*0Sstevel@tonic-gate 		sfree(dn);
722*0Sstevel@tonic-gate 		freeLdapSearch(ls);
723*0Sstevel@tonic-gate 		freeRuleValue(rv, 1);
724*0Sstevel@tonic-gate 		return (0);
725*0Sstevel@tonic-gate 	}
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	freeLdapSearch(ls);
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 	if (new != 0) {
730*0Sstevel@tonic-gate 		/*
731*0Sstevel@tonic-gate 		 * Create a rule-value from the new NIS+ entry.
732*0Sstevel@tonic-gate 		 * Don't want to mix in the rule-value derived
733*0Sstevel@tonic-gate 		 * from 'old', so delete it. However, we still
734*0Sstevel@tonic-gate 		 * want the owner, group, etc., from 'rvIn'.
735*0Sstevel@tonic-gate 		 */
736*0Sstevel@tonic-gate 		if (old != 0) {
737*0Sstevel@tonic-gate 			freeRuleValue(rv, 1);
738*0Sstevel@tonic-gate 			if (rvIn != 0) {
739*0Sstevel@tonic-gate 				rv = initRuleValue(1, rvIn);
740*0Sstevel@tonic-gate 				if (rv == 0) {
741*0Sstevel@tonic-gate 					sfree(dn);
742*0Sstevel@tonic-gate 					return (0);
743*0Sstevel@tonic-gate 				}
744*0Sstevel@tonic-gate 			} else {
745*0Sstevel@tonic-gate 				rv = 0;
746*0Sstevel@tonic-gate 			}
747*0Sstevel@tonic-gate 		}
748*0Sstevel@tonic-gate 		rvt = rv;
749*0Sstevel@tonic-gate 		rv = buildNisPlusRuleValue(t, new, rvt);
750*0Sstevel@tonic-gate 		freeRuleValue(rvt, 1);
751*0Sstevel@tonic-gate 		if (rv == 0) {
752*0Sstevel@tonic-gate 			logmsg(MSG_NORULEVALUE, LOG_WARNING,
753*0Sstevel@tonic-gate 				"%s: No new rule-value derived for \"%s: %s\"",
754*0Sstevel@tonic-gate 				myself, NIL(t->dbId), dn);
755*0Sstevel@tonic-gate 			sfree(dn);
756*0Sstevel@tonic-gate 			return (0);
757*0Sstevel@tonic-gate 		}
758*0Sstevel@tonic-gate 		/*
759*0Sstevel@tonic-gate 		 * Check if the proposed modification would result in a
760*0Sstevel@tonic-gate 		 * a change to the DN.
761*0Sstevel@tonic-gate 		 */
762*0Sstevel@tonic-gate 		if (old != 0) {
763*0Sstevel@tonic-gate 			oldDn = dn;
764*0Sstevel@tonic-gate 			dn = 0;
765*0Sstevel@tonic-gate 			ls = createLdapRequest(t, rv, &dn, 0, NULL, NULL);
766*0Sstevel@tonic-gate 			if (ls == 0 || dn == 0) {
767*0Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
768*0Sstevel@tonic-gate 				"%s: Unable to create new DN for \"%s: %s\"",
769*0Sstevel@tonic-gate 					myself, NIL(t->dbId), oldDn);
770*0Sstevel@tonic-gate 				sfree(oldDn);
771*0Sstevel@tonic-gate 				freeLdapSearch(ls);
772*0Sstevel@tonic-gate 				freeRuleValue(rv, 1);
773*0Sstevel@tonic-gate 				return (0);
774*0Sstevel@tonic-gate 			}
775*0Sstevel@tonic-gate 			freeLdapSearch(ls);
776*0Sstevel@tonic-gate 			if (strcasecmp(oldDn, dn) == 0) {
777*0Sstevel@tonic-gate 				sfree(oldDn);
778*0Sstevel@tonic-gate 				oldDn = 0;
779*0Sstevel@tonic-gate 			}
780*0Sstevel@tonic-gate 		}
781*0Sstevel@tonic-gate 	}
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 	*rvOutP = rv;
785*0Sstevel@tonic-gate 	if (oldDnP != 0)
786*0Sstevel@tonic-gate 		*oldDnP = oldDn;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	return (dn);
789*0Sstevel@tonic-gate }
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate /*
792*0Sstevel@tonic-gate  * Since the DN hash list is an automatic variable, there's no need for
793*0Sstevel@tonic-gate  * locking, and we remove the locking overhead by using the libnsl
794*0Sstevel@tonic-gate  * hash functions.
795*0Sstevel@tonic-gate  */
796*0Sstevel@tonic-gate #undef	NIS_HASH_ITEM
797*0Sstevel@tonic-gate #undef	NIS_HASH_TABLE
798*0Sstevel@tonic-gate #undef	nis_insert_item
799*0Sstevel@tonic-gate #undef	nis_find_item
800*0Sstevel@tonic-gate #undef	nis_pop_item
801*0Sstevel@tonic-gate #undef	nis_remove_item
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate typedef struct {
804*0Sstevel@tonic-gate 	NIS_HASH_ITEM	item;
805*0Sstevel@tonic-gate 	int		index;
806*0Sstevel@tonic-gate 	char		*oldDn;
807*0Sstevel@tonic-gate } __dn_item_t;
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate /*
810*0Sstevel@tonic-gate  * Update LDAP per the supplied table mapping and db_query's.
811*0Sstevel@tonic-gate  *
812*0Sstevel@tonic-gate  * 'nq' is the number of elements in the 'old', 'new', and 'rvIn'
813*0Sstevel@tonic-gate  * arrays. mapToLDAP() generally performs one update for each
814*0Sstevel@tonic-gate  * element; however, if one or more of the individual queries
815*0Sstevel@tonic-gate  * produce the same DN, they're merged into a single update.
816*0Sstevel@tonic-gate  *
817*0Sstevel@tonic-gate  * There are four cases, depending on the values of 'old[iq]' and
818*0Sstevel@tonic-gate  * 'new[iq]':
819*0Sstevel@tonic-gate  *
820*0Sstevel@tonic-gate  * (1)	old[iq] == 0 && new[iq] == 0
821*0Sstevel@tonic-gate  *	No action; skip to next query
822*0Sstevel@tonic-gate  *
823*0Sstevel@tonic-gate  * (2)	old[iq] == 0 && new[iq] != 0
824*0Sstevel@tonic-gate  *	Attempt to use the 'new' db_query to get a DN, and try to create
825*0Sstevel@tonic-gate  *	the corresponding LDAP entry.
826*0Sstevel@tonic-gate  *
827*0Sstevel@tonic-gate  * (3)	old[iq] != 0 && new[iq] == 0
828*0Sstevel@tonic-gate  *	Use the 'old' db_query to get a DN, and try to delete the LDAP
829*0Sstevel@tonic-gate  *	entry per the table mapping.
830*0Sstevel@tonic-gate  *
831*0Sstevel@tonic-gate  * (4)	old[iq] != 0 && new[iq] != 0
832*0Sstevel@tonic-gate  *	Use the 'old' db_query to get a DN, and update (possibly create)
833*0Sstevel@tonic-gate  *	the corresponding LDAP entry per the 'new' db_query.
834*0Sstevel@tonic-gate  *
835*0Sstevel@tonic-gate  * If 'rvIn' is non-NULL, it is expected to contain the object attributes
836*0Sstevel@tonic-gate  * (zo_owner, etc.) to be written to LDAP. 'rvIn' is an array with 'nq'
837*0Sstevel@tonic-gate  * elements.
838*0Sstevel@tonic-gate  *
839*0Sstevel@tonic-gate  * If 'firstOnly' is set, only the first old[iq]/new[iq] pair is used
840*0Sstevel@tonic-gate  * to perform the actual update. Any additional queries specified will
841*0Sstevel@tonic-gate  * have their values folded in, but are not used to derive update targets.
842*0Sstevel@tonic-gate  * This mode is inteded to support the case where multiple NIS+ entries
843*0Sstevel@tonic-gate  * map to one and the same LDAP entry. Note that 'rvIn' must still be
844*0Sstevel@tonic-gate  * an array of 'nq' elements, though if 'firstOnly' is set, it should be
845*0Sstevel@tonic-gate  * OK to leave all but 'rvIn[0]' empty.
846*0Sstevel@tonic-gate  *
847*0Sstevel@tonic-gate  * 'dbId' is used to further narow down the selection of mapping candidates
848*0Sstevel@tonic-gate  * to those matching the 'dbId' value.
849*0Sstevel@tonic-gate  */
850*0Sstevel@tonic-gate int
851*0Sstevel@tonic-gate mapToLDAP(__nis_table_mapping_t *tm, int nq, db_query **old, db_query **new,
852*0Sstevel@tonic-gate 		__nis_rule_value_t *rvIn, int firstOnly, char *dbId) {
853*0Sstevel@tonic-gate 	__nis_table_mapping_t	**tp, **tpa;
854*0Sstevel@tonic-gate 	int			i, n, rnq, iq, r, ret = LDAP_SUCCESS;
855*0Sstevel@tonic-gate 	int			maxMatches, numMatches = 0;
856*0Sstevel@tonic-gate 	__nis_ldap_search_t	*ls;
857*0Sstevel@tonic-gate 	char			**dn = 0, **odn = 0;
858*0Sstevel@tonic-gate 	__nis_rule_value_t	**rv;
859*0Sstevel@tonic-gate 	NIS_HASH_TABLE		dntab;
860*0Sstevel@tonic-gate 	__dn_item_t		*dni;
861*0Sstevel@tonic-gate 	char			*myself = "mapToLDAP";
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	if (tm == 0 || (old == 0 && new == 0) || nq <= 0)
865*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 	/* Determine maximum number of table mapping matches */
868*0Sstevel@tonic-gate 	if (nq == 1) {
869*0Sstevel@tonic-gate 		tp = selectTableMapping(tm,
870*0Sstevel@tonic-gate 			(old != 0 && old[0] != 0) ? old[0] : new[0], 1, 0,
871*0Sstevel@tonic-gate 				dbId, &maxMatches);
872*0Sstevel@tonic-gate 		numMatches = maxMatches;
873*0Sstevel@tonic-gate 	} else {
874*0Sstevel@tonic-gate 		tp = selectTableMapping(tm, 0, 1, 0, dbId, &maxMatches);
875*0Sstevel@tonic-gate 	}
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 	/*
878*0Sstevel@tonic-gate 	 * If no matching mapping, we're not mapping to LDAP in this
879*0Sstevel@tonic-gate 	 * particular case.
880*0Sstevel@tonic-gate 	 */
881*0Sstevel@tonic-gate 	if (tp == 0 || maxMatches == 0) {
882*0Sstevel@tonic-gate 		sfree(tp);
883*0Sstevel@tonic-gate 		return (LDAP_SUCCESS);
884*0Sstevel@tonic-gate 	}
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 	/*
887*0Sstevel@tonic-gate 	 * Allocate the 'rv', 'dn', and 'tpa' arrays. Worst case is that
888*0Sstevel@tonic-gate 	 * we need nq * maxMatches elements in each array. However, if
889*0Sstevel@tonic-gate 	 * 'firstOnly' is set, we only need one element per matching
890*0Sstevel@tonic-gate 	 * mapping in each.
891*0Sstevel@tonic-gate 	 */
892*0Sstevel@tonic-gate 	dn = am(myself, (firstOnly ? 1 : nq) * maxMatches * sizeof (dn[0]));
893*0Sstevel@tonic-gate 	odn = am(myself, (firstOnly ? 1 : nq) * maxMatches * sizeof (odn[0]));
894*0Sstevel@tonic-gate 	rv = am(myself, (firstOnly ? 1 : nq) * maxMatches * sizeof (rv[0]));
895*0Sstevel@tonic-gate 	tpa = am(myself, (firstOnly ? 1 : nq) * maxMatches * sizeof (tpa[0]));
896*0Sstevel@tonic-gate 	if (dn == 0 || odn == 0 || rv == 0 || tpa == 0) {
897*0Sstevel@tonic-gate 		sfree(tp);
898*0Sstevel@tonic-gate 		sfree(dn);
899*0Sstevel@tonic-gate 		sfree(odn);
900*0Sstevel@tonic-gate 		sfree(rv);
901*0Sstevel@tonic-gate 		sfree(tpa);
902*0Sstevel@tonic-gate 		return (LDAP_NO_MEMORY);
903*0Sstevel@tonic-gate 	}
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	/* Unless nq == 1, we don't need the 'tp' value */
906*0Sstevel@tonic-gate 	if (nq != 1)
907*0Sstevel@tonic-gate 		sfree(tp);
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
910*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
911*0Sstevel@tonic-gate 		LOG_WARNING,
912*0Sstevel@tonic-gate #else
913*0Sstevel@tonic-gate 		LOG_INFO,
914*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
915*0Sstevel@tonic-gate 		"%s: %s: %d * %d potential updates",
916*0Sstevel@tonic-gate 		myself, NIL(tm->objName), nq, maxMatches);
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 	(void) memset(&dntab, 0, sizeof (dntab));
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	/*
921*0Sstevel@tonic-gate 	 * Create DNs, column and attribute values, and merge duplicate DNs.
922*0Sstevel@tonic-gate 	 */
923*0Sstevel@tonic-gate 	for (iq = 0, rnq = 0; iq < nq; iq++) {
924*0Sstevel@tonic-gate 		int	idx;
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate 		if ((old == 0 || old[iq] == 0) &&
927*0Sstevel@tonic-gate 				(new == 0 || new[iq] == 0))
928*0Sstevel@tonic-gate 			continue;
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 		/*
931*0Sstevel@tonic-gate 		 * Select matching table mappings; if nq == 1, we've already
932*0Sstevel@tonic-gate 		 * got the 'tp' array from above. We expect this to be the
933*0Sstevel@tonic-gate 		 * most common case, so it's worth special treatment.
934*0Sstevel@tonic-gate 		 */
935*0Sstevel@tonic-gate 		if (nq != 1)
936*0Sstevel@tonic-gate 			tp = selectTableMapping(tm,
937*0Sstevel@tonic-gate 			(old != 0 && old[iq] != 0) ? old[iq] : new[iq], 1, 0,
938*0Sstevel@tonic-gate 					dbId, &numMatches);
939*0Sstevel@tonic-gate 		if (tp == 0)
940*0Sstevel@tonic-gate 			continue;
941*0Sstevel@tonic-gate 		else if (numMatches <= 0) {
942*0Sstevel@tonic-gate 			sfree(tp);
943*0Sstevel@tonic-gate 			continue;
944*0Sstevel@tonic-gate 		}
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 		idx = iq * maxMatches;
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 		if (idx == 0 || !firstOnly)
949*0Sstevel@tonic-gate 			(void) memcpy(&tpa[idx], tp,
950*0Sstevel@tonic-gate 					numMatches * sizeof (tpa[idx]));
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate 		for (n = 0; n < numMatches; n++) {
953*0Sstevel@tonic-gate 			char			*dnt, *odnt;
954*0Sstevel@tonic-gate 			__nis_rule_value_t	*rvt = 0;
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 			if (tp[n] == 0)
957*0Sstevel@tonic-gate 				continue;
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 			dnt = map1qToLDAP(tp[n],
960*0Sstevel@tonic-gate 					(old != 0) ? old[iq] : 0,
961*0Sstevel@tonic-gate 					(new != 0) ? new[iq] : 0,
962*0Sstevel@tonic-gate 					(rvIn != 0) ? &rvIn[iq] : 0,
963*0Sstevel@tonic-gate 					&rvt, &odnt);
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 			if (dnt == 0)
966*0Sstevel@tonic-gate 				continue;
967*0Sstevel@tonic-gate 			if (rvt == 0) {
968*0Sstevel@tonic-gate #ifdef  NISDB_LDAP_DEBUG
969*0Sstevel@tonic-gate 				abort();
970*0Sstevel@tonic-gate #else
971*0Sstevel@tonic-gate 				sfree(dnt);
972*0Sstevel@tonic-gate 				sfree(odnt);
973*0Sstevel@tonic-gate 				continue;
974*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
975*0Sstevel@tonic-gate 			}
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate 			/*
978*0Sstevel@tonic-gate 			 * Create a request to get a rule-value with
979*0Sstevel@tonic-gate 			 * NIS+ data translated to LDAP equivalents.
980*0Sstevel@tonic-gate 			 */
981*0Sstevel@tonic-gate 			ls = createLdapRequest(tp[n], rvt, 0, 0, NULL, NULL);
982*0Sstevel@tonic-gate 			if (ls == 0) {
983*0Sstevel@tonic-gate 				if (ret == LDAP_SUCCESS)
984*0Sstevel@tonic-gate 					ret = LDAP_OPERATIONS_ERROR;
985*0Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
986*0Sstevel@tonic-gate 				"%s: Unable to map to LDAP attrs for %s:dn=%s",
987*0Sstevel@tonic-gate 				myself, NIL(tp[n]->dbId), dnt);
988*0Sstevel@tonic-gate 				sfree(dnt);
989*0Sstevel@tonic-gate 				freeRuleValue(rvt, 1);
990*0Sstevel@tonic-gate 				continue;
991*0Sstevel@tonic-gate 			}
992*0Sstevel@tonic-gate 			freeLdapSearch(ls);
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 			/*
995*0Sstevel@tonic-gate 			 * If the DN is the same as one we already know
996*0Sstevel@tonic-gate 			 * about, merge the rule-values.
997*0Sstevel@tonic-gate 			 */
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 			dni = (__dn_item_t *)nis_find_item(dnt, &dntab);
1000*0Sstevel@tonic-gate 			if (dni != 0) {
1001*0Sstevel@tonic-gate 				i = dni->index;
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 				if (i >= (firstOnly ? ((idx < maxMatches) ?
1004*0Sstevel@tonic-gate 						idx : maxMatches) : idx)) {
1005*0Sstevel@tonic-gate 					goto update_cleanup;
1006*0Sstevel@tonic-gate 				}
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 				if (odnt != 0 && (dni->oldDn == 0 ||
1009*0Sstevel@tonic-gate 						strcasecmp(odnt, dni->oldDn) !=
1010*0Sstevel@tonic-gate 							0)) {
1011*0Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
1012*0Sstevel@tonic-gate 			"%s: DN mismatch while merging updates: %s: %s != %s",
1013*0Sstevel@tonic-gate 						myself, NIL(tpa[i]->dbId),
1014*0Sstevel@tonic-gate 						NIL(odnt), NIL(dni->oldDn));
1015*0Sstevel@tonic-gate 					goto update_cleanup;
1016*0Sstevel@tonic-gate 				}
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 				if (mergeRuleValue(rv[i], rvt)) {
1019*0Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
1020*0Sstevel@tonic-gate 				"%s: Error merging updates for %s:dn=%s",
1021*0Sstevel@tonic-gate 						myself, NIL(tpa[i]->dbId),
1022*0Sstevel@tonic-gate 						dn[i]);
1023*0Sstevel@tonic-gate 					if ((dni = (__dn_item_t *)
1024*0Sstevel@tonic-gate 						nis_remove_item(dnt, &dntab)) !=
1025*0Sstevel@tonic-gate 							0) {
1026*0Sstevel@tonic-gate 						i = dni->index;
1027*0Sstevel@tonic-gate 						sfree(dn[i]);
1028*0Sstevel@tonic-gate 						dn[i] = 0;
1029*0Sstevel@tonic-gate 						tpa[i] = 0;
1030*0Sstevel@tonic-gate 						freeRuleValue(rv[i], 1);
1031*0Sstevel@tonic-gate 						rv[i] = 0;
1032*0Sstevel@tonic-gate 						sfree(dni);
1033*0Sstevel@tonic-gate 					}
1034*0Sstevel@tonic-gate 					goto update_cleanup;
1035*0Sstevel@tonic-gate 				}
1036*0Sstevel@tonic-gate update_cleanup:
1037*0Sstevel@tonic-gate 				sfree(dnt);
1038*0Sstevel@tonic-gate 				dnt = 0;
1039*0Sstevel@tonic-gate 				sfree(odnt);
1040*0Sstevel@tonic-gate 				odnt = 0;
1041*0Sstevel@tonic-gate 				freeRuleValue(rvt, 1);
1042*0Sstevel@tonic-gate 				rvt = 0;
1043*0Sstevel@tonic-gate 			} else if ((iq == 0 || !firstOnly) && dnt != 0) {
1044*0Sstevel@tonic-gate 				dni = am(myself, sizeof (*dni));
1045*0Sstevel@tonic-gate 				if (dni != 0) {
1046*0Sstevel@tonic-gate 					dni->item.name = dnt;
1047*0Sstevel@tonic-gate 					dni->index = idx + n;
1048*0Sstevel@tonic-gate 					dni->oldDn = odnt;
1049*0Sstevel@tonic-gate 				} else {
1050*0Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
1051*0Sstevel@tonic-gate 					"%s: Skipping update for dn=\"%s\"",
1052*0Sstevel@tonic-gate 						myself, dnt);
1053*0Sstevel@tonic-gate 					sfree(dnt);
1054*0Sstevel@tonic-gate 					dnt = 0;
1055*0Sstevel@tonic-gate 				}
1056*0Sstevel@tonic-gate 				if (dni != 0 &&
1057*0Sstevel@tonic-gate 					nis_insert_item((NIS_HASH_ITEM *)dni,
1058*0Sstevel@tonic-gate 							&dntab) != 1) {
1059*0Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
1060*0Sstevel@tonic-gate 					"%s: Unable to memorize dn=\"%s\"",
1061*0Sstevel@tonic-gate 						myself, dnt);
1062*0Sstevel@tonic-gate 					sfree(dnt);
1063*0Sstevel@tonic-gate 					dnt = 0;
1064*0Sstevel@tonic-gate 					sfree(odnt);
1065*0Sstevel@tonic-gate 					odnt = 0;
1066*0Sstevel@tonic-gate 				}
1067*0Sstevel@tonic-gate 				if (dnt != 0) {
1068*0Sstevel@tonic-gate 					dn[idx+n] = dnt;
1069*0Sstevel@tonic-gate 					odn[idx+n] = odnt;
1070*0Sstevel@tonic-gate 					rv[idx+n] = rvt;
1071*0Sstevel@tonic-gate 					rnq++;
1072*0Sstevel@tonic-gate 				} else {
1073*0Sstevel@tonic-gate 					freeRuleValue(rvt, 1);
1074*0Sstevel@tonic-gate 					rvt = 0;
1075*0Sstevel@tonic-gate 				}
1076*0Sstevel@tonic-gate 			} else if (dnt != 0) {
1077*0Sstevel@tonic-gate 				sfree(dnt);
1078*0Sstevel@tonic-gate 				sfree(odnt);
1079*0Sstevel@tonic-gate 				freeRuleValue(rvt, 1);
1080*0Sstevel@tonic-gate 			}
1081*0Sstevel@tonic-gate 		}
1082*0Sstevel@tonic-gate 		sfree(tp);
1083*0Sstevel@tonic-gate 	}
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 	/* Done with the dntab */
1086*0Sstevel@tonic-gate 	while ((dni = (__dn_item_t *)nis_pop_item(&dntab)) != 0) {
1087*0Sstevel@tonic-gate 		sfree(dni);
1088*0Sstevel@tonic-gate 	}
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1091*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1092*0Sstevel@tonic-gate 		LOG_WARNING,
1093*0Sstevel@tonic-gate #else
1094*0Sstevel@tonic-gate 		LOG_INFO,
1095*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1096*0Sstevel@tonic-gate 		"%s: %s: %d update%s requested",
1097*0Sstevel@tonic-gate 		myself, NIL(tm->objName), rnq, rnq != 1 ? "s" : "");
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 	/* Perform the updates */
1100*0Sstevel@tonic-gate 	for (i = rnq = 0; i < (firstOnly ? maxMatches : nq*maxMatches); i++) {
1101*0Sstevel@tonic-gate 		int	delPerDbId;
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 		if (dn[i] == 0)
1104*0Sstevel@tonic-gate 			continue;
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1107*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_INFO,
1108*0Sstevel@tonic-gate 			"%s: %s %s:dn=%s",
1109*0Sstevel@tonic-gate 			myself,
1110*0Sstevel@tonic-gate 			(new != 0 && new[i/maxMatches] != 0) ?
1111*0Sstevel@tonic-gate 				"modify" : "delete",
1112*0Sstevel@tonic-gate 			NIL(tpa[i]->dbId), dn[i]);
1113*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 		delPerDbId = (tpa[i]->objectDN->delDisp == dd_perDbId);
1116*0Sstevel@tonic-gate 		if ((new != 0 && new[i/maxMatches] != 0) || delPerDbId) {
1117*0Sstevel@tonic-gate 			/*
1118*0Sstevel@tonic-gate 			 * Try to modify/create the specified DN. First,
1119*0Sstevel@tonic-gate 			 * however, if the update changes the DN, make
1120*0Sstevel@tonic-gate 			 * that change.
1121*0Sstevel@tonic-gate 			 */
1122*0Sstevel@tonic-gate 			if (odn[i] == 0 || (r = ldapChangeDN(odn[i], dn[i])) ==
1123*0Sstevel@tonic-gate 					LDAP_SUCCESS) {
1124*0Sstevel@tonic-gate 				int	addFirst;
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 				addFirst = (new != 0 &&
1127*0Sstevel@tonic-gate 						new[i/maxMatches] != 0 &&
1128*0Sstevel@tonic-gate 						!delPerDbId);
1129*0Sstevel@tonic-gate 				r = ldapModify(dn[i], rv[i],
1130*0Sstevel@tonic-gate 					tpa[i]->objectDN->write.attrs,
1131*0Sstevel@tonic-gate 						addFirst);
1132*0Sstevel@tonic-gate 			}
1133*0Sstevel@tonic-gate 		} else {
1134*0Sstevel@tonic-gate 			/* Try to delete the specified DN */
1135*0Sstevel@tonic-gate 			r = ldapModify(dn[i], 0,
1136*0Sstevel@tonic-gate 					tpa[i]->objectDN->write.attrs, 0);
1137*0Sstevel@tonic-gate 		}
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate 		if (r == LDAP_SUCCESS) {
1140*0Sstevel@tonic-gate 			rnq++;
1141*0Sstevel@tonic-gate 		} else {
1142*0Sstevel@tonic-gate 			if (ret == LDAP_SUCCESS)
1143*0Sstevel@tonic-gate 				ret = r;
1144*0Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
1145*0Sstevel@tonic-gate 				"%s: LDAP %s request error %d for %s:dn=%s",
1146*0Sstevel@tonic-gate 				myself,
1147*0Sstevel@tonic-gate 				(new != 0 && new[i/maxMatches] != 0) ?
1148*0Sstevel@tonic-gate 					"modify" : "delete",
1149*0Sstevel@tonic-gate 				r, NIL(tpa[i]->dbId), dn[i]);
1150*0Sstevel@tonic-gate 		}
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 		sfree(dn[i]);
1153*0Sstevel@tonic-gate 		dn[i] = 0;
1154*0Sstevel@tonic-gate 		freeRuleValue(rv[i], 1);
1155*0Sstevel@tonic-gate 		rv[i] = 0;
1156*0Sstevel@tonic-gate 	}
1157*0Sstevel@tonic-gate 
1158*0Sstevel@tonic-gate 	sfree(dn);
1159*0Sstevel@tonic-gate 	sfree(odn);
1160*0Sstevel@tonic-gate 	sfree(rv);
1161*0Sstevel@tonic-gate 	sfree(tpa);
1162*0Sstevel@tonic-gate 
1163*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1164*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1165*0Sstevel@tonic-gate 		LOG_WARNING,
1166*0Sstevel@tonic-gate #else
1167*0Sstevel@tonic-gate 		LOG_INFO,
1168*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1169*0Sstevel@tonic-gate 		"%s: %s: %d update%s performed",
1170*0Sstevel@tonic-gate 		myself, NIL(tm->objName), rnq, rnq != 1 ? "s" : "");
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 	return (ret);
1173*0Sstevel@tonic-gate }
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate /*
1176*0Sstevel@tonic-gate  * In nis+2ldap, check if the query 'q' matches the selector index 'x->index'.
1177*0Sstevel@tonic-gate  *
1178*0Sstevel@tonic-gate  * In nis2ldap, if 'name' is provided then check if its value in 'val'
1179*0Sstevel@tonic-gate  * matches the selector index. If 'name' is NULL, then check if rule-value 'rv'
1180*0Sstevel@tonic-gate  * matches the index.
1181*0Sstevel@tonic-gate  * To match the selector index, all fieldspecs in the indexlist should match
1182*0Sstevel@tonic-gate  * (AND). In nis2ldap, an exception is, if there are multiple fieldspecs with
1183*0Sstevel@tonic-gate  * the same fieldname then only one of them needs to match (OR).
1184*0Sstevel@tonic-gate  * Example:
1185*0Sstevel@tonic-gate  *	Indexlist = [host="H*", host="I*", user="U*", domain="D*"]
1186*0Sstevel@tonic-gate  * Then,
1187*0Sstevel@tonic-gate  *	host = "H1", user="U1", domain="D1" ==> pass
1188*0Sstevel@tonic-gate  *	host = "I1", user="U1", domain="D1" ==> pass
1189*0Sstevel@tonic-gate  *	host = "X1", user="U1", domain="D1" ==> fail
1190*0Sstevel@tonic-gate  *	host = "H1", user="X1", domain="D1" ==> fail
1191*0Sstevel@tonic-gate  *	host = "H1", user="U1" ==> fail
1192*0Sstevel@tonic-gate  *
1193*0Sstevel@tonic-gate  * Return 1 in case of a match, 0 otherwise.
1194*0Sstevel@tonic-gate  */
1195*0Sstevel@tonic-gate int
1196*0Sstevel@tonic-gate verifyIndexMatch(__nis_table_mapping_t *x, db_query *q,
1197*0Sstevel@tonic-gate 		__nis_rule_value_t *rv, char *name, char *val) {
1198*0Sstevel@tonic-gate 	int	i, j, k, match = 1;
1199*0Sstevel@tonic-gate 	char	*myself = "verifyIndexMatch";
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	/*
1202*0Sstevel@tonic-gate 	 * The pass and fail arrays are used by N2L to keep track of
1203*0Sstevel@tonic-gate 	 * index matches. This saves us from having matches in a
1204*0Sstevel@tonic-gate 	 * nested loop to decide OR or AND.
1205*0Sstevel@tonic-gate 	 */
1206*0Sstevel@tonic-gate 	int	ppos, fpos;
1207*0Sstevel@tonic-gate 	char	**pass, **fail;
1208*0Sstevel@tonic-gate 
1209*0Sstevel@tonic-gate 	if (x == 0)
1210*0Sstevel@tonic-gate 		return (0);
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate 	/* Trivial match */
1213*0Sstevel@tonic-gate 	if (x->index.numIndexes <= 0 || (!yp2ldap && q == 0))
1214*0Sstevel@tonic-gate 		return (1);
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 	if (yp2ldap) {
1217*0Sstevel@tonic-gate 		if (!(pass = am(myself, x->index.numIndexes * sizeof (char *))))
1218*0Sstevel@tonic-gate 			return (0);
1219*0Sstevel@tonic-gate 		if (!(fail = am(myself,
1220*0Sstevel@tonic-gate 				x->index.numIndexes * sizeof (char *)))) {
1221*0Sstevel@tonic-gate 			sfree(pass);
1222*0Sstevel@tonic-gate 			return (0);
1223*0Sstevel@tonic-gate 		}
1224*0Sstevel@tonic-gate 		ppos = fpos = 0;
1225*0Sstevel@tonic-gate 	}
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 	/* Check each index */
1228*0Sstevel@tonic-gate 	for (i = 0; i < x->index.numIndexes; i++) {
1229*0Sstevel@tonic-gate 		int	len = 0;
1230*0Sstevel@tonic-gate 		char	*value = 0;
1231*0Sstevel@tonic-gate 
1232*0Sstevel@tonic-gate 		/* Skip NULL index names */
1233*0Sstevel@tonic-gate 		if (x->index.name[i] == 0)
1234*0Sstevel@tonic-gate 			continue;
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 		/* Check N2L values */
1237*0Sstevel@tonic-gate 		if (yp2ldap) {
1238*0Sstevel@tonic-gate 			if (name) {
1239*0Sstevel@tonic-gate 				if (strcasecmp(x->index.name[i], name) == 0)
1240*0Sstevel@tonic-gate 					value = val;
1241*0Sstevel@tonic-gate 				else
1242*0Sstevel@tonic-gate 					continue;
1243*0Sstevel@tonic-gate 			} else if (rv) {
1244*0Sstevel@tonic-gate 				if (strcasecmp(x->index.name[i], N2LKEY) == 0 ||
1245*0Sstevel@tonic-gate 					strcasecmp(x->index.name[i], N2LIPKEY)
1246*0Sstevel@tonic-gate 							== 0)
1247*0Sstevel@tonic-gate 					continue;
1248*0Sstevel@tonic-gate 				value = findVal(x->index.name[i], rv,
1249*0Sstevel@tonic-gate 							mit_nisplus);
1250*0Sstevel@tonic-gate 			}
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate 			if (value && verifyMappingMatch(x->index.value[i],
1253*0Sstevel@tonic-gate 									value))
1254*0Sstevel@tonic-gate 				pass[ppos++] = x->index.name[i];
1255*0Sstevel@tonic-gate 			else
1256*0Sstevel@tonic-gate 				fail[fpos++] = x->index.name[i];
1257*0Sstevel@tonic-gate 			continue;
1258*0Sstevel@tonic-gate 		}
1259*0Sstevel@tonic-gate 
1260*0Sstevel@tonic-gate 		/* If here, means nis+2ldap */
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate 		/* Is the index name a known column ? */
1263*0Sstevel@tonic-gate 		for (j = 0; j < x->numColumns; j++) {
1264*0Sstevel@tonic-gate 			if (strcmp(x->index.name[i], x->column[j]) == 0) {
1265*0Sstevel@tonic-gate 				/*
1266*0Sstevel@tonic-gate 				 * Do we have a value for the column ?
1267*0Sstevel@tonic-gate 				 */
1268*0Sstevel@tonic-gate 				for (k = 0; k < q->components.components_len;
1269*0Sstevel@tonic-gate 						k++) {
1270*0Sstevel@tonic-gate 					if (q->components.components_val[k].
1271*0Sstevel@tonic-gate 							which_index == j) {
1272*0Sstevel@tonic-gate 						value = q->components.
1273*0Sstevel@tonic-gate 							components_val[k].
1274*0Sstevel@tonic-gate 							index_value->
1275*0Sstevel@tonic-gate 							itemvalue.
1276*0Sstevel@tonic-gate 							itemvalue_val;
1277*0Sstevel@tonic-gate 						len = q->components.
1278*0Sstevel@tonic-gate 							components_val[k].
1279*0Sstevel@tonic-gate 							index_value->
1280*0Sstevel@tonic-gate 							itemvalue.
1281*0Sstevel@tonic-gate 							itemvalue_len;
1282*0Sstevel@tonic-gate 						break;
1283*0Sstevel@tonic-gate 					}
1284*0Sstevel@tonic-gate 				}
1285*0Sstevel@tonic-gate 				if (value != 0)
1286*0Sstevel@tonic-gate 					break;
1287*0Sstevel@tonic-gate 			}
1288*0Sstevel@tonic-gate 		}
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate 		/*
1291*0Sstevel@tonic-gate 		 * If we found a value, check if it matches the
1292*0Sstevel@tonic-gate 		 * format. If no value found or no match, this
1293*0Sstevel@tonic-gate 		 * mapping is _not_ an alternative. Otherwise,
1294*0Sstevel@tonic-gate 		 * we continue checking any other indexes.
1295*0Sstevel@tonic-gate 		 */
1296*0Sstevel@tonic-gate 		if (value == 0 ||
1297*0Sstevel@tonic-gate 			!verifyMappingMatch(x->index.value[i],
1298*0Sstevel@tonic-gate 				value)) {
1299*0Sstevel@tonic-gate 			match = 0;
1300*0Sstevel@tonic-gate 			break;
1301*0Sstevel@tonic-gate 		}
1302*0Sstevel@tonic-gate 	}
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	if (yp2ldap) {
1305*0Sstevel@tonic-gate 		for (--fpos; fpos >= 0; fpos--) {
1306*0Sstevel@tonic-gate 			for (i = 0; i < ppos; i++) {
1307*0Sstevel@tonic-gate 				if (strcmp(pass[i], fail[fpos]) == 0)
1308*0Sstevel@tonic-gate 					break;
1309*0Sstevel@tonic-gate 			}
1310*0Sstevel@tonic-gate 			if (i == ppos) {
1311*0Sstevel@tonic-gate 				match = 0;
1312*0Sstevel@tonic-gate 				break;
1313*0Sstevel@tonic-gate 			}
1314*0Sstevel@tonic-gate 		}
1315*0Sstevel@tonic-gate 		sfree(pass);
1316*0Sstevel@tonic-gate 		sfree(fail);
1317*0Sstevel@tonic-gate 	}
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 	return (match);
1320*0Sstevel@tonic-gate }
1321*0Sstevel@tonic-gate 
1322*0Sstevel@tonic-gate /*
1323*0Sstevel@tonic-gate  * Return all table mappings that match the column values in 'q'.
1324*0Sstevel@tonic-gate  * If there's no match, return those alternative mappings that don't
1325*0Sstevel@tonic-gate  * have an index; if no such mapping exists, return NULL.
1326*0Sstevel@tonic-gate  *
1327*0Sstevel@tonic-gate  * If 'wantWrite' is set, we want mappings for writing (i.e., data
1328*0Sstevel@tonic-gate  * to LDAP); otherwise, we want mappings for reading.
1329*0Sstevel@tonic-gate  *
1330*0Sstevel@tonic-gate  * If 'wantObj' is set, we want object mappings only (i.e., _not_
1331*0Sstevel@tonic-gate  * those used to map entries in tables).
1332*0Sstevel@tonic-gate  *
1333*0Sstevel@tonic-gate  * If 'dbId' is non-NULL, we select mappings with a matching dbId field.
1334*0Sstevel@tonic-gate  */
1335*0Sstevel@tonic-gate __nis_table_mapping_t **
1336*0Sstevel@tonic-gate selectTableMapping(__nis_table_mapping_t *t, db_query *q,
1337*0Sstevel@tonic-gate 			int wantWrite, int wantObj, char *dbId,
1338*0Sstevel@tonic-gate 			int *numMatches) {
1339*0Sstevel@tonic-gate 	__nis_table_mapping_t	*r, *x, **tp;
1340*0Sstevel@tonic-gate 	int			i, j, k, nm, numap;
1341*0Sstevel@tonic-gate 	char			*myself = "selectTableMapping";
1342*0Sstevel@tonic-gate 
1343*0Sstevel@tonic-gate 	if (numMatches == 0)
1344*0Sstevel@tonic-gate 		numMatches = &nm;
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate 	/*
1347*0Sstevel@tonic-gate 	 * Count the number of possible mappings, so that we can
1348*0Sstevel@tonic-gate 	 * allocate the 'tp' array up front.
1349*0Sstevel@tonic-gate 	 */
1350*0Sstevel@tonic-gate 	for (numap = 0, x = t; x != 0; numap++, x = x->next);
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate 	if (numap == 0) {
1353*0Sstevel@tonic-gate 		*numMatches = 0;
1354*0Sstevel@tonic-gate 		return (0);
1355*0Sstevel@tonic-gate 	}
1356*0Sstevel@tonic-gate 
1357*0Sstevel@tonic-gate 	tp = am(myself, numap * sizeof (tp[0]));
1358*0Sstevel@tonic-gate 	if (tp == 0) {
1359*0Sstevel@tonic-gate 		*numMatches = -1;
1360*0Sstevel@tonic-gate 		return (0);
1361*0Sstevel@tonic-gate 	}
1362*0Sstevel@tonic-gate 
1363*0Sstevel@tonic-gate 	/*
1364*0Sstevel@tonic-gate 	 * Special cases:
1365*0Sstevel@tonic-gate 	 *
1366*0Sstevel@tonic-gate 	 *	q == 0 trivially matches any 't' of the correct object type
1367*0Sstevel@tonic-gate 	 *
1368*0Sstevel@tonic-gate 	 *	wantObj != 0 means we ignore 'q'
1369*0Sstevel@tonic-gate 	 */
1370*0Sstevel@tonic-gate 	if (q == 0 || wantObj) {
1371*0Sstevel@tonic-gate 		for (i = 0, x = t, nm = 0; i < numap; i++, x = x->next) {
1372*0Sstevel@tonic-gate 			if (x->objectDN == 0)
1373*0Sstevel@tonic-gate 				continue;
1374*0Sstevel@tonic-gate 			if (wantWrite) {
1375*0Sstevel@tonic-gate 				if (x->objectDN->write.scope ==
1376*0Sstevel@tonic-gate 						LDAP_SCOPE_UNKNOWN)
1377*0Sstevel@tonic-gate 					continue;
1378*0Sstevel@tonic-gate 			} else {
1379*0Sstevel@tonic-gate 				if (x->objectDN->read.scope ==
1380*0Sstevel@tonic-gate 						LDAP_SCOPE_UNKNOWN)
1381*0Sstevel@tonic-gate 					continue;
1382*0Sstevel@tonic-gate 			}
1383*0Sstevel@tonic-gate 			if (wantObj) {
1384*0Sstevel@tonic-gate 				if (x->numColumns > 0)
1385*0Sstevel@tonic-gate 					continue;
1386*0Sstevel@tonic-gate 			} else {
1387*0Sstevel@tonic-gate 				if (x->numColumns <= 0)
1388*0Sstevel@tonic-gate 					continue;
1389*0Sstevel@tonic-gate 			}
1390*0Sstevel@tonic-gate 			if (dbId != 0 && x->dbId != 0 &&
1391*0Sstevel@tonic-gate 					strcmp(dbId, x->dbId) != 0)
1392*0Sstevel@tonic-gate 				continue;
1393*0Sstevel@tonic-gate 			tp[nm] = x;
1394*0Sstevel@tonic-gate 			nm++;
1395*0Sstevel@tonic-gate 		}
1396*0Sstevel@tonic-gate 		*numMatches = nm;
1397*0Sstevel@tonic-gate 		if (nm == 0) {
1398*0Sstevel@tonic-gate 			sfree(tp);
1399*0Sstevel@tonic-gate 			tp = 0;
1400*0Sstevel@tonic-gate 		}
1401*0Sstevel@tonic-gate 		return (tp);
1402*0Sstevel@tonic-gate 	}
1403*0Sstevel@tonic-gate 
1404*0Sstevel@tonic-gate 	/* Scan all mappings, and collect candidates */
1405*0Sstevel@tonic-gate 	for (nm = 0, r = 0, x = t; x != 0; x = x->next) {
1406*0Sstevel@tonic-gate 		if (x->objectDN == 0)
1407*0Sstevel@tonic-gate 			continue;
1408*0Sstevel@tonic-gate 		if (wantWrite) {
1409*0Sstevel@tonic-gate 			if (x->objectDN->write.scope == LDAP_SCOPE_UNKNOWN)
1410*0Sstevel@tonic-gate 				continue;
1411*0Sstevel@tonic-gate 		} else {
1412*0Sstevel@tonic-gate 			if (x->objectDN->read.scope == LDAP_SCOPE_UNKNOWN)
1413*0Sstevel@tonic-gate 				continue;
1414*0Sstevel@tonic-gate 		}
1415*0Sstevel@tonic-gate 		/* Only want table/entry mappings */
1416*0Sstevel@tonic-gate 		if (x->numColumns <= 0)
1417*0Sstevel@tonic-gate 			continue;
1418*0Sstevel@tonic-gate 		if (dbId != 0 && x->dbId != 0 &&
1419*0Sstevel@tonic-gate 				strcmp(dbId, x->dbId) != 0)
1420*0Sstevel@tonic-gate 			continue;
1421*0Sstevel@tonic-gate 		/*
1422*0Sstevel@tonic-gate 		 * It's a match if: there are no indexes, or we actually
1423*0Sstevel@tonic-gate 		 * match the query with the indexes.
1424*0Sstevel@tonic-gate 		 */
1425*0Sstevel@tonic-gate 		if (x->index.numIndexes <= 0 ||
1426*0Sstevel@tonic-gate 					verifyIndexMatch(x, q, 0, 0, 0)) {
1427*0Sstevel@tonic-gate 			tp[nm] = x;
1428*0Sstevel@tonic-gate 			nm++;
1429*0Sstevel@tonic-gate 		}
1430*0Sstevel@tonic-gate 	}
1431*0Sstevel@tonic-gate 
1432*0Sstevel@tonic-gate 	if (nm == 0) {
1433*0Sstevel@tonic-gate 		free(tp);
1434*0Sstevel@tonic-gate 		tp = 0;
1435*0Sstevel@tonic-gate 	}
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 	*numMatches = nm;
1438*0Sstevel@tonic-gate 
1439*0Sstevel@tonic-gate 	return (tp);
1440*0Sstevel@tonic-gate }
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate /*
1443*0Sstevel@tonic-gate  * Return 1 if there's an indexed mapping, 0 otherwise.
1444*0Sstevel@tonic-gate  */
1445*0Sstevel@tonic-gate int
1446*0Sstevel@tonic-gate haveIndexedMapping(__nis_table_mapping_t *t) {
1447*0Sstevel@tonic-gate 	__nis_table_mapping_t	*x;
1448*0Sstevel@tonic-gate 
1449*0Sstevel@tonic-gate 	for (x = t; x != 0; x = x->next) {
1450*0Sstevel@tonic-gate 		if (x->index.numIndexes > 0)
1451*0Sstevel@tonic-gate 			return (1);
1452*0Sstevel@tonic-gate 	}
1453*0Sstevel@tonic-gate 
1454*0Sstevel@tonic-gate 	return (0);
1455*0Sstevel@tonic-gate }
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate /*
1458*0Sstevel@tonic-gate  * Given an input string 'attrs' of the form "attr1=val1,attr2=val2,...",
1459*0Sstevel@tonic-gate  * or a filter, return the value associated with the attribute 'attrName'.
1460*0Sstevel@tonic-gate  * If no instance of 'attrName' is found, return 'default'. In all cases,
1461*0Sstevel@tonic-gate  * the return value is a copy, and must be freed by the caller.
1462*0Sstevel@tonic-gate  *
1463*0Sstevel@tonic-gate  * Of course, return NULL in case of failure.
1464*0Sstevel@tonic-gate  */
1465*0Sstevel@tonic-gate static char *
1466*0Sstevel@tonic-gate attrVal(char *msg, char *attrName, char *def, char *attrs) {
1467*0Sstevel@tonic-gate 	char	*val, *filter, **fc = 0;
1468*0Sstevel@tonic-gate 	int	i, nfc;
1469*0Sstevel@tonic-gate 	char	*myself = "attrVal";
1470*0Sstevel@tonic-gate 
1471*0Sstevel@tonic-gate 	if (attrName == 0 || attrs == 0)
1472*0Sstevel@tonic-gate 		return (0);
1473*0Sstevel@tonic-gate 
1474*0Sstevel@tonic-gate 	if (msg == 0)
1475*0Sstevel@tonic-gate 		msg = myself;
1476*0Sstevel@tonic-gate 
1477*0Sstevel@tonic-gate 	val = def;
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate 	filter = makeFilter(attrs);
1480*0Sstevel@tonic-gate 	if (filter != 0 && (fc = makeFilterComp(filter, &nfc)) != 0 &&
1481*0Sstevel@tonic-gate 			nfc > 0) {
1482*0Sstevel@tonic-gate 		for (i = 0; i < nfc; i++) {
1483*0Sstevel@tonic-gate 			char	*name, *value;
1484*0Sstevel@tonic-gate 
1485*0Sstevel@tonic-gate 			name = fc[i];
1486*0Sstevel@tonic-gate 			/* Skip if not of attr=value form */
1487*0Sstevel@tonic-gate 			if ((value = strchr(name, '=')) == 0)
1488*0Sstevel@tonic-gate 				continue;
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate 			*value = '\0';
1491*0Sstevel@tonic-gate 			value++;
1492*0Sstevel@tonic-gate 
1493*0Sstevel@tonic-gate 			if (strcasecmp(attrName, name) == 0) {
1494*0Sstevel@tonic-gate 				val = value;
1495*0Sstevel@tonic-gate 				break;
1496*0Sstevel@tonic-gate 			}
1497*0Sstevel@tonic-gate 		}
1498*0Sstevel@tonic-gate 	}
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	if (val != 0)
1501*0Sstevel@tonic-gate 		val = sdup(msg, T, val);
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 	sfree(filter);
1504*0Sstevel@tonic-gate 	freeFilterComp(fc, nfc);
1505*0Sstevel@tonic-gate 
1506*0Sstevel@tonic-gate 	return (val);
1507*0Sstevel@tonic-gate }
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate extern bool_t	xdr_nis_object(register XDR *xdrs, nis_object *objp);
1510*0Sstevel@tonic-gate 
1511*0Sstevel@tonic-gate /*
1512*0Sstevel@tonic-gate  * Copy an XDR:ed version of the NIS+ object 'o' (or the one indicated
1513*0Sstevel@tonic-gate  * by 't->objName' if 'o' is NULL) to the place indicated by
1514*0Sstevel@tonic-gate  * 't->objectDN->write'. Return an appropriate LDAP status code.
1515*0Sstevel@tonic-gate  */
1516*0Sstevel@tonic-gate int
1517*0Sstevel@tonic-gate objToLDAP(__nis_table_mapping_t *t, nis_object *o, entry_obj **ea, int numEa) {
1518*0Sstevel@tonic-gate 	__nis_table_mapping_t	**tp;
1519*0Sstevel@tonic-gate 	XDR			xdr;
1520*0Sstevel@tonic-gate 	nis_result		*res = 0;
1521*0Sstevel@tonic-gate 	char			*objName;
1522*0Sstevel@tonic-gate 	int			stat, osize, n, numMatches = 0;
1523*0Sstevel@tonic-gate 	void			*buf;
1524*0Sstevel@tonic-gate 	__nis_rule_value_t	*rv;
1525*0Sstevel@tonic-gate 	__nis_value_t		*val;
1526*0Sstevel@tonic-gate 	__nis_single_value_t	*sv;
1527*0Sstevel@tonic-gate 	char			**attrName, *dn;
1528*0Sstevel@tonic-gate 	char			*myself = "objToLDAP";
1529*0Sstevel@tonic-gate 
1530*0Sstevel@tonic-gate 	if (t == 0)
1531*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1532*0Sstevel@tonic-gate 
1533*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1534*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1535*0Sstevel@tonic-gate 		LOG_WARNING,
1536*0Sstevel@tonic-gate #else
1537*0Sstevel@tonic-gate 		LOG_INFO,
1538*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1539*0Sstevel@tonic-gate 		"%s: %s", myself, NIL(t->objName));
1540*0Sstevel@tonic-gate 
1541*0Sstevel@tonic-gate 	tp = selectTableMapping(t, 0, 1, 1, 0, &numMatches);
1542*0Sstevel@tonic-gate 	if (tp == 0 || numMatches <= 0) {
1543*0Sstevel@tonic-gate 		sfree(tp);
1544*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK,
1545*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1546*0Sstevel@tonic-gate 			LOG_WARNING,
1547*0Sstevel@tonic-gate #else
1548*0Sstevel@tonic-gate 			LOG_INFO,
1549*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1550*0Sstevel@tonic-gate 			"%s: %s (no mapping)", myself, NIL(t->objName));
1551*0Sstevel@tonic-gate 		return (LDAP_SUCCESS);
1552*0Sstevel@tonic-gate 	}
1553*0Sstevel@tonic-gate 
1554*0Sstevel@tonic-gate 	for (n = 0; n < numMatches; n++) {
1555*0Sstevel@tonic-gate 
1556*0Sstevel@tonic-gate 		t = tp[n];
1557*0Sstevel@tonic-gate 
1558*0Sstevel@tonic-gate 		if (o == 0) {
1559*0Sstevel@tonic-gate 			stat = getNisPlusObj(t->objName, myself, &res);
1560*0Sstevel@tonic-gate 			if (stat != LDAP_SUCCESS) {
1561*0Sstevel@tonic-gate 				sfree(tp);
1562*0Sstevel@tonic-gate 				return (stat);
1563*0Sstevel@tonic-gate 			}
1564*0Sstevel@tonic-gate 
1565*0Sstevel@tonic-gate 			/*
1566*0Sstevel@tonic-gate 			 * getNisPlusObj() only returns success when res != 0,
1567*0Sstevel@tonic-gate 			 * and res->objects.objects_len > 0, so no need to
1568*0Sstevel@tonic-gate 			 * check for those conditons.
1569*0Sstevel@tonic-gate 			 */
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate 			o = res->objects.objects_val;
1572*0Sstevel@tonic-gate 			if (o == 0) {
1573*0Sstevel@tonic-gate 				sfree(tp);
1574*0Sstevel@tonic-gate 				nis_freeresult(res);
1575*0Sstevel@tonic-gate 				return (LDAP_OPERATIONS_ERROR);
1576*0Sstevel@tonic-gate 			}
1577*0Sstevel@tonic-gate 			if (o->zo_data.zo_type == NIS_DIRECTORY_OBJ) {
1578*0Sstevel@tonic-gate 				/* XXX??? get dir list, set 'ea' and 'numEa' */
1579*0Sstevel@tonic-gate 			}
1580*0Sstevel@tonic-gate 		}
1581*0Sstevel@tonic-gate 
1582*0Sstevel@tonic-gate 		buf = (char *)xdrNisObject(o, ea, numEa, &osize);
1583*0Sstevel@tonic-gate 		if (res != 0) {
1584*0Sstevel@tonic-gate 			nis_freeresult(res);
1585*0Sstevel@tonic-gate 			res = 0;
1586*0Sstevel@tonic-gate 		}
1587*0Sstevel@tonic-gate 		if (buf == 0) {
1588*0Sstevel@tonic-gate 			sfree(tp);
1589*0Sstevel@tonic-gate 			return (LDAP_OPERATIONS_ERROR);
1590*0Sstevel@tonic-gate 		}
1591*0Sstevel@tonic-gate 
1592*0Sstevel@tonic-gate 		/*
1593*0Sstevel@tonic-gate 		 * Prepare to build a rule-value containing the XDR:ed
1594*0Sstevel@tonic-gate 		 * object
1595*0Sstevel@tonic-gate 		 */
1596*0Sstevel@tonic-gate 		rv = am(myself, sizeof (*rv));
1597*0Sstevel@tonic-gate 		sv = am(myself, sizeof (*sv));
1598*0Sstevel@tonic-gate 		val = am(myself, sizeof (*val));
1599*0Sstevel@tonic-gate 		attrName = am(myself, sizeof (attrName[0]));
1600*0Sstevel@tonic-gate 		if (attrName != 0)
1601*0Sstevel@tonic-gate 			attrName[0] = attrVal(myself, "nisplusObject",
1602*0Sstevel@tonic-gate 						"nisplusObject",
1603*0Sstevel@tonic-gate 						t->objectDN->write.attrs);
1604*0Sstevel@tonic-gate 		if (rv == 0 || sv == 0 || val == 0 || attrName == 0 ||
1605*0Sstevel@tonic-gate 				attrName[0] == 0) {
1606*0Sstevel@tonic-gate 			sfree(tp);
1607*0Sstevel@tonic-gate 			sfree(buf);
1608*0Sstevel@tonic-gate 			sfree(rv);
1609*0Sstevel@tonic-gate 			sfree(sv);
1610*0Sstevel@tonic-gate 			sfree(val);
1611*0Sstevel@tonic-gate 			sfree(attrName);
1612*0Sstevel@tonic-gate 			return (LDAP_NO_MEMORY);
1613*0Sstevel@tonic-gate 		}
1614*0Sstevel@tonic-gate 
1615*0Sstevel@tonic-gate 		sv->length = osize;
1616*0Sstevel@tonic-gate 		sv->value = buf;
1617*0Sstevel@tonic-gate 
1618*0Sstevel@tonic-gate 		/* 'vt_ber' just means "not a NUL-terminated string" */
1619*0Sstevel@tonic-gate 		val->type = vt_ber;
1620*0Sstevel@tonic-gate 		val->repeat = 0;
1621*0Sstevel@tonic-gate 		val->numVals = 1;
1622*0Sstevel@tonic-gate 		val->val = sv;
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 		rv->numAttrs = 1;
1625*0Sstevel@tonic-gate 		rv->attrName = attrName;
1626*0Sstevel@tonic-gate 		rv->attrVal = val;
1627*0Sstevel@tonic-gate 
1628*0Sstevel@tonic-gate 		/*
1629*0Sstevel@tonic-gate 		 * The 'write.base' is the actual DN of the entry (and the
1630*0Sstevel@tonic-gate 		 * scope had better be 'base', but we don't check that).
1631*0Sstevel@tonic-gate 		 */
1632*0Sstevel@tonic-gate 		dn = t->objectDN->write.base;
1633*0Sstevel@tonic-gate 
1634*0Sstevel@tonic-gate 		stat = ldapModify(dn, rv, t->objectDN->write.attrs, 1);
1635*0Sstevel@tonic-gate 
1636*0Sstevel@tonic-gate 		freeRuleValue(rv, 1);
1637*0Sstevel@tonic-gate 
1638*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1639*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1640*0Sstevel@tonic-gate 		LOG_WARNING,
1641*0Sstevel@tonic-gate #else
1642*0Sstevel@tonic-gate 		LOG_INFO,
1643*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1644*0Sstevel@tonic-gate 		"%s: %s (%s)", myself, NIL(t->objName), ldap_err2string(stat));
1645*0Sstevel@tonic-gate 
1646*0Sstevel@tonic-gate 		if (stat != LDAP_SUCCESS)
1647*0Sstevel@tonic-gate 			break;
1648*0Sstevel@tonic-gate 
1649*0Sstevel@tonic-gate 	}
1650*0Sstevel@tonic-gate 
1651*0Sstevel@tonic-gate 	sfree(tp);
1652*0Sstevel@tonic-gate 
1653*0Sstevel@tonic-gate 	return (stat);
1654*0Sstevel@tonic-gate }
1655*0Sstevel@tonic-gate 
1656*0Sstevel@tonic-gate /*
1657*0Sstevel@tonic-gate  * Retrieve a copy of the 't->objName' object from LDAP, where it's
1658*0Sstevel@tonic-gate  * stored in XDR:ed form in the place indicated by 't->objectDN->read'.
1659*0Sstevel@tonic-gate  * Un-XDR the object, and return a pointer to it in '*obj'; it's the
1660*0Sstevel@tonic-gate  * responsibility of the caller to free the object when it's no
1661*0Sstevel@tonic-gate  * longer needed.
1662*0Sstevel@tonic-gate  *
1663*0Sstevel@tonic-gate  * Returns an appropriate LDAP status.
1664*0Sstevel@tonic-gate  */
1665*0Sstevel@tonic-gate int
1666*0Sstevel@tonic-gate objFromLDAP(__nis_table_mapping_t *t, nis_object **obj,
1667*0Sstevel@tonic-gate 		entry_obj ***eaP, int *numEaP) {
1668*0Sstevel@tonic-gate 	__nis_table_mapping_t	**tp;
1669*0Sstevel@tonic-gate 	XDR			xdr;
1670*0Sstevel@tonic-gate 	nis_object		*o;
1671*0Sstevel@tonic-gate 	__nis_rule_value_t	*rv;
1672*0Sstevel@tonic-gate 	__nis_ldap_search_t	*ls;
1673*0Sstevel@tonic-gate 	char			*attrs[2], *filter, **fc = 0;
1674*0Sstevel@tonic-gate 	void			*buf;
1675*0Sstevel@tonic-gate 	int			i, j, nfc, nrv, blen, stat = LDAP_SUCCESS;
1676*0Sstevel@tonic-gate 	int			n, numMatches;
1677*0Sstevel@tonic-gate 	char			*myself = "objFromLDAP";
1678*0Sstevel@tonic-gate 
1679*0Sstevel@tonic-gate 	if (t == 0)
1680*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate 	/*
1683*0Sstevel@tonic-gate 	 * If there's nowhere to store the result, we might as
1684*0Sstevel@tonic-gate 	 * well pretend all went well, and return right away.
1685*0Sstevel@tonic-gate 	 */
1686*0Sstevel@tonic-gate 	if (obj == 0)
1687*0Sstevel@tonic-gate 		return (LDAP_SUCCESS);
1688*0Sstevel@tonic-gate 
1689*0Sstevel@tonic-gate 	/* Prepare for the worst */
1690*0Sstevel@tonic-gate 	*obj = 0;
1691*0Sstevel@tonic-gate 
1692*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1693*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1694*0Sstevel@tonic-gate 		LOG_WARNING,
1695*0Sstevel@tonic-gate #else
1696*0Sstevel@tonic-gate 		LOG_INFO,
1697*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1698*0Sstevel@tonic-gate 		"%s: %s", myself, NIL(t->objName));
1699*0Sstevel@tonic-gate 
1700*0Sstevel@tonic-gate 	tp = selectTableMapping(t, 0, 0, 1, 0, &numMatches);
1701*0Sstevel@tonic-gate 	if (tp == 0 || numMatches <= 0) {
1702*0Sstevel@tonic-gate 		sfree(tp);
1703*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK,
1704*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1705*0Sstevel@tonic-gate 			LOG_WARNING,
1706*0Sstevel@tonic-gate #else
1707*0Sstevel@tonic-gate 			LOG_INFO,
1708*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1709*0Sstevel@tonic-gate 			"%s: %s (no mapping)", myself, NIL(t->objName));
1710*0Sstevel@tonic-gate 		return (LDAP_SUCCESS);
1711*0Sstevel@tonic-gate 	}
1712*0Sstevel@tonic-gate 
1713*0Sstevel@tonic-gate 	for (n = 0; n < numMatches; n++) {
1714*0Sstevel@tonic-gate 
1715*0Sstevel@tonic-gate 		t = tp[n];
1716*0Sstevel@tonic-gate 
1717*0Sstevel@tonic-gate 		filter = makeFilter(t->objectDN->read.attrs);
1718*0Sstevel@tonic-gate 		if (filter == 0 || (fc = makeFilterComp(filter, &nfc)) == 0 ||
1719*0Sstevel@tonic-gate 				nfc <= 0) {
1720*0Sstevel@tonic-gate 			sfree(tp);
1721*0Sstevel@tonic-gate 			sfree(filter);
1722*0Sstevel@tonic-gate 			freeFilterComp(fc, nfc);
1723*0Sstevel@tonic-gate 			return ((t->objectDN->read.attrs != 0) ?
1724*0Sstevel@tonic-gate 				LDAP_NO_MEMORY : LDAP_PARAM_ERROR);
1725*0Sstevel@tonic-gate 		}
1726*0Sstevel@tonic-gate 		/* Don't need the filter, just the components */
1727*0Sstevel@tonic-gate 		sfree(filter);
1728*0Sstevel@tonic-gate 
1729*0Sstevel@tonic-gate 		/*
1730*0Sstevel@tonic-gate 		 * Look for a "nisplusObject" attribute, and (if found) copy
1731*0Sstevel@tonic-gate 		 * the value to attrs[0]. Also remove the "nisplusObject"
1732*0Sstevel@tonic-gate 		 * attribute and value from the filter components.
1733*0Sstevel@tonic-gate 		 */
1734*0Sstevel@tonic-gate 		attrs[0] = sdup(myself, T, "nisplusObject");
1735*0Sstevel@tonic-gate 		if (attrs[0] == 0) {
1736*0Sstevel@tonic-gate 			sfree(tp);
1737*0Sstevel@tonic-gate 			freeFilterComp(fc, nfc);
1738*0Sstevel@tonic-gate 			return (LDAP_NO_MEMORY);
1739*0Sstevel@tonic-gate 		}
1740*0Sstevel@tonic-gate 		attrs[1] = 0;
1741*0Sstevel@tonic-gate 		for (i = 0; i < nfc; i++) {
1742*0Sstevel@tonic-gate 			char	*name, *value;
1743*0Sstevel@tonic-gate 			int	compare;
1744*0Sstevel@tonic-gate 
1745*0Sstevel@tonic-gate 			name = fc[i];
1746*0Sstevel@tonic-gate 			/* Skip if not of attr=value form */
1747*0Sstevel@tonic-gate 			if ((value = strchr(name, '=')) == 0)
1748*0Sstevel@tonic-gate 				continue;
1749*0Sstevel@tonic-gate 
1750*0Sstevel@tonic-gate 			/* Temporarily overWrite the '=' with a '\0' */
1751*0Sstevel@tonic-gate 			*value = '\0';
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate 			/* Compare with our target attribute name */
1754*0Sstevel@tonic-gate 			compare = strcasecmp("nisplusObject", name);
1755*0Sstevel@tonic-gate 
1756*0Sstevel@tonic-gate 			/* Put back the '=' */
1757*0Sstevel@tonic-gate 			*value = '=';
1758*0Sstevel@tonic-gate 
1759*0Sstevel@tonic-gate 			/* Is it the name we're looking for ? */
1760*0Sstevel@tonic-gate 			if (compare == 0) {
1761*0Sstevel@tonic-gate 				sfree(attrs[0]);
1762*0Sstevel@tonic-gate 				attrs[0] = sdup(myself, T, value+1);
1763*0Sstevel@tonic-gate 				if (attrs[0] == 0) {
1764*0Sstevel@tonic-gate 					sfree(tp);
1765*0Sstevel@tonic-gate 					freeFilterComp(fc, nfc);
1766*0Sstevel@tonic-gate 					return (LDAP_NO_MEMORY);
1767*0Sstevel@tonic-gate 				}
1768*0Sstevel@tonic-gate 				sfree(fc[i]);
1769*0Sstevel@tonic-gate 				if (i < nfc-1)
1770*0Sstevel@tonic-gate 					(void) memmove(&fc[i], &fc[i+1],
1771*0Sstevel@tonic-gate 						(nfc-1-i) * sizeof (fc[i]));
1772*0Sstevel@tonic-gate 				nfc--;
1773*0Sstevel@tonic-gate 				break;
1774*0Sstevel@tonic-gate 			}
1775*0Sstevel@tonic-gate 		}
1776*0Sstevel@tonic-gate 
1777*0Sstevel@tonic-gate 		ls = buildLdapSearch(t->objectDN->read.base,
1778*0Sstevel@tonic-gate 					t->objectDN->read.scope,
1779*0Sstevel@tonic-gate 					nfc, fc, 0, attrs, 0, 1);
1780*0Sstevel@tonic-gate 		sfree(attrs[0]);
1781*0Sstevel@tonic-gate 		freeFilterComp(fc, nfc);
1782*0Sstevel@tonic-gate 		if (ls == 0) {
1783*0Sstevel@tonic-gate 			sfree(tp);
1784*0Sstevel@tonic-gate 			return (LDAP_OPERATIONS_ERROR);
1785*0Sstevel@tonic-gate 		}
1786*0Sstevel@tonic-gate 
1787*0Sstevel@tonic-gate 		nrv = 0;
1788*0Sstevel@tonic-gate 		rv = ldapSearch(ls, &nrv, 0, &stat);
1789*0Sstevel@tonic-gate 		if (rv == 0) {
1790*0Sstevel@tonic-gate 			sfree(tp);
1791*0Sstevel@tonic-gate 			freeLdapSearch(ls);
1792*0Sstevel@tonic-gate 			return (stat);
1793*0Sstevel@tonic-gate 		}
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate 		for (i = 0, buf = 0; i < nrv && buf == 0; i++) {
1796*0Sstevel@tonic-gate 			for (j = 0; j < rv[i].numAttrs; j++) {
1797*0Sstevel@tonic-gate 				if (strcasecmp(ls->attrs[0],
1798*0Sstevel@tonic-gate 					rv[i].attrName[j]) == 0) {
1799*0Sstevel@tonic-gate 					if (rv[i].attrVal[j].numVals <= 0)
1800*0Sstevel@tonic-gate 						continue;
1801*0Sstevel@tonic-gate 					buf = rv[i].attrVal[j].val[0].value;
1802*0Sstevel@tonic-gate 					blen = rv[i].attrVal[j].val[0].length;
1803*0Sstevel@tonic-gate 					break;
1804*0Sstevel@tonic-gate 				}
1805*0Sstevel@tonic-gate 			}
1806*0Sstevel@tonic-gate 		}
1807*0Sstevel@tonic-gate 
1808*0Sstevel@tonic-gate 		if (buf != 0) {
1809*0Sstevel@tonic-gate 			o = unXdrNisObject(buf, blen, eaP, numEaP);
1810*0Sstevel@tonic-gate 			if (o == 0) {
1811*0Sstevel@tonic-gate 				sfree(tp);
1812*0Sstevel@tonic-gate 				freeLdapSearch(ls);
1813*0Sstevel@tonic-gate 				freeRuleValue(rv, nrv);
1814*0Sstevel@tonic-gate 				return (LDAP_OPERATIONS_ERROR);
1815*0Sstevel@tonic-gate 			}
1816*0Sstevel@tonic-gate 			stat = LDAP_SUCCESS;
1817*0Sstevel@tonic-gate 			*obj = o;
1818*0Sstevel@tonic-gate 		} else {
1819*0Sstevel@tonic-gate 			stat = LDAP_NO_SUCH_OBJECT;
1820*0Sstevel@tonic-gate 		}
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate 		freeLdapSearch(ls);
1823*0Sstevel@tonic-gate 		freeRuleValue(rv, nrv);
1824*0Sstevel@tonic-gate 
1825*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1826*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1827*0Sstevel@tonic-gate 		LOG_WARNING,
1828*0Sstevel@tonic-gate #else
1829*0Sstevel@tonic-gate 		LOG_INFO,
1830*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1831*0Sstevel@tonic-gate 		"%s: %s (%s)", myself, NIL(t->objName), ldap_err2string(stat));
1832*0Sstevel@tonic-gate 
1833*0Sstevel@tonic-gate 		if (stat != LDAP_SUCCESS)
1834*0Sstevel@tonic-gate 			break;
1835*0Sstevel@tonic-gate 
1836*0Sstevel@tonic-gate 	}
1837*0Sstevel@tonic-gate 
1838*0Sstevel@tonic-gate 	sfree(tp);
1839*0Sstevel@tonic-gate 
1840*0Sstevel@tonic-gate 	return (stat);
1841*0Sstevel@tonic-gate }
1842*0Sstevel@tonic-gate 
1843*0Sstevel@tonic-gate int
1844*0Sstevel@tonic-gate deleteLDAPobj(__nis_table_mapping_t *t) {
1845*0Sstevel@tonic-gate 	__nis_table_mapping_t	**tp;
1846*0Sstevel@tonic-gate 	int		n, stat, numMatches = 0;
1847*0Sstevel@tonic-gate 	char		*myself = "deleteLDAPobj";
1848*0Sstevel@tonic-gate 
1849*0Sstevel@tonic-gate 	if (t == 0)
1850*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1851*0Sstevel@tonic-gate 
1852*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1853*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1854*0Sstevel@tonic-gate 		LOG_WARNING,
1855*0Sstevel@tonic-gate #else
1856*0Sstevel@tonic-gate 		LOG_INFO,
1857*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1858*0Sstevel@tonic-gate 		"%s: %s", myself, NIL(t->objName));
1859*0Sstevel@tonic-gate 
1860*0Sstevel@tonic-gate 	tp = selectTableMapping(t, 0, 1, 1, 0, &numMatches);
1861*0Sstevel@tonic-gate 	if (tp == 0 || numMatches <= 0) {
1862*0Sstevel@tonic-gate 		sfree(tp);
1863*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK,
1864*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1865*0Sstevel@tonic-gate 			LOG_WARNING,
1866*0Sstevel@tonic-gate #else
1867*0Sstevel@tonic-gate 			LOG_INFO,
1868*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1869*0Sstevel@tonic-gate 			"%s: %s (no mapping)", myself, NIL(t->objName));
1870*0Sstevel@tonic-gate 		return (LDAP_SUCCESS);
1871*0Sstevel@tonic-gate 	}
1872*0Sstevel@tonic-gate 
1873*0Sstevel@tonic-gate 	for (n = 0; n < numMatches; n++) {
1874*0Sstevel@tonic-gate 
1875*0Sstevel@tonic-gate 		t = tp[n];
1876*0Sstevel@tonic-gate 
1877*0Sstevel@tonic-gate 		if (t->objectDN->delDisp == dd_always) {
1878*0Sstevel@tonic-gate 			/* Delete entire entry */
1879*0Sstevel@tonic-gate 			stat = ldapModify(t->objectDN->write.base, 0,
1880*0Sstevel@tonic-gate 					t->objectDN->write.attrs, 1);
1881*0Sstevel@tonic-gate 		} else if (t->objectDN->delDisp == dd_perDbId) {
1882*0Sstevel@tonic-gate 			/*
1883*0Sstevel@tonic-gate 			 * Delete the attribute holding the object.
1884*0Sstevel@tonic-gate 			 * First, determine what that attribute is called.
1885*0Sstevel@tonic-gate 			 */
1886*0Sstevel@tonic-gate 			char			*attrName =
1887*0Sstevel@tonic-gate 						attrVal(myself,
1888*0Sstevel@tonic-gate 							"nisplusObject",
1889*0Sstevel@tonic-gate 							"nisplusObject",
1890*0Sstevel@tonic-gate 						t->objectDN->write.attrs);
1891*0Sstevel@tonic-gate 			__nis_rule_value_t	rv;
1892*0Sstevel@tonic-gate 			__nis_value_t		val;
1893*0Sstevel@tonic-gate 
1894*0Sstevel@tonic-gate 			if (attrName == 0) {
1895*0Sstevel@tonic-gate 				sfree(tp);
1896*0Sstevel@tonic-gate 				return (LDAP_NO_MEMORY);
1897*0Sstevel@tonic-gate 			}
1898*0Sstevel@tonic-gate 
1899*0Sstevel@tonic-gate 			/*
1900*0Sstevel@tonic-gate 			 * Build a __nis_value_t with 'numVals' < 0 to
1901*0Sstevel@tonic-gate 			 * indicate deletion.
1902*0Sstevel@tonic-gate 			 */
1903*0Sstevel@tonic-gate 			val.type = vt_ber;
1904*0Sstevel@tonic-gate 			val.numVals = -1;
1905*0Sstevel@tonic-gate 			val.val = 0;
1906*0Sstevel@tonic-gate 
1907*0Sstevel@tonic-gate 			/*
1908*0Sstevel@tonic-gate 			 * Build a rule-value with the name we determined
1909*0Sstevel@tonic-gate 			 * above, and the deletion value.
1910*0Sstevel@tonic-gate 			 */
1911*0Sstevel@tonic-gate 			(void) memset(&rv, 0, sizeof (rv));
1912*0Sstevel@tonic-gate 			rv.numAttrs = 1;
1913*0Sstevel@tonic-gate 			rv.attrName = &attrName;
1914*0Sstevel@tonic-gate 			rv.attrVal = &val;
1915*0Sstevel@tonic-gate 
1916*0Sstevel@tonic-gate 			stat = ldapModify(t->objectDN->write.base, &rv,
1917*0Sstevel@tonic-gate 						t->objectDN->write.attrs, 0);
1918*0Sstevel@tonic-gate 
1919*0Sstevel@tonic-gate 			sfree(attrName);
1920*0Sstevel@tonic-gate 		} else if (t->objectDN->delDisp == dd_never) {
1921*0Sstevel@tonic-gate 			/* Nothing to do, so we're trivially successful */
1922*0Sstevel@tonic-gate 			stat = LDAP_SUCCESS;
1923*0Sstevel@tonic-gate 		} else {
1924*0Sstevel@tonic-gate 			stat = LDAP_PARAM_ERROR;
1925*0Sstevel@tonic-gate 		}
1926*0Sstevel@tonic-gate 
1927*0Sstevel@tonic-gate 	logmsg(MSG_NOTIMECHECK,
1928*0Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
1929*0Sstevel@tonic-gate 		LOG_WARNING,
1930*0Sstevel@tonic-gate #else
1931*0Sstevel@tonic-gate 		LOG_INFO,
1932*0Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
1933*0Sstevel@tonic-gate 		"%s: %s (%s)", myself, NIL(t->objName), ldap_err2string(stat));
1934*0Sstevel@tonic-gate 
1935*0Sstevel@tonic-gate 		/* If there were no such object, we've trivially succeeded */
1936*0Sstevel@tonic-gate 		if (stat == LDAP_NO_SUCH_OBJECT)
1937*0Sstevel@tonic-gate 			stat = LDAP_SUCCESS;
1938*0Sstevel@tonic-gate 
1939*0Sstevel@tonic-gate 		if (stat != LDAP_SUCCESS)
1940*0Sstevel@tonic-gate 			break;
1941*0Sstevel@tonic-gate 
1942*0Sstevel@tonic-gate 	}
1943*0Sstevel@tonic-gate 
1944*0Sstevel@tonic-gate 	sfree(tp);
1945*0Sstevel@tonic-gate 
1946*0Sstevel@tonic-gate 	return (stat);
1947*0Sstevel@tonic-gate }
1948