1 /* dynlist.c - dynamic list overlay */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2004-2005 Pierangelo Masarati.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Pierangelo Masarati
18 * for SysNet s.n.c., for inclusion in OpenLDAP Software.
23 #ifdef SLAPD_OVER_DYNLIST
27 #include <ac/string.h>
32 /* FIXME: the code differs if SLAP_OPATTRS is defined or not;
33 * SLAP_OPATTRS is not defined in 2.2 yet, while this overlay
34 * expects HEAD code at least later than August 6, 2004. */
36 typedef struct dynlist_info {
38 AttributeDescription *dli_ad;
39 AttributeDescription *dli_member_ad;
40 struct berval dli_default_filter;
44 dynlist_is_dynlist( Operation *op, SlapReply *rs )
46 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
47 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
51 a = attrs_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
53 /* FIXME: objectClass must be present; for non-storage
54 * backends, like back-ldap, it needs to be added
55 * to the requested attributes */
59 if ( value_find_ex( slap_schema.si_ad_objectClass,
60 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
61 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
62 a->a_nvals, &dli->dli_oc->soc_cname,
63 op->o_tmpmemctx ) == 0 )
72 dynlist_make_filter( Operation *op, struct berval *oldf, struct berval *newf )
74 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
75 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
81 assert( !BER_BVISNULL( oldf ) );
82 assert( !BER_BVISEMPTY( oldf ) );
84 newf->bv_len = STRLENOF( "(&(!(objectClass=" "))" ")" )
85 + dli->dli_oc->soc_cname.bv_len + oldf->bv_len;
86 newf->bv_val = op->o_tmpalloc( newf->bv_len + 1, op->o_tmpmemctx );
87 if ( newf->bv_val == NULL ) {
90 ptr = lutil_strcopy( newf->bv_val, "(&(!(objectClass=" );
91 ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
92 ptr = lutil_strcopy( ptr, "))" );
93 ptr = lutil_strcopy( ptr, oldf->bv_val );
94 ptr = lutil_strcopy( ptr, ")" );
95 newf->bv_len = ptr - newf->bv_val;
100 typedef struct dynlist_sc_t {
101 dynlist_info *dlc_dli;
106 dynlist_sc_update( Operation *op, SlapReply *rs )
112 AccessControlState acl_state = ACL_STATE_INIT;
116 if ( rs->sr_type != REP_SEARCH ) {
120 dlc = (dynlist_sc_t *)op->o_callback->sc_private;
124 assert( rs->sr_entry != NULL );
126 /* test access to entry */
127 if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
128 NULL, ACL_READ, NULL ) )
133 if ( dlc->dlc_dli->dli_member_ad ) {
135 /* if access allowed, try to add values, emulating permissive
136 * control to silently ignore duplicates */
137 if ( access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
138 NULL, ACL_READ, NULL ) )
141 const char *text = NULL;
143 struct berval vals[ 2 ], nvals[ 2 ];
145 vals[ 0 ] = rs->sr_entry->e_name;
146 BER_BVZERO( &vals[ 1 ] );
147 nvals[ 0 ] = rs->sr_entry->e_nname;
148 BER_BVZERO( &nvals[ 1 ] );
150 mod.sm_op = LDAP_MOD_ADD;
151 mod.sm_desc = dlc->dlc_dli->dli_member_ad;
152 mod.sm_type = dlc->dlc_dli->dli_member_ad->ad_cname;
153 mod.sm_values = vals;
154 mod.sm_nvalues = nvals;
156 (void)modify_add_values( e, &mod, /* permissive */ 1,
157 &text, textbuf, sizeof( textbuf ) );
164 opattrs = ( rs->sr_attrs == NULL ) ? 0 : an_find( rs->sr_attrs, &AllOper );
165 userattrs = ( rs->sr_attrs == NULL ) ? 1 : an_find( rs->sr_attrs, &AllUser );
166 #else /* SLAP_OPATTRS */
167 opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
168 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
169 #endif /* SLAP_OPATTRS */
171 for ( a = rs->sr_entry->e_attrs; a != NULL; a = a->a_next ) {
172 BerVarray vals, nvals = NULL;
175 /* if attribute is not requested, skip it */
176 if ( rs->sr_attrs == NULL ) {
177 if ( is_at_operational( a->a_desc->ad_type ) ) {
182 if ( is_at_operational( a->a_desc->ad_type ) ) {
183 if ( !opattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
189 if ( !userattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
196 /* test access to attribute */
197 if ( op->ors_attrsonly ) {
198 if ( !access_allowed( op, rs->sr_entry, a->a_desc, NULL,
199 ACL_READ, &acl_state ) )
205 /* single-value check: keep first only */
206 if ( is_at_single_value( a->a_desc->ad_type ) ) {
207 if ( attr_find( e->e_attrs, a->a_desc ) != NULL ) {
212 /* test access to attribute */
213 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
216 vals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
217 if ( a->a_nvals != a->a_vals ) {
218 nvals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
221 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
222 if ( access_allowed( op, rs->sr_entry, a->a_desc,
223 &a->a_nvals[i], ACL_READ, &acl_state ) )
225 vals[j] = a->a_vals[i];
227 nvals[j] = a->a_nvals[i];
233 /* if access allowed, try to add values, emulating permissive
234 * control to silently ignore duplicates */
237 const char *text = NULL;
240 BER_BVZERO( &vals[j] );
242 BER_BVZERO( &nvals[j] );
245 mod.sm_op = LDAP_MOD_ADD;
246 mod.sm_desc = a->a_desc;
247 mod.sm_type = a->a_desc->ad_cname;
248 mod.sm_values = vals;
249 mod.sm_nvalues = nvals;
251 (void)modify_add_values( e, &mod, /* permissive */ 1,
252 &text, textbuf, sizeof( textbuf ) );
255 op->o_tmpfree( vals, op->o_tmpmemctx );
257 op->o_tmpfree( nvals, op->o_tmpmemctx );
262 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
263 entry_free( rs->sr_entry );
270 dynlist_send_entry( Operation *op, SlapReply *rs )
272 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
273 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
284 dynlist_sc_t dlc = { 0 };
286 a = attrs_find( rs->sr_entry->e_attrs, dli->dli_ad );
289 return SLAP_CB_CONTINUE;
292 e = entry_dup( rs->sr_entry );
293 e_flags = rs->sr_flags | ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
297 cb.sc_private = &dlc;
298 cb.sc_response = dynlist_sc_update;
299 cb.sc_cleanup = NULL;
304 o.ors_tlimit = SLAP_NO_LIMIT;
305 o.ors_slimit = SLAP_NO_LIMIT;
308 opattrs = ( rs->sr_attrs == NULL ) ? 0 : an_find( rs->sr_attrs, &AllOper );
309 userattrs = ( rs->sr_attrs == NULL ) ? 1 : an_find( rs->sr_attrs, &AllUser );
310 #else /* SLAP_OPATTRS */
311 opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
312 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
313 #endif /* SLAP_OPATTRS */
315 for ( url = a->a_nvals; !BER_BVISNULL( url ); url++ ) {
316 LDAPURLDesc *lud = NULL;
321 BER_BVZERO( &o.o_req_dn );
322 BER_BVZERO( &o.o_req_ndn );
325 BER_BVZERO( &o.ors_filterstr );
327 if ( ldap_url_parse( url->bv_val, &lud ) != LDAP_URL_SUCCESS ) {
332 if ( lud->lud_host ) {
333 /* FIXME: host not allowed; reject as illegal? */
334 Debug( LDAP_DEBUG_ANY, "dynlist_send_entry(\"%s\"): "
335 "illegal URI \"%s\"\n",
336 e->e_name.bv_val, url->bv_val, 0 );
340 if ( lud->lud_dn == NULL ) {
341 /* note that an empty base is not honored in terms
342 * of defaultSearchBase, because select_backend()
343 * is not aware of the defaultSearchBase option;
344 * this can be useful in case of a database serving
345 * the empty suffix */
346 BER_BVSTR( &dn, "" );
348 ber_str2bv( lud->lud_dn, 0, 0, &dn );
350 rc = dnPrettyNormal( NULL, &dn, &o.o_req_dn, &o.o_req_ndn, op->o_tmpmemctx );
351 if ( rc != LDAP_SUCCESS ) {
355 o.ors_scope = lud->lud_scope;
357 if ( dli->dli_member_ad != NULL ) {
358 o.ors_attrs = slap_anlist_no_attrs;
360 } else if ( lud->lud_attrs == NULL ) {
361 o.ors_attrs = rs->sr_attrs;
364 for ( i = 0; lud->lud_attrs[i]; i++)
367 o.ors_attrs = op->o_tmpcalloc( i + 1, sizeof( AttributeName ), op->o_tmpmemctx );
368 for ( i = 0, j = 0; lud->lud_attrs[i]; i++) {
369 const char *text = NULL;
371 ber_str2bv( lud->lud_attrs[i], 0, 0, &o.ors_attrs[j].an_name );
372 o.ors_attrs[j].an_desc = NULL;
373 (void)slap_bv2ad( &o.ors_attrs[j].an_name, &o.ors_attrs[j].an_desc, &text );
374 /* FIXME: ignore errors... */
376 if ( rs->sr_attrs == NULL ) {
377 if ( o.ors_attrs[j].an_desc != NULL &&
378 is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
384 if ( o.ors_attrs[j].an_desc != NULL &&
385 is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
387 if ( !opattrs && !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
394 o.ors_attrs[j].an_desc != NULL &&
395 !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
409 BER_BVZERO( &o.ors_attrs[j].an_name );
412 if ( lud->lud_filter == NULL ) {
413 ber_dupbv_x( &o.ors_filterstr,
414 &dli->dli_default_filter, op->o_tmpmemctx );
417 ber_str2bv( lud->lud_filter, 0, 0, &flt );
418 if ( dynlist_make_filter( op, &flt, &o.ors_filterstr ) ) {
423 o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
424 if ( o.ors_filter == NULL ) {
428 o.o_bd = select_backend( &o.o_req_ndn, 0, 1 );
429 if ( o.o_bd && o.o_bd->be_search ) {
431 r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
432 #endif /* SLAP_OPATTRS */
433 (void)o.o_bd->be_search( &o, &r );
437 if ( o.ors_filter ) {
438 filter_free_x( &o, o.ors_filter );
440 if ( o.ors_attrs && o.ors_attrs != rs->sr_attrs
441 && o.ors_attrs != slap_anlist_no_attrs )
443 op->o_tmpfree( o.ors_attrs, op->o_tmpmemctx );
445 if ( !BER_BVISNULL( &o.o_req_dn ) ) {
446 op->o_tmpfree( o.o_req_dn.bv_val, op->o_tmpmemctx );
448 if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
449 op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
451 if ( o.ors_filterstr.bv_val != lud->lud_filter ) {
452 op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
453 lud->lud_filter = NULL;
456 ldap_free_urldesc( lud );
461 rs->sr_flags = e_flags;
463 return SLAP_CB_CONTINUE;
467 dynlist_sc_save_entry( Operation *op, SlapReply *rs )
469 /* save the entry in the private field of the callback,
470 * so it doesn't get freed (it's temporary!) */
471 if ( rs->sr_entry != NULL ) {
472 dynlist_sc_t *dlc = (dynlist_sc_t *)op->o_callback->sc_private;
473 dlc->dlc_e = rs->sr_entry;
481 dynlist_compare( Operation *op, SlapReply *rs )
483 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
484 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
489 SlapReply r = { REP_SEARCH };
492 dynlist_sc_t dlc = { 0 };
495 cb.sc_private = &dlc;
496 cb.sc_response = dynlist_sc_save_entry;
497 cb.sc_cleanup = NULL;
501 o.o_tag = LDAP_REQ_SEARCH;
503 o.ors_tlimit = SLAP_NO_LIMIT;
504 o.ors_slimit = SLAP_NO_LIMIT;
506 o.o_bd = select_backend( &o.o_req_ndn, 0, 1 );
507 if ( !o.o_bd || !o.o_bd->be_search ) {
508 return SLAP_CB_CONTINUE;
511 BER_BVSTR( &o.ors_filterstr, "(objectClass=*)" );
512 o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
513 if ( o.ors_filter == NULL ) {
515 return SLAP_CB_CONTINUE;
518 o.ors_scope = LDAP_SCOPE_BASE;
519 an[0].an_name = op->orc_ava->aa_desc->ad_cname;
520 an[0].an_desc = op->orc_ava->aa_desc;
521 BER_BVZERO( &an[1].an_name );
525 rc = o.o_bd->be_search( &o, &r );
526 filter_free_x( &o, o.ors_filter );
532 if ( dlc.dlc_e != NULL ) {
533 r.sr_entry = dlc.dlc_e;
536 if ( r.sr_err != LDAP_SUCCESS || r.sr_entry == NULL ) {
538 return SLAP_CB_CONTINUE;
541 /* if we're here, we got a match... */
542 rs->sr_err = LDAP_COMPARE_FALSE;
543 for ( a = attrs_find( r.sr_entry->e_attrs, op->orc_ava->aa_desc );
545 a = attrs_find( a->a_next, op->orc_ava->aa_desc ) )
547 if ( value_find_ex( op->orc_ava->aa_desc,
548 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
549 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
550 a->a_nvals, &op->orc_ava->aa_value, op->o_tmpmemctx ) == 0 )
552 rs->sr_err = LDAP_COMPARE_TRUE;
557 if ( r.sr_flags & REP_ENTRY_MUSTBEFREED ) {
558 entry_free( r.sr_entry );
561 return SLAP_CB_CONTINUE;
565 dynlist_response( Operation *op, SlapReply *rs )
567 switch ( op->o_tag ) {
568 case LDAP_REQ_SEARCH:
569 if ( rs->sr_type == REP_SEARCH && !get_manageDSAit( op ) )
571 if ( dynlist_is_dynlist( op, rs ) ) {
572 return dynlist_send_entry( op, rs );
577 case LDAP_REQ_COMPARE:
578 if ( rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) {
579 return dynlist_compare( op, rs );
587 return SLAP_CB_CONTINUE;
599 slap_overinst *on = (slap_overinst *)be->bd_info;
600 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
604 if ( strcasecmp( argv[0], "dynlist-oc" ) == 0 ) {
606 fprintf( stderr, "dynlist-oc <oc>\n" );
609 dli->dli_oc = oc_find( argv[1] );
610 if ( dli->dli_oc == NULL ) {
611 fprintf( stderr, "dynlist-oc <oc>: "
612 "unable to find ObjectClass "
613 "\"%s\"\n", argv[1] );
617 } else if ( strcasecmp( argv[0], "dynlist-ad" ) == 0 ) {
621 fprintf( stderr, "dynlist-ad <ad>\n" );
625 rc = slap_str2ad( argv[1], &dli->dli_ad, &text );
626 if ( rc != LDAP_SUCCESS ) {
627 fprintf( stderr, "dynlist-ad <ad>: "
628 "unable to find AttributeDescription "
629 "\"%s\"\n", argv[1] );
633 } else if ( strcasecmp( argv[0], "dynlist-member-ad" ) == 0 ) {
637 fprintf( stderr, "dynlist-member-ad <ad>\n" );
640 dli->dli_member_ad = NULL;
641 rc = slap_str2ad( argv[1], &dli->dli_member_ad, &text );
642 if ( rc != LDAP_SUCCESS ) {
643 fprintf( stderr, "dynlist-member-ad <ad>: "
644 "unable to find AttributeDescription "
645 "\"%s\"\n", argv[1] );
650 rc = SLAP_CONF_UNKNOWN;
661 slap_overinst *on = (slap_overinst *) be->bd_info;
664 dli = (dynlist_info *)ch_malloc( sizeof( dynlist_info ) );
665 memset( dli, 0, sizeof( dynlist_info ) );
667 on->on_bi.bi_private = (void *)dli;
677 slap_overinst *on = (slap_overinst *) be->bd_info;
678 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
683 if ( dli->dli_oc == NULL ) {
684 fprintf( stderr, "dynlist_db_open(): missing \"dynlist-oc <ObjectClass>\"\n" );
688 if ( dli->dli_ad == NULL ) {
689 fprintf( stderr, "dynlist_db_open(): missing \"dynlist-ad <AttributeDescription>\"\n" );
693 len = STRLENOF( "(!(objectClass=" "))" )
694 + dli->dli_oc->soc_cname.bv_len;
695 dli->dli_default_filter.bv_val = SLAP_MALLOC( len + 1 );
696 if ( dli->dli_default_filter.bv_val == NULL ) {
697 fprintf( stderr, "dynlist_db_open(): malloc failed\n" );
700 ptr = lutil_strcopy( dli->dli_default_filter.bv_val, "(!(objectClass=" );
701 ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
702 ptr = lutil_strcopy( ptr, "))" );
703 dli->dli_default_filter.bv_len = ptr - dli->dli_default_filter.bv_val;
713 slap_overinst *on = (slap_overinst *) be->bd_info;
716 if ( on->on_bi.bi_private ) {
717 dynlist_info *dli = (dynlist_info *)on->on_bi.bi_private;
728 static slap_overinst dynlist = { { NULL } };
733 dynlist.on_bi.bi_type = "dynlist";
734 dynlist.on_bi.bi_db_init = dynlist_db_init;
735 dynlist.on_bi.bi_db_config = dynlist_db_config;
736 dynlist.on_bi.bi_db_open = dynlist_db_open;
737 dynlist.on_bi.bi_db_destroy = dynlist_db_destroy;
739 dynlist.on_response = dynlist_response;
741 return overlay_register( &dynlist );
744 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
746 init_module( int argc, char *argv[] )
748 return dynlist_init();
752 #endif /* SLAPD_OVER_DYNLIST */