]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2entry.c
Happy New Year
[openldap] / servers / slapd / back-bdb / dn2entry.c
1 /* dn2entry.c - routines to deal with the dn2id / id2entry glue */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2018 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 /*
25  * dn2entry - look up dn in the cache/indexes and return the corresponding
26  * entry. If the requested DN is not found and matched is TRUE, return info
27  * for the closest ancestor of the DN. Otherwise e is NULL.
28  */
29
30 int
31 bdb_dn2entry(
32         Operation *op,
33         DB_TXN *tid,
34         struct berval *dn,
35         EntryInfo **e,
36         int matched,
37         DB_LOCK *lock )
38 {
39         EntryInfo *ei = NULL;
40         int rc, rc2;
41
42         Debug(LDAP_DEBUG_TRACE, "bdb_dn2entry(\"%s\")\n",
43                 dn->bv_val, 0, 0 );
44
45         *e = NULL;
46
47         rc = bdb_cache_find_ndn( op, tid, dn, &ei );
48         if ( rc ) {
49                 if ( matched && rc == DB_NOTFOUND ) {
50                         /* Set the return value, whether we have its entry
51                          * or not.
52                          */
53                         *e = ei;
54                         if ( ei && ei->bei_id ) {
55                                 rc2 = bdb_cache_find_id( op, tid, ei->bei_id,
56                                         &ei, ID_LOCKED, lock );
57                                 if ( rc2 ) rc = rc2;
58                         } else if ( ei ) {
59                                 bdb_cache_entryinfo_unlock( ei );
60                                 memset( lock, 0, sizeof( *lock ));
61                                 lock->mode = DB_LOCK_NG;
62                         }
63                 } else if ( ei ) {
64                         bdb_cache_entryinfo_unlock( ei );
65                 }
66         } else {
67                 rc = bdb_cache_find_id( op, tid, ei->bei_id, &ei, ID_LOCKED,
68                         lock );
69                 if ( rc == 0 ) {
70                         *e = ei;
71                 } else if ( matched && rc == DB_NOTFOUND ) {
72                         /* always return EntryInfo */
73                         if ( ei->bei_parent ) {
74                                 ei = ei->bei_parent;
75                                 rc2 = bdb_cache_find_id( op, tid, ei->bei_id, &ei, 0,
76                                         lock );
77                                 if ( rc2 ) rc = rc2;
78                         }
79                         *e = ei;
80                 }
81         }
82
83         return rc;
84 }