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