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