]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/refint.c
ITS#5153 from HEAD - sorted values
[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-2007 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 void *
496 refint_qtask( void *ctx, void *arg )
497 {
498         struct re_s *rtask = arg;
499         refint_data *id = rtask->arg;
500         Connection conn = {0};
501         OperationBuffer opbuf;
502         Operation *op;
503         SlapReply rs = {REP_RESULT};
504         slap_callback cb = { NULL, NULL, NULL, NULL };
505         Filter ftop, *fptr;
506         refint_q *rq;
507         dependent_data *dp, *dp_next;
508         refint_attrs *ra, *ip;
509         int rc;
510
511         connection_fake_init( &conn, &opbuf, ctx );
512         op = &opbuf.ob_op;
513
514         /*
515         ** build a search filter for all configured attributes;
516         ** populate our Operation;
517         ** pass our data (attr list, dn) to backend via sc_private;
518         ** call the backend search function;
519         ** nb: (|(one=thing)) is valid, but do smart formatting anyway;
520         ** nb: 16 is arbitrarily a dozen or so extra bytes;
521         **
522         */
523
524         ftop.f_choice = LDAP_FILTER_OR;
525         ftop.f_next = NULL;
526         ftop.f_or = NULL;
527         op->ors_filter = &ftop;
528         for(ip = id->attrs; ip; ip = ip->next) {
529                 fptr = op->o_tmpcalloc( sizeof(Filter) + sizeof(MatchingRuleAssertion),
530                         1, op->o_tmpmemctx );
531                 /* Use (attr:dnSubtreeMatch:=value) to catch subtree rename
532                  * and subtree delete where supported */
533                 fptr->f_choice = LDAP_FILTER_EXT;
534                 fptr->f_mra = (MatchingRuleAssertion *)(fptr+1);
535                 fptr->f_mr_rule = mr_dnSubtreeMatch;
536                 fptr->f_mr_rule_text = mr_dnSubtreeMatch->smr_str;
537                 fptr->f_mr_desc = ip->attr;
538                 fptr->f_mr_dnattrs = 0;
539                 fptr->f_next = ftop.f_or;
540                 ftop.f_or = fptr;
541         }
542
543         for (;;) {
544                 /* Dequeue an op */
545                 ldap_pvt_thread_mutex_lock( &id->qmutex );
546                 rq = id->qhead;
547                 if ( rq ) {
548                         id->qhead = rq->next;
549                         if ( !id->qhead )
550                                 id->qtail = NULL;
551                 }
552                 ldap_pvt_thread_mutex_unlock( &id->qmutex );
553                 if ( !rq )
554                         break;
555
556                 for (fptr = ftop.f_or; fptr; fptr=fptr->f_next )
557                         fptr->f_mr_value = rq->oldndn;
558
559                 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
560
561                 /* callback gets the searched dn instead */
562                 cb.sc_private   = rq;
563                 cb.sc_response  = refint_search_cb;
564                 op->o_callback  = &cb;
565                 op->o_tag       = LDAP_REQ_SEARCH;
566                 op->ors_scope   = LDAP_SCOPE_SUBTREE;
567                 op->ors_deref   = LDAP_DEREF_NEVER;
568                 op->ors_limit   = NULL;
569                 op->ors_slimit  = SLAP_NO_LIMIT;
570                 op->ors_tlimit  = SLAP_NO_LIMIT;
571
572                 /* no attrs! */
573                 op->ors_attrs = slap_anlist_no_attrs;
574
575                 op->o_req_ndn = id->dn;
576                 op->o_req_dn = id->dn;
577                 op->o_bd = rq->db;
578                 op->o_dn = op->o_bd->be_rootdn;
579                 op->o_ndn = op->o_bd->be_rootndn;
580                 slap_op_time( &op->o_time, &op->o_tincr );
581
582                 /* search */
583                 rc = op->o_bd->be_search(op, &rs);
584
585                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
586
587                 if(rc != LDAP_SUCCESS) {
588                         Debug( LDAP_DEBUG_TRACE,
589                                 "refint_response: search failed: %d\n",
590                                 rc, 0, 0 );
591                         continue;
592                 }
593
594                 /* safety? paranoid just in case */
595                 if(!cb.sc_private) {
596                         Debug( LDAP_DEBUG_TRACE,
597                                 "refint_response: callback wiped out sc_private?!\n",
598                                 0, 0, 0 );
599                         continue;
600                 }
601
602                 /* Set up the Modify requests */
603                 cb.sc_response  = &slap_null_cb;
604                 op->o_tag       = LDAP_REQ_MODIFY;
605
606                 /*
607                 ** [our search callback builds a list of attrs]
608                 ** foreach attr:
609                 **      make sure its dn has a backend;
610                 **      build Modification* chain;
611                 **      call the backend modify function;
612                 **
613                 */
614
615                 for(dp = rq->attrs; dp; dp = dp_next) {
616                         Modifications *m, *first = NULL;
617
618                         dp_next = dp->next;
619
620                         op->orm_modlist = NULL;
621
622                         op->o_req_dn    = dp->dn;
623                         op->o_req_ndn   = dp->ndn;
624                         op->o_bd = select_backend(&dp->ndn, 1);
625                         if(!op->o_bd) {
626                                 Debug( LDAP_DEBUG_TRACE,
627                                         "refint_response: no backend for DN %s!\n",
628                                         dp->dn.bv_val, 0, 0 );
629                                 goto done;
630                         }
631                         rs.sr_type      = REP_RESULT;
632                         for (ra = dp->attrs; ra; ra = dp->attrs) {
633                                 size_t  len;
634
635                                 dp->attrs = ra->next;
636                                 /* Set our ModifiersName */
637                                 if ( SLAP_LASTMOD( op->o_bd )) {
638                                         m = op->o_tmpalloc( sizeof(Modifications) +
639                                                 4*sizeof(BerValue), op->o_tmpmemctx );
640                                         m->sml_next = op->orm_modlist;
641                                         if ( !first )
642                                                 first = m;
643                                         op->orm_modlist = m;
644                                         m->sml_op = LDAP_MOD_REPLACE;
645                                         m->sml_flags = SLAP_MOD_INTERNAL;
646                                         m->sml_desc = slap_schema.si_ad_modifiersName;
647                                         m->sml_type = m->sml_desc->ad_cname;
648                                         m->sml_numvals = 1;
649                                         m->sml_values = (BerVarray)(m+1);
650                                         m->sml_nvalues = m->sml_values+2;
651                                         BER_BVZERO( &m->sml_values[1] );
652                                         BER_BVZERO( &m->sml_nvalues[1] );
653                                         m->sml_values[0] = refint_dn;
654                                         m->sml_nvalues[0] = refint_ndn;
655                                 }
656                                 if ( !BER_BVISEMPTY( &rq->newdn ) || ( ra->next &&
657                                         ra->attr == ra->next->attr ))
658                                 {
659                                         len = sizeof(Modifications);
660
661                                         if ( ra->new_vals == NULL ) {
662                                                 len += 4*sizeof(BerValue);
663                                         }
664
665                                         m = op->o_tmpalloc( len, op->o_tmpmemctx );
666                                         m->sml_next = op->orm_modlist;
667                                         if ( !first )
668                                                 first = m;
669                                         op->orm_modlist = m;
670                                         m->sml_op = LDAP_MOD_ADD;
671                                         m->sml_flags = 0;
672                                         m->sml_desc = ra->attr;
673                                         m->sml_type = ra->attr->ad_cname;
674                                         if ( ra->new_vals == NULL ) {
675                                                 m->sml_values = (BerVarray)(m+1);
676                                                 m->sml_nvalues = m->sml_values+2;
677                                                 BER_BVZERO( &m->sml_values[1] );
678                                                 BER_BVZERO( &m->sml_nvalues[1] );
679                                                 m->sml_numvals = 1;
680                                                 if ( BER_BVISEMPTY( &rq->newdn )) {
681                                                         op->o_tmpfree( ra, op->o_tmpmemctx );
682                                                         ra = dp->attrs;
683                                                         dp->attrs = ra->next;
684                                                         m->sml_values[0] = id->nothing;
685                                                         m->sml_nvalues[0] = id->nnothing;
686                                                 } else {
687                                                         m->sml_values[0] = rq->newdn;
688                                                         m->sml_nvalues[0] = rq->newndn;
689                                                 }
690                                         } else {
691                                                 m->sml_values = ra->new_vals;
692                                                 m->sml_nvalues = ra->new_nvals;
693                                                 m->sml_numvals = ra->ra_numvals;
694                                         }
695                                 }
696
697                                 len = sizeof(Modifications);
698                                 if ( ra->old_vals == NULL ) {
699                                         len += 4*sizeof(BerValue);
700                                 }
701
702                                 m = op->o_tmpalloc( len, op->o_tmpmemctx );
703                                 m->sml_next = op->orm_modlist;
704                                 op->orm_modlist = m;
705                                 if ( !first )
706                                         first = m;
707                                 m->sml_op = LDAP_MOD_DELETE;
708                                 m->sml_flags = 0;
709                                 m->sml_desc = ra->attr;
710                                 m->sml_type = ra->attr->ad_cname;
711                                 if ( ra->old_vals == NULL ) {
712                                         m->sml_numvals = 1;
713                                         m->sml_values = (BerVarray)(m+1);
714                                         m->sml_nvalues = m->sml_values+2;
715                                         m->sml_values[0] = rq->olddn;
716                                         m->sml_nvalues[0] = rq->oldndn;
717                                         BER_BVZERO( &m->sml_values[1] );
718                                         BER_BVZERO( &m->sml_nvalues[1] );
719                                 } else {
720                                         m->sml_values = ra->old_vals;
721                                         m->sml_nvalues = ra->old_nvals;
722                                         m->sml_numvals = ra->ra_numvals;
723                                 }
724                                 op->o_tmpfree( ra, op->o_tmpmemctx );
725                         }
726
727                         op->o_dn = op->o_bd->be_rootdn;
728                         op->o_ndn = op->o_bd->be_rootndn;
729                         slap_op_time( &op->o_time, &op->o_tincr );
730                         if((rc = op->o_bd->be_modify(op, &rs)) != LDAP_SUCCESS) {
731                                 Debug( LDAP_DEBUG_TRACE,
732                                         "refint_response: dependent modify failed: %d\n",
733                                         rs.sr_err, 0, 0 );
734                         }
735
736                         while (( m = op->orm_modlist )) {
737                                 op->orm_modlist = m->sml_next;
738                                 if ( m->sml_values && m->sml_values != (BerVarray)(m+1) ) {
739                                         ber_bvarray_free_x( m->sml_values, op->o_tmpmemctx );
740                                         ber_bvarray_free_x( m->sml_nvalues, op->o_tmpmemctx );
741                                 }
742                                 op->o_tmpfree( m, op->o_tmpmemctx );
743                                 if ( m == first ) break;
744                         }
745                         slap_mods_free( op->orm_modlist, 1 );
746                         op->o_tmpfree( dp->ndn.bv_val, op->o_tmpmemctx );
747                         op->o_tmpfree( dp->dn.bv_val, op->o_tmpmemctx );
748                         op->o_tmpfree( dp, op->o_tmpmemctx );
749                 }
750 done:
751                 if ( !BER_BVISNULL( &rq->newndn )) {
752                         ch_free( rq->newndn.bv_val );
753                         ch_free( rq->newdn.bv_val );
754                 }
755                 ch_free( rq->oldndn.bv_val );
756                 ch_free( rq->olddn.bv_val );
757                 ch_free( rq );
758         }
759
760         /* free filter */
761         for ( fptr = ftop.f_or; fptr; ) {
762                 Filter *f_next = fptr->f_next;
763                 op->o_tmpfree( fptr, op->o_tmpmemctx );
764                 fptr = f_next;
765         }
766
767         /* wait until we get explicitly scheduled again */
768         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
769         ldap_pvt_runqueue_stoptask( &slapd_rq, id->qtask );
770         ldap_pvt_runqueue_resched( &slapd_rq,id->qtask, 1 );
771         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
772
773         return NULL;
774 }
775
776 /*
777 ** refint_response
778 ** search for matching records and modify them
779 */
780
781 static int
782 refint_response(
783         Operation *op,
784         SlapReply *rs
785 )
786 {
787         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
788         refint_data *id = on->on_bi.bi_private;
789         BerValue pdn;
790         int ac;
791         refint_q *rq;
792         BackendDB *db;
793         refint_attrs *ip;
794
795         id->message = "_refint_response";
796
797         /* If the main op failed or is not a Delete or ModRdn, ignore it */
798         if (( op->o_tag != LDAP_REQ_DELETE && op->o_tag != LDAP_REQ_MODRDN ) ||
799                 rs->sr_err != LDAP_SUCCESS )
800                 return SLAP_CB_CONTINUE;
801
802         /*
803         ** validate (and count) the list of attrs;
804         **
805         */
806
807         for(ip = id->attrs, ac = 0; ip; ip = ip->next, ac++);
808         if(!ac) {
809                 Debug( LDAP_DEBUG_TRACE,
810                         "refint_response called without any attributes\n", 0, 0, 0 );
811                 return SLAP_CB_CONTINUE;
812         }
813
814         /*
815         ** find the backend that matches our configured basedn;
816         ** make sure it exists and has search and modify methods;
817         **
818         */
819
820         db = select_backend(&id->dn, 1);
821
822         if(db) {
823                 if (!db->be_search || !db->be_modify) {
824                         Debug( LDAP_DEBUG_TRACE,
825                                 "refint_response: backend missing search and/or modify\n",
826                                 0, 0, 0 );
827                         return SLAP_CB_CONTINUE;
828                 }
829         } else {
830                 Debug( LDAP_DEBUG_TRACE,
831                         "refint_response: no backend for our baseDN %s??\n",
832                         id->dn.bv_val, 0, 0 );
833                 return SLAP_CB_CONTINUE;
834         }
835
836         rq = ch_calloc( 1, sizeof( refint_q ));
837         ber_dupbv( &rq->olddn, &op->o_req_dn );
838         ber_dupbv( &rq->oldndn, &op->o_req_ndn );
839         rq->db = db;
840         rq->rdata = id;
841
842         if(op->o_tag == LDAP_REQ_MODRDN) {
843                 if ( op->oq_modrdn.rs_newSup ) {
844                         pdn = *op->oq_modrdn.rs_newSup;
845                 } else {
846                         dnParent( &op->o_req_dn, &pdn );
847                 }
848                 build_new_dn( &rq->newdn, &pdn, &op->orr_newrdn, NULL );
849                 if ( op->oq_modrdn.rs_nnewSup ) {
850                         pdn = *op->oq_modrdn.rs_nnewSup;
851                 } else {
852                         dnParent( &op->o_req_ndn, &pdn );
853                 }
854                 build_new_dn( &rq->newndn, &pdn, &op->orr_nnewrdn, NULL );
855         }
856
857         ldap_pvt_thread_mutex_lock( &id->qmutex );
858         if ( id->qtail ) {
859                 id->qtail->next = rq;
860         } else {
861                 id->qhead = rq;
862         }
863         id->qtail = rq;
864         ldap_pvt_thread_mutex_unlock( &id->qmutex );
865
866         ac = 0;
867         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
868         if ( !id->qtask ) {
869                 id->qtask = ldap_pvt_runqueue_insert( &slapd_rq, RUNQ_INTERVAL,
870                         refint_qtask, id, "refint_qtask",
871                         op->o_bd->be_suffix[0].bv_val );
872                 ac = 1;
873         } else {
874                 if ( !ldap_pvt_runqueue_isrunning( &slapd_rq, id->qtask ) &&
875                         !id->qtask->next_sched.tv_sec ) {
876                         id->qtask->interval.tv_sec = 0;
877                         ldap_pvt_runqueue_resched( &slapd_rq, id->qtask, 0 );
878                         id->qtask->interval.tv_sec = RUNQ_INTERVAL;
879                         ac = 1;
880                 }
881         }
882         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
883         if ( ac )
884                 slap_wake_listener();
885
886         return SLAP_CB_CONTINUE;
887 }
888
889 /*
890 ** init_module is last so the symbols resolve "for free" --
891 ** it expects to be called automagically during dynamic module initialization
892 */
893
894 int refint_initialize() {
895         int rc;
896
897         mr_dnSubtreeMatch = mr_find( "dnSubtreeMatch" );
898         if ( mr_dnSubtreeMatch == NULL ) {
899                 Debug( LDAP_DEBUG_ANY, "refint_initialize: "
900                         "unable to find MatchingRule 'dnSubtreeMatch'.\n",
901                         0, 0, 0 );
902                 return 1;
903         }
904
905         /* statically declared just after the #includes at top */
906         refint.on_bi.bi_type = "refint";
907         refint.on_bi.bi_db_init = refint_db_init;
908         refint.on_bi.bi_db_destroy = refint_db_destroy;
909         refint.on_bi.bi_db_open = refint_open;
910         refint.on_bi.bi_db_close = refint_close;
911         refint.on_response = refint_response;
912
913         refint.on_bi.bi_cf_ocs = refintocs;
914         rc = config_register_schema ( refintcfg, refintocs );
915         if ( rc ) return rc;
916
917         return(overlay_register(&refint));
918 }
919
920 #if SLAPD_OVER_REFINT == SLAPD_MOD_DYNAMIC && defined(PIC)
921 int init_module(int argc, char *argv[]) {
922         return refint_initialize();
923 }
924 #endif
925
926 #endif /* SLAPD_OVER_REFINT */