]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/memberof.c
if referential integrity is broken, things could go wrong; don't assert
[openldap] / servers / slapd / overlays / memberof.c
1 /* memberof.c - back-reference for group membership */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2007 Pierangelo Masarati <ando@sys-net.it>
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 /* ACKNOWLEDGMENTS:
17  * This work was initially developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software, sponsored by SysNet s.r.l.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_OVER_MEMBEROF
24
25 #include <stdio.h>
26
27 #include "ac/string.h"
28 #include "ac/socket.h"
29
30 #include "slap.h"
31 #include "config.h"
32 #include "lutil.h"
33
34 /*
35  *      Glossary:
36  *
37  *              GROUP           a group object (an entry with GROUP_OC
38  *                              objectClass)
39  *              MEMBER          a member object (an entry whose DN is
40  *                              listed as MEMBER_AT value of a GROUP)
41  *              GROUP_OC        the objectClass of the group object
42  *                              (default: groupOfNames)
43  *              MEMBER_AT       the membership attribute, DN-valued;
44  *                              note: nameAndOptionalUID is tolerated
45  *                              as soon as the optionalUID is absent
46  *                              (default: member)
47  *              MEMBER_OF       reverse membership attribute
48  *                              (default: memberOf)
49  *
50  *      - add:
51  *              - if the entry that is being added is a GROUP,
52  *                the MEMBER_AT defined as values of the add operation
53  *                get the MEMBER_OF value directly from the request.
54  *
55  *                if configured to do so, the MEMBER objects do not exist,
56  *                and no relax control is issued, either:
57  *                      - fail
58  *                      - drop non-existing members
59  *                (by default: don't muck with values)
60  *
61  *              - if (configured to do so,) the referenced GROUP exists,
62  *                the relax control is set and the user has
63  *                "manage" privileges, allow to add MEMBER_OF values to
64  *                generic entries.
65  *
66  *      - modify:
67  *              - if the entry being modified is a GROUP_OC and the 
68  *                MEMBER_AT attribute is modified, the MEMBER_OF value
69  *                of the (existing) MEMBER_AT entries that are affected
70  *                is modified according to the request:
71  *                      - if a MEMBER is removed from the group,
72  *                        delete the corresponding MEMBER_OF
73  *                      - if a MEMBER is added to a group,
74  *                        add the corresponding MEMBER_OF
75  *
76  *                We need to determine, from the database, if it is
77  *                a GROUP_OC, and we need to check, from the
78  *                modification list, if the MEMBER_AT attribute is being
79  *                affected, and what MEMBER_AT values are affected.
80  *
81  *                if configured to do so, the entries corresponding to
82  *                the MEMBER_AT values do not exist, and no relax control
83  *                is issued, either:
84  *                      - fail
85  *                      - drop non-existing members
86  *                (by default: don't muck with values)
87  *
88  *              - if configured to do so, the referenced GROUP exists,
89  *                (the relax control is set) and the user has
90  *                "manage" privileges, allow to add MEMBER_OF values to
91  *                generic entries; the change is NOT automatically reflected
92  *                in the MEMBER attribute of the GROUP referenced
93  *                by the value of MEMBER_OF; a separate modification,
94  *                with or without relax control, needs to be performed.
95  *
96  *      - modrdn:
97  *              - if the entry being renamed is a GROUP, the MEMBER_OF
98  *                value of the (existing) MEMBER objects is modified
99  *                accordingly based on the newDN of the GROUP.
100  *
101  *                We need to determine, from the database, if it is
102  *                a GROUP; the list of MEMBER objects is obtained from
103  *                the database.
104  *
105  *                Non-existing MEMBER objects are ignored, since the
106  *                MEMBER_AT is not being addressed by the operation.
107  *
108  *              - if the entry being renamed has the MEMBER_OF attribute,
109  *                the corresponding MEMBER value must be modified in the
110  *                respective group entries.
111  *              
112  *
113  *      - delete:
114  *              - if the entry being deleted is a GROUP, the (existing)
115  *                MEMBER objects are modified accordingly; a copy of the 
116  *                values of the MEMBER_AT is saved and, if the delete 
117  *                succeeds, the MEMBER_OF value of the (existing) MEMBER
118  *                objects is deleted.
119  *
120  *                We need to determine, from the database, if it is
121  *                a GROUP.
122  *
123  *                Non-existing MEMBER objects are ignored, since the entry
124  *                is being deleted.
125  *
126  *              - if the entry being deleted has the MEMBER_OF attribute,
127  *                the corresponding value of the MEMBER_AT must be deleted
128  *                from the respective GROUP entries.
129  */
130
131 #define SLAPD_MEMBEROF_ATTR     "memberOf"
132
133 static slap_overinst            memberof;
134
135 typedef struct memberof_t {
136         struct berval           mo_dn;
137         struct berval           mo_ndn;
138
139         ObjectClass             *mo_oc_group;
140         AttributeDescription    *mo_ad_member;
141         AttributeDescription    *mo_ad_memberof;
142         
143         struct berval           mo_groupFilterstr;
144         AttributeAssertion      mo_groupAVA;
145         Filter                  mo_groupFilter;
146
147         struct berval           mo_memberFilterstr;
148         Filter                  mo_memberFilter;
149
150         unsigned                mo_flags;
151 #define MEMBEROF_NONE           0x00U
152 #define MEMBEROF_FDANGLING_DROP 0x01U
153 #define MEMBEROF_FDANGLING_ERROR        0x02U
154 #define MEMBEROF_FDANGLING_MASK (MEMBEROF_FDANGLING_DROP|MEMBEROF_FDANGLING_ERROR)
155 #define MEMBEROF_FREFINT        0x04U
156 #define MEMBEROF_FREVERSE       0x08U
157
158 #define MEMBEROF_CHK(mo,f) \
159         (((mo)->mo_flags & (f)) == (f))
160 #define MEMBEROF_DANGLING_CHECK(mo) \
161         ((mo)->mo_flags & MEMBEROF_FDANGLING_MASK)
162 #define MEMBEROF_DANGLING_DROP(mo) \
163         MEMBEROF_CHK((mo),MEMBEROF_FDANGLING_DROP)
164 #define MEMBEROF_DANGLING_ERROR(mo) \
165         MEMBEROF_CHK((mo),MEMBEROF_FDANGLING_ERROR)
166 #define MEMBEROF_REFINT(mo) \
167         MEMBEROF_CHK((mo),MEMBEROF_FREFINT)
168 #define MEMBEROF_REVERSE(mo) \
169         MEMBEROF_CHK((mo),MEMBEROF_FREVERSE)
170 } memberof_t;
171
172 typedef enum memberof_is_t {
173         MEMBEROF_IS_NONE = 0x00,
174         MEMBEROF_IS_GROUP = 0x01,
175         MEMBEROF_IS_MEMBER = 0x02,
176         MEMBEROF_IS_BOTH = (MEMBEROF_IS_GROUP|MEMBEROF_IS_MEMBER)
177 } memberof_is_t;
178
179 /*
180  * failover storage for member attribute values of groups being deleted
181  * handles [no]thread cases.
182  */
183 static BerVarray        saved_member_vals;
184 static BerVarray        saved_memberof_vals;
185
186 static void
187 memberof_saved_member_free( void *key, void *data )
188 {
189         ber_bvarray_free( (BerVarray)data );
190 }
191
192 static BerVarray
193 memberof_saved_member_get( Operation *op, void *keyp )
194 {
195         BerVarray       vals;
196         BerVarray       *key = (BerVarray *)keyp;
197
198         assert( op != NULL );
199
200         if ( op->o_threadctx == NULL ) {
201                 vals = *key;
202                 *key = NULL;
203
204         } else {
205                 ldap_pvt_thread_pool_getkey( op->o_threadctx,
206                                 key, (void **)&vals, NULL );
207                 ldap_pvt_thread_pool_setkey( op->o_threadctx,
208                                 key, NULL, NULL );
209         }
210
211         return vals;
212 }
213
214 static void
215 memberof_saved_member_set( Operation *op, void *keyp, BerVarray vals )
216 {
217         BerVarray       saved_vals = NULL;
218         BerVarray       *key = (BerVarray*)keyp;
219
220         assert( op != NULL );
221
222         if ( vals ) {
223                 ber_bvarray_dup_x( &saved_vals, vals, NULL );
224         }
225
226         if ( op->o_threadctx == NULL ) {
227                 if ( *key ) {
228                         ber_bvarray_free( *key );
229                 }
230                 *key = saved_vals;
231
232         } else {
233                 ldap_pvt_thread_pool_setkey( op->o_threadctx, key,
234                                 saved_vals, memberof_saved_member_free );
235         }
236 }
237
238 typedef struct memberof_cookie_t {
239         AttributeDescription    *ad;
240         void                    *key;
241         int                     foundit;
242 } memberof_cookie_t;
243
244 static int
245 memberof_isGroupOrMember_cb( Operation *op, SlapReply *rs )
246 {
247         if ( rs->sr_type == REP_SEARCH ) {
248                 memberof_cookie_t       *mc;
249
250                 mc = (memberof_cookie_t *)op->o_callback->sc_private;
251                 mc->foundit = 1;
252         }
253
254         return 0;
255 }
256
257 /*
258  * callback for internal search that saves the member attribute values
259  * of groups being deleted.
260  */
261 static int
262 memberof_saveMember_cb( Operation *op, SlapReply *rs )
263 {
264         if ( rs->sr_type == REP_SEARCH ) {
265                 memberof_cookie_t       *mc;
266                 Attribute               *a;
267                 BerVarray               vals = NULL;
268
269                 mc = (memberof_cookie_t *)op->o_callback->sc_private;
270                 mc->foundit = 1;
271
272                 assert( rs->sr_entry != NULL );
273                 assert( rs->sr_entry->e_attrs != NULL );
274
275                 a = attr_find( rs->sr_entry->e_attrs, mc->ad );
276                 if ( a != NULL ) {
277                         vals = a->a_nvals;
278                 }
279
280                 memberof_saved_member_set( op, mc->key, vals );
281
282                 if ( a && attr_find( a->a_next, mc->ad ) != NULL ) {
283                         Debug( LDAP_DEBUG_ANY,
284                                 "%s: memberof_saveMember_cb(\"%s\"): "
285                                 "more than one occurrence of \"%s\" "
286                                 "attribute.\n",
287                                 op->o_log_prefix,
288                                 rs->sr_entry->e_name.bv_val,
289                                 mc->ad->ad_cname.bv_val );
290                 }
291         }
292
293         return 0;
294 }
295
296 /*
297  * the delete hook performs an internal search that saves the member
298  * attribute values of groups being deleted.
299  */
300 static int
301 memberof_isGroupOrMember( Operation *op, memberof_is_t *iswhatp )
302 {
303         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
304         memberof_t              *mo = (memberof_t *)on->on_bi.bi_private;
305
306         Operation               op2 = *op;
307         SlapReply               rs2 = { REP_RESULT };
308         slap_callback           cb = { 0 };
309         memberof_cookie_t       mc;
310         AttributeName           an[ 2 ];
311
312         memberof_is_t           iswhat = MEMBEROF_IS_NONE;
313
314         assert( iswhatp != NULL );
315         assert( *iswhatp != MEMBEROF_IS_NONE );
316
317         cb.sc_private = &mc;
318         if ( op->o_tag == LDAP_REQ_DELETE ) {
319                 cb.sc_response = memberof_saveMember_cb;
320
321         } else {
322                 cb.sc_response = memberof_isGroupOrMember_cb;
323         }
324
325         op2.o_tag = LDAP_REQ_SEARCH;
326         op2.o_callback = &cb;
327         op2.o_dn = op->o_bd->be_rootdn;
328         op2.o_ndn = op->o_bd->be_rootndn;
329
330         op2.ors_scope = LDAP_SCOPE_BASE;
331         op2.ors_deref = LDAP_DEREF_NEVER;
332         BER_BVZERO( &an[ 1 ].an_name );
333         op2.ors_attrs = an;
334         op2.ors_attrsonly = 0;
335         op2.ors_limit = NULL;
336         op2.ors_slimit = 1;
337         op2.ors_tlimit = SLAP_NO_LIMIT;
338
339         if ( *iswhatp & MEMBEROF_IS_GROUP ) {
340                 mc.ad = mo->mo_ad_member;
341                 mc.key = &saved_member_vals;
342                 mc.foundit = 0;
343                 an[ 0 ].an_desc = mo->mo_ad_member;
344                 an[ 0 ].an_name = an[ 0 ].an_desc->ad_cname;
345                 op2.ors_filterstr = mo->mo_groupFilterstr;
346                 op2.ors_filter = &mo->mo_groupFilter;
347
348                 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
349                 (void)op->o_bd->be_search( &op2, &rs2 );
350                 op2.o_bd->bd_info = (BackendInfo *)on;
351
352                 if ( mc.foundit ) {
353                         iswhat |= MEMBEROF_IS_GROUP;
354
355                 } else {
356                         memberof_saved_member_set( op, mc.key, NULL );
357                 }
358         }
359
360         if ( *iswhatp & MEMBEROF_IS_MEMBER ) {
361                 mc.ad = mo->mo_ad_memberof;
362                 mc.key = &saved_memberof_vals;
363                 mc.foundit = 0;
364                 an[ 0 ].an_desc = mo->mo_ad_memberof;
365                 an[ 0 ].an_name = an[ 0 ].an_desc->ad_cname;
366                 op2.ors_filterstr = mo->mo_memberFilterstr;
367                 op2.ors_filter = &mo->mo_memberFilter;
368
369                 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
370                 (void)op->o_bd->be_search( &op2, &rs2 );
371                 op2.o_bd->bd_info = (BackendInfo *)on;
372
373                 if ( mc.foundit ) {
374                         iswhat |= MEMBEROF_IS_MEMBER;
375
376                 } else {
377                         memberof_saved_member_set( op, mc.key, NULL );
378                 }
379         }
380
381         *iswhatp = iswhat;
382
383         return LDAP_SUCCESS;
384 }
385
386 /*
387  * response callback that adds memberof values when a group is modified.
388  */
389 static int
390 memberof_value_modify(
391         Operation               *op,
392         SlapReply               *rs,
393         struct berval           *ndn,
394         AttributeDescription    *ad,
395         struct berval           *old_dn,
396         struct berval           *old_ndn,
397         struct berval           *new_dn,
398         struct berval           *new_ndn )
399 {
400         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
401         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
402
403         Operation       op2 = *op;
404         SlapReply       rs2 = { REP_RESULT };
405         slap_callback   cb = { NULL, slap_null_cb, NULL, NULL };
406         Modifications   mod[ 2 ] = { { { 0 } } }, *ml;
407         struct berval   values[ 4 ], nvalues[ 4 ];
408
409         op2.o_tag = LDAP_REQ_MODIFY;
410
411         op2.o_req_dn = *ndn;
412         op2.o_req_ndn = *ndn;
413
414         op2.o_bd->bd_info = (BackendInfo *)on->on_info;
415
416         op2.o_callback = &cb;
417         op2.o_dn = op->o_bd->be_rootdn;
418         op2.o_ndn = op->o_bd->be_rootndn;
419
420         ml = &mod[ 0 ];
421         ml->sml_numvals = 1;
422         ml->sml_values = &values[ 0 ];
423         ml->sml_values[ 0 ] = mo->mo_dn;
424         BER_BVZERO( &ml->sml_values[ 1 ] );
425         ml->sml_nvalues = &nvalues[ 0 ];
426         ml->sml_nvalues[ 0 ] = mo->mo_ndn;
427         BER_BVZERO( &ml->sml_nvalues[ 1 ] );
428         ml->sml_desc = slap_schema.si_ad_modifiersName;
429         ml->sml_type = ml->sml_desc->ad_cname;
430         ml->sml_op = LDAP_MOD_REPLACE;
431         ml->sml_flags = SLAP_MOD_INTERNAL;
432         ml->sml_next = NULL;
433         op2.orm_modlist = ml;
434
435         ml = &mod[ 1 ];
436         ml->sml_numvals = 1;
437         ml->sml_values = &values[ 2 ];
438         BER_BVZERO( &ml->sml_values[ 1 ] );
439         ml->sml_nvalues = &nvalues[ 2 ];
440         BER_BVZERO( &ml->sml_nvalues[ 1 ] );
441         ml->sml_desc = ad;
442         ml->sml_type = ml->sml_desc->ad_cname;
443         ml->sml_flags = SLAP_MOD_INTERNAL;
444         ml->sml_next = NULL;
445         op2.orm_modlist->sml_next = ml;
446
447         if ( new_ndn != NULL ) {
448                 assert( !BER_BVISNULL( new_dn ) );
449                 assert( !BER_BVISNULL( new_ndn ) );
450
451                 ml = &mod[ 1 ];
452                 ml->sml_op = LDAP_MOD_ADD;
453
454                 ml->sml_values[ 0 ] = *new_dn;
455                 ml->sml_nvalues[ 0 ] = *new_ndn;
456
457                 (void)op->o_bd->be_modify( &op2, &rs2 );
458                 if ( rs2.sr_err != LDAP_SUCCESS ) {
459                         char buf[ SLAP_TEXT_BUFLEN ];
460                         snprintf( buf, sizeof( buf ),
461                                 "memberof_value_modify %s=\"%s\" failed err=%d",
462                                 ad->ad_cname.bv_val, new_dn->bv_val, rs2.sr_err );
463                         Debug( LDAP_DEBUG_ANY, "%s: %s\n", op->o_log_prefix, buf, 0 );
464                 }
465
466                 assert( op2.orm_modlist == &mod[ 0 ] );
467                 assert( op2.orm_modlist->sml_next == &mod[ 1 ] );
468                 ml = op2.orm_modlist->sml_next->sml_next;
469                 if ( ml != NULL ) {
470                         slap_mods_free( ml, 1 );
471                 }
472         }
473
474         if ( old_ndn != NULL ) {
475                 assert( !BER_BVISNULL( old_dn ) );
476                 assert( !BER_BVISNULL( old_ndn ) );
477
478                 ml = &mod[ 1 ];
479                 ml->sml_op = LDAP_MOD_DELETE;
480
481                 ml->sml_values[ 0 ] = *old_dn;
482                 ml->sml_nvalues[ 0 ] = *old_ndn;
483
484                 (void)op->o_bd->be_modify( &op2, &rs2 );
485                 if ( rs2.sr_err != LDAP_SUCCESS ) {
486                         char buf[ SLAP_TEXT_BUFLEN ];
487                         snprintf( buf, sizeof( buf ),
488                                 "memberof_value_modify %s=\"%s\" failed err=%d",
489                                 ad->ad_cname.bv_val, old_dn->bv_val, rs2.sr_err );
490                         Debug( LDAP_DEBUG_ANY, "%s: %s\n", op->o_log_prefix, buf, 0 );
491                 }
492
493                 assert( op2.orm_modlist == &mod[ 0 ] );
494                 assert( op2.orm_modlist->sml_next == &mod[ 1 ] );
495                 ml = op2.orm_modlist->sml_next->sml_next;
496                 if ( ml != NULL ) {
497                         slap_mods_free( ml, 1 );
498                 }
499         }
500
501         /* FIXME: if old_group_ndn doesn't exist, both delete __and__
502          * add will fail; better split in two operations, although
503          * not optimal in terms of performance.  At least it would
504          * move towards self-repairing capabilities. */
505
506         op2.o_bd->bd_info = (BackendInfo *)on;
507
508         return rs2.sr_err;
509 }
510
511 static int
512 memberof_op_add( Operation *op, SlapReply *rs )
513 {
514         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
515         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
516
517         Attribute       **ap, **map = NULL;
518         int             rc = SLAP_CB_CONTINUE;
519         int             i;
520         struct berval   save_dn, save_ndn;
521
522         if ( op->ora_e->e_attrs == NULL ) {
523                 /* FIXME: global overlay; need to deal with */
524                 Debug( LDAP_DEBUG_ANY, "%s: memberof_op_add(\"%s\"): "
525                         "consistency checks not implemented when overlay "
526                         "is instantiated as global.\n",
527                         op->o_log_prefix, op->o_req_dn.bv_val, 0 );
528                 return SLAP_CB_CONTINUE;
529         }
530
531         if ( MEMBEROF_REVERSE( mo ) ) {
532                 for ( ap = &op->ora_e->e_attrs; *ap; ap = &(*ap)->a_next ) {
533                         Attribute       *a = *ap;
534
535                         if ( a->a_desc == mo->mo_ad_memberof ) {
536                                 map = ap;
537                                 break;
538                         }
539                 }
540         }
541
542         save_dn = op->o_dn;
543         save_ndn = op->o_ndn;
544
545         if ( MEMBEROF_DANGLING_CHECK( mo )
546                         && !get_relax( op )
547                         && is_entry_objectclass_or_sub( op->ora_e, mo->mo_oc_group ) )
548         {
549                 op->o_dn = op->o_bd->be_rootdn;
550                 op->o_dn = op->o_bd->be_rootndn;
551                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
552
553                 for ( ap = &op->ora_e->e_attrs; *ap; ) {
554                         Attribute       *a = *ap;
555
556                         if ( !is_ad_subtype( a->a_desc, mo->mo_ad_member ) ) {
557                                 ap = &a->a_next;
558                                 continue;
559                         }
560
561                         assert( a->a_nvals != NULL );
562
563                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
564                                 Entry           *e = NULL;
565
566                                 rc = be_entry_get_rw( op, &a->a_nvals[ i ],
567                                                 NULL, NULL, 0, &e );
568                                 if ( rc == LDAP_SUCCESS ) {
569                                         be_entry_release_r( op, e );
570                                         continue;
571                                 }
572
573                                 if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
574                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
575                                         rs->sr_text = "adding non-existing object "
576                                                 "as group member";
577                                         send_ldap_result( op, rs );
578                                         goto done;
579                                 }
580
581                                 if ( MEMBEROF_DANGLING_DROP( mo ) ) {
582                                         int     j;
583         
584                                         Debug( LDAP_DEBUG_ANY, "%s: memberof_op_add(\"%s\"): "
585                                                 "member=\"%s\" does not exist (stripping...)\n",
586                                                 op->o_log_prefix, op->ora_e->e_name.bv_val,
587                                                 a->a_vals[ i ].bv_val );
588         
589                                         for ( j = i + 1; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ );
590                                         ber_memfree( a->a_vals[ i ].bv_val );
591                                         BER_BVZERO( &a->a_vals[ i ] );
592                                         if ( a->a_nvals != a->a_vals ) {
593                                                 ber_memfree( a->a_nvals[ i ].bv_val );
594                                                 BER_BVZERO( &a->a_nvals[ i ] );
595                                         }
596                                         if ( j - i == 1 ) {
597                                                 break;
598                                         }
599                 
600                                         AC_MEMCPY( &a->a_vals[ i ], &a->a_vals[ i + 1 ],
601                                                 sizeof( struct berval ) * ( j - i ) );
602                                         if ( a->a_nvals != a->a_vals ) {
603                                                 AC_MEMCPY( &a->a_nvals[ i ], &a->a_nvals[ i + 1 ],
604                                                         sizeof( struct berval ) * ( j - i ) );
605                                         }
606                                         i--;
607                                 }
608                         }
609
610                         /* If all values have been removed,
611                          * remove the attribute itself. */
612                         if ( BER_BVISNULL( &a->a_nvals[ 0 ] ) ) {
613                                 *ap = a->a_next;
614                                 attr_free( a );
615         
616                         } else {
617                                 ap = &a->a_next;
618                         }
619                 }
620                 op->o_dn = save_dn;
621                 op->o_ndn = save_ndn;
622                 op->o_bd->bd_info = (BackendInfo *)on;
623         }
624
625         if ( map != NULL ) {
626                 Attribute               *a = *map;
627                 AccessControlState      acl_state = ACL_STATE_INIT;
628
629                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
630                         Entry           *e;
631
632                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
633                         /* access is checked with the original identity */
634                         rc = access_allowed( op, op->ora_e, mo->mo_ad_memberof,
635                                         &a->a_nvals[ i ], ACL_WADD,
636                                         &acl_state );
637                         if ( rc == 0 ) {
638                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
639                                 rs->sr_text = NULL;
640                                 send_ldap_result( op, rs );
641                                 goto done;
642                         }
643                         rc = be_entry_get_rw( op, &a->a_nvals[ i ],
644                                         NULL, NULL, 0, &e );
645                         op->o_bd->bd_info = (BackendInfo *)on;
646                         if ( rc != LDAP_SUCCESS ) {
647                                 if ( get_relax( op ) ) {
648                                         continue;
649                                 }
650
651                                 if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
652                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
653                                         rs->sr_text = "adding non-existing object "
654                                                 "as memberof";
655                                         send_ldap_result( op, rs );
656                                         goto done;
657                                 }
658
659                                 if ( MEMBEROF_DANGLING_DROP( mo ) ) {
660                                         int     j;
661         
662                                         Debug( LDAP_DEBUG_ANY, "%s: memberof_op_add(\"%s\"): "
663                                                 "memberof=\"%s\" does not exist (stripping...)\n",
664                                                 op->o_log_prefix, op->ora_e->e_name.bv_val,
665                                                 a->a_nvals[ i ].bv_val );
666         
667                                         for ( j = i + 1; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ );
668                                         ber_memfree( a->a_vals[ i ].bv_val );
669                                         BER_BVZERO( &a->a_vals[ i ] );
670                                         if ( a->a_nvals != a->a_vals ) {
671                                                 ber_memfree( a->a_nvals[ i ].bv_val );
672                                                 BER_BVZERO( &a->a_nvals[ i ] );
673                                         }
674                                         if ( j - i == 1 ) {
675                                                 break;
676                                         }
677                 
678                                         AC_MEMCPY( &a->a_vals[ i ], &a->a_vals[ i + 1 ],
679                                                 sizeof( struct berval ) * ( j - i ) );
680                                         if ( a->a_nvals != a->a_vals ) {
681                                                 AC_MEMCPY( &a->a_nvals[ i ], &a->a_nvals[ i + 1 ],
682                                                         sizeof( struct berval ) * ( j - i ) );
683                                         }
684                                         i--;
685                                 }
686                                 
687                                 continue;
688                         }
689
690                         /* access is checked with the original identity */
691                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
692                         rc = access_allowed( op, e, mo->mo_ad_member,
693                                         &op->o_req_ndn, ACL_WADD, NULL );
694                         be_entry_release_r( op, e );
695                         op->o_bd->bd_info = (BackendInfo *)on;
696
697                         if ( !rc ) {
698                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
699                                 rs->sr_text = "insufficient access to object referenced by memberof";
700                                 send_ldap_result( op, rs );
701                                 goto done;
702                         }
703                 }
704
705                 if ( BER_BVISNULL( &a->a_nvals[ 0 ] ) ) {
706                         *map = a->a_next;
707                         attr_free( a );
708                 }
709         }
710
711         rc = SLAP_CB_CONTINUE;
712         
713 done:;
714         op->o_dn = save_dn;
715         op->o_ndn = save_ndn;
716         op->o_bd->bd_info = (BackendInfo *)on;
717
718         return rc;
719 }
720
721 static int
722 memberof_op_delete( Operation *op, SlapReply *rs )
723 {
724         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
725         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
726
727         memberof_is_t   iswhat = MEMBEROF_IS_GROUP;
728
729         if ( MEMBEROF_REFINT( mo ) ) {
730                 iswhat = MEMBEROF_IS_BOTH;
731         }
732
733         memberof_isGroupOrMember( op, &iswhat );
734
735         return SLAP_CB_CONTINUE;
736 }
737
738 static int
739 memberof_op_modify( Operation *op, SlapReply *rs )
740 {
741         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
742         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
743
744         Modifications   **mlp, **mmlp = NULL;
745         int             rc = SLAP_CB_CONTINUE;
746         struct berval   save_dn, save_ndn;
747         memberof_is_t   iswhat = MEMBEROF_IS_GROUP;
748
749         if ( MEMBEROF_REVERSE( mo ) ) {
750                 for ( mlp = &op->orm_modlist; *mlp; mlp = &(*mlp)->sml_next ) {
751                         Modifications   *ml = *mlp;
752
753                         if ( ml->sml_desc == mo->mo_ad_memberof ) {
754                                 mmlp = mlp;
755                                 break;
756                         }
757                 }
758         }
759
760         save_dn = op->o_dn;
761         save_ndn = op->o_ndn;
762
763         if ( memberof_isGroupOrMember( op, &iswhat ) == LDAP_SUCCESS
764                 && ( iswhat & MEMBEROF_IS_GROUP ) )
765         {
766                 Modifications *ml;
767                 int save_member = 0;
768
769                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
770                         if ( ml->sml_desc == mo->mo_ad_member ) {
771                                 switch ( ml->sml_op ) {
772                                 case LDAP_MOD_DELETE:
773                                 case LDAP_MOD_REPLACE:
774                                         save_member = 1;
775                                         break;
776                                 }
777                         }
778                 }
779
780                 if ( save_member ) {
781                         BerVarray       vals = NULL;
782
783                         op->o_dn = op->o_bd->be_rootdn;
784                         op->o_dn = op->o_bd->be_rootndn;
785                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
786                         rc = backend_attribute( op, NULL, &op->o_req_ndn,
787                                         mo->mo_ad_member, &vals, ACL_READ );
788                         op->o_bd->bd_info = (BackendInfo *)on;
789                         if ( rc == LDAP_SUCCESS && vals != NULL ) {
790                                 memberof_saved_member_set( op, &saved_member_vals, vals );
791                                 ber_bvarray_free_x( vals, op->o_tmpmemctx );
792                         }
793                 }
794
795                 if ( MEMBEROF_DANGLING_CHECK( mo )
796                                 && !get_relax( op ) )
797                 {
798                         op->o_dn = op->o_bd->be_rootdn;
799                         op->o_dn = op->o_bd->be_rootndn;
800                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
801                 
802                         assert( op->orm_modlist != NULL );
803                 
804                         for ( mlp = &op->orm_modlist; *mlp; ) {
805                                 Modifications   *ml = *mlp;
806                                 int             i;
807                 
808                                 if ( !is_ad_subtype( ml->sml_desc, mo->mo_ad_member ) ) {
809                                         mlp = &ml->sml_next;
810                                         continue;
811                                 }
812                 
813                                 switch ( ml->sml_op ) {
814                                 case LDAP_MOD_DELETE:
815                                         /* we don't care about cancellations: if the value
816                                          * exists, fine; if it doesn't, we let the underlying
817                                          * database fail as appropriate; */
818                                         mlp = &ml->sml_next;
819                                         break;
820                 
821                                 case LDAP_MOD_REPLACE:
822                                 case LDAP_MOD_ADD:
823                                         /* NOTE: right now, the attributeType we use
824                                          * for member must have a normalized value */
825                                         assert( ml->sml_nvalues != NULL );
826                 
827                                         for ( i = 0; !BER_BVISNULL( &ml->sml_nvalues[ i ] ); i++ ) {
828                                                 int             rc;
829                                                 Entry           *e;
830                 
831                                                 if ( be_entry_get_rw( op, &ml->sml_nvalues[ i ],
832                                                                 NULL, NULL, 0, &e ) == LDAP_SUCCESS )
833                                                 {
834                                                         be_entry_release_r( op, e );
835                                                         continue;
836                                                 }
837                 
838                                                 if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
839                                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
840                                                         rs->sr_text = "adding non-existing object "
841                                                                 "as group member";
842                                                         send_ldap_result( op, rs );
843                                                         goto done;
844                                                 }
845                 
846                                                 if ( MEMBEROF_DANGLING_DROP( mo ) ) {
847                                                         int     j;
848                 
849                                                         Debug( LDAP_DEBUG_ANY, "%s: memberof_op_modify(\"%s\"): "
850                                                                 "member=\"%s\" does not exist (stripping...)\n",
851                                                                 op->o_log_prefix, op->o_req_dn.bv_val,
852                                                                 ml->sml_nvalues[ i ].bv_val );
853                 
854                                                         for ( j = i + 1; !BER_BVISNULL( &ml->sml_nvalues[ j ] ); j++ );
855                                                         ber_memfree( ml->sml_values[ i ].bv_val );
856                                                         BER_BVZERO( &ml->sml_values[ i ] );
857                                                         ber_memfree( ml->sml_nvalues[ i ].bv_val );
858                                                         BER_BVZERO( &ml->sml_nvalues[ i ] );
859                                                         ml->sml_numvals--;
860                                                         if ( j - i == 1 ) {
861                                                                 break;
862                                                         }
863                 
864                                                         AC_MEMCPY( &ml->sml_values[ i ], &ml->sml_values[ i + 1 ],
865                                                                 sizeof( struct berval ) * ( j - i ) );
866                                                         AC_MEMCPY( &ml->sml_nvalues[ i ], &ml->sml_nvalues[ i + 1 ],
867                                                                 sizeof( struct berval ) * ( j - i ) );
868                                                         i--;
869                                                 }
870                                         }
871                 
872                                         if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
873                                                 *mlp = ml->sml_next;
874                                                 slap_mod_free( &ml->sml_mod, 0 );
875                                                 free( ml );
876                 
877                                         } else {
878                                                 mlp = &ml->sml_next;
879                                         }
880                 
881                                         break;
882                 
883                                 default:
884                                         assert( 0 );
885                                 }
886                         }
887                 }
888         }
889         
890         if ( mmlp != NULL ) {
891                 Modifications   *ml = *mmlp;
892                 int             i;
893                 Entry           *target;
894
895                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
896                 rc = be_entry_get_rw( op, &op->o_req_ndn,
897                                 NULL, NULL, 0, &target );
898                 op->o_bd->bd_info = (BackendInfo *)on;
899                 if ( rc != LDAP_SUCCESS ) {
900                         rc = rs->sr_err = LDAP_NO_SUCH_OBJECT;
901                         send_ldap_result( op, rs );
902                         goto done;
903                 }
904
905                 switch ( ml->sml_op ) {
906                 case LDAP_MOD_DELETE:
907                         if ( ml->sml_nvalues != NULL ) {
908                                 AccessControlState      acl_state = ACL_STATE_INIT;
909
910                                 for ( i = 0; !BER_BVISNULL( &ml->sml_nvalues[ i ] ); i++ ) {
911                                         Entry           *e;
912
913                                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
914                                         /* access is checked with the original identity */
915                                         rc = access_allowed( op, target,
916                                                         mo->mo_ad_memberof,
917                                                         &ml->sml_nvalues[ i ],
918                                                         ACL_WDEL,
919                                                         &acl_state );
920                                         if ( rc == 0 ) {
921                                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
922                                                 rs->sr_text = NULL;
923                                                 send_ldap_result( op, rs );
924                                                 goto done2;
925                                         }
926
927                                         rc = be_entry_get_rw( op, &ml->sml_nvalues[ i ],
928                                                         NULL, NULL, 0, &e );
929                                         op->o_bd->bd_info = (BackendInfo *)on;
930                                         if ( rc != LDAP_SUCCESS ) {
931                                                 if ( get_relax( op ) ) {
932                                                         continue;
933                                                 }
934
935                                                 if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
936                                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
937                                                         rs->sr_text = "deleting non-existing object "
938                                                                 "as memberof";
939                                                         send_ldap_result( op, rs );
940                                                         goto done2;
941                                                 }
942
943                                                 if ( MEMBEROF_DANGLING_DROP( mo ) ) {
944                                                         int     j;
945         
946                                                         Debug( LDAP_DEBUG_ANY, "%s: memberof_op_modify(\"%s\"): "
947                                                                 "memberof=\"%s\" does not exist (stripping...)\n",
948                                                                 op->o_log_prefix, op->o_req_ndn.bv_val,
949                                                                 ml->sml_nvalues[ i ].bv_val );
950         
951                                                         for ( j = i + 1; !BER_BVISNULL( &ml->sml_nvalues[ j ] ); j++ );
952                                                         ber_memfree( ml->sml_values[ i ].bv_val );
953                                                         BER_BVZERO( &ml->sml_values[ i ] );
954                                                         if ( ml->sml_nvalues != ml->sml_values ) {
955                                                                 ber_memfree( ml->sml_nvalues[ i ].bv_val );
956                                                                 BER_BVZERO( &ml->sml_nvalues[ i ] );
957                                                         }
958                                                         ml->sml_numvals--;
959                                                         if ( j - i == 1 ) {
960                                                                 break;
961                                                         }
962                 
963                                                         AC_MEMCPY( &ml->sml_values[ i ], &ml->sml_values[ i + 1 ],
964                                                                 sizeof( struct berval ) * ( j - i ) );
965                                                         if ( ml->sml_nvalues != ml->sml_values ) {
966                                                                 AC_MEMCPY( &ml->sml_nvalues[ i ], &ml->sml_nvalues[ i + 1 ],
967                                                                         sizeof( struct berval ) * ( j - i ) );
968                                                         }
969                                                         i--;
970                                                 }
971
972                                                 continue;
973                                         }
974
975                                         /* access is checked with the original identity */
976                                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
977                                         rc = access_allowed( op, e, mo->mo_ad_member,
978                                                         &op->o_req_ndn,
979                                                         ACL_WDEL, NULL );
980                                         be_entry_release_r( op, e );
981                                         op->o_bd->bd_info = (BackendInfo *)on;
982
983                                         if ( !rc ) {
984                                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
985                                                 rs->sr_text = "insufficient access to object referenced by memberof";
986                                                 send_ldap_result( op, rs );
987                                                 goto done;
988                                         }
989                                 }
990
991                                 if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
992                                         *mmlp = ml->sml_next;
993                                         slap_mod_free( &ml->sml_mod, 0 );
994                                         free( ml );
995                                 }
996
997                                 break;
998                         }
999                         /* fall thru */
1000
1001                 case LDAP_MOD_REPLACE:
1002
1003                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1004                         /* access is checked with the original identity */
1005                         rc = access_allowed( op, target,
1006                                         mo->mo_ad_memberof,
1007                                         NULL,
1008                                         ACL_WDEL, NULL );
1009                         op->o_bd->bd_info = (BackendInfo *)on;
1010                         if ( rc == 0 ) {
1011                                 rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1012                                 rs->sr_text = NULL;
1013                                 send_ldap_result( op, rs );
1014                                 goto done2;
1015                         }
1016
1017                         if ( ml->sml_op == LDAP_MOD_DELETE ) {
1018                                 break;
1019                         }
1020                         /* fall thru */
1021
1022                 case LDAP_MOD_ADD: {
1023                         AccessControlState      acl_state = ACL_STATE_INIT;
1024
1025                         for ( i = 0; !BER_BVISNULL( &ml->sml_nvalues[ i ] ); i++ ) {
1026                                 Entry           *e;
1027
1028                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1029                                 /* access is checked with the original identity */
1030                                 rc = access_allowed( op, target,
1031                                                 mo->mo_ad_memberof,
1032                                                 &ml->sml_nvalues[ i ],
1033                                                 ACL_WADD,
1034                                                 &acl_state );
1035                                 if ( rc == 0 ) {
1036                                         rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1037                                         rs->sr_text = NULL;
1038                                         send_ldap_result( op, rs );
1039                                         goto done2;
1040                                 }
1041
1042                                 rc = be_entry_get_rw( op, &ml->sml_nvalues[ i ],
1043                                                 NULL, NULL, 0, &e );
1044                                 op->o_bd->bd_info = (BackendInfo *)on;
1045                                 if ( rc != LDAP_SUCCESS ) {
1046                                         if ( MEMBEROF_DANGLING_ERROR( mo ) ) {
1047                                                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
1048                                                 rs->sr_text = "adding non-existing object "
1049                                                         "as memberof";
1050                                                 send_ldap_result( op, rs );
1051                                                 goto done2;
1052                                         }
1053
1054                                         if ( MEMBEROF_DANGLING_DROP( mo ) ) {
1055                                                 int     j;
1056
1057                                                 Debug( LDAP_DEBUG_ANY, "%s: memberof_op_modify(\"%s\"): "
1058                                                         "memberof=\"%s\" does not exist (stripping...)\n",
1059                                                         op->o_log_prefix, op->o_req_ndn.bv_val,
1060                                                         ml->sml_nvalues[ i ].bv_val );
1061
1062                                                 for ( j = i + 1; !BER_BVISNULL( &ml->sml_nvalues[ j ] ); j++ );
1063                                                 ber_memfree( ml->sml_values[ i ].bv_val );
1064                                                 BER_BVZERO( &ml->sml_values[ i ] );
1065                                                 if ( ml->sml_nvalues != ml->sml_values ) {
1066                                                         ber_memfree( ml->sml_nvalues[ i ].bv_val );
1067                                                         BER_BVZERO( &ml->sml_nvalues[ i ] );
1068                                                 }
1069                                                 ml->sml_numvals--;
1070                                                 if ( j - i == 1 ) {
1071                                                         break;
1072                                                 }
1073         
1074                                                 AC_MEMCPY( &ml->sml_values[ i ], &ml->sml_values[ i + 1 ],
1075                                                         sizeof( struct berval ) * ( j - i ) );
1076                                                 if ( ml->sml_nvalues != ml->sml_values ) {
1077                                                         AC_MEMCPY( &ml->sml_nvalues[ i ], &ml->sml_nvalues[ i + 1 ],
1078                                                                 sizeof( struct berval ) * ( j - i ) );
1079                                                 }
1080                                                 i--;
1081                                         }
1082
1083                                         continue;
1084                                 }
1085
1086                                 /* access is checked with the original identity */
1087                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1088                                 rc = access_allowed( op, e, mo->mo_ad_member,
1089                                                 &op->o_req_ndn,
1090                                                 ACL_WDEL, NULL );
1091                                 be_entry_release_r( op, e );
1092                                 op->o_bd->bd_info = (BackendInfo *)on;
1093
1094                                 if ( !rc ) {
1095                                         rc = rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1096                                         rs->sr_text = "insufficient access to object referenced by memberof";
1097                                         send_ldap_result( op, rs );
1098                                         goto done;
1099                                 }
1100                         }
1101
1102                         if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
1103                                 *mmlp = ml->sml_next;
1104                                 slap_mod_free( &ml->sml_mod, 0 );
1105                                 free( ml );
1106                         }
1107
1108                         } break;
1109
1110                 default:
1111                         assert( 0 );
1112                 }
1113
1114 done2:;
1115                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1116                 be_entry_release_r( op, target );
1117                 op->o_bd->bd_info = (BackendInfo *)on;
1118         }
1119
1120         rc = SLAP_CB_CONTINUE;
1121
1122 done:;
1123         op->o_dn = save_dn;
1124         op->o_ndn = save_ndn;
1125         op->o_bd->bd_info = (BackendInfo *)on;
1126
1127         return rc;
1128 }
1129
1130 /*
1131  * response callback that adds memberof values when a group is added.
1132  */
1133 static int
1134 memberof_res_add( Operation *op, SlapReply *rs )
1135 {
1136         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
1137         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1138
1139         int             i;
1140
1141         if ( MEMBEROF_REVERSE( mo ) ) {
1142                 Attribute       *ma;
1143
1144                 ma = attr_find( op->ora_e->e_attrs, mo->mo_ad_memberof );
1145                 if ( ma != NULL ) {
1146                         Operation       op2 = *op;
1147                         SlapReply       rs2 = { 0 };
1148
1149                         /* relax is required to allow to add
1150                          * a non-existing member */
1151                         op2.o_relax = SLAP_CONTROL_CRITICAL;
1152
1153                         for ( i = 0; !BER_BVISNULL( &ma->a_nvals[ i ] ); i++ ) {
1154                 
1155                                 /* the modification is attempted
1156                                  * with the original identity */
1157                                 (void)memberof_value_modify( &op2, &rs2,
1158                                         &ma->a_nvals[ i ], mo->mo_ad_member,
1159                                         NULL, NULL, &op->o_req_dn, &op->o_req_ndn );
1160                         }
1161                 }
1162         }
1163
1164         if ( is_entry_objectclass_or_sub( op->ora_e, mo->mo_oc_group ) ) {
1165                 Attribute       *a;
1166
1167                 for ( a = attrs_find( op->ora_e->e_attrs, mo->mo_ad_member );
1168                                 a != NULL;
1169                                 a = attrs_find( a->a_next, mo->mo_ad_member ) )
1170                 {
1171                         for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
1172                                 (void)memberof_value_modify( op, rs,
1173                                                 &a->a_nvals[ i ],
1174                                                 mo->mo_ad_memberof,
1175                                                 NULL, NULL,
1176                                                 &op->o_req_dn,
1177                                                 &op->o_req_ndn );
1178                         }
1179                 }
1180         }
1181
1182         return SLAP_CB_CONTINUE;
1183 }
1184
1185 /*
1186  * response callback that deletes memberof values when a group is deleted.
1187  */
1188 static int
1189 memberof_res_delete( Operation *op, SlapReply *rs )
1190 {
1191         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
1192         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1193
1194         BerVarray       vals;
1195         int             i;
1196
1197         vals = memberof_saved_member_get( op, &saved_member_vals );
1198         if ( vals != NULL ) {
1199                 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1200                         (void)memberof_value_modify( op, rs,
1201                                         &vals[ i ], mo->mo_ad_memberof,
1202                                         &op->o_req_dn, &op->o_req_ndn,
1203                                         NULL, NULL );
1204                 }
1205
1206                 ber_bvarray_free( vals );
1207         }
1208
1209         if ( MEMBEROF_REFINT( mo ) ) {
1210                 vals = memberof_saved_member_get( op, &saved_memberof_vals );
1211                 if ( vals != NULL ) {
1212                         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1213                                 (void)memberof_value_modify( op, rs,
1214                                                 &vals[ i ], mo->mo_ad_member,
1215                                                 &op->o_req_dn, &op->o_req_ndn,
1216                                                 NULL, NULL );
1217                         }
1218
1219                         ber_bvarray_free( vals );
1220                 }
1221         }
1222
1223         return SLAP_CB_CONTINUE;
1224 }
1225
1226 /*
1227  * response callback that adds/deletes memberof values when a group
1228  * is modified.
1229  */
1230 static int
1231 memberof_res_modify( Operation *op, SlapReply *rs )
1232 {
1233         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
1234         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1235
1236         int             i, rc;
1237         Modifications   *ml, *mml = NULL;
1238         BerVarray       vals;
1239         memberof_is_t   iswhat = MEMBEROF_IS_GROUP;
1240
1241         if ( MEMBEROF_REVERSE( mo ) ) {
1242                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
1243                         if ( ml->sml_desc == mo->mo_ad_memberof ) {
1244                                 mml = ml;
1245                                 break;
1246                         }
1247                 }
1248         }
1249
1250         if ( mml != NULL ) {
1251                 BerVarray       vals = mml->sml_nvalues;
1252
1253                 switch ( mml->sml_op ) {
1254                 case LDAP_MOD_DELETE:
1255                         if ( vals != NULL ) {
1256                                 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1257                                         memberof_value_modify( op, rs,
1258                                                         &vals[ i ], mo->mo_ad_member,
1259                                                         &op->o_req_dn, &op->o_req_ndn,
1260                                                         NULL, NULL );
1261                                 }
1262                                 break;
1263                         }
1264                         /* fall thru */
1265
1266                 case LDAP_MOD_REPLACE:
1267                         /* delete all ... */
1268                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1269                         rc = backend_attribute( op, NULL, &op->o_req_ndn,
1270                                         mo->mo_ad_memberof, &vals, ACL_READ );
1271                         op->o_bd->bd_info = (BackendInfo *)on;
1272                         if ( rc == LDAP_SUCCESS ) {
1273                                 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1274                                         (void)memberof_value_modify( op, rs,
1275                                                         &vals[ i ], mo->mo_ad_member,
1276                                                         &op->o_req_dn, &op->o_req_ndn,
1277                                                         NULL, NULL );
1278                                 }
1279                                 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1280                         }
1281
1282                         if ( ml->sml_op == LDAP_MOD_DELETE ) {
1283                                 break;
1284                         }
1285                         /* fall thru */
1286
1287                 case LDAP_MOD_ADD:
1288                         assert( vals != NULL );
1289
1290                         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1291                                 memberof_value_modify( op, rs,
1292                                                 &vals[ i ], mo->mo_ad_member,
1293                                                 NULL, NULL,
1294                                                 &op->o_req_dn, &op->o_req_ndn );
1295                         }
1296                         break;
1297
1298                 default:
1299                         assert( 0 );
1300                 }
1301         }
1302
1303         if ( memberof_isGroupOrMember( op, &iswhat ) == LDAP_SUCCESS
1304                         && ( iswhat & MEMBEROF_IS_GROUP ) )
1305         {
1306                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
1307                         if ( ml->sml_desc != mo->mo_ad_member ) {
1308                                 continue;
1309                         }
1310
1311                         switch ( ml->sml_op ) {
1312                         case LDAP_MOD_DELETE:
1313                                 vals = ml->sml_nvalues;
1314                                 if ( vals != NULL ) {
1315                                         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1316                                                 memberof_value_modify( op, rs,
1317                                                                 &vals[ i ], mo->mo_ad_memberof,
1318                                                                 &op->o_req_dn, &op->o_req_ndn,
1319                                                                 NULL, NULL );
1320                                         }
1321                                         break;
1322                                 }
1323                                 /* fall thru */
1324         
1325                         case LDAP_MOD_REPLACE:
1326                                 vals = memberof_saved_member_get( op, &saved_member_vals );
1327
1328                                 /* delete all ... */
1329                                 if ( vals != NULL ) {
1330                                         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1331                                                 (void)memberof_value_modify( op, rs,
1332                                                                 &vals[ i ], mo->mo_ad_memberof,
1333                                                                 &op->o_req_dn, &op->o_req_ndn,
1334                                                                 NULL, NULL );
1335                                         }
1336                                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
1337                                 }
1338         
1339                                 if ( ml->sml_op == LDAP_MOD_DELETE ) {
1340                                         break;
1341                                 }
1342                                 /* fall thru */
1343         
1344                         case LDAP_MOD_ADD:
1345                                 assert( ml->sml_nvalues != NULL );
1346                                 vals = ml->sml_nvalues;
1347                                 for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1348                                         memberof_value_modify( op, rs,
1349                                                         &vals[ i ], mo->mo_ad_memberof,
1350                                                         NULL, NULL,
1351                                                         &op->o_req_dn, &op->o_req_ndn );
1352                                 }
1353                                 break;
1354         
1355                         default:
1356                                 assert( 0 );
1357                         }
1358                 }
1359         }
1360
1361         return SLAP_CB_CONTINUE;
1362 }
1363
1364 /*
1365  * response callback that adds/deletes member values when a group member
1366  * is modified.
1367  */
1368 static int
1369 memberof_res_rename( Operation *op, SlapReply *rs )
1370 {
1371         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
1372         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1373
1374         struct berval   newPDN, newDN = BER_BVNULL, newPNDN, newNDN;
1375         int             i, rc;
1376         BerVarray       vals;
1377
1378         struct berval   save_dn, save_ndn;
1379         memberof_is_t   iswhat = MEMBEROF_IS_GROUP;
1380
1381         if ( MEMBEROF_REFINT( mo ) ) {
1382                 iswhat |= MEMBEROF_IS_MEMBER;
1383         }
1384
1385         if ( op->orr_nnewSup ) {
1386                 newPNDN = *op->orr_nnewSup;
1387
1388         } else {
1389                 dnParent( &op->o_req_ndn, &newPNDN );
1390         }
1391
1392         build_new_dn( &newNDN, &newPNDN, &op->orr_nnewrdn, op->o_tmpmemctx ); 
1393
1394         save_dn = op->o_req_dn;
1395         save_ndn = op->o_req_ndn;
1396
1397         op->o_req_dn = newNDN;
1398         op->o_req_ndn = newNDN;
1399         rc = memberof_isGroupOrMember( op, &iswhat );
1400         op->o_req_dn = save_dn;
1401         op->o_req_ndn = save_ndn;
1402
1403         if ( rc != LDAP_SUCCESS || iswhat == MEMBEROF_IS_NONE ) {
1404                 goto done;
1405         }
1406
1407         if ( op->orr_newSup ) {
1408                 newPDN = *op->orr_newSup;
1409
1410         } else {
1411                 dnParent( &op->o_req_dn, &newPDN );
1412         }
1413
1414         build_new_dn( &newDN, &newPDN, &op->orr_newrdn, op->o_tmpmemctx ); 
1415
1416         if ( iswhat & MEMBEROF_IS_GROUP ) {
1417                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1418                 rc = backend_attribute( op, NULL, &newNDN,
1419                                 mo->mo_ad_member, &vals, ACL_READ );
1420                 op->o_bd->bd_info = (BackendInfo *)on;
1421
1422                 if ( rc == LDAP_SUCCESS ) {
1423                         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1424                                 (void)memberof_value_modify( op, rs,
1425                                                 &vals[ i ], mo->mo_ad_memberof,
1426                                                 &op->o_req_dn, &op->o_req_ndn,
1427                                                 &newDN, &newNDN );
1428                         }
1429                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
1430                 }
1431         }
1432
1433         if ( MEMBEROF_REFINT( mo ) && ( iswhat & MEMBEROF_IS_MEMBER ) ) {
1434                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1435                 rc = backend_attribute( op, NULL, &newNDN,
1436                                 mo->mo_ad_memberof, &vals, ACL_READ );
1437                 op->o_bd->bd_info = (BackendInfo *)on;
1438
1439                 if ( rc == LDAP_SUCCESS ) {
1440                         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
1441                                 (void)memberof_value_modify( op, rs,
1442                                                 &vals[ i ], mo->mo_ad_member,
1443                                                 &op->o_req_dn, &op->o_req_ndn,
1444                                                 &newDN, &newNDN );
1445                         }
1446                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
1447                 }
1448         }
1449
1450 done:;
1451         if ( !BER_BVISNULL( &newDN ) ) {
1452                 op->o_tmpfree( newDN.bv_val, op->o_tmpmemctx );
1453         }
1454         op->o_tmpfree( newNDN.bv_val, op->o_tmpmemctx );
1455
1456         return SLAP_CB_CONTINUE;
1457 }
1458
1459 static int
1460 memberof_response( Operation *op, SlapReply *rs )
1461 {
1462         if ( rs->sr_err != LDAP_SUCCESS ) {
1463                 return SLAP_CB_CONTINUE;
1464         }
1465
1466         switch ( op->o_tag ) {
1467         case LDAP_REQ_ADD:
1468                 return memberof_res_add( op, rs );
1469
1470         case LDAP_REQ_DELETE:
1471                 return memberof_res_delete( op, rs );
1472
1473         case LDAP_REQ_MODIFY:
1474                 return memberof_res_modify( op, rs );
1475
1476         case LDAP_REQ_MODDN:
1477                 return memberof_res_rename( op, rs );
1478
1479         default:
1480                 return SLAP_CB_CONTINUE;
1481         }
1482 }
1483
1484 static int
1485 memberof_db_init(
1486         BackendDB       *be,
1487         ConfigReply     *cr )
1488 {
1489         slap_overinst   *on = (slap_overinst *)be->bd_info;
1490         memberof_t      tmp_mo = { 0 }, *mo;
1491
1492         mo = (memberof_t *)ch_calloc( 1, sizeof( memberof_t ) );
1493         on->on_bi.bi_private = (void *)mo;
1494
1495         return 0;
1496 }
1497
1498 enum {
1499         MO_DN = 1,
1500         MO_DANGLING,
1501         MO_REFINT,
1502 #if 0
1503         MO_REVERSE,
1504 #endif
1505         MO_GROUP_OC,
1506         MO_MEMBER_AD,
1507         MO_MEMBER_OF_AD
1508 };
1509
1510 static ConfigDriver mo_cf_gen;
1511
1512 #define OID             "1.3.6.1.4.1.7136.2.666.4"
1513 #define OIDAT           OID ".1.1"
1514 #define OIDCFGAT        OID ".1.2"
1515 #define OIDOC           OID ".2.1"
1516 #define OIDCFGOC        OID ".2.2"
1517
1518
1519 static ConfigTable mo_cfg[] = {
1520         { "memberof-dn", "modifiersName",
1521                 2, 2, 0, ARG_MAGIC|ARG_DN|MO_DN, mo_cf_gen,
1522                 "( OLcfgOvAt:18.0 NAME 'olcMemberOfDN' "
1523                         "DESC 'DN to be used as modifiersName' "
1524                         "SYNTAX OMsDN SINGLE-VALUE )",
1525                 NULL, NULL },
1526
1527         { "memberof-dangling", "ignore|drop|error",
1528                 2, 2, 0, ARG_MAGIC|MO_DANGLING, mo_cf_gen,
1529                 "( OLcfgOvAt:18.1 NAME 'olcMemberOfDangling' "
1530                         "DESC 'Behavior with respect to dangling members, "
1531                                 "constrained to ignore, drop, error' "
1532                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",
1533                 NULL, NULL },
1534
1535         { "memberof-refint", "true|FALSE",
1536                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|MO_REFINT, mo_cf_gen,
1537                 "( OLcfgOvAt:18.2 NAME 'olcMemberOfRefInt' "
1538                         "DESC 'Take care of referential integrity' "
1539                         "SYNTAX OMsBoolean SINGLE-VALUE )",
1540                 NULL, NULL },
1541
1542         { "memberof-group-oc", "objectClass",
1543                 2, 2, 0, ARG_MAGIC|MO_GROUP_OC, mo_cf_gen,
1544                 "( OLcfgOvAt:18.3 NAME 'olcMemberOfGroupOC' "
1545                         "DESC 'Group objectClass' "
1546                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",
1547                 NULL, NULL },
1548
1549         { "memberof-member-ad", "member attribute",
1550                 2, 2, 0, ARG_MAGIC|MO_MEMBER_AD, mo_cf_gen,
1551                 "( OLcfgOvAt:18.4 NAME 'olcMemberOfMemberAD' "
1552                         "DESC 'member attribute' "
1553                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",
1554                 NULL, NULL },
1555
1556         { "memberof-memberof-ad", "memberOf attribute",
1557                 2, 2, 0, ARG_MAGIC|MO_MEMBER_OF_AD, mo_cf_gen,
1558                 "( OLcfgOvAt:18.5 NAME 'olcMemberOfMemberOfAD' "
1559                         "DESC 'memberOf attribute' "
1560                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",
1561                 NULL, NULL },
1562
1563 #if 0
1564         { "memberof-reverse", "true|FALSE",
1565                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|MO_REVERSE, mo_cf_gen,
1566                 "( OLcfgOvAt:18.6 NAME 'olcMemberOfReverse' "
1567                         "DESC 'Take care of referential integrity "
1568                                 "also when directly modifying memberOf' "
1569                         "SYNTAX OMsBoolean SINGLE-VALUE )",
1570                 NULL, NULL },
1571 #endif
1572
1573         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1574 };
1575
1576 static ConfigOCs mo_ocs[] = {
1577         { "( OLcfgOvOc:18.1 "
1578                 "NAME 'olcMemberOf' "
1579                 "DESC 'Member-of configuration' "
1580                 "SUP olcOverlayConfig "
1581                 "MAY ( "
1582                         "olcMemberOfDN "
1583                         "$ olcMemberOfDangling "
1584                         "$ olcMemberOfRefInt "
1585                         "$ olcMemberOfGroupOC "
1586                         "$ olcMemberOfMemberAD "
1587                         "$ olcMemberOfMemberOfAD "
1588 #if 0
1589                         "$ olcMemberOfReverse "
1590 #endif
1591                         ") "
1592                 ")",
1593                 Cft_Overlay, mo_cfg, NULL, NULL },
1594         { NULL, 0, NULL }
1595 };
1596
1597 static slap_verbmasks dangling_mode[] = {
1598         { BER_BVC( "ignore" ),          MEMBEROF_NONE },
1599         { BER_BVC( "drop" ),            MEMBEROF_FDANGLING_DROP },
1600         { BER_BVC( "error" ),           MEMBEROF_FDANGLING_ERROR },
1601         { BER_BVNULL,                   0 }
1602 };
1603
1604 static int
1605 memberof_make_group_filter( memberof_t *mo )
1606 {
1607         char            *ptr;
1608
1609         if ( !BER_BVISNULL( &mo->mo_groupFilterstr ) ) {
1610                 ch_free( mo->mo_groupFilterstr.bv_val );
1611         }
1612
1613         mo->mo_groupFilter.f_choice = LDAP_FILTER_EQUALITY;
1614         mo->mo_groupFilter.f_ava = &mo->mo_groupAVA;
1615         
1616         mo->mo_groupFilter.f_av_desc = slap_schema.si_ad_objectClass;
1617         mo->mo_groupFilter.f_av_value = mo->mo_oc_group->soc_cname;
1618
1619         mo->mo_groupFilterstr.bv_len = STRLENOF( "(=)" )
1620                 + slap_schema.si_ad_objectClass->ad_cname.bv_len
1621                 + mo->mo_oc_group->soc_cname.bv_len;
1622         ptr = mo->mo_groupFilterstr.bv_val = ch_malloc( mo->mo_groupFilterstr.bv_len + 1 );
1623         *ptr++ = '(';
1624         ptr = lutil_strcopy( ptr, slap_schema.si_ad_objectClass->ad_cname.bv_val );
1625         *ptr++ = '=';
1626         ptr = lutil_strcopy( ptr, mo->mo_oc_group->soc_cname.bv_val );
1627         *ptr++ = ')';
1628         *ptr = '\0';
1629
1630         return 0;
1631 }
1632
1633 static int
1634 memberof_make_member_filter( memberof_t *mo )
1635 {
1636         char            *ptr;
1637
1638         if ( !BER_BVISNULL( &mo->mo_memberFilterstr ) ) {
1639                 ch_free( mo->mo_memberFilterstr.bv_val );
1640         }
1641
1642         mo->mo_memberFilter.f_choice = LDAP_FILTER_PRESENT;
1643         mo->mo_memberFilter.f_desc = mo->mo_ad_memberof;
1644
1645         mo->mo_memberFilterstr.bv_len = STRLENOF( "(=*)" )
1646                 + mo->mo_ad_memberof->ad_cname.bv_len;
1647         ptr = mo->mo_memberFilterstr.bv_val = ch_malloc( mo->mo_memberFilterstr.bv_len + 1 );
1648         *ptr++ = '(';
1649         ptr = lutil_strcopy( ptr, mo->mo_ad_memberof->ad_cname.bv_val );
1650         ptr = lutil_strcopy( ptr, "=*)" );
1651
1652         return 0;
1653 }
1654
1655 static int
1656 mo_cf_gen( ConfigArgs *c )
1657 {
1658         slap_overinst   *on = (slap_overinst *)c->bi;
1659         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1660
1661         int             i, rc = 0;
1662
1663         if ( c->op == SLAP_CONFIG_EMIT ) {
1664                 struct berval bv = BER_BVNULL;
1665
1666                 switch( c->type ) {
1667                 case MO_DN:
1668                         if ( mo->mo_dn.bv_val != NULL) {
1669                                 value_add_one( &c->rvalue_vals, &mo->mo_dn );
1670                                 value_add_one( &c->rvalue_nvals, &mo->mo_ndn );
1671                         }
1672                         break;
1673
1674                 case MO_DANGLING:
1675                         enum_to_verb( dangling_mode, (mo->mo_flags & MEMBEROF_FDANGLING_MASK), &bv );
1676                         if ( BER_BVISNULL( &bv ) ) {
1677                                 /* there's something wrong... */
1678                                 assert( 0 );
1679                                 rc = 1;
1680
1681                         } else {
1682                                 value_add_one( &c->rvalue_vals, &bv );
1683                         }
1684                         break;
1685
1686                 case MO_REFINT:
1687                         c->value_int = MEMBEROF_REFINT( mo );
1688                         break;
1689
1690 #if 0
1691                 case MO_REVERSE:
1692                         c->value_int = MEMBEROF_REVERSE( mo );
1693                         break;
1694 #endif
1695
1696                 case MO_GROUP_OC:
1697                         if ( mo->mo_oc_group != NULL ){
1698                                 value_add_one( &c->rvalue_vals, &mo->mo_oc_group->soc_cname );
1699                         }
1700                         break;
1701
1702                 case MO_MEMBER_AD:
1703                         if ( mo->mo_ad_member != NULL ){
1704                                 value_add_one( &c->rvalue_vals, &mo->mo_ad_member->ad_cname );
1705                         }
1706                         break;
1707
1708                 case MO_MEMBER_OF_AD:
1709                         if ( mo->mo_ad_memberof != NULL ){
1710                                 value_add_one( &c->rvalue_vals, &mo->mo_ad_memberof->ad_cname );
1711                         }
1712                         break;
1713
1714                 default:
1715                         assert( 0 );
1716                         return 1;
1717                 }
1718
1719                 return rc;
1720
1721         } else if ( c->op == LDAP_MOD_DELETE ) {
1722                 return 1;       /* FIXME */
1723
1724         } else {
1725                 switch( c->type ) {
1726                 case MO_DN:
1727                         if ( !BER_BVISNULL( &mo->mo_dn ) ) {
1728                                 ber_memfree( mo->mo_dn.bv_val );
1729                                 ber_memfree( mo->mo_ndn.bv_val );
1730                         }
1731                         mo->mo_dn = c->value_dn;
1732                         mo->mo_ndn = c->value_ndn;
1733                         break;
1734
1735                 case MO_DANGLING:
1736                         i = verb_to_mask( c->argv[ 1 ], dangling_mode );
1737                         if ( BER_BVISNULL( &dangling_mode[ i ].word ) ) {
1738                                 return 1;
1739                         }
1740
1741                         mo->mo_flags &= ~MEMBEROF_FDANGLING_MASK;
1742                         mo->mo_flags |= dangling_mode[ i ].mask;
1743                         break;
1744
1745                 case MO_REFINT:
1746                         if ( c->value_int ) {
1747                                 mo->mo_flags |= MEMBEROF_FREFINT;
1748
1749                         } else {
1750                                 mo->mo_flags &= ~MEMBEROF_FREFINT;
1751                         }
1752                         break;
1753
1754 #if 0
1755                 case MO_REVERSE:
1756                         if ( c->value_int ) {
1757                                 mo->mo_flags |= MEMBEROF_FREVERSE;
1758
1759                         } else {
1760                                 mo->mo_flags &= ~MEMBEROF_FREVERSE;
1761                         }
1762                         break;
1763 #endif
1764
1765                 case MO_GROUP_OC: {
1766                         ObjectClass     *oc = NULL;
1767
1768                         oc = oc_find( c->argv[ 1 ] );
1769                         if ( oc == NULL ) {
1770                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1771                                         "unable to find group objectClass=\"%s\"",
1772                                         c->argv[ 1 ] );
1773                                 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n",
1774                                         c->log, c->cr_msg, 0 );
1775                                 return 1;
1776                         }
1777
1778                         mo->mo_oc_group = oc;
1779                         memberof_make_group_filter( mo );
1780                         } break;
1781
1782                 case MO_MEMBER_AD: {
1783                         AttributeDescription    *ad = NULL;
1784                         const char              *text = NULL;
1785
1786
1787                         rc = slap_str2ad( c->argv[ 1 ], &ad, &text );
1788                         if ( rc != LDAP_SUCCESS ) {
1789                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1790                                         "unable to find member attribute=\"%s\": %s (%d)",
1791                                         c->argv[ 1 ], text, rc );
1792                                 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n",
1793                                         c->log, c->cr_msg, 0 );
1794                                 return 1;
1795                         }
1796
1797                         if ( !is_at_syntax( ad->ad_type, SLAPD_DN_SYNTAX )              /* e.g. "member" */
1798                                 && !is_at_syntax( ad->ad_type, SLAPD_NAMEUID_SYNTAX ) ) /* e.g. "uniqueMember" */
1799                         {
1800                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1801                                         "member attribute=\"%s\" must either "
1802                                         "have DN (%s) or nameUID (%s) syntax",
1803                                         c->argv[ 1 ], SLAPD_DN_SYNTAX, SLAPD_NAMEUID_SYNTAX );
1804                                 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n",
1805                                         c->log, c->cr_msg, 0 );
1806                                 return 1;
1807                         }
1808
1809                         mo->mo_ad_member = ad;
1810                         } break;
1811
1812                 case MO_MEMBER_OF_AD: {
1813                         AttributeDescription    *ad = NULL;
1814                         const char              *text = NULL;
1815
1816
1817                         rc = slap_str2ad( c->argv[ 1 ], &ad, &text );
1818                         if ( rc != LDAP_SUCCESS ) {
1819                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1820                                         "unable to find memberof attribute=\"%s\": %s (%d)",
1821                                         c->argv[ 1 ], text, rc );
1822                                 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n",
1823                                         c->log, c->cr_msg, 0 );
1824                                 return 1;
1825                         }
1826
1827                         if ( !is_at_syntax( ad->ad_type, SLAPD_DN_SYNTAX )              /* e.g. "member" */
1828                                 && !is_at_syntax( ad->ad_type, SLAPD_NAMEUID_SYNTAX ) ) /* e.g. "uniqueMember" */
1829                         {
1830                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1831                                         "memberof attribute=\"%s\" must either "
1832                                         "have DN (%s) or nameUID (%s) syntax",
1833                                         c->argv[ 1 ], SLAPD_DN_SYNTAX, SLAPD_NAMEUID_SYNTAX );
1834                                 Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n",
1835                                         c->log, c->cr_msg, 0 );
1836                                 return 1;
1837                         }
1838
1839                         mo->mo_ad_memberof = ad;
1840                         memberof_make_member_filter( mo );
1841                         } break;
1842
1843                 default:
1844                         assert( 0 );
1845                         return 1;
1846                 }
1847         }
1848
1849         return 0;
1850 }
1851
1852 static int
1853 memberof_db_open(
1854         BackendDB       *be,
1855         ConfigReply     *cr )
1856 {
1857         slap_overinst   *on = (slap_overinst *)be->bd_info;
1858         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1859         
1860         int             rc;
1861         const char      *text = NULL;
1862
1863         if( ! mo->mo_ad_memberof ){
1864                 rc = slap_str2ad( SLAPD_MEMBEROF_ATTR, &mo->mo_ad_memberof, &text );
1865                 if ( rc != LDAP_SUCCESS ) {
1866                         Debug( LDAP_DEBUG_ANY, "memberof_db_open: "
1867                                         "unable to find attribute=\"%s\": %s (%d)\n",
1868                                         SLAPD_MEMBEROF_ATTR, text, rc );
1869                         return rc;
1870                 }
1871         }
1872
1873         if( ! mo->mo_ad_member ){
1874                 rc = slap_str2ad( SLAPD_GROUP_ATTR, &mo->mo_ad_member, &text );
1875                 if ( rc != LDAP_SUCCESS ) {
1876                         Debug( LDAP_DEBUG_ANY, "memberof_db_open: "
1877                                         "unable to find attribute=\"%s\": %s (%d)\n",
1878                                         SLAPD_GROUP_ATTR, text, rc );
1879                         return rc;
1880                 }
1881         }
1882
1883     if( ! mo->mo_oc_group ){
1884                 mo->mo_oc_group = oc_find( SLAPD_GROUP_CLASS );
1885                 if ( mo->mo_oc_group == NULL ) {
1886                         Debug( LDAP_DEBUG_ANY,
1887                                         "memberof_db_open: "
1888                                         "unable to find objectClass=\"%s\"\n",
1889                                         SLAPD_GROUP_CLASS, 0, 0 );
1890                         return 1;
1891                 }
1892         }
1893
1894         if ( BER_BVISNULL( &mo->mo_dn ) ) {
1895                 ber_dupbv( &mo->mo_dn, &be->be_rootdn );
1896                 ber_dupbv( &mo->mo_ndn, &be->be_rootndn );
1897         }
1898
1899         if ( BER_BVISNULL( &mo->mo_groupFilterstr ) ) {
1900                 memberof_make_group_filter( mo );
1901         }
1902
1903         if ( BER_BVISNULL( &mo->mo_memberFilterstr ) ) {
1904                 memberof_make_member_filter( mo );
1905         }
1906
1907         return 0;
1908 }
1909
1910 static int
1911 memberof_db_destroy(
1912         BackendDB       *be,
1913         ConfigReply     *cr )
1914 {
1915         slap_overinst   *on = (slap_overinst *)be->bd_info;
1916         memberof_t      *mo = (memberof_t *)on->on_bi.bi_private;
1917
1918         if ( mo ) {
1919                 if ( !BER_BVISNULL( &mo->mo_dn ) ) {
1920                         ber_memfree( mo->mo_dn.bv_val );
1921                         ber_memfree( mo->mo_ndn.bv_val );
1922                 }
1923
1924                 if ( !BER_BVISNULL( &mo->mo_groupFilterstr ) ) {
1925                         ber_memfree( mo->mo_groupFilterstr.bv_val );
1926                 }
1927
1928                 if ( !BER_BVISNULL( &mo->mo_memberFilterstr ) ) {
1929                         ber_memfree( mo->mo_memberFilterstr.bv_val );
1930                 }
1931
1932                 ber_memfree( mo );
1933         }
1934
1935         return 0;
1936 }
1937
1938 /* unused */
1939 static AttributeDescription     *ad_memberOf;
1940
1941 static struct {
1942         char    *desc;
1943         AttributeDescription **adp;
1944 } as[] = {
1945         { "( 1.2.840.113556.1.2.102 "
1946                 "NAME 'memberOf' "
1947                 "DESC 'Group that the entry belongs to' "
1948                 "SYNTAX '1.3.6.1.4.1.1466.115.121.1.12' "
1949                 "EQUALITY distinguishedNameMatch "      /* added */
1950                 "USAGE dSAOperation "                   /* added; questioned */
1951                 /* "NO-USER-MODIFICATION " */           /* add? */
1952                 "X-ORIGIN 'iPlanet Delegated Administrator' )",
1953                 &ad_memberOf },
1954         { NULL }
1955 };
1956
1957 #if SLAPD_OVER_MEMBEROF == SLAPD_MOD_DYNAMIC
1958 static
1959 #endif /* SLAPD_OVER_MEMBEROF == SLAPD_MOD_DYNAMIC */
1960 int
1961 memberof_initialize( void )
1962 {
1963         int                     code, i;
1964
1965         for ( i = 0; as[ i ].desc != NULL; i++ ) {
1966                 code = register_at( as[ i ].desc, as[ i ].adp, 0 );
1967                 if ( code ) {
1968                         Debug( LDAP_DEBUG_ANY,
1969                                 "memberof_initialize: register_at #%d failed\n",
1970                                 i, 0, 0 );
1971                         return code;
1972                 }
1973         }
1974
1975         memberof.on_bi.bi_type = "memberof";
1976
1977         memberof.on_bi.bi_db_init = memberof_db_init;
1978         memberof.on_bi.bi_db_open = memberof_db_open;
1979         memberof.on_bi.bi_db_destroy = memberof_db_destroy;
1980
1981         memberof.on_bi.bi_op_add = memberof_op_add;
1982         memberof.on_bi.bi_op_delete = memberof_op_delete;
1983         memberof.on_bi.bi_op_modify = memberof_op_modify;
1984
1985         memberof.on_response = memberof_response;
1986
1987         memberof.on_bi.bi_cf_ocs = mo_ocs;
1988
1989         code = config_register_schema( mo_cfg, mo_ocs );
1990         if ( code ) return code;
1991
1992         return overlay_register( &memberof );
1993 }
1994
1995 #if SLAPD_OVER_MEMBEROF == SLAPD_MOD_DYNAMIC
1996 int
1997 init_module( int argc, char *argv[] )
1998 {
1999         return memberof_initialize();
2000 }
2001 #endif /* SLAPD_OVER_MEMBEROF == SLAPD_MOD_DYNAMIC */
2002
2003 #endif /* SLAPD_OVER_MEMBEROF */