]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
f7f573eabdd23d428467b6f0783ad4ce5b23d1ad
[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
15 int
16 bdb_dn2id_add(
17         BackendDB       *be,
18         DB_TXN *txn,
19         const char      *dn,
20         ID              id )
21 {
22         int             rc;
23         DBT             key, data;
24         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
25         DB *db = bdb->bi_dn2id->bdi_db;
26
27         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
28                 dn, id, 0 );
29         assert( id != NOID );
30
31         DBTzero( &key );
32         key.size = strlen( dn ) + 2;
33         key.data = ch_malloc( key.size );
34         ((char *)key.data)[0] = DN_BASE_PREFIX;
35         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
36
37         DBTzero( &data );
38         data.data = (char *) &id;
39         data.size = sizeof( id );
40
41         /* store it -- don't override */
42         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
43         if( rc != 0 ) {
44                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
45                         db_strerror(rc), rc, 0 );
46                 goto done;
47         }
48
49         {
50                 char *pdn = dn_parent( NULL, dn );
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, 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                                 free( pdn );
65                                 goto done;
66                         }
67                         free( pdn );
68                 }
69         }
70
71         {
72                 char **subtree = dn_subtree( NULL, dn );
73
74                 if( subtree != NULL ) {
75                         int i;
76                         ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
77                         for( i=0; subtree[i] != NULL; i++ ) {
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, id );
83
84                                 if( rc != 0 ) {
85                                         Debug( LDAP_DEBUG_ANY,
86                                                 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
87                                                 subtree[i], rc, 0 );
88                                         break;
89                                 }
90                         }
91
92                         charray_free( subtree );
93                 }
94         }
95
96 done:
97         ch_free( key.data );
98         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
99         return rc;
100 }
101
102 int
103 bdb_dn2id_delete(
104         BackendDB       *be,
105         DB_TXN *txn,
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         ((char *)key.data)[0] = DN_BASE_PREFIX;
121         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
122
123         /* store it -- don't override */
124         rc = db->del( db, txn, &key, 0 );
125         if( rc != 0 ) {
126                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
127                         db_strerror(rc), rc, 0 );
128                 goto done;
129         }
130
131         {
132                 char *pdn = dn_parent( NULL, dn );
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                                 free( pdn );
147                                 goto done;
148                         }
149                         free( pdn );
150                 }
151         }
152
153         {
154                 char **subtree = dn_subtree( NULL, 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, 0 );
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, 0 );
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                                 *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, 0 );
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 }