1 /* refint.c - referential integrity module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2004 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Symas Corp. for inclusion in
19 * OpenLDAP Software. This work was sponsored by Hewlett-Packard.
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.
29 * Updates are performed using the database rootdn in a separate task
30 * to allow the original operation to complete immediately.
33 #ifdef SLAPD_OVER_REFINT
37 #include <ac/string.h>
38 #include <ac/socket.h>
44 static slap_overinst refint;
46 typedef struct refint_attrs_s {
47 struct refint_attrs_s *next;
48 AttributeDescription *attr;
51 typedef struct dependents_s {
52 struct dependents_s *next;
53 BerValue dn; /* target dn */
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 */
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 */
78 ldap_pvt_thread_mutex_t qmutex;
81 #define RUNQ_INTERVAL 36000 /* a long time */
88 static ConfigDriver refint_cf_gen;
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 }
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 },
115 refint_cf_gen(ConfigArgs *c)
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;
122 int rc = ARG_BAD_CONF;
126 case SLAP_CONFIG_EMIT:
131 value_add_one( &c->rvalue_vals,
132 &ip->attr->ad_cname );
138 if ( !BER_BVISEMPTY( &dd->nothing )) {
139 rc = value_add_one( &c->rvalue_vals,
142 rc = value_add_one( &c->rvalue_nvals,
152 case LDAP_MOD_DELETE:
165 /* delete from linked list */
166 for ( i=0; i < c->valx; ++i ) {
167 pipp = &(*pipp)->next;
170 *pipp = (*pipp)->next;
172 /* AttributeDescriptions are global so
173 * shouldn't be freed here... */
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;
191 case SLAP_CONFIG_ADD:
192 /* fallthrough to LDAP_MOD_ADD */
197 for ( i=1; i < c->argc; ++i ) {
199 if ( slap_str2ad ( c->argv[i], &ad, &text )
202 sizeof ( refint_attrs ) );
204 ip->next = dd->attrs;
207 Debug ( LDAP_DEBUG_CONFIG,
208 "refint add: <%s>: %s\n",
209 c->argv[i], text, NULL );
212 SLAP_TEXT_BUFLEN-1 );
213 c->msg[SLAP_TEXT_BUFLEN-1] = '\0';
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;
239 ** allocate new refint_data;
240 ** store in on_bi.bi_private;
249 slap_overinst *on = (slap_overinst *)be->bd_info;
250 refint_data *id = ch_calloc(1,sizeof(refint_data));
252 id->message = "_init";
253 on->on_bi.bi_private = id;
254 ldap_pvt_thread_mutex_init( &id->qmutex );
263 slap_overinst *on = (slap_overinst *)be->bd_info;
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 );
275 ** initialize, copy basedn if not already set
284 slap_overinst *on = (slap_overinst *)be->bd_info;
285 refint_data *id = on->on_bi.bi_private;
286 id->message = "_open";
288 if ( BER_BVISNULL( &id->dn )) {
289 if ( BER_BVISNULL( &be->be_nsuffix[0] ))
291 ber_dupbv( &id->dn, &be->be_nsuffix[0] );
298 ** foreach configured attribute:
301 ** (do not) free id->message;
302 ** reset on_bi.bi_private;
303 ** free our config data;
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";
317 for(ii = id->attrs; ii; ii = ij) {
322 ch_free(id->dn.bv_val);
323 ch_free(id->nothing.bv_val);
324 ch_free(id->nnothing.bv_val);
326 memset( id, 0, sizeof(*id));
333 ** generates a list of Attributes from search results
344 refint_q *rq = op->o_callback->sc_private;
345 refint_data *dd = rq->rdata;
346 refint_attrs *ia, *da = dd->attrs, *na;
350 Debug(LDAP_DEBUG_TRACE, "refint_search_cb <%s>\n",
351 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "NOTHING", 0, 0);
353 if (rs->sr_type != REP_SEARCH || !rs->sr_entry) return(0);
356 ** foreach configured attribute type:
357 ** if this attr exists in the search result,
358 ** and it has a value matching the target:
360 ** if this is a delete and there's only one value:
361 ** allocate the same attr again;
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;
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;
379 /* If this is a delete and there's only one value, and
380 * we have a nothing DN configured, allocate the attr again.
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;
389 Debug(LDAP_DEBUG_TRACE, "refint_search_cb: %s: %s\n",
390 a->a_desc->ad_cname.bv_val, rq->olddn.bv_val, 0);
398 refint_qtask( void *ctx, void *arg )
400 struct re_s *rtask = arg;
401 refint_data *id = rtask->arg;
402 Connection conn = {0};
403 OperationBuffer opbuf;
405 SlapReply rs = {REP_RESULT};
406 slap_callback cb = { NULL, NULL, NULL, NULL };
410 refint_attrs *ra, *ip;
413 op = (Operation *) &opbuf;
414 connection_fake_init( &conn, op, ctx );
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;
426 ftop.f_choice = LDAP_FILTER_OR;
429 op->ors_filter = &ftop;
430 for(ip = id->attrs; ip; ip = ip->next) {
431 fptr = op->o_tmpalloc( sizeof(Filter) + sizeof(AttributeAssertion),
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;
442 ldap_pvt_thread_mutex_lock( &id->qmutex );
445 id->qhead = rq->next;
449 ldap_pvt_thread_mutex_unlock( &id->qmutex );
453 for (fptr = ftop.f_or; fptr; fptr=fptr->f_next )
454 fptr->f_av_value = rq->oldndn;
456 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
458 /* callback gets the searched dn instead */
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;
470 op->ors_attrs = slap_anlist_no_attrs;
472 op->o_req_ndn = id->dn;
473 op->o_req_dn = id->dn;
477 rc = op->o_bd->be_search(op, &rs);
479 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
481 if(rc != LDAP_SUCCESS) {
482 Debug( LDAP_DEBUG_TRACE,
483 "refint_response: search failed: %d\n",
488 /* safety? paranoid just in case */
490 Debug( LDAP_DEBUG_TRACE,
491 "refint_response: callback wiped out sc_private?!\n",
496 /* Set up the Modify requests */
497 cb.sc_response = &slap_null_cb;
498 op->o_tag = LDAP_REQ_MODIFY;
501 ** [our search callback builds a list of attrs]
503 ** make sure its dn has a backend;
504 ** build Modification* chain;
505 ** call the backend modify function;
509 for(dp = rq->attrs; dp; dp = dp->next) {
510 Modifications *m, *first = NULL;
512 op->orm_modlist = NULL;
514 op->o_req_dn = dp->dn;
515 op->o_req_ndn = dp->ndn;
516 op->o_bd = select_backend(&dp->ndn, 0, 1);
518 Debug( LDAP_DEBUG_TRACE,
519 "refint_response: no backend for DN %s!\n",
520 dp->dn.bv_val, 0, 0 );
523 rs.sr_type = REP_RESULT;
524 for (ra = dp->attrs; ra; ra = dp->attrs) {
525 dp->attrs = ra->next;
526 if ( !BER_BVISEMPTY( &rq->newdn ) || ( ra->next &&
527 ra->attr == ra->next->attr )) {
528 m = op->o_tmpalloc( sizeof(Modifications) +
529 4*sizeof(BerValue), op->o_tmpmemctx );
530 m->sml_next = op->orm_modlist;
534 m->sml_op = LDAP_MOD_ADD;
536 m->sml_desc = ra->attr;
537 m->sml_type = ra->attr->ad_cname;
538 m->sml_values = (BerVarray)(m+1);
539 m->sml_nvalues = m->sml_values+2;
540 BER_BVZERO( &m->sml_values[1] );
541 BER_BVZERO( &m->sml_nvalues[1] );
542 if ( BER_BVISEMPTY( &rq->newdn )) {
543 op->o_tmpfree( ra, op->o_tmpmemctx );
545 dp->attrs = ra->next;
546 m->sml_values[0] = id->nothing;
547 m->sml_nvalues[0] = id->nnothing;
549 m->sml_values[0] = rq->newdn;
550 m->sml_nvalues[0] = rq->newndn;
553 m = op->o_tmpalloc( sizeof(Modifications) + 4*sizeof(BerValue),
555 m->sml_next = op->orm_modlist;
559 m->sml_op = LDAP_MOD_DELETE;
561 m->sml_desc = ra->attr;
562 m->sml_type = ra->attr->ad_cname;
563 m->sml_values = (BerVarray)(m+1);
564 m->sml_nvalues = m->sml_values+2;
565 m->sml_values[0] = rq->olddn;
566 m->sml_nvalues[0] = rq->oldndn;
567 BER_BVZERO( &m->sml_values[1] );
568 BER_BVZERO( &m->sml_nvalues[1] );
569 op->o_tmpfree( ra, op->o_tmpmemctx );
572 op->o_dn = op->o_bd->be_rootdn;
573 op->o_ndn = op->o_bd->be_rootndn;
574 if((rc = op->o_bd->be_modify(op, &rs)) != LDAP_SUCCESS) {
575 Debug( LDAP_DEBUG_TRACE,
576 "refint_response: dependent modify failed: %d\n",
580 while (( m = op->orm_modlist )) {
581 op->orm_modlist = m->sml_next;
582 op->o_tmpfree( m, op->o_tmpmemctx );
583 if ( m == first ) break;
585 slap_mods_free( op->orm_modlist, 1 );
586 op->o_tmpfree( dp->ndn.bv_val, op->o_tmpmemctx );
587 op->o_tmpfree( dp->dn.bv_val, op->o_tmpmemctx );
588 op->o_tmpfree( dp, op->o_tmpmemctx );
591 if ( !BER_BVISNULL( &rq->newndn )) {
592 ch_free( rq->newndn.bv_val );
593 ch_free( rq->newdn.bv_val );
595 ch_free( rq->oldndn.bv_val );
596 ch_free( rq->olddn.bv_val );
600 /* wait until we get explicitly scheduled again */
601 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
602 ldap_pvt_runqueue_stoptask( &slapd_rq, id->qtask );
603 ldap_pvt_runqueue_resched( &slapd_rq,id->qtask, 1 );
604 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
611 ** search for matching records and modify them
620 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
621 refint_data *id = on->on_bi.bi_private;
628 id->message = "_refint_response";
630 /* If the main op failed or is not a Delete or ModRdn, ignore it */
631 if (( op->o_tag != LDAP_REQ_DELETE && op->o_tag != LDAP_REQ_MODRDN ) ||
632 rs->sr_err != LDAP_SUCCESS )
633 return SLAP_CB_CONTINUE;
636 ** validate (and count) the list of attrs;
640 for(ip = id->attrs, ac = 0; ip; ip = ip->next, ac++);
642 Debug( LDAP_DEBUG_TRACE,
643 "refint_response called without any attributes\n", 0, 0, 0 );
644 return SLAP_CB_CONTINUE;
648 ** find the backend that matches our configured basedn;
649 ** make sure it exists and has search and modify methods;
653 db = select_backend(&id->dn, 0, 1);
656 if (!db->be_search || !db->be_modify) {
657 Debug( LDAP_DEBUG_TRACE,
658 "refint_response: backend missing search and/or modify\n",
660 return SLAP_CB_CONTINUE;
663 Debug( LDAP_DEBUG_TRACE,
664 "refint_response: no backend for our baseDN %s??\n",
665 id->dn.bv_val, 0, 0 );
666 return SLAP_CB_CONTINUE;
669 rq = ch_calloc( 1, sizeof( refint_q ));
670 ber_dupbv( &rq->olddn, &op->o_req_dn );
671 ber_dupbv( &rq->oldndn, &op->o_req_ndn );
675 if(op->o_tag == LDAP_REQ_MODRDN) {
676 if ( op->oq_modrdn.rs_newSup ) {
677 pdn = *op->oq_modrdn.rs_newSup;
679 dnParent( &op->o_req_dn, &pdn );
681 build_new_dn( &rq->newdn, &pdn, &op->orr_newrdn, NULL );
682 if ( op->oq_modrdn.rs_nnewSup ) {
683 pdn = *op->oq_modrdn.rs_nnewSup;
685 dnParent( &op->o_req_ndn, &pdn );
687 build_new_dn( &rq->newndn, &pdn, &op->orr_nnewrdn, NULL );
690 ldap_pvt_thread_mutex_lock( &id->qmutex );
692 id->qtail->next = rq;
697 ldap_pvt_thread_mutex_unlock( &id->qmutex );
700 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
702 id->qtask = ldap_pvt_runqueue_insert( &slapd_rq, RUNQ_INTERVAL,
703 refint_qtask, id, "refint_qtask",
704 op->o_bd->be_suffix[0].bv_val );
707 if ( !ldap_pvt_runqueue_isrunning( &slapd_rq, id->qtask ) &&
708 !id->qtask->next_sched.tv_sec ) {
709 id->qtask->interval.tv_sec = 0;
710 ldap_pvt_runqueue_resched( &slapd_rq, id->qtask, 0 );
711 id->qtask->interval.tv_sec = RUNQ_INTERVAL;
715 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
717 slap_wake_listener();
719 return SLAP_CB_CONTINUE;
723 ** init_module is last so the symbols resolve "for free" --
724 ** it expects to be called automagically during dynamic module initialization
727 int refint_initialize() {
730 /* statically declared just after the #includes at top */
731 refint.on_bi.bi_type = "refint";
732 refint.on_bi.bi_db_init = refint_db_init;
733 refint.on_bi.bi_db_destroy = refint_db_destroy;
734 refint.on_bi.bi_db_open = refint_open;
735 refint.on_bi.bi_db_close = refint_close;
736 refint.on_response = refint_response;
738 refint.on_bi.bi_cf_ocs = refintocs;
739 rc = config_register_schema ( refintcfg, refintocs );
742 return(overlay_register(&refint));
745 #if SLAPD_OVER_REFINT == SLAPD_MOD_DYNAMIC && defined(PIC)
746 int init_module(int argc, char *argv[]) {
747 return refint_initialize();
751 #endif /* SLAPD_OVER_REFINT */