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