]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/dynlist.c
Don't do ctxcsn checks in Tool mode, don't generate ctxcsn if it's missing
[openldap] / servers / slapd / overlays / dynlist.c
1 /* dynlist.c - dynamic list overlay */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2003-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2004-2005 Pierangelo Masarati.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Pierangelo Masarati
18  * for SysNet s.n.c., for inclusion in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_OVER_DYNLIST
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28
29 #include "slap.h"
30 #include "lutil.h"
31
32 /* FIXME: the code differs if SLAP_OPATTRS is defined or not;
33  * SLAP_OPATTRS is not defined in 2.2 yet, while this overlay
34  * expects HEAD code at least later than August 6, 2004. */
35
36 typedef struct dynlist_info {
37         ObjectClass             *dli_oc;
38         AttributeDescription    *dli_ad;
39         AttributeDescription    *dli_member_ad;
40         struct berval           dli_default_filter;
41 } dynlist_info;
42
43 static int
44 dynlist_is_dynlist( Operation *op, SlapReply *rs )
45 {
46         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
47         dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
48
49         Attribute       *a;
50
51         a = attrs_find( rs->sr_entry->e_attrs, slap_schema.si_ad_objectClass );
52         if ( a == NULL ) {
53                 /* FIXME: objectClass must be present; for non-storage
54                  * backends, like back-ldap, it needs to be added
55                  * to the requested attributes */
56                 return 0;
57         }
58
59         if ( value_find_ex( slap_schema.si_ad_objectClass, 
60                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
61                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
62                         a->a_nvals, &dli->dli_oc->soc_cname,
63                         op->o_tmpmemctx ) == 0 )
64         {
65                 return 1;
66         }
67
68         return 0;
69 }
70
71 static int
72 dynlist_make_filter( Operation *op, struct berval *oldf, struct berval *newf )
73 {
74         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
75         dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
76
77         char            *ptr;
78
79         assert( oldf );
80         assert( newf );
81         assert( !BER_BVISNULL( oldf ) );
82         assert( !BER_BVISEMPTY( oldf ) );
83
84         newf->bv_len = STRLENOF( "(&(!(objectClass=" "))" ")" )
85                 + dli->dli_oc->soc_cname.bv_len + oldf->bv_len;
86         newf->bv_val = op->o_tmpalloc( newf->bv_len + 1, op->o_tmpmemctx );
87         if ( newf->bv_val == NULL ) {
88                 return -1;
89         }
90         ptr = lutil_strcopy( newf->bv_val, "(&(!(objectClass=" );
91         ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
92         ptr = lutil_strcopy( ptr, "))" );
93         ptr = lutil_strcopy( ptr, oldf->bv_val );
94         ptr = lutil_strcopy( ptr, ")" );
95         newf->bv_len = ptr - newf->bv_val;
96
97         return 0;
98 }
99
100 typedef struct dynlist_sc_t {
101         dynlist_info    *dlc_dli;
102         Entry           *dlc_e;
103 } dynlist_sc_t;
104
105 static int
106 dynlist_sc_update( Operation *op, SlapReply *rs )
107 {
108         Entry                   *e;
109         Attribute               *a;
110         int                     opattrs,
111                                 userattrs;
112         AccessControlState      acl_state = ACL_STATE_INIT;
113
114         dynlist_sc_t            *dlc;
115
116         if ( rs->sr_type != REP_SEARCH ) {
117                 return 0;
118         }
119
120         dlc = (dynlist_sc_t *)op->o_callback->sc_private;
121         e = dlc->dlc_e;
122
123         assert( e != NULL );
124         assert( rs->sr_entry != NULL );
125
126         /* test access to entry */
127         if ( !access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
128                                 NULL, ACL_READ, NULL ) )
129         {
130                 goto done;
131         }
132
133         if ( dlc->dlc_dli->dli_member_ad ) {
134
135                 /* if access allowed, try to add values, emulating permissive
136                  * control to silently ignore duplicates */
137                 if ( access_allowed( op, rs->sr_entry, slap_schema.si_ad_entry,
138                                         NULL, ACL_READ, NULL ) )
139                 {
140                         Modification    mod;
141                         const char      *text = NULL;
142                         char            textbuf[1024];
143                         struct berval   vals[ 2 ], nvals[ 2 ];
144
145                         vals[ 0 ] = rs->sr_entry->e_name;
146                         BER_BVZERO( &vals[ 1 ] );
147                         nvals[ 0 ] = rs->sr_entry->e_nname;
148                         BER_BVZERO( &nvals[ 1 ] );
149
150                         mod.sm_op = LDAP_MOD_ADD;
151                         mod.sm_desc = dlc->dlc_dli->dli_member_ad;
152                         mod.sm_type = dlc->dlc_dli->dli_member_ad->ad_cname;
153                         mod.sm_values = vals;
154                         mod.sm_nvalues = nvals;
155
156                         (void)modify_add_values( e, &mod, /* permissive */ 1,
157                                         &text, textbuf, sizeof( textbuf ) );
158                 }
159
160                 goto done;
161         }
162
163 #ifndef SLAP_OPATTRS
164         opattrs = ( rs->sr_attrs == NULL ) ? 0 : an_find( rs->sr_attrs, &AllOper );
165         userattrs = ( rs->sr_attrs == NULL ) ? 1 : an_find( rs->sr_attrs, &AllUser );
166 #else /* SLAP_OPATTRS */
167         opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
168         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
169 #endif /* SLAP_OPATTRS */
170
171         for ( a = rs->sr_entry->e_attrs; a != NULL; a = a->a_next ) {
172                 BerVarray       vals, nvals = NULL;
173                 int             i, j;
174
175                 /* if attribute is not requested, skip it */
176                 if ( rs->sr_attrs == NULL ) {
177                         if ( is_at_operational( a->a_desc->ad_type ) ) {
178                                 continue;
179                         }
180
181                 } else {
182                         if ( is_at_operational( a->a_desc->ad_type ) ) {
183                                 if ( !opattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
184                                 {
185                                         continue;
186                                 }
187
188                         } else {
189                                 if ( !userattrs && !ad_inlist( a->a_desc, rs->sr_attrs ) )
190                                 {
191                                         continue;
192                                 }
193                         }
194                 }
195
196                 /* test access to attribute */
197                 if ( op->ors_attrsonly ) {
198                         if ( !access_allowed( op, rs->sr_entry, a->a_desc, NULL,
199                                                 ACL_READ, &acl_state ) )
200                         {
201                                 continue;
202                         }
203                 }
204
205                 /* single-value check: keep first only */
206                 if ( is_at_single_value( a->a_desc->ad_type ) ) {
207                         if ( attr_find( e->e_attrs, a->a_desc ) != NULL ) {
208                                 continue;
209                         }
210                 }
211
212                 /* test access to attribute */
213                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
214                         /* jst count */ ;
215
216                 vals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
217                 if ( a->a_nvals != a->a_vals ) {
218                         nvals = op->o_tmpalloc( ( i + 1 ) * sizeof( struct berval ), op->o_tmpmemctx );
219                 }
220
221                 for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
222                         if ( access_allowed( op, rs->sr_entry, a->a_desc,
223                                                 &a->a_nvals[i], ACL_READ, &acl_state ) )
224                         {
225                                 vals[j] = a->a_vals[i];
226                                 if ( nvals ) {
227                                         nvals[j] = a->a_nvals[i];
228                                 }
229                                 j++;
230                         }
231                 }
232
233                 /* if access allowed, try to add values, emulating permissive
234                  * control to silently ignore duplicates */
235                 if ( j != 0 ) {
236                         Modification    mod;
237                         const char      *text = NULL;
238                         char            textbuf[1024];
239
240                         BER_BVZERO( &vals[j] );
241                         if ( nvals ) {
242                                 BER_BVZERO( &nvals[j] );
243                         }
244
245                         mod.sm_op = LDAP_MOD_ADD;
246                         mod.sm_desc = a->a_desc;
247                         mod.sm_type = a->a_desc->ad_cname;
248                         mod.sm_values = vals;
249                         mod.sm_nvalues = nvals;
250
251                         (void)modify_add_values( e, &mod, /* permissive */ 1,
252                                         &text, textbuf, sizeof( textbuf ) );
253                 }
254
255                 op->o_tmpfree( vals, op->o_tmpmemctx );
256                 if ( nvals ) {
257                         op->o_tmpfree( nvals, op->o_tmpmemctx );
258                 }
259         }
260
261 done:;
262         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
263                 entry_free( rs->sr_entry );
264         }
265
266         return 0;
267 }
268         
269 static int
270 dynlist_send_entry( Operation *op, SlapReply *rs )
271 {
272         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
273         dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
274
275         Attribute       *a;
276         slap_callback   cb;
277         Operation       o = *op;
278         SlapReply       r = *rs;
279         struct berval   *url;
280         Entry           *e;
281         int             e_flags;
282         int             opattrs,
283                         userattrs;
284         dynlist_sc_t    dlc = { 0 };
285
286         a = attrs_find( rs->sr_entry->e_attrs, dli->dli_ad );
287         if ( a == NULL ) {
288                 /* FIXME: error? */
289                 return SLAP_CB_CONTINUE;
290         }
291
292         e = entry_dup( rs->sr_entry );
293         e_flags = rs->sr_flags | ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
294
295         dlc.dlc_e = e;
296         dlc.dlc_dli = dli;
297         cb.sc_private = &dlc;
298         cb.sc_response = dynlist_sc_update;
299         cb.sc_cleanup = NULL;
300         cb.sc_next = NULL;
301
302         o.o_callback = &cb;
303         o.ors_limit = NULL;
304         o.ors_tlimit = SLAP_NO_LIMIT;
305         o.ors_slimit = SLAP_NO_LIMIT;
306
307 #ifndef SLAP_OPATTRS
308         opattrs = ( rs->sr_attrs == NULL ) ? 0 : an_find( rs->sr_attrs, &AllOper );
309         userattrs = ( rs->sr_attrs == NULL ) ? 1 : an_find( rs->sr_attrs, &AllUser );
310 #else /* SLAP_OPATTRS */
311         opattrs = SLAP_OPATTRS( rs->sr_attr_flags );
312         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
313 #endif /* SLAP_OPATTRS */
314
315         for ( url = a->a_nvals; !BER_BVISNULL( url ); url++ ) {
316                 LDAPURLDesc     *lud = NULL;
317                 int             i, j;
318                 struct berval   dn;
319                 int             rc;
320
321                 BER_BVZERO( &o.o_req_dn );
322                 BER_BVZERO( &o.o_req_ndn );
323                 o.ors_filter = NULL;
324                 o.ors_attrs = NULL;
325                 BER_BVZERO( &o.ors_filterstr );
326
327                 if ( ldap_url_parse( url->bv_val, &lud ) != LDAP_URL_SUCCESS ) {
328                         /* FIXME: error? */
329                         continue;
330                 }
331
332                 if ( lud->lud_host ) {
333                         /* FIXME: host not allowed; reject as illegal? */
334                         Debug( LDAP_DEBUG_ANY, "dynlist_send_entry(\"%s\"): "
335                                 "illegal URI \"%s\"\n",
336                                 e->e_name.bv_val, url->bv_val, 0 );
337                         goto cleanup;
338                 }
339
340                 if ( lud->lud_dn == NULL ) {
341                         /* note that an empty base is not honored in terms
342                          * of defaultSearchBase, because select_backend()
343                          * is not aware of the defaultSearchBase option;
344                          * this can be useful in case of a database serving
345                          * the empty suffix */
346                         BER_BVSTR( &dn, "" );
347                 } else {
348                         ber_str2bv( lud->lud_dn, 0, 0, &dn );
349                 }
350                 rc = dnPrettyNormal( NULL, &dn, &o.o_req_dn, &o.o_req_ndn, op->o_tmpmemctx );
351                 if ( rc != LDAP_SUCCESS ) {
352                         /* FIXME: error? */
353                         goto cleanup;
354                 }
355                 o.ors_scope = lud->lud_scope;
356
357                 if ( dli->dli_member_ad != NULL ) {
358                         o.ors_attrs = slap_anlist_no_attrs;
359
360                 } else if ( lud->lud_attrs == NULL ) {
361                         o.ors_attrs = rs->sr_attrs;
362
363                 } else {
364                         for ( i = 0; lud->lud_attrs[i]; i++)
365                                 /* just count */ ;
366
367                         o.ors_attrs = op->o_tmpcalloc( i + 1, sizeof( AttributeName ), op->o_tmpmemctx );
368                         for ( i = 0, j = 0; lud->lud_attrs[i]; i++) {
369                                 const char      *text = NULL;
370         
371                                 ber_str2bv( lud->lud_attrs[i], 0, 0, &o.ors_attrs[j].an_name );
372                                 o.ors_attrs[j].an_desc = NULL;
373                                 (void)slap_bv2ad( &o.ors_attrs[j].an_name, &o.ors_attrs[j].an_desc, &text );
374                                 /* FIXME: ignore errors... */
375
376                                 if ( rs->sr_attrs == NULL ) {
377                                         if ( o.ors_attrs[j].an_desc != NULL &&
378                                                         is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
379                                         {
380                                                 continue;
381                                         }
382
383                                 } else {
384                                         if ( o.ors_attrs[j].an_desc != NULL &&
385                                                         is_at_operational( o.ors_attrs[j].an_desc->ad_type ) )
386                                         {
387                                                 if ( !opattrs && !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
388                                                 {
389                                                         continue;
390                                                 }
391
392                                         } else {
393                                                 if ( !userattrs && 
394                                                                 o.ors_attrs[j].an_desc != NULL &&
395                                                                 !ad_inlist( o.ors_attrs[j].an_desc, rs->sr_attrs ) )
396                                                 {
397                                                         continue;
398                                                 }
399                                         }
400                                 }
401
402                                 j++;
403                         }
404
405                         if ( j == 0 ) {
406                                 goto cleanup;
407                         }
408                 
409                         BER_BVZERO( &o.ors_attrs[j].an_name );
410                 }
411
412                 if ( lud->lud_filter == NULL ) {
413                         ber_dupbv_x( &o.ors_filterstr,
414                                         &dli->dli_default_filter, op->o_tmpmemctx );
415                 } else {
416                         struct berval   flt;
417                         ber_str2bv( lud->lud_filter, 0, 0, &flt );
418                         if ( dynlist_make_filter( op, &flt, &o.ors_filterstr ) ) {
419                                 /* error */
420                                 goto cleanup;
421                         }
422                 }
423                 o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
424                 if ( o.ors_filter == NULL ) {
425                         goto cleanup;
426                 }
427                 
428                 o.o_bd = select_backend( &o.o_req_ndn, 0, 1 );
429                 if ( o.o_bd && o.o_bd->be_search ) {
430 #ifdef SLAP_OPATTRS
431                         r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
432 #endif /* SLAP_OPATTRS */
433                         (void)o.o_bd->be_search( &o, &r );
434                 }
435
436 cleanup:;
437                 if ( o.ors_filter ) {
438                         filter_free_x( &o, o.ors_filter );
439                 }
440                 if ( o.ors_attrs && o.ors_attrs != rs->sr_attrs
441                                 && o.ors_attrs != slap_anlist_no_attrs )
442                 {
443                         op->o_tmpfree( o.ors_attrs, op->o_tmpmemctx );
444                 }
445                 if ( !BER_BVISNULL( &o.o_req_dn ) ) {
446                         op->o_tmpfree( o.o_req_dn.bv_val, op->o_tmpmemctx );
447                 }
448                 if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
449                         op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
450                 }
451                 if ( o.ors_filterstr.bv_val != lud->lud_filter ) {
452                         op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
453                         lud->lud_filter = NULL;
454                 }
455                 if ( lud ) {
456                         ldap_free_urldesc( lud );
457                 }
458         }
459
460         rs->sr_entry = e;
461         rs->sr_flags = e_flags;
462
463         return SLAP_CB_CONTINUE;
464 }
465
466 static int
467 dynlist_sc_save_entry( Operation *op, SlapReply *rs )
468 {
469         /* save the entry in the private field of the callback,
470          * so it doesn't get freed (it's temporary!) */
471         if ( rs->sr_entry != NULL ) {
472                 dynlist_sc_t    *dlc = (dynlist_sc_t *)op->o_callback->sc_private;
473                 dlc->dlc_e = rs->sr_entry;
474                 rs->sr_entry = NULL;
475         }
476
477         return 0;
478 }
479
480 static int
481 dynlist_compare( Operation *op, SlapReply *rs )
482 {
483         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
484         dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
485
486         Attribute       *a;
487         slap_callback   cb;
488         Operation       o = *op;
489         SlapReply       r = { REP_SEARCH };
490         AttributeName   an[2];
491         int             rc;
492         dynlist_sc_t    dlc = { 0 };
493
494         dlc.dlc_dli = dli;
495         cb.sc_private = &dlc;
496         cb.sc_response = dynlist_sc_save_entry;
497         cb.sc_cleanup = NULL;
498         cb.sc_next = NULL;
499         o.o_callback = &cb;
500
501         o.o_tag = LDAP_REQ_SEARCH;
502         o.ors_limit = NULL;
503         o.ors_tlimit = SLAP_NO_LIMIT;
504         o.ors_slimit = SLAP_NO_LIMIT;
505
506         o.o_bd = select_backend( &o.o_req_ndn, 0, 1 );
507         if ( !o.o_bd || !o.o_bd->be_search ) {
508                 return SLAP_CB_CONTINUE;
509         }
510
511         BER_BVSTR( &o.ors_filterstr, "(objectClass=*)" );
512         o.ors_filter = str2filter_x( op, o.ors_filterstr.bv_val );
513         if ( o.ors_filter == NULL ) {
514                 /* FIXME: error? */
515                 return SLAP_CB_CONTINUE;
516         }
517
518         o.ors_scope = LDAP_SCOPE_BASE;
519         an[0].an_name = op->orc_ava->aa_desc->ad_cname;
520         an[0].an_desc = op->orc_ava->aa_desc;
521         BER_BVZERO( &an[1].an_name );
522         o.ors_attrs = an;
523         o.ors_attrsonly = 0;
524
525         rc = o.o_bd->be_search( &o, &r );
526         filter_free_x( &o, o.ors_filter );
527
528         if ( rc != 0 ) {
529                 return rc;
530         }
531
532         if ( dlc.dlc_e != NULL ) {
533                 r.sr_entry = dlc.dlc_e;
534         }
535
536         if ( r.sr_err != LDAP_SUCCESS || r.sr_entry == NULL ) {
537                 /* error? */
538                 return SLAP_CB_CONTINUE;
539         }
540
541         /* if we're here, we got a match... */
542         rs->sr_err = LDAP_COMPARE_FALSE;
543         for ( a = attrs_find( r.sr_entry->e_attrs, op->orc_ava->aa_desc );
544                 a != NULL;
545                 a = attrs_find( a->a_next, op->orc_ava->aa_desc ) )
546         {
547                 if ( value_find_ex( op->orc_ava->aa_desc,
548                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
549                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
550                         a->a_nvals, &op->orc_ava->aa_value, op->o_tmpmemctx ) == 0 )
551                 {
552                         rs->sr_err = LDAP_COMPARE_TRUE;
553                         break;
554                 }
555         }
556
557         if ( r.sr_flags & REP_ENTRY_MUSTBEFREED ) {
558                 entry_free( r.sr_entry );
559         }
560
561         return SLAP_CB_CONTINUE;
562 }
563
564 static int
565 dynlist_response( Operation *op, SlapReply *rs )
566 {
567         switch ( op->o_tag ) {
568         case LDAP_REQ_SEARCH:
569                 if ( rs->sr_type == REP_SEARCH && !get_manageDSAit( op ) )
570                 {
571                         if ( dynlist_is_dynlist( op, rs ) ) {
572                                 return dynlist_send_entry( op, rs );
573                         }
574                 }
575                 break;
576
577         case LDAP_REQ_COMPARE:
578                 if ( rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) {
579                         return dynlist_compare( op, rs );
580                 }
581                 break;
582
583         default:
584                 break;
585         }
586
587         return SLAP_CB_CONTINUE;
588 }
589
590 static int
591 dynlist_db_config(
592     BackendDB   *be,
593     const char  *fname,
594     int         lineno,
595     int         argc,
596     char        **argv
597 )
598 {
599         slap_overinst   *on = (slap_overinst *)be->bd_info;
600         dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
601
602         int             rc = 0;
603
604         if ( strcasecmp( argv[0], "dynlist-oc" ) == 0 ) {
605                 if ( argc != 2 ) {
606                         fprintf( stderr, "dynlist-oc <oc>\n" );
607                         return 1;
608                 }
609                 dli->dli_oc = oc_find( argv[1] );
610                 if ( dli->dli_oc == NULL ) {
611                         fprintf( stderr, "dynlist-oc <oc>: "
612                                         "unable to find ObjectClass "
613                                         "\"%s\"\n", argv[1] );
614                         return 1;
615                 }
616
617         } else if ( strcasecmp( argv[0], "dynlist-ad" ) == 0 ) {
618                 const char      *text;
619
620                 if ( argc != 2 ) {
621                         fprintf( stderr, "dynlist-ad <ad>\n" );
622                         return 1;
623                 }
624                 dli->dli_ad = NULL;
625                 rc = slap_str2ad( argv[1], &dli->dli_ad, &text );
626                 if ( rc != LDAP_SUCCESS ) {
627                         fprintf( stderr, "dynlist-ad <ad>: "
628                                         "unable to find AttributeDescription "
629                                         "\"%s\"\n", argv[1] );
630                         return 1;
631                 }
632
633         } else if ( strcasecmp( argv[0], "dynlist-member-ad" ) == 0 ) {
634                 const char      *text;
635
636                 if ( argc != 2 ) {
637                         fprintf( stderr, "dynlist-member-ad <ad>\n" );
638                         return 1;
639                 }
640                 dli->dli_member_ad = NULL;
641                 rc = slap_str2ad( argv[1], &dli->dli_member_ad, &text );
642                 if ( rc != LDAP_SUCCESS ) {
643                         fprintf( stderr, "dynlist-member-ad <ad>: "
644                                         "unable to find AttributeDescription "
645                                         "\"%s\"\n", argv[1] );
646                         return 1;
647                 }
648
649         } else {
650                 rc = SLAP_CONF_UNKNOWN;
651         }
652
653         return rc;
654 }
655
656 static int
657 dynlist_db_init(
658         BackendDB *be
659 )
660 {
661         slap_overinst   *on = (slap_overinst *) be->bd_info;
662         dynlist_info    *dli;
663
664         dli = (dynlist_info *)ch_malloc( sizeof( dynlist_info ) );
665         memset( dli, 0, sizeof( dynlist_info ) );
666
667         on->on_bi.bi_private = (void *)dli;
668
669         return 0;
670 }
671
672 static int
673 dynlist_db_open(
674         BackendDB *be
675 )
676 {
677         slap_overinst   *on = (slap_overinst *) be->bd_info;
678         dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
679         int             rc = 0;
680         ber_len_t       len;
681         char            *ptr;
682
683         if ( dli->dli_oc == NULL ) {
684                 fprintf( stderr, "dynlist_db_open(): missing \"dynlist-oc <ObjectClass>\"\n" );
685                 rc = -1;
686         }
687
688         if ( dli->dli_ad == NULL ) {
689                 fprintf( stderr, "dynlist_db_open(): missing \"dynlist-ad <AttributeDescription>\"\n" );
690                 rc = -1;
691         }
692
693         len = STRLENOF( "(!(objectClass=" "))" )
694                 + dli->dli_oc->soc_cname.bv_len;
695         dli->dli_default_filter.bv_val = SLAP_MALLOC( len + 1 );
696         if ( dli->dli_default_filter.bv_val == NULL ) {
697                 fprintf( stderr, "dynlist_db_open(): malloc failed\n" );
698                 return -1;
699         }
700         ptr = lutil_strcopy( dli->dli_default_filter.bv_val, "(!(objectClass=" );
701         ptr = lutil_strcopy( ptr, dli->dli_oc->soc_cname.bv_val );
702         ptr = lutil_strcopy( ptr, "))" );
703         dli->dli_default_filter.bv_len = ptr - dli->dli_default_filter.bv_val;
704
705         return rc;
706 }
707
708 static int
709 dynlist_db_destroy(
710         BackendDB *be
711 )
712 {
713         slap_overinst   *on = (slap_overinst *) be->bd_info;
714         int             rc = 0;
715
716         if ( on->on_bi.bi_private ) {
717                 dynlist_info    *dli = (dynlist_info *)on->on_bi.bi_private;
718
719                 dli->dli_oc = NULL;
720                 dli->dli_ad = NULL;
721
722                 ch_free( dli );
723         }
724
725         return rc;
726 }
727
728 static slap_overinst dynlist = { { NULL } };
729
730 int
731 dynlist_init(void)
732 {
733         dynlist.on_bi.bi_type = "dynlist";
734         dynlist.on_bi.bi_db_init = dynlist_db_init;
735         dynlist.on_bi.bi_db_config = dynlist_db_config;
736         dynlist.on_bi.bi_db_open = dynlist_db_open;
737         dynlist.on_bi.bi_db_destroy = dynlist_db_destroy;
738
739         dynlist.on_response = dynlist_response;
740
741         return overlay_register( &dynlist );
742 }
743
744 #if SLAPD_OVER_DYNLIST == SLAPD_MOD_DYNAMIC
745 int
746 init_module( int argc, char *argv[] )
747 {
748         return dynlist_init();
749 }
750 #endif
751
752 #endif /* SLAPD_OVER_DYNLIST */