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