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