]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/dynlist.c
allow intermixing of mapped/not mapped attrs (spotted while invstigating ITS#5717)
[openldap] / servers / slapd / overlays / dynlist.c
1 /* dynlist.c - dynamic list overlay */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2008 The OpenLDAP Foundation.
6  * Portions Copyright 2004-2005 Pierangelo Masarati.
7  * Portions Copyright 2008 Emmanuel Dreyfus.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
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>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Pierangelo Masarati
20  * for SysNet s.n.c., for inclusion in OpenLDAP Software.
21  */
22
23 #include "portable.h"
24
25 #ifdef SLAPD_OVER_DYNLIST
26
27 #if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 3
28 #if SLAPD_OVER_DYNGROUP != SLAPD_MOD_STATIC
29 #define TAKEOVER_DYNGROUP
30 #endif
31 #else
32 #if LDAP_VENDOR_VERSION_MINOR < 3
33 #define OL_2_2_COMPAT
34 #endif
35 #endif
36
37 #include <stdio.h>
38
39 #include <ac/string.h>
40
41 #include "slap.h"
42 #ifndef OL_2_2_COMPAT
43 #include "config.h"
44 #endif
45 #include "lutil.h"
46
47 /* FIXME: the code differs if SLAP_OPATTRS is defined or not;
48  * SLAP_OPATTRS is not defined in 2.2 yet, while this overlay
49  * expects HEAD code at least later than August 6, 2004. */
50 /* FIXME: slap_anlist_no_attrs was introduced in 2.3; here it
51  * is anticipated to allow using this overlay with 2.2. */
52
53 #ifdef OL_2_2_COMPAT
54 static AttributeName anlist_no_attrs[] = {
55         { BER_BVC( LDAP_NO_ATTRS ), NULL, 0, NULL },
56         { BER_BVNULL, NULL, 0, NULL }
57 };
58
59 static AttributeName *slap_anlist_no_attrs = anlist_no_attrs;
60 #endif
61
62 static AttributeDescription *ad_dgIdentity, *ad_dgAuthz;
63
64 typedef struct dynlist_map_t {
65         AttributeDescription *dlm_member_ad;
66         AttributeDescription *dlm_mapped_ad;
67         struct dynlist_map_t *dlm_next;
68 } dynlist_map_t;
69
70 typedef struct dynlist_info_t {
71         ObjectClass             *dli_oc;
72         AttributeDescription    *dli_ad;
73         struct dynlist_map_t    *dli_dlm;
74         struct berval           dli_default_filter;
75         struct dynlist_info_t   *dli_next;
76 } dynlist_info_t;
77
78 #define DYNLIST_USAGE \
79         "\"dynlist-attrset <oc> <URL-ad> [[<mapped-ad>:]<member-ad> ...]\": "
80
81 static dynlist_info_t *
82 dynlist_is_dynlist_next( Operation *op, SlapReply *rs, dynlist_info_t *old_dli )
83 {
84         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
85         dynlist_info_t  *dli;
86
87         Attribute       *a;
88
89         if ( old_dli == NULL ) {
90                 dli = (dynlist_info_t *)on->on_bi.bi_private;
91
92         } else {
93                 dli = old_dli->dli_next;
94         }
95
96         a = attrs_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
97         if ( a == NULL ) {
98                 /* FIXME: objectClass must be present; for non-storage
99                  * backends, like back-ldap, it needs to be added
100                  * to the requested attributes */
101                 return NULL;
102         }
103
104         for ( ; dli; dli = dli->dli_next ) {
105                 if ( attr_valfind( a,
106                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
107                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
108                                 &dli->dli_oc->soc_cname, NULL,
109                                 op->o_tmpmemctx ) == 0 )
110                 {
111                         return dli;
112                 }
113         }
114
115         return NULL;
116 }
117
118 static int
119 dynlist_make_filter( Operation *op, struct berval *oldf, struct berval *newf )
120 {
121         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
122         dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private;
123
124         char            *ptr;
125
126         assert( oldf != NULL );
127         assert( newf != NULL );
128         assert( !BER_BVISNULL( oldf ) );
129         assert( !BER_BVISEMPTY( oldf ) );
130
131         newf->bv_len = STRLENOF( "(&(!(objectClass=" "))" ")" )
132                 + dli->dli_oc->soc_cname.bv_len + oldf->bv_len;
133         newf->bv_val = op->o_tmpalloc( newf->bv_len + 1, op->o_tmpmemctx );
134         if ( newf->bv_val == NULL ) {
135                 return -1;
136         }
137         ptr = lutil_strcopy( newf->bv_val, "(&(!(objectClass=" );
138         ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
139         ptr = lutil_strcopy( ptr, "))" );
140         ptr = lutil_strcopy( ptr, oldf->bv_val );
141         ptr = lutil_strcopy( ptr, ")" );
142         newf->bv_len = ptr - newf->bv_val;
143
144         return 0;
145 }
146
147 typedef struct dynlist_sc_t {
148         dynlist_info_t    *dlc_dli;
149         Entry           *dlc_e;
150 } dynlist_sc_t;
151
152 static int
153 dynlist_sc_update( Operation *op, SlapReply *rs )
154 {
155         Entry                   *e;
156         Attribute               *a;
157         int                     opattrs,
158                                 userattrs;
159         AccessControlState      acl_state = ACL_STATE_INIT;
160
161         dynlist_sc_t            *dlc;
162         dynlist_map_t           *dlm;
163
164         if ( rs->sr_type != REP_SEARCH ) {
165                 return 0;
166         }
167
168         dlc = (dynlist_sc_t *)op->o_callback->sc_private;
169         e = dlc->dlc_e;
170
171         assert( e != NULL );
172         assert( rs->sr_entry != NULL );
173
174         /* test access to entry */
175         if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
176                                 NULL, ACL_READ, NULL ) )
177         {
178                 goto done;
179         }
180
181         /* if there is only one member_ad, and it's not mapped,
182          * consider it as old-style member listing */
183         dlm = dlc->dlc_dli->dli_dlm;
184         if ( dlm && dlm->dlm_mapped_ad == NULL && dlm->dlm_next == NULL ) {
185                 /* if access allowed, try to add values, emulating permissive
186                  * control to silently ignore duplicates */
187                 if ( access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
188                                         NULL, ACL_READ, NULL ) )
189                 {
190                         Modification    mod;
191                         const char      *text = NULL;
192                         char            textbuf[1024];
193                         struct berval   vals[ 2 ], nvals[ 2 ];
194
195                         vals[ 0 ] = rs->sr_entry->e_name;
196                         BER_BVZERO( &vals[ 1 ] );
197                         nvals[ 0 ] = rs->sr_entry->e_nname;
198                         BER_BVZERO( &nvals[ 1 ] );
199
200                         mod.sm_op = LDAP_MOD_ADD;
201                         mod.sm_desc = dlm->dlm_member_ad;
202                         mod.sm_type = dlm->dlm_member_ad->ad_cname;
203                         mod.sm_values = vals;
204                         mod.sm_nvalues = nvals;
205                         mod.sm_numvals = 1;
206
207                         (void)modify_add_values( e, &mod, /* permissive */ 1,
208                                         &text, textbuf, sizeof( textbuf ) );
209                 }
210
211                 goto done;
212         }
213
214 #ifndef SLAP_OPATTRS
215         opattrs = ( rs->sr_attrs == NULL ) ? 0 : an_find( rs->sr_attrs, &AllOper );
216         userattrs = ( rs->sr_attrs == NULL ) ? 1 : an_find( rs->sr_attrs, &AllUser );
217 #else /* SLAP_OPATTRS */
218         opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
219         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
220 #endif /* SLAP_OPATTRS */
221
222         for ( a = rs->sr_entry->e_attrs; a != NULL; a = a->a_next ) {
223                 BerVarray       vals, nvals = NULL;
224                 int             i, j,
225                                 is_oc = a->a_desc == slap_schema.si_ad_objectClass;
226
227                 /* if attribute is not requested, skip it */
228                 if ( rs->sr_attrs == NULL ) {
229                         if ( is_at_operational( a->a_desc->ad_type ) ) {
230                                 continue;
231                         }
232
233                 } else {
234                         if ( is_at_operational( a->a_desc->ad_type ) ) {
235                                 if ( !opattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
236                                 {
237                                         continue;
238                                 }
239
240                         } else {
241                                 if ( !userattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
242                                 {
243                                         continue;
244                                 }
245                         }
246                 }
247
248                 /* test access to attribute */
249                 if ( op->ors_attrsonly ) {
250                         if ( !access_allowed( op, rs->sr_entry, a->a_desc, NULL,
251                                                 ACL_READ, &acl_state ) )
252                         {
253                                 continue;
254                         }
255                 }
256
257                 /* single-value check: keep first only */
258                 if ( is_at_single_value( a->a_desc->ad_type ) ) {
259                         if ( attr_find( e->e_attrs, a->a_desc ) != NULL ) {
260                                 continue;
261                         }
262                 }
263
264                 /* test access to attribute */
265                 i = a->a_numvals;
266
267                 vals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
268                 if ( a->a_nvals != a->a_vals ) {
269                         nvals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
270                 }
271
272                 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
273                         if ( is_oc ) {
274                                 ObjectClass     *soc = oc_bvfind( &a->a_vals[i] );
275
276                                 if ( soc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
277                                         continue;
278                                 }
279                         }
280
281                         if ( access_allowed( op, rs->sr_entry, a->a_desc,
282                                                 &a->a_nvals[i], ACL_READ, &acl_state ) )
283                         {
284                                 vals[j] = a->a_vals[i];
285                                 if ( nvals ) {
286                                         nvals[j] = a->a_nvals[i];
287                                 }
288                                 j++;
289                         }
290                 }
291
292                 /* if access allowed, try to add values, emulating permissive
293                  * control to silently ignore duplicates */
294                 if ( j != 0 ) {
295                         Modification    mod;
296                         const char      *text = NULL;
297                         char            textbuf[1024];
298                         dynlist_map_t   *dlm;
299                         AttributeDescription *ad;
300
301                         BER_BVZERO( &vals[j] );
302                         if ( nvals ) {
303                                 BER_BVZERO( &nvals[j] );
304                         }
305
306                         ad = a->a_desc;
307                         for ( dlm = dlc->dlc_dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
308                                 if ( dlm->dlm_member_ad == a->a_desc ) {
309                                         if ( dlm->dlm_mapped_ad ) {
310                                                 ad = dlm->dlm_mapped_ad;
311                                         }
312                                         break;
313                                 }
314                         }
315
316                         mod.sm_op = LDAP_MOD_ADD;
317                         mod.sm_desc = ad;
318                         mod.sm_type = ad->ad_cname;
319                         mod.sm_values = vals;
320                         mod.sm_nvalues = nvals;
321                         mod.sm_numvals = j;
322
323                         (void)modify_add_values( e, &mod, /* permissive */ 1,
324                                         &text, textbuf, sizeof( textbuf ) );
325                 }
326
327                 op->o_tmpfree( vals, op->o_tmpmemctx );
328                 if ( nvals ) {
329                         op->o_tmpfree( nvals, op->o_tmpmemctx );
330                 }
331         }
332
333 done:;
334         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
335                 entry_free( rs->sr_entry );
336                 rs->sr_entry = NULL;
337                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
338         }
339
340         return 0;
341 }
342         
343 static int
344 dynlist_prepare_entry( Operation *op, SlapReply *rs, dynlist_info_t *dli )
345 {
346         Attribute       *a, *id = NULL;
347         slap_callback   cb;
348         Operation       o = *op;
349         SlapReply       r = { REP_SEARCH };
350         struct berval   *url;
351         Entry           *e;
352         slap_mask_t     e_flags;
353         int             opattrs,
354                         userattrs;
355         dynlist_sc_t    dlc = { 0 };
356         dynlist_map_t   *dlm;
357
358         a = attrs_find( rs->sr_entry->e_attrs, dli->dli_ad );
359         if ( a == NULL ) {
360                 /* FIXME: error? */
361                 return SLAP_CB_CONTINUE;
362         }
363
364 #ifndef SLAP_OPATTRS
365         opattrs = ( rs->sr_attrs == NULL ) ? 0 : an_find( rs->sr_attrs, &AllOper );
366         userattrs = ( rs->sr_attrs == NULL ) ? 1 : an_find( rs->sr_attrs, &AllUser );
367 #else /* SLAP_OPATTRS */
368         opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
369         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
370 #endif /* SLAP_OPATTRS */
371
372         /* Don't generate member list if it wasn't requested */
373         for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
374                 if ( userattrs ||
375                      ad_inlist( dlm->dlm_member_ad, rs->sr_attrs ) ) 
376                         break;
377         }
378         if ( dli->dli_dlm && !dlm )
379                 return SLAP_CB_CONTINUE;
380
381         if ( ad_dgIdentity && ( id = attrs_find( rs->sr_entry->e_attrs, ad_dgIdentity ))) {
382                 Attribute *authz = NULL;
383
384                 /* if not rootdn and dgAuthz is present,
385                  * check if user can be authorized as dgIdentity */
386                 if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
387                         && ( authz = attrs_find( rs->sr_entry->e_attrs, ad_dgAuthz ) ) )
388                 {
389                         if ( slap_sasl_matches( op, authz->a_nvals,
390                                 &o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
391                         {
392                                 return SLAP_CB_CONTINUE;
393                         }
394                 }
395
396                 o.o_dn = id->a_vals[0];
397                 o.o_ndn = id->a_nvals[0];
398                 o.o_groups = NULL;
399         }
400
401         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
402                 e = entry_dup( rs->sr_entry );
403         } else {
404                 e = rs->sr_entry;
405         }
406         e_flags = rs->sr_flags | ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
407
408         dlc.dlc_e = e;
409         dlc.dlc_dli = dli;
410         cb.sc_private = &dlc;
411         cb.sc_response = dynlist_sc_update;
412         cb.sc_cleanup = NULL;
413         cb.sc_next = NULL;
414
415         o.o_callback = &cb;
416         o.ors_deref = LDAP_DEREF_NEVER;
417         o.ors_limit = NULL;
418         o.ors_tlimit = SLAP_NO_LIMIT;
419         o.ors_slimit = SLAP_NO_LIMIT;
420
421         for ( url = a->a_nvals; !BER_BVISNULL( url ); url++ ) {
422                 LDAPURLDesc     *lud = NULL;
423                 int             i, j;
424                 struct berval   dn;
425                 int             rc;
426                 dynlist_map_t   *dlm;
427
428                 BER_BVZERO( &o.o_req_dn );
429                 BER_BVZERO( &o.o_req_ndn );
430                 o.ors_filter = NULL;
431                 o.ors_attrs = NULL;
432                 BER_BVZERO( &o.ors_filterstr );
433
434                 if ( ldap_url_parse( url->bv_val, &lud ) != LDAP_URL_SUCCESS ) {
435                         /* FIXME: error? */
436                         continue;
437                 }
438
439                 if ( lud->lud_host != NULL ) {
440                         /* FIXME: host not allowed; reject as illegal? */
441                         Debug( LDAP_DEBUG_ANY, "dynlist_prepare_entry(\"%s\"): "
442                                 "illegal URI \"%s\"\n",
443                                 e->e_name.bv_val, url->bv_val, 0 );
444                         goto cleanup;
445                 }
446
447                 if ( lud->lud_dn == NULL ) {
448                         /* note that an empty base is not honored in terms
449                          * of defaultSearchBase, because select_backend()
450                          * is not aware of the defaultSearchBase option;
451                          * this can be useful in case of a database serving
452                          * the empty suffix */
453                         BER_BVSTR( &dn, "" );
454
455                 } else {
456                         ber_str2bv( lud->lud_dn, 0, 0, &dn );
457                 }
458                 rc = dnPrettyNormal( NULL, &dn, &o.o_req_dn, &o.o_req_ndn, op->o_tmpmemctx );
459                 if ( rc != LDAP_SUCCESS ) {
460                         /* FIXME: error? */
461                         goto cleanup;
462                 }
463                 o.ors_scope = lud->lud_scope;
464
465                 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
466                         if ( dlm->dlm_mapped_ad != NULL ) {
467                                 break;
468                         }
469                 }
470
471                 if ( dli->dli_dlm && !dlm ) {
472                         /* if ( lud->lud_attrs != NULL ),
473                          * the URL should be ignored */
474                         o.ors_attrs = slap_anlist_no_attrs;
475
476                 } else if ( lud->lud_attrs == NULL ) {
477                         o.ors_attrs = rs->sr_attrs;
478
479                 } else {
480                         for ( i = 0; lud->lud_attrs[i]; i++)
481                                 /* just count */ ;
482
483                         o.ors_attrs = op->o_tmpcalloc( i + 1, sizeof( AttributeName ), op->o_tmpmemctx );
484                         for ( i = 0, j = 0; lud->lud_attrs[i]; i++) {
485                                 const char      *text = NULL;
486         
487                                 ber_str2bv( lud->lud_attrs[i], 0, 0, &o.ors_attrs[j].an_name );
488                                 o.ors_attrs[j].an_desc = NULL;
489                                 (void)slap_bv2ad( &o.ors_attrs[j].an_name, &o.ors_attrs[j].an_desc, &text );
490                                 /* FIXME: ignore errors... */
491
492                                 if ( rs->sr_attrs == NULL ) {
493                                         if ( o.ors_attrs[j].an_desc != NULL &&
494                                                         is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
495                                         {
496                                                 continue;
497                                         }
498
499                                 } else {
500                                         if ( o.ors_attrs[j].an_desc != NULL &&
501                                                         is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
502                                         {
503                                                 if ( !opattrs && !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
504                                                 {
505                                                         continue;
506                                                 }
507
508                                         } else {
509                                                 if ( !userattrs && 
510                                                                 o.ors_attrs[j].an_desc != NULL &&
511                                                                 !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
512                                                 {
513                                                         continue;
514                                                 }
515                                         }
516                                 }
517
518                                 j++;
519                         }
520
521                         if ( j == 0 ) {
522                                 goto cleanup;
523                         }
524                 
525                         BER_BVZERO( &o.ors_attrs[j].an_name );
526                 }
527
528                 if ( lud->lud_filter == NULL ) {
529                         ber_dupbv_x( &o.ors_filterstr,
530                                         &dli->dli_default_filter, op->o_tmpmemctx );
531
532                 } else {
533                         struct berval   flt;
534                         ber_str2bv( lud->lud_filter, 0, 0, &flt );
535                         if ( dynlist_make_filter( op, &flt, &o.ors_filterstr ) ) {
536                                 /* error */
537                                 goto cleanup;
538                         }
539                 }
540                 o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
541                 if ( o.ors_filter == NULL ) {
542                         goto cleanup;
543                 }
544                 
545                 o.o_bd = select_backend( &o.o_req_ndn, 1 );
546                 if ( o.o_bd && o.o_bd->be_search ) {
547 #ifdef SLAP_OPATTRS
548                         r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
549 #endif /* SLAP_OPATTRS */
550                         (void)o.o_bd->be_search( &o, &r );
551                 }
552
553 cleanup:;
554                 if ( id ) {
555                         slap_op_groups_free( &o );
556                 }
557                 if ( o.ors_filter ) {
558                         filter_free_x( &o, o.ors_filter );
559                 }
560                 if ( o.ors_attrs && o.ors_attrs != rs->sr_attrs
561                                 && o.ors_attrs != slap_anlist_no_attrs )
562                 {
563                         op->o_tmpfree( o.ors_attrs, op->o_tmpmemctx );
564                 }
565                 if ( !BER_BVISNULL( &o.o_req_dn ) ) {
566                         op->o_tmpfree( o.o_req_dn.bv_val, op->o_tmpmemctx );
567                 }
568                 if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
569                         op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
570                 }
571                 assert( BER_BVISNULL( &o.ors_filterstr )
572                         || o.ors_filterstr.bv_val != lud->lud_filter );
573                 op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
574                 ldap_free_urldesc( lud );
575         }
576
577         rs->sr_entry = e;
578         rs->sr_flags = e_flags;
579
580         return SLAP_CB_CONTINUE;
581 }
582
583 static int
584 dynlist_sc_save_entry( Operation *op, SlapReply *rs )
585 {
586         /* save the entry in the private field of the callback,
587          * so it doesn't get freed (it's temporary!) */
588         if ( rs->sr_entry != NULL ) {
589                 dynlist_sc_t    *dlc = (dynlist_sc_t *)op->o_callback->sc_private;
590                 dlc->dlc_e = rs->sr_entry;
591                 rs->sr_entry = NULL;
592         }
593
594         return 0;
595 }
596
597 static int
598 dynlist_compare( Operation *op, SlapReply *rs )
599 {
600         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
601         dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private;
602         Operation o = *op;
603         Entry *e = NULL;
604         dynlist_map_t *dlm;
605
606         for ( ; dli != NULL; dli = dli->dli_next ) {
607                 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next )
608                         if ( op->oq_compare.rs_ava->aa_desc == dlm->dlm_member_ad )
609                                 break;
610
611                 if ( dli->dli_dlm && dlm ) {
612                         /* This compare is for one of the attributes we're
613                          * interested in. We'll use slapd's existing dyngroup
614                          * evaluator to get the answer we want.
615                          */
616                         BerVarray id = NULL, authz = NULL;
617
618                         o.o_do_not_cache = 1;
619
620                         if ( ad_dgIdentity && backend_attribute( &o, NULL, &o.o_req_ndn,
621                                 ad_dgIdentity, &id, ACL_READ ) == LDAP_SUCCESS )
622                         {
623                                 /* if not rootdn and dgAuthz is present,
624                                  * check if user can be authorized as dgIdentity */
625                                 if ( ad_dgAuthz && !BER_BVISEMPTY( id ) && !be_isroot( op )
626                                         && backend_attribute( &o, NULL, &o.o_req_ndn,
627                                                 ad_dgAuthz, &authz, ACL_READ ) == LDAP_SUCCESS )
628                                 {
629                                         
630                                         rs->sr_err = slap_sasl_matches( op, authz,
631                                                 &o.o_ndn, &o.o_ndn );
632                                         ber_bvarray_free_x( authz, op->o_tmpmemctx );
633                                         if ( rs->sr_err != LDAP_SUCCESS ) {
634                                                 goto done;
635                                         }
636                                 }
637
638                                 o.o_dn = *id;
639                                 o.o_ndn = *id;
640                                 o.o_groups = NULL; /* authz changed, invalidate cached groups */
641                         }
642
643                         rs->sr_err = backend_group( &o, NULL, &o.o_req_ndn,
644                                 &o.oq_compare.rs_ava->aa_value, dli->dli_oc, dli->dli_ad );
645                         switch ( rs->sr_err ) {
646                         case LDAP_SUCCESS:
647                                 rs->sr_err = LDAP_COMPARE_TRUE;
648                                 break;
649
650                         case LDAP_NO_SUCH_OBJECT:
651                                 /* NOTE: backend_group() returns noSuchObject
652                                  * if op_ndn does not exist; however, since
653                                  * dynamic list expansion means that the
654                                  * member attribute is virtually present, the
655                                  * non-existence of the asserted value implies
656                                  * the assertion is FALSE rather than
657                                  * UNDEFINED */
658                                 rs->sr_err = LDAP_COMPARE_FALSE;
659                                 break;
660                         }
661
662 done:;
663                         if ( id ) ber_bvarray_free_x( id, o.o_tmpmemctx );
664
665                         return SLAP_CB_CONTINUE;
666                 }
667         }
668
669         if ( overlay_entry_get_ov( &o, &o.o_req_ndn, NULL, NULL, 0, &e, on ) !=
670                 LDAP_SUCCESS || e == NULL )
671         {
672                 return SLAP_CB_CONTINUE;
673         }
674
675         if ( ad_dgIdentity ) {
676                 Attribute *id = attrs_find( e->e_attrs, ad_dgIdentity );
677                 if ( id ) {
678                         Attribute *authz;
679
680                         /* if not rootdn and dgAuthz is present,
681                          * check if user can be authorized as dgIdentity */
682                         if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
683                                 && ( authz = attrs_find( e->e_attrs, ad_dgAuthz ) ) )
684                         {
685                                 if ( slap_sasl_matches( op, authz->a_nvals,
686                                         &o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
687                                 {
688                                         goto release;
689                                 }
690                         }
691
692                         o.o_dn = id->a_vals[0];
693                         o.o_ndn = id->a_nvals[0];
694                         o.o_groups = NULL;
695                 }
696         }
697
698         dli = (dynlist_info_t *)on->on_bi.bi_private;
699         for ( ; dli != NULL && rs->sr_err != LDAP_COMPARE_TRUE; dli = dli->dli_next ) {
700                 Attribute       *a;
701                 slap_callback   cb;
702                 SlapReply       r = { REP_SEARCH };
703                 AttributeName   an[2];
704                 int             rc;
705                 dynlist_sc_t    dlc = { 0 };
706
707                 if ( !is_entry_objectclass_or_sub( e, dli->dli_oc ))
708                         continue;
709
710                 /* if the entry has the right objectClass, generate
711                  * the dynamic list and compare */
712                 dlc.dlc_dli = dli;
713                 cb.sc_private = &dlc;
714                 cb.sc_response = dynlist_sc_save_entry;
715                 cb.sc_cleanup = NULL;
716                 cb.sc_next = NULL;
717                 o.o_callback = &cb;
718
719                 o.o_tag = LDAP_REQ_SEARCH;
720                 o.ors_limit = NULL;
721                 o.ors_tlimit = SLAP_NO_LIMIT;
722                 o.ors_slimit = SLAP_NO_LIMIT;
723
724                 o.o_bd = select_backend( &o.o_req_ndn, 1 );
725                 if ( !o.o_bd || !o.o_bd->be_search ) {
726                         goto release;
727                 }
728
729                 o.ors_filterstr = *slap_filterstr_objectClass_pres;
730                 o.ors_filter = (Filter *) slap_filter_objectClass_pres;
731
732                 o.ors_scope = LDAP_SCOPE_BASE;
733                 o.ors_deref = LDAP_DEREF_NEVER;
734                 an[0].an_name = op->orc_ava->aa_desc->ad_cname;
735                 an[0].an_desc = op->orc_ava->aa_desc;
736                 BER_BVZERO( &an[1].an_name );
737                 o.ors_attrs = an;
738                 o.ors_attrsonly = 0;
739
740                 o.o_acl_priv = ACL_COMPARE;
741
742                 rc = o.o_bd->be_search( &o, &r );
743
744                 if ( o.o_dn.bv_val != op->o_dn.bv_val ) {
745                         slap_op_groups_free( &o );
746                 }
747
748                 if ( rc != 0 ) {
749                         goto release;
750                 }
751
752                 if ( dlc.dlc_e != NULL ) {
753                         r.sr_entry = dlc.dlc_e;
754                 }
755
756                 if ( r.sr_err != LDAP_SUCCESS || r.sr_entry == NULL ) {
757                         /* error? */
758                         goto release;
759                 }
760
761                 for ( a = attrs_find( r.sr_entry->e_attrs, op->orc_ava->aa_desc );
762                         a != NULL;
763                         a = attrs_find( a->a_next, op->orc_ava->aa_desc ) )
764                 {
765                         /* if we're here, we got a match... */
766                         rs->sr_err = LDAP_COMPARE_FALSE;
767
768                         if ( attr_valfind( a,
769                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
770                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
771                                 &op->orc_ava->aa_value, NULL, op->o_tmpmemctx ) == 0 )
772                         {
773                                 rs->sr_err = LDAP_COMPARE_TRUE;
774                                 break;
775                         }
776                 }
777
778                 if ( r.sr_flags & REP_ENTRY_MUSTBEFREED ) {
779                         entry_free( r.sr_entry );
780                 }
781         }
782
783 release:;
784         if ( e != NULL ) {
785                 overlay_entry_release_ov( op, e, 0, on );
786         }
787
788         return SLAP_CB_CONTINUE;
789 }
790
791 static int
792 dynlist_response( Operation *op, SlapReply *rs )
793 {
794         dynlist_info_t  *dli;
795
796         switch ( op->o_tag ) {
797         case LDAP_REQ_SEARCH:
798                 if ( rs->sr_type == REP_SEARCH && !get_manageDSAit( op ) )
799                 {
800                         int     rc = LDAP_OTHER;
801
802                         for ( dli = dynlist_is_dynlist_next( op, rs, NULL );
803                                 dli;
804                                 dli = dynlist_is_dynlist_next( op, rs, dli ) )
805                         {
806                                 rc = dynlist_prepare_entry( op, rs, dli );
807                         }
808
809                         if ( rc != LDAP_OTHER ) {
810                                 return rc;
811                         }
812                 }
813                 break;
814
815         case LDAP_REQ_COMPARE:
816                 switch ( rs->sr_err ) {
817                 /* NOTE: we waste a few cycles running the dynamic list
818                  * also when the result is FALSE, which occurs if the
819                  * dynamic entry itself contains the AVA attribute  */
820                 /* FIXME: this approach is less than optimal; a dedicated
821                  * compare op should be implemented, that fetches the
822                  * entry, checks if it has the appropriate objectClass
823                  * and, in case, runs a compare thru all the URIs,
824                  * stopping at the first positive occurrence; see ITS#3756 */
825                 case LDAP_COMPARE_FALSE:
826                 case LDAP_NO_SUCH_ATTRIBUTE:
827                         return dynlist_compare( op, rs );
828                 }
829                 break;
830
831         default:
832                 break;
833         }
834
835         return SLAP_CB_CONTINUE;
836 }
837
838 static int
839 dynlist_build_def_filter( dynlist_info_t *dli )
840 {
841         char    *ptr;
842
843         dli->dli_default_filter.bv_len = STRLENOF( "(!(objectClass=" "))" )
844                 + dli->dli_oc->soc_cname.bv_len;
845         dli->dli_default_filter.bv_val = ch_malloc( dli->dli_default_filter.bv_len + 1 );
846         if ( dli->dli_default_filter.bv_val == NULL ) {
847                 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: malloc failed.\n",
848                         0, 0, 0 );
849                 return -1;
850         }
851
852         ptr = lutil_strcopy( dli->dli_default_filter.bv_val, "(!(objectClass=" );
853         ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
854         ptr = lutil_strcopy( ptr, "))" );
855
856         assert( dli->dli_default_filter.bv_len == ptr - dli->dli_default_filter.bv_val );
857
858         return 0;
859 }
860
861 #ifdef OL_2_2_COMPAT
862 static int
863 dynlist_db_config(
864         BackendDB       *be,
865         const char      *fname,
866         int             lineno,
867         int             argc,
868         char            **argv )
869 {
870         slap_overinst   *on = (slap_overinst *)be->bd_info;
871
872         int             rc = 0;
873
874         if ( strcasecmp( argv[0], "dynlist-attrset" ) == 0 ) {
875                 dynlist_info_t          **dlip;
876                 ObjectClass             *oc;
877                 AttributeDescription    *ad = NULL,
878                                         *member_ad = NULL;
879                 dynlist_map_t           *dlm = NULL;
880                 const char              *text;
881
882                 if ( argc < 3 ) {
883                         Debug( LDAP_DEBUG_ANY, "%s: line %d: " DYNLIST_USAGE
884                                 "invalid arg number #%d.\n",
885                                 fname, lineno, argc );
886                         return 1;
887                 }
888
889                 oc = oc_find( argv[1] );
890                 if ( oc == NULL ) {
891                         Debug( LDAP_DEBUG_ANY, "%s: line %d: " DYNLIST_USAGE
892                                 "unable to find ObjectClass \"%s\"\n",
893                                 fname, lineno, argv[ 1 ] );
894                         return 1;
895                 }
896
897                 rc = slap_str2ad( argv[2], &ad, &text );
898                 if ( rc != LDAP_SUCCESS ) {
899                         Debug( LDAP_DEBUG_ANY, "%s: line %d: " DYNLIST_USAGE
900                                 "unable to find AttributeDescription \"%s\"\n",
901                                 fname, lineno, argv[2] );
902                         return 1;
903                 }
904
905                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
906                         Debug( LDAP_DEBUG_ANY, "%s: line %d: " DYNLIST_USAGE
907                                 "AttributeDescription \"%s\" "
908                                 "must be a subtype of \"labeledURI\"\n",
909                                 fname, lineno, argv[2] );
910                         return 1;
911                 }
912
913                 for ( i = 3; i < argc; i++ ) {
914                         char *arg; 
915                         char *cp;
916                         AttributeDescription *member_ad = NULL;
917                         AttributeDescription *mapped_ad = NULL;
918                         dynlist_map_t *dlmp;
919                         dynlist_map_t *dlml;
920
921
922                         /*
923                          * If no mapped attribute is given, dn is used 
924                          * for backward compatibility.
925                          */
926                         arg = argv[i];
927                         if ( cp = strchr( arg, (int)':' ) != NULL ) {
928                                 struct berval bv;
929                                 ber_str2bv( arg, cp - arg, 0, &bv );
930                                 rc = slap_bv2ad( &bv, &mapped_ad, &text );
931                                 if ( rc != LDAP_SUCCESS ) {
932                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
933                                                 DYNLIST_USAGE
934                                                 "unable to find mapped AttributeDescription \"%s\"\n",
935                                                 fname, lineno, arg );
936                                         return 1;
937                                 }
938                                 
939                                 arg = cp + 1;
940                         }
941
942                         rc = slap_str2ad( arg, &member_ad, &text );
943                         if ( rc != LDAP_SUCCESS ) {
944                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
945                                         DYNLIST_USAGE
946                                         "unable to find AttributeDescription \"%s\"\n",
947                                         fname, lineno, arg );
948                                 return 1;
949                         }
950
951                         dlmp = (dynlist_map_t *)ch_calloc( 1, sizeof( dynlist_map_t ) );
952                         if ( dlm == NULL ) {
953                                 dlm = dlmp;
954                                 dlml = NULL;
955                         }
956                         dlmp->dlm_member_ad = member_ad;
957                         dlmp->dlm_mapped_ad = mapped_ad;
958                         dlmp->dlm_next = NULL;
959                 
960                         if ( dlml != NULL )
961                                 dlml->dlm_next = dlmp;
962                         dlml = dlmp;
963                 }
964
965                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
966                         *dlip; dlip = &(*dlip)->dli_next )
967                 {
968                         /* 
969                          * The same URL attribute / member attribute pair
970                          * cannot be repeated, but we enforce this only 
971                          * when the member attribute is unique. Performing
972                          * the check for multiple values would require
973                          * sorting and comparing the lists, which is left
974                          * as a future improvement
975                          */
976                         if ( (*dlip)->dli_ad == ad &&
977                              (*dlip)->dli_dlm->dlm_next == NULL &&
978                              dlm->dlm_next == NULL &&
979                              dlm->dlm_member_ad == (*dlip)->dli_dlm->dlm_member_ad &&
980                              dlm->dlm_mapped_ad == (*dlip)->dli_dlm->dlm_mapped_ad ) {
981                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
982                                         DYNLIST_USAGE
983                                         "URL attributeDescription \"%s\" already mapped.\n",
984                                         fname, lineno, ad->ad_cname.bv_val );
985 #if 0
986                                 /* make it a warning... */
987                                 return 1;
988 #endif
989                         }
990                 }
991
992                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
993                 (*dlip)->dli_oc = oc;
994                 (*dlip)->dli_ad = ad;
995                 (*dlip)->dli_dlm = dlm;
996
997                 if ( dynlist_build_def_filter( *dlip ) ) {
998                         dynlist_map_t *dlm = (*dlip)->ldi_dlm;
999                         dynlist_map_t *dlm_next;
1000
1001                         while ( dlm != NULL ) {
1002                                 dlm_next = dlm->dlm_next;
1003                                 ch_free( dlm );
1004                                 dlm = dlm_next;
1005                         }
1006
1007                         ch_free( *dlip );
1008                         *dlip = NULL;
1009                         return 1;
1010                 }
1011
1012         /* allow dyngroup syntax */
1013         } else if ( strcasecmp( argv[0], "dynlist-attrpair" ) == 0 ) {
1014                 dynlist_info_t          **dlip;
1015                 ObjectClass             *oc;
1016                 AttributeDescription    *ad = NULL,
1017                                         *member_ad = NULL;
1018                 const char              *text;
1019
1020                 if ( argc != 3 ) {
1021                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1022                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1023                                 "invalid arg number #%d.\n",
1024                                 fname, lineno, argc );
1025                         return 1;
1026                 }
1027
1028                 oc = oc_find( "groupOfURLs" );
1029                 if ( oc == NULL ) {
1030                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1031                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1032                                 "unable to find default ObjectClass \"groupOfURLs\"\n",
1033                                 fname, lineno, 0 );
1034                         return 1;
1035                 }
1036
1037                 rc = slap_str2ad( argv[1], &member_ad, &text );
1038                 if ( rc != LDAP_SUCCESS ) {
1039                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1040                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1041                                 "unable to find AttributeDescription \"%s\"\n",
1042                                 fname, lineno, argv[1] );
1043                         return 1;
1044                 }
1045
1046                 rc = slap_str2ad( argv[2], &ad, &text );
1047                 if ( rc != LDAP_SUCCESS ) {
1048                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1049                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1050                                 "unable to find AttributeDescription \"%s\"\n",
1051                                 fname, lineno, argv[2] );
1052                         return 1;
1053                 }
1054
1055                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1056                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1057                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1058                                 "AttributeDescription \"%s\" "
1059                                 "must be a subtype of \"labeledURI\"\n",
1060                                 fname, lineno, argv[2] );
1061                         return 1;
1062                 }
1063
1064                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1065                         *dlip; dlip = &(*dlip)->dli_next )
1066                 {
1067                         /* 
1068                          * The same URL attribute / member attribute pair
1069                          * cannot be repeated, but we enforce this only 
1070                          * when the member attribute is unique. Performing
1071                          * the check for multiple values would require
1072                          * sorting and comparing the lists, which is left
1073                          * as a future improvement
1074                          */
1075                         if ( (*dlip)->dli_ad == ad &&
1076                              (*dlip)->dli_dlm->dlm_next == NULL &&
1077                              member_ad == (*dlip)->dli_dlm->dlm_member_ad ) {
1078                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1079                                         "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1080                                         "URL attributeDescription \"%s\" already mapped.\n",
1081                                         fname, lineno, ad->ad_cname.bv_val );
1082 #if 0
1083                                 /* make it a warning... */
1084                                 return 1;
1085 #endif
1086                         }
1087                 }
1088
1089                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1090                 (*dlip)->dli_oc = oc;
1091                 (*dlip)->dli_ad = ad;
1092                 (*dlip)->dli_dlm = (dynlist_map_t *)ch_calloc( 1, sizeof( dynlist_map_t ) );
1093                 (*dlip)->dli_dlm->dlm_member_ad = member_ad;
1094                 (*dlip)->dli_dlm->dlm_mapped_ad = NULL;
1095
1096                 if ( dynlist_build_def_filter( *dlip ) ) {
1097                         ch_free( (*dlip)->dli_dlm );
1098                         ch_free( *dlip );
1099                         *dlip = NULL;
1100                         return 1;
1101                 }
1102
1103         } else {
1104                 rc = SLAP_CONF_UNKNOWN;
1105         }
1106
1107         return rc;
1108 }
1109
1110 #else
1111 enum {
1112         DL_ATTRSET = 1,
1113         DL_ATTRPAIR,
1114         DL_ATTRPAIR_COMPAT,
1115         DL_LAST
1116 };
1117
1118 static ConfigDriver     dl_cfgen;
1119
1120 /* XXXmanu 255 is the maximum arguments we allow. Can we go beyond? */
1121 static ConfigTable dlcfg[] = {
1122         { "dynlist-attrset", "group-oc> <URL-ad> <member-ad",
1123                 3, 255, 0, ARG_MAGIC|DL_ATTRSET, dl_cfgen,
1124                 "( OLcfgOvAt:8.1 NAME 'olcDLattrSet' "
1125                         "DESC 'Dynamic list: <group objectClass>, <URL attributeDescription>, <member attributeDescription>' "
1126                         "EQUALITY caseIgnoreMatch "
1127                         "SYNTAX OMsDirectoryString "
1128                         "X-ORDERED 'VALUES' )",
1129                         NULL, NULL },
1130         { "dynlist-attrpair", "member-ad> <URL-ad",
1131                 3, 3, 0, ARG_MAGIC|DL_ATTRPAIR, dl_cfgen,
1132                         NULL, NULL, NULL },
1133 #ifdef TAKEOVER_DYNGROUP
1134         { "attrpair", "member-ad> <URL-ad",
1135                 3, 3, 0, ARG_MAGIC|DL_ATTRPAIR_COMPAT, dl_cfgen,
1136                         NULL, NULL, NULL },
1137 #endif
1138         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1139 };
1140
1141 static ConfigOCs dlocs[] = {
1142         { "( OLcfgOvOc:8.1 "
1143                 "NAME 'olcDynamicList' "
1144                 "DESC 'Dynamic list configuration' "
1145                 "SUP olcOverlayConfig "
1146                 "MAY olcDLattrSet )",
1147                 Cft_Overlay, dlcfg, NULL, NULL },
1148         { NULL, 0, NULL }
1149 };
1150
1151 static int
1152 dl_cfgen( ConfigArgs *c )
1153 {
1154         slap_overinst   *on = (slap_overinst *)c->bi;
1155         dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private;
1156
1157         int             rc = 0, i;
1158
1159         if ( c->op == SLAP_CONFIG_EMIT ) {
1160                 switch( c->type ) {
1161                 case DL_ATTRSET:
1162                         for ( i = 0; dli; i++, dli = dli->dli_next ) {
1163                                 struct berval   bv;
1164                                 char            *ptr = c->cr_msg;
1165                                 dynlist_map_t   *dlm;
1166
1167                                 assert( dli->dli_oc != NULL );
1168                                 assert( dli->dli_ad != NULL );
1169
1170                                 ptr += snprintf( c->cr_msg, sizeof( c->cr_msg ),
1171                                         SLAP_X_ORDERED_FMT "%s %s", i,
1172                                         dli->dli_oc->soc_cname.bv_val,
1173                                         dli->dli_ad->ad_cname.bv_val );
1174
1175                                 for ( dlm = dli->dli_dlm; dlm; dlm = dlm->dlm_next ) {
1176                                         ptr[ 0 ] = ' ';
1177                                         ptr++;
1178                                         if ( dlm->dlm_mapped_ad ) {
1179                                                 ptr = lutil_strcopy( ptr, dlm->dlm_mapped_ad->ad_cname.bv_val );
1180                                                 ptr[ 0 ] = ':';
1181                                                 ptr++;
1182                                         }
1183                                                 
1184                                         ptr = lutil_strcopy( ptr, dlm->dlm_member_ad->ad_cname.bv_val );
1185                                 }
1186
1187                                 bv.bv_val = c->cr_msg;
1188                                 bv.bv_len = ptr - bv.bv_val;
1189                                 value_add_one( &c->rvalue_vals, &bv );
1190                         }
1191                         break;
1192
1193                 case DL_ATTRPAIR_COMPAT:
1194                 case DL_ATTRPAIR:
1195                         rc = 1;
1196                         break;
1197
1198                 default:
1199                         rc = 1;
1200                         break;
1201                 }
1202
1203                 return rc;
1204
1205         } else if ( c->op == LDAP_MOD_DELETE ) {
1206                 switch( c->type ) {
1207                 case DL_ATTRSET:
1208                         if ( c->valx < 0 ) {
1209                                 dynlist_info_t  *dli_next;
1210
1211                                 for ( dli_next = dli; dli_next; dli = dli_next ) {
1212                                         dynlist_map_t *dlm = dli->dli_dlm;
1213                                         dynlist_map_t *dlm_next;
1214
1215                                         dli_next = dli->dli_next;
1216
1217                                         ch_free( dli->dli_default_filter.bv_val );
1218
1219                                         while ( dlm != NULL ) {
1220                                                 dlm_next = dlm->dlm_next;
1221                                                 ch_free( dlm );
1222                                                 dlm = dlm_next;
1223                                         }
1224                                         ch_free( dli );
1225                                 }
1226
1227                                 on->on_bi.bi_private = NULL;
1228
1229                         } else {
1230                                 dynlist_info_t  **dlip;
1231                                 dynlist_map_t *dlm;
1232                                 dynlist_map_t *dlm_next;
1233
1234                                 for ( i = 0, dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1235                                         i < c->valx; i++ )
1236                                 {
1237                                         if ( *dlip == NULL ) {
1238                                                 return 1;
1239                                         }
1240                                         dlip = &(*dlip)->dli_next;
1241                                 }
1242
1243                                 dli = *dlip;
1244                                 *dlip = dli->dli_next;
1245                                 ch_free( dli->dli_default_filter.bv_val );
1246
1247                                 dlm = dli->dli_dlm;
1248                                 while ( dlm != NULL ) {
1249                                         dlm_next = dlm->dlm_next;
1250                                         ch_free( dlm );
1251                                         dlm = dlm_next;
1252                                 }
1253                                 ch_free( dli );
1254
1255                                 dli = (dynlist_info_t *)on->on_bi.bi_private;
1256                         }
1257                         break;
1258
1259                 case DL_ATTRPAIR_COMPAT:
1260                 case DL_ATTRPAIR:
1261                         rc = 1;
1262                         break;
1263
1264                 default:
1265                         rc = 1;
1266                         break;
1267                 }
1268
1269                 return rc;
1270         }
1271
1272         switch( c->type ) {
1273         case DL_ATTRSET: {
1274                 dynlist_info_t          **dlip,
1275                                         *dli_next = NULL;
1276                 ObjectClass             *oc = NULL;
1277                 AttributeDescription    *ad = NULL;
1278                 dynlist_map_t           *dlm = NULL;
1279                 const char              *text;
1280
1281                 oc = oc_find( c->argv[ 1 ] );
1282                 if ( oc == NULL ) {
1283                         snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1284                                 "unable to find ObjectClass \"%s\"",
1285                                 c->argv[ 1 ] );
1286                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1287                                 c->log, c->cr_msg, 0 );
1288                         return 1;
1289                 }
1290
1291                 rc = slap_str2ad( c->argv[ 2 ], &ad, &text );
1292                 if ( rc != LDAP_SUCCESS ) {
1293                         snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1294                                 "unable to find AttributeDescription \"%s\"",
1295                                 c->argv[ 2 ] );
1296                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1297                                 c->log, c->cr_msg, 0 );
1298                         return 1;
1299                 }
1300
1301                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1302                         snprintf( c->cr_msg, sizeof( c->cr_msg ), DYNLIST_USAGE
1303                                 "AttributeDescription \"%s\" "
1304                                 "must be a subtype of \"labeledURI\"",
1305                                 c->argv[ 2 ] );
1306                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1307                                 c->log, c->cr_msg, 0 );
1308                         return 1;
1309                 }
1310
1311                 for ( i = 3; i < c->argc; i++ ) {
1312                         char *arg; 
1313                         char *cp;
1314                         AttributeDescription *member_ad = NULL;
1315                         AttributeDescription *mapped_ad = NULL;
1316                         dynlist_map_t *dlmp;
1317                         dynlist_map_t *dlml;
1318
1319
1320                         /*
1321                          * If no mapped attribute is given, dn is used 
1322                          * for backward compatibility.
1323                          */
1324                         arg = c->argv[i];
1325                         if ( ( cp = strchr( arg, ':' ) ) != NULL ) {
1326                                 struct berval bv;
1327                                 ber_str2bv( arg, cp - arg, 0, &bv );
1328                                 rc = slap_bv2ad( &bv, &mapped_ad, &text );
1329                                 if ( rc != LDAP_SUCCESS ) {
1330                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1331                                                 DYNLIST_USAGE
1332                                                 "unable to find mapped AttributeDescription #%d \"%s\"\n",
1333                                                 i - 3, c->argv[ i ] );
1334                                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1335                                                 c->log, c->cr_msg, 0 );
1336                                         return 1;
1337                                 }
1338                                 arg = cp + 1;
1339                         }
1340
1341                         rc = slap_str2ad( arg, &member_ad, &text );
1342                         if ( rc != LDAP_SUCCESS ) {
1343                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1344                                         DYNLIST_USAGE
1345                                         "unable to find AttributeDescription #%d \"%s\"\n",
1346                                         i - 3, c->argv[ i ] );
1347                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1348                                         c->log, c->cr_msg, 0 );
1349                                 return 1;
1350                         }
1351
1352                         dlmp = (dynlist_map_t *)ch_calloc( 1, sizeof( dynlist_map_t ) );
1353                         if ( dlm == NULL ) {
1354                                 dlm = dlmp;
1355                                 dlml = NULL;
1356                         }
1357                         dlmp->dlm_member_ad = member_ad;
1358                         dlmp->dlm_mapped_ad = mapped_ad;
1359                         dlmp->dlm_next = NULL;
1360                 
1361                         if ( dlml != NULL ) 
1362                                 dlml->dlm_next = dlmp;
1363                         dlml = dlmp;
1364                 }
1365
1366                 if ( c->valx > 0 ) {
1367                         int     i;
1368
1369                         for ( i = 0, dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1370                                 i < c->valx; i++ )
1371                         {
1372                                 if ( *dlip == NULL ) {
1373                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1374                                                 DYNLIST_USAGE
1375                                                 "invalid index {%d}\n",
1376                                                 c->valx );
1377                                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1378                                                 c->log, c->cr_msg, 0 );
1379                                         return 1;
1380                                 }
1381                                 dlip = &(*dlip)->dli_next;
1382                         }
1383                         dli_next = *dlip;
1384
1385                 } else {
1386                         for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1387                                 *dlip; dlip = &(*dlip)->dli_next )
1388                                 /* goto last */;
1389                 }
1390
1391                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1392
1393                 (*dlip)->dli_oc = oc;
1394                 (*dlip)->dli_ad = ad;
1395                 (*dlip)->dli_dlm = dlm;
1396                 (*dlip)->dli_next = dli_next;
1397
1398                 rc = dynlist_build_def_filter( *dlip );
1399
1400                 } break;
1401
1402         case DL_ATTRPAIR_COMPAT:
1403                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1404                         "warning: \"attrpair\" only supported for limited "
1405                         "backward compatibility with overlay \"dyngroup\"" );
1406                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
1407                 /* fallthru */
1408
1409         case DL_ATTRPAIR: {
1410                 dynlist_info_t          **dlip;
1411                 ObjectClass             *oc = NULL;
1412                 AttributeDescription    *ad = NULL,
1413                                         *member_ad = NULL;
1414                 const char              *text;
1415
1416                 oc = oc_find( "groupOfURLs" );
1417                 if ( oc == NULL ) {
1418                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1419                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1420                                 "unable to find default ObjectClass \"groupOfURLs\"" );
1421                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1422                                 c->log, c->cr_msg, 0 );
1423                         return 1;
1424                 }
1425
1426                 rc = slap_str2ad( c->argv[ 1 ], &member_ad, &text );
1427                 if ( rc != LDAP_SUCCESS ) {
1428                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1429                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1430                                 "unable to find AttributeDescription \"%s\"",
1431                                 c->argv[ 1 ] );
1432                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1433                                 c->log, c->cr_msg, 0 );
1434                         return 1;
1435                 }
1436
1437                 rc = slap_str2ad( c->argv[ 2 ], &ad, &text );
1438                 if ( rc != LDAP_SUCCESS ) {
1439                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1440                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1441                                 "unable to find AttributeDescription \"%s\"\n",
1442                                 c->argv[ 2 ] );
1443                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1444                                 c->log, c->cr_msg, 0 );
1445                         return 1;
1446                 }
1447
1448                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1449                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1450                                 DYNLIST_USAGE
1451                                 "AttributeDescription \"%s\" "
1452                                 "must be a subtype of \"labeledURI\"",
1453                                 c->argv[ 2 ] );
1454                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1455                                 c->log, c->cr_msg, 0 );
1456                         return 1;
1457                 }
1458
1459                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1460                         *dlip; dlip = &(*dlip)->dli_next )
1461                 {
1462                         /* 
1463                          * The same URL attribute / member attribute pair
1464                          * cannot be repeated, but we enforce this only 
1465                          * when the member attribute is unique. Performing
1466                          * the check for multiple values would require
1467                          * sorting and comparing the lists, which is left
1468                          * as a future improvement
1469                          */
1470                         if ( (*dlip)->dli_ad == ad &&
1471                              (*dlip)->dli_dlm->dlm_next == NULL &&
1472                              member_ad == (*dlip)->dli_dlm->dlm_member_ad ) {
1473                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1474                                         "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1475                                         "URL attributeDescription \"%s\" already mapped.\n",
1476                                         ad->ad_cname.bv_val );
1477                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1478                                         c->log, c->cr_msg, 0 );
1479 #if 0
1480                                 /* make it a warning... */
1481                                 return 1;
1482 #endif
1483                         }
1484                 }
1485
1486                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1487
1488                 (*dlip)->dli_oc = oc;
1489                 (*dlip)->dli_ad = ad;
1490                 (*dlip)->dli_dlm = (dynlist_map_t *)ch_calloc( 1, sizeof( dynlist_map_t ) );
1491                 (*dlip)->dli_dlm->dlm_member_ad = member_ad;
1492                 (*dlip)->dli_dlm->dlm_mapped_ad = NULL;
1493
1494                 rc = dynlist_build_def_filter( *dlip );
1495
1496                 } break;
1497
1498         default:
1499                 rc = 1;
1500                 break;
1501         }
1502
1503         return rc;
1504 }
1505 #endif
1506
1507 static int
1508 dynlist_db_open(
1509         BackendDB       *be,
1510         ConfigReply     *cr )
1511 {
1512         slap_overinst           *on = (slap_overinst *) be->bd_info;
1513         dynlist_info_t          *dli = (dynlist_info_t *)on->on_bi.bi_private;
1514         ObjectClass             *oc = NULL;
1515         AttributeDescription    *ad = NULL;
1516         const char      *text;
1517         int rc;
1518
1519         if ( dli == NULL ) {
1520                 dli = ch_calloc( 1, sizeof( dynlist_info_t ) );
1521                 on->on_bi.bi_private = (void *)dli;
1522         }
1523
1524         for ( ; dli; dli = dli->dli_next ) {
1525                 if ( dli->dli_oc == NULL ) {
1526                         if ( oc == NULL ) {
1527                                 oc = oc_find( "groupOfURLs" );
1528                                 if ( oc == NULL ) {
1529                                         snprintf( cr->msg, sizeof( cr->msg),
1530                                                 "unable to fetch objectClass \"groupOfURLs\"" );
1531                                         Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s.\n", cr->msg, 0, 0 );
1532                                         return 1;
1533                                 }
1534                         }
1535
1536                         dli->dli_oc = oc;
1537                 }
1538
1539                 if ( dli->dli_ad == NULL ) {
1540                         if ( ad == NULL ) {
1541                                 rc = slap_str2ad( "memberURL", &ad, &text );
1542                                 if ( rc != LDAP_SUCCESS ) {
1543                                         snprintf( cr->msg, sizeof( cr->msg),
1544                                                 "unable to fetch attributeDescription \"memberURL\": %d (%s)",
1545                                                 rc, text );
1546                                         Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s.\n", cr->msg, 0, 0 );
1547                                         return 1;
1548                                 }
1549                         }
1550                 
1551                         dli->dli_ad = ad;                       
1552                 }
1553
1554                 if ( BER_BVISNULL( &dli->dli_default_filter ) ) {
1555                         rc = dynlist_build_def_filter( dli );
1556                         if ( rc != 0 ) {
1557                                 return rc;
1558                         }
1559                 }
1560         }
1561
1562         if ( ad_dgIdentity == NULL ) {
1563                 rc = slap_str2ad( "dgIdentity", &ad_dgIdentity, &text );
1564                 if ( rc != LDAP_SUCCESS ) {
1565                         snprintf( cr->msg, sizeof( cr->msg),
1566                                 "unable to fetch attributeDescription \"dgIdentity\": %d (%s)",
1567                                 rc, text );
1568                         Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s\n", cr->msg, 0, 0 );
1569                         /* Just a warning */
1570                 }
1571         }
1572
1573         if ( ad_dgAuthz == NULL ) {
1574                 rc = slap_str2ad( "dgAuthz", &ad_dgAuthz, &text );
1575                 if ( rc != LDAP_SUCCESS ) {
1576                         snprintf( cr->msg, sizeof( cr->msg),
1577                                 "unable to fetch attributeDescription \"dgAuthz\": %d (%s)",
1578                                 rc, text );
1579                         Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s\n", cr->msg, 0, 0 );
1580                         /* Just a warning */
1581                 }
1582         }
1583
1584         return 0;
1585 }
1586
1587 static int
1588 dynlist_db_destroy(
1589         BackendDB       *be,
1590         ConfigReply     *cr )
1591 {
1592         slap_overinst   *on = (slap_overinst *) be->bd_info;
1593
1594         if ( on->on_bi.bi_private ) {
1595                 dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private,
1596                                 *dli_next;
1597
1598                 for ( dli_next = dli; dli_next; dli = dli_next ) {
1599                         dynlist_map_t *dlm;
1600                         dynlist_map_t *dlm_next;
1601
1602                         dli_next = dli->dli_next;
1603
1604                         ch_free( dli->dli_default_filter.bv_val );
1605                         dlm = dli->dli_dlm;
1606                         while ( dlm != NULL ) {
1607                                 dlm_next = dlm->dlm_next;
1608                                 ch_free( dlm );
1609                                 dlm = dlm_next;
1610                         }
1611                         ch_free( dli );
1612                 }
1613         }
1614
1615         return 0;
1616 }
1617
1618 static slap_overinst    dynlist = { { NULL } };
1619 #ifdef TAKEOVER_DYNGROUP
1620 static char             *obsolete_names[] = {
1621         "dyngroup",
1622         NULL
1623 };
1624 #endif
1625
1626 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
1627 static
1628 #endif /* SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC */
1629 int
1630 dynlist_initialize(void)
1631 {
1632 #ifndef OL_2_2_COMPAT
1633         int     rc = 0;
1634 #endif
1635
1636         dynlist.on_bi.bi_type = "dynlist";
1637
1638 #ifdef TAKEOVER_DYNGROUP
1639         /* makes dynlist incompatible with dyngroup */
1640         dynlist.on_bi.bi_obsolete_names = obsolete_names;
1641 #endif
1642
1643 #ifdef OL_2_2_COMPAT
1644         dynlist.on_bi.bi_db_config = dynlist_db_config;
1645 #else
1646         dynlist.on_bi.bi_db_config = config_generic_wrapper;
1647 #endif
1648         dynlist.on_bi.bi_db_open = dynlist_db_open;
1649         dynlist.on_bi.bi_db_destroy = dynlist_db_destroy;
1650
1651         dynlist.on_response = dynlist_response;
1652
1653 #ifndef OL_2_2_COMPAT
1654         dynlist.on_bi.bi_cf_ocs = dlocs;
1655
1656         rc = config_register_schema( dlcfg, dlocs );
1657         if ( rc ) {
1658                 return rc;
1659         }
1660 #endif
1661
1662         return overlay_register( &dynlist );
1663 }
1664
1665 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
1666 int
1667 init_module( int argc, char *argv[] )
1668 {
1669         return dynlist_initialize();
1670 }
1671 #endif
1672
1673 #endif /* SLAPD_OVER_DYNLIST */