2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <time.h>
2N/A#include <sys/time.h>
2N/A#include <lber.h>
2N/A#include <ldap.h>
2N/A#include <signal.h>
2N/A#include <pthread.h>
2N/A#include "db_headers.h"
2N/A#include "db.h"
2N/A#include "db_mindex.h"
2N/A#include "db_dictionary.h"
2N/A#include "nisdb_mt.h"
2N/A#include "ldap_map.h"
2N/A#include "ldap_glob.h"
2N/A#include "ldap_util.h"
2N/A
2N/A
2N/Aextern db_dictionary *InUseDictionary;
2N/A
2N/A
2N/Aextern "C" {
2N/A
2N/Atypedef struct {
2N/A db_mindex *mindex;
2N/A __nis_table_mapping_t *t;
2N/A db_query *qin;
2N/A db_query *q;
2N/A char *dbId;
2N/A nis_object *dirObj;
2N/A int isDeferred;
2N/A char *tableName;
2N/A} __entries_from_ldap_arg_t;
2N/A
2N/Astatic void *entriesFromLDAPthread(void *);
2N/A
2N/A}
2N/A
2N/Aint entriesFromLDAPreal(__entries_from_ldap_arg_t *);
2N/A
2N/A#ifdef SET_ENTRY_FLAGS
2N/Astatic uint_t
2N/AentryFlagsFromTable(uint_t tf) {
2N/A uint_t ef = 0;
2N/A
2N/A if ((tf & TA_BINARY) != 0)
2N/A ef |= EN_BINARY;
2N/A if ((tf & TA_CRYPT) != 0)
2N/A ef |= EN_CRYPT;
2N/A if ((tf & TA_XDR) != 0)
2N/A ef |= EN_XDR;
2N/A if ((tf & TA_ASN1) != 0)
2N/A ef |= EN_ASN1;
2N/A
2N/A return (ef);
2N/A}
2N/A#endif /* SET_ENTRY_FLAGS */
2N/A
2N/Astatic void setOid(nis_object *obj);
2N/A
2N/A/*
2N/A * Retrieve container entries from LDAP per 't' and 'qin'/'q'.
2N/A * This is a helper function for db_mindex::queryLDAP(); see
2N/A * that function for details of the parameters (except doAsynch).
2N/A *
2N/A * If 'doAsynch' is set, and the retrieval is an enumeration
2N/A * (qin == NULL), the retrieval is performed in a detached
2N/A * thread. In this case, the return code just reflects the
2N/A * setup and launch of the detached thread. Retrieval will
2N/A * complete asynchronously.
2N/A */
2N/Aint
2N/Adb_mindex::entriesFromLDAP(__nis_table_mapping_t *t, db_query *qin, db_query *q,
2N/A char *dbId, nis_object *dirObj, int doAsynch) {
2N/A __entries_from_ldap_arg_t *arg;
2N/A int stat;
2N/A db_status dstat;
2N/A const char *myself = "db_mindex::entriesFromLDAP";
2N/A
2N/A arg = (__entries_from_ldap_arg_t *)am(myself, sizeof (*arg));
2N/A if (arg == 0) {
2N/A freeQuery(q);
2N/A if (dirObj != 0)
2N/A nis_destroy_object(dirObj);
2N/A return (LDAP_NO_MEMORY);
2N/A }
2N/A
2N/A arg->mindex = this;
2N/A arg->t = t;
2N/A arg->qin = qin;
2N/A arg->q = q;
2N/A arg->dbId = dbId;
2N/A arg->dirObj = dirObj;
2N/A arg->tableName = t->objName;
2N/A
2N/A /*
2N/A * Check if an enumeration thread is running; if so, then regardless
2N/A * of whether or not the current operation is an enumeration, we
2N/A * just return success, and let our caller get the data from the
2N/A * existing (deferred) DB.
2N/A */
2N/A (void) mutex_lock(&table->mapping.enumLock);
2N/A if (table->mapping.enumTid != 0) {
2N/A int doReturn = 0;
2N/A
2N/A stat = pthread_kill(table->mapping.enumTid, 0);
2N/A if (stat == ESRCH) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Enumeration thread %d not found for \"%s\"; exit status = %d (%s)",
2N/A myself, table->mapping.enumTid,
2N/A NIL(t->objName), table->mapping.enumStat,
2N/A ldap_err2string(table->mapping.enumStat));
2N/A /* Reflect the fact that no enum thread is running */
2N/A table->mapping.enumTid = 0;
2N/A table->mapping.enumStat = -1;
2N/A /* Cleanup deferred mode */
2N/A if (table->mapping.enumDeferred) {
2N/A dstat = InUseDictionary->commit(t->objPath);
2N/A if (dstat == DB_SUCCESS) {
2N/A table->mapping.enumDeferred = 0;
2N/A } else {
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: DB error %d committing \"%s\"",
2N/A myself, dstat, NIL(t->objName));
2N/A }
2N/A }
2N/A } else if (stat == 0) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: Enumeration thread %d already running for \"%s\"",
2N/A myself, table->mapping.enumTid,
2N/A NIL(t->objName));
2N/A stat = LDAP_SUCCESS;
2N/A doReturn = 1;
2N/A } else {
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: Error %d looking for enumeration thread %d for \"%s\"",
2N/A myself, stat, table->mapping.enumTid,
2N/A NIL(t->objName));
2N/A doReturn = 1;
2N/A stat = LDAP_OPERATIONS_ERROR;
2N/A }
2N/A if (doReturn) {
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A sfree(arg);
2N/A freeQuery(q);
2N/A if (dirObj != 0)
2N/A nis_destroy_object(dirObj);
2N/A return (stat);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If we're enumerating (and hence expect that retrieving all data,
2N/A * and updating the local DB, might take a while), create a deferred-
2N/A * update table that clients can use while we are updating the real
2N/A * one.
2N/A */
2N/A if (doAsynch && qin == 0) {
2N/A if ((dstat = InUseDictionary->defer(t->objPath)) ==
2N/A DB_SUCCESS) {
2N/A arg->isDeferred = 1;
2N/A table->mapping.enumDeferred = 1;
2N/A } else {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Unable to defer updates for \"%s\" (status=%d);"
2N/A " updating in place",
2N/A myself, NIL(t->objName), dstat);
2N/A arg->isDeferred = 0;
2N/A table->mapping.enumDeferred = 0;
2N/A }
2N/A } else {
2N/A arg->isDeferred = 0;
2N/A table->mapping.enumDeferred = 0;
2N/A }
2N/A
2N/A /* If enumerating, perform the operation in a separate thread */
2N/A if (doAsynch && qin == 0) {
2N/A pthread_t tid;
2N/A pthread_attr_t attr;
2N/A
2N/A (void) pthread_attr_init(&attr);
2N/A#ifdef FORCE_SYNCHRONOUS
2N/A#else
2N/A (void) pthread_attr_setdetachstate(&attr,
2N/A PTHREAD_CREATE_DETACHED);
2N/A#endif /* FORCE_SYNCHRONOUS */
2N/A stat = pthread_create(&tid, &attr, entriesFromLDAPthread, arg);
2N/A if (stat != 0) {
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Error %d creating new thread; using current one",
2N/A myself, stat);
2N/A stat = (int)entriesFromLDAPthread(arg);
2N/A return (stat);
2N/A }
2N/A
2N/A table->mapping.enumTid = tid;
2N/A table->mapping.enumStat = -1;
2N/A
2N/A /*
2N/A * We're now returning to the caller, who will get data
2N/A * from:
2N/A *
2N/A * The deferred DB, if an enumeration thread already
2N/A * was running, and deferred mode was on, or
2N/A *
2N/A * The original DB, if we just started an enumeration
2N/A * thread. In this case, our caller (several levels up)
2N/A * is holding a lock on the db_mindex/db_table, which
2N/A * means that the enum thread will have to wait for
2N/A * our caller once it's done the LDAP retrieval, and
2N/A * wants to update the DB.
2N/A */
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A stat = LDAP_SUCCESS;
2N/A#ifdef FORCE_SYNCHRONOUS
2N/A {
2N/A int tstat;
2N/A
2N/A stat = pthread_join(tid, (void **)&tstat);
2N/A if (stat == 0) {
2N/A stat = tstat;
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: thread %d => %d",
2N/A myself, tid, tstat);
2N/A } else {
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: pthread_join(%d) => %d",
2N/A myself, tid, stat);
2N/A stat = LDAP_OPERATIONS_ERROR;
2N/A }
2N/A }
2N/A#endif /* FORCE_SYNCHRONOUS */
2N/A } else {
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A stat = (int)entriesFromLDAPthread(arg);
2N/A }
2N/A
2N/A return (stat);
2N/A}
2N/A
2N/Aextern "C" {
2N/A
2N/A/*
2N/A * We use this 'extern "C"' function in order to make sure that
2N/A * pthread_create() doesn't have any problems trying to invoke a
2N/A * C++ function.
2N/A */
2N/Astatic void *
2N/AentriesFromLDAPthread(void *voidarg) {
2N/A __entries_from_ldap_arg_t *arg;
2N/A int stat;
2N/A db *dbase;
2N/A db_table_desc *tbl = 0;
2N/A char *tableName;
2N/A
2N/A arg = (__entries_from_ldap_arg_t *)voidarg;
2N/A
2N/A /* Lock to prevent removal */
2N/A (void) __nis_lock_db_table(arg->tableName, 1, 0,
2N/A "entriesFromLDAPthread");
2N/A
2N/A /*
2N/A * It's possible that the db_mindex for the table has changed,
2N/A * or disappeared, between now and the time when our parent
2N/A * thread released its lock on the table. Hence, we search the
2N/A * dictionary to re-acquire the 'db', and the db_mindex.
2N/A */
2N/A tableName = internalTableName(arg->tableName);
2N/A if (tableName != 0) {
2N/A#ifdef NISDB_LDAP_DEBUG
2N/A db_mindex *oldMindex = arg->mindex;
2N/A#endif /* NISDB_LDAP_DEBUG */
2N/A
2N/A dbase = InUseDictionary->find_table(tableName, &tbl, FALSE);
2N/A if (dbase != 0)
2N/A arg->mindex = dbase->mindex();
2N/A else
2N/A arg->mindex = 0;
2N/A#ifdef NISDB_LDAP_DEBUG
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "entriesFromLDAPthread: %s -> %s -> 0x%x (0x%x)",
2N/A NIL(arg->tableName), NIL(tableName),
2N/A arg->mindex, oldMindex);
2N/A#endif /* NISDB_LDAP_DEBUG */
2N/A sfree(tableName);
2N/A tableName = 0;
2N/A }
2N/A
2N/A stat = entriesFromLDAPreal(arg);
2N/A
2N/A (void) __nis_ulock_db_table(arg->tableName, 1, 0,
2N/A "entriesFromLDAPthread");
2N/A
2N/A freeQuery(arg->q);
2N/A if (arg->dirObj != 0)
2N/A nis_destroy_object(arg->dirObj);
2N/A sfree(arg);
2N/A return ((void *)stat);
2N/A}
2N/A
2N/A}
2N/A
2N/Aint
2N/AentriesFromLDAPreal(__entries_from_ldap_arg_t *arg) {
2N/A db_mindex *mindex;
2N/A db_table *table;
2N/A __nis_table_mapping_t *t;
2N/A db_query *q, *qin;
2N/A char *dbId;
2N/A nis_object *dirObj;
2N/A int i, na, nau, nq = 0, xid = 0;
2N/A int ret, stat = LDAP_SUCCESS, stat2, stat3;
2N/A int lstat;
2N/A __nis_obj_attr_t **oa = 0;
2N/A db_query **res;
2N/A entry_object **ea;
2N/A long numEa;
2N/A bool_t doEnum;
2N/A db_status dstat;
2N/A struct timeval start;
2N/A const char *myself =
2N/A "db_mindex::entriesFromLDAPreal";
2N/A
2N/A if (arg == 0)
2N/A return (LDAP_PARAM_ERROR);
2N/A mindex = arg->mindex;
2N/A t = arg->t;
2N/A q = arg->q;
2N/A qin = arg->qin;
2N/A dbId = arg->dbId;
2N/A dirObj = arg->dirObj;
2N/A
2N/A table = (mindex != 0) ? mindex->getTable() : 0;
2N/A
2N/A if (mindex == 0 || t == 0 || table == 0) {
2N/A /* We haven't done anything, so rollback should be OK */
2N/A if (arg->isDeferred && t != 0) {
2N/A dstat = InUseDictionary->rollback(t->objPath);
2N/A if (dstat != DB_SUCCESS) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: DB error %d rolling back \"%s\"",
2N/A myself, dstat, NIL(t->objName));
2N/A /*
2N/A * Had rollback succeeded, the 'table'
2N/A * would have disappeared. However, since
2N/A * rollback failed, we need to update the
2N/A * table->mapping.enum* fields.
2N/A */
2N/A if (table != 0) {
2N/A (void) mutex_lock(&table->
2N/A mapping.enumLock);
2N/A table->mapping.enumStat =
2N/A LDAP_PARAM_ERROR;
2N/A table->mapping.enumTime = 0;
2N/A table->mapping.enumEntries = 0;
2N/A table->mapping.enumTid = 0;
2N/A (void) mutex_unlock(&table->
2N/A mapping.enumLock);
2N/A }
2N/A }
2N/A }
2N/A return (LDAP_PARAM_ERROR);
2N/A }
2N/A
2N/A if (qin == 0)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO, "%s: enumerating \"%s%s%s\"",
2N/A myself, dbId ? dbId : "", dbId ? ":" : "",
2N/A NIL(t->objName));
2N/A
2N/A (void) gettimeofday(&start, 0);
2N/A
2N/A /* Getting table entries */
2N/A res = mapFromLDAP(t, q, &nq, dbId, &stat, &oa);
2N/A#ifdef NISDB_LDAP_DEBUG
2N/A logmsg(MSG_ALWAYS, LOG_INFO,
2N/A "%s: mapFromLDAP() => 0x%x, status=%d %s; nq = %d",
2N/A myself, res, stat, stat == LDAP_SUCCESS ? "" :
2N/A ldap_err2string(stat), nq);
2N/A#endif /* NISDB_LDAP_DEBUG */
2N/A
2N/A /*
2N/A * Keep track of the number of NIS+ entries we got back;
2N/A * note that the number of LDAP entries may have been
2N/A * smaller or larger.
2N/A */
2N/A (void) mutex_lock(&table->mapping.enumLock);
2N/A table->mapping.enumEntries = nq;
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A
2N/A /*
2N/A * If we get LDAP_NO_SUCH_OBJECT, we need to delete the entries
2N/A * in the table, so we can't just return.
2N/A */
2N/A if (res == 0 && stat != LDAP_NO_SUCH_OBJECT) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: mapFromLDAP() => 0x0, status=%d (%s)",
2N/A myself, stat, ldap_err2string(stat));
2N/A if (arg->isDeferred) {
2N/A dstat = InUseDictionary->rollback(t->objPath);
2N/A if (dstat != DB_SUCCESS) {
2N/A struct timeval end;
2N/A
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: DB error %d rolling back \"%s\"",
2N/A myself, dstat, NIL(t->objName));
2N/A /*
2N/A * Had rollback succeeded, the 'table'
2N/A * would have disappeared. However, since
2N/A * rollback failed, we need to update the
2N/A * table->mapping.enum* fields.
2N/A */
2N/A (void) mutex_lock(&table->mapping.enumLock);
2N/A table->mapping.enumStat = stat;
2N/A (void) gettimeofday(&end, 0);
2N/A end.tv_sec -= start.tv_sec;
2N/A end.tv_usec -= start.tv_usec;
2N/A if (end.tv_usec < 0) {
2N/A end.tv_usec += 1000000;
2N/A end.tv_sec -= 1;
2N/A }
2N/A table->mapping.enumTime =
2N/A 1000000*end.tv_sec + end.tv_usec;
2N/A table->mapping.enumTid = 0;
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A }
2N/A }
2N/A return (stat);
2N/A }
2N/A
2N/A /*
2N/A * Need to disable write-through to LDAP, for which we need a lock
2N/A * on our db_mindex ('mindex'); we're also updating the table, so
2N/A * we need a write lock on that as well. However, before locking the
2N/A * mindex, we need to maintain lock integrity by acquiring the
2N/A * trans log lock. Note that actually beginning a transaction is
2N/A * expensive, so we defer that until we know that we really need
2N/A * to update.
2N/A */
2N/A lstat = lockTransLog(myself, 1, 1);
2N/A if (lstat != 0) {
2N/A if (lstat == EBUSY)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: transaction log busy; no LDAP update for \"%s\"",
2N/A myself, NIL(t->objName));
2N/A else
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: Error %d locking transaction log; no LDAP update for \"%s\"",
2N/A myself, lstat, NIL(t->objName));
2N/A if (arg->isDeferred) {
2N/A dstat = InUseDictionary->rollback(t->objPath);
2N/A if (dstat != DB_SUCCESS) {
2N/A struct timeval end;
2N/A
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: DB error %d rolling back \"%s\"",
2N/A myself, dstat, NIL(t->objName));
2N/A /*
2N/A * Had rollback succeeded, the 'table'
2N/A * would have disappeared. However, since
2N/A * rollback failed, we need to update the
2N/A * table->mapping.enum* fields.
2N/A */
2N/A (void) mutex_lock(&table->mapping.enumLock);
2N/A table->mapping.enumStat = LDAP_OPERATIONS_ERROR;
2N/A (void) gettimeofday(&end, 0);
2N/A end.tv_sec -= start.tv_sec;
2N/A end.tv_usec -= start.tv_usec;
2N/A if (end.tv_usec < 0) {
2N/A end.tv_usec += 1000000;
2N/A end.tv_sec -= 1;
2N/A }
2N/A table->mapping.enumTime = 1000000*end.tv_sec +
2N/A end.tv_usec;
2N/A table->mapping.enumTid = 0;
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A }
2N/A }
2N/A return (LDAP_OPERATIONS_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * If we have any updates, we'll call db::sync_log, which write-
2N/A * locks the 'db' instance. In order to avoid a dead-lock with
2N/A * threads performing a DB lookup (which will lock the 'db' and
2N/A * then the 'db_mindex'), we need hence need to lock in the
2N/A * following order:
2N/A *
2N/A * trans.log (already holding that one)
2N/A * db
2N/A * db_mindex
2N/A * db_table
2N/A */
2N/A TRYWRITELOCK(((db *)mindex->getDbPtr()), stat,
2N/A "w db db_mindex::entriesFromLDAPreal");
2N/A if (stat == 0) {
2N/A TRYWRITELOCK(mindex, stat2, "w db_mindex::entriesFromLDAPreal");
2N/A if (stat2 == 0) {
2N/A TRYWRITELOCK(table, stat3,
2N/A "table w db_mindex::entriesFromLDAPreal");
2N/A }
2N/A }
2N/A
2N/A if (stat != 0 || stat2 != 0 || stat3 != 0) {
2N/A if (stat != 0) {
2N/A if (stat == EBUSY)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: 'db' busy; no LDAP update for \"%s\"",
2N/A myself, NIL(t->objName));
2N/A else
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: 'db' lock error %d; no LDAP update for \"%s\"",
2N/A myself, stat, NIL(t->objName));
2N/A } else if (stat2 != 0) {
2N/A if (stat2 == EBUSY)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: 'db_mindex' busy; no LDAP update for \"%s\"",
2N/A myself, NIL(t->objName));
2N/A else
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: 'db_mindex' lock error %d; no LDAP update for \"%s\"",
2N/A myself, stat2, NIL(t->objName));
2N/A } else {
2N/A if (stat3 == EBUSY)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: 'db_table' busy; no LDAP update for \"%s\"",
2N/A myself, NIL(t->objName));
2N/A else
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: 'db_table' lock error %d; no LDAP update for \"%s\"",
2N/A myself, stat3, NIL(t->objName));
2N/A }
2N/A freeQueries(res, nq);
2N/A if (arg->isDeferred) {
2N/A dstat = InUseDictionary->rollback(t->objPath);
2N/A if (dstat != DB_SUCCESS) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: DB error %d rolling back \"%s\"",
2N/A myself, dstat, NIL(t->objName));
2N/A /*
2N/A * Had rollback succeeded, the 'table'
2N/A * would have disappeared. However, since
2N/A * rollback failed, we need to update the
2N/A * table->mapping.enum* fields.
2N/A */
2N/A (void) mutex_lock(&table->mapping.enumLock);
2N/A table->mapping.enumStat = LDAP_OPERATIONS_ERROR;
2N/A table->mapping.enumTid = 0;
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A }
2N/A }
2N/A if (stat == 0) {
2N/A if (stat2 == 0) {
2N/A WRITEUNLOCK2(mindex, ((db *)mindex->getDbPtr()),
2N/A LDAP_OPERATIONS_ERROR,
2N/A LDAP_OPERATIONS_ERROR,
2N/A "db_mindex::entriesFromLDAPreal wu",
2N/A "db_mindex::entriesFromLDAPreal wu db");
2N/A } else {
2N/A WRITEUNLOCK(((db *)mindex->getDbPtr()),
2N/A LDAP_OPERATIONS_ERROR,
2N/A "db_mindex::entriesFromLDAPreal wu db");
2N/A }
2N/A }
2N/A unlockTransLog(myself, 1);
2N/A return (LDAP_OPERATIONS_ERROR);
2N/A }
2N/A
2N/A stat = LDAP_SUCCESS;
2N/A mindex->setNoWriteThrough();
2N/A mindex->setNoLDAPquery();
2N/A if (qin == 0) {
2N/A table->setEnumMode(0);
2N/A doEnum = TRUE;
2N/A
2N/A /*
2N/A * If there is no non-indexed table mapping, we must filter
2N/A * the enum mode (i.e., deletion candidates) array to only
2N/A * contain those entries that match the indexes.
2N/A */
2N/A if (haveIndexedMapping(t)) {
2N/A entry_object **tea = table->gettab();
2N/A long i, ntea = table->getsize();
2N/A
2N/A
2N/A /*
2N/A * Walk through the entry array, and remove any enum
2N/A * array entry that _doesn't_ match the index(es).
2N/A */
2N/A for (i = 0; i < ntea; i++) {
2N/A db_query *q;
2N/A __nis_table_mapping_t **tp;
2N/A int numMatches;
2N/A
2N/A if (tea[i] == 0)
2N/A continue;
2N/A
2N/A q = pseudoEntryObj2Query(tea[i], 0, 0);
2N/A if (q == 0)
2N/A continue;
2N/A
2N/A tp = selectTableMapping(t, q, 0, 0, dbId,
2N/A &numMatches);
2N/A if (tp == 0 || numMatches <= 0)
2N/A table->enumTouch(i);
2N/A
2N/A sfree(tp);
2N/A
2N/A freeQuery(q);
2N/A }
2N/A }
2N/A
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO, "%s: %d entries from LDAP",
2N/A myself, nq);
2N/A } else {
2N/A db_index_entry *dbie;
2N/A long i, count;
2N/A bool_t valid;
2N/A
2N/A /*
2N/A * Find the entries in the DB that currently match the
2N/A * query, and add them to the enum array. Those that
2N/A * remain untouched when we've processed the LDAP data
2N/A * don't currently exist in LDAP, and should be deleted
2N/A * from the DB.
2N/A */
2N/A dbie = mindex->satisfy_query_dbonly(qin, &count, FALSE, &valid);
2N/A if (dbie != 0 && valid && count > 0) {
2N/A table->setEnumMode(count);
2N/A doEnum = TRUE;
2N/A for (i = 0; i < count; i++) {
2N/A table->enumSetup(dbie->getlocation(), i);
2N/A dbie = dbie->getnextresult();
2N/A if (dbie == 0)
2N/A break;
2N/A }
2N/A } else {
2N/A doEnum = FALSE;
2N/A }
2N/A }
2N/A
2N/A entry_col ec[NIS_MAXCOLUMNS+1];
2N/A for (i = 0, na = 0; i < nq; i++) {
2N/A entry_object eo, *e;
2N/A table_col *tc;
2N/A nis_object o, *to;
2N/A int j, nc;
2N/A db_qcomp *qc;
2N/A
2N/A if (res[i] == 0)
2N/A continue;
2N/A
2N/A#ifdef NISDB_LDAP_DEBUG
2N/A printQuery(res[i], t);
2N/A printObjAttr(oa[i]);
2N/A#endif /* NISDB_LDAP_DEBUG */
2N/A
2N/A /* Assemble an object from the query and attributes */
2N/A (void) memset(&o, 0, sizeof (o));
2N/A if (oa[i] != 0) {
2N/A o.zo_owner = oa[i]->zo_owner;
2N/A o.zo_group = oa[i]->zo_group;
2N/A o.zo_domain = oa[i]->zo_domain;
2N/A o.zo_access = oa[i]->zo_access;
2N/A o.zo_ttl = oa[i]->zo_ttl;
2N/A }
2N/A if ((to = t->obj) != 0) {
2N/A o.zo_name = to->zo_name;
2N/A o.zo_data.objdata_u.en_data.en_type =
2N/A to->zo_data.objdata_u.ta_data.ta_type;
2N/A tc = to->zo_data.objdata_u.ta_data.ta_cols.ta_cols_val;
2N/A if (to->zo_data.objdata_u.ta_data.ta_cols.ta_cols_len
2N/A != t->numColumns)
2N/A tc = 0;
2N/A if (o.zo_owner == 0)
2N/A o.zo_owner = to->zo_owner;
2N/A if (o.zo_group == 0)
2N/A o.zo_group = to->zo_group;
2N/A if (o.zo_domain == 0)
2N/A o.zo_domain = to->zo_domain;
2N/A if (o.zo_access == 0)
2N/A o.zo_access = to->zo_access;
2N/A if (o.zo_ttl == 0)
2N/A o.zo_ttl = to->zo_ttl;
2N/A } else {
2N/A tc = 0;
2N/A o.zo_owner = (char *)"";
2N/A o.zo_group = (char *)"";
2N/A o.zo_domain = (char *)"";
2N/A }
2N/A
2N/A o.zo_data.zo_type = NIS_ENTRY_OBJ;
2N/A o.zo_data.objdata_u.en_data.en_cols.en_cols_len =
2N/A t->numColumns + 1;
2N/A o.zo_data.objdata_u.en_data.en_cols.en_cols_val = ec;
2N/A
2N/A (void) memset(&ec, 0, sizeof (ec));
2N/A nc = res[i]->size();
2N/A qc = res[i]->queryloc();
2N/A if (qc == 0) {
2N/A freeQuery(res[i]);
2N/A continue;
2N/A }
2N/A for (j = 0; j < nc; j++) {
2N/A int ic = 1+ qc[j].which_index;
2N/A if (ic < 1 || ic > t->numColumns)
2N/A continue;
2N/A#ifdef SET_ENTRY_FLAGS
2N/A if (tc != 0)
2N/A ec[ic].ec_flags =
2N/A entryFlagsFromTable(tc[ic-1].tc_flags);
2N/A#else
2N/A /*
2N/A * In theory, the entry flags should be derived
2N/A * from the table flags. However, that doesn't
2N/A * seem to be the way that the DB code has done
2N/A * things so far, so leave the entry flags unset.
2N/A */
2N/A#endif /* SET_ENTRY_FLAGS */
2N/A qc[j].index_value->get_value(
2N/A &ec[ic].ec_value.ec_value_val,
2N/A (int *)&ec[ic].ec_value.ec_value_len);
2N/A }
2N/A
2N/A setOid(&o);
2N/A e = makePseudoEntryObj(&o, &eo, t->obj);
2N/A if (e == 0) {
2N/A freeQuery(res[i]);
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * 'o' is currently a pseudo-object of type entry, with
2N/A * column zero used for an XDR:ed version of the entry_obj,
2N/A * column one the real column zero of the entry, etc.
2N/A * We now need a real NIS_ENTRY_OBJ object, so move the
2N/A * entry_col array one step left.
2N/A */
2N/A o.zo_data.objdata_u.en_data.en_cols.en_cols_len = t->numColumns;
2N/A o.zo_data.objdata_u.en_data.en_cols.en_cols_val = &ec[1];
2N/A
2N/A stat = mindex->updateTableEntry(e, 1, t->objName, &o, t->obj,
2N/A o.zo_oid.mtime, &xid);
2N/A /*
2N/A * LDAP_SUCCESS => Entry added or modified
2N/A * LDAP_COMPARE_TRUE => Entry same as existing one
2N/A * other => Error
2N/A */
2N/A if (stat == LDAP_SUCCESS) {
2N/A na++;
2N/A } else if (stat == LDAP_COMPARE_TRUE) {
2N/A stat = LDAP_SUCCESS;
2N/A } else {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Error adding entry to \"%s\": %s",
2N/A myself, NIL(t->objName),
2N/A ldap_err2string(stat));
2N/A }
2N/A
2N/A if (e->en_cols.en_cols_val != 0)
2N/A sfree(e->en_cols.en_cols_val[0].ec_value.ec_value_val);
2N/A
2N/A freeQuery(res[i]);
2N/A }
2N/A
2N/A sfree(res);
2N/A
2N/A /* Take care of deletes if we enumerated the table */
2N/A if (doEnum) {
2N/A ea = table->endEnumMode(&numEa);
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: %d entries added/updated", myself, na);
2N/A nau = na;
2N/A } else
2N/A ea = 0;
2N/A if (ea != 0) {
2N/A uint32_t nowt = time(0);
2N/A
2N/A for (i = 0; i < numEa; i++) {
2N/A int st;
2N/A
2N/A if (ea[i] == 0)
2N/A continue;
2N/A
2N/A st = mindex->updateTableEntry(ea[i], 0, t->objName, 0,
2N/A t->obj, nowt, &xid);
2N/A if (st == LDAP_SUCCESS) {
2N/A na++;
2N/A } else {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Error removing directory entry for \"%s\": %s",
2N/A myself, NIL(t->objName),
2N/A ldap_err2string(st));
2N/A if (stat == LDAP_SUCCESS)
2N/A stat = st;
2N/A }
2N/A }
2N/A if (stat == LDAP_SUCCESS) {
2N/A struct timeval now;
2N/A (void) gettimeofday(&now, 0);
2N/A table->mapping.enumExpire = now.tv_sec +
2N/A table->mapping.ttl;
2N/A }
2N/A if (doEnum)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: %d entries deleted", myself, na-nau);
2N/A }
2N/A
2N/A sfree(ea);
2N/A
2N/A /* If we called log_action() successfully, we need to sync the log */
2N/A if (na > 0)
2N/A (void) ((db *)mindex->getDbPtr())->sync_log();
2N/A
2N/A if (xid != 0 && na > 0 && stat == LDAP_SUCCESS)
2N/A ret = endTransaction(xid, dirObj);
2N/A else if (xid != 0)
2N/A ret = abort_transaction(xid);
2N/A else
2N/A ret = 0;
2N/A if (ret != 0) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_ERR,
2N/A "%s: Error %s transaction for \"%s\"",
2N/A myself, (na > 0 && stat == LDAP_SUCCESS) ?
2N/A "ending" : "aborting",
2N/A NIL(t->objName));
2N/A stat = LDAP_OPERATIONS_ERROR;
2N/A }
2N/A
2N/A mindex->clearNoLDAPquery();
2N/A mindex->clearNoWriteThrough();
2N/A freeObjAttr(oa, nq);
2N/A
2N/A#ifdef NISDB_LDAP_DEBUG
2N/A printbuf();
2N/A#endif /* NISDB_LDAP_DEBUG */
2N/A
2N/A if (doEnum)
2N/A logmsg(MSG_NOTIMECHECK, LOG_INFO,
2N/A "%s: enumeration \"%s\" done", myself, NIL(t->objName));
2N/A
2N/A if (arg->isDeferred) {
2N/A /*
2N/A * Rollback doesn't recover data written to disk, so
2N/A * we should commit even if we're returning failure.
2N/A */
2N/A dstat = InUseDictionary->commit(t->objPath);
2N/A if (dstat != DB_SUCCESS) {
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: DB error %d committing \"%s\"",
2N/A myself, dstat, NIL(t->objName));
2N/A }
2N/A }
2N/A (void) mutex_lock(&table->mapping.enumLock);
2N/A if (arg->isDeferred && dstat == DB_SUCCESS)
2N/A table->mapping.enumDeferred = 0;
2N/A table->mapping.enumStat = stat;
2N/A {
2N/A struct timeval end;
2N/A
2N/A (void) gettimeofday(&end, 0);
2N/A end.tv_sec -= start.tv_sec;
2N/A end.tv_usec -= start.tv_usec;
2N/A if (end.tv_usec < 0) {
2N/A end.tv_usec += 1000000;
2N/A end.tv_sec -= 1;
2N/A }
2N/A table->mapping.enumTime = 1000000*end.tv_sec + end.tv_usec;
2N/A logmsg(MSG_NOTIMECHECK,
2N/A#ifdef NISDB_LDAP_DEBUG
2N/A LOG_WARNING,
2N/A#else
2N/A LOG_INFO,
2N/A#endif /* NISDB_LDAP_DEBUG */
2N/A "%s: %d entries in %ld usec => %ld usec/entry",
2N/A NIL(t->objName), table->mapping.enumEntries,
2N/A table->mapping.enumTime,
2N/A table->mapping.enumTime/
2N/A (table->mapping.enumEntries != 0 ?
2N/A table->mapping.enumEntries : 1));
2N/A }
2N/A table->mapping.enumTid = 0;
2N/A (void) mutex_unlock(&table->mapping.enumLock);
2N/A
2N/A WRITEUNLOCKNR(table, stat3, "table wu db_mindex::entriesFromLDAPreal");
2N/A WRITEUNLOCKNR(mindex, stat2, "db_mindex::entriesFromLDAPreal wu");
2N/A WRITEUNLOCKNR(((db *)mindex->getDbPtr()), lstat,
2N/A "db db_mindex::entriesFromLDAPreal wu");
2N/A unlockTransLog(myself, 1);
2N/A if (stat3 != 0)
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Error %d unlocking db_table", myself, stat3);
2N/A if (stat2 != 0)
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Error %d unlocking db_mindex", myself, stat2);
2N/A if (lstat != 0)
2N/A logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2N/A "%s: Error %d unlocking db", myself, lstat);
2N/A
2N/A return (stat);
2N/A}
2N/A/*
2N/A * Sets the oid (i.e., the creation and modification times) for the
2N/A * specified object. In order to avoid retrieving the old incarnation
2N/A * (if any) from the DB first, we're punting and setting both mtime
2N/A * and ctime to the current time.
2N/A */
2N/Astatic void
2N/AsetOid(nis_object *obj) {
2N/A if (obj != 0) {
2N/A obj->zo_oid.ctime = obj->zo_oid.mtime = time(0);
2N/A }
2N/A}