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