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