]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
ITS#3616 allow adding entry with zero-length DN if the suffix is zero-length
[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 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 = {0};
144
145         if ( BER_BVISEMPTY(dn) )
146                 return 0;
147
148         op.o_hdr = &ohdr;
149         op.o_bd = be;
150         op.o_tmpmemctx = NULL;
151         op.o_tmpmfuncs = &ch_mfuncs;
152
153         ei.bei_id = NOID;
154
155         bdb_dn2id( &op, NULL, dn, &ei );
156         
157         return ei.bei_id;
158 }
159
160 static struct berval ocbva[] = {
161         BER_BVC("locality"),
162         BER_BVC("syncProviderSubentry"),
163         BER_BVNULL
164 };
165
166 int bdb_tool_id2entry_get(
167         Backend *be,
168         ID id,
169         Entry **e
170 )
171 {
172         int rc = bdb_id2entry( be, NULL, id, e );
173
174         if ( rc == DB_NOTFOUND && id == 0 ) {
175                 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
176                 dummy->e_name.bv_val = ch_strdup( "" );
177                 dummy->e_nname.bv_val = ch_strdup( "" );
178                 attr_merge( dummy, slap_schema.si_ad_objectClass, ocbva, NULL );
179                 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
180                         &ocbva[0], NULL );
181                 *e = dummy;
182                 rc = LDAP_SUCCESS;
183         }
184         return rc;
185 }
186
187 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
188 {
189         int rc;
190         Entry *e = NULL;
191         struct berval bv;
192
193         assert( be != NULL );
194         assert( slapMode & SLAP_TOOL_MODE );
195         assert( data.data != NULL );
196
197 #ifndef BDB_HIER
198         DBT2bv( &data, &bv );
199
200 #ifdef SLAP_ZONE_ALLOC
201         /* FIXME: will add ctx later */
202         rc = entry_decode( &bv, &e, NULL );
203 #else
204         rc = entry_decode( &bv, &e );
205 #endif
206
207         if( rc == LDAP_SUCCESS ) {
208                 e->e_id = id;
209         }
210 #else
211         {
212                 EntryInfo *ei = NULL;
213                 Operation op = {0};
214                 Opheader ohdr = {0};
215
216                 op.o_hdr = &ohdr;
217                 op.o_bd = be;
218                 op.o_tmpmemctx = NULL;
219                 op.o_tmpmfuncs = &ch_mfuncs;
220
221                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
222                 if ( rc == LDAP_SUCCESS )
223                         e = ei->bei_e;
224         }
225 #endif
226         return e;
227 }
228
229 static int bdb_tool_next_id(
230         Operation *op,
231         DB_TXN *tid,
232         Entry *e,
233         struct berval *text,
234         int hole )
235 {
236         struct berval dn = e->e_name;
237         struct berval ndn = e->e_nname;
238         struct berval pdn, npdn;
239         EntryInfo *ei = NULL, eidummy;
240         int rc;
241
242         if (ndn.bv_len == 0) {
243                 e->e_id = 0;
244                 return 0;
245         }
246
247         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
248         if ( ei ) bdb_cache_entryinfo_unlock( ei );
249         if ( rc == DB_NOTFOUND ) {
250                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
251                         ID eid = e->e_id;
252                         dnParent( &dn, &pdn );
253                         dnParent( &ndn, &npdn );
254                         e->e_name = pdn;
255                         e->e_nname = npdn;
256                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
257                         e->e_name = dn;
258                         e->e_nname = ndn;
259                         if ( rc ) {
260                                 return rc;
261                         }
262                         /* If parent didn't exist, it was created just now
263                          * and its ID is now in e->e_id. Make sure the current
264                          * entry gets added under the new parent ID.
265                          */
266                         if ( eid != e->e_id ) {
267                                 eidummy.bei_id = e->e_id;
268                                 ei = &eidummy;
269                         }
270                 }
271                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
272                 if ( rc ) {
273                         snprintf( text->bv_val, text->bv_len,
274                                 "next_id failed: %s (%d)",
275                                 db_strerror(rc), rc );
276                 Debug( LDAP_DEBUG_ANY,
277                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
278                         return rc;
279                 }
280                 rc = bdb_dn2id_add( op, tid, ei, e );
281                 if ( rc ) {
282                         snprintf( text->bv_val, text->bv_len, 
283                                 "dn2id_add failed: %s (%d)",
284                                 db_strerror(rc), rc );
285                 Debug( LDAP_DEBUG_ANY,
286                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
287                 } else if ( hole ) {
288                         if ( nholes == nhmax - 1 ) {
289                                 if ( holes == hbuf ) {
290                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
291                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
292                                 } else {
293                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
294                                 }
295                                 nhmax *= 2;
296                         }
297                         ber_dupbv( &holes[nholes].dn, &ndn );
298                         holes[nholes++].id = e->e_id;
299                 }
300         } else if ( !hole ) {
301                 unsigned i;
302
303                 e->e_id = ei->bei_id;
304
305                 for ( i=0; i<nholes; i++) {
306                         if ( holes[i].id == e->e_id ) {
307                                 int j;
308                                 free(holes[i].dn.bv_val);
309                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
310                                 holes[j].id = 0;
311                                 nholes--;
312                                 break;
313                         } else if ( holes[i].id > e->e_id ) {
314                                 break;
315                         }
316                 }
317         }
318         return rc;
319 }
320
321 ID bdb_tool_entry_put(
322         BackendDB *be,
323         Entry *e,
324         struct berval *text )
325 {
326         int rc;
327         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
328         DB_TXN *tid = NULL;
329         Operation op = {0};
330         Opheader ohdr = {0};
331
332         assert( be != NULL );
333         assert( slapMode & SLAP_TOOL_MODE );
334
335         assert( text );
336         assert( text->bv_val );
337         assert( text->bv_val[0] == '\0' );      /* overconservative? */
338
339         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
340                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
341
342         if (! (slapMode & SLAP_TOOL_QUICK)) {
343         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
344                 bdb->bi_db_opflags );
345         if( rc != 0 ) {
346                 snprintf( text->bv_val, text->bv_len,
347                         "txn_begin failed: %s (%d)",
348                         db_strerror(rc), rc );
349                 Debug( LDAP_DEBUG_ANY,
350                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
351                          text->bv_val, 0, 0 );
352                 return NOID;
353         }
354         }
355
356         op.o_hdr = &ohdr;
357         op.o_bd = be;
358         op.o_tmpmemctx = NULL;
359         op.o_tmpmfuncs = &ch_mfuncs;
360
361         /* add dn2id indices */
362         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
363         if( rc != 0 ) {
364                 goto done;
365         }
366
367         /* id2entry index */
368         rc = bdb_id2entry_add( be, tid, e );
369         if( rc != 0 ) {
370                 snprintf( text->bv_val, text->bv_len,
371                                 "id2entry_add failed: %s (%d)",
372                                 db_strerror(rc), rc );
373                 Debug( LDAP_DEBUG_ANY,
374                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
375                         text->bv_val, 0, 0 );
376                 goto done;
377         }
378
379         if ( !bdb->bi_linear_index )
380                 rc = bdb_index_entry_add( &op, tid, e );
381         if( rc != 0 ) {
382                 snprintf( text->bv_val, text->bv_len,
383                                 "index_entry_add failed: %s (%d)",
384                                 db_strerror(rc), rc );
385                 Debug( LDAP_DEBUG_ANY,
386                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
387                         text->bv_val, 0, 0 );
388                 goto done;
389         }
390
391 done:
392         if( rc == 0 ) {
393                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
394                 rc = TXN_COMMIT( tid, 0 );
395                 if( rc != 0 ) {
396                         snprintf( text->bv_val, text->bv_len,
397                                         "txn_commit failed: %s (%d)",
398                                         db_strerror(rc), rc );
399                         Debug( LDAP_DEBUG_ANY,
400                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
401                                 text->bv_val, 0, 0 );
402                         e->e_id = NOID;
403                 }
404                 }
405
406         } else {
407                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
408                 TXN_ABORT( tid );
409                 snprintf( text->bv_val, text->bv_len,
410                         "txn_aborted! %s (%d)",
411                         db_strerror(rc), rc );
412                 Debug( LDAP_DEBUG_ANY,
413                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
414                         text->bv_val, 0, 0 );
415                 }
416                 e->e_id = NOID;
417         }
418
419         return e->e_id;
420 }
421
422 int bdb_tool_entry_reindex(
423         BackendDB *be,
424         ID id )
425 {
426         struct bdb_info *bi = (struct bdb_info *) be->be_private;
427         int rc;
428         Entry *e;
429         DB_TXN *tid = NULL;
430         Operation op = {0};
431         Opheader ohdr = {0};
432
433         Debug( LDAP_DEBUG_ARGS,
434                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
435                 (long) id, 0, 0 );
436
437         /* No indexes configured, nothing to do. Could return an
438          * error here to shortcut things.
439          */
440         if (!bi->bi_attrs) {
441                 return 0;
442         }
443
444         /* Get the first attribute to index */
445         if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
446                 index_attrs = bi->bi_attrs;
447                 bi->bi_attrs = &index_dummy;
448                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
449         }
450
451         e = bdb_tool_entry_get( be, id );
452
453         if( e == NULL ) {
454                 Debug( LDAP_DEBUG_ANY,
455                         LDAP_XSTRING(bdb_tool_entry_reindex)
456                         ": could not locate id=%ld\n",
457                         (long) id, 0, 0 );
458                 return -1;
459         }
460
461         if (! (slapMode & SLAP_TOOL_QUICK)) {
462         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
463         if( rc != 0 ) {
464                 Debug( LDAP_DEBUG_ANY,
465                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
466                         "txn_begin failed: %s (%d)\n",
467                         db_strerror(rc), rc, 0 );
468                 goto done;
469         }
470         }
471         
472         /*
473          * just (re)add them for now
474          * assume that some other routine (not yet implemented)
475          * will zap index databases
476          *
477          */
478
479         Debug( LDAP_DEBUG_TRACE,
480                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
481                 (long) id, e->e_dn, 0 );
482
483         op.o_hdr = &ohdr;
484         op.o_bd = be;
485         op.o_tmpmemctx = NULL;
486         op.o_tmpmfuncs = &ch_mfuncs;
487
488         rc = bdb_index_entry_add( &op, tid, e );
489
490 done:
491         if( rc == 0 ) {
492                 if (! (slapMode & SLAP_TOOL_QUICK)) {
493                 rc = TXN_COMMIT( tid, 0 );
494                 if( rc != 0 ) {
495                         Debug( LDAP_DEBUG_ANY,
496                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
497                                 ": txn_commit failed: %s (%d)\n",
498                                 db_strerror(rc), rc, 0 );
499                         e->e_id = NOID;
500                 }
501                 }
502
503         } else {
504                 if (! (slapMode & SLAP_TOOL_QUICK)) {
505                 TXN_ABORT( tid );
506                 Debug( LDAP_DEBUG_ANY,
507                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
508                         ": txn_aborted! %s (%d)\n",
509                         db_strerror(rc), rc, 0 );
510                 }
511                 e->e_id = NOID;
512         }
513         bdb_entry_release( &op, e, 0 );
514
515         return rc;
516 }
517
518 ID bdb_tool_entry_modify(
519         BackendDB *be,
520         Entry *e,
521         struct berval *text )
522 {
523         int rc;
524         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
525         DB_TXN *tid = NULL;
526         Operation op = {0};
527         Opheader ohdr = {0};
528
529         assert( be != NULL );
530         assert( slapMode & SLAP_TOOL_MODE );
531
532         assert( text );
533         assert( text->bv_val );
534         assert( text->bv_val[0] == '\0' );      /* overconservative? */
535
536         assert ( e->e_id != NOID );
537
538         Debug( LDAP_DEBUG_TRACE,
539                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
540                 (long) e->e_id, e->e_dn, 0 );
541
542         if (! (slapMode & SLAP_TOOL_QUICK)) {
543         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
544                 bdb->bi_db_opflags );
545         if( rc != 0 ) {
546                 snprintf( text->bv_val, text->bv_len,
547                         "txn_begin failed: %s (%d)",
548                         db_strerror(rc), rc );
549                 Debug( LDAP_DEBUG_ANY,
550                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
551                          text->bv_val, 0, 0 );
552                 return NOID;
553         }
554         }
555
556         op.o_hdr = &ohdr;
557         op.o_bd = be;
558         op.o_tmpmemctx = NULL;
559         op.o_tmpmfuncs = &ch_mfuncs;
560
561         /* id2entry index */
562         rc = bdb_id2entry_update( be, tid, e );
563         if( rc != 0 ) {
564                 snprintf( text->bv_val, text->bv_len,
565                                 "id2entry_add failed: %s (%d)",
566                                 db_strerror(rc), rc );
567                 Debug( LDAP_DEBUG_ANY,
568                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
569                         text->bv_val, 0, 0 );
570                 goto done;
571         }
572
573 #if 0
574         /* FIXME: this is bogus, we don't have the old values to delete
575          * from the index because the given entry has already been modified.
576          */
577         rc = bdb_index_entry_del( &op, tid, e );
578         if( rc != 0 ) {
579                 snprintf( text->bv_val, text->bv_len,
580                                 "index_entry_del failed: %s (%d)",
581                                 db_strerror(rc), rc );
582                 Debug( LDAP_DEBUG_ANY,
583                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
584                         text->bv_val, 0, 0 );
585                 goto done;
586         }
587 #endif
588
589         rc = bdb_index_entry_add( &op, tid, e );
590         if( rc != 0 ) {
591                 snprintf( text->bv_val, text->bv_len,
592                                 "index_entry_add failed: %s (%d)",
593                                 db_strerror(rc), rc );
594                 Debug( LDAP_DEBUG_ANY,
595                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
596                         text->bv_val, 0, 0 );
597                 goto done;
598         }
599
600 done:
601         if( rc == 0 ) {
602                 if (! (slapMode & SLAP_TOOL_QUICK)) {
603                 rc = TXN_COMMIT( tid, 0 );
604                 if( rc != 0 ) {
605                         snprintf( text->bv_val, text->bv_len,
606                                         "txn_commit failed: %s (%d)",
607                                         db_strerror(rc), rc );
608                         Debug( LDAP_DEBUG_ANY,
609                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
610                                 "%s\n", text->bv_val, 0, 0 );
611                         e->e_id = NOID;
612                 }
613                 }
614
615         } else {
616                 if (! (slapMode & SLAP_TOOL_QUICK)) {
617                 TXN_ABORT( tid );
618                 snprintf( text->bv_val, text->bv_len,
619                         "txn_aborted! %s (%d)",
620                         db_strerror(rc), rc );
621                 Debug( LDAP_DEBUG_ANY,
622                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
623                         text->bv_val, 0, 0 );
624                 }
625                 e->e_id = NOID;
626         }
627
628         return e->e_id;
629 }