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