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