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