17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
536e852a1SRaja Andra * Common Development and Distribution License (the "License").
636e852a1SRaja Andra * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
2236e852a1SRaja Andra * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <time.h>
287c478bd9Sstevel@tonic-gate #include <sys/time.h>
297c478bd9Sstevel@tonic-gate #include <lber.h>
307c478bd9Sstevel@tonic-gate #include <ldap.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <pthread.h>
337c478bd9Sstevel@tonic-gate #include "db_headers.h"
347c478bd9Sstevel@tonic-gate #include "db.h"
357c478bd9Sstevel@tonic-gate #include "db_mindex.h"
367c478bd9Sstevel@tonic-gate #include "db_dictionary.h"
377c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
387c478bd9Sstevel@tonic-gate #include "ldap_map.h"
397c478bd9Sstevel@tonic-gate #include "ldap_glob.h"
407c478bd9Sstevel@tonic-gate #include "ldap_util.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate extern db_dictionary *InUseDictionary;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate extern "C" {
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate typedef struct {
497c478bd9Sstevel@tonic-gate db_mindex *mindex;
507c478bd9Sstevel@tonic-gate __nis_table_mapping_t *t;
517c478bd9Sstevel@tonic-gate db_query *qin;
527c478bd9Sstevel@tonic-gate db_query *q;
537c478bd9Sstevel@tonic-gate char *dbId;
547c478bd9Sstevel@tonic-gate nis_object *dirObj;
557c478bd9Sstevel@tonic-gate int isDeferred;
567c478bd9Sstevel@tonic-gate char *tableName;
577c478bd9Sstevel@tonic-gate } __entries_from_ldap_arg_t;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate static void *entriesFromLDAPthread(void *);
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate int entriesFromLDAPreal(__entries_from_ldap_arg_t *);
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate #ifdef SET_ENTRY_FLAGS
667c478bd9Sstevel@tonic-gate static uint_t
entryFlagsFromTable(uint_t tf)677c478bd9Sstevel@tonic-gate entryFlagsFromTable(uint_t tf) {
687c478bd9Sstevel@tonic-gate uint_t ef = 0;
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate if ((tf & TA_BINARY) != 0)
717c478bd9Sstevel@tonic-gate ef |= EN_BINARY;
727c478bd9Sstevel@tonic-gate if ((tf & TA_CRYPT) != 0)
737c478bd9Sstevel@tonic-gate ef |= EN_CRYPT;
747c478bd9Sstevel@tonic-gate if ((tf & TA_XDR) != 0)
757c478bd9Sstevel@tonic-gate ef |= EN_XDR;
767c478bd9Sstevel@tonic-gate if ((tf & TA_ASN1) != 0)
777c478bd9Sstevel@tonic-gate ef |= EN_ASN1;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate return (ef);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate #endif /* SET_ENTRY_FLAGS */
827c478bd9Sstevel@tonic-gate
8336e852a1SRaja Andra static void setOid(nis_object *obj);
8436e852a1SRaja Andra
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate * Retrieve container entries from LDAP per 't' and 'qin'/'q'.
877c478bd9Sstevel@tonic-gate * This is a helper function for db_mindex::queryLDAP(); see
887c478bd9Sstevel@tonic-gate * that function for details of the parameters (except doAsynch).
897c478bd9Sstevel@tonic-gate *
907c478bd9Sstevel@tonic-gate * If 'doAsynch' is set, and the retrieval is an enumeration
917c478bd9Sstevel@tonic-gate * (qin == NULL), the retrieval is performed in a detached
927c478bd9Sstevel@tonic-gate * thread. In this case, the return code just reflects the
937c478bd9Sstevel@tonic-gate * setup and launch of the detached thread. Retrieval will
947c478bd9Sstevel@tonic-gate * complete asynchronously.
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate int
entriesFromLDAP(__nis_table_mapping_t * t,db_query * qin,db_query * q,char * dbId,nis_object * dirObj,int doAsynch)977c478bd9Sstevel@tonic-gate db_mindex::entriesFromLDAP(__nis_table_mapping_t *t, db_query *qin, db_query *q,
987c478bd9Sstevel@tonic-gate char *dbId, nis_object *dirObj, int doAsynch) {
997c478bd9Sstevel@tonic-gate __entries_from_ldap_arg_t *arg;
1007c478bd9Sstevel@tonic-gate int stat;
1017c478bd9Sstevel@tonic-gate db_status dstat;
102*8d0852b7SRichard Lowe const char *myself = "db_mindex::entriesFromLDAP";
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate arg = (__entries_from_ldap_arg_t *)am(myself, sizeof (*arg));
1057c478bd9Sstevel@tonic-gate if (arg == 0) {
1067c478bd9Sstevel@tonic-gate freeQuery(q);
1077c478bd9Sstevel@tonic-gate if (dirObj != 0)
1087c478bd9Sstevel@tonic-gate nis_destroy_object(dirObj);
1097c478bd9Sstevel@tonic-gate return (LDAP_NO_MEMORY);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate arg->mindex = this;
1137c478bd9Sstevel@tonic-gate arg->t = t;
1147c478bd9Sstevel@tonic-gate arg->qin = qin;
1157c478bd9Sstevel@tonic-gate arg->q = q;
1167c478bd9Sstevel@tonic-gate arg->dbId = dbId;
1177c478bd9Sstevel@tonic-gate arg->dirObj = dirObj;
1187c478bd9Sstevel@tonic-gate arg->tableName = t->objName;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * Check if an enumeration thread is running; if so, then regardless
1227c478bd9Sstevel@tonic-gate * of whether or not the current operation is an enumeration, we
1237c478bd9Sstevel@tonic-gate * just return success, and let our caller get the data from the
1247c478bd9Sstevel@tonic-gate * existing (deferred) DB.
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->mapping.enumLock);
1277c478bd9Sstevel@tonic-gate if (table->mapping.enumTid != 0) {
1287c478bd9Sstevel@tonic-gate int doReturn = 0;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate stat = pthread_kill(table->mapping.enumTid, 0);
1317c478bd9Sstevel@tonic-gate if (stat == ESRCH) {
1327c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
1337c478bd9Sstevel@tonic-gate "%s: Enumeration thread %d not found for \"%s\"; exit status = %d (%s)",
1347c478bd9Sstevel@tonic-gate myself, table->mapping.enumTid,
1357c478bd9Sstevel@tonic-gate NIL(t->objName), table->mapping.enumStat,
1367c478bd9Sstevel@tonic-gate ldap_err2string(table->mapping.enumStat));
1377c478bd9Sstevel@tonic-gate /* Reflect the fact that no enum thread is running */
1387c478bd9Sstevel@tonic-gate table->mapping.enumTid = 0;
1397c478bd9Sstevel@tonic-gate table->mapping.enumStat = -1;
1407c478bd9Sstevel@tonic-gate /* Cleanup deferred mode */
1417c478bd9Sstevel@tonic-gate if (table->mapping.enumDeferred) {
1427c478bd9Sstevel@tonic-gate dstat = InUseDictionary->commit(t->objPath);
1437c478bd9Sstevel@tonic-gate if (dstat == DB_SUCCESS) {
1447c478bd9Sstevel@tonic-gate table->mapping.enumDeferred = 0;
1457c478bd9Sstevel@tonic-gate } else {
1467c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
1477c478bd9Sstevel@tonic-gate "%s: DB error %d committing \"%s\"",
1487c478bd9Sstevel@tonic-gate myself, dstat, NIL(t->objName));
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate } else if (stat == 0) {
1527c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
1537c478bd9Sstevel@tonic-gate "%s: Enumeration thread %d already running for \"%s\"",
1547c478bd9Sstevel@tonic-gate myself, table->mapping.enumTid,
1557c478bd9Sstevel@tonic-gate NIL(t->objName));
1567c478bd9Sstevel@tonic-gate stat = LDAP_SUCCESS;
1577c478bd9Sstevel@tonic-gate doReturn = 1;
1587c478bd9Sstevel@tonic-gate } else {
1597c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
1607c478bd9Sstevel@tonic-gate "%s: Error %d looking for enumeration thread %d for \"%s\"",
1617c478bd9Sstevel@tonic-gate myself, stat, table->mapping.enumTid,
1627c478bd9Sstevel@tonic-gate NIL(t->objName));
1637c478bd9Sstevel@tonic-gate doReturn = 1;
1647c478bd9Sstevel@tonic-gate stat = LDAP_OPERATIONS_ERROR;
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate if (doReturn) {
1677c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
1687c478bd9Sstevel@tonic-gate sfree(arg);
1697c478bd9Sstevel@tonic-gate freeQuery(q);
1707c478bd9Sstevel@tonic-gate if (dirObj != 0)
1717c478bd9Sstevel@tonic-gate nis_destroy_object(dirObj);
1727c478bd9Sstevel@tonic-gate return (stat);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * If we're enumerating (and hence expect that retrieving all data,
1787c478bd9Sstevel@tonic-gate * and updating the local DB, might take a while), create a deferred-
1797c478bd9Sstevel@tonic-gate * update table that clients can use while we are updating the real
1807c478bd9Sstevel@tonic-gate * one.
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate if (doAsynch && qin == 0) {
1837c478bd9Sstevel@tonic-gate if ((dstat = InUseDictionary->defer(t->objPath)) ==
1847c478bd9Sstevel@tonic-gate DB_SUCCESS) {
1857c478bd9Sstevel@tonic-gate arg->isDeferred = 1;
1867c478bd9Sstevel@tonic-gate table->mapping.enumDeferred = 1;
1877c478bd9Sstevel@tonic-gate } else {
1887c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
1897c478bd9Sstevel@tonic-gate "%s: Unable to defer updates for \"%s\" (status=%d);"
1907c478bd9Sstevel@tonic-gate " updating in place",
1917c478bd9Sstevel@tonic-gate myself, NIL(t->objName), dstat);
1927c478bd9Sstevel@tonic-gate arg->isDeferred = 0;
1937c478bd9Sstevel@tonic-gate table->mapping.enumDeferred = 0;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate } else {
1967c478bd9Sstevel@tonic-gate arg->isDeferred = 0;
1977c478bd9Sstevel@tonic-gate table->mapping.enumDeferred = 0;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /* If enumerating, perform the operation in a separate thread */
2017c478bd9Sstevel@tonic-gate if (doAsynch && qin == 0) {
2027c478bd9Sstevel@tonic-gate pthread_t tid;
2037c478bd9Sstevel@tonic-gate pthread_attr_t attr;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate (void) pthread_attr_init(&attr);
2067c478bd9Sstevel@tonic-gate #ifdef FORCE_SYNCHRONOUS
2077c478bd9Sstevel@tonic-gate #else
2087c478bd9Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&attr,
2097c478bd9Sstevel@tonic-gate PTHREAD_CREATE_DETACHED);
2107c478bd9Sstevel@tonic-gate #endif /* FORCE_SYNCHRONOUS */
2117c478bd9Sstevel@tonic-gate stat = pthread_create(&tid, &attr, entriesFromLDAPthread, arg);
2127c478bd9Sstevel@tonic-gate if (stat != 0) {
2137c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
2147c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2157c478bd9Sstevel@tonic-gate "%s: Error %d creating new thread; using current one",
2167c478bd9Sstevel@tonic-gate myself, stat);
2177c478bd9Sstevel@tonic-gate stat = (int)entriesFromLDAPthread(arg);
2187c478bd9Sstevel@tonic-gate return (stat);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate table->mapping.enumTid = tid;
2227c478bd9Sstevel@tonic-gate table->mapping.enumStat = -1;
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * We're now returning to the caller, who will get data
2267c478bd9Sstevel@tonic-gate * from:
2277c478bd9Sstevel@tonic-gate *
2287c478bd9Sstevel@tonic-gate * The deferred DB, if an enumeration thread already
2297c478bd9Sstevel@tonic-gate * was running, and deferred mode was on, or
2307c478bd9Sstevel@tonic-gate *
2317c478bd9Sstevel@tonic-gate * The original DB, if we just started an enumeration
2327c478bd9Sstevel@tonic-gate * thread. In this case, our caller (several levels up)
2337c478bd9Sstevel@tonic-gate * is holding a lock on the db_mindex/db_table, which
2347c478bd9Sstevel@tonic-gate * means that the enum thread will have to wait for
2357c478bd9Sstevel@tonic-gate * our caller once it's done the LDAP retrieval, and
2367c478bd9Sstevel@tonic-gate * wants to update the DB.
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
2397c478bd9Sstevel@tonic-gate stat = LDAP_SUCCESS;
2407c478bd9Sstevel@tonic-gate #ifdef FORCE_SYNCHRONOUS
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate int tstat;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate stat = pthread_join(tid, (void **)&tstat);
2457c478bd9Sstevel@tonic-gate if (stat == 0) {
2467c478bd9Sstevel@tonic-gate stat = tstat;
2477c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2487c478bd9Sstevel@tonic-gate "%s: thread %d => %d",
2497c478bd9Sstevel@tonic-gate myself, tid, tstat);
2507c478bd9Sstevel@tonic-gate } else {
2517c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
2527c478bd9Sstevel@tonic-gate "%s: pthread_join(%d) => %d",
2537c478bd9Sstevel@tonic-gate myself, tid, stat);
2547c478bd9Sstevel@tonic-gate stat = LDAP_OPERATIONS_ERROR;
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate #endif /* FORCE_SYNCHRONOUS */
2587c478bd9Sstevel@tonic-gate } else {
2597c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
2607c478bd9Sstevel@tonic-gate stat = (int)entriesFromLDAPthread(arg);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate return (stat);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate extern "C" {
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate * We use this 'extern "C"' function in order to make sure that
2707c478bd9Sstevel@tonic-gate * pthread_create() doesn't have any problems trying to invoke a
2717c478bd9Sstevel@tonic-gate * C++ function.
2727c478bd9Sstevel@tonic-gate */
2737c478bd9Sstevel@tonic-gate static void *
entriesFromLDAPthread(void * voidarg)2747c478bd9Sstevel@tonic-gate entriesFromLDAPthread(void *voidarg) {
2757c478bd9Sstevel@tonic-gate __entries_from_ldap_arg_t *arg;
2767c478bd9Sstevel@tonic-gate int stat;
2777c478bd9Sstevel@tonic-gate db *dbase;
2787c478bd9Sstevel@tonic-gate db_table_desc *tbl = 0;
2797c478bd9Sstevel@tonic-gate char *tableName;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate arg = (__entries_from_ldap_arg_t *)voidarg;
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /* Lock to prevent removal */
2847c478bd9Sstevel@tonic-gate (void) __nis_lock_db_table(arg->tableName, 1, 0,
2857c478bd9Sstevel@tonic-gate "entriesFromLDAPthread");
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate * It's possible that the db_mindex for the table has changed,
2897c478bd9Sstevel@tonic-gate * or disappeared, between now and the time when our parent
2907c478bd9Sstevel@tonic-gate * thread released its lock on the table. Hence, we search the
2917c478bd9Sstevel@tonic-gate * dictionary to re-acquire the 'db', and the db_mindex.
2927c478bd9Sstevel@tonic-gate */
2937c478bd9Sstevel@tonic-gate tableName = internalTableName(arg->tableName);
2947c478bd9Sstevel@tonic-gate if (tableName != 0) {
2957c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
2967c478bd9Sstevel@tonic-gate db_mindex *oldMindex = arg->mindex;
2977c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate dbase = InUseDictionary->find_table(tableName, &tbl, FALSE);
3007c478bd9Sstevel@tonic-gate if (dbase != 0)
3017c478bd9Sstevel@tonic-gate arg->mindex = dbase->mindex();
3027c478bd9Sstevel@tonic-gate else
3037c478bd9Sstevel@tonic-gate arg->mindex = 0;
3047c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
3057c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
3067c478bd9Sstevel@tonic-gate "entriesFromLDAPthread: %s -> %s -> 0x%x (0x%x)",
3077c478bd9Sstevel@tonic-gate NIL(arg->tableName), NIL(tableName),
3087c478bd9Sstevel@tonic-gate arg->mindex, oldMindex);
3097c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
3107c478bd9Sstevel@tonic-gate sfree(tableName);
3117c478bd9Sstevel@tonic-gate tableName = 0;
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate stat = entriesFromLDAPreal(arg);
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate (void) __nis_ulock_db_table(arg->tableName, 1, 0,
3177c478bd9Sstevel@tonic-gate "entriesFromLDAPthread");
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate freeQuery(arg->q);
3207c478bd9Sstevel@tonic-gate if (arg->dirObj != 0)
3217c478bd9Sstevel@tonic-gate nis_destroy_object(arg->dirObj);
3227c478bd9Sstevel@tonic-gate sfree(arg);
3237c478bd9Sstevel@tonic-gate return ((void *)stat);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate int
entriesFromLDAPreal(__entries_from_ldap_arg_t * arg)3297c478bd9Sstevel@tonic-gate entriesFromLDAPreal(__entries_from_ldap_arg_t *arg) {
3307c478bd9Sstevel@tonic-gate db_mindex *mindex;
3317c478bd9Sstevel@tonic-gate db_table *table;
3327c478bd9Sstevel@tonic-gate __nis_table_mapping_t *t;
3337c478bd9Sstevel@tonic-gate db_query *q, *qin;
3347c478bd9Sstevel@tonic-gate char *dbId;
3357c478bd9Sstevel@tonic-gate nis_object *dirObj;
3367c478bd9Sstevel@tonic-gate int i, na, nau, nq = 0, xid = 0;
3377c478bd9Sstevel@tonic-gate int ret, stat = LDAP_SUCCESS, stat2, stat3;
3387c478bd9Sstevel@tonic-gate int lstat;
3397c478bd9Sstevel@tonic-gate __nis_obj_attr_t **oa = 0;
3407c478bd9Sstevel@tonic-gate db_query **res;
3417c478bd9Sstevel@tonic-gate entry_object **ea;
3427c478bd9Sstevel@tonic-gate long numEa;
3437c478bd9Sstevel@tonic-gate bool_t doEnum;
3447c478bd9Sstevel@tonic-gate db_status dstat;
3457c478bd9Sstevel@tonic-gate struct timeval start;
346*8d0852b7SRichard Lowe const char *myself =
3477c478bd9Sstevel@tonic-gate "db_mindex::entriesFromLDAPreal";
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate if (arg == 0)
3507c478bd9Sstevel@tonic-gate return (LDAP_PARAM_ERROR);
3517c478bd9Sstevel@tonic-gate mindex = arg->mindex;
3527c478bd9Sstevel@tonic-gate t = arg->t;
3537c478bd9Sstevel@tonic-gate q = arg->q;
3547c478bd9Sstevel@tonic-gate qin = arg->qin;
3557c478bd9Sstevel@tonic-gate dbId = arg->dbId;
3567c478bd9Sstevel@tonic-gate dirObj = arg->dirObj;
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate table = (mindex != 0) ? mindex->getTable() : 0;
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate if (mindex == 0 || t == 0 || table == 0) {
3617c478bd9Sstevel@tonic-gate /* We haven't done anything, so rollback should be OK */
3627c478bd9Sstevel@tonic-gate if (arg->isDeferred && t != 0) {
3637c478bd9Sstevel@tonic-gate dstat = InUseDictionary->rollback(t->objPath);
3647c478bd9Sstevel@tonic-gate if (dstat != DB_SUCCESS) {
3657c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
3667c478bd9Sstevel@tonic-gate "%s: DB error %d rolling back \"%s\"",
3677c478bd9Sstevel@tonic-gate myself, dstat, NIL(t->objName));
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate * Had rollback succeeded, the 'table'
3707c478bd9Sstevel@tonic-gate * would have disappeared. However, since
3717c478bd9Sstevel@tonic-gate * rollback failed, we need to update the
3727c478bd9Sstevel@tonic-gate * table->mapping.enum* fields.
3737c478bd9Sstevel@tonic-gate */
3747c478bd9Sstevel@tonic-gate if (table != 0) {
3757c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->
3767c478bd9Sstevel@tonic-gate mapping.enumLock);
3777c478bd9Sstevel@tonic-gate table->mapping.enumStat =
3787c478bd9Sstevel@tonic-gate LDAP_PARAM_ERROR;
3797c478bd9Sstevel@tonic-gate table->mapping.enumTime = 0;
3807c478bd9Sstevel@tonic-gate table->mapping.enumEntries = 0;
3817c478bd9Sstevel@tonic-gate table->mapping.enumTid = 0;
3827c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->
3837c478bd9Sstevel@tonic-gate mapping.enumLock);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate return (LDAP_PARAM_ERROR);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate if (qin == 0)
3917c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO, "%s: enumerating \"%s%s%s\"",
3927c478bd9Sstevel@tonic-gate myself, dbId ? dbId : "", dbId ? ":" : "",
3937c478bd9Sstevel@tonic-gate NIL(t->objName));
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate (void) gettimeofday(&start, 0);
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate /* Getting table entries */
3987c478bd9Sstevel@tonic-gate res = mapFromLDAP(t, q, &nq, dbId, &stat, &oa);
3997c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
4007c478bd9Sstevel@tonic-gate logmsg(MSG_ALWAYS, LOG_INFO,
4017c478bd9Sstevel@tonic-gate "%s: mapFromLDAP() => 0x%x, status=%d %s; nq = %d",
4027c478bd9Sstevel@tonic-gate myself, res, stat, stat == LDAP_SUCCESS ? "" :
4037c478bd9Sstevel@tonic-gate ldap_err2string(stat), nq);
4047c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * Keep track of the number of NIS+ entries we got back;
4087c478bd9Sstevel@tonic-gate * note that the number of LDAP entries may have been
4097c478bd9Sstevel@tonic-gate * smaller or larger.
4107c478bd9Sstevel@tonic-gate */
4117c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->mapping.enumLock);
4127c478bd9Sstevel@tonic-gate table->mapping.enumEntries = nq;
4137c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate * If we get LDAP_NO_SUCH_OBJECT, we need to delete the entries
4177c478bd9Sstevel@tonic-gate * in the table, so we can't just return.
4187c478bd9Sstevel@tonic-gate */
4197c478bd9Sstevel@tonic-gate if (res == 0 && stat != LDAP_NO_SUCH_OBJECT) {
4207c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
4217c478bd9Sstevel@tonic-gate "%s: mapFromLDAP() => 0x0, status=%d (%s)",
4227c478bd9Sstevel@tonic-gate myself, stat, ldap_err2string(stat));
4237c478bd9Sstevel@tonic-gate if (arg->isDeferred) {
4247c478bd9Sstevel@tonic-gate dstat = InUseDictionary->rollback(t->objPath);
4257c478bd9Sstevel@tonic-gate if (dstat != DB_SUCCESS) {
4267c478bd9Sstevel@tonic-gate struct timeval end;
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
4297c478bd9Sstevel@tonic-gate "%s: DB error %d rolling back \"%s\"",
4307c478bd9Sstevel@tonic-gate myself, dstat, NIL(t->objName));
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate * Had rollback succeeded, the 'table'
4337c478bd9Sstevel@tonic-gate * would have disappeared. However, since
4347c478bd9Sstevel@tonic-gate * rollback failed, we need to update the
4357c478bd9Sstevel@tonic-gate * table->mapping.enum* fields.
4367c478bd9Sstevel@tonic-gate */
4377c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->mapping.enumLock);
4387c478bd9Sstevel@tonic-gate table->mapping.enumStat = stat;
4397c478bd9Sstevel@tonic-gate (void) gettimeofday(&end, 0);
4407c478bd9Sstevel@tonic-gate end.tv_sec -= start.tv_sec;
4417c478bd9Sstevel@tonic-gate end.tv_usec -= start.tv_usec;
4427c478bd9Sstevel@tonic-gate if (end.tv_usec < 0) {
4437c478bd9Sstevel@tonic-gate end.tv_usec += 1000000;
4447c478bd9Sstevel@tonic-gate end.tv_sec -= 1;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate table->mapping.enumTime =
4477c478bd9Sstevel@tonic-gate 1000000*end.tv_sec + end.tv_usec;
4487c478bd9Sstevel@tonic-gate table->mapping.enumTid = 0;
4497c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate return (stat);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate * Need to disable write-through to LDAP, for which we need a lock
4577c478bd9Sstevel@tonic-gate * on our db_mindex ('mindex'); we're also updating the table, so
4587c478bd9Sstevel@tonic-gate * we need a write lock on that as well. However, before locking the
4597c478bd9Sstevel@tonic-gate * mindex, we need to maintain lock integrity by acquiring the
4607c478bd9Sstevel@tonic-gate * trans log lock. Note that actually beginning a transaction is
4617c478bd9Sstevel@tonic-gate * expensive, so we defer that until we know that we really need
4627c478bd9Sstevel@tonic-gate * to update.
4637c478bd9Sstevel@tonic-gate */
4647c478bd9Sstevel@tonic-gate lstat = lockTransLog(myself, 1, 1);
4657c478bd9Sstevel@tonic-gate if (lstat != 0) {
4667c478bd9Sstevel@tonic-gate if (lstat == EBUSY)
4677c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
4687c478bd9Sstevel@tonic-gate "%s: transaction log busy; no LDAP update for \"%s\"",
4697c478bd9Sstevel@tonic-gate myself, NIL(t->objName));
4707c478bd9Sstevel@tonic-gate else
4717c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
4727c478bd9Sstevel@tonic-gate "%s: Error %d locking transaction log; no LDAP update for \"%s\"",
4737c478bd9Sstevel@tonic-gate myself, lstat, NIL(t->objName));
4747c478bd9Sstevel@tonic-gate if (arg->isDeferred) {
4757c478bd9Sstevel@tonic-gate dstat = InUseDictionary->rollback(t->objPath);
4767c478bd9Sstevel@tonic-gate if (dstat != DB_SUCCESS) {
4777c478bd9Sstevel@tonic-gate struct timeval end;
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
4807c478bd9Sstevel@tonic-gate "%s: DB error %d rolling back \"%s\"",
4817c478bd9Sstevel@tonic-gate myself, dstat, NIL(t->objName));
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate * Had rollback succeeded, the 'table'
4847c478bd9Sstevel@tonic-gate * would have disappeared. However, since
4857c478bd9Sstevel@tonic-gate * rollback failed, we need to update the
4867c478bd9Sstevel@tonic-gate * table->mapping.enum* fields.
4877c478bd9Sstevel@tonic-gate */
4887c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->mapping.enumLock);
4897c478bd9Sstevel@tonic-gate table->mapping.enumStat = LDAP_OPERATIONS_ERROR;
4907c478bd9Sstevel@tonic-gate (void) gettimeofday(&end, 0);
4917c478bd9Sstevel@tonic-gate end.tv_sec -= start.tv_sec;
4927c478bd9Sstevel@tonic-gate end.tv_usec -= start.tv_usec;
4937c478bd9Sstevel@tonic-gate if (end.tv_usec < 0) {
4947c478bd9Sstevel@tonic-gate end.tv_usec += 1000000;
4957c478bd9Sstevel@tonic-gate end.tv_sec -= 1;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate table->mapping.enumTime = 1000000*end.tv_sec +
4987c478bd9Sstevel@tonic-gate end.tv_usec;
4997c478bd9Sstevel@tonic-gate table->mapping.enumTid = 0;
5007c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate return (LDAP_OPERATIONS_ERROR);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate * If we have any updates, we'll call db::sync_log, which write-
5087c478bd9Sstevel@tonic-gate * locks the 'db' instance. In order to avoid a dead-lock with
5097c478bd9Sstevel@tonic-gate * threads performing a DB lookup (which will lock the 'db' and
5107c478bd9Sstevel@tonic-gate * then the 'db_mindex'), we need hence need to lock in the
5117c478bd9Sstevel@tonic-gate * following order:
5127c478bd9Sstevel@tonic-gate *
5137c478bd9Sstevel@tonic-gate * trans.log (already holding that one)
5147c478bd9Sstevel@tonic-gate * db
5157c478bd9Sstevel@tonic-gate * db_mindex
5167c478bd9Sstevel@tonic-gate * db_table
5177c478bd9Sstevel@tonic-gate */
5187c478bd9Sstevel@tonic-gate TRYWRITELOCK(((db *)mindex->getDbPtr()), stat,
5197c478bd9Sstevel@tonic-gate "w db db_mindex::entriesFromLDAPreal");
5207c478bd9Sstevel@tonic-gate if (stat == 0) {
5217c478bd9Sstevel@tonic-gate TRYWRITELOCK(mindex, stat2, "w db_mindex::entriesFromLDAPreal");
5227c478bd9Sstevel@tonic-gate if (stat2 == 0) {
5237c478bd9Sstevel@tonic-gate TRYWRITELOCK(table, stat3,
5247c478bd9Sstevel@tonic-gate "table w db_mindex::entriesFromLDAPreal");
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate if (stat != 0 || stat2 != 0 || stat3 != 0) {
5297c478bd9Sstevel@tonic-gate if (stat != 0) {
5307c478bd9Sstevel@tonic-gate if (stat == EBUSY)
5317c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
5327c478bd9Sstevel@tonic-gate "%s: 'db' busy; no LDAP update for \"%s\"",
5337c478bd9Sstevel@tonic-gate myself, NIL(t->objName));
5347c478bd9Sstevel@tonic-gate else
5357c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
5367c478bd9Sstevel@tonic-gate "%s: 'db' lock error %d; no LDAP update for \"%s\"",
5377c478bd9Sstevel@tonic-gate myself, stat, NIL(t->objName));
5387c478bd9Sstevel@tonic-gate } else if (stat2 != 0) {
5397c478bd9Sstevel@tonic-gate if (stat2 == EBUSY)
5407c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
5417c478bd9Sstevel@tonic-gate "%s: 'db_mindex' busy; no LDAP update for \"%s\"",
5427c478bd9Sstevel@tonic-gate myself, NIL(t->objName));
5437c478bd9Sstevel@tonic-gate else
5447c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
5457c478bd9Sstevel@tonic-gate "%s: 'db_mindex' lock error %d; no LDAP update for \"%s\"",
5467c478bd9Sstevel@tonic-gate myself, stat2, NIL(t->objName));
5477c478bd9Sstevel@tonic-gate } else {
5487c478bd9Sstevel@tonic-gate if (stat3 == EBUSY)
5497c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
5507c478bd9Sstevel@tonic-gate "%s: 'db_table' busy; no LDAP update for \"%s\"",
5517c478bd9Sstevel@tonic-gate myself, NIL(t->objName));
5527c478bd9Sstevel@tonic-gate else
5537c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
5547c478bd9Sstevel@tonic-gate "%s: 'db_table' lock error %d; no LDAP update for \"%s\"",
5557c478bd9Sstevel@tonic-gate myself, stat3, NIL(t->objName));
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate freeQueries(res, nq);
5587c478bd9Sstevel@tonic-gate if (arg->isDeferred) {
5597c478bd9Sstevel@tonic-gate dstat = InUseDictionary->rollback(t->objPath);
5607c478bd9Sstevel@tonic-gate if (dstat != DB_SUCCESS) {
5617c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
5627c478bd9Sstevel@tonic-gate "%s: DB error %d rolling back \"%s\"",
5637c478bd9Sstevel@tonic-gate myself, dstat, NIL(t->objName));
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate * Had rollback succeeded, the 'table'
5667c478bd9Sstevel@tonic-gate * would have disappeared. However, since
5677c478bd9Sstevel@tonic-gate * rollback failed, we need to update the
5687c478bd9Sstevel@tonic-gate * table->mapping.enum* fields.
5697c478bd9Sstevel@tonic-gate */
5707c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->mapping.enumLock);
5717c478bd9Sstevel@tonic-gate table->mapping.enumStat = LDAP_OPERATIONS_ERROR;
5727c478bd9Sstevel@tonic-gate table->mapping.enumTid = 0;
5737c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate if (stat == 0) {
5777c478bd9Sstevel@tonic-gate if (stat2 == 0) {
5787c478bd9Sstevel@tonic-gate WRITEUNLOCK2(mindex, ((db *)mindex->getDbPtr()),
5797c478bd9Sstevel@tonic-gate LDAP_OPERATIONS_ERROR,
5807c478bd9Sstevel@tonic-gate LDAP_OPERATIONS_ERROR,
5817c478bd9Sstevel@tonic-gate "db_mindex::entriesFromLDAPreal wu",
5827c478bd9Sstevel@tonic-gate "db_mindex::entriesFromLDAPreal wu db");
5837c478bd9Sstevel@tonic-gate } else {
5847c478bd9Sstevel@tonic-gate WRITEUNLOCK(((db *)mindex->getDbPtr()),
5857c478bd9Sstevel@tonic-gate LDAP_OPERATIONS_ERROR,
5867c478bd9Sstevel@tonic-gate "db_mindex::entriesFromLDAPreal wu db");
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate unlockTransLog(myself, 1);
5907c478bd9Sstevel@tonic-gate return (LDAP_OPERATIONS_ERROR);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate stat = LDAP_SUCCESS;
5947c478bd9Sstevel@tonic-gate mindex->setNoWriteThrough();
5957c478bd9Sstevel@tonic-gate mindex->setNoLDAPquery();
5967c478bd9Sstevel@tonic-gate if (qin == 0) {
5977c478bd9Sstevel@tonic-gate table->setEnumMode(0);
5987c478bd9Sstevel@tonic-gate doEnum = TRUE;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate * If there is no non-indexed table mapping, we must filter
6027c478bd9Sstevel@tonic-gate * the enum mode (i.e., deletion candidates) array to only
6037c478bd9Sstevel@tonic-gate * contain those entries that match the indexes.
6047c478bd9Sstevel@tonic-gate */
6057c478bd9Sstevel@tonic-gate if (haveIndexedMapping(t)) {
6067c478bd9Sstevel@tonic-gate entry_object **tea = table->gettab();
6077c478bd9Sstevel@tonic-gate long i, ntea = table->getsize();
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate * Walk through the entry array, and remove any enum
6127c478bd9Sstevel@tonic-gate * array entry that _doesn't_ match the index(es).
6137c478bd9Sstevel@tonic-gate */
6147c478bd9Sstevel@tonic-gate for (i = 0; i < ntea; i++) {
6157c478bd9Sstevel@tonic-gate db_query *q;
6167c478bd9Sstevel@tonic-gate __nis_table_mapping_t **tp;
6177c478bd9Sstevel@tonic-gate int numMatches;
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate if (tea[i] == 0)
6207c478bd9Sstevel@tonic-gate continue;
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate q = pseudoEntryObj2Query(tea[i], 0, 0);
6237c478bd9Sstevel@tonic-gate if (q == 0)
6247c478bd9Sstevel@tonic-gate continue;
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate tp = selectTableMapping(t, q, 0, 0, dbId,
6277c478bd9Sstevel@tonic-gate &numMatches);
6287c478bd9Sstevel@tonic-gate if (tp == 0 || numMatches <= 0)
6297c478bd9Sstevel@tonic-gate table->enumTouch(i);
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate sfree(tp);
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate freeQuery(q);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO, "%s: %d entries from LDAP",
6387c478bd9Sstevel@tonic-gate myself, nq);
6397c478bd9Sstevel@tonic-gate } else {
6407c478bd9Sstevel@tonic-gate db_index_entry *dbie;
6417c478bd9Sstevel@tonic-gate long i, count;
6427c478bd9Sstevel@tonic-gate bool_t valid;
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate * Find the entries in the DB that currently match the
6467c478bd9Sstevel@tonic-gate * query, and add them to the enum array. Those that
6477c478bd9Sstevel@tonic-gate * remain untouched when we've processed the LDAP data
6487c478bd9Sstevel@tonic-gate * don't currently exist in LDAP, and should be deleted
6497c478bd9Sstevel@tonic-gate * from the DB.
6507c478bd9Sstevel@tonic-gate */
6517c478bd9Sstevel@tonic-gate dbie = mindex->satisfy_query_dbonly(qin, &count, FALSE, &valid);
6527c478bd9Sstevel@tonic-gate if (dbie != 0 && valid && count > 0) {
6537c478bd9Sstevel@tonic-gate table->setEnumMode(count);
6547c478bd9Sstevel@tonic-gate doEnum = TRUE;
6557c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) {
6567c478bd9Sstevel@tonic-gate table->enumSetup(dbie->getlocation(), i);
6577c478bd9Sstevel@tonic-gate dbie = dbie->getnextresult();
6587c478bd9Sstevel@tonic-gate if (dbie == 0)
6597c478bd9Sstevel@tonic-gate break;
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate } else {
6627c478bd9Sstevel@tonic-gate doEnum = FALSE;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate entry_col ec[NIS_MAXCOLUMNS+1];
6677c478bd9Sstevel@tonic-gate for (i = 0, na = 0; i < nq; i++) {
6687c478bd9Sstevel@tonic-gate entry_object eo, *e;
6697c478bd9Sstevel@tonic-gate table_col *tc;
6707c478bd9Sstevel@tonic-gate nis_object o, *to;
6717c478bd9Sstevel@tonic-gate int j, nc;
6727c478bd9Sstevel@tonic-gate db_qcomp *qc;
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate if (res[i] == 0)
6757c478bd9Sstevel@tonic-gate continue;
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
6787c478bd9Sstevel@tonic-gate printQuery(res[i], t);
6797c478bd9Sstevel@tonic-gate printObjAttr(oa[i]);
6807c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
6817c478bd9Sstevel@tonic-gate
6827c478bd9Sstevel@tonic-gate /* Assemble an object from the query and attributes */
6837c478bd9Sstevel@tonic-gate (void) memset(&o, 0, sizeof (o));
6847c478bd9Sstevel@tonic-gate if (oa[i] != 0) {
6857c478bd9Sstevel@tonic-gate o.zo_owner = oa[i]->zo_owner;
6867c478bd9Sstevel@tonic-gate o.zo_group = oa[i]->zo_group;
6877c478bd9Sstevel@tonic-gate o.zo_domain = oa[i]->zo_domain;
6887c478bd9Sstevel@tonic-gate o.zo_access = oa[i]->zo_access;
6897c478bd9Sstevel@tonic-gate o.zo_ttl = oa[i]->zo_ttl;
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate if ((to = t->obj) != 0) {
6927c478bd9Sstevel@tonic-gate o.zo_name = to->zo_name;
6937c478bd9Sstevel@tonic-gate o.zo_data.objdata_u.en_data.en_type =
6947c478bd9Sstevel@tonic-gate to->zo_data.objdata_u.ta_data.ta_type;
6957c478bd9Sstevel@tonic-gate tc = to->zo_data.objdata_u.ta_data.ta_cols.ta_cols_val;
6967c478bd9Sstevel@tonic-gate if (to->zo_data.objdata_u.ta_data.ta_cols.ta_cols_len
6977c478bd9Sstevel@tonic-gate != t->numColumns)
6987c478bd9Sstevel@tonic-gate tc = 0;
6997c478bd9Sstevel@tonic-gate if (o.zo_owner == 0)
7007c478bd9Sstevel@tonic-gate o.zo_owner = to->zo_owner;
7017c478bd9Sstevel@tonic-gate if (o.zo_group == 0)
7027c478bd9Sstevel@tonic-gate o.zo_group = to->zo_group;
7037c478bd9Sstevel@tonic-gate if (o.zo_domain == 0)
7047c478bd9Sstevel@tonic-gate o.zo_domain = to->zo_domain;
7057c478bd9Sstevel@tonic-gate if (o.zo_access == 0)
7067c478bd9Sstevel@tonic-gate o.zo_access = to->zo_access;
7077c478bd9Sstevel@tonic-gate if (o.zo_ttl == 0)
7087c478bd9Sstevel@tonic-gate o.zo_ttl = to->zo_ttl;
7097c478bd9Sstevel@tonic-gate } else {
7107c478bd9Sstevel@tonic-gate tc = 0;
711*8d0852b7SRichard Lowe o.zo_owner = (nis_name)"";
712*8d0852b7SRichard Lowe o.zo_group = (nis_name)"";
713*8d0852b7SRichard Lowe o.zo_domain = (nis_name)"";
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate o.zo_data.zo_type = NIS_ENTRY_OBJ;
7177c478bd9Sstevel@tonic-gate o.zo_data.objdata_u.en_data.en_cols.en_cols_len =
7187c478bd9Sstevel@tonic-gate t->numColumns + 1;
7197c478bd9Sstevel@tonic-gate o.zo_data.objdata_u.en_data.en_cols.en_cols_val = ec;
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate (void) memset(&ec, 0, sizeof (ec));
7227c478bd9Sstevel@tonic-gate nc = res[i]->size();
7237c478bd9Sstevel@tonic-gate qc = res[i]->queryloc();
7247c478bd9Sstevel@tonic-gate if (qc == 0) {
7257c478bd9Sstevel@tonic-gate freeQuery(res[i]);
7267c478bd9Sstevel@tonic-gate continue;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate for (j = 0; j < nc; j++) {
7297c478bd9Sstevel@tonic-gate int ic = 1+ qc[j].which_index;
7307c478bd9Sstevel@tonic-gate if (ic < 1 || ic > t->numColumns)
7317c478bd9Sstevel@tonic-gate continue;
7327c478bd9Sstevel@tonic-gate #ifdef SET_ENTRY_FLAGS
7337c478bd9Sstevel@tonic-gate if (tc != 0)
7347c478bd9Sstevel@tonic-gate ec[ic].ec_flags =
7357c478bd9Sstevel@tonic-gate entryFlagsFromTable(tc[ic-1].tc_flags);
7367c478bd9Sstevel@tonic-gate #else
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate * In theory, the entry flags should be derived
7397c478bd9Sstevel@tonic-gate * from the table flags. However, that doesn't
7407c478bd9Sstevel@tonic-gate * seem to be the way that the DB code has done
7417c478bd9Sstevel@tonic-gate * things so far, so leave the entry flags unset.
7427c478bd9Sstevel@tonic-gate */
7437c478bd9Sstevel@tonic-gate #endif /* SET_ENTRY_FLAGS */
7447c478bd9Sstevel@tonic-gate qc[j].index_value->get_value(
7457c478bd9Sstevel@tonic-gate &ec[ic].ec_value.ec_value_val,
7467c478bd9Sstevel@tonic-gate (int *)&ec[ic].ec_value.ec_value_len);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate
7497c478bd9Sstevel@tonic-gate setOid(&o);
7507c478bd9Sstevel@tonic-gate e = makePseudoEntryObj(&o, &eo, t->obj);
7517c478bd9Sstevel@tonic-gate if (e == 0) {
7527c478bd9Sstevel@tonic-gate freeQuery(res[i]);
7537c478bd9Sstevel@tonic-gate continue;
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate * 'o' is currently a pseudo-object of type entry, with
7587c478bd9Sstevel@tonic-gate * column zero used for an XDR:ed version of the entry_obj,
7597c478bd9Sstevel@tonic-gate * column one the real column zero of the entry, etc.
7607c478bd9Sstevel@tonic-gate * We now need a real NIS_ENTRY_OBJ object, so move the
7617c478bd9Sstevel@tonic-gate * entry_col array one step left.
7627c478bd9Sstevel@tonic-gate */
7637c478bd9Sstevel@tonic-gate o.zo_data.objdata_u.en_data.en_cols.en_cols_len = t->numColumns;
7647c478bd9Sstevel@tonic-gate o.zo_data.objdata_u.en_data.en_cols.en_cols_val = &ec[1];
7657c478bd9Sstevel@tonic-gate
7667c478bd9Sstevel@tonic-gate stat = mindex->updateTableEntry(e, 1, t->objName, &o, t->obj,
7677c478bd9Sstevel@tonic-gate o.zo_oid.mtime, &xid);
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate * LDAP_SUCCESS => Entry added or modified
7707c478bd9Sstevel@tonic-gate * LDAP_COMPARE_TRUE => Entry same as existing one
7717c478bd9Sstevel@tonic-gate * other => Error
7727c478bd9Sstevel@tonic-gate */
7737c478bd9Sstevel@tonic-gate if (stat == LDAP_SUCCESS) {
7747c478bd9Sstevel@tonic-gate na++;
7757c478bd9Sstevel@tonic-gate } else if (stat == LDAP_COMPARE_TRUE) {
7767c478bd9Sstevel@tonic-gate stat = LDAP_SUCCESS;
7777c478bd9Sstevel@tonic-gate } else {
7787c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
7797c478bd9Sstevel@tonic-gate "%s: Error adding entry to \"%s\": %s",
7807c478bd9Sstevel@tonic-gate myself, NIL(t->objName),
7817c478bd9Sstevel@tonic-gate ldap_err2string(stat));
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate
7847c478bd9Sstevel@tonic-gate if (e->en_cols.en_cols_val != 0)
7857c478bd9Sstevel@tonic-gate sfree(e->en_cols.en_cols_val[0].ec_value.ec_value_val);
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate freeQuery(res[i]);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate sfree(res);
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate /* Take care of deletes if we enumerated the table */
7937c478bd9Sstevel@tonic-gate if (doEnum) {
7947c478bd9Sstevel@tonic-gate ea = table->endEnumMode(&numEa);
7957c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
7967c478bd9Sstevel@tonic-gate "%s: %d entries added/updated", myself, na);
7977c478bd9Sstevel@tonic-gate nau = na;
7987c478bd9Sstevel@tonic-gate } else
7997c478bd9Sstevel@tonic-gate ea = 0;
8007c478bd9Sstevel@tonic-gate if (ea != 0) {
8017c478bd9Sstevel@tonic-gate uint32_t nowt = time(0);
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate for (i = 0; i < numEa; i++) {
8047c478bd9Sstevel@tonic-gate int st;
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate if (ea[i] == 0)
8077c478bd9Sstevel@tonic-gate continue;
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate st = mindex->updateTableEntry(ea[i], 0, t->objName, 0,
8107c478bd9Sstevel@tonic-gate t->obj, nowt, &xid);
8117c478bd9Sstevel@tonic-gate if (st == LDAP_SUCCESS) {
8127c478bd9Sstevel@tonic-gate na++;
8137c478bd9Sstevel@tonic-gate } else {
8147c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
8157c478bd9Sstevel@tonic-gate "%s: Error removing directory entry for \"%s\": %s",
8167c478bd9Sstevel@tonic-gate myself, NIL(t->objName),
8177c478bd9Sstevel@tonic-gate ldap_err2string(st));
8187c478bd9Sstevel@tonic-gate if (stat == LDAP_SUCCESS)
8197c478bd9Sstevel@tonic-gate stat = st;
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate if (stat == LDAP_SUCCESS) {
8237c478bd9Sstevel@tonic-gate struct timeval now;
8247c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, 0);
8257c478bd9Sstevel@tonic-gate table->mapping.enumExpire = now.tv_sec +
8267c478bd9Sstevel@tonic-gate table->mapping.ttl;
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate if (doEnum)
8297c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
8307c478bd9Sstevel@tonic-gate "%s: %d entries deleted", myself, na-nau);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate sfree(ea);
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate /* If we called log_action() successfully, we need to sync the log */
8367c478bd9Sstevel@tonic-gate if (na > 0)
8377c478bd9Sstevel@tonic-gate (void) ((db *)mindex->getDbPtr())->sync_log();
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate if (xid != 0 && na > 0 && stat == LDAP_SUCCESS)
8407c478bd9Sstevel@tonic-gate ret = endTransaction(xid, dirObj);
8417c478bd9Sstevel@tonic-gate else if (xid != 0)
8427c478bd9Sstevel@tonic-gate ret = abort_transaction(xid);
8437c478bd9Sstevel@tonic-gate else
8447c478bd9Sstevel@tonic-gate ret = 0;
8457c478bd9Sstevel@tonic-gate if (ret != 0) {
8467c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
8477c478bd9Sstevel@tonic-gate "%s: Error %s transaction for \"%s\"",
8487c478bd9Sstevel@tonic-gate myself, (na > 0 && stat == LDAP_SUCCESS) ?
8497c478bd9Sstevel@tonic-gate "ending" : "aborting",
8507c478bd9Sstevel@tonic-gate NIL(t->objName));
8517c478bd9Sstevel@tonic-gate stat = LDAP_OPERATIONS_ERROR;
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate mindex->clearNoLDAPquery();
8557c478bd9Sstevel@tonic-gate mindex->clearNoWriteThrough();
8567c478bd9Sstevel@tonic-gate freeObjAttr(oa, nq);
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
8597c478bd9Sstevel@tonic-gate printbuf();
8607c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate if (doEnum)
8637c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
8647c478bd9Sstevel@tonic-gate "%s: enumeration \"%s\" done", myself, NIL(t->objName));
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate if (arg->isDeferred) {
8677c478bd9Sstevel@tonic-gate /*
8687c478bd9Sstevel@tonic-gate * Rollback doesn't recover data written to disk, so
8697c478bd9Sstevel@tonic-gate * we should commit even if we're returning failure.
8707c478bd9Sstevel@tonic-gate */
8717c478bd9Sstevel@tonic-gate dstat = InUseDictionary->commit(t->objPath);
8727c478bd9Sstevel@tonic-gate if (dstat != DB_SUCCESS) {
8737c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
8747c478bd9Sstevel@tonic-gate "%s: DB error %d committing \"%s\"",
8757c478bd9Sstevel@tonic-gate myself, dstat, NIL(t->objName));
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate (void) mutex_lock(&table->mapping.enumLock);
8797c478bd9Sstevel@tonic-gate if (arg->isDeferred && dstat == DB_SUCCESS)
8807c478bd9Sstevel@tonic-gate table->mapping.enumDeferred = 0;
8817c478bd9Sstevel@tonic-gate table->mapping.enumStat = stat;
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate struct timeval end;
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate (void) gettimeofday(&end, 0);
8867c478bd9Sstevel@tonic-gate end.tv_sec -= start.tv_sec;
8877c478bd9Sstevel@tonic-gate end.tv_usec -= start.tv_usec;
8887c478bd9Sstevel@tonic-gate if (end.tv_usec < 0) {
8897c478bd9Sstevel@tonic-gate end.tv_usec += 1000000;
8907c478bd9Sstevel@tonic-gate end.tv_sec -= 1;
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate table->mapping.enumTime = 1000000*end.tv_sec + end.tv_usec;
8937c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK,
8947c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG
8957c478bd9Sstevel@tonic-gate LOG_WARNING,
8967c478bd9Sstevel@tonic-gate #else
8977c478bd9Sstevel@tonic-gate LOG_INFO,
8987c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */
8997c478bd9Sstevel@tonic-gate "%s: %d entries in %ld usec => %ld usec/entry",
9007c478bd9Sstevel@tonic-gate NIL(t->objName), table->mapping.enumEntries,
9017c478bd9Sstevel@tonic-gate table->mapping.enumTime,
9027c478bd9Sstevel@tonic-gate table->mapping.enumTime/
9037c478bd9Sstevel@tonic-gate (table->mapping.enumEntries != 0 ?
9047c478bd9Sstevel@tonic-gate table->mapping.enumEntries : 1));
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate table->mapping.enumTid = 0;
9077c478bd9Sstevel@tonic-gate (void) mutex_unlock(&table->mapping.enumLock);
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate WRITEUNLOCKNR(table, stat3, "table wu db_mindex::entriesFromLDAPreal");
9107c478bd9Sstevel@tonic-gate WRITEUNLOCKNR(mindex, stat2, "db_mindex::entriesFromLDAPreal wu");
9117c478bd9Sstevel@tonic-gate WRITEUNLOCKNR(((db *)mindex->getDbPtr()), lstat,
9127c478bd9Sstevel@tonic-gate "db db_mindex::entriesFromLDAPreal wu");
9137c478bd9Sstevel@tonic-gate unlockTransLog(myself, 1);
9147c478bd9Sstevel@tonic-gate if (stat3 != 0)
9157c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
9167c478bd9Sstevel@tonic-gate "%s: Error %d unlocking db_table", myself, stat3);
9177c478bd9Sstevel@tonic-gate if (stat2 != 0)
9187c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
9197c478bd9Sstevel@tonic-gate "%s: Error %d unlocking db_mindex", myself, stat2);
9207c478bd9Sstevel@tonic-gate if (lstat != 0)
9217c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
9227c478bd9Sstevel@tonic-gate "%s: Error %d unlocking db", myself, lstat);
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate return (stat);
9257c478bd9Sstevel@tonic-gate }
92636e852a1SRaja Andra /*
92736e852a1SRaja Andra * Sets the oid (i.e., the creation and modification times) for the
92836e852a1SRaja Andra * specified object. In order to avoid retrieving the old incarnation
92936e852a1SRaja Andra * (if any) from the DB first, we're punting and setting both mtime
93036e852a1SRaja Andra * and ctime to the current time.
93136e852a1SRaja Andra */
93236e852a1SRaja Andra static void
setOid(nis_object * obj)93336e852a1SRaja Andra setOid(nis_object *obj) {
93436e852a1SRaja Andra if (obj != 0) {
93536e852a1SRaja Andra obj->zo_oid.ctime = obj->zo_oid.mtime = time(0);
93636e852a1SRaja Andra }
93736e852a1SRaja Andra }
938