]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
fa4fe3fc0598041c0365f3b3782f8f70c13bfa77
[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                                         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\", %ld )\n", dn, id, 0 );
115
116         DBTzero( &key );
117         key.size = strlen( dn ) + 2;
118         key.data = ch_malloc( key.size );
119         ((char *)key.data)[0] = DN_BASE_PREFIX;
120         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
121
122         /* store it -- don't override */
123         rc = db->del( db, txn, &key, 0 );
124         if( rc != 0 ) {
125                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
126                         db_strerror(rc), rc, 0 );
127                 goto done;
128         }
129
130         {
131                 char *pdn = dn_parent( NULL, dn );
132                 ((char *)(key.data))[0] = DN_ONE_PREFIX;
133
134                 if( pdn != NULL ) {
135                         key.size = strlen( pdn ) + 2;
136                         AC_MEMCPY( &((char*)key.data)[1],
137                                 pdn, key.size - 1 );
138
139                         rc = bdb_idl_delete_key( be, db, txn, &key, id );
140
141                         if( rc != 0 ) {
142                                 Debug( LDAP_DEBUG_ANY,
143                                         "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
144                                         pdn, rc, 0 );
145                                 free( pdn );
146                                 goto done;
147                         }
148                         free( pdn );
149                 }
150         }
151
152         {
153                 char **subtree = dn_subtree( NULL, dn );
154
155                 if( subtree != NULL ) {
156                         int i;
157                         ((char *)key.data)[0] = DN_SUBTREE_PREFIX;
158                         for( i=0; subtree[i] != NULL; i++ ) {
159                                 key.size = strlen( subtree[i] ) + 2;
160                                 AC_MEMCPY( &((char *)key.data)[1],
161                                         subtree[i], key.size - 1 );
162
163                                 rc = bdb_idl_delete_key( be, db, txn, &key, id );
164
165                                 if( rc != 0 ) {
166                                         Debug( LDAP_DEBUG_ANY,
167                                                 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
168                                                 subtree[i], rc, 0 );
169                                         charray_free( subtree );
170                                         goto done;
171                                 }
172                         }
173
174                         charray_free( subtree );
175                 }
176         }
177
178 done:
179         ch_free( key.data );
180         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
181         return rc;
182 }
183
184 int
185 bdb_dn2id(
186     BackendDB   *be,
187         DB_TXN *txn,
188     const char  *dn,
189         ID *id )
190 {
191         int             rc;
192         DBT             key, data;
193         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
194         DB *db = bdb->bi_dn2id->bdi_db;
195
196         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
197
198         DBTzero( &key );
199         key.size = strlen( dn ) + 2;
200         key.data = ch_malloc( key.size );
201         ((char *)key.data)[0] = DN_BASE_PREFIX;
202         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
203
204         /* store the ID */
205         DBTzero( &data );
206         data.data = id;
207         data.ulen = sizeof(ID);
208         data.flags = DB_DBT_USERMEM;
209
210         /* fetch it */
211         rc = db->get( db, txn, &key, &data, 0 );
212
213         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: id=%ld: %s (%d)\n",
214                 id, db_strerror( rc ), rc );
215
216         ch_free( key.data );
217         return rc;
218 }
219
220 int
221 bdb_dn2id_matched(
222     BackendDB   *be,
223         DB_TXN *txn,
224     const char  *in,
225         ID *id,
226         char **matchedDN )
227 {
228         int             rc;
229         DBT             key, data;
230         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
231         DB *db = bdb->bi_dn2id->bdi_db;
232         const char *dn = in;
233         char *tmp = NULL;
234
235         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
236
237         DBTzero( &key );
238         key.size = strlen( dn ) + 2;
239         key.data = ch_malloc( key.size );
240         ((char *)key.data)[0] = DN_BASE_PREFIX;
241
242         /* store the ID */
243         DBTzero( &data );
244         data.data = id;
245         data.ulen = sizeof(ID);
246         data.flags = DB_DBT_USERMEM;
247
248         *matchedDN = NULL;
249
250         while(1) {
251                 AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
252
253                 /* fetch it */
254                 rc = db->get( db, txn, &key, &data, 0 );
255
256                 if( rc == DB_NOTFOUND ) {
257                         char *pdn = dn_parent( be, dn );
258                         ch_free( tmp );
259                         tmp = NULL;
260
261                         if( pdn == NULL || *pdn == '\0' ) {
262                                 ch_free( pdn );
263                                 break;
264                         }
265
266                         dn = pdn;
267                         tmp = pdn;
268                         key.size = strlen( dn ) + 2;
269
270                 } else if ( rc == 0 ) {
271                         if( in != dn ) {
272                                 *matchedDN = (char *) dn;
273                         }
274                         Debug( LDAP_DEBUG_TRACE,
275                                 "<= bdb_dn2id_matched: id=%ld: %s\n",
276                                 id, dn, 0 );
277                         break;
278
279                 } else {
280                         ch_free( tmp );
281                         break;
282                 }
283         }
284
285         ch_free( key.data );
286         return rc;
287 }
288
289 int
290 bdb_dn2id_children(
291     BackendDB   *be,
292         DB_TXN *txn,
293     const char *dn )
294 {
295         int             rc;
296         DBT             key, data;
297         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
298         DB *db = bdb->bi_dn2id->bdi_db;
299         ID              id;
300
301         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
302                 dn, 0, 0 );
303
304         DBTzero( &key );
305         key.size = strlen( dn ) + 2;
306         key.data = ch_malloc( key.size );
307         ((char *)key.data)[0] = DN_ONE_PREFIX;
308         AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
309
310         /* we actually could do a empty get... */
311         DBTzero( &data );
312         data.data = &id;
313         data.ulen = sizeof(id);
314         data.flags = DB_DBT_USERMEM;
315         data.doff = 0;
316         data.dlen = sizeof(id);
317
318         rc = db->get( db, txn, &key, &data, 0 );
319
320         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
321                 dn,
322                 rc == 0 ? "yes" : ( rc == DB_NOTFOUND ? "no" :
323                         db_strerror(rc) ), rc );
324
325         return rc;
326 }