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