]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/dynlist.c
965b6b553540219219b87a89f2cf917b8e16aa5c
[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, *ad_dgAuthz;
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 ( ad_dgIdentity && ( id = attrs_find( rs->sr_entry->e_attrs, ad_dgIdentity ))) {
351                 Attribute *authz = NULL;
352
353                 /* if not rootdn and dgAuthz is present,
354                  * check if user can be authorized as dgIdentity */
355                 if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
356                         && ( authz = attrs_find( rs->sr_entry->e_attrs, ad_dgAuthz ) ) )
357                 {
358                         if ( slap_sasl_matches( op, authz->a_nvals,
359                                 &o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
360                         {
361                                 return SLAP_CB_CONTINUE;
362                         }
363                 }
364
365                 o.o_dn = id->a_vals[0];
366                 o.o_ndn = id->a_nvals[0];
367                 o.o_groups = NULL;
368         }
369
370         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
371                 e = entry_dup( rs->sr_entry );
372         } else {
373                 e = rs->sr_entry;
374         }
375         e_flags = rs->sr_flags | ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
376
377         dlc.dlc_e = e;
378         dlc.dlc_dli = dli;
379         cb.sc_private = &dlc;
380         cb.sc_response = dynlist_sc_update;
381         cb.sc_cleanup = NULL;
382         cb.sc_next = NULL;
383
384         o.o_callback = &cb;
385         o.ors_deref = LDAP_DEREF_NEVER;
386         o.ors_limit = NULL;
387         o.ors_tlimit = SLAP_NO_LIMIT;
388         o.ors_slimit = SLAP_NO_LIMIT;
389
390         for ( url = a->a_nvals; !BER_BVISNULL( url ); url++ ) {
391                 LDAPURLDesc     *lud = NULL;
392                 int             i, j;
393                 struct berval   dn;
394                 int             rc;
395
396                 BER_BVZERO( &o.o_req_dn );
397                 BER_BVZERO( &o.o_req_ndn );
398                 o.ors_filter = NULL;
399                 o.ors_attrs = NULL;
400                 BER_BVZERO( &o.ors_filterstr );
401
402                 if ( ldap_url_parse( url->bv_val, &lud ) != LDAP_URL_SUCCESS ) {
403                         /* FIXME: error? */
404                         continue;
405                 }
406
407                 if ( lud->lud_host != NULL ) {
408                         /* FIXME: host not allowed; reject as illegal? */
409                         Debug( LDAP_DEBUG_ANY, "dynlist_prepare_entry(\"%s\"): "
410                                 "illegal URI \"%s\"\n",
411                                 e->e_name.bv_val, url->bv_val, 0 );
412                         goto cleanup;
413                 }
414
415                 if ( lud->lud_dn == NULL ) {
416                         /* note that an empty base is not honored in terms
417                          * of defaultSearchBase, because select_backend()
418                          * is not aware of the defaultSearchBase option;
419                          * this can be useful in case of a database serving
420                          * the empty suffix */
421                         BER_BVSTR( &dn, "" );
422
423                 } else {
424                         ber_str2bv( lud->lud_dn, 0, 0, &dn );
425                 }
426                 rc = dnPrettyNormal( NULL, &dn, &o.o_req_dn, &o.o_req_ndn, op->o_tmpmemctx );
427                 if ( rc != LDAP_SUCCESS ) {
428                         /* FIXME: error? */
429                         goto cleanup;
430                 }
431                 o.ors_scope = lud->lud_scope;
432
433                 if ( dli->dli_member_ad != NULL ) {
434                         /* if ( lud->lud_attrs != NULL ),
435                          * the URL should be ignored */
436                         o.ors_attrs = slap_anlist_no_attrs;
437
438                 } else if ( lud->lud_attrs == NULL ) {
439                         o.ors_attrs = rs->sr_attrs;
440
441                 } else {
442                         for ( i = 0; lud->lud_attrs[i]; i++)
443                                 /* just count */ ;
444
445                         o.ors_attrs = op->o_tmpcalloc( i + 1, sizeof( AttributeName ), op->o_tmpmemctx );
446                         for ( i = 0, j = 0; lud->lud_attrs[i]; i++) {
447                                 const char      *text = NULL;
448         
449                                 ber_str2bv( lud->lud_attrs[i], 0, 0, &o.ors_attrs[j].an_name );
450                                 o.ors_attrs[j].an_desc = NULL;
451                                 (void)slap_bv2ad( &o.ors_attrs[j].an_name, &o.ors_attrs[j].an_desc, &text );
452                                 /* FIXME: ignore errors... */
453
454                                 if ( rs->sr_attrs == NULL ) {
455                                         if ( o.ors_attrs[j].an_desc != NULL &&
456                                                         is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
457                                         {
458                                                 continue;
459                                         }
460
461                                 } else {
462                                         if ( o.ors_attrs[j].an_desc != NULL &&
463                                                         is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
464                                         {
465                                                 if ( !opattrs && !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
466                                                 {
467                                                         continue;
468                                                 }
469
470                                         } else {
471                                                 if ( !userattrs && 
472                                                                 o.ors_attrs[j].an_desc != NULL &&
473                                                                 !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
474                                                 {
475                                                         continue;
476                                                 }
477                                         }
478                                 }
479
480                                 j++;
481                         }
482
483                         if ( j == 0 ) {
484                                 goto cleanup;
485                         }
486                 
487                         BER_BVZERO( &o.ors_attrs[j].an_name );
488                 }
489
490                 if ( lud->lud_filter == NULL ) {
491                         ber_dupbv_x( &o.ors_filterstr,
492                                         &dli->dli_default_filter, op->o_tmpmemctx );
493
494                 } else {
495                         struct berval   flt;
496                         ber_str2bv( lud->lud_filter, 0, 0, &flt );
497                         if ( dynlist_make_filter( op, &flt, &o.ors_filterstr ) ) {
498                                 /* error */
499                                 goto cleanup;
500                         }
501                 }
502                 o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
503                 if ( o.ors_filter == NULL ) {
504                         goto cleanup;
505                 }
506                 
507                 o.o_bd = select_backend( &o.o_req_ndn, 1 );
508                 if ( o.o_bd && o.o_bd->be_search ) {
509 #ifdef SLAP_OPATTRS
510                         r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
511 #endif /* SLAP_OPATTRS */
512                         (void)o.o_bd->be_search( &o, &r );
513                 }
514
515 cleanup:;
516                 if ( id ) {
517                         slap_op_groups_free( &o );
518                 }
519                 if ( o.ors_filter ) {
520                         filter_free_x( &o, o.ors_filter );
521                 }
522                 if ( o.ors_attrs && o.ors_attrs != rs->sr_attrs
523                                 && o.ors_attrs != slap_anlist_no_attrs )
524                 {
525                         op->o_tmpfree( o.ors_attrs, op->o_tmpmemctx );
526                 }
527                 if ( !BER_BVISNULL( &o.o_req_dn ) ) {
528                         op->o_tmpfree( o.o_req_dn.bv_val, op->o_tmpmemctx );
529                 }
530                 if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
531                         op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
532                 }
533                 assert( BER_BVISNULL( &o.ors_filterstr )
534                         || o.ors_filterstr.bv_val != lud->lud_filter );
535                 op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
536                 ldap_free_urldesc( lud );
537         }
538
539         rs->sr_entry = e;
540         rs->sr_flags = e_flags;
541
542         return SLAP_CB_CONTINUE;
543 }
544
545 static int
546 dynlist_sc_save_entry( Operation *op, SlapReply *rs )
547 {
548         /* save the entry in the private field of the callback,
549          * so it doesn't get freed (it's temporary!) */
550         if ( rs->sr_entry != NULL ) {
551                 dynlist_sc_t    *dlc = (dynlist_sc_t *)op->o_callback->sc_private;
552                 dlc->dlc_e = rs->sr_entry;
553                 rs->sr_entry = NULL;
554         }
555
556         return 0;
557 }
558
559 static int
560 dynlist_compare( Operation *op, SlapReply *rs )
561 {
562         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
563         dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private;
564         Operation o = *op;
565         Entry *e = NULL;
566
567         for ( ; dli != NULL; dli = dli->dli_next ) {
568                 if ( op->oq_compare.rs_ava->aa_desc == dli->dli_member_ad ) {
569                         /* This compare is for one of the attributes we're
570                          * interested in. We'll use slapd's existing dyngroup
571                          * evaluator to get the answer we want.
572                          */
573                         BerVarray id = NULL, authz = NULL;
574
575                         o.o_do_not_cache = 1;
576
577                         if ( ad_dgIdentity && backend_attribute( &o, NULL, &o.o_req_ndn,
578                                 ad_dgIdentity, &id, ACL_READ ) == LDAP_SUCCESS )
579                         {
580                                 /* if not rootdn and dgAuthz is present,
581                                  * check if user can be authorized as dgIdentity */
582                                 if ( ad_dgAuthz && !BER_BVISEMPTY( id ) && !be_isroot( op )
583                                         && backend_attribute( &o, NULL, &o.o_req_ndn,
584                                                 ad_dgAuthz, &authz, ACL_READ ) == LDAP_SUCCESS )
585                                 {
586                                         
587                                         rs->sr_err = slap_sasl_matches( op, authz,
588                                                 &o.o_ndn, &o.o_ndn );
589                                         ber_bvarray_free_x( authz, op->o_tmpmemctx );
590                                         if ( rs->sr_err != LDAP_SUCCESS ) {
591                                                 goto done;
592                                         }
593                                 }
594
595                                 o.o_dn = *id;
596                                 o.o_ndn = *id;
597                                 o.o_groups = NULL; /* authz changed, invalidate cached groups */
598                         }
599
600                         rs->sr_err = backend_group( &o, NULL, &o.o_req_ndn,
601                                 &o.oq_compare.rs_ava->aa_value, dli->dli_oc, dli->dli_ad );
602                         switch ( rs->sr_err ) {
603                         case LDAP_SUCCESS:
604                                 rs->sr_err = LDAP_COMPARE_TRUE;
605                                 break;
606
607                         case LDAP_NO_SUCH_OBJECT:
608                                 /* NOTE: backend_group() returns noSuchObject
609                                  * if op_ndn does not exist; however, since
610                                  * dynamic list expansion means that the
611                                  * member attribute is virtually present, the
612                                  * non-existence of the asserted value implies
613                                  * the assertion is FALSE rather than
614                                  * UNDEFINED */
615                                 rs->sr_err = LDAP_COMPARE_FALSE;
616                                 break;
617                         }
618
619 done:;
620                         if ( id ) ber_bvarray_free_x( id, o.o_tmpmemctx );
621
622                         return SLAP_CB_CONTINUE;
623                 }
624         }
625
626         if ( overlay_entry_get_ov( &o, &o.o_req_ndn, NULL, NULL, 0, &e, on ) !=
627                 LDAP_SUCCESS || e == NULL )
628         {
629                 return SLAP_CB_CONTINUE;
630         }
631
632         if ( ad_dgIdentity ) {
633                 Attribute *id = attrs_find( e->e_attrs, ad_dgIdentity );
634                 if ( id ) {
635                         Attribute *authz;
636
637                         /* if not rootdn and dgAuthz is present,
638                          * check if user can be authorized as dgIdentity */
639                         if ( ad_dgAuthz && !BER_BVISEMPTY( &id->a_nvals[0] ) && !be_isroot( op )
640                                 && ( authz = attrs_find( e->e_attrs, ad_dgAuthz ) ) )
641                         {
642                                 if ( slap_sasl_matches( op, authz->a_nvals,
643                                         &o.o_ndn, &o.o_ndn ) != LDAP_SUCCESS )
644                                 {
645                                         goto release;
646                                 }
647                         }
648
649                         o.o_dn = id->a_vals[0];
650                         o.o_ndn = id->a_nvals[0];
651                         o.o_groups = NULL;
652                 }
653         }
654
655         dli = (dynlist_info_t *)on->on_bi.bi_private;
656         for ( ; dli != NULL && rs->sr_err != LDAP_COMPARE_TRUE; dli = dli->dli_next ) {
657                 Attribute       *a;
658                 slap_callback   cb;
659                 SlapReply       r = { REP_SEARCH };
660                 AttributeName   an[2];
661                 int             rc;
662                 dynlist_sc_t    dlc = { 0 };
663
664                 if ( !is_entry_objectclass_or_sub( e, dli->dli_oc ))
665                         continue;
666
667                 /* if the entry has the right objectClass, generate
668                  * the dynamic list and compare */
669                 dlc.dlc_dli = dli;
670                 cb.sc_private = &dlc;
671                 cb.sc_response = dynlist_sc_save_entry;
672                 cb.sc_cleanup = NULL;
673                 cb.sc_next = NULL;
674                 o.o_callback = &cb;
675
676                 o.o_tag = LDAP_REQ_SEARCH;
677                 o.ors_limit = NULL;
678                 o.ors_tlimit = SLAP_NO_LIMIT;
679                 o.ors_slimit = SLAP_NO_LIMIT;
680
681                 o.o_bd = select_backend( &o.o_req_ndn, 1 );
682                 if ( !o.o_bd || !o.o_bd->be_search ) {
683                         goto release;
684                 }
685
686                 o.ors_filterstr = *slap_filterstr_objectClass_pres;
687                 o.ors_filter = (Filter *) slap_filter_objectClass_pres;
688
689                 o.ors_scope = LDAP_SCOPE_BASE;
690                 o.ors_deref = LDAP_DEREF_NEVER;
691                 an[0].an_name = op->orc_ava->aa_desc->ad_cname;
692                 an[0].an_desc = op->orc_ava->aa_desc;
693                 BER_BVZERO( &an[1].an_name );
694                 o.ors_attrs = an;
695                 o.ors_attrsonly = 0;
696
697                 o.o_acl_priv = ACL_COMPARE;
698
699                 rc = o.o_bd->be_search( &o, &r );
700
701                 if ( o.o_dn.bv_val != op->o_dn.bv_val ) {
702                         slap_op_groups_free( &o );
703                 }
704
705                 if ( rc != 0 ) {
706                         goto release;
707                 }
708
709                 if ( dlc.dlc_e != NULL ) {
710                         r.sr_entry = dlc.dlc_e;
711                 }
712
713                 if ( r.sr_err != LDAP_SUCCESS || r.sr_entry == NULL ) {
714                         /* error? */
715                         goto release;
716                 }
717
718                 for ( a = attrs_find( r.sr_entry->e_attrs, op->orc_ava->aa_desc );
719                         a != NULL;
720                         a = attrs_find( a->a_next, op->orc_ava->aa_desc ) )
721                 {
722                         /* if we're here, we got a match... */
723                         rs->sr_err = LDAP_COMPARE_FALSE;
724
725                         if ( value_find_ex( op->orc_ava->aa_desc,
726                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
727                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
728                                 a->a_nvals, &op->orc_ava->aa_value, op->o_tmpmemctx ) == 0 )
729                         {
730                                 rs->sr_err = LDAP_COMPARE_TRUE;
731                                 break;
732                         }
733                 }
734
735                 if ( r.sr_flags & REP_ENTRY_MUSTBEFREED ) {
736                         entry_free( r.sr_entry );
737                 }
738         }
739
740 release:;
741         if ( e != NULL ) {
742                 overlay_entry_release_ov( &o, e, 0, on );
743         }
744
745         return SLAP_CB_CONTINUE;
746 }
747
748 static int
749 dynlist_response( Operation *op, SlapReply *rs )
750 {
751         dynlist_info_t  *dli;
752
753         switch ( op->o_tag ) {
754         case LDAP_REQ_SEARCH:
755                 if ( rs->sr_type == REP_SEARCH && !get_manageDSAit( op ) )
756                 {
757                         int     rc = LDAP_OTHER;
758
759                         for ( dli = dynlist_is_dynlist_next( op, rs, NULL );
760                                 dli;
761                                 dli = dynlist_is_dynlist_next( op, rs, dli ) )
762                         {
763                                 rc = dynlist_prepare_entry( op, rs, dli );
764                         }
765
766                         if ( rc != LDAP_OTHER ) {
767                                 return rc;
768                         }
769                 }
770                 break;
771
772         case LDAP_REQ_COMPARE:
773                 switch ( rs->sr_err ) {
774                 /* NOTE: we waste a few cycles running the dynamic list
775                  * also when the result is FALSE, which occurs if the
776                  * dynamic entry itself contains the AVA attribute  */
777                 /* FIXME: this approach is less than optimal; a dedicated
778                  * compare op should be implemented, that fetches the
779                  * entry, checks if it has the appropriate objectClass
780                  * and, in case, runs a compare thru all the URIs,
781                  * stopping at the first positive occurrence; see ITS#3756 */
782                 case LDAP_COMPARE_FALSE:
783                 case LDAP_NO_SUCH_ATTRIBUTE:
784                         return dynlist_compare( op, rs );
785                 }
786                 break;
787
788         default:
789                 break;
790         }
791
792         return SLAP_CB_CONTINUE;
793 }
794
795 static int
796 dynlist_build_def_filter( dynlist_info_t *dli )
797 {
798         char    *ptr;
799
800         dli->dli_default_filter.bv_len = STRLENOF( "(!(objectClass=" "))" )
801                 + dli->dli_oc->soc_cname.bv_len;
802         dli->dli_default_filter.bv_val = ch_malloc( dli->dli_default_filter.bv_len + 1 );
803         if ( dli->dli_default_filter.bv_val == NULL ) {
804                 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: malloc failed.\n",
805                         0, 0, 0 );
806                 return -1;
807         }
808
809         ptr = lutil_strcopy( dli->dli_default_filter.bv_val, "(!(objectClass=" );
810         ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
811         ptr = lutil_strcopy( ptr, "))" );
812
813         assert( dli->dli_default_filter.bv_len == ptr - dli->dli_default_filter.bv_val );
814
815         return 0;
816 }
817
818 #ifdef OL_2_2_COMPAT
819 static int
820 dynlist_db_config(
821         BackendDB       *be,
822         const char      *fname,
823         int             lineno,
824         int             argc,
825         char            **argv )
826 {
827         slap_overinst   *on = (slap_overinst *)be->bd_info;
828
829         int             rc = 0;
830
831         if ( strcasecmp( argv[0], "dynlist-attrset" ) == 0 ) {
832                 dynlist_info_t          **dlip;
833                 ObjectClass             *oc;
834                 AttributeDescription    *ad = NULL,
835                                         *member_ad = NULL;
836                 const char              *text;
837
838                 if ( argc < 3 || argc > 4 ) {
839                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
840                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
841                                 "invalid arg number #%d.\n",
842                                 fname, lineno, argc );
843                         return 1;
844                 }
845
846                 oc = oc_find( argv[1] );
847                 if ( oc == NULL ) {
848                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
849                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
850                                 "unable to find ObjectClass \"%s\"\n",
851                                 fname, lineno, argv[ 1 ] );
852                         return 1;
853                 }
854
855                 rc = slap_str2ad( argv[2], &ad, &text );
856                 if ( rc != LDAP_SUCCESS ) {
857                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
858                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
859                                 "unable to find AttributeDescription \"%s\"\n",
860                                 fname, lineno, argv[2] );
861                         return 1;
862                 }
863
864                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
865                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
866                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
867                                 "AttributeDescription \"%s\" "
868                                 "must be a subtype of \"labeledURI\"\n",
869                                 fname, lineno, argv[2] );
870                         return 1;
871                 }
872
873                 if ( argc == 4 ) {
874                         rc = slap_str2ad( argv[3], &member_ad, &text );
875                         if ( rc != LDAP_SUCCESS ) {
876                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
877                                         "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
878                                         "unable to find AttributeDescription \"%s\"\n",
879                                         fname, lineno, argv[3] );
880                                 return 1;
881                         }
882                 }
883
884                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
885                         *dlip; dlip = &(*dlip)->dli_next )
886                 {
887                         /* The same URL attribute / member attribute pair
888                          * cannot be repeated */
889                         if ( (*dlip)->dli_ad == ad && (*dlip)->dli_member_ad == member_ad ) {
890                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
891                                         "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
892                                         "URL attributeDescription \"%s\" already mapped.\n",
893                                         fname, lineno, ad->ad_cname.bv_val );
894 #if 0
895                                 /* make it a warning... */
896                                 return 1;
897 #endif
898                         }
899                 }
900
901                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
902                 (*dlip)->dli_oc = oc;
903                 (*dlip)->dli_ad = ad;
904                 (*dlip)->dli_member_ad = member_ad;
905
906                 if ( dynlist_build_def_filter( *dlip ) ) {
907                         ch_free( *dlip );
908                         *dlip = NULL;
909                         return 1;
910                 }
911
912         /* allow dyngroup syntax */
913         } else if ( strcasecmp( argv[0], "dynlist-attrpair" ) == 0 ) {
914                 dynlist_info_t          **dlip;
915                 ObjectClass             *oc;
916                 AttributeDescription    *ad = NULL,
917                                         *member_ad = NULL;
918                 const char              *text;
919
920                 if ( argc != 3 ) {
921                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
922                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
923                                 "invalid arg number #%d.\n",
924                                 fname, lineno, argc );
925                         return 1;
926                 }
927
928                 oc = oc_find( "groupOfURLs" );
929                 if ( oc == NULL ) {
930                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
931                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
932                                 "unable to find default ObjectClass \"groupOfURLs\"\n",
933                                 fname, lineno, 0 );
934                         return 1;
935                 }
936
937                 rc = slap_str2ad( argv[1], &member_ad, &text );
938                 if ( rc != LDAP_SUCCESS ) {
939                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
940                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
941                                 "unable to find AttributeDescription \"%s\"\n",
942                                 fname, lineno, argv[1] );
943                         return 1;
944                 }
945
946                 rc = slap_str2ad( argv[2], &ad, &text );
947                 if ( rc != LDAP_SUCCESS ) {
948                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
949                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
950                                 "unable to find AttributeDescription \"%s\"\n",
951                                 fname, lineno, argv[2] );
952                         return 1;
953                 }
954
955                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
956                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
957                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
958                                 "AttributeDescription \"%s\" "
959                                 "must be a subtype of \"labeledURI\"\n",
960                                 fname, lineno, argv[2] );
961                         return 1;
962                 }
963
964                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
965                         *dlip; dlip = &(*dlip)->dli_next )
966                 {
967                         /* The same URL attribute / member attribute pair
968                          * cannot be repeated */
969                         if ( (*dlip)->dli_ad == ad && (*dlip)->dli_member_ad == member_ad ) {
970                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
971                                         "\"dynlist-attrpair <member-ad> <URL-ad>\": "
972                                         "URL attributeDescription \"%s\" already mapped.\n",
973                                         fname, lineno, ad->ad_cname.bv_val );
974 #if 0
975                                 /* make it a warning... */
976                                 return 1;
977 #endif
978                         }
979                 }
980
981                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
982                 (*dlip)->dli_oc = oc;
983                 (*dlip)->dli_ad = ad;
984                 (*dlip)->dli_member_ad = member_ad;
985
986                 if ( dynlist_build_def_filter( *dlip ) ) {
987                         ch_free( *dlip );
988                         *dlip = NULL;
989                         return 1;
990                 }
991
992         } else {
993                 rc = SLAP_CONF_UNKNOWN;
994         }
995
996         return rc;
997 }
998
999 #else
1000 enum {
1001         DL_ATTRSET = 1,
1002         DL_ATTRPAIR,
1003         DL_ATTRPAIR_COMPAT,
1004         DL_LAST
1005 };
1006
1007 static ConfigDriver     dl_cfgen;
1008
1009 static ConfigTable dlcfg[] = {
1010         { "dynlist-attrset", "group-oc> <URL-ad> <member-ad",
1011                 3, 4, 0, ARG_MAGIC|DL_ATTRSET, dl_cfgen,
1012                 "( OLcfgOvAt:8.1 NAME 'olcDLattrSet' "
1013                         "DESC 'Dynamic list: <group objectClass>, <URL attributeDescription>, <member attributeDescription>' "
1014                         "EQUALITY caseIgnoreMatch "
1015                         "SYNTAX OMsDirectoryString "
1016                         "X-ORDERED 'VALUES' )",
1017                         NULL, NULL },
1018         { "dynlist-attrpair", "member-ad> <URL-ad",
1019                 3, 3, 0, ARG_MAGIC|DL_ATTRPAIR, dl_cfgen,
1020                         NULL, NULL, NULL },
1021 #ifdef TAKEOVER_DYNGROUP
1022         { "attrpair", "member-ad> <URL-ad",
1023                 3, 3, 0, ARG_MAGIC|DL_ATTRPAIR_COMPAT, dl_cfgen,
1024                         NULL, NULL, NULL },
1025 #endif
1026         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1027 };
1028
1029 static ConfigOCs dlocs[] = {
1030         { "( OLcfgOvOc:8.1 "
1031                 "NAME 'olcDynamicList' "
1032                 "DESC 'Dynamic list configuration' "
1033                 "SUP olcOverlayConfig "
1034                 "MAY olcDLattrSet )",
1035                 Cft_Overlay, dlcfg, NULL, NULL },
1036         { NULL, 0, NULL }
1037 };
1038
1039 static int
1040 dl_cfgen( ConfigArgs *c )
1041 {
1042         slap_overinst   *on = (slap_overinst *)c->bi;
1043         dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private;
1044
1045         int             rc = 0, i;
1046
1047         if ( c->op == SLAP_CONFIG_EMIT ) {
1048                 switch( c->type ) {
1049                 case DL_ATTRSET:
1050                         for ( i = 0; dli; i++, dli = dli->dli_next ) {
1051                                 struct berval   bv;
1052                                 char            *ptr = c->cr_msg;
1053
1054                                 assert( dli->dli_oc != NULL );
1055                                 assert( dli->dli_ad != NULL );
1056
1057                                 ptr += snprintf( c->cr_msg, sizeof( c->cr_msg ),
1058                                         SLAP_X_ORDERED_FMT "%s %s", i,
1059                                         dli->dli_oc->soc_cname.bv_val,
1060                                         dli->dli_ad->ad_cname.bv_val );
1061
1062                                 if ( dli->dli_member_ad != NULL ) {
1063                                         ptr[ 0 ] = ' ';
1064                                         ptr++;
1065                                         ptr = lutil_strcopy( ptr, dli->dli_member_ad->ad_cname.bv_val );
1066                                 }
1067
1068                                 bv.bv_val = c->cr_msg;
1069                                 bv.bv_len = ptr - bv.bv_val;
1070                                 value_add_one( &c->rvalue_vals, &bv );
1071                         }
1072                         break;
1073
1074                 case DL_ATTRPAIR_COMPAT:
1075                 case DL_ATTRPAIR:
1076                         rc = 1;
1077                         break;
1078
1079                 default:
1080                         rc = 1;
1081                         break;
1082                 }
1083
1084                 return rc;
1085
1086         } else if ( c->op == LDAP_MOD_DELETE ) {
1087                 switch( c->type ) {
1088                 case DL_ATTRSET:
1089                         if ( c->valx < 0 ) {
1090                                 dynlist_info_t  *dli_next;
1091
1092                                 for ( dli_next = dli; dli_next; dli = dli_next ) {
1093                                         dli_next = dli->dli_next;
1094
1095                                         ch_free( dli->dli_default_filter.bv_val );
1096                                         ch_free( dli );
1097                                 }
1098
1099                                 on->on_bi.bi_private = NULL;
1100
1101                         } else {
1102                                 dynlist_info_t  **dlip;
1103
1104                                 for ( i = 0, dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1105                                         i < c->valx; i++ )
1106                                 {
1107                                         if ( *dlip == NULL ) {
1108                                                 return 1;
1109                                         }
1110                                         dlip = &(*dlip)->dli_next;
1111                                 }
1112
1113                                 dli = *dlip;
1114                                 *dlip = dli->dli_next;
1115                                 ch_free( dli->dli_default_filter.bv_val );
1116                                 ch_free( dli );
1117
1118                                 dli = (dynlist_info_t *)on->on_bi.bi_private;
1119                         }
1120                         break;
1121
1122                 case DL_ATTRPAIR_COMPAT:
1123                 case DL_ATTRPAIR:
1124                         rc = 1;
1125                         break;
1126
1127                 default:
1128                         rc = 1;
1129                         break;
1130                 }
1131
1132                 return rc;
1133         }
1134
1135         switch( c->type ) {
1136         case DL_ATTRSET: {
1137                 dynlist_info_t          **dlip,
1138                                         *dli_next = NULL;
1139                 ObjectClass             *oc = NULL;
1140                 AttributeDescription    *ad = NULL,
1141                                         *member_ad = NULL;
1142                 const char              *text;
1143
1144                 oc = oc_find( c->argv[ 1 ] );
1145                 if ( oc == NULL ) {
1146                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1147                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1148                                 "unable to find ObjectClass \"%s\"",
1149                                 c->argv[ 1 ] );
1150                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1151                                 c->log, c->cr_msg, 0 );
1152                         return 1;
1153                 }
1154
1155                 rc = slap_str2ad( c->argv[ 2 ], &ad, &text );
1156                 if ( rc != LDAP_SUCCESS ) {
1157                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1158                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1159                                 "unable to find AttributeDescription \"%s\"",
1160                                 c->argv[ 2 ] );
1161                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1162                                 c->log, c->cr_msg, 0 );
1163                         return 1;
1164                 }
1165
1166                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1167                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1168                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1169                                 "AttributeDescription \"%s\" "
1170                                 "must be a subtype of \"labeledURI\"",
1171                                 c->argv[ 2 ] );
1172                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1173                                 c->log, c->cr_msg, 0 );
1174                         return 1;
1175                 }
1176
1177                 if ( c->argc == 4 ) {
1178                         rc = slap_str2ad( c->argv[ 3 ], &member_ad, &text );
1179                         if ( rc != LDAP_SUCCESS ) {
1180                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1181                                         "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1182                                         "unable to find AttributeDescription \"%s\"\n",
1183                                         c->argv[ 3 ] );
1184                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1185                                         c->log, c->cr_msg, 0 );
1186                                 return 1;
1187                         }
1188                 }
1189
1190                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1191                         *dlip; dlip = &(*dlip)->dli_next )
1192                 {
1193                         /* The same URL attribute / member attribute pair
1194                          * cannot be repeated */
1195                         if ( (*dlip)->dli_ad == ad && (*dlip)->dli_member_ad == member_ad ) {
1196                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1197                                         "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1198                                         "URL attributeDescription \"%s\" already mapped.\n",
1199                                         ad->ad_cname.bv_val );
1200                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1201                                         c->log, c->cr_msg, 0 );
1202 #if 0
1203                                 /* make it a warning... */
1204                                 return 1;
1205 #endif
1206                         }
1207                 }
1208
1209                 if ( c->valx > 0 ) {
1210                         int     i;
1211
1212                         for ( i = 0, dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1213                                 i < c->valx; i++ )
1214                         {
1215                                 if ( *dlip == NULL ) {
1216                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1217                                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1218                                                 "invalid index {%d}\n",
1219                                                 c->valx );
1220                                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1221                                                 c->log, c->cr_msg, 0 );
1222                                         return 1;
1223                                 }
1224                                 dlip = &(*dlip)->dli_next;
1225                         }
1226                         dli_next = *dlip;
1227
1228                 } else {
1229                         for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1230                                 *dlip; dlip = &(*dlip)->dli_next )
1231                                 /* goto last */;
1232                 }
1233
1234                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1235
1236                 (*dlip)->dli_oc = oc;
1237                 (*dlip)->dli_ad = ad;
1238                 (*dlip)->dli_member_ad = member_ad;
1239                 (*dlip)->dli_next = dli_next;
1240
1241                 rc = dynlist_build_def_filter( *dlip );
1242
1243                 } break;
1244
1245         case DL_ATTRPAIR_COMPAT:
1246                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1247                         "warning: \"attrpair\" only supported for limited "
1248                         "backward compatibility with overlay \"dyngroup\"" );
1249                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
1250                 /* fallthru */
1251
1252         case DL_ATTRPAIR: {
1253                 dynlist_info_t          **dlip;
1254                 ObjectClass             *oc = NULL;
1255                 AttributeDescription    *ad = NULL,
1256                                         *member_ad = NULL;
1257                 const char              *text;
1258
1259                 oc = oc_find( "groupOfURLs" );
1260                 if ( oc == NULL ) {
1261                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1262                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1263                                 "unable to find default ObjectClass \"groupOfURLs\"" );
1264                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1265                                 c->log, c->cr_msg, 0 );
1266                         return 1;
1267                 }
1268
1269                 rc = slap_str2ad( c->argv[ 1 ], &member_ad, &text );
1270                 if ( rc != LDAP_SUCCESS ) {
1271                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1272                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1273                                 "unable to find AttributeDescription \"%s\"",
1274                                 c->argv[ 1 ] );
1275                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1276                                 c->log, c->cr_msg, 0 );
1277                         return 1;
1278                 }
1279
1280                 rc = slap_str2ad( c->argv[ 2 ], &ad, &text );
1281                 if ( rc != LDAP_SUCCESS ) {
1282                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1283                                 "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1284                                 "unable to find AttributeDescription \"%s\"\n",
1285                                 c->argv[ 2 ] );
1286                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1287                                 c->log, c->cr_msg, 0 );
1288                         return 1;
1289                 }
1290
1291                 if ( !is_at_subtype( ad->ad_type, slap_schema.si_ad_labeledURI->ad_type ) ) {
1292                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1293                                 "\"dynlist-attrset <oc> <URL-ad> [<member-ad>]\": "
1294                                 "AttributeDescription \"%s\" "
1295                                 "must be a subtype of \"labeledURI\"",
1296                                 c->argv[ 2 ] );
1297                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1298                                 c->log, c->cr_msg, 0 );
1299                         return 1;
1300                 }
1301
1302                 for ( dlip = (dynlist_info_t **)&on->on_bi.bi_private;
1303                         *dlip; dlip = &(*dlip)->dli_next )
1304                 {
1305                         /* The same URL attribute / member attribute pair
1306                          * cannot be repeated */
1307                         if ( (*dlip)->dli_ad == ad && (*dlip)->dli_member_ad == member_ad ) {
1308                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1309                                         "\"dynlist-attrpair <member-ad> <URL-ad>\": "
1310                                         "URL attributeDescription \"%s\" already mapped.\n",
1311                                         ad->ad_cname.bv_val );
1312                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1313                                         c->log, c->cr_msg, 0 );
1314 #if 0
1315                                 /* make it a warning... */
1316                                 return 1;
1317 #endif
1318                         }
1319                 }
1320
1321                 *dlip = (dynlist_info_t *)ch_calloc( 1, sizeof( dynlist_info_t ) );
1322
1323                 (*dlip)->dli_oc = oc;
1324                 (*dlip)->dli_ad = ad;
1325                 (*dlip)->dli_member_ad = member_ad;
1326
1327                 rc = dynlist_build_def_filter( *dlip );
1328
1329                 } break;
1330
1331         default:
1332                 rc = 1;
1333                 break;
1334         }
1335
1336         return rc;
1337 }
1338 #endif
1339
1340 static int
1341 dynlist_db_open(
1342         BackendDB       *be,
1343         ConfigReply     *cr )
1344 {
1345         slap_overinst           *on = (slap_overinst *) be->bd_info;
1346         dynlist_info_t          *dli = (dynlist_info_t *)on->on_bi.bi_private;
1347         ObjectClass             *oc = NULL;
1348         AttributeDescription    *ad = NULL;
1349         const char      *text;
1350         int rc;
1351
1352         if ( dli == NULL ) {
1353                 dli = ch_calloc( 1, sizeof( dynlist_info_t ) );
1354                 on->on_bi.bi_private = (void *)dli;
1355         }
1356
1357         for ( ; dli; dli = dli->dli_next ) {
1358                 if ( dli->dli_oc == NULL ) {
1359                         if ( oc == NULL ) {
1360                                 oc = oc_find( "groupOfURLs" );
1361                                 if ( oc == NULL ) {
1362                                         sprintf( cr->msg, "unable to fetch objectClass \"groupOfURLs\"" );
1363                                         Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s.\n", cr->msg, 0, 0 );
1364                                         return 1;
1365                                 }
1366                         }
1367
1368                         dli->dli_oc = oc;
1369                 }
1370
1371                 if ( dli->dli_ad == NULL ) {
1372                         if ( ad == NULL ) {
1373                                 rc = slap_str2ad( "memberURL", &ad, &text );
1374                                 if ( rc != LDAP_SUCCESS ) {
1375                                         sprintf( cr->msg, "unable to fetch attributeDescription \"memberURL\": %d (%s)",
1376                                                 rc, text );
1377                                         Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s.\n", cr->msg, 0, 0 );
1378                                         return 1;
1379                                 }
1380                         }
1381                 
1382                         dli->dli_ad = ad;                       
1383                 }
1384
1385                 if ( BER_BVISNULL( &dli->dli_default_filter ) ) {
1386                         rc = dynlist_build_def_filter( dli );
1387                         if ( rc != 0 ) {
1388                                 return rc;
1389                         }
1390                 }
1391         }
1392
1393         rc = slap_str2ad( "dgIdentity", &ad_dgIdentity, &text );
1394         if ( rc != LDAP_SUCCESS ) {
1395                 snprintf( cr->msg, sizeof( cr->msg),
1396                         "unable to fetch attributeDescription \"dgIdentity\": %d (%s)",
1397                         rc, text );
1398                 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s\n", cr->msg, 0, 0 );
1399                 /* Just a warning */
1400         }
1401
1402         rc = slap_str2ad( "dgAuthz", &ad_dgAuthz, &text );
1403         if ( rc != LDAP_SUCCESS ) {
1404                 snprintf( cr->msg, sizeof( cr->msg),
1405                         "unable to fetch attributeDescription \"dgAuthz\": %d (%s)",
1406                         rc, text );
1407                 Debug( LDAP_DEBUG_ANY, "dynlist_db_open: %s\n", cr->msg, 0, 0 );
1408                 /* Just a warning */
1409         }
1410
1411         return 0;
1412 }
1413
1414 static int
1415 dynlist_db_destroy(
1416         BackendDB       *be,
1417         ConfigReply     *cr )
1418 {
1419         slap_overinst   *on = (slap_overinst *) be->bd_info;
1420
1421         if ( on->on_bi.bi_private ) {
1422                 dynlist_info_t  *dli = (dynlist_info_t *)on->on_bi.bi_private,
1423                                 *dli_next;
1424
1425                 for ( dli_next = dli; dli_next; dli = dli_next ) {
1426                         dli_next = dli->dli_next;
1427
1428                         ch_free( dli->dli_default_filter.bv_val );
1429                         ch_free( dli );
1430                 }
1431         }
1432
1433         return 0;
1434 }
1435
1436 static slap_overinst    dynlist = { { NULL } };
1437 #ifdef TAKEOVER_DYNGROUP
1438 static char             *obsolete_names[] = {
1439         "dyngroup",
1440         NULL
1441 };
1442 #endif
1443
1444 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
1445 static
1446 #endif /* SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC */
1447 int
1448 dynlist_initialize(void)
1449 {
1450 #ifndef OL_2_2_COMPAT
1451         int     rc = 0;
1452 #endif
1453
1454         dynlist.on_bi.bi_type = "dynlist";
1455
1456 #ifdef TAKEOVER_DYNGROUP
1457         /* makes dynlist incompatible with dyngroup */
1458         dynlist.on_bi.bi_obsolete_names = obsolete_names;
1459 #endif
1460
1461 #ifdef OL_2_2_COMPAT
1462         dynlist.on_bi.bi_db_config = dynlist_db_config;
1463 #else
1464         dynlist.on_bi.bi_db_config = config_generic_wrapper;
1465 #endif
1466         dynlist.on_bi.bi_db_open = dynlist_db_open;
1467         dynlist.on_bi.bi_db_destroy = dynlist_db_destroy;
1468
1469         dynlist.on_response = dynlist_response;
1470
1471 #ifndef OL_2_2_COMPAT
1472         dynlist.on_bi.bi_cf_ocs = dlocs;
1473
1474         rc = config_register_schema( dlcfg, dlocs );
1475         if ( rc ) {
1476                 return rc;
1477         }
1478 #endif
1479
1480         return overlay_register( &dynlist );
1481 }
1482
1483 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
1484 int
1485 init_module( int argc, char *argv[] )
1486 {
1487         return dynlist_initialize();
1488 }
1489 #endif
1490
1491 #endif /* SLAPD_OVER_DYNLIST */