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