]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
3479310f47b95abcb7d05c19be14535abb08ae01
[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 {
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         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: id=0x%08lx: %s (%d)\n",
216                 id, db_strerror( rc ), rc );
217
218         ch_free( key.data );
219         return rc;
220 }
221
222 int
223 bdb_dn2id_matched(
224         BackendDB       *be,
225         DB_TXN *txn,
226         const char      *in,
227         ID *id,
228         char **matchedDN )
229 {
230         int             rc;
231         DBT             key, data;
232         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
233         DB *db = bdb->bi_dn2id->bdi_db;
234         const char *dn = in;
235         char *tmp = NULL;
236
237         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
238
239         DBTzero( &key );
240         key.size = strlen( dn ) + 2;
241         key.data = ch_malloc( key.size );
242         ((char *)key.data)[0] = DN_BASE_PREFIX;
243
244         /* store the ID */
245         DBTzero( &data );
246         data.data = id;
247         data.ulen = sizeof(ID);
248         data.flags = DB_DBT_USERMEM;
249
250         *matchedDN = NULL;
251
252         while(1) {
253                 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
254
255                 *id = NOID;
256
257                 /* fetch it */
258                 rc = db->get( db, txn, &key, &data, 0 );
259
260                 if( rc == DB_NOTFOUND ) {
261                         char *pdn = dn_parent( be, dn );
262                         ch_free( tmp );
263                         tmp = NULL;
264
265                         if( pdn == NULL || *pdn == '\0' ) {
266                                 Debug( LDAP_DEBUG_TRACE,
267                                         "<= bdb_dn2id_matched: no match\n",
268                                         0, 0, 0 );
269                                 ch_free( pdn );
270                                 break;
271                         }
272
273                         dn = pdn;
274                         tmp = pdn;
275                         key.size = strlen( dn ) + 2;
276
277                 } else if ( rc == 0 ) {
278                         if( data.size != sizeof( ID ) ) {
279                                 Debug( LDAP_DEBUG_ANY,
280                                         "<= bdb_dn2id_matched: get size mismatch: "
281                                         "expected %ld, got %ld\n",
282                                         (long) sizeof(ID), (long) data.size, 0 );
283                                 ch_free( tmp );
284                         }
285
286                         if( in != dn ) {
287                                 *matchedDN = (char *) dn;
288                         }
289
290                         Debug( LDAP_DEBUG_TRACE,
291                                 "<= bdb_dn2id_matched: id=0x%08lx: %s\n",
292                                 *id, dn, 0 );
293                         break;
294
295                 } else {
296                         Debug( LDAP_DEBUG_ANY,
297                                 "<= bdb_dn2id_matched: get failed: %s (%d)\n",
298                                 db_strerror(rc), rc, 0 );
299                         ch_free( tmp );
300                         break;
301                 }
302         }
303
304         ch_free( key.data );
305         return rc;
306 }
307
308 int
309 bdb_dn2id_children(
310         BackendDB       *be,
311         DB_TXN *txn,
312         const char *dn )
313 {
314         int             rc;
315         DBT             key, data;
316         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
317         DB *db = bdb->bi_dn2id->bdi_db;
318         ID              id;
319
320         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
321                 dn, 0, 0 );
322
323         DBTzero( &key );
324         key.size = strlen( dn ) + 2;
325         key.data = ch_malloc( key.size );
326         ((char *)key.data)[0] = DN_ONE_PREFIX;
327         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
328
329         /* we actually could do a empty get... */
330         DBTzero( &data );
331         data.data = &id;
332         data.ulen = sizeof(id);
333         data.flags = DB_DBT_USERMEM;
334         data.doff = 0;
335         data.dlen = sizeof(id);
336
337         rc = db->get( db, txn, &key, &data, 0 );
338
339         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
340                 dn,
341                 rc == 0 ? "yes" : ( rc == DB_NOTFOUND ? "no" :
342                         db_strerror(rc) ), rc );
343
344         return rc;
345 }