]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Missed a spot where root DN_SUBTREE index was still getting written.
[openldap] / servers / slapd / back-bdb / dn2id.c
1 /* dn2id.c - routines to deal with the dn2id index */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 "idl.h"
15
16 int
17 bdb_dn2id_add(
18         BackendDB       *be,
19         DB_TXN *txn,
20         const char      *pdn,
21         Entry           *e )
22 {
23         int             rc;
24         DBT             key, data;
25         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26         DB *db = bdb->bi_dn2id->bdi_db;
27
28         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
29                 e->e_ndn, (long) e->e_id, 0 );
30         assert( e->e_id != NOID );
31
32         DBTzero( &key );
33         key.size = strlen( e->e_ndn ) + 2;
34         key.data = ch_malloc( key.size );
35         ((char *)key.data)[0] = DN_BASE_PREFIX;
36         AC_MEMCPY( &((char *)key.data)[1], e->e_ndn, key.size - 1 );
37
38         DBTzero( &data );
39         data.data = (char *) &e->e_id;
40         data.size = sizeof( e->e_id );
41
42         /* store it -- don't override */
43         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
44         if( rc != 0 ) {
45                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
46                         db_strerror(rc), rc, 0 );
47                 goto done;
48         }
49
50         {
51                 ((char *)(key.data))[0] = DN_ONE_PREFIX;
52
53                 if( pdn != NULL ) {
54                         key.size = strlen( pdn ) + 2;
55                         AC_MEMCPY( &((char*)key.data)[1],
56                                 pdn, key.size - 1 );
57
58                         rc = bdb_idl_insert_key( be, db, txn, &key, e->e_id );
59
60                         if( rc != 0 ) {
61                                 Debug( LDAP_DEBUG_ANY,
62                                         "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
63                                         pdn, rc, 0 );
64                                 goto done;
65                         }
66                 }
67         }
68
69         {
70                 char **subtree = dn_subtree( be, e->e_ndn );
71
72                 if( subtree != NULL ) {
73                         int i;
74                         ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
75                         for( i=0; subtree[i] != NULL; i++ ) {
76                                 if (be_issuffix(be, subtree[i]))
77                                         continue;
78                                 key.size = strlen( subtree[i] ) + 2;
79                                 AC_MEMCPY( &((char *)key.data)[1],
80                                         subtree[i], key.size - 1 );
81
82                                 rc = bdb_idl_insert_key( be, db, txn, &key,
83                                         e->e_id );
84
85                                 if( rc != 0 ) {
86                                         Debug( LDAP_DEBUG_ANY,
87                                                 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
88                                                 subtree[i], rc, 0 );
89                                         break;
90                                 }
91                         }
92
93                         charray_free( subtree );
94                 }
95         }
96
97 done:
98         ch_free( key.data );
99         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
100         return rc;
101 }
102
103 int
104 bdb_dn2id_delete(
105         BackendDB       *be,
106         DB_TXN *txn,
107         const char      *pdn,
108         const char      *dn,
109         ID              id )
110 {
111         int             rc;
112         DBT             key;
113         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
114         DB *db = bdb->bi_dn2id->bdi_db;
115
116         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
117                 dn, id, 0 );
118
119         DBTzero( &key );
120         key.size = strlen( dn ) + 2;
121         key.data = ch_malloc( key.size );
122         key.flags = DB_DBT_USERMEM;
123         ((char *)key.data)[0] = DN_BASE_PREFIX;
124         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
125
126         /* delete it */
127         rc = db->del( db, txn, &key, 0 );
128         if( rc != 0 ) {
129                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
130                         db_strerror(rc), rc, 0 );
131                 goto done;
132         }
133
134         {
135                 ((char *)(key.data))[0] = DN_ONE_PREFIX;
136
137                 if( pdn != NULL ) {
138                         key.size = strlen( pdn ) + 2;
139                         AC_MEMCPY( &((char*)key.data)[1],
140                                 pdn, key.size - 1 );
141
142                         rc = bdb_idl_delete_key( be, db, txn, &key, id );
143
144                         if( rc != 0 ) {
145                                 Debug( LDAP_DEBUG_ANY,
146                                         "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
147                                         pdn, rc, 0 );
148                                 goto done;
149                         }
150                 }
151         }
152
153         {
154                 char **subtree = dn_subtree( be, dn );
155
156                 if( subtree != NULL ) {
157                         int i;
158                         ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
159                         for( i=0; subtree[i] != NULL; i++ ) {
160                                 key.size = strlen( subtree[i] ) + 2;
161                                 AC_MEMCPY( &((char *)key.data)[1],
162                                         subtree[i], key.size - 1 );
163
164                                 rc = bdb_idl_delete_key( be, db, txn, &key, id );
165
166                                 if( rc != 0 ) {
167                                         Debug( LDAP_DEBUG_ANY,
168                                                 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
169                                                 subtree[i], rc, 0 );
170                                         charray_free( subtree );
171                                         goto done;
172                                 }
173                         }
174
175                         charray_free( subtree );
176                 }
177         }
178
179 done:
180         ch_free( key.data );
181         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
182         return rc;
183 }
184
185 int
186 bdb_dn2id(
187         BackendDB       *be,
188         DB_TXN *txn,
189         const char      *dn,
190         ID *id )
191 {
192         int             rc;
193         DBT             key, data;
194         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
195         DB *db = bdb->bi_dn2id->bdi_db;
196
197         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
198
199         DBTzero( &key );
200         key.size = strlen( dn ) + 2;
201         key.data = ch_malloc( key.size );
202         ((char *)key.data)[0] = DN_BASE_PREFIX;
203         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
204
205         /* store the ID */
206         DBTzero( &data );
207         data.data = id;
208         data.ulen = sizeof(ID);
209         data.flags = DB_DBT_USERMEM;
210
211         /* fetch it */
212         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
213
214         if( rc != 0 ) {
215                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
216                         db_strerror( rc ), rc, 0 );
217         } else {
218                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
219                         *id, 0, 0 );
220         }
221
222         ch_free( key.data );
223         return rc;
224 }
225
226 int
227 bdb_dn2id_matched(
228         BackendDB       *be,
229         DB_TXN *txn,
230         const char      *in,
231         ID *id,
232         char **matchedDN )
233 {
234         int             rc;
235         DBT             key, data;
236         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
237         DB *db = bdb->bi_dn2id->bdi_db;
238         const char *dn = in;
239         char *tmp = NULL;
240
241         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
242
243         DBTzero( &key );
244         key.size = strlen( dn ) + 2;
245         key.data = ch_malloc( key.size );
246         ((char *)key.data)[0] = DN_BASE_PREFIX;
247
248         /* store the ID */
249         DBTzero( &data );
250         data.data = id;
251         data.ulen = sizeof(ID);
252         data.flags = DB_DBT_USERMEM;
253
254         *matchedDN = NULL;
255
256         while(1) {
257                 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
258
259                 *id = NOID;
260
261                 /* fetch it */
262                 rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
263
264                 if( rc == DB_NOTFOUND ) {
265                         char *pdn = dn_parent( be, dn );
266                         ch_free( tmp );
267                         tmp = NULL;
268
269                         if( pdn == NULL || *pdn == '\0' ) {
270                                 Debug( LDAP_DEBUG_TRACE,
271                                         "<= bdb_dn2id_matched: no match\n",
272                                         0, 0, 0 );
273                                 ch_free( pdn );
274                                 break;
275                         }
276
277                         dn = pdn;
278                         tmp = pdn;
279                         key.size = strlen( dn ) + 2;
280
281                 } else if ( rc == 0 ) {
282                         if( data.size != sizeof( ID ) ) {
283                                 Debug( LDAP_DEBUG_ANY,
284                                         "<= bdb_dn2id_matched: get size mismatch: "
285                                         "expected %ld, got %ld\n",
286                                         (long) sizeof(ID), (long) data.size, 0 );
287                                 ch_free( tmp );
288                         }
289
290                         if( in != dn ) {
291                                 *matchedDN = (char *) dn;
292                         }
293
294                         Debug( LDAP_DEBUG_TRACE,
295                                 "<= bdb_dn2id_matched: id=0x%08lx: %s %s\n",
296                                 (long) *id, *matchedDN == NULL ? "entry" : "matched", dn );
297                         break;
298
299                 } else {
300                         Debug( LDAP_DEBUG_ANY,
301                                 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
302                                 db_strerror(rc), rc, 0 );
303                         ch_free( tmp );
304                         break;
305                 }
306         }
307
308         ch_free( key.data );
309         return rc;
310 }
311
312 int
313 bdb_dn2id_children(
314         BackendDB       *be,
315         DB_TXN *txn,
316         const char *dn )
317 {
318         int             rc;
319         DBT             key, data;
320         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
321         DB *db = bdb->bi_dn2id->bdi_db;
322         ID              id;
323
324         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
325                 dn, 0, 0 );
326
327         DBTzero( &key );
328         key.size = strlen( dn ) + 2;
329         key.data = ch_malloc( key.size );
330         ((char *)key.data)[0] = DN_ONE_PREFIX;
331         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
332
333         /* we actually could do a empty get... */
334         DBTzero( &data );
335         data.data = &id;
336         data.ulen = sizeof(id);
337         data.flags = DB_DBT_USERMEM;
338         data.doff = 0;
339         data.dlen = sizeof(id);
340
341         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
342
343         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %schildren (%d)\n",
344                 dn,
345                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
346                         db_strerror(rc) ), rc );
347
348         return rc;
349 }
350
351 int
352 bdb_dn2idl(
353         BackendDB       *be,
354         const char      *dn,
355         int prefix,
356         ID *ids )
357 {
358         int             rc;
359         DBT             key, data;
360         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
361         DB *db = bdb->bi_dn2id->bdi_db;
362
363         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl( \"%s\" )\n", dn, 0, 0 );
364
365         if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn))
366         {
367                 BDB_IDL_ALL(bdb, ids);
368                 return 0;
369         }
370
371         DBTzero( &key );
372         key.size = strlen( dn ) + 2;
373         key.data = ch_malloc( key.size );
374         ((char *)key.data)[0] = prefix;
375         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
376
377         rc = bdb_idl_fetch_key( be, db, NULL, &key, ids );
378
379         if( rc != 0 ) {
380                 Debug( LDAP_DEBUG_TRACE,
381                         "<= bdb_dn2idl: get failed: %s (%d)\n",
382                         db_strerror( rc ), rc, 0 );
383
384         } else {
385                 Debug( LDAP_DEBUG_TRACE,
386                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
387                         (long) ids[0],
388                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
389         }
390
391         ch_free( key.data );
392         return rc;
393 }