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