xref: /titanic_41/usr/src/lib/libnisdb/db_mindex3.cc (revision 6f45ec7b0b964c3be967c4880e8867ac1e7763a5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <time.h>
31 #include <sys/time.h>
32 #include <lber.h>
33 #include <ldap.h>
34 #include <signal.h>
35 #include <pthread.h>
36 #include "db_headers.h"
37 #include "db.h"
38 #include "db_mindex.h"
39 #include "db_dictionary.h"
40 #include "nisdb_mt.h"
41 #include "ldap_map.h"
42 #include "ldap_glob.h"
43 #include "ldap_nisplus.h"
44 #include "ldap_util.h"
45 
46 
47 extern db_dictionary	*InUseDictionary;
48 
49 
50 extern "C" {
51 
52 typedef struct {
53 	db_mindex		*mindex;
54 	__nis_table_mapping_t	*t;
55 	db_query		*qin;
56 	db_query		*q;
57 	char			*dbId;
58 	nis_object		*dirObj;
59 	int			isDeferred;
60 	char			*tableName;
61 } __entries_from_ldap_arg_t;
62 
63 static void	*entriesFromLDAPthread(void *);
64 
65 }
66 
67 int		entriesFromLDAPreal(__entries_from_ldap_arg_t *);
68 
69 #ifdef	SET_ENTRY_FLAGS
70 static uint_t
71 entryFlagsFromTable(uint_t tf) {
72 	uint_t	ef = 0;
73 
74 	if ((tf & TA_BINARY) != 0)
75 		ef |= EN_BINARY;
76 	if ((tf & TA_CRYPT) != 0)
77 		ef |= EN_CRYPT;
78 	if ((tf & TA_XDR) != 0)
79 		ef |= EN_XDR;
80 	if ((tf & TA_ASN1) != 0)
81 		ef |= EN_ASN1;
82 
83 	return (ef);
84 }
85 #endif	/* SET_ENTRY_FLAGS */
86 
87 /*
88  * Retrieve container entries from LDAP per 't' and 'qin'/'q'.
89  * This is a helper function for db_mindex::queryLDAP(); see
90  * that function for details of the parameters (except doAsynch).
91  *
92  * If 'doAsynch' is set, and the retrieval is an enumeration
93  * (qin == NULL), the retrieval is performed in a detached
94  * thread. In this case, the return code just reflects the
95  * setup and launch of the detached thread. Retrieval will
96  * complete asynchronously.
97  */
98 int
99 db_mindex::entriesFromLDAP(__nis_table_mapping_t *t, db_query *qin, db_query *q,
100 			char *dbId, nis_object *dirObj, int doAsynch) {
101 	__entries_from_ldap_arg_t	*arg;
102 	int				stat;
103 	db_status			dstat;
104 	char				*myself = "db_mindex::entriesFromLDAP";
105 
106 	arg = (__entries_from_ldap_arg_t *)am(myself, sizeof (*arg));
107 	if (arg == 0) {
108 		freeQuery(q);
109 		if (dirObj != 0)
110 			nis_destroy_object(dirObj);
111 		return (LDAP_NO_MEMORY);
112 	}
113 
114 	arg->mindex = this;
115 	arg->t = t;
116 	arg->qin = qin;
117 	arg->q = q;
118 	arg->dbId = dbId;
119 	arg->dirObj = dirObj;
120 	arg->tableName = t->objName;
121 
122 	/*
123 	 * Check if an enumeration thread is running; if so, then regardless
124 	 * of whether or not the current operation is an enumeration, we
125 	 * just return success, and let our caller get the data from the
126 	 * existing (deferred) DB.
127 	 */
128 	(void) mutex_lock(&table->mapping.enumLock);
129 	if (table->mapping.enumTid != 0) {
130 		int	doReturn = 0;
131 
132 		stat = pthread_kill(table->mapping.enumTid, 0);
133 		if (stat == ESRCH) {
134 			logmsg(MSG_NOTIMECHECK, LOG_WARNING,
135 	"%s: Enumeration thread %d not found for \"%s\"; exit status = %d (%s)",
136 				myself, table->mapping.enumTid,
137 				NIL(t->objName), table->mapping.enumStat,
138 				ldap_err2string(table->mapping.enumStat));
139 			/* Reflect the fact that no enum thread is running */
140 			table->mapping.enumTid = 0;
141 			table->mapping.enumStat = -1;
142 			/* Cleanup deferred mode */
143 			if (table->mapping.enumDeferred) {
144 				dstat = InUseDictionary->commit(t->objPath);
145 				if (dstat == DB_SUCCESS) {
146 					table->mapping.enumDeferred = 0;
147 				} else {
148 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
149 					"%s: DB error %d committing \"%s\"",
150 						myself, dstat, NIL(t->objName));
151 				}
152 			}
153 		} else if (stat == 0) {
154 			logmsg(MSG_NOTIMECHECK, LOG_INFO,
155 			"%s: Enumeration thread %d already running for \"%s\"",
156 				myself, table->mapping.enumTid,
157 				NIL(t->objName));
158 			stat = LDAP_SUCCESS;
159 			doReturn = 1;
160 		} else {
161 			logmsg(MSG_NOTIMECHECK, LOG_INFO,
162 		"%s: Error %d looking for enumeration thread %d for \"%s\"",
163 				myself, stat, table->mapping.enumTid,
164 				NIL(t->objName));
165 			doReturn = 1;
166 			stat = LDAP_OPERATIONS_ERROR;
167 		}
168 		if (doReturn) {
169 			(void) mutex_unlock(&table->mapping.enumLock);
170 			sfree(arg);
171 			freeQuery(q);
172 			if (dirObj != 0)
173 				nis_destroy_object(dirObj);
174 			return (stat);
175 		}
176 	}
177 
178 	/*
179 	 * If we're enumerating (and hence expect that retrieving all data,
180 	 * and updating the local DB, might take a while), create a deferred-
181 	 * update table that clients can use while we are updating the real
182 	 * one.
183 	 */
184 	if (doAsynch && qin == 0) {
185 		if ((dstat = InUseDictionary->defer(t->objPath)) ==
186 				DB_SUCCESS) {
187 			arg->isDeferred = 1;
188 			table->mapping.enumDeferred = 1;
189 		} else {
190 			logmsg(MSG_NOTIMECHECK, LOG_WARNING,
191 			"%s: Unable to defer updates for \"%s\" (status=%d);"
192 					" updating in place",
193 				myself, NIL(t->objName), dstat);
194 			arg->isDeferred = 0;
195 			table->mapping.enumDeferred = 0;
196 		}
197 	} else {
198 		arg->isDeferred = 0;
199 		table->mapping.enumDeferred = 0;
200 	}
201 
202 	/* If enumerating, perform the operation in a separate thread */
203 	if (doAsynch && qin == 0) {
204 		pthread_t	tid;
205 		pthread_attr_t	attr;
206 
207 		(void) pthread_attr_init(&attr);
208 #ifdef	FORCE_SYNCHRONOUS
209 #else
210 		(void) pthread_attr_setdetachstate(&attr,
211 						PTHREAD_CREATE_DETACHED);
212 #endif	/* FORCE_SYNCHRONOUS */
213 		stat = pthread_create(&tid, &attr, entriesFromLDAPthread, arg);
214 		if (stat != 0) {
215 			(void) mutex_unlock(&table->mapping.enumLock);
216 			logmsg(MSG_NOTIMECHECK, LOG_WARNING,
217 			"%s: Error %d creating new thread; using current one",
218 				myself, stat);
219 			stat = (int)entriesFromLDAPthread(arg);
220 			return (stat);
221 		}
222 
223 		table->mapping.enumTid = tid;
224 		table->mapping.enumStat = -1;
225 
226 		/*
227 		 * We're now returning to the caller, who will get data
228 		 * from:
229 		 *
230 		 *	The deferred DB, if an enumeration thread already
231 		 *	was running, and deferred mode was on, or
232 		 *
233 		 *	The original DB, if we just started an enumeration
234 		 *	thread. In this case, our caller (several levels up)
235 		 *	is holding a lock on the db_mindex/db_table, which
236 		 *	means that the enum thread will have to wait for
237 		 *	our caller once it's done the LDAP retrieval, and
238 		 *	wants to update the DB.
239 		 */
240 		(void) mutex_unlock(&table->mapping.enumLock);
241 		stat = LDAP_SUCCESS;
242 #ifdef	FORCE_SYNCHRONOUS
243 		{
244 			int	tstat;
245 
246 			stat = pthread_join(tid, (void **)&tstat);
247 			if (stat == 0) {
248 				stat = tstat;
249 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
250 					"%s: thread %d => %d",
251 					myself, tid, tstat);
252 			} else {
253 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
254 					"%s: pthread_join(%d) => %d",
255 					myself, tid, stat);
256 				stat = LDAP_OPERATIONS_ERROR;
257 			}
258 		}
259 #endif	/* FORCE_SYNCHRONOUS */
260 	} else {
261 		(void) mutex_unlock(&table->mapping.enumLock);
262 		stat = (int)entriesFromLDAPthread(arg);
263 	}
264 
265 	return (stat);
266 }
267 
268 extern "C" {
269 
270 /*
271  * We use this 'extern "C"' function in order to make sure that
272  * pthread_create() doesn't have any problems trying to invoke a
273  * C++ function.
274  */
275 static void *
276 entriesFromLDAPthread(void *voidarg) {
277 	__entries_from_ldap_arg_t	*arg;
278 	int				stat;
279 	db				*dbase;
280 	db_table_desc			*tbl = 0;
281 	char				*tableName;
282 
283 	arg = (__entries_from_ldap_arg_t *)voidarg;
284 
285 	/* Lock to prevent removal */
286 	(void) __nis_lock_db_table(arg->tableName, 1, 0,
287 					"entriesFromLDAPthread");
288 
289 	/*
290 	 * It's possible that the db_mindex for the table has changed,
291 	 * or disappeared, between now and the time when our parent
292 	 * thread released its lock on the table. Hence, we search the
293 	 * dictionary to re-acquire the 'db', and the db_mindex.
294 	 */
295 	tableName = internalTableName(arg->tableName);
296 	if (tableName != 0) {
297 #ifdef	NISDB_LDAP_DEBUG
298 		db_mindex	*oldMindex = arg->mindex;
299 #endif	/* NISDB_LDAP_DEBUG */
300 
301 		dbase = InUseDictionary->find_table(tableName, &tbl, FALSE);
302 		if (dbase != 0)
303 			arg->mindex = dbase->mindex();
304 		else
305 			arg->mindex = 0;
306 #ifdef	NISDB_LDAP_DEBUG
307 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
308 			"entriesFromLDAPthread: %s -> %s -> 0x%x (0x%x)",
309 			NIL(arg->tableName), NIL(tableName),
310 			arg->mindex, oldMindex);
311 #endif	/* NISDB_LDAP_DEBUG */
312 		sfree(tableName);
313 		tableName = 0;
314 	}
315 
316 	stat = entriesFromLDAPreal(arg);
317 
318 	(void) __nis_ulock_db_table(arg->tableName, 1, 0,
319 					"entriesFromLDAPthread");
320 
321 	freeQuery(arg->q);
322 	if (arg->dirObj != 0)
323 		nis_destroy_object(arg->dirObj);
324 	sfree(arg);
325 	return ((void *)stat);
326 }
327 
328 }
329 
330 int
331 entriesFromLDAPreal(__entries_from_ldap_arg_t *arg) {
332 	db_mindex			*mindex;
333 	db_table			*table;
334 	__nis_table_mapping_t		*t;
335 	db_query			*q, *qin;
336 	char				*dbId;
337 	nis_object			*dirObj;
338 	int				i, na, nau, nq = 0, xid = 0;
339 	int				ret, stat = LDAP_SUCCESS, stat2, stat3;
340 	int				lstat;
341 	__nis_obj_attr_t		**oa = 0;
342 	db_query			**res;
343 	entry_object			**ea;
344 	long				numEa;
345 	bool_t				doEnum;
346 	db_status			dstat;
347 	struct timeval			start;
348 	char				*myself =
349 					"db_mindex::entriesFromLDAPreal";
350 
351 	if (arg == 0)
352 		return (LDAP_PARAM_ERROR);
353 	mindex = arg->mindex;
354 	t = arg->t;
355 	q = arg->q;
356 	qin = arg->qin;
357 	dbId = arg->dbId;
358 	dirObj = arg->dirObj;
359 
360 	table = (mindex != 0) ? mindex->getTable() : 0;
361 
362 	if (mindex == 0 || t == 0 || table == 0) {
363 		/* We haven't done anything, so rollback should be OK */
364 		if (arg->isDeferred && t != 0) {
365 			dstat = InUseDictionary->rollback(t->objPath);
366 			if (dstat != DB_SUCCESS) {
367 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
368 				"%s: DB error %d rolling back \"%s\"",
369 					myself, dstat, NIL(t->objName));
370 				/*
371 				 * Had rollback succeeded, the 'table'
372 				 * would have disappeared. However, since
373 				 * rollback failed, we need to update the
374 				 * table->mapping.enum* fields.
375 				 */
376 				if (table != 0) {
377 					(void) mutex_lock(&table->
378 							mapping.enumLock);
379 					table->mapping.enumStat =
380 							LDAP_PARAM_ERROR;
381 					table->mapping.enumTime = 0;
382 					table->mapping.enumEntries = 0;
383 					table->mapping.enumTid = 0;
384 					(void) mutex_unlock(&table->
385 							mapping.enumLock);
386 				}
387 			}
388 		}
389 		return (LDAP_PARAM_ERROR);
390 	}
391 
392 	if (qin == 0)
393 		logmsg(MSG_NOTIMECHECK, LOG_INFO, "%s: enumerating \"%s%s%s\"",
394 			myself, dbId ? dbId : "", dbId ? ":" : "",
395 			NIL(t->objName));
396 
397 	(void) gettimeofday(&start, 0);
398 
399 	/* Getting table entries */
400 	res = mapFromLDAP(t, q, &nq, dbId, &stat, &oa);
401 #ifdef	NISDB_LDAP_DEBUG
402 	logmsg(MSG_ALWAYS, LOG_INFO,
403 		"%s: mapFromLDAP() => 0x%x, status=%d %s; nq = %d",
404 		myself, res, stat, stat == LDAP_SUCCESS ? "" :
405 		ldap_err2string(stat), nq);
406 #endif	/* NISDB_LDAP_DEBUG */
407 
408 	/*
409 	 * Keep track of the number of NIS+ entries we got back;
410 	 * note that the number of LDAP entries may have been
411 	 * smaller or larger.
412 	 */
413 	(void) mutex_lock(&table->mapping.enumLock);
414 	table->mapping.enumEntries = nq;
415 	(void) mutex_unlock(&table->mapping.enumLock);
416 
417 	/*
418 	 * If we get LDAP_NO_SUCH_OBJECT, we need to delete the entries
419 	 * in the table, so we can't just return.
420 	 */
421 	if (res == 0 && stat != LDAP_NO_SUCH_OBJECT) {
422 		logmsg(MSG_NOTIMECHECK, LOG_INFO,
423 			"%s: mapFromLDAP() => 0x0, status=%d (%s)",
424 			myself, stat, ldap_err2string(stat));
425 		if (arg->isDeferred) {
426 			dstat = InUseDictionary->rollback(t->objPath);
427 			if (dstat != DB_SUCCESS) {
428 				struct timeval	end;
429 
430 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
431 				"%s: DB error %d rolling back \"%s\"",
432 					myself, dstat, NIL(t->objName));
433 				/*
434 				 * Had rollback succeeded, the 'table'
435 				 * would have disappeared. However, since
436 				 * rollback failed, we need to update the
437 				 * table->mapping.enum* fields.
438 				 */
439 				(void) mutex_lock(&table->mapping.enumLock);
440 				table->mapping.enumStat = stat;
441 				(void) gettimeofday(&end, 0);
442 				end.tv_sec -= start.tv_sec;
443 				end.tv_usec -= start.tv_usec;
444 				if (end.tv_usec < 0) {
445 					end.tv_usec += 1000000;
446 					end.tv_sec -= 1;
447 				}
448 				table->mapping.enumTime =
449 					1000000*end.tv_sec + end.tv_usec;
450 				table->mapping.enumTid = 0;
451 				(void) mutex_unlock(&table->mapping.enumLock);
452 			}
453 		}
454 		return (stat);
455 	}
456 
457 	/*
458 	 * Need to disable write-through to LDAP, for which we need a lock
459 	 * on our db_mindex ('mindex'); we're also updating the table, so
460 	 * we need a write lock on that as well. However, before locking the
461 	 * mindex, we need to maintain lock integrity by acquiring the
462 	 * trans log lock. Note that actually beginning a transaction is
463 	 * expensive, so we defer that until we know that we really need
464 	 * to update.
465 	 */
466 	lstat = lockTransLog(myself, 1, 1);
467 	if (lstat != 0) {
468 		if (lstat == EBUSY)
469 			logmsg(MSG_NOTIMECHECK, LOG_INFO,
470 		"%s: transaction log busy; no LDAP update for \"%s\"",
471 				myself, NIL(t->objName));
472 		else
473 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
474 	"%s: Error %d locking transaction log; no LDAP update for \"%s\"",
475 				myself, lstat, NIL(t->objName));
476 		if (arg->isDeferred) {
477 			dstat = InUseDictionary->rollback(t->objPath);
478 			if (dstat != DB_SUCCESS) {
479 				struct timeval	end;
480 
481 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
482 				"%s: DB error %d rolling back \"%s\"",
483 					myself, dstat, NIL(t->objName));
484 				/*
485 				 * Had rollback succeeded, the 'table'
486 				 * would have disappeared. However, since
487 				 * rollback failed, we need to update the
488 				 * table->mapping.enum* fields.
489 				 */
490 				(void) mutex_lock(&table->mapping.enumLock);
491 				table->mapping.enumStat = LDAP_OPERATIONS_ERROR;
492 				(void) gettimeofday(&end, 0);
493 				end.tv_sec -= start.tv_sec;
494 				end.tv_usec -= start.tv_usec;
495 				if (end.tv_usec < 0) {
496 					end.tv_usec += 1000000;
497 					end.tv_sec -= 1;
498 				}
499 				table->mapping.enumTime = 1000000*end.tv_sec +
500 					end.tv_usec;
501 				table->mapping.enumTid = 0;
502 				(void) mutex_unlock(&table->mapping.enumLock);
503 			}
504 		}
505 		return (LDAP_OPERATIONS_ERROR);
506 	}
507 
508 	/*
509 	 * If we have any updates, we'll call db::sync_log, which write-
510 	 * locks the 'db' instance. In order to avoid a dead-lock with
511 	 * threads performing a DB lookup (which will lock the 'db' and
512 	 * then the 'db_mindex'), we need hence need to lock in the
513 	 * following order:
514 	 *
515 	 *	trans.log	(already holding that one)
516 	 *	db
517 	 *	db_mindex
518 	 *	db_table
519 	 */
520 	TRYWRITELOCK(((db *)mindex->getDbPtr()), stat,
521 		"w db db_mindex::entriesFromLDAPreal");
522 	if (stat == 0) {
523 		TRYWRITELOCK(mindex, stat2, "w db_mindex::entriesFromLDAPreal");
524 		if (stat2 == 0) {
525 			TRYWRITELOCK(table, stat3,
526 				"table w db_mindex::entriesFromLDAPreal");
527 		}
528 	}
529 
530 	if (stat != 0 || stat2 != 0 || stat3 != 0) {
531 		if (stat != 0) {
532 			if (stat == EBUSY)
533 				logmsg(MSG_NOTIMECHECK, LOG_INFO,
534 				"%s: 'db' busy; no LDAP update for \"%s\"",
535 					myself, NIL(t->objName));
536 			else
537 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
538 			"%s: 'db' lock error %d; no LDAP update for \"%s\"",
539 					myself, stat, NIL(t->objName));
540 		} else if (stat2 != 0) {
541 			if (stat2 == EBUSY)
542 				logmsg(MSG_NOTIMECHECK, LOG_INFO,
543 			"%s: 'db_mindex' busy; no LDAP update for \"%s\"",
544 					myself, NIL(t->objName));
545 			else
546 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
547 		"%s: 'db_mindex' lock error %d; no LDAP update for \"%s\"",
548 					myself, stat2, NIL(t->objName));
549 		} else {
550 			if (stat3 == EBUSY)
551 				logmsg(MSG_NOTIMECHECK, LOG_INFO,
552 			"%s: 'db_table' busy; no LDAP update for \"%s\"",
553 					myself, NIL(t->objName));
554 			else
555 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
556 		"%s: 'db_table' lock error %d; no LDAP update for \"%s\"",
557 					myself, stat3, NIL(t->objName));
558 		}
559 		freeQueries(res, nq);
560 		if (arg->isDeferred) {
561 			dstat = InUseDictionary->rollback(t->objPath);
562 			if (dstat != DB_SUCCESS) {
563 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
564 				"%s: DB error %d rolling back \"%s\"",
565 					myself, dstat, NIL(t->objName));
566 				/*
567 				 * Had rollback succeeded, the 'table'
568 				 * would have disappeared. However, since
569 				 * rollback failed, we need to update the
570 				 * table->mapping.enum* fields.
571 				 */
572 				(void) mutex_lock(&table->mapping.enumLock);
573 				table->mapping.enumStat = LDAP_OPERATIONS_ERROR;
574 				table->mapping.enumTid = 0;
575 				(void) mutex_unlock(&table->mapping.enumLock);
576 			}
577 		}
578 		if (stat == 0) {
579 			if (stat2 == 0) {
580 				WRITEUNLOCK2(mindex, ((db *)mindex->getDbPtr()),
581 					LDAP_OPERATIONS_ERROR,
582 					LDAP_OPERATIONS_ERROR,
583 					"db_mindex::entriesFromLDAPreal wu",
584 					"db_mindex::entriesFromLDAPreal wu db");
585 			} else {
586 				WRITEUNLOCK(((db *)mindex->getDbPtr()),
587 					LDAP_OPERATIONS_ERROR,
588 					"db_mindex::entriesFromLDAPreal wu db");
589 			}
590 		}
591 		unlockTransLog(myself, 1);
592 		return (LDAP_OPERATIONS_ERROR);
593 	}
594 
595 	stat = LDAP_SUCCESS;
596 	mindex->setNoWriteThrough();
597 	mindex->setNoLDAPquery();
598 	if (qin == 0) {
599 		table->setEnumMode(0);
600 		doEnum = TRUE;
601 
602 		/*
603 		 * If there is no non-indexed table mapping, we must filter
604 		 * the enum mode (i.e., deletion candidates) array to only
605 		 * contain those entries that match the indexes.
606 		 */
607 		if (haveIndexedMapping(t)) {
608 			entry_object	**tea = table->gettab();
609 			long		i, ntea = table->getsize();
610 
611 
612 			/*
613 			 * Walk through the entry array, and remove any enum
614 			 * array entry that _doesn't_ match the index(es).
615 			 */
616 			for (i = 0; i < ntea; i++) {
617 				db_query		*q;
618 				__nis_table_mapping_t	**tp;
619 				int			numMatches;
620 
621 				if (tea[i] == 0)
622 					continue;
623 
624 				q = pseudoEntryObj2Query(tea[i], 0, 0);
625 				if (q == 0)
626 					continue;
627 
628 				tp = selectTableMapping(t, q, 0, 0, dbId,
629 							&numMatches);
630 				if (tp == 0 || numMatches <= 0)
631 					table->enumTouch(i);
632 
633 				sfree(tp);
634 
635 				freeQuery(q);
636 			}
637 		}
638 
639 		logmsg(MSG_NOTIMECHECK, LOG_INFO, "%s: %d entries from LDAP",
640 			myself, nq);
641 	} else {
642 		db_index_entry	*dbie;
643 		long		i, count;
644 		bool_t		valid;
645 
646 		/*
647 		 * Find the entries in the DB that currently match the
648 		 * query, and add them to the enum array. Those that
649 		 * remain untouched when we've processed the LDAP data
650 		 * don't currently exist in LDAP, and should be deleted
651 		 * from the DB.
652 		 */
653 		dbie = mindex->satisfy_query_dbonly(qin, &count, FALSE, &valid);
654 		if (dbie != 0 && valid && count > 0) {
655 			table->setEnumMode(count);
656 			doEnum = TRUE;
657 			for (i = 0; i < count; i++) {
658 				table->enumSetup(dbie->getlocation(), i);
659 				dbie = dbie->getnextresult();
660 				if (dbie == 0)
661 					break;
662 			}
663 		} else {
664 			doEnum = FALSE;
665 		}
666 	}
667 
668 	entry_col	ec[NIS_MAXCOLUMNS+1];
669 	for (i = 0, na = 0; i < nq; i++) {
670 		entry_object	eo, *e;
671 		table_col	*tc;
672 		nis_object	o, *to;
673 		int		j, nc;
674 		db_qcomp	*qc;
675 
676 		if (res[i] == 0)
677 			continue;
678 
679 #ifdef	NISDB_LDAP_DEBUG
680 		printQuery(res[i], t);
681 		printObjAttr(oa[i]);
682 #endif	/* NISDB_LDAP_DEBUG */
683 
684 		/* Assemble an object from the query and attributes */
685 		(void) memset(&o, 0, sizeof (o));
686 		if (oa[i] != 0) {
687 			o.zo_owner = oa[i]->zo_owner;
688 			o.zo_group = oa[i]->zo_group;
689 			o.zo_domain = oa[i]->zo_domain;
690 			o.zo_access = oa[i]->zo_access;
691 			o.zo_ttl = oa[i]->zo_ttl;
692 		}
693 		if ((to = t->obj) != 0) {
694 			o.zo_name = to->zo_name;
695 			o.zo_data.objdata_u.en_data.en_type =
696 				to->zo_data.objdata_u.ta_data.ta_type;
697 			tc = to->zo_data.objdata_u.ta_data.ta_cols.ta_cols_val;
698 			if (to->zo_data.objdata_u.ta_data.ta_cols.ta_cols_len
699 					!= t->numColumns)
700 				tc = 0;
701 			if (o.zo_owner == 0)
702 				o.zo_owner = to->zo_owner;
703 			if (o.zo_group == 0)
704 				o.zo_group = to->zo_group;
705 			if (o.zo_domain == 0)
706 				o.zo_domain = to->zo_domain;
707 			if (o.zo_access == 0)
708 				o.zo_access = to->zo_access;
709 			if (o.zo_ttl == 0)
710 				o.zo_ttl = to->zo_ttl;
711 		} else {
712 			tc = 0;
713 			o.zo_owner = "";
714 			o.zo_group = "";
715 			o.zo_domain = "";
716 		}
717 
718 		o.zo_data.zo_type = NIS_ENTRY_OBJ;
719 		o.zo_data.objdata_u.en_data.en_cols.en_cols_len =
720 			t->numColumns + 1;
721 		o.zo_data.objdata_u.en_data.en_cols.en_cols_val = ec;
722 
723 		(void) memset(&ec, 0, sizeof (ec));
724 		nc = res[i]->size();
725 		qc = res[i]->queryloc();
726 		if (qc == 0) {
727 			freeQuery(res[i]);
728 			continue;
729 		}
730 		for (j = 0; j < nc; j++) {
731 			int	ic = 1+ qc[j].which_index;
732 			if (ic < 1 || ic > t->numColumns)
733 				continue;
734 #ifdef	SET_ENTRY_FLAGS
735 			if (tc != 0)
736 				ec[ic].ec_flags =
737 					entryFlagsFromTable(tc[ic-1].tc_flags);
738 #else
739 			/*
740 			 * In theory, the entry flags should be derived
741 			 * from the table flags. However, that doesn't
742 			 * seem to be the way that the DB code has done
743 			 * things so far, so leave the entry flags unset.
744 			 */
745 #endif	/* SET_ENTRY_FLAGS */
746 			qc[j].index_value->get_value(
747 					&ec[ic].ec_value.ec_value_val,
748 					(int *)&ec[ic].ec_value.ec_value_len);
749 		}
750 
751 		setOid(&o);
752 		e = makePseudoEntryObj(&o, &eo, t->obj);
753 		if (e == 0) {
754 			freeQuery(res[i]);
755 			continue;
756 		}
757 
758 		/*
759 		 * 'o' is currently a pseudo-object of type entry, with
760 		 * column zero used for an XDR:ed version of the entry_obj,
761 		 * column one the real column zero of the entry, etc.
762 		 * We now need a real NIS_ENTRY_OBJ object, so move the
763 		 * entry_col array one step left.
764 		 */
765 		o.zo_data.objdata_u.en_data.en_cols.en_cols_len = t->numColumns;
766 		o.zo_data.objdata_u.en_data.en_cols.en_cols_val = &ec[1];
767 
768 		stat = mindex->updateTableEntry(e, 1, t->objName, &o, t->obj,
769 						o.zo_oid.mtime, &xid);
770 		/*
771 		 * LDAP_SUCCESS =>	Entry added or modified
772 		 * LDAP_COMPARE_TRUE =>	Entry same as existing one
773 		 * other =>		Error
774 		 */
775 		if (stat == LDAP_SUCCESS) {
776 			na++;
777 		} else if (stat == LDAP_COMPARE_TRUE) {
778 			stat = LDAP_SUCCESS;
779 		} else {
780 			logmsg(MSG_NOTIMECHECK, LOG_WARNING,
781 				"%s: Error adding entry to \"%s\": %s",
782 				myself, NIL(t->objName),
783 				ldap_err2string(stat));
784 		}
785 
786 		if (e->en_cols.en_cols_val != 0)
787 			sfree(e->en_cols.en_cols_val[0].ec_value.ec_value_val);
788 
789 		freeQuery(res[i]);
790 	}
791 
792 	sfree(res);
793 
794 	/* Take care of deletes if we enumerated the table */
795 	if (doEnum) {
796 		ea = table->endEnumMode(&numEa);
797 		logmsg(MSG_NOTIMECHECK, LOG_INFO,
798 			"%s: %d entries added/updated", myself, na);
799 		nau = na;
800 	} else
801 		ea = 0;
802 	if (ea != 0) {
803 		uint32_t	nowt = time(0);
804 
805 		for (i = 0; i < numEa; i++) {
806 			int	st;
807 
808 			if (ea[i] == 0)
809 				continue;
810 
811 			st = mindex->updateTableEntry(ea[i], 0, t->objName, 0,
812 						t->obj, nowt, &xid);
813 			if (st == LDAP_SUCCESS) {
814 				na++;
815 			} else {
816 				logmsg(MSG_NOTIMECHECK, LOG_WARNING,
817 			"%s: Error removing directory entry for \"%s\": %s",
818 					myself, NIL(t->objName),
819 					ldap_err2string(st));
820 				if (stat == LDAP_SUCCESS)
821 					stat = st;
822 			}
823 		}
824 		if (stat == LDAP_SUCCESS) {
825 			struct timeval	now;
826 			(void) gettimeofday(&now, 0);
827 			table->mapping.enumExpire = now.tv_sec +
828 				table->mapping.ttl;
829 		}
830 		if (doEnum)
831 			logmsg(MSG_NOTIMECHECK, LOG_INFO,
832 				"%s: %d entries deleted", myself, na-nau);
833 	}
834 
835 	sfree(ea);
836 
837 	/* If we called log_action() successfully, we need to sync the log */
838 	if (na > 0)
839 		(void) ((db *)mindex->getDbPtr())->sync_log();
840 
841 	if (xid != 0 && na > 0 && stat == LDAP_SUCCESS)
842 		ret = endTransaction(xid, dirObj);
843 	else if (xid != 0)
844 		ret = abort_transaction(xid);
845 	else
846 		ret = 0;
847 	if (ret != 0) {
848 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
849 			"%s: Error %s transaction for \"%s\"",
850 			myself, (na > 0 && stat == LDAP_SUCCESS) ?
851 					"ending" : "aborting",
852 			NIL(t->objName));
853 		stat = LDAP_OPERATIONS_ERROR;
854 	}
855 
856 	mindex->clearNoLDAPquery();
857 	mindex->clearNoWriteThrough();
858 	freeObjAttr(oa, nq);
859 
860 #ifdef	NISDB_LDAP_DEBUG
861 	printbuf();
862 #endif	/* NISDB_LDAP_DEBUG */
863 
864 	if (doEnum)
865 		logmsg(MSG_NOTIMECHECK, LOG_INFO,
866 			"%s: enumeration \"%s\" done", myself, NIL(t->objName));
867 
868 	if (arg->isDeferred) {
869 		/*
870 		 * Rollback doesn't recover data written to disk, so
871 		 * we should commit even if we're returning failure.
872 		 */
873 		dstat = InUseDictionary->commit(t->objPath);
874 		if (dstat != DB_SUCCESS) {
875 			logmsg(MSG_NOTIMECHECK, LOG_WARNING,
876 				"%s: DB error %d committing \"%s\"",
877 				myself, dstat, NIL(t->objName));
878 		}
879 	}
880 	(void) mutex_lock(&table->mapping.enumLock);
881 	if (arg->isDeferred && dstat == DB_SUCCESS)
882 		table->mapping.enumDeferred = 0;
883 	table->mapping.enumStat = stat;
884 	{
885 		struct timeval	end;
886 
887 		(void) gettimeofday(&end, 0);
888 		end.tv_sec -= start.tv_sec;
889 		end.tv_usec -= start.tv_usec;
890 		if (end.tv_usec < 0) {
891 			end.tv_usec += 1000000;
892 			end.tv_sec -= 1;
893 		}
894 		table->mapping.enumTime = 1000000*end.tv_sec + end.tv_usec;
895 		logmsg(MSG_NOTIMECHECK,
896 #ifdef	NISDB_LDAP_DEBUG
897 			LOG_WARNING,
898 #else
899 			LOG_INFO,
900 #endif	/* NISDB_LDAP_DEBUG */
901 			"%s: %d entries in %ld usec => %ld usec/entry",
902 			NIL(t->objName), table->mapping.enumEntries,
903 			table->mapping.enumTime,
904 			table->mapping.enumTime/
905 			(table->mapping.enumEntries != 0 ?
906 				table->mapping.enumEntries : 1));
907 	}
908 	table->mapping.enumTid = 0;
909 	(void) mutex_unlock(&table->mapping.enumLock);
910 
911 	WRITEUNLOCKNR(table, stat3, "table wu db_mindex::entriesFromLDAPreal");
912 	WRITEUNLOCKNR(mindex, stat2, "db_mindex::entriesFromLDAPreal wu");
913 	WRITEUNLOCKNR(((db *)mindex->getDbPtr()), lstat,
914 		"db db_mindex::entriesFromLDAPreal wu");
915 	unlockTransLog(myself, 1);
916 	if (stat3 != 0)
917 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
918 			"%s: Error %d unlocking db_table", myself, stat3);
919 	if (stat2 != 0)
920 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
921 			"%s: Error %d unlocking db_mindex", myself, stat2);
922 	if (lstat != 0)
923 		logmsg(MSG_NOTIMECHECK, LOG_WARNING,
924 			"%s: Error %d unlocking db", myself, lstat);
925 
926 	return (stat);
927 }
928