]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
73d26a251aabe817d8be493fafb02912f52280f6
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #define AVL_INTERNAL
23 #include "back-bdb.h"
24
25 static DBC *cursor = NULL;
26 static DBT key, data;
27
28 typedef struct dn_id {
29         ID id;
30         struct berval dn;
31 } dn_id;
32
33 #define HOLE_SIZE       4096
34 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
35 static unsigned nhmax = HOLE_SIZE;
36 static unsigned nholes;
37
38 static Avlnode *index_attrs, index_dummy;
39
40 int bdb_tool_entry_open(
41         BackendDB *be, int mode )
42 {
43         /* initialize key and data thangs */
44         DBTzero( &key );
45         DBTzero( &data );
46         key.flags = DB_DBT_REALLOC;
47         data.flags = DB_DBT_REALLOC;
48
49         return 0;
50 }
51
52 int bdb_tool_entry_close(
53         BackendDB *be )
54 {
55         assert( be != NULL );
56
57         if( key.data ) {
58                 ch_free( key.data );
59                 key.data = NULL;
60         }
61         if( data.data ) {
62                 ch_free( data.data );
63                 data.data = NULL;
64         }
65
66         if( cursor ) {
67                 cursor->c_close( cursor );
68                 cursor = NULL;
69         }
70
71         if( nholes ) {
72                 unsigned i;
73                 fprintf( stderr, "Error, entries missing!\n");
74                 for (i=0; i<nholes; i++) {
75                         fprintf(stderr, "  entry %ld: %s\n",
76                                 holes[i].id, holes[i].dn.bv_val);
77                 }
78                 return -1;
79         }
80                         
81         return 0;
82 }
83
84 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
85
86 ID bdb_tool_entry_next(
87         BackendDB *be )
88 {
89         int rc;
90         ID id;
91         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
92
93         assert( be != NULL );
94         assert( slapMode & SLAP_TOOL_MODE );
95         assert( bdb != NULL );
96         
97         /* Initialization */
98         if (cursor == NULL) {
99                 rc = bdb->bi_id2entry->bdi_db->cursor(
100                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
101                         bdb->bi_db_opflags );
102                 if( rc != 0 ) {
103                         return NOID;
104                 }
105         }
106
107         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
108
109         if( rc != 0 ) {
110                 /* If we're doing linear indexing and there are more attrs to
111                  * index, and we're at the end of the database, start over.
112                  */
113                 if ( bdb->bi_attrs == &index_dummy ) {
114                         if ( index_attrs && rc == DB_NOTFOUND ) {
115                                 /* optional - do a checkpoint here? */
116                                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
117                                 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
118                         }
119                         if ( rc ) {
120                                 bdb->bi_attrs = NULL;
121                                 return NOID;
122                         }
123                 } else {
124                         return NOID;
125                 }
126         }
127
128         if( data.data == NULL ) {
129                 return NOID;
130         }
131
132         BDB_DISK2ID( key.data, &id );
133         return id;
134 }
135
136 ID bdb_tool_dn2id_get(
137         Backend *be,
138         struct berval *dn
139 )
140 {
141         Operation op = {0};
142         Opheader ohdr = {0};
143         EntryInfo *ei = NULL;
144         int rc;
145
146         if ( BER_BVISEMPTY(dn) )
147                 return 0;
148
149         op.o_hdr = &ohdr;
150         op.o_bd = be;
151         op.o_tmpmemctx = NULL;
152         op.o_tmpmfuncs = &ch_mfuncs;
153
154         rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
155         if ( ei ) bdb_cache_entryinfo_unlock( ei );
156         if ( rc == DB_NOTFOUND )
157                 return NOID;
158         
159         return ei->bei_id;
160 }
161
162 int bdb_tool_id2entry_get(
163         Backend *be,
164         ID id,
165         Entry **e
166 )
167 {
168         int rc = bdb_id2entry( be, NULL, id, e );
169
170         if ( rc == DB_NOTFOUND && id == 0 ) {
171                 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
172                 struct berval gluebv = BER_BVC("glue");
173                 dummy->e_name.bv_val = ch_strdup( "" );
174                 dummy->e_nname.bv_val = ch_strdup( "" );
175                 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
176                 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
177                         &gluebv, NULL );
178                 *e = dummy;
179                 rc = LDAP_SUCCESS;
180         }
181         return rc;
182 }
183
184 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
185 {
186         int rc;
187         Entry *e = NULL;
188         struct berval bv;
189
190         assert( be != NULL );
191         assert( slapMode & SLAP_TOOL_MODE );
192         assert( data.data != NULL );
193
194 #ifndef BDB_HIER
195         DBT2bv( &data, &bv );
196
197 #ifdef SLAP_ZONE_ALLOC
198         /* FIXME: will add ctx later */
199         rc = entry_decode( &bv, &e, NULL );
200 #else
201         rc = entry_decode( &bv, &e );
202 #endif
203
204         if( rc == LDAP_SUCCESS ) {
205                 e->e_id = id;
206         }
207 #else
208         {
209                 EntryInfo *ei = NULL;
210                 Operation op = {0};
211                 Opheader ohdr = {0};
212
213                 op.o_hdr = &ohdr;
214                 op.o_bd = be;
215                 op.o_tmpmemctx = NULL;
216                 op.o_tmpmfuncs = &ch_mfuncs;
217
218                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
219                 if ( rc == LDAP_SUCCESS )
220                         e = ei->bei_e;
221         }
222 #endif
223         return e;
224 }
225
226 static int bdb_tool_next_id(
227         Operation *op,
228         DB_TXN *tid,
229         Entry *e,
230         struct berval *text,
231         int hole )
232 {
233         struct berval dn = e->e_name;
234         struct berval ndn = e->e_nname;
235         struct berval pdn, npdn;
236         EntryInfo *ei = NULL, eidummy;
237         int rc;
238
239         if (ndn.bv_len == 0) {
240                 e->e_id = 0;
241                 return 0;
242         }
243
244         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
245         if ( ei ) bdb_cache_entryinfo_unlock( ei );
246         if ( rc == DB_NOTFOUND ) {
247                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
248                         ID eid = e->e_id;
249                         dnParent( &dn, &pdn );
250                         dnParent( &ndn, &npdn );
251                         e->e_name = pdn;
252                         e->e_nname = npdn;
253                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
254                         e->e_name = dn;
255                         e->e_nname = ndn;
256                         if ( rc ) {
257                                 return rc;
258                         }
259                         /* If parent didn't exist, it was created just now
260                          * and its ID is now in e->e_id. Make sure the current
261                          * entry gets added under the new parent ID.
262                          */
263                         if ( eid != e->e_id ) {
264                                 eidummy.bei_id = e->e_id;
265                                 ei = &eidummy;
266                         }
267                 }
268                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
269                 if ( rc ) {
270                         snprintf( text->bv_val, text->bv_len,
271                                 "next_id failed: %s (%d)",
272                                 db_strerror(rc), rc );
273                 Debug( LDAP_DEBUG_ANY,
274                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
275                         return rc;
276                 }
277                 rc = bdb_dn2id_add( op, tid, ei, e );
278                 if ( rc ) {
279                         snprintf( text->bv_val, text->bv_len, 
280                                 "dn2id_add failed: %s (%d)",
281                                 db_strerror(rc), rc );
282                 Debug( LDAP_DEBUG_ANY,
283                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
284                 } else if ( hole ) {
285                         if ( nholes == nhmax - 1 ) {
286                                 if ( holes == hbuf ) {
287                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
288                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
289                                 } else {
290                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
291                                 }
292                                 nhmax *= 2;
293                         }
294                         ber_dupbv( &holes[nholes].dn, &ndn );
295                         holes[nholes++].id = e->e_id;
296                 }
297         } else if ( !hole ) {
298                 unsigned i;
299
300                 e->e_id = ei->bei_id;
301
302                 for ( i=0; i<nholes; i++) {
303                         if ( holes[i].id == e->e_id ) {
304                                 int j;
305                                 free(holes[i].dn.bv_val);
306                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
307                                 holes[j].id = 0;
308                                 nholes--;
309                                 break;
310                         } else if ( holes[i].id > e->e_id ) {
311                                 break;
312                         }
313                 }
314         }
315         return rc;
316 }
317
318 ID bdb_tool_entry_put(
319         BackendDB *be,
320         Entry *e,
321         struct berval *text )
322 {
323         int rc;
324         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
325         DB_TXN *tid = NULL;
326         Operation op = {0};
327         Opheader ohdr = {0};
328
329         assert( be != NULL );
330         assert( slapMode & SLAP_TOOL_MODE );
331
332         assert( text != NULL );
333         assert( text->bv_val != NULL );
334         assert( text->bv_val[0] == '\0' );      /* overconservative? */
335
336         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
337                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
338
339         if (! (slapMode & SLAP_TOOL_QUICK)) {
340         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
341                 bdb->bi_db_opflags );
342         if( rc != 0 ) {
343                 snprintf( text->bv_val, text->bv_len,
344                         "txn_begin failed: %s (%d)",
345                         db_strerror(rc), rc );
346                 Debug( LDAP_DEBUG_ANY,
347                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
348                          text->bv_val, 0, 0 );
349                 return NOID;
350         }
351         }
352
353         op.o_hdr = &ohdr;
354         op.o_bd = be;
355         op.o_tmpmemctx = NULL;
356         op.o_tmpmfuncs = &ch_mfuncs;
357
358         /* add dn2id indices */
359         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
360         if( rc != 0 ) {
361                 goto done;
362         }
363
364         /* id2entry index */
365         rc = bdb_id2entry_add( be, tid, e );
366         if( rc != 0 ) {
367                 snprintf( text->bv_val, text->bv_len,
368                                 "id2entry_add failed: %s (%d)",
369                                 db_strerror(rc), rc );
370                 Debug( LDAP_DEBUG_ANY,
371                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
372                         text->bv_val, 0, 0 );
373                 goto done;
374         }
375
376         if ( !bdb->bi_linear_index )
377                 rc = bdb_index_entry_add( &op, tid, e );
378         if( rc != 0 ) {
379                 snprintf( text->bv_val, text->bv_len,
380                                 "index_entry_add failed: %s (%d)",
381                                 db_strerror(rc), rc );
382                 Debug( LDAP_DEBUG_ANY,
383                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
384                         text->bv_val, 0, 0 );
385                 goto done;
386         }
387
388 done:
389         if( rc == 0 ) {
390                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
391                 rc = TXN_COMMIT( tid, 0 );
392                 if( rc != 0 ) {
393                         snprintf( text->bv_val, text->bv_len,
394                                         "txn_commit failed: %s (%d)",
395                                         db_strerror(rc), rc );
396                         Debug( LDAP_DEBUG_ANY,
397                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
398                                 text->bv_val, 0, 0 );
399                         e->e_id = NOID;
400                 }
401                 }
402
403         } else {
404                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
405                 TXN_ABORT( tid );
406                 snprintf( text->bv_val, text->bv_len,
407                         "txn_aborted! %s (%d)",
408                         db_strerror(rc), rc );
409                 Debug( LDAP_DEBUG_ANY,
410                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
411                         text->bv_val, 0, 0 );
412                 }
413                 e->e_id = NOID;
414         }
415
416         return e->e_id;
417 }
418
419 int bdb_tool_entry_reindex(
420         BackendDB *be,
421         ID id )
422 {
423         struct bdb_info *bi = (struct bdb_info *) be->be_private;
424         int rc;
425         Entry *e;
426         DB_TXN *tid = NULL;
427         Operation op = {0};
428         Opheader ohdr = {0};
429
430         Debug( LDAP_DEBUG_ARGS,
431                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
432                 (long) id, 0, 0 );
433
434         /* No indexes configured, nothing to do. Could return an
435          * error here to shortcut things.
436          */
437         if (!bi->bi_attrs) {
438                 return 0;
439         }
440
441         /* Get the first attribute to index */
442         if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
443                 index_attrs = bi->bi_attrs;
444                 bi->bi_attrs = &index_dummy;
445                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
446         }
447
448         e = bdb_tool_entry_get( be, id );
449
450         if( e == NULL ) {
451                 Debug( LDAP_DEBUG_ANY,
452                         LDAP_XSTRING(bdb_tool_entry_reindex)
453                         ": could not locate id=%ld\n",
454                         (long) id, 0, 0 );
455                 return -1;
456         }
457
458         if (! (slapMode & SLAP_TOOL_QUICK)) {
459         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
460         if( rc != 0 ) {
461                 Debug( LDAP_DEBUG_ANY,
462                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
463                         "txn_begin failed: %s (%d)\n",
464                         db_strerror(rc), rc, 0 );
465                 goto done;
466         }
467         }
468         
469         /*
470          * just (re)add them for now
471          * assume that some other routine (not yet implemented)
472          * will zap index databases
473          *
474          */
475
476         Debug( LDAP_DEBUG_TRACE,
477                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
478                 (long) id, e->e_dn, 0 );
479
480         op.o_hdr = &ohdr;
481         op.o_bd = be;
482         op.o_tmpmemctx = NULL;
483         op.o_tmpmfuncs = &ch_mfuncs;
484
485         rc = bdb_index_entry_add( &op, tid, e );
486
487 done:
488         if( rc == 0 ) {
489                 if (! (slapMode & SLAP_TOOL_QUICK)) {
490                 rc = TXN_COMMIT( tid, 0 );
491                 if( rc != 0 ) {
492                         Debug( LDAP_DEBUG_ANY,
493                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
494                                 ": txn_commit failed: %s (%d)\n",
495                                 db_strerror(rc), rc, 0 );
496                         e->e_id = NOID;
497                 }
498                 }
499
500         } else {
501                 if (! (slapMode & SLAP_TOOL_QUICK)) {
502                 TXN_ABORT( tid );
503                 Debug( LDAP_DEBUG_ANY,
504                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
505                         ": txn_aborted! %s (%d)\n",
506                         db_strerror(rc), rc, 0 );
507                 }
508                 e->e_id = NOID;
509         }
510         bdb_entry_release( &op, e, 0 );
511
512         return rc;
513 }
514
515 ID bdb_tool_entry_modify(
516         BackendDB *be,
517         Entry *e,
518         struct berval *text )
519 {
520         int rc;
521         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
522         DB_TXN *tid = NULL;
523         Operation op = {0};
524         Opheader ohdr = {0};
525
526         assert( be != NULL );
527         assert( slapMode & SLAP_TOOL_MODE );
528
529         assert( text != NULL );
530         assert( text->bv_val != NULL );
531         assert( text->bv_val[0] == '\0' );      /* overconservative? */
532
533         assert ( e->e_id != NOID );
534
535         Debug( LDAP_DEBUG_TRACE,
536                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
537                 (long) e->e_id, e->e_dn, 0 );
538
539         if (! (slapMode & SLAP_TOOL_QUICK)) {
540         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
541                 bdb->bi_db_opflags );
542         if( rc != 0 ) {
543                 snprintf( text->bv_val, text->bv_len,
544                         "txn_begin failed: %s (%d)",
545                         db_strerror(rc), rc );
546                 Debug( LDAP_DEBUG_ANY,
547                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
548                          text->bv_val, 0, 0 );
549                 return NOID;
550         }
551         }
552
553         op.o_hdr = &ohdr;
554         op.o_bd = be;
555         op.o_tmpmemctx = NULL;
556         op.o_tmpmfuncs = &ch_mfuncs;
557
558         /* id2entry index */
559         rc = bdb_id2entry_update( be, tid, e );
560         if( rc != 0 ) {
561                 snprintf( text->bv_val, text->bv_len,
562                                 "id2entry_add failed: %s (%d)",
563                                 db_strerror(rc), rc );
564                 Debug( LDAP_DEBUG_ANY,
565                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
566                         text->bv_val, 0, 0 );
567                 goto done;
568         }
569
570 #if 0
571         /* FIXME: this is bogus, we don't have the old values to delete
572          * from the index because the given entry has already been modified.
573          */
574         rc = bdb_index_entry_del( &op, tid, e );
575         if( rc != 0 ) {
576                 snprintf( text->bv_val, text->bv_len,
577                                 "index_entry_del failed: %s (%d)",
578                                 db_strerror(rc), rc );
579                 Debug( LDAP_DEBUG_ANY,
580                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
581                         text->bv_val, 0, 0 );
582                 goto done;
583         }
584 #endif
585
586         rc = bdb_index_entry_add( &op, tid, e );
587         if( rc != 0 ) {
588                 snprintf( text->bv_val, text->bv_len,
589                                 "index_entry_add failed: %s (%d)",
590                                 db_strerror(rc), rc );
591                 Debug( LDAP_DEBUG_ANY,
592                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
593                         text->bv_val, 0, 0 );
594                 goto done;
595         }
596
597 done:
598         if( rc == 0 ) {
599                 if (! (slapMode & SLAP_TOOL_QUICK)) {
600                 rc = TXN_COMMIT( tid, 0 );
601                 if( rc != 0 ) {
602                         snprintf( text->bv_val, text->bv_len,
603                                         "txn_commit failed: %s (%d)",
604                                         db_strerror(rc), rc );
605                         Debug( LDAP_DEBUG_ANY,
606                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
607                                 "%s\n", text->bv_val, 0, 0 );
608                         e->e_id = NOID;
609                 }
610                 }
611
612         } else {
613                 if (! (slapMode & SLAP_TOOL_QUICK)) {
614                 TXN_ABORT( tid );
615                 snprintf( text->bv_val, text->bv_len,
616                         "txn_aborted! %s (%d)",
617                         db_strerror(rc), rc );
618                 Debug( LDAP_DEBUG_ANY,
619                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
620                         text->bv_val, 0, 0 );
621                 }
622                 e->e_id = NOID;
623         }
624
625         return e->e_id;
626 }