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