Back to index

openldap  2.4.31
dn2entry.c
Go to the documentation of this file.
00001 /* dn2entry.c - routines to deal with the dn2id / id2entry glue */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2000-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 #include <ac/string.h>
00021 
00022 #include "back-bdb.h"
00023 
00024 /*
00025  * dn2entry - look up dn in the cache/indexes and return the corresponding
00026  * entry. If the requested DN is not found and matched is TRUE, return info
00027  * for the closest ancestor of the DN. Otherwise e is NULL.
00028  */
00029 
00030 int
00031 bdb_dn2entry(
00032        Operation *op,
00033        DB_TXN *tid,
00034        struct berval *dn,
00035        EntryInfo **e,
00036        int matched,
00037        DB_LOCK *lock )
00038 {
00039        EntryInfo *ei = NULL;
00040        int rc, rc2;
00041 
00042        Debug(LDAP_DEBUG_TRACE, "bdb_dn2entry(\"%s\")\n",
00043               dn->bv_val, 0, 0 );
00044 
00045        *e = NULL;
00046 
00047        rc = bdb_cache_find_ndn( op, tid, dn, &ei );
00048        if ( rc ) {
00049               if ( matched && rc == DB_NOTFOUND ) {
00050                      /* Set the return value, whether we have its entry
00051                       * or not.
00052                       */
00053                      *e = ei;
00054                      if ( ei && ei->bei_id ) {
00055                             rc2 = bdb_cache_find_id( op, tid, ei->bei_id,
00056                                    &ei, ID_LOCKED, lock );
00057                             if ( rc2 ) rc = rc2;
00058                      } else if ( ei ) {
00059                             bdb_cache_entryinfo_unlock( ei );
00060                             memset( lock, 0, sizeof( *lock ));
00061                             lock->mode = DB_LOCK_NG;
00062                      }
00063               } else if ( ei ) {
00064                      bdb_cache_entryinfo_unlock( ei );
00065               }
00066        } else {
00067               rc = bdb_cache_find_id( op, tid, ei->bei_id, &ei, ID_LOCKED,
00068                      lock );
00069               if ( rc == 0 ) {
00070                      *e = ei;
00071               } else if ( matched && rc == DB_NOTFOUND ) {
00072                      /* always return EntryInfo */
00073                      if ( ei->bei_parent ) {
00074                             ei = ei->bei_parent;
00075                             rc2 = bdb_cache_find_id( op, tid, ei->bei_id, &ei, 0,
00076                                    lock );
00077                             if ( rc2 ) rc = rc2;
00078                      }
00079                      *e = ei;
00080               }
00081        }
00082 
00083        return rc;
00084 }