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;
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 );
480 rc = op->o_bd->be_search(op, &rs);
482 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
484 if(rc != LDAP_SUCCESS) {
485 Debug( LDAP_DEBUG_TRACE,
486 "refint_response: search failed: %d\n",
491 /* safety? paranoid just in case */
493 Debug( LDAP_DEBUG_TRACE,
494 "refint_response: callback wiped out sc_private?!\n",
499 /* Set up the Modify requests */
500 cb.sc_response = &slap_null_cb;
501 op->o_tag = LDAP_REQ_MODIFY;
504 ** [our search callback builds a list of attrs]
506 ** make sure its dn has a backend;
507 ** build Modification* chain;
508 ** call the backend modify function;
512 for(dp = rq->attrs; dp; dp = dp->next) {
513 Modifications *m, *first = NULL;
515 op->orm_modlist = NULL;
517 op->o_req_dn = dp->dn;
518 op->o_req_ndn = dp->ndn;
519 op->o_bd = select_backend(&dp->ndn, 0, 1);
521 Debug( LDAP_DEBUG_TRACE,
522 "refint_response: no backend for DN %s!\n",
523 dp->dn.bv_val, 0, 0 );
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;
537 m->sml_op = LDAP_MOD_ADD;
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 );
548 dp->attrs = ra->next;
549 m->sml_values[0] = id->nothing;
550 m->sml_nvalues[0] = id->nnothing;
552 m->sml_values[0] = rq->newdn;
553 m->sml_nvalues[0] = rq->newndn;
556 m = op->o_tmpalloc( sizeof(Modifications) + 4*sizeof(BerValue),
558 m->sml_next = op->orm_modlist;
562 m->sml_op = LDAP_MOD_DELETE;
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 );
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",
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;
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 );
595 if ( !BER_BVISNULL( &rq->newndn )) {
596 ch_free( rq->newndn.bv_val );
597 ch_free( rq->newdn.bv_val );
599 ch_free( rq->oldndn.bv_val );
600 ch_free( rq->olddn.bv_val );
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 );
615 ** search for matching records and modify them
624 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
625 refint_data *id = on->on_bi.bi_private;
632 id->message = "_refint_response";
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;
640 ** validate (and count) the list of attrs;
644 for(ip = id->attrs, ac = 0; ip; ip = ip->next, ac++);
646 Debug( LDAP_DEBUG_TRACE,
647 "refint_response called without any attributes\n", 0, 0, 0 );
648 return SLAP_CB_CONTINUE;
652 ** find the backend that matches our configured basedn;
653 ** make sure it exists and has search and modify methods;
657 db = select_backend(&id->dn, 0, 1);
660 if (!db->be_search || !db->be_modify) {
661 Debug( LDAP_DEBUG_TRACE,
662 "refint_response: backend missing search and/or modify\n",
664 return SLAP_CB_CONTINUE;
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;
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 );
679 if(op->o_tag == LDAP_REQ_MODRDN) {
680 if ( op->oq_modrdn.rs_newSup ) {
681 pdn = *op->oq_modrdn.rs_newSup;
683 dnParent( &op->o_req_dn, &pdn );
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;
689 dnParent( &op->o_req_ndn, &pdn );
691 build_new_dn( &rq->newndn, &pdn, &op->orr_nnewrdn, NULL );
694 ldap_pvt_thread_mutex_lock( &id->qmutex );
696 id->qtail->next = rq;
701 ldap_pvt_thread_mutex_unlock( &id->qmutex );
704 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
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 );
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;
719 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
721 slap_wake_listener();
723 return SLAP_CB_CONTINUE;
727 ** init_module is last so the symbols resolve "for free" --
728 ** it expects to be called automagically during dynamic module initialization
731 int refint_initialize() {
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;
742 refint.on_bi.bi_cf_ocs = refintocs;
743 rc = config_register_schema ( refintcfg, refintocs );
746 return(overlay_register(&refint));
749 #if SLAPD_OVER_REFINT == SLAPD_MOD_DYNAMIC && defined(PIC)
750 int init_module(int argc, char *argv[]) {
751 return refint_initialize();
755 #endif /* SLAPD_OVER_REFINT */