Debian Codegraph @

servers/slapd/overlays/refint.c

raw
1 /* refint.c - referential integrity module */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2004-2024 The OpenLDAP Foundation.
6 * Portions Copyright 2004 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Symas Corp. for inclusion in
19 * OpenLDAP Software. This work was sponsored by Hewlett-Packard.
20 */
21
22 #include "portable.h"
23
24 /* This module maintains referential integrity for a set of
25 * DN-valued attributes by searching for all references to a given
26 * DN whenever the DN is changed or its entry is deleted, and making
27 * the appropriate update.
28 *
29 * Updates are performed using the database rootdn in a separate task
30 * to allow the original operation to complete immediately.
31 */
32
33 #ifdef SLAPD_OVER_REFINT
34
35 #include <stdio.h>
36
37 #include <ac/string.h>
38 #include <ac/socket.h>
39
40 #include "slap.h"
41 #include "slap-config.h"
42 #include "ldap_rq.h"
43
44 static slap_overinst refint;
45
46 /* The DN to use in the ModifiersName for all refint updates */
47 static BerValue refint_dn = BER_BVC("cn=Referential Integrity Overlay");
48 static BerValue refint_ndn = BER_BVC("cn=referential integrity overlay");
49
50 typedef struct refint_attrs_s {
51 struct refint_attrs_s *next;
52 AttributeDescription *attr;
53 BerVarray old_vals;
54 BerVarray old_nvals;
55 BerVarray new_vals;
56 BerVarray new_nvals;
57 int ra_numvals;
58 int dont_empty;
59 } refint_attrs;
60
61 typedef struct dependents_s {
62 struct dependents_s *next;
63 BerValue dn; /* target dn */
64 BerValue ndn;
65 refint_attrs *attrs;
66 } dependent_data;
67
68 typedef struct refint_q {
69 struct refint_q *next;
70 struct refint_data_s *rdata;
71 dependent_data *attrs; /* entries and attrs returned from callback */
72 BackendDB *db;
73 BerValue olddn;
74 BerValue oldndn;
75 BerValue newdn;
76 BerValue newndn;
77 int do_sub;
78 } refint_q;
79
80 typedef struct refint_data_s {
81 struct refint_attrs_s *attrs; /* list of known attrs */
82 BerValue dn; /* basedn in parent, */
83 BerValue nothing; /* the nothing value, if needed */
84 BerValue nnothing; /* normalized nothingness */
85 BerValue refint_dn; /* modifier's name */
86 BerValue refint_ndn; /* normalized modifier's name */
87 struct re_s *qtask;
88 refint_q *qhead;
89 refint_q *qtail;
90 BackendDB *db;
91 ldap_pvt_thread_mutex_t qmutex;
92 } refint_data;
93
94 typedef struct refint_pre_s {
95 slap_overinst *on;
96 int do_sub;
97 } refint_pre;
98
99 #define RUNQ_INTERVAL 36000 /* a long time */
100
101 static MatchingRule *mr_dnSubtreeMatch;
102
103 enum {
104 REFINT_ATTRS = 1,
105 REFINT_NOTHING,
106 REFINT_MODIFIERSNAME
107 };
108
109 static ConfigDriver refint_cf_gen;
110
111 static ConfigTable refintcfg[] = {
112 { "refint_attributes", "attribute...", 2, 0, 0,
113 ARG_MAGIC|REFINT_ATTRS, refint_cf_gen,
114 "( OLcfgOvAt:11.1 NAME 'olcRefintAttribute' "
115 "DESC 'Attributes for referential integrity' "
116 "EQUALITY caseIgnoreMatch "
117 "SYNTAX OMsDirectoryString )", NULL, NULL },
118 { "refint_nothing", "string", 2, 2, 0,
119 ARG_DN|ARG_QUOTE|ARG_MAGIC|REFINT_NOTHING, refint_cf_gen,
120 "( OLcfgOvAt:11.2 NAME 'olcRefintNothing' "
121 "DESC 'Replacement DN to supply when needed' "
122 "EQUALITY distinguishedNameMatch "
123 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
124 { "refint_modifiersName", "DN", 2, 2, 0,
125 ARG_DN|ARG_QUOTE|ARG_MAGIC|REFINT_MODIFIERSNAME, refint_cf_gen,
126 "( OLcfgOvAt:11.3 NAME 'olcRefintModifiersName' "
127 "DESC 'The DN to use as modifiersName' "
128 "EQUALITY distinguishedNameMatch "
129 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
130 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
131 };
132
133 static ConfigOCs refintocs[] = {
134 { "( OLcfgOvOc:11.1 "
135 "NAME 'olcRefintConfig' "
136 "DESC 'Referential integrity configuration' "
137 "SUP olcOverlayConfig "
138 "MAY ( olcRefintAttribute "
139 "$ olcRefintNothing "
140 "$ olcRefintModifiersName "
141 ") )",
142 Cft_Overlay, refintcfg },
143 { NULL, 0, NULL }
144 };
145
146 static int
147 refint_cf_gen(ConfigArgs *c)
148 {
149 slap_overinst *on = (slap_overinst *)c->bi;
150 refint_data *dd = (refint_data *)on->on_bi.bi_private;
151 refint_attrs *ip, *pip, **pipp = NULL;
152 AttributeDescription *ad;
153 const char *text;
154 int rc = ARG_BAD_CONF;
155 int i;
156
157 switch ( c->op ) {
158 case SLAP_CONFIG_EMIT:
159 switch ( c->type ) {
160 case REFINT_ATTRS:
161 ip = dd->attrs;
162 while ( ip ) {
163 value_add_one( &c->rvalue_vals,
164 &ip->attr->ad_cname );
165 ip = ip->next;
166 }
167 rc = 0;
168 break;
169 case REFINT_NOTHING:
170 if ( !BER_BVISEMPTY( &dd->nothing )) {
171 rc = value_add_one( &c->rvalue_vals,
172 &dd->nothing );
173 if ( rc ) return rc;
174 rc = value_add_one( &c->rvalue_nvals,
175 &dd->nnothing );
176 return rc;
177 }
178 rc = 0;
179 break;
180 case REFINT_MODIFIERSNAME:
181 if ( !BER_BVISEMPTY( &dd->refint_dn )) {
182 rc = value_add_one( &c->rvalue_vals,
183 &dd->refint_dn );
184 if ( rc ) return rc;
185 rc = value_add_one( &c->rvalue_nvals,
186 &dd->refint_ndn );
187 return rc;
188 }
189 rc = 0;
190 break;
191 default:
192 abort ();
193 }
194 break;
195 case LDAP_MOD_DELETE:
196 switch ( c->type ) {
197 case REFINT_ATTRS:
198 pipp = &dd->attrs;
199 if ( c->valx < 0 ) {
200 ip = *pipp;
201 *pipp = NULL;
202 while ( ip ) {
203 pip = ip;
204 ip = ip->next;
205 ch_free ( pip );
206 }
207 } else {
208 /* delete from linked list */
209 for ( i=0; i < c->valx; ++i ) {
210 pipp = &(*pipp)->next;
211 }
212 ip = *pipp;
213 *pipp = (*pipp)->next;
214
215 /* AttributeDescriptions are global so
216 * shouldn't be freed here... */
217 ch_free ( ip );
218 }
219 rc = 0;
220 break;
221 case REFINT_NOTHING:
222 ch_free( dd->nothing.bv_val );
223 ch_free( dd->nnothing.bv_val );
224 BER_BVZERO( &dd->nothing );
225 BER_BVZERO( &dd->nnothing );
226 rc = 0;
227 break;
228 case REFINT_MODIFIERSNAME:
229 ch_free( dd->refint_dn.bv_val );
230 ch_free( dd->refint_ndn.bv_val );
231 BER_BVZERO( &dd->refint_dn );
232 BER_BVZERO( &dd->refint_ndn );
233 rc = 0;
234 break;
235 default:
236 abort ();
237 }
238 break;
239 case SLAP_CONFIG_ADD:
240 /* fallthru to LDAP_MOD_ADD */
241 case LDAP_MOD_ADD:
242 switch ( c->type ) {
243 case REFINT_ATTRS:
244 rc = 0;
245 if ( c->op != SLAP_CONFIG_ADD && c->argc > 2 ) {
246 /* We wouldn't know how to delete these values later */
247 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
248 "Supplying multiple names in a single %s value is "
249 "unsupported and will be disallowed in a future version\n",
250 c->argv[0] );
251 }
252
253 for ( i=1; i < c->argc; ++i ) {
254 ad = NULL;
255 if ( slap_str2ad ( c->argv[i], &ad, &text )
256 == LDAP_SUCCESS) {
257 ip = ch_malloc (
258 sizeof ( refint_attrs ) );
259 ip->attr = ad;
260
261 for ( pipp = &dd->attrs; *pipp; pipp = &(*pipp)->next )
262 /* Get to the end */ ;
263 ip->next = *pipp;
264 *pipp = ip;
265 } else {
266 snprintf( c->cr_msg, sizeof( c->cr_msg ),
267 "%s <%s>: %s", c->argv[0], c->argv[i], text );
268 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
269 "%s: %s\n", c->log, c->cr_msg );
270 rc = ARG_BAD_CONF;
271 }
272 }
273 break;
274 case REFINT_NOTHING:
275 if ( !BER_BVISNULL( &c->value_ndn )) {
276 ch_free ( dd->nothing.bv_val );
277 ch_free ( dd->nnothing.bv_val );
278 dd->nothing = c->value_dn;
279 dd->nnothing = c->value_ndn;
280 rc = 0;
281 } else {
282 rc = ARG_BAD_CONF;
283 }
284 break;
285 case REFINT_MODIFIERSNAME:
286 if ( !BER_BVISNULL( &c->value_ndn )) {
287 ch_free( dd->refint_dn.bv_val );
288 ch_free( dd->refint_ndn.bv_val );
289 dd->refint_dn = c->value_dn;
290 dd->refint_ndn = c->value_ndn;
291 rc = 0;
292 } else {
293 rc = ARG_BAD_CONF;
294 }
295 break;
296 default:
297 abort ();
298 }
299 break;
300 default:
301 abort ();
302 }
303
304 return rc;
305 }
306
307 /*
308 ** allocate new refint_data;
309 ** store in on_bi.bi_private;
310 **
311 */
312
313 static int
314 refint_db_init(
315 BackendDB *be,
316 ConfigReply *cr
317 )
318 {
319 slap_overinst *on = (slap_overinst *)be->bd_info;
320 refint_data *id = ch_calloc(1,sizeof(refint_data));
321
322 on->on_bi.bi_private = id;
323 ldap_pvt_thread_mutex_init( &id->qmutex );
324 return(0);
325 }
326
327 static int
328 refint_db_destroy(
329 BackendDB *be,
330 ConfigReply *cr
331 )
332 {
333 slap_overinst *on = (slap_overinst *)be->bd_info;
334
335 if ( on->on_bi.bi_private ) {
336 refint_data *id = on->on_bi.bi_private;
337 refint_attrs *ii, *ij;
338
339 on->on_bi.bi_private = NULL;
340 ldap_pvt_thread_mutex_destroy( &id->qmutex );
341
342 for(ii = id->attrs; ii; ii = ij) {
343 ij = ii->next;
344 ch_free(ii);
345 }
346
347 ch_free( id->nothing.bv_val );
348 BER_BVZERO( &id->nothing );
349 ch_free( id->nnothing.bv_val );
350 BER_BVZERO( &id->nnothing );
351
352 ch_free( id );
353 }
354 return(0);
355 }
356
357 /*
358 ** initialize, copy basedn if not already set
359 **
360 */
361
362 static int
363 refint_open(
364 BackendDB *be,
365 ConfigReply *cr
366 )
367 {
368 slap_overinst *on = (slap_overinst *)be->bd_info;
369 refint_data *id = on->on_bi.bi_private;
370
371 if ( BER_BVISNULL( &id->dn )) {
372 if ( BER_BVISNULL( &be->be_nsuffix[0] ))
373 return -1;
374 ber_dupbv( &id->dn, &be->be_nsuffix[0] );
375 }
376 if ( BER_BVISNULL( &id->refint_dn ) ) {
377 ber_dupbv( &id->refint_dn, &refint_dn );
378 ber_dupbv( &id->refint_ndn, &refint_ndn );
379 }
380
381 /*
382 ** find the backend that matches our configured basedn;
383 ** make sure it exists and has search and modify methods;
384 **
385 */
386
387 if ( on->on_info->oi_origdb != frontendDB ) {
388 BackendDB *db = select_backend(&id->dn, 1);
389
390 if ( db ) {
391 BackendInfo *bi;
392 if ( db == be )
393 bi = on->on_info->oi_orig;
394 else
395 bi = db->bd_info;
396 if ( !bi->bi_op_search || !bi->bi_op_modify ) {
397 Debug( LDAP_DEBUG_CONFIG,
398 "refint_response: backend missing search and/or modify\n" );
399 return -1;
400 }
401 id->db = db;
402 } else {
403 Debug( LDAP_DEBUG_CONFIG,
404 "refint_response: no backend for our baseDN %s??\n",
405 id->dn.bv_val );
406 return -1;
407 }
408 }
409 return(0);
410 }
411
412
413 /*
414 ** free our basedn;
415 ** free our refintdn
416 **
417 */
418
419 static int
420 refint_close(
421 BackendDB *be,
422 ConfigReply *cr
423 )
424 {
425 slap_overinst *on = (slap_overinst *) be->bd_info;
426 refint_data *id = on->on_bi.bi_private;
427
428 ch_free( id->dn.bv_val );
429 BER_BVZERO( &id->dn );
430 ch_free( id->refint_dn.bv_val );
431 BER_BVZERO( &id->refint_dn );
432 ch_free( id->refint_ndn.bv_val );
433 BER_BVZERO( &id->refint_ndn );
434
435 return(0);
436 }
437
438 /*
439 ** search callback
440 ** generates a list of Attributes from search results
441 */
442
443 static int
444 refint_search_cb(
445 Operation *op,
446 SlapReply *rs
447 )
448 {
449 Attribute *a;
450 BerVarray b = NULL;
451 refint_q *rq = op->o_callback->sc_private;
452 refint_data *dd = rq->rdata;
453 refint_attrs *ia, *da = dd->attrs, *na;
454 dependent_data *ip;
455 int i;
456
457 Debug(LDAP_DEBUG_TRACE, "refint_search_cb <%s>\n",
458 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "NOTHING" );
459
460 if (rs->sr_type != REP_SEARCH || !rs->sr_entry) return(0);
461
462 /*
463 ** foreach configured attribute type:
464 ** if this attr exists in the search result,
465 ** and it has a value matching the target:
466 ** allocate an attr;
467 ** save/build DNs of any subordinate matches;
468 ** handle special case: found exact + subordinate match;
469 ** handle olcRefintNothing;
470 **
471 */
472
473 ip = op->o_tmpalloc(sizeof(dependent_data), op->o_tmpmemctx );
474 ber_dupbv_x( &ip->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
475 ber_dupbv_x( &ip->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
476 ip->next = rq->attrs;
477 rq->attrs = ip;
478 ip->attrs = NULL;
479 for(ia = da; ia; ia = ia->next) {
480 if ( (a = attr_find(rs->sr_entry->e_attrs, ia->attr) ) ) {
481 int exact = -1, is_exact;
482
483 na = NULL;
484
485 /* Are we doing subtree matching or simple equality? */
486 if ( rq->do_sub ) {
487 for(i = 0, b = a->a_nvals; b[i].bv_val; i++) {
488 if(dnIsSuffix(&b[i], &rq->oldndn)) {
489 is_exact = b[i].bv_len == rq->oldndn.bv_len;
490
491 /* Paranoia: skip buggy duplicate exact match,
492 * it would break ra_numvals
493 */
494 if ( is_exact && exact >= 0 )
495 continue;
496
497 /* first match? create structure */
498 if ( na == NULL ) {
499 na = op->o_tmpcalloc( 1,
500 sizeof( refint_attrs ),
501 op->o_tmpmemctx );
502 na->next = ip->attrs;
503 ip->attrs = na;
504 na->attr = ia->attr;
505 }
506
507 na->ra_numvals++;
508
509 if ( is_exact ) {
510 /* Exact match: refint_repair will deduce the DNs */
511 exact = i;
512
513 } else {
514 /* Subordinate match */
515 struct berval newsub, newdn, olddn, oldndn;
516
517 /* Save old DN */
518 ber_dupbv_x( &olddn, &a->a_vals[i], op->o_tmpmemctx );
519 ber_bvarray_add_x( &na->old_vals, &olddn, op->o_tmpmemctx );
520
521 ber_dupbv_x( &oldndn, &a->a_nvals[i], op->o_tmpmemctx );
522 ber_bvarray_add_x( &na->old_nvals, &oldndn, op->o_tmpmemctx );
523
524 if ( BER_BVISEMPTY( &rq->newdn ) )
525 continue;
526
527 /* Rename subordinate match: Build new DN */
528 newsub = a->a_vals[i];
529 newsub.bv_len -= rq->olddn.bv_len + 1;
530 build_new_dn( &newdn, &rq->newdn, &newsub, op->o_tmpmemctx );
531 ber_bvarray_add_x( &na->new_vals, &newdn, op->o_tmpmemctx );
532
533 newsub = a->a_nvals[i];
534 newsub.bv_len -= rq->oldndn.bv_len + 1;
535 build_new_dn( &newdn, &rq->newndn, &newsub, op->o_tmpmemctx );
536 ber_bvarray_add_x( &na->new_nvals, &newdn, op->o_tmpmemctx );
537 }
538 }
539 }
540
541 /* If we got both subordinate and exact match,
542 * refint_repair won't special-case the exact match */
543 if ( exact >= 0 && na->old_vals ) {
544 struct berval dn;
545
546 ber_dupbv_x( &dn, &a->a_vals[exact], op->o_tmpmemctx );
547 ber_bvarray_add_x( &na->old_vals, &dn, op->o_tmpmemctx );
548 ber_dupbv_x( &dn, &a->a_nvals[exact], op->o_tmpmemctx );
549 ber_bvarray_add_x( &na->old_nvals, &dn, op->o_tmpmemctx );
550
551 if ( !BER_BVISEMPTY( &rq->newdn ) ) {
552 ber_dupbv_x( &dn, &rq->newdn, op->o_tmpmemctx );
553 ber_bvarray_add_x( &na->new_vals, &dn, op->o_tmpmemctx );
554 ber_dupbv_x( &dn, &rq->newndn, op->o_tmpmemctx );
555 ber_bvarray_add_x( &na->new_nvals, &dn, op->o_tmpmemctx );
556 }
557 }
558 } else {
559 /* entry has no children, just equality matching */
560 is_exact = attr_valfind( a,
561 SLAP_MR_EQUALITY|SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH|
562 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH, &rq->oldndn, &i, NULL );
563 if ( is_exact == LDAP_SUCCESS ) {
564 na = op->o_tmpcalloc( 1,
565 sizeof( refint_attrs ),
566 op->o_tmpmemctx );
567 na->next = ip->attrs;
568 ip->attrs = na;
569 na->attr = ia->attr;
570 na->ra_numvals = 1;
571 }
572 }
573
574 /* Deleting/replacing all values and a nothing DN is configured? */
575 if ( na && na->ra_numvals == a->a_numvals && !BER_BVISNULL(&dd->nothing) )
576 na->dont_empty = 1;
577
578 Debug( LDAP_DEBUG_TRACE, "refint_search_cb: %s: %s (#%d)\n",
579 a->a_desc->ad_cname.bv_val, rq->olddn.bv_val, i );
580 }
581 }
582
583 return(0);
584 }
585
586 static int
587 refint_repair(
588 Operation *op,
589 refint_data *id,
590 refint_q *rq )
591 {
592 dependent_data *dp;
593 SlapReply rs = {REP_RESULT};
594 Operation op2;
595 unsigned long opid;
596 int rc;
597 int cache;
598
599 op->o_callback->sc_response = refint_search_cb;
600 op->o_req_dn = op->o_bd->be_suffix[ 0 ];
601 op->o_req_ndn = op->o_bd->be_nsuffix[ 0 ];
602 op->o_dn = op->o_bd->be_rootdn;
603 op->o_ndn = op->o_bd->be_rootndn;
604 cache = op->o_do_not_cache;
605 op->o_do_not_cache = 1;
606 op->o_abandon = 0;
607
608 /* search */
609 rc = op->o_bd->be_search( op, &rs );
610 op->o_do_not_cache = cache;
611
612 if ( rc != LDAP_SUCCESS ) {
613 Debug( LDAP_DEBUG_TRACE,
614 "refint_repair: search failed: %d\n",
615 rc );
616 return rc;
617 }
618
619 /* safety? paranoid just in case */
620 if ( op->o_callback->sc_private == NULL ) {
621 Debug( LDAP_DEBUG_TRACE,
622 "refint_repair: callback wiped out sc_private?!\n" );
623 return 0;
624 }
625
626 /* Set up the Modify requests */
627 op->o_callback->sc_response = &slap_null_cb;
628
629 /*
630 * [our search callback builds a list of attrs]
631 * foreach attr:
632 * make sure its dn has a backend;
633 * build Modification* chain;
634 * call the backend modify function;
635 *
636 */
637
638 opid = op->o_opid;
639 op2 = *op;
640 for ( dp = rq->attrs; dp; dp = dp->next ) {
641 SlapReply rs2 = {REP_RESULT};
642 refint_attrs *ra;
643 Modifications *m;
644
645 if ( dp->attrs == NULL ) continue; /* TODO: Is this needed? */
646
647 op2.o_bd = select_backend( &dp->ndn, 1 );
648 if ( !op2.o_bd ) {
649 Debug( LDAP_DEBUG_TRACE,
650 "refint_repair: no backend for DN %s!\n",
651 dp->dn.bv_val );
652 continue;
653 }
654 op2.o_tag = LDAP_REQ_MODIFY;
655 op2.orm_modlist = NULL;
656 op2.o_req_dn = dp->dn;
657 op2.o_req_ndn = dp->ndn;
658 /* Internal ops, never replicate these */
659 op2.orm_no_opattrs = 1;
660 op2.o_dont_replicate = 1;
661 op2.o_opid = 0;
662
663 /* Set our ModifiersName */
664 if ( SLAP_LASTMOD( op->o_bd ) ) {
665 m = op2.o_tmpalloc( sizeof(Modifications) +
666 4*sizeof(BerValue), op2.o_tmpmemctx );
667 m->sml_next = op2.orm_modlist;
668 op2.orm_modlist = m;
669 m->sml_op = LDAP_MOD_REPLACE;
670 m->sml_flags = SLAP_MOD_INTERNAL;
671 m->sml_desc = slap_schema.si_ad_modifiersName;
672 m->sml_type = m->sml_desc->ad_cname;
673 m->sml_numvals = 1;
674 m->sml_values = (BerVarray)(m+1);
675 m->sml_nvalues = m->sml_values+2;
676 BER_BVZERO( &m->sml_values[1] );
677 BER_BVZERO( &m->sml_nvalues[1] );
678 m->sml_values[0] = id->refint_dn;
679 m->sml_nvalues[0] = id->refint_ndn;
680 }
681
682 for ( ra = dp->attrs; ra; ra = ra->next ) {
683 size_t len;
684
685 /* Add values */
686 if ( ra->dont_empty || !BER_BVISEMPTY( &rq->newdn ) ) {
687 len = sizeof(Modifications);
688
689 if ( ra->new_vals == NULL ) {
690 len += 4*sizeof(BerValue);
691 }
692
693 m = op2.o_tmpalloc( len, op2.o_tmpmemctx );
694 m->sml_next = op2.orm_modlist;
695 op2.orm_modlist = m;
696 m->sml_op = LDAP_MOD_ADD;
697 m->sml_flags = 0;
698 m->sml_desc = ra->attr;
699 m->sml_type = ra->attr->ad_cname;
700 if ( ra->new_vals == NULL ) {
701 m->sml_values = (BerVarray)(m+1);
702 m->sml_nvalues = m->sml_values+2;
703 BER_BVZERO( &m->sml_values[1] );
704 BER_BVZERO( &m->sml_nvalues[1] );
705 m->sml_numvals = 1;
706 if ( BER_BVISEMPTY( &rq->newdn ) ) {
707 m->sml_values[0] = id->nothing;
708 m->sml_nvalues[0] = id->nnothing;
709 } else {
710 m->sml_values[0] = rq->newdn;
711 m->sml_nvalues[0] = rq->newndn;
712 }
713 } else {
714 m->sml_values = ra->new_vals;
715 m->sml_nvalues = ra->new_nvals;
716 m->sml_numvals = ra->ra_numvals;
717 }
718 }
719
720 /* Delete values */
721 len = sizeof(Modifications);
722 if ( ra->old_vals == NULL ) {
723 len += 4*sizeof(BerValue);
724 }
725 m = op2.o_tmpalloc( len, op2.o_tmpmemctx );
726 m->sml_next = op2.orm_modlist;
727 op2.orm_modlist = m;
728 m->sml_op = LDAP_MOD_DELETE;
729 m->sml_flags = 0;
730 m->sml_desc = ra->attr;
731 m->sml_type = ra->attr->ad_cname;
732 if ( ra->old_vals == NULL ) {
733 m->sml_numvals = 1;
734 m->sml_values = (BerVarray)(m+1);
735 m->sml_nvalues = m->sml_values+2;
736 m->sml_values[0] = rq->olddn;
737 m->sml_nvalues[0] = rq->oldndn;
738 BER_BVZERO( &m->sml_values[1] );
739 BER_BVZERO( &m->sml_nvalues[1] );
740 } else {
741 m->sml_values = ra->old_vals;
742 m->sml_nvalues = ra->old_nvals;
743 m->sml_numvals = ra->ra_numvals;
744 }
745 }
746
747 op2.o_dn = op2.o_bd->be_rootdn;
748 op2.o_ndn = op2.o_bd->be_rootndn;
749 rc = op2.o_bd->be_modify( &op2, &rs2 );
750 if ( rc != LDAP_SUCCESS ) {
751 Debug( LDAP_DEBUG_TRACE,
752 "refint_repair: dependent modify failed: %d\n",
753 rs2.sr_err );
754 }
755
756 while ( ( m = op2.orm_modlist ) ) {
757 op2.orm_modlist = m->sml_next;
758 op2.o_tmpfree( m, op2.o_tmpmemctx );
759 }
760 }
761 op2.o_opid = opid;
762
763 return 0;
764 }
765
766 static void *
767 refint_qtask( void *ctx, void *arg )
768 {
769 struct re_s *rtask = arg;
770 refint_data *id = rtask->arg;
771 Connection conn = {0};
772 OperationBuffer opbuf;
773 Operation *op;
774 slap_callback cb = { NULL, NULL, NULL, NULL };
775 Filter ftop, *fptr;
776 refint_q *rq;
777 refint_attrs *ip;
778 int pausing = 0, rc = 0;
779
780 connection_fake_init( &conn, &opbuf, ctx );
781 op = &opbuf.ob_op;
782
783 /*
784 ** build a search filter for all configured attributes;
785 ** populate our Operation;
786 ** pass our data (attr list, dn) to backend via sc_private;
787 ** call the backend search function;
788 ** nb: (|(one=thing)) is valid, but do smart formatting anyway;
789 ** nb: 16 is arbitrarily a dozen or so extra bytes;
790 **
791 */
792
793 ftop.f_choice = LDAP_FILTER_OR;
794 ftop.f_next = NULL;
795 ftop.f_or = NULL;
796 op->ors_filter = &ftop;
797 for(ip = id->attrs; ip; ip = ip->next) {
798 /* this filter can be either EQUALITY or EXT */
799 fptr = op->o_tmpcalloc( sizeof(Filter) + sizeof(MatchingRuleAssertion),
800 1, op->o_tmpmemctx );
801 fptr->f_mra = (MatchingRuleAssertion *)(fptr+1);
802 fptr->f_mr_rule = mr_dnSubtreeMatch;
803 fptr->f_mr_rule_text = mr_dnSubtreeMatch->smr_bvoid;
804 fptr->f_mr_desc = ip->attr;
805 fptr->f_mr_dnattrs = 0;
806 fptr->f_next = ftop.f_or;
807 ftop.f_or = fptr;
808 }
809
810 for (;;) {
811 dependent_data *dp, *dp_next;
812 refint_attrs *ra, *ra_next;
813
814 if ( ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 ) {
815 pausing = 1;
816 break;
817 }
818
819 /* Dequeue an op */
820 ldap_pvt_thread_mutex_lock( &id->qmutex );
821 rq = id->qhead;
822 if ( rq ) {
823 id->qhead = rq->next;
824 if ( !id->qhead )
825 id->qtail = NULL;
826 }
827 ldap_pvt_thread_mutex_unlock( &id->qmutex );
828 if ( !rq )
829 break;
830
831 for (fptr = ftop.f_or; fptr; fptr = fptr->f_next ) {
832 fptr->f_mr_value = rq->oldndn;
833 /* Use (attr:dnSubtreeMatch:=value) to catch subtree rename
834 * and subtree delete where supported */
835 if (rq->do_sub)
836 fptr->f_choice = LDAP_FILTER_EXT;
837 else
838 fptr->f_choice = LDAP_FILTER_EQUALITY;
839 }
840
841 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
842
843 /* callback gets the searched dn instead */
844 cb.sc_private = rq;
845 cb.sc_response = refint_search_cb;
846 op->o_callback = &cb;
847 op->o_tag = LDAP_REQ_SEARCH;
848 op->ors_scope = LDAP_SCOPE_SUBTREE;
849 op->ors_deref = LDAP_DEREF_NEVER;
850 op->ors_limit = NULL;
851 op->ors_slimit = SLAP_NO_LIMIT;
852 op->ors_tlimit = SLAP_NO_LIMIT;
853
854 /* no attrs! */
855 op->ors_attrs = slap_anlist_no_attrs;
856
857 slap_op_time( &op->o_time, &op->o_tincr );
858
859 if ( rq->db != NULL ) {
860 op->o_bd = rq->db;
861 rc = refint_repair( op, id, rq );
862
863 } else {
864 BackendDB *be;
865
866 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
867 /* we may want to skip cn=config */
868 if ( be == LDAP_STAILQ_FIRST(&backendDB) ) {
869 continue;
870 }
871
872 if ( be->be_search && be->be_modify ) {
873 op->o_bd = be;
874 rc = refint_repair( op, id, rq );
875 }
876 }
877 }
878
879 for ( dp = rq->attrs; dp; dp = dp_next ) {
880 dp_next = dp->next;
881 for ( ra = dp->attrs; ra; ra = ra_next ) {
882 ra_next = ra->next;
883 ber_bvarray_free_x( ra->new_nvals, op->o_tmpmemctx );
884 ber_bvarray_free_x( ra->new_vals, op->o_tmpmemctx );
885 ber_bvarray_free_x( ra->old_nvals, op->o_tmpmemctx );
886 ber_bvarray_free_x( ra->old_vals, op->o_tmpmemctx );
887 op->o_tmpfree( ra, op->o_tmpmemctx );
888 }
889 op->o_tmpfree( dp->ndn.bv_val, op->o_tmpmemctx );
890 op->o_tmpfree( dp->dn.bv_val, op->o_tmpmemctx );
891 op->o_tmpfree( dp, op->o_tmpmemctx );
892 }
893 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
894 if ( rc == LDAP_BUSY ) {
895 pausing = 1;
896 /* re-queue this op */
897 ldap_pvt_thread_mutex_lock( &id->qmutex );
898 rq->next = id->qhead;
899 id->qhead = rq;
900 if ( !id->qtail )
901 id->qtail = rq;
902 ldap_pvt_thread_mutex_unlock( &id->qmutex );
903 break;
904 }
905
906 if ( !BER_BVISNULL( &rq->newndn )) {
907 ch_free( rq->newndn.bv_val );
908 ch_free( rq->newdn.bv_val );
909 }
910 ch_free( rq->oldndn.bv_val );
911 ch_free( rq->olddn.bv_val );
912 ch_free( rq );
913 }
914
915 /* free filter */
916 for ( fptr = ftop.f_or; fptr; ) {
917 Filter *f_next = fptr->f_next;
918 op->o_tmpfree( fptr, op->o_tmpmemctx );
919 fptr = f_next;
920 }
921
922 /* wait until we get explicitly scheduled again */
923 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
924 ldap_pvt_runqueue_stoptask( &slapd_rq, id->qtask );
925 if ( pausing ) {
926 /* try to run again as soon as the pause is done */
927 id->qtask->interval.tv_sec = 0;
928 ldap_pvt_runqueue_resched( &slapd_rq, id->qtask, 0 );
929 id->qtask->interval.tv_sec = RUNQ_INTERVAL;
930 } else {
931 ldap_pvt_runqueue_resched( &slapd_rq,id->qtask, 1 );
932 }
933 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
934
935 return NULL;
936 }
937
938 /*
939 ** refint_response
940 ** search for matching records and modify them
941 */
942
943 static int
944 refint_response(
945 Operation *op,
946 SlapReply *rs
947 )
948 {
949 refint_pre *rp;
950 slap_overinst *on;
951 refint_data *id;
952 refint_q *rq;
953 refint_attrs *ip;
954 int ac;
955
956 /* If the main op failed or is not a Delete or ModRdn, ignore it */
957 if (( op->o_tag != LDAP_REQ_DELETE && op->o_tag != LDAP_REQ_MODRDN ) ||
958 rs->sr_err != LDAP_SUCCESS )
959 return SLAP_CB_CONTINUE;
960
961 rp = op->o_callback->sc_private;
962 on = rp->on;
963 id = on->on_bi.bi_private;
964
965 rq = ch_calloc( 1, sizeof( refint_q ));
966 ber_dupbv( &rq->olddn, &op->o_req_dn );
967 ber_dupbv( &rq->oldndn, &op->o_req_ndn );
968 rq->db = id->db;
969 rq->rdata = id;
970 rq->do_sub = rp->do_sub;
971
972 if ( op->o_tag == LDAP_REQ_MODRDN ) {
973 ber_dupbv( &rq->newdn, &op->orr_newDN );
974 ber_dupbv( &rq->newndn, &op->orr_nnewDN );
975 }
976
977 ldap_pvt_thread_mutex_lock( &id->qmutex );
978 if ( id->qtail ) {
979 id->qtail->next = rq;
980 } else {
981 id->qhead = rq;
982 }
983 id->qtail = rq;
984 ldap_pvt_thread_mutex_unlock( &id->qmutex );
985
986 ac = 0;
987 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
988 if ( !id->qtask ) {
989 id->qtask = ldap_pvt_runqueue_insert( &slapd_rq, RUNQ_INTERVAL,
990 refint_qtask, id, "refint_qtask",
991 op->o_bd->be_suffix[0].bv_val );
992 ac = 1;
993 } else {
994 if ( !ldap_pvt_runqueue_isrunning( &slapd_rq, id->qtask ) &&
995 !id->qtask->next_sched.tv_sec ) {
996 id->qtask->interval.tv_sec = 0;
997 ldap_pvt_runqueue_resched( &slapd_rq, id->qtask, 0 );
998 id->qtask->interval.tv_sec = RUNQ_INTERVAL;
999 ac = 1;
1000 }
1001 }
1002 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1003 if ( ac )
1004 slap_wake_listener();
1005
1006 return SLAP_CB_CONTINUE;
1007 }
1008
1009 /* Check if the target entry exists and has children.
1010 * Do nothing if target doesn't exist.
1011 */
1012 static int
1013 refint_preop(
1014 Operation *op,
1015 SlapReply *rs
1016 )
1017 {
1018 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1019 refint_data *id = on->on_bi.bi_private;
1020 Entry *e;
1021 int rc;
1022
1023 /* are any attrs configured? */
1024 if ( !id->attrs )
1025 return SLAP_CB_CONTINUE;
1026
1027 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
1028 if ( rc == LDAP_SUCCESS ) {
1029 slap_callback *sc = op->o_tmpcalloc( 1,
1030 sizeof(slap_callback)+sizeof(refint_pre), op->o_tmpmemctx );
1031 refint_pre *rp = (refint_pre *)(sc+1);
1032 rp->on = on;
1033 rp->do_sub = 1; /* assume there are children */
1034 if ( op->o_bd->be_has_subordinates ) {
1035 int has = 0;
1036 rc = op->o_bd->be_has_subordinates( op, e, &has );
1037 /* there definitely are not children */
1038 if ( rc == LDAP_SUCCESS && has == LDAP_COMPARE_FALSE )
1039 rp->do_sub = 0;
1040 }
1041 overlay_entry_release_ov( op, e, 0, on );
1042 sc->sc_response = refint_response;
1043 sc->sc_private = rp;
1044 sc->sc_next = op->o_callback;
1045 op->o_callback = sc;
1046 }
1047 return SLAP_CB_CONTINUE;
1048 }
1049
1050 /*
1051 ** init_module is last so the symbols resolve "for free" --
1052 ** it expects to be called automagically during dynamic module initialization
1053 */
1054
1055 int refint_initialize() {
1056 int rc;
1057
1058 mr_dnSubtreeMatch = mr_find( "dnSubtreeMatch" );
1059 if ( mr_dnSubtreeMatch == NULL ) {
1060 Debug( LDAP_DEBUG_ANY, "refint_initialize: "
1061 "unable to find MatchingRule 'dnSubtreeMatch'.\n" );
1062 return 1;
1063 }
1064
1065 /* statically declared just after the #includes at top */
1066 refint.on_bi.bi_type = "refint";
1067 refint.on_bi.bi_db_init = refint_db_init;
1068 refint.on_bi.bi_db_destroy = refint_db_destroy;
1069 refint.on_bi.bi_db_open = refint_open;
1070 refint.on_bi.bi_db_close = refint_close;
1071 refint.on_bi.bi_op_delete = refint_preop;
1072 refint.on_bi.bi_op_modrdn = refint_preop;
1073
1074 refint.on_bi.bi_cf_ocs = refintocs;
1075 rc = config_register_schema ( refintcfg, refintocs );
1076 if ( rc ) return rc;
1077
1078 return(overlay_register(&refint));
1079 }
1080
1081 #if SLAPD_OVER_REFINT == SLAPD_MOD_DYNAMIC && defined(PIC)
1082 int init_module(int argc, char *argv[]) {
1083 return refint_initialize();
1084 }
1085 #endif
1086
1087 #endif /* SLAPD_OVER_REFINT */