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