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