1 /* dynlist.c - dynamic list overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2013 The OpenLDAP Foundation.
6 * Portions Copyright 2004-2005 Pierangelo Masarati.
7 * Portions Copyright 2008 Emmanuel Dreyfus.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Pierangelo Masarati
20 * for SysNet s.n.c., for inclusion in OpenLDAP Software.
25 #ifdef SLAPD_OVER_DYNLIST
27 #if SLAPD_OVER_DYNGROUP != SLAPD_MOD_STATIC
28 #define TAKEOVER_DYNGROUP
33 #include <ac/string.h>
39 static AttributeDescription *ad_dgIdentity, *ad_dgAuthz;
41 typedef struct dynlist_map_t {
42 AttributeDescription *dlm_member_ad;
43 AttributeDescription *dlm_mapped_ad;
44 struct dynlist_map_t *dlm_next;
47 typedef struct dynlist_info_t {
49 AttributeDescription *dli_ad;
50 struct dynlist_map_t *dli_dlm;
51 struct berval dli_uri;
53 struct berval dli_uri_nbase;
54 Filter *dli_uri_filter;
55 struct berval dli_default_filter;
56 struct dynlist_info_t *dli_next;
59 #define DYNLIST_USAGE \
60 "\"dynlist-attrset <oc> [uri] <URL-ad> [[<mapped-ad>:]<member-ad> ...]\": "
62 static dynlist_info_t *
63 dynlist_is_dynlist_next( Operation *op, SlapReply *rs, dynlist_info_t *old_dli )
65 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
70 if ( old_dli == NULL ) {
71 dli = (dynlist_info_t *)on->on_bi.bi_private;
74 dli = old_dli->dli_next;
77 a = attrs_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
79 /* FIXME: objectClass must be present; for non-storage
80 * backends, like back-ldap, it needs to be added
81 * to the requested attributes */
85 for ( ; dli; dli = dli->dli_next ) {
86 if ( dli->dli_lud != NULL ) {
87 /* check base and scope */
88 if ( !BER_BVISNULL( &dli->dli_uri_nbase )
89 && !dnIsSuffixScope( &rs->sr_entry->e_nname,
91 dli->dli_lud->lud_scope ) )
97 if ( dli->dli_uri_filter && test_filter( op, rs->sr_entry, dli->dli_uri_filter ) != LDAP_COMPARE_TRUE ) {
102 if ( attr_valfind( a,
103 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
104 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
105 &dli->dli_oc->soc_cname, NULL,
106 op->o_tmpmemctx ) == 0 )
116 dynlist_make_filter( Operation *op, Entry *e, const char *url, struct berval *oldf, struct berval *newf )
118 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
119 dynlist_info_t *dli = (dynlist_info_t *)on->on_bi.bi_private;
122 int needBrackets = 0;
124 assert( oldf != NULL );
125 assert( newf != NULL );
126 assert( !BER_BVISNULL( oldf ) );
127 assert( !BER_BVISEMPTY( oldf ) );
129 if ( oldf->bv_val[0] != '(' ) {
130 Debug( LDAP_DEBUG_ANY, "%s: dynlist, DN=\"%s\": missing brackets in URI=\"%s\" filter\n",
131 op->o_log_prefix, e->e_name.bv_val, url );
135 newf->bv_len = STRLENOF( "(&(!(objectClass=" "))" ")" )
136 + dli->dli_oc->soc_cname.bv_len + oldf->bv_len + needBrackets;
137 newf->bv_val = op->o_tmpalloc( newf->bv_len + 1, op->o_tmpmemctx );
138 if ( newf->bv_val == NULL ) {
141 ptr = lutil_strcopy( newf->bv_val, "(&(!(objectClass=" );
142 ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
143 ptr = lutil_strcopy( ptr, "))" );
144 if ( needBrackets ) *ptr++ = '(';
145 ptr = lutil_strcopy( ptr, oldf->bv_val );
146 if ( needBrackets ) *ptr++ = ')';
147 ptr = lutil_strcopy( ptr, ")" );
148 newf->bv_len = ptr - newf->bv_val;
153 /* dynlist_sc_update() callback info set by dynlist_prepare_entry() */
154 typedef struct dynlist_sc_t {
155 dynlist_info_t *dlc_dli;
160 dynlist_sc_update( Operation *op, SlapReply *rs )
166 AccessControlState acl_state = ACL_STATE_INIT;
171 if ( rs->sr_type != REP_SEARCH ) {
175 dlc = (dynlist_sc_t *)op->o_callback->sc_private;
179 assert( rs->sr_entry != NULL );
181 /* test access to entry */
182 if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
183 NULL, ACL_READ, NULL ) )
188 /* if there is only one member_ad, and it's not mapped,
189 * consider it as old-style member listing */
190 dlm = dlc->dlc_dli->dli_dlm;
191 if ( dlm && dlm->dlm_mapped_ad == NULL && dlm->dlm_next == NULL ) {
192 /* if access allowed, try to add values, emulating permissive
193 * control to silently ignore duplicates */
194 if ( access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
195 NULL, ACL_READ, NULL ) )
198 const char *text = NULL;
200 struct berval vals[ 2 ], nvals[ 2 ];
202 vals[ 0 ] = rs->sr_entry->e_name;
203 BER_BVZERO( &vals[ 1 ] );
204 nvals[ 0 ] = rs->sr_entry->e_nname;
205 BER_BVZERO( &nvals[ 1 ] );
207 mod.sm_op = LDAP_MOD_ADD;
208 mod.sm_desc = dlm->dlm_member_ad;
209 mod.sm_type = dlm->dlm_member_ad->ad_cname;
210 mod.sm_values = vals;
211 mod.sm_nvalues = nvals;
214 (void)modify_add_values( e, &mod, /* permissive */ 1,
215 &text, textbuf, sizeof( textbuf ) );
221 opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
222 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
224 for ( a = rs->sr_entry->e_attrs; a != NULL; a = a->a_next ) {
225 BerVarray vals, nvals = NULL;
227 is_oc = a->a_desc == slap_schema.si_ad_objectClass;
229 /* if attribute is not requested, skip it */
230 if ( rs->sr_attrs == NULL ) {
231 if ( is_at_operational( a->a_desc->ad_type ) ) {
236 if ( is_at_operational( a->a_desc->ad_type ) ) {
237 if ( !opattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
243 if ( !userattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
250 /* test access to attribute */
251 if ( op->ors_attrsonly ) {
252 if ( !access_allowed( op, rs->sr_entry, a->a_desc, NULL,
253 ACL_READ, &acl_state ) )
259 /* single-value check: keep first only */
260 if ( is_at_single_value( a->a_desc->ad_type ) ) {
261 if ( attr_find( e->e_attrs, a->a_desc ) != NULL ) {
266 /* test access to attribute */
269 vals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
270 if ( a->a_nvals != a->a_vals ) {
271 nvals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
274 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
276 ObjectClass *soc = oc_bvfind( &a->a_vals[i] );
278 if ( soc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
283 if ( access_allowed( op, rs->sr_entry, a->a_desc,
284 &a->a_nvals[i], ACL_READ, &acl_state ) )
286 vals[j] = a->a_vals[i];
288 nvals[j] = a->a_nvals[i];
294 /* if access allowed, try to add values, emulating permissive
295 * control to silently ignore duplicates */
298 const char *text = NULL;
301 AttributeDescription *ad;
303 BER_BVZERO( &vals[j] );
305 BER_BVZERO( &nvals[j] );
309 for ( dlm = dlc->dlc_dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
310 if ( dlm->dlm_member_ad == a->a_desc ) {
311 if ( dlm->dlm_mapped_ad ) {
312 ad = dlm->dlm_mapped_ad;
318 mod.sm_op = LDAP_MOD_ADD;
320 mod.sm_type = ad->ad_cname;
321 mod.sm_values = vals;
322 mod.sm_nvalues = nvals;
325 (void)modify_add_values( e, &mod, /* permissive */ 1,
326 &text, textbuf, sizeof( textbuf ) );
329 op->o_tmpfree( vals, op->o_tmpmemctx );
331 op->o_tmpfree( nvals, op->o_tmpmemctx );
336 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
337 entry_free( rs->sr_entry );
339 rs->sr_flags &= ~REP_ENTRY_MASK;
346 dynlist_prepare_entry( Operation *op, SlapReply *rs, dynlist_info_t *dli )
348 Attribute *a, *id = NULL;
355 dynlist_sc_t dlc = { 0 };
358 a = attrs_find( rs->sr_entry->e_attrs, dli->dli_ad );
361 return SLAP_CB_CONTINUE;
364 opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
365 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
367 /* Don't generate member list if it wasn't requested */
368 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
369 AttributeDescription *ad = dlm->dlm_mapped_ad ? dlm->dlm_mapped_ad : dlm->dlm_member_ad;
370 if ( userattrs || ad_inlist( ad, rs->sr_attrs ) )
373 if ( dli->dli_dlm && !dlm )
374 return SLAP_CB_CONTINUE;
376 if ( ad_dgIdentity && ( id = attrs_find( rs->sr_entry->e_attrs, ad_dgIdentity ))) {
377 Attribute *authz = NULL;
379 /* if not rootdn and dgAuthz is present,
380 * check if user can be authorized as dgIdentity */
381 if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
382 && ( authz = attrs_find( rs->sr_entry->e_attrs, ad_dgAuthz ) ) )
384 if ( slap_sasl_matches( op, authz->a_nvals,
385 &o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
387 return SLAP_CB_CONTINUE;
391 o.o_dn = id->a_vals[0];
392 o.o_ndn = id->a_nvals[0];
397 /* ensure e is modifiable, but do not replace
398 * sr_entry yet since we have pointers into it */
399 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
400 e = entry_dup( rs->sr_entry );
405 cb.sc_private = &dlc;
406 cb.sc_response = dynlist_sc_update;
407 cb.sc_cleanup = NULL;
411 o.ors_deref = LDAP_DEREF_NEVER;
413 o.ors_tlimit = SLAP_NO_LIMIT;
414 o.ors_slimit = SLAP_NO_LIMIT;
416 for ( url = a->a_nvals; !BER_BVISNULL( url ); url++ ) {
417 LDAPURLDesc *lud = NULL;
422 BER_BVZERO( &o.o_req_dn );
423 BER_BVZERO( &o.o_req_ndn );
426 BER_BVZERO( &o.ors_filterstr );
428 if ( ldap_url_parse( url->bv_val, &lud ) != LDAP_URL_SUCCESS ) {
433 if ( lud->lud_host != NULL ) {
434 /* FIXME: host not allowed; reject as illegal? */
435 Debug( LDAP_DEBUG_ANY, "dynlist_prepare_entry(\"%s\"): "
436 "illegal URI \"%s\"\n",
437 e->e_name.bv_val, url->bv_val, 0 );
441 if ( lud->lud_dn == NULL ) {
442 /* note that an empty base is not honored in terms
443 * of defaultSearchBase, because select_backend()
444 * is not aware of the defaultSearchBase option;
445 * this can be useful in case of a database serving
446 * the empty suffix */
447 BER_BVSTR( &dn, "" );
450 ber_str2bv( lud->lud_dn, 0, 0, &dn );
452 rc = dnPrettyNormal( NULL, &dn, &o.o_req_dn, &o.o_req_ndn, op->o_tmpmemctx );
453 if ( rc != LDAP_SUCCESS ) {
457 o.ors_scope = lud->lud_scope;
459 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
460 if ( dlm->dlm_mapped_ad != NULL ) {
465 if ( dli->dli_dlm && !dlm ) {
466 /* if ( lud->lud_attrs != NULL ),
467 * the URL should be ignored */
468 o.ors_attrs = slap_anlist_no_attrs;
470 } else if ( lud->lud_attrs == NULL ) {
471 o.ors_attrs = rs->sr_attrs;
474 for ( i = 0; lud->lud_attrs[i]; i++)
477 o.ors_attrs = op->o_tmpcalloc( i + 1, sizeof( AttributeName ), op->o_tmpmemctx );
478 for ( i = 0, j = 0; lud->lud_attrs[i]; i++) {
479 const char *text = NULL;
481 ber_str2bv( lud->lud_attrs[i], 0, 0, &o.ors_attrs[j].an_name );
482 o.ors_attrs[j].an_desc = NULL;
483 (void)slap_bv2ad( &o.ors_attrs[j].an_name, &o.ors_attrs[j].an_desc, &text );
484 /* FIXME: ignore errors... */
486 if ( rs->sr_attrs == NULL ) {
487 if ( o.ors_attrs[j].an_desc != NULL &&
488 is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
494 if ( o.ors_attrs[j].an_desc != NULL &&
495 is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
501 if ( !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) ) {
502 /* lookup if mapped -- linear search,
503 * not very efficient unless list
505 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
506 if ( dlm->dlm_member_ad == o.ors_attrs[j].an_desc ) {
518 o.ors_attrs[j].an_desc != NULL &&
519 !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
521 /* lookup if mapped -- linear search,
522 * not very efficient unless list
524 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
525 if ( dlm->dlm_member_ad == o.ors_attrs[j].an_desc ) {
544 BER_BVZERO( &o.ors_attrs[j].an_name );
547 if ( lud->lud_filter == NULL ) {
548 ber_dupbv_x( &o.ors_filterstr,
549 &dli->dli_default_filter, op->o_tmpmemctx );
553 ber_str2bv( lud->lud_filter, 0, 0, &flt );
554 if ( dynlist_make_filter( op, rs->sr_entry, url->bv_val, &flt, &o.ors_filterstr ) ) {
559 o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
560 if ( o.ors_filter == NULL ) {
564 o.o_bd = select_backend( &o.o_req_ndn, 1 );
565 if ( o.o_bd && o.o_bd->be_search ) {
566 SlapReply r = { REP_SEARCH };
567 r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
568 (void)o.o_bd->be_search( &o, &r );
573 slap_op_groups_free( &o );
575 if ( o.ors_filter ) {
576 filter_free_x( &o, o.ors_filter, 1 );
578 if ( o.ors_attrs && o.ors_attrs != rs->sr_attrs
579 && o.ors_attrs != slap_anlist_no_attrs )
581 op->o_tmpfree( o.ors_attrs, op->o_tmpmemctx );
583 if ( !BER_BVISNULL( &o.o_req_dn ) ) {
584 op->o_tmpfree( o.o_req_dn.bv_val, op->o_tmpmemctx );
586 if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
587 op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
589 assert( BER_BVISNULL( &o.ors_filterstr )
590 || o.ors_filterstr.bv_val != lud->lud_filter );
591 op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
592 ldap_free_urldesc( lud );
595 if ( e != rs->sr_entry ) {
596 rs_replace_entry( op, rs, (slap_overinst *)op->o_bd->bd_info, e );
597 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
600 return SLAP_CB_CONTINUE;
603 /* dynlist_sc_compare_entry() callback set by dynlist_compare() */
604 typedef struct dynlist_cc_t {
606 # define dc_ava dc_cb.sc_private /* attr:val to compare with */
611 dynlist_sc_compare_entry( Operation *op, SlapReply *rs )
613 if ( rs->sr_type == REP_SEARCH && rs->sr_entry != NULL ) {
614 dynlist_cc_t *dc = (dynlist_cc_t *)op->o_callback;
615 AttributeAssertion *ava = dc->dc_ava;
616 Attribute *a = attrs_find( rs->sr_entry->e_attrs, ava->aa_desc );
619 while ( LDAP_SUCCESS != attr_valfind( a,
620 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
621 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
622 &ava->aa_value, NULL, op->o_tmpmemctx )
623 && (a = attrs_find( a->a_next, ava->aa_desc )) != NULL )
625 *dc->dc_res = a ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
633 dynlist_compare( Operation *op, SlapReply *rs )
635 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
636 dynlist_info_t *dli = (dynlist_info_t *)on->on_bi.bi_private;
642 for ( ; dli != NULL; dli = dli->dli_next ) {
643 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next )
644 if ( op->oq_compare.rs_ava->aa_desc == dlm->dlm_member_ad )
648 /* This compare is for one of the attributes we're
649 * interested in. We'll use slapd's existing dyngroup
650 * evaluator to get the answer we want.
652 BerVarray id = NULL, authz = NULL;
654 o.o_do_not_cache = 1;
656 if ( ad_dgIdentity && backend_attribute( &o, NULL, &o.o_req_ndn,
657 ad_dgIdentity, &id, ACL_READ ) == LDAP_SUCCESS )
659 /* if not rootdn and dgAuthz is present,
660 * check if user can be authorized as dgIdentity */
661 if ( ad_dgAuthz && !BER_BVISEMPTY( id ) && !be_isroot( op )
662 && backend_attribute( &o, NULL, &o.o_req_ndn,
663 ad_dgAuthz, &authz, ACL_READ ) == LDAP_SUCCESS )
666 rs->sr_err = slap_sasl_matches( op, authz,
667 &o.o_ndn, &o.o_ndn );
668 ber_bvarray_free_x( authz, op->o_tmpmemctx );
669 if ( rs->sr_err != LDAP_SUCCESS ) {
676 o.o_groups = NULL; /* authz changed, invalidate cached groups */
679 rs->sr_err = backend_group( &o, NULL, &o.o_req_ndn,
680 &o.oq_compare.rs_ava->aa_value, dli->dli_oc, dli->dli_ad );
681 switch ( rs->sr_err ) {
683 rs->sr_err = LDAP_COMPARE_TRUE;
686 case LDAP_NO_SUCH_OBJECT:
687 /* NOTE: backend_group() returns noSuchObject
688 * if op_ndn does not exist; however, since
689 * dynamic list expansion means that the
690 * member attribute is virtually present, the
691 * non-existence of the asserted value implies
692 * the assertion is FALSE rather than
694 rs->sr_err = LDAP_COMPARE_FALSE;
699 if ( id ) ber_bvarray_free_x( id, o.o_tmpmemctx );
701 return SLAP_CB_CONTINUE;
705 be = select_backend( &o.o_req_ndn, 1 );
706 if ( !be || !be->be_search ) {
707 return SLAP_CB_CONTINUE;
710 if ( overlay_entry_get_ov( &o, &o.o_req_ndn, NULL, NULL, 0, &e, on ) !=
711 LDAP_SUCCESS || e == NULL )
713 return SLAP_CB_CONTINUE;
716 /* check for dynlist objectClass; done if not found */
717 dli = (dynlist_info_t *)on->on_bi.bi_private;
718 while ( dli != NULL && !is_entry_objectclass_or_sub( e, dli->dli_oc ) ) {
725 if ( ad_dgIdentity ) {
726 Attribute *id = attrs_find( e->e_attrs, ad_dgIdentity );
730 /* if not rootdn and dgAuthz is present,
731 * check if user can be authorized as dgIdentity */
732 if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
733 && ( authz = attrs_find( e->e_attrs, ad_dgAuthz ) ) )
735 if ( slap_sasl_matches( op, authz->a_nvals,
736 &o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
742 o.o_dn = id->a_vals[0];
743 o.o_ndn = id->a_nvals[0];
748 /* generate dynamic list with dynlist_response() and compare */
750 SlapReply r = { REP_SEARCH };
751 dynlist_cc_t dc = { { 0, dynlist_sc_compare_entry, 0, 0 }, 0 };
754 dc.dc_ava = op->orc_ava;
755 dc.dc_res = &rs->sr_err;
756 o.o_callback = (slap_callback *) &dc;
758 o.o_tag = LDAP_REQ_SEARCH;
760 o.ors_tlimit = SLAP_NO_LIMIT;
761 o.ors_slimit = SLAP_NO_LIMIT;
763 o.ors_filterstr = *slap_filterstr_objectClass_pres;
764 o.ors_filter = (Filter *) slap_filter_objectClass_pres;
766 o.ors_scope = LDAP_SCOPE_BASE;
767 o.ors_deref = LDAP_DEREF_NEVER;
768 an[0].an_name = op->orc_ava->aa_desc->ad_cname;
769 an[0].an_desc = op->orc_ava->aa_desc;
770 BER_BVZERO( &an[1].an_name );
774 o.o_acl_priv = ACL_COMPARE;
777 (void)be->be_search( &o, &r );
779 if ( o.o_dn.bv_val != op->o_dn.bv_val ) {
780 slap_op_groups_free( &o );
786 overlay_entry_release_ov( &o, e, 0, on );
789 return SLAP_CB_CONTINUE;
793 dynlist_response( Operation *op, SlapReply *rs )
795 switch ( op->o_tag ) {
796 case LDAP_REQ_SEARCH:
797 if ( rs->sr_type == REP_SEARCH && !get_manageDSAit( op ) )
799 int rc = SLAP_CB_CONTINUE;
800 dynlist_info_t *dli = NULL;
802 while ( (dli = dynlist_is_dynlist_next( op, rs, dli )) != NULL ) {
803 rc = dynlist_prepare_entry( op, rs, dli );
810 case LDAP_REQ_COMPARE:
811 switch ( rs->sr_err ) {
812 /* NOTE: we waste a few cycles running the dynamic list
813 * also when the result is FALSE, which occurs if the
814 * dynamic entry itself contains the AVA attribute */
815 /* FIXME: this approach is less than optimal; a dedicated
816 * compare op should be implemented, that fetches the
817 * entry, checks if it has the appropriate objectClass
818 * and, in case, runs a compare thru all the URIs,
819 * stopping at the first positive occurrence; see ITS#3756 */
820 case LDAP_COMPARE_FALSE:
821 case LDAP_NO_SUCH_ATTRIBUTE:
822 return dynlist_compare( op, rs );
827 return SLAP_CB_CONTINUE;
831 dynlist_build_def_filter( dynlist_info_t *dli )
835 dli->dli_default_filter.bv_len = STRLENOF( "(!(objectClass=" "))" )
836 + dli->dli_oc->soc_cname.bv_len;
837 dli->dli_default_filter.bv_val = ch_malloc( dli->dli_default_filter.bv_len + 1 );
838 if ( dli->dli_default_filter.bv_val == NULL ) {
839 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: malloc failed.\n",
844 ptr = lutil_strcopy( dli->dli_default_filter.bv_val, "(!(objectClass=" );
845 ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
846 ptr = lutil_strcopy( ptr, "))" );
848 assert( ptr == &dli->dli_default_filter.bv_val[dli->dli_default_filter.bv_len] );
860 static ConfigDriver dl_cfgen;
862 /* XXXmanu 255 is the maximum arguments we allow. Can we go beyond? */
863 static ConfigTable dlcfg[] = {
864 { "dynlist-attrset", "group-oc> [uri] <URL-ad> <[mapped:]member-ad> [...]",
865 3, 0, 0, ARG_MAGIC|DL_ATTRSET, dl_cfgen,
866 "( OLcfgOvAt:8.1 NAME 'olcDlAttrSet' "
867 "DESC 'Dynamic list: <group objectClass>, <URL attributeDescription>, <member attributeDescription>' "
868 "EQUALITY caseIgnoreMatch "
869 "SYNTAX OMsDirectoryString "
870 "X-ORDERED 'VALUES' )",
872 { "dynlist-attrpair", "member-ad> <URL-ad",
873 3, 3, 0, ARG_MAGIC|DL_ATTRPAIR, dl_cfgen,
875 #ifdef TAKEOVER_DYNGROUP
876 { "attrpair", "member-ad> <URL-ad",
877 3, 3, 0, ARG_MAGIC|DL_ATTRPAIR_COMPAT, dl_cfgen,
880 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
883 static ConfigOCs dlocs[] = {
885 "NAME 'olcDynamicList' "
886 "DESC 'Dynamic list configuration' "
887 "SUP olcOverlayConfig "
888 "MAY olcDLattrSet )",
889 Cft_Overlay, dlcfg, NULL, NULL },
894 dl_cfgen( ConfigArgs *c )
896 slap_overinst *on = (slap_overinst *)c->bi;
897 dynlist_info_t *dli = (dynlist_info_t *)on->on_bi.bi_private;
901 if ( c->op == SLAP_CONFIG_EMIT ) {
904 for ( i = 0; dli; i++, dli = dli->dli_next ) {
906 char *ptr = c->cr_msg;
909 assert( dli->dli_oc != NULL );
910 assert( dli->dli_ad != NULL );
912 /* FIXME: check buffer overflow! */
913 ptr += snprintf( c->cr_msg, sizeof( c->cr_msg ),
914 SLAP_X_ORDERED_FMT "%s", i,
915 dli->dli_oc->soc_cname.bv_val );
917 if ( !BER_BVISNULL( &dli->dli_uri ) ) {
920 ptr = lutil_strncopy( ptr, dli->dli_uri.bv_val,
921 dli->dli_uri.bv_len );
926 ptr = lutil_strncopy( ptr, dli->dli_ad->ad_cname.bv_val,
927 dli->dli_ad->ad_cname.bv_len );
929 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
932 if ( dlm->dlm_mapped_ad ) {
933 ptr = lutil_strcopy( ptr, dlm->dlm_mapped_ad->ad_cname.bv_val );
938 ptr = lutil_strcopy( ptr, dlm->dlm_member_ad->ad_cname.bv_val );
941 bv.bv_val = c->cr_msg;
942 bv.bv_len = ptr - bv.bv_val;
943 value_add_one( &c->rvalue_vals, &bv );
947 case DL_ATTRPAIR_COMPAT:
959 } else if ( c->op == LDAP_MOD_DELETE ) {
963 dynlist_info_t *dli_next;
965 for ( dli_next = dli; dli_next; dli = dli_next ) {
966 dynlist_map_t *dlm = dli->dli_dlm;
967 dynlist_map_t *dlm_next;
969 dli_next = dli->dli_next;
971 if ( !BER_BVISNULL( &dli->dli_uri ) ) {
972 ch_free( dli->dli_uri.bv_val );
975 if ( dli->dli_lud != NULL ) {
976 ldap_free_urldesc( dli->dli_lud );
979 if ( !BER_BVISNULL( &dli->dli_uri_nbase ) ) {
980 ber_memfree( dli->dli_uri_nbase.bv_val );
983 if ( dli->dli_uri_filter != NULL ) {
984 filter_free( dli->dli_uri_filter );
987 ch_free( dli->dli_default_filter.bv_val );
989 while ( dlm != NULL ) {
990 dlm_next = dlm->dlm_next;
997 on->on_bi.bi_private = NULL;
1000 dynlist_info_t **dlip;
1002 dynlist_map_t *dlm_next;
1004 for ( i = 0, dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1007 if ( *dlip == NULL ) {
1010 dlip = &(*dlip)->dli_next;
1014 *dlip = dli->dli_next;
1016 if ( !BER_BVISNULL( &dli->dli_uri ) ) {
1017 ch_free( dli->dli_uri.bv_val );
1020 if ( dli->dli_lud != NULL ) {
1021 ldap_free_urldesc( dli->dli_lud );
1024 if ( !BER_BVISNULL( &dli->dli_uri_nbase ) ) {
1025 ber_memfree( dli->dli_uri_nbase.bv_val );
1028 if ( dli->dli_uri_filter != NULL ) {
1029 filter_free( dli->dli_uri_filter );
1032 ch_free( dli->dli_default_filter.bv_val );
1035 while ( dlm != NULL ) {
1036 dlm_next = dlm->dlm_next;
1042 dli = (dynlist_info_t *)on->on_bi.bi_private;
1046 case DL_ATTRPAIR_COMPAT:
1061 dynlist_info_t **dlip,
1063 ObjectClass *oc = NULL;
1064 AttributeDescription *ad = NULL;
1066 LDAPURLDesc *lud = NULL;
1067 struct berval nbase = BER_BVNULL;
1068 Filter *filter = NULL;
1069 struct berval uri = BER_BVNULL;
1070 dynlist_map_t *dlm = NULL, *dlml = NULL;
1073 oc = oc_find( c->argv[ 1 ] );
1075 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1076 "unable to find ObjectClass \"%s\"",
1078 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1079 c->log, c->cr_msg, 0 );
1083 if ( strncasecmp( c->argv[ attridx ], "ldap://", STRLENOF("ldap://") ) == 0 ) {
1084 if ( ldap_url_parse( c->argv[ attridx ], &lud ) != LDAP_URL_SUCCESS ) {
1085 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1086 "unable to parse URI \"%s\"",
1087 c->argv[ attridx ] );
1092 if ( lud->lud_host != NULL ) {
1093 if ( lud->lud_host[0] == '\0' ) {
1094 ch_free( lud->lud_host );
1095 lud->lud_host = NULL;
1098 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1099 "host not allowed in URI \"%s\"",
1100 c->argv[ attridx ] );
1106 if ( lud->lud_attrs != NULL ) {
1107 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1108 "attrs not allowed in URI \"%s\"",
1109 c->argv[ attridx ] );
1114 if ( lud->lud_exts != NULL ) {
1115 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1116 "extensions not allowed in URI \"%s\"",
1117 c->argv[ attridx ] );
1122 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' ) {
1124 ber_str2bv( lud->lud_dn, 0, 0, &dn );
1125 rc = dnNormalize( 0, NULL, NULL, &dn, &nbase, NULL );
1126 if ( rc != LDAP_SUCCESS ) {
1127 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1128 "DN normalization failed in URI \"%s\"",
1129 c->argv[ attridx ] );
1134 if ( lud->lud_filter != NULL && lud->lud_filter[ 0 ] != '\0' ) {
1135 filter = str2filter( lud->lud_filter );
1136 if ( filter == NULL ) {
1137 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1138 "filter parsing failed in URI \"%s\"",
1139 c->argv[ attridx ] );
1145 ber_str2bv( c->argv[ attridx ], 0, 1, &uri );
1150 ldap_free_urldesc( lud );
1153 if ( !BER_BVISNULL( &nbase ) ) {
1154 ber_memfree( nbase.bv_val );
1157 if ( filter != NULL ) {
1158 filter_free( filter );
1161 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1162 c->log, c->cr_msg, 0 );
1170 rc = slap_str2ad( c->argv[ attridx ], &ad, &text );
1171 if ( rc != LDAP_SUCCESS ) {
1172 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1173 "unable to find AttributeDescription \"%s\"",
1174 c->argv[ attridx ] );
1175 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1176 c->log, c->cr_msg, 0 );
1180 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1181 snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1182 "AttributeDescription \"%s\" "
1183 "must be a subtype of \"labeledURI\"",
1184 c->argv[ attridx ] );
1185 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1186 c->log, c->cr_msg, 0 );
1192 for ( i = attridx; i < c->argc; i++ ) {
1195 AttributeDescription *member_ad = NULL;
1196 AttributeDescription *mapped_ad = NULL;
1197 dynlist_map_t *dlmp;
1201 * If no mapped attribute is given, dn is used
1202 * for backward compatibility.
1205 if ( ( cp = strchr( arg, ':' ) ) != NULL ) {
1207 ber_str2bv( arg, cp - arg, 0, &bv );
1208 rc = slap_bv2ad( &bv, &mapped_ad, &text );
1209 if ( rc != LDAP_SUCCESS ) {
1210 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1212 "unable to find mapped AttributeDescription #%d \"%s\"\n",
1213 i - 3, c->argv[ i ] );
1214 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1215 c->log, c->cr_msg, 0 );
1221 rc = slap_str2ad( arg, &member_ad, &text );
1222 if ( rc != LDAP_SUCCESS ) {
1223 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1225 "unable to find AttributeDescription #%d \"%s\"\n",
1226 i - 3, c->argv[ i ] );
1227 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1228 c->log, c->cr_msg, 0 );
1232 dlmp = (dynlist_map_t *)ch_calloc( 1, sizeof( dynlist_map_t ) );
1233 if ( dlm == NULL ) {
1236 dlmp->dlm_member_ad = member_ad;
1237 dlmp->dlm_mapped_ad = mapped_ad;
1238 dlmp->dlm_next = NULL;
1241 dlml->dlm_next = dlmp;
1245 if ( c->valx > 0 ) {
1248 for ( i = 0, dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1251 if ( *dlip == NULL ) {
1252 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1254 "invalid index {%d}\n",
1256 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1257 c->log, c->cr_msg, 0 );
1260 dlip = &(*dlip)->dli_next;
1265 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1266 *dlip; dlip = &(*dlip)->dli_next )
1270 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1272 (*dlip)->dli_oc = oc;
1273 (*dlip)->dli_ad = ad;
1274 (*dlip)->dli_dlm = dlm;
1275 (*dlip)->dli_next = dli_next;
1277 (*dlip)->dli_lud = lud;
1278 (*dlip)->dli_uri_nbase = nbase;
1279 (*dlip)->dli_uri_filter = filter;
1280 (*dlip)->dli_uri = uri;
1282 rc = dynlist_build_def_filter( *dlip );
1286 case DL_ATTRPAIR_COMPAT:
1287 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1288 "warning: \"attrpair\" only supported for limited "
1289 "backward compatibility with overlay \"dyngroup\"" );
1290 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
1294 dynlist_info_t **dlip;
1295 ObjectClass *oc = NULL;
1296 AttributeDescription *ad = NULL,
1300 oc = oc_find( "groupOfURLs" );
1302 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1303 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1304 "unable to find default ObjectClass \"groupOfURLs\"" );
1305 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1306 c->log, c->cr_msg, 0 );
1310 rc = slap_str2ad( c->argv[ 1 ], &member_ad, &text );
1311 if ( rc != LDAP_SUCCESS ) {
1312 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1313 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1314 "unable to find AttributeDescription \"%s\"",
1316 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1317 c->log, c->cr_msg, 0 );
1321 rc = slap_str2ad( c->argv[ 2 ], &ad, &text );
1322 if ( rc != LDAP_SUCCESS ) {
1323 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1324 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1325 "unable to find AttributeDescription \"%s\"\n",
1327 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1328 c->log, c->cr_msg, 0 );
1332 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1333 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1335 "AttributeDescription \"%s\" "
1336 "must be a subtype of \"labeledURI\"",
1338 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1339 c->log, c->cr_msg, 0 );
1343 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1344 *dlip; dlip = &(*dlip)->dli_next )
1347 * The same URL attribute / member attribute pair
1348 * cannot be repeated, but we enforce this only
1349 * when the member attribute is unique. Performing
1350 * the check for multiple values would require
1351 * sorting and comparing the lists, which is left
1352 * as a future improvement
1354 if ( (*dlip)->dli_ad == ad &&
1355 (*dlip)->dli_dlm->dlm_next == NULL &&
1356 member_ad == (*dlip)->dli_dlm->dlm_member_ad ) {
1357 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1358 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1359 "URL attributeDescription \"%s\" already mapped.\n",
1360 ad->ad_cname.bv_val );
1361 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1362 c->log, c->cr_msg, 0 );
1364 /* make it a warning... */
1370 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1372 (*dlip)->dli_oc = oc;
1373 (*dlip)->dli_ad = ad;
1374 (*dlip)->dli_dlm = (dynlist_map_t *)ch_calloc( 1, sizeof( dynlist_map_t ) );
1375 (*dlip)->dli_dlm->dlm_member_ad = member_ad;
1376 (*dlip)->dli_dlm->dlm_mapped_ad = NULL;
1378 rc = dynlist_build_def_filter( *dlip );
1395 slap_overinst *on = (slap_overinst *) be->bd_info;
1396 dynlist_info_t *dli = (dynlist_info_t *)on->on_bi.bi_private;
1397 ObjectClass *oc = NULL;
1398 AttributeDescription *ad = NULL;
1402 if ( dli == NULL ) {
1403 dli = ch_calloc( 1, sizeof( dynlist_info_t ) );
1404 on->on_bi.bi_private = (void *)dli;
1407 for ( ; dli; dli = dli->dli_next ) {
1408 if ( dli->dli_oc == NULL ) {
1410 oc = oc_find( "groupOfURLs" );
1412 snprintf( cr->msg, sizeof( cr->msg),
1413 "unable to fetch objectClass \"groupOfURLs\"" );
1414 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s.\n", cr->msg, 0, 0 );
1422 if ( dli->dli_ad == NULL ) {
1424 rc = slap_str2ad( "memberURL", &ad, &text );
1425 if ( rc != LDAP_SUCCESS ) {
1426 snprintf( cr->msg, sizeof( cr->msg),
1427 "unable to fetch attributeDescription \"memberURL\": %d (%s)",
1429 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s.\n", cr->msg, 0, 0 );
1437 if ( BER_BVISNULL( &dli->dli_default_filter ) ) {
1438 rc = dynlist_build_def_filter( dli );
1445 if ( ad_dgIdentity == NULL ) {
1446 rc = slap_str2ad( "dgIdentity", &ad_dgIdentity, &text );
1447 if ( rc != LDAP_SUCCESS ) {
1448 snprintf( cr->msg, sizeof( cr->msg),
1449 "unable to fetch attributeDescription \"dgIdentity\": %d (%s)",
1451 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s\n", cr->msg, 0, 0 );
1452 /* Just a warning */
1456 if ( ad_dgAuthz == NULL ) {
1457 rc = slap_str2ad( "dgAuthz", &ad_dgAuthz, &text );
1458 if ( rc != LDAP_SUCCESS ) {
1459 snprintf( cr->msg, sizeof( cr->msg),
1460 "unable to fetch attributeDescription \"dgAuthz\": %d (%s)",
1462 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s\n", cr->msg, 0, 0 );
1463 /* Just a warning */
1475 slap_overinst *on = (slap_overinst *) be->bd_info;
1477 if ( on->on_bi.bi_private ) {
1478 dynlist_info_t *dli = (dynlist_info_t *)on->on_bi.bi_private,
1481 for ( dli_next = dli; dli_next; dli = dli_next ) {
1483 dynlist_map_t *dlm_next;
1485 dli_next = dli->dli_next;
1487 if ( !BER_BVISNULL( &dli->dli_uri ) ) {
1488 ch_free( dli->dli_uri.bv_val );
1491 if ( dli->dli_lud != NULL ) {
1492 ldap_free_urldesc( dli->dli_lud );
1495 if ( !BER_BVISNULL( &dli->dli_uri_nbase ) ) {
1496 ber_memfree( dli->dli_uri_nbase.bv_val );
1499 if ( dli->dli_uri_filter != NULL ) {
1500 filter_free( dli->dli_uri_filter );
1503 ch_free( dli->dli_default_filter.bv_val );
1506 while ( dlm != NULL ) {
1507 dlm_next = dlm->dlm_next;
1518 static slap_overinst dynlist = { { NULL } };
1519 #ifdef TAKEOVER_DYNGROUP
1520 static char *obsolete_names[] = {
1526 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
1528 #endif /* SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC */
1530 dynlist_initialize(void)
1534 dynlist.on_bi.bi_type = "dynlist";
1536 #ifdef TAKEOVER_DYNGROUP
1537 /* makes dynlist incompatible with dyngroup */
1538 dynlist.on_bi.bi_obsolete_names = obsolete_names;
1541 dynlist.on_bi.bi_db_config = config_generic_wrapper;
1542 dynlist.on_bi.bi_db_open = dynlist_db_open;
1543 dynlist.on_bi.bi_db_destroy = dynlist_db_destroy;
1545 dynlist.on_response = dynlist_response;
1547 dynlist.on_bi.bi_cf_ocs = dlocs;
1549 rc = config_register_schema( dlcfg, dlocs );
1554 return overlay_register( &dynlist );
1557 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
1559 init_module( int argc, char *argv[] )
1561 return dynlist_initialize();
1565 #endif /* SLAPD_OVER_DYNLIST */