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