Debian Codegraph @

servers/slapd/back-wt/tools.c

raw
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2002-2024 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 /* ACKNOWLEDGEMENTS:
17 * This work was developed by HAMANO Tsukasa <[email protected]>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
20 */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include "back-wt.h"
27 #include "slap-config.h"
28
29 typedef struct dn_id {
30 ID id;
31 struct berval dn;
32 } dn_id;
33
34 #define HOLE_SIZE 4096
35 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
36 static unsigned nholes;
37
38 static struct berval *tool_base;
39 static int tool_scope;
40 static Filter *tool_filter;
41
42 static wt_ctx *wc;
43 static WT_CURSOR *reader;
44 static WT_ITEM item;
45
46 int
47 wt_tool_entry_open( BackendDB *be, int mode )
48 {
49 struct wt_info *wi = (struct wt_info *) be->be_private;
50 int rc;
51
52 if ( slapMode & SLAP_TOOL_DRYRUN )
53 return 0;
54
55 wc = wt_ctx_init(wi);
56 if( !wc ){
57 Debug( LDAP_DEBUG_ANY,
58 "wt_tool_entry_open: wt_ctx_get failed\n" );
59 return -1;
60 }
61
62 rc = wc->session->open_cursor(wc->session, WT_TABLE_ID2ENTRY"(entry)"
63 ,NULL, NULL, &reader);
64 if ( rc ) {
65 Debug( LDAP_DEBUG_ANY,
66 "wt_tool_entry_open: cursor open failed: %s (%d)\n",
67 wiredtiger_strerror(rc), rc );
68 return -1;
69 }
70
71 return 0;
72 }
73
74 int
75 wt_tool_entry_close( BackendDB *be )
76 {
77 if ( slapMode & SLAP_TOOL_DRYRUN )
78 return 0;
79
80 if( reader ) {
81 reader->close(reader);
82 reader = NULL;
83 }
84
85 wt_ctx_free(NULL, wc);
86
87 if( nholes ) {
88 unsigned i;
89 fprintf( stderr, "Error, entries missing!\n");
90 for (i=0; i<nholes; i++) {
91 fprintf(stderr, " entry %ld: %s\n",
92 holes[i].id, holes[i].dn.bv_val);
93 }
94 return -1;
95 }
96
97 return 0;
98 }
99
100 ID
101 wt_tool_entry_first_x( BackendDB *be,
102 struct berval *base,
103 int scope,
104 Filter *f )
105 {
106 tool_base = base;
107 tool_scope = scope;
108 tool_filter = f;
109
110 return wt_tool_entry_next( be );
111 }
112
113 ID
114 wt_tool_entry_next( BackendDB *be )
115 {
116 int rc;
117 ID id;
118
119 rc = reader->next(reader);
120 switch( rc ){
121 case 0:
122 break;
123 case WT_NOTFOUND:
124 return NOID;
125 default:
126 Debug( LDAP_DEBUG_ANY,
127 "wt_tool_entry_next: next failed: %s (%d)\n",
128 wiredtiger_strerror(rc), rc );
129 return NOID;
130 }
131
132 rc = reader->get_key(reader, &id);
133 if( rc ){
134 Debug( LDAP_DEBUG_ANY,
135 "wt_tool_entry_next: get_key failed: %s (%d)\n",
136 wiredtiger_strerror(rc), rc );
137 }
138 return id;
139 }
140
141 static ber_len_t
142 entry_getlen(unsigned char **buf)
143 {
144 ber_len_t len;
145 int i;
146
147 len = *(*buf)++;
148 if (len <= 0x7f)
149 return len;
150 i = len & 0x7f;
151 len = 0;
152 for (;i > 0; i--) {
153 len <<= 8;
154 len |= *(*buf)++;
155 }
156 return len;
157 }
158
159 int wt_entry_header(WT_ITEM *item, EntryHeader *eh)
160 {
161 unsigned char *ptr = (unsigned char *)item->data;
162
163 /* Some overlays can create empty entries
164 * so don't check for zeros here.
165 */
166 eh->nattrs = entry_getlen(&ptr);
167 eh->nvals = entry_getlen(&ptr);
168 eh->data = (char *)ptr;
169 return LDAP_SUCCESS;
170 }
171
172 Entry *
173 wt_tool_entry_get( BackendDB *be, ID id )
174 {
175 Entry *e = NULL;
176 static EntryHeader eh;
177 int rc, eoff;
178
179 assert( be != NULL );
180 assert( slapMode & SLAP_TOOL_MODE );
181
182 reader->set_key(reader, id);
183 rc = reader->search(reader);
184 if ( rc ) {
185 Debug( LDAP_DEBUG_ANY,
186 "wt_tool_entry_get: search failed: %s (%d)\n",
187 wiredtiger_strerror(rc), rc );
188 goto done;
189 }
190 rc = reader->get_value(reader, &item);
191 if( rc ){
192 Debug( LDAP_DEBUG_ANY,
193 "wt_tool_entry_get: get_value failed: %s (%d)\n",
194 wiredtiger_strerror(rc), rc );
195 goto done;
196 }
197
198 rc = wt_entry_header( &item, &eh );
199 assert( rc == 0 );
200 eoff = eh.data - (char *)item.data;
201
202 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
203 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
204 memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
205 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
206 memcpy(eh.data, item.data, item.size);
207 eh.data += eoff;
208
209 rc = entry_decode( &eh, &e );
210 assert( rc == 0 );
211
212 if( rc == LDAP_SUCCESS ) {
213 e->e_id = id;
214 }
215
216 done:
217 return e;
218 }
219
220 static int wt_tool_next_id(
221 Operation *op,
222 Entry *e,
223 struct berval *text,
224 int hole )
225 {
226 struct berval dn = e->e_name;
227 struct berval ndn = e->e_nname;
228 struct berval pdn, npdn;
229 int rc;
230 ID id = 0;
231 ID pid = 0;
232
233 if(ndn.bv_len == 0){
234 e->e_id = 0;
235 return 0;
236 }
237
238 rc = wt_dn2id(op, wc, &ndn, &id);
239 if(rc == 0){
240 e->e_id = id;
241 }else if( rc == WT_NOTFOUND ){
242 if ( !be_issuffix( op->o_bd, &ndn ) ) {
243 ID eid = e->e_id;
244 dnParent( &dn, &pdn );
245 dnParent( &ndn, &npdn );
246 e->e_name = pdn;
247 e->e_nname = npdn;
248 rc = wt_tool_next_id( op, e, text, 1 );
249 e->e_name = dn;
250 e->e_nname = ndn;
251 if ( rc ) {
252 return rc;
253 }
254 /* If parent didn't exist, it was created just now
255 * and its ID is now in e->e_id. Make sure the current
256 * entry gets added under the new parent ID.
257 */
258 if ( eid != e->e_id ) {
259 pid = e->e_id;
260 }
261 }else{
262 pid = id;
263 }
264 wt_next_id( op->o_bd, &e->e_id );
265 rc = wt_dn2id_add(op, wc, pid, e);
266 if( rc ){
267 snprintf( text->bv_val, text->bv_len,
268 "wt_dn2id_add failed: %s (%d)",
269 wiredtiger_strerror(rc), rc );
270 Debug( LDAP_DEBUG_ANY,
271 "=> wt_tool_next_id: %s\n", text->bv_val );
272 }
273
274 }else if ( !hole ) {
275 unsigned i, j;
276 e->e_id = id;
277
278 for ( i=0; i<nholes; i++) {
279 if ( holes[i].id == e->e_id ) {
280 free(holes[i].dn.bv_val);
281 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
282 holes[j].id = 0;
283 nholes--;
284 break;
285 } else if ( holes[i].id > e->e_id ) {
286 break;
287 }
288 }
289 }
290 return rc;
291 }
292
293 static int
294 wt_tool_index_add(
295 Operation *op,
296 wt_ctx *wc,
297 Entry *e )
298 {
299 return wt_index_entry_add( op, wc, e );
300 }
301
302 ID
303 wt_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
304 {
305 int rc;
306 Operation op = {0};
307 Opheader ohdr = {0};
308
309 if ( slapMode & SLAP_TOOL_DRYRUN )
310 return 0;
311
312 assert( slapMode & SLAP_TOOL_MODE );
313 assert( text != NULL );
314 assert( text->bv_val != NULL );
315 assert( text->bv_val[0] == '\0' ); /* overconservative? */
316
317 Debug( LDAP_DEBUG_TRACE,
318 "=> wt_tool_entry_put: ( \"%s\" )\n", e->e_dn );
319
320 rc = wc->session->begin_transaction(wc->session, NULL);
321 if( rc ){
322 Debug( LDAP_DEBUG_ANY,
323 "wt_dn2id_add: begin_transaction failed: %s (%d)\n",
324 wiredtiger_strerror(rc), rc );
325 return NOID;
326 }
327
328 op.o_hdr = &ohdr;
329 op.o_bd = be;
330 op.o_tmpmemctx = NULL;
331 op.o_tmpmfuncs = &ch_mfuncs;
332
333 rc = wt_tool_next_id( &op, e, text, 0 );
334 if( rc != 0 ) {
335 snprintf( text->bv_val, text->bv_len,
336 "wt_tool_next_id failed: %s (%d)",
337 wiredtiger_strerror(rc), rc );
338 Debug( LDAP_DEBUG_ANY,
339 "=> wt_tool_entry_put: %s\n", text->bv_val );
340 goto done;
341 }
342
343 rc = wt_id2entry_add( &op, wc, e );
344 if( rc != 0 ) {
345 snprintf( text->bv_val, text->bv_len,
346 "id2entry_add failed: %s (%d)",
347 wiredtiger_strerror(rc), rc );
348 Debug( LDAP_DEBUG_ANY,
349 "=> wt_tool_entry_put: %s\n",
350 text->bv_val );
351 goto done;
352 }
353
354 rc = wt_tool_index_add( &op, wc, e );
355 if( rc != 0 ) {
356 snprintf( text->bv_val, text->bv_len,
357 "index_entry_add failed: %s (%d)",
358 rc == LDAP_OTHER ? "Internal error" :
359 wiredtiger_strerror(rc), rc );
360 Debug( LDAP_DEBUG_ANY,
361 "=> wt_tool_entry_put: %s\n", text->bv_val );
362 goto done;
363 }
364
365 done:
366 if ( rc == 0 ){
367 rc = wc->session->commit_transaction(wc->session, NULL);
368 if( rc != 0 ) {
369 snprintf( text->bv_val, text->bv_len,
370 "txn_commit failed: %s (%d)",
371 wiredtiger_strerror(rc), rc );
372 Debug( LDAP_DEBUG_ANY,
373 "=> wt_tool_entry_put: %s\n", text->bv_val );
374 e->e_id = NOID;
375 }
376 }else{
377 rc = wc->session->rollback_transaction(wc->session, NULL);
378 snprintf( text->bv_val, text->bv_len,
379 "txn_aborted! %s (%d)",
380 rc == LDAP_OTHER ? "Internal error" :
381 wiredtiger_strerror(rc), rc );
382 Debug( LDAP_DEBUG_ANY,
383 "=> wt_tool_entry_put: %s\n", text->bv_val );
384 e->e_id = NOID;
385 }
386
387 return e->e_id;
388 }
389
390 int wt_tool_entry_reindex(
391 BackendDB *be,
392 ID id,
393 AttributeDescription **adv )
394 {
395 struct wt_info *wi = (struct wt_info *) be->be_private;
396 int rc;
397 Entry *e;
398 Operation op = {0};
399 Opheader ohdr = {0};
400
401 Debug( LDAP_DEBUG_ARGS,
402 "=> wt_tool_entry_reindex( %ld )\n", (long) id );
403 assert( tool_base == NULL );
404 assert( tool_filter == NULL );
405
406 /* No indexes configured, nothing to do. Could return an
407 * error here to shortcut things.
408 */
409 if (!wi->wi_attrs) {
410 return 0;
411 }
412
413 /* Check for explicit list of attrs to index */
414 if ( adv ) {
415 int i, j, n;
416
417 if ( wi->wi_attrs[0]->ai_desc != adv[0] ) {
418 /* count */
419 for ( n = 0; adv[n]; n++ ) ;
420
421 /* insertion sort */
422 for ( i = 0; i < n; i++ ) {
423 AttributeDescription *ad = adv[i];
424 for ( j = i-1; j>=0; j--) {
425 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
426 adv[j+1] = adv[j];
427 }
428 adv[j+1] = ad;
429 }
430 }
431
432 for ( i = 0; adv[i]; i++ ) {
433 if ( wi->wi_attrs[i]->ai_desc != adv[i] ) {
434 for ( j = i+1; j < wi->wi_nattrs; j++ ) {
435 if ( wi->wi_attrs[j]->ai_desc == adv[i] ) {
436 AttrInfo *ai = wi->wi_attrs[i];
437 wi->wi_attrs[i] = wi->wi_attrs[j];
438 wi->wi_attrs[j] = ai;
439 break;
440 }
441 }
442 if ( j == wi->wi_nattrs ) {
443 Debug( LDAP_DEBUG_ANY,
444 "wt_tool_entry_reindex: no index configured for %s\n",
445 adv[i]->ad_cname.bv_val );
446 return -1;
447 }
448 }
449 }
450 wi->wi_nattrs = i;
451 }
452
453 e = wt_tool_entry_get( be, id );
454
455 if( e == NULL ) {
456 Debug( LDAP_DEBUG_ANY, "=> wt_tool_entry_reindex"
457 ": could not locate id=%ld\n",
458 (long) id );
459 return -1;
460 }
461
462 op.o_hdr = &ohdr;
463 op.o_bd = be;
464 op.o_tmpmemctx = NULL;
465 op.o_tmpmfuncs = &ch_mfuncs;
466
467 rc = wc->session->begin_transaction(wc->session, NULL);
468 if( rc ){
469 Debug( LDAP_DEBUG_ANY,
470 "wt_tool_entry_reindex: begin_transaction failed %s (%d)\n",
471 wiredtiger_strerror(rc), rc );
472 goto done;
473 }
474 Debug( LDAP_DEBUG_TRACE,
475 "=> wt_tool_entry_reindex( %ld, \"%s\" )\n",
476 (long) id, e->e_dn );
477
478 rc = wt_tool_index_add( &op, wc, e );
479
480 done:
481 if ( rc == 0 ){
482 rc = wc->session->commit_transaction(wc->session, NULL);
483 if( rc ) {
484 Debug( LDAP_DEBUG_ANY,
485 "=> wt_tool_entry_reindex: commit_transaction failed %s (%d)\n",
486 wiredtiger_strerror(rc), rc );
487 }
488 }else{
489 rc = wc->session->rollback_transaction(wc->session, NULL);
490 Debug( LDAP_DEBUG_ANY,
491 "=> wt_tool_entry_reindex: rollback transaction %s (%d)\n",
492 wiredtiger_strerror(rc), rc );
493 }
494
495 wt_entry_release( &op, e, 0 );
496
497 return rc;
498 }
499
500 ID wt_tool_dn2id_get(
501 Backend *be,
502 struct berval *dn
503 )
504 {
505 Operation op = {0};
506 Opheader ohdr = {0};
507 ID id;
508 int rc;
509
510 if ( BER_BVISEMPTY(dn) )
511 return 0;
512
513 op.o_hdr = &ohdr;
514 op.o_bd = be;
515 op.o_tmpmemctx = NULL;
516 op.o_tmpmfuncs = &ch_mfuncs;
517
518 rc = wt_dn2id(&op, wc, dn, &id);
519 switch( rc ){
520 case 0:
521 break;
522 case WT_NOTFOUND:
523 return NOID;
524 default:
525 Debug( LDAP_DEBUG_ANY,
526 "wt_tool_entry_get: entry get failed: %s (%d)\n",
527 wiredtiger_strerror(rc), rc );
528 return NOID;
529 }
530 return id;
531 }
532
533 ID wt_tool_entry_modify(
534 BackendDB *be,
535 Entry *e,
536 struct berval *text )
537 {
538 int rc;
539 Operation op = {0};
540 Opheader ohdr = {0};
541
542 assert( be != NULL );
543 assert( slapMode & SLAP_TOOL_MODE );
544
545 assert( text != NULL );
546 assert( text->bv_val != NULL );
547 assert( text->bv_val[0] == '\0' ); /* overconservative? */
548
549 assert ( e->e_id != NOID );
550
551 Debug( LDAP_DEBUG_TRACE,
552 "=> wt_tool_entry_modify( %ld, \"%s\" )\n",
553 (long) e->e_id, e->e_dn );
554
555 rc = wc->session->begin_transaction(wc->session, NULL);
556 if( rc ){
557 Debug( LDAP_DEBUG_ANY, "=> wt_tool_entry_modify"
558 ": begin_transaction failed: %s (%d)\n",
559 wiredtiger_strerror(rc), rc );
560 return NOID;
561 }
562
563 op.o_hdr = &ohdr;
564 op.o_bd = be;
565 op.o_tmpmemctx = NULL;
566 op.o_tmpmfuncs = &ch_mfuncs;
567
568 rc = wt_id2entry_update( &op, wc, e );
569 if( rc != 0 ) {
570 snprintf( text->bv_val, text->bv_len,
571 "id2entry_update failed: %s (%d)",
572 wiredtiger_strerror(rc), rc );
573 Debug( LDAP_DEBUG_ANY, "=> wt_tool_entry_modify: %s\n",
574 text->bv_val );
575 goto done;
576 }
577
578 done:
579 if ( rc == 0 ){
580 rc = wc->session->commit_transaction(wc->session, NULL);
581 if( rc != 0 ) {
582 snprintf( text->bv_val, text->bv_len,
583 "txn_commit failed: %s (%d)",
584 wiredtiger_strerror(rc), rc );
585 Debug( LDAP_DEBUG_ANY, "=> wt_tool_entry_modify: %s\n",
586 text->bv_val );
587 e->e_id = NOID;
588 }
589 }else{
590 rc = wc->session->rollback_transaction(wc->session, NULL);
591 snprintf( text->bv_val, text->bv_len,
592 "txn_aborted! %s (%d)",
593 rc == LDAP_OTHER ? "Internal error" :
594 wiredtiger_strerror(rc), rc );
595 Debug( LDAP_DEBUG_ANY, "=> wt_tool_entry_modify: %s\n",
596 text->bv_val );
597 e->e_id = NOID;
598 }
599
600 return e->e_id;
601 }
602
603 int wt_tool_entry_delete(
604 BackendDB *be,
605 struct berval *ndn,
606 struct berval *text )
607 {
608 struct wt_info *wi = (struct wt_info *) be->be_private;
609 int rc;
610 Operation op = {0};
611 Opheader ohdr = {0};
612 Entry *e = NULL;
613
614 assert( be != NULL );
615 assert( slapMode & SLAP_TOOL_MODE );
616
617 assert( text != NULL );
618 assert( text->bv_val != NULL );
619 assert( text->bv_val[0] == '\0' ); /* overconservative? */
620
621 assert ( ndn != NULL );
622 assert ( ndn->bv_val != NULL );
623
624 Debug( LDAP_DEBUG_TRACE,
625 "=> wt_tool_entry_delete( %s )\n",
626 ndn->bv_val );
627
628 op.o_hdr = &ohdr;
629 op.o_bd = be;
630 op.o_tmpmemctx = NULL;
631 op.o_tmpmfuncs = &ch_mfuncs;
632
633 /* get entry */
634 rc = wt_dn2entry(op.o_bd, wc, ndn, &e);
635 switch( rc ) {
636 case 0:
637 break;
638 case WT_NOTFOUND:
639 Debug( LDAP_DEBUG_ARGS,
640 "<== wt_tool_entry_delete: no such object %s\n",
641 ndn->bv_val);
642 goto done;
643 default:
644 Debug( LDAP_DEBUG_ANY,
645 "wt_tool_entry_delete: error at wt_dn2entry() rc=%d\n",
646 rc );
647 goto done;
648 }
649
650 rc = wt_dn2id_has_children( &op, wc, e->e_id );
651 if( rc != WT_NOTFOUND ) {
652 /* subordinate objects must be deleted first */
653 rc = -1;
654 goto done;
655 }
656
657 rc = wc->session->begin_transaction(wc->session, NULL);
658 if( rc ){
659 Debug( LDAP_DEBUG_ANY,
660 "wt_tool_entry_delete: begin_transaction failed: %s (%d)\n",
661 wiredtiger_strerror(rc), rc );
662 goto done;
663 }
664
665 /* delete from dn2id */
666 rc = wt_dn2id_delete( &op, wc, &e->e_nname);
667 if ( rc ) {
668 Debug( LDAP_DEBUG_TRACE,
669 "<== wt_tool_entry_delete: dn2id failed: %s (%d)\n",
670 wiredtiger_strerror(rc), rc );
671 wc->session->rollback_transaction(wc->session, NULL);
672 goto done;
673 }
674
675 /* delete indices for old attributes */
676 rc = wt_index_entry_del( &op, wc, e );
677 if ( rc ) {
678 Debug( LDAP_DEBUG_TRACE,
679 "<== wt_tool_entry_delete: index delete failed: %s (%d)\n",
680 wiredtiger_strerror(rc), rc );
681 wc->session->rollback_transaction(wc->session, NULL);
682 goto done;
683 }
684
685 /* delete from id2entry */
686 rc = wt_id2entry_delete( &op, wc, e );
687 if ( rc ) {
688 Debug( LDAP_DEBUG_TRACE,
689 "<== wt_tool_entry_delete: id2entry failed: %s (%d)\n",
690 wiredtiger_strerror(rc), rc );
691 wc->session->rollback_transaction(wc->session, NULL);
692 goto done;
693 }
694
695 rc = wc->session->commit_transaction(wc->session, NULL);
696 if( rc != 0 ) {
697 snprintf( text->bv_val, text->bv_len,
698 "txn_commit failed: %s (%d)",
699 wiredtiger_strerror(rc), rc );
700 Debug( LDAP_DEBUG_ANY,
701 "=> wt_tool_entry_delete: %s\n",
702 text->bv_val );
703 goto done;
704 }
705
706 done:
707 /* free entry */
708 if( e != NULL ) {
709 wt_entry_return( e );
710 }
711 return rc;
712 }
713
714
715 /*
716 * Local variables:
717 * indent-tabs-mode: t
718 * tab-width: 4
719 * c-basic-offset: 4
720 * End:
721 */