Debian Codegraph @

servers/slapd/overlays/memberof.c

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