]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/id2entry.c
ITS#2449, broken NOT filters
[openldap] / servers / slapd / back-bdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int bdb_id2entry_put(
16         BackendDB *be,
17         DB_TXN *tid,
18         Entry *e,
19         int flag )
20 {
21         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
22         DB *db = bdb->bi_id2entry->bdi_db;
23         DBT key, data;
24         struct berval bv;
25         int rc;
26 #ifdef BDB_HIER
27         struct berval odn, ondn;
28
29         /* We only store rdns, and they go in the id2parent database. */
30
31         odn = e->e_name; ondn = e->e_nname;
32
33         e->e_name = slap_empty_bv;
34         e->e_nname = slap_empty_bv;
35 #endif
36         DBTzero( &key );
37         key.data = (char *) &e->e_id;
38         key.size = sizeof(ID);
39
40         rc = entry_encode( e, &bv );
41 #ifdef BDB_HIER
42         e->e_name = odn; e->e_nname = ondn;
43 #endif
44         if( rc != LDAP_SUCCESS ) {
45                 return -1;
46         }
47
48         DBTzero( &data );
49         bv2DBT( &bv, &data );
50
51         rc = db->put( db, tid, &key, &data, flag );
52
53         free( bv.bv_val );
54         return rc;
55 }
56
57 /*
58  * This routine adds (or updates) an entry on disk.
59  * The cache should be already be updated.
60  */
61
62
63 int bdb_id2entry_add(
64         BackendDB *be,
65         DB_TXN *tid,
66         Entry *e )
67 {
68         return bdb_id2entry_put(be, tid, e, DB_NOOVERWRITE);
69 }
70
71 int bdb_id2entry_update(
72         BackendDB *be,
73         DB_TXN *tid,
74         Entry *e )
75 {
76         return bdb_id2entry_put(be, tid, e, 0);
77 }
78
79 int bdb_id2entry(
80         BackendDB *be,
81         DB_TXN *tid,
82         ID id,
83         Entry **e )
84 {
85         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
86         DB *db = bdb->bi_id2entry->bdi_db;
87         DBT key, data;
88         struct berval bv;
89         int rc = 0, ret = 0;
90
91         *e = NULL;
92
93         DBTzero( &key );
94         key.data = (char *) &id;
95         key.size = sizeof(ID);
96
97         DBTzero( &data );
98         data.flags = DB_DBT_MALLOC;
99
100         /* fetch it */
101         rc = db->get( db, tid, &key, &data, bdb->bi_db_opflags );
102
103         if( rc != 0 ) {
104                 return rc;
105         }
106
107         DBT2bv( &data, &bv );
108
109         rc = entry_decode( &bv, e );
110
111         if( rc == 0 ) {
112                 (*e)->e_id = id;
113         } else {
114                 /* only free on error. On success, the entry was
115                  * decoded in place.
116                  */
117                 ch_free( data.data );
118         }
119
120         if ( rc == 0 ) {
121 #ifdef BDB_HIER
122                 bdb_fix_dn(be, id, *e);
123 #endif
124         }
125
126         return rc;
127 }
128
129 int bdb_id2entry_delete(
130         BackendDB *be,
131         DB_TXN *tid,
132         Entry *e )
133 {
134         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
135         DB *db = bdb->bi_id2entry->bdi_db;
136         DBT key;
137         int rc;
138
139         DBTzero( &key );
140         key.data = (char *) &e->e_id;
141         key.size = sizeof(ID);
142
143         /* delete from database */
144         rc = db->del( db, tid, &key, 0 );
145
146         return rc;
147 }
148
149 int bdb_entry_return(
150         Entry *e )
151 {
152         /* Our entries are allocated in two blocks; the data comes from
153          * the db itself and the Entry structure and associated pointers
154          * are allocated in entry_decode. The db data pointer is saved
155          * in e_bv. Since the Entry structure is allocated as a single
156          * block, e_attrs is always a fixed offset from e. The exception
157          * is when an entry has been modified, in which case we also need
158          * to free e_attrs.
159          */
160         if( !e->e_bv.bv_val ) { /* A regular entry, from do_add */
161                 entry_free( e );
162                 return 0;
163         }
164         if( (void *) e->e_attrs != (void *) (e+1)) {
165                 attrs_free( e->e_attrs );
166         }
167
168 #ifndef BDB_HIER
169         /* See if the DNs were changed by modrdn */
170         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
171                 e->e_bv.bv_val + e->e_bv.bv_len ) {
172                 ch_free(e->e_name.bv_val);
173                 ch_free(e->e_nname.bv_val);
174                 e->e_name.bv_val = NULL;
175                 e->e_nname.bv_val = NULL;
176         }
177 #else
178         /* We had to construct the dn and ndn as well, in a single block */
179         if( e->e_name.bv_val ) {
180                 free( e->e_name.bv_val );
181         }
182 #endif
183         /* In tool mode the e_bv buffer is realloc'd, leave it alone */
184         if( !(slapMode & SLAP_TOOL_MODE) ) {
185                 free( e->e_bv.bv_val );
186         }
187
188         free( e );
189
190         return 0;
191 }
192
193 int bdb_entry_release(
194         Operation *o,
195         Entry *e,
196         int rw )
197 {
198         struct bdb_info *bdb = (struct bdb_info *) o->o_bd->be_private;
199         struct bdb_op_info *boi = NULL;
200  
201         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
202                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
203  
204         if ( slapMode == SLAP_SERVER_MODE ) {
205                 /* free entry and reader or writer lock */
206                 if ( o ) {
207                         boi = (struct bdb_op_info *)o->o_private;
208                 }
209                 /* lock is freed with txn */
210                 if ( !boi || boi->boi_txn ) {
211                         bdb_unlocked_cache_return_entry_rw( &bdb->bi_cache, e, rw );
212                 } else {
213                         bdb_cache_return_entry_rw( bdb->bi_dbenv, &bdb->bi_cache, e, rw, &boi->boi_lock );
214                         sl_free( boi, o->o_tmpmemctx );
215                         o->o_private = NULL;
216                 }
217         } else {
218                 if (e->e_private != NULL)
219                         free (e->e_private);
220                 e->e_private = NULL;
221                 bdb_entry_return ( e );
222         }
223  
224         return 0;
225 }
226
227 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
228  */
229 int bdb_entry_get(
230         Operation *op,
231         struct berval *ndn,
232         ObjectClass *oc,
233         AttributeDescription *at,
234         int rw,
235         Entry **ent )
236 {
237         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
238         struct bdb_op_info *boi = NULL;
239         DB_TXN *txn = NULL;
240         Entry *e;
241         EntryInfo *ei;
242         int     rc;
243         const char *at_name = at->ad_cname.bv_val;
244
245         u_int32_t       locker = 0;
246         DB_LOCK         lock;
247         int             free_lock_id = 0;
248
249 #ifdef NEW_LOGGING
250         LDAP_LOG( BACK_BDB, ARGS, 
251                 "bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 );
252         LDAP_LOG( BACK_BDB, ARGS, 
253                 "bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
254                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
255 #else
256         Debug( LDAP_DEBUG_ARGS,
257                 "=> bdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
258         Debug( LDAP_DEBUG_ARGS,
259                 "=> bdb_entry_get: oc: \"%s\", at: \"%s\"\n",
260                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
261 #endif
262
263         if( op ) boi = (struct bdb_op_info *) op->o_private;
264         if( boi != NULL && op->o_bd == boi->boi_bdb ) {
265                 txn = boi->boi_txn;
266                 locker = boi->boi_locker;
267         }
268
269         if ( txn != NULL ) {
270                 locker = TXN_ID ( txn );
271         } else if ( !locker ) {
272                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
273                 free_lock_id = 1;
274                 switch(rc) {
275                 case 0:
276                         break;
277                 default:
278                         return LDAP_OTHER;
279                 }
280         }
281
282 dn2entry_retry:
283         /* can we find entry */
284         rc = bdb_dn2entry( op->o_bd, txn, ndn, &ei, 0, locker, &lock, op->o_tmpmemctx );
285         switch( rc ) {
286         case DB_NOTFOUND:
287         case 0:
288                 break;
289         case DB_LOCK_DEADLOCK:
290         case DB_LOCK_NOTGRANTED:
291                 /* the txn must abort and retry */
292                 if ( txn ) {
293                         boi->boi_err = rc;
294                         return LDAP_BUSY;
295                 }
296                 ldap_pvt_thread_yield();
297                 goto dn2entry_retry;
298         default:
299                 boi->boi_err = rc;
300                 if ( free_lock_id ) {
301                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
302                 }
303                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
304         }
305         if (ei) e = ei->bei_e;
306         if (e == NULL) {
307 #ifdef NEW_LOGGING
308                 LDAP_LOG( BACK_BDB, INFO, 
309                         "bdb_entry_get: cannot find entry (%s)\n", 
310                         ndn->bv_val, 0, 0 );
311 #else
312                 Debug( LDAP_DEBUG_ACL,
313                         "=> bdb_entry_get: cannot find entry: \"%s\"\n",
314                                 ndn->bv_val, 0, 0 ); 
315 #endif
316                 if ( free_lock_id ) {
317                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
318                 }
319                 return LDAP_NO_SUCH_OBJECT; 
320         }
321         
322 #ifdef NEW_LOGGING
323         LDAP_LOG( BACK_BDB, DETAIL1, "bdb_entry_get: found entry (%s)\n",
324                 ndn->bv_val, 0, 0 );
325 #else
326         Debug( LDAP_DEBUG_ACL,
327                 "=> bdb_entry_get: found entry: \"%s\"\n",
328                 ndn->bv_val, 0, 0 ); 
329 #endif
330
331 #ifdef BDB_ALIASES
332         /* find attribute values */
333         if( is_entry_alias( e ) ) {
334 #ifdef NEW_LOGGING
335                 LDAP_LOG( BACK_BDB, INFO, 
336                         "bdb_entry_get: entry (%s) is an alias\n", e->e_name.bv_val, 0, 0 );
337 #else
338                 Debug( LDAP_DEBUG_ACL,
339                         "<= bdb_entry_get: entry is an alias\n", 0, 0, 0 );
340 #endif
341                 rc = LDAP_ALIAS_PROBLEM;
342                 goto return_results;
343         }
344 #endif
345
346         if( is_entry_referral( e ) ) {
347 #ifdef NEW_LOGGING
348                 LDAP_LOG( BACK_BDB, INFO, 
349                         "bdb_entry_get: entry (%s) is a referral.\n", e->e_name.bv_val, 0, 0);
350 #else
351                 Debug( LDAP_DEBUG_ACL,
352                         "<= bdb_entry_get: entry is a referral\n", 0, 0, 0 );
353 #endif
354                 rc = LDAP_REFERRAL;
355                 goto return_results;
356         }
357
358         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
359 #ifdef NEW_LOGGING
360                 LDAP_LOG( BACK_BDB, INFO, 
361                         "bdb_entry_get: failed to find objectClass.\n", 0, 0, 0 );
362 #else
363                 Debug( LDAP_DEBUG_ACL,
364                         "<= bdb_entry_get: failed to find objectClass\n",
365                         0, 0, 0 ); 
366 #endif
367                 rc = LDAP_NO_SUCH_ATTRIBUTE;
368                 goto return_results;
369         }
370
371 return_results:
372         if( rc != LDAP_SUCCESS ) {
373                 /* free entry */
374                 bdb_cache_return_entry_rw(bdb->bi_dbenv, &bdb->bi_cache, e, rw, &lock);
375         } else {
376                 *ent = e;
377                 /* big drag. we need a place to store a read lock so we can
378                  * release it later??
379                  */
380                 if ( op && !boi ) {
381                         boi = sl_calloc(1,sizeof(struct bdb_op_info),op->o_tmpmemctx);
382                         boi->boi_lock = lock;
383                         op->o_private = boi;
384                 }
385         }
386
387         if ( free_lock_id ) {
388                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
389         }
390
391 #ifdef NEW_LOGGING
392         LDAP_LOG( BACK_BDB, ENTRY, "bdb_entry_get: rc=%d\n", rc, 0, 0 );
393 #else
394         Debug( LDAP_DEBUG_TRACE,
395                 "bdb_entry_get: rc=%d\n",
396                 rc, 0, 0 ); 
397 #endif
398         return(rc);
399 }