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