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