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