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