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