Debian Codegraph @

servers/slapd/overlays/nestgroup.c

raw
1 /* nestgroup.c - nested group overlay */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2024 The OpenLDAP Foundation.
6 * Copyright 2024 by Howard Chu.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Howard Chu for inclusion in
19 * OpenLDAP Software.
20 */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_NESTGROUP
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "lutil.h"
32 #include "slap.h"
33 #include "slap-config.h"
34
35 /* This overlay dynamically constructs member and memberOf attributes
36 * for nested groups.
37 */
38
39 #define SLAPD_MEMBEROF_ATTR "memberOf"
40
41 #define NG_MBR_VALUES 0x01
42 #define NG_MBR_FILTER 0x02
43 #define NG_MOF_VALUES 0x04
44 #define NG_MOF_FILTER 0x08
45 #define NG_NEGATED 0x10
46
47 static AttributeDescription *ad_member;
48 static AttributeDescription *ad_memberOf;
49
50 static slap_verbmasks nestgroup_flags[] = {
51 { BER_BVC("member-values"), NG_MBR_VALUES },
52 { BER_BVC("member-filter"), NG_MBR_FILTER },
53 { BER_BVC("memberof-values"), NG_MOF_VALUES },
54 { BER_BVC("memberof-filter"), NG_MOF_FILTER },
55 { BER_BVNULL, 0 }
56 };
57
58 enum {
59 NG_MEMBER = 1,
60 NG_MEMBEROF,
61 NG_GROUPBASE,
62 NG_FLAGS
63 };
64
65 typedef struct nestgroup_info_t {
66 AttributeDescription *ngi_member;
67 AttributeDescription *ngi_memberOf;
68 BerVarray ngi_groupBase;
69 BerVarray ngi_ngroupBase;
70 int ngi_flags;
71 } nestgroup_info_t;
72
73 static int ngroup_cf( ConfigArgs *c )
74 {
75 slap_overinst *on = (slap_overinst *)c->bi;
76 nestgroup_info_t *ngi = (nestgroup_info_t *)on->on_bi.bi_private;
77 int rc = 1;
78
79 if ( c->op == SLAP_CONFIG_EMIT ) {
80 switch( c->type ) {
81 case NG_MEMBER:
82 if ( ngi->ngi_member ) {
83 value_add_one( &c->rvalue_vals, &ngi->ngi_member->ad_cname );
84 rc = 0;
85 }
86 break;
87 case NG_MEMBEROF:
88 if ( ngi->ngi_memberOf ) {
89 value_add_one( &c->rvalue_vals, &ngi->ngi_memberOf->ad_cname );
90 rc = 0;
91 }
92 break;
93 case NG_GROUPBASE:
94 if ( ngi->ngi_groupBase ) {
95 value_add( &c->rvalue_vals, ngi->ngi_groupBase );
96 value_add( &c->rvalue_nvals, ngi->ngi_ngroupBase );
97 rc = 0;
98 }
99 break;
100 case NG_FLAGS:
101 return mask_to_verbs( nestgroup_flags, ngi->ngi_flags, &c->rvalue_vals );
102 default:
103 break;
104 }
105 return rc;
106 } else if ( c->op == LDAP_MOD_DELETE ) {
107 switch( c->type ) {
108 case NG_MEMBER:
109 ngi->ngi_member = ad_member;
110 rc = 0;
111 break;
112 case NG_MEMBEROF:
113 ngi->ngi_memberOf = ad_memberOf;
114 rc = 0;
115 break;
116 case NG_GROUPBASE:
117 if ( c->valx < 0 ) {
118 ber_bvarray_free( ngi->ngi_groupBase );
119 ber_bvarray_free( ngi->ngi_ngroupBase );
120 ngi->ngi_groupBase = NULL;
121 ngi->ngi_ngroupBase = NULL;
122 } else {
123 int i = c->valx;
124 ch_free( ngi->ngi_groupBase[i].bv_val );
125 ch_free( ngi->ngi_ngroupBase[i].bv_val );
126 do {
127 ngi->ngi_groupBase[i] = ngi->ngi_groupBase[i+1];
128 ngi->ngi_ngroupBase[i] = ngi->ngi_ngroupBase[i+1];
129 i++;
130 } while ( !BER_BVISNULL( &ngi->ngi_groupBase[i] ));
131 }
132 rc = 0;
133 break;
134 case NG_FLAGS:
135 if ( !c->line ) {
136 ngi->ngi_flags = 0;
137 } else {
138 int i = verb_to_mask( c->line, nestgroup_flags );
139 ngi->ngi_flags &= ~nestgroup_flags[i].mask;
140 }
141 rc = 0;
142 break;
143 default:
144 break;
145 }
146 return rc;
147 }
148
149 switch( c->type ) {
150 case NG_MEMBER:
151 if ( !is_at_syntax( c->value_ad->ad_type, SLAPD_DN_SYNTAX ) &&
152 !is_at_syntax( c->value_ad->ad_type, SLAPD_NAMEUID_SYNTAX )) {
153 snprintf( c->cr_msg, sizeof( c->cr_msg ),
154 "member attribute=\"%s\" must use DN (%s) or NAMEUID (%s) syntax",
155 c->argv[1], SLAPD_DN_SYNTAX, SLAPD_NAMEUID_SYNTAX );
156 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
157 "%s: %s\n", c->log, c->cr_msg );
158 return ARG_BAD_CONF;
159 }
160 ngi->ngi_member = c->value_ad;
161 rc = 0;
162 break;
163 case NG_MEMBEROF:
164 if ( !is_at_syntax( c->value_ad->ad_type, SLAPD_DN_SYNTAX ) &&
165 !is_at_syntax( c->value_ad->ad_type, SLAPD_NAMEUID_SYNTAX )) {
166 snprintf( c->cr_msg, sizeof( c->cr_msg ),
167 "memberOf attribute=\"%s\" must use DN (%s) or NAMEUID (%s) syntax",
168 c->argv[1], SLAPD_DN_SYNTAX, SLAPD_NAMEUID_SYNTAX );
169 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
170 "%s: %s\n", c->log, c->cr_msg );
171 return ARG_BAD_CONF;
172 }
173 ngi->ngi_memberOf = c->value_ad;
174 rc = 0;
175 break;
176 case NG_GROUPBASE:
177 ber_bvarray_add( &ngi->ngi_groupBase, &c->value_dn );
178 ber_bvarray_add( &ngi->ngi_ngroupBase, &c->value_ndn );
179 rc = 0;
180 break;
181 case NG_FLAGS: {
182 slap_mask_t flags = 0;
183 int i;
184 if ( c->op != SLAP_CONFIG_ADD && c->argc > 2 ) {
185 /* We wouldn't know how to delete these values later */
186 snprintf( c->cr_msg, sizeof( c->cr_msg ),
187 "Please insert multiple names as separate %s values",
188 c->argv[0] );
189 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
190 "%s: %s\n", c->log, c->cr_msg );
191 rc = LDAP_INVALID_SYNTAX;
192 break;
193 }
194 i = verbs_to_mask( c->argc, c->argv, nestgroup_flags, &flags );
195 if ( i ) {
196 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown option", c->argv[0] );
197 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
198 c->log, c->cr_msg, c->argv[i]);
199 return(1);
200 }
201 ngi->ngi_flags |= flags;
202 rc = 0;
203 break; }
204 default:
205 break;
206 }
207
208 return rc;
209 }
210
211 static ConfigTable ngroupcfg[] = {
212 { "nestgroup-member", "member-ad", 2, 2, 0,
213 ARG_MAGIC|ARG_ATDESC|NG_MEMBER, ngroup_cf,
214 "( OLcfgOvAt:25.1 NAME 'olcNestGroupMember' "
215 "EQUALITY caseIgnoreMatch "
216 "DESC 'Member attribute' "
217 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
218 { "nestgroup-memberof", "memberOf-ad", 2, 2, 0,
219 ARG_MAGIC|ARG_ATDESC|NG_MEMBEROF, ngroup_cf,
220 "( OLcfgOvAt:25.2 NAME 'olcNestGroupMemberOf' "
221 "EQUALITY caseIgnoreMatch "
222 "DESC 'MemberOf attribute' "
223 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
224 { "nestgroup-base", "dn", 2, 2, 0,
225 ARG_DN|ARG_QUOTE|ARG_MAGIC|NG_GROUPBASE, ngroup_cf,
226 "( OLcfgOvAt:25.3 NAME 'olcNestGroupBase' "
227 "EQUALITY distinguishedNameMatch "
228 "DESC 'Base[s] of group subtree[s]' "
229 "SYNTAX OMsDN )", NULL, NULL },
230 { "nestgroup-flags", "options", 2, 0, 0,
231 ARG_MAGIC|NG_FLAGS, ngroup_cf,
232 "( OLcfgOvAt:25.4 NAME 'olcNestGroupFlags' "
233 "EQUALITY caseIgnoreMatch "
234 "DESC 'Features to use' "
235 "SYNTAX OMsDirectoryString )", NULL, NULL },
236 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
237 };
238
239 static ConfigOCs ngroupocs[] = {
240 { "( OLcfgOvOc:25.1 "
241 "NAME 'olcNestGroupConfig' "
242 "DESC 'Nested Group configuration' "
243 "SUP olcOverlayConfig "
244 "MAY ( olcNestGroupMember $ olcNestGroupMemberOf $ "
245 " olcNestGroupBase $ olcNestGroupFlags ) ) ",
246 Cft_Overlay, ngroupcfg },
247 { NULL, 0, NULL }
248 };
249
250 typedef struct nestgroup_filterinst_t {
251 Filter *nf_f;
252 Filter *nf_new;
253 Entry *nf_e;
254 } nestgroup_filterinst_t;
255
256 /* Record occurrences of ad in filter. Ignore in negated filters. */
257 static void
258 nestgroup_filter_instances( Operation *op, AttributeDescription *ad, Filter *f, int not,
259 int *nfn, nestgroup_filterinst_t **nfp, int *negated )
260 {
261 if ( !f )
262 return;
263
264 switch( f->f_choice & SLAPD_FILTER_MASK ) {
265 case LDAP_FILTER_EQUALITY:
266 if ( f->f_av_desc == ad ) {
267 if ( not ) {
268 *negated = 1;
269 } else {
270 nestgroup_filterinst_t *nf = *nfp;
271 int n = *nfn;
272 nf = op->o_tmprealloc( nf, (n + 1) * sizeof(nestgroup_filterinst_t), op->o_tmpmemctx );
273 nf[n].nf_f = f;
274 nf[n].nf_new = NULL;
275 nf[n++].nf_e = NULL;
276 *nfp = nf;
277 *nfn = n;
278 }
279 }
280 break;
281 case SLAPD_FILTER_COMPUTED:
282 case LDAP_FILTER_PRESENT:
283 case LDAP_FILTER_GE:
284 case LDAP_FILTER_LE:
285 case LDAP_FILTER_APPROX:
286 case LDAP_FILTER_SUBSTRINGS:
287 case LDAP_FILTER_EXT:
288 break;
289 case LDAP_FILTER_NOT: not ^= 1;
290 /* FALLTHRU */
291 case LDAP_FILTER_AND:
292 case LDAP_FILTER_OR:
293 for ( f = f->f_list; f; f = f->f_next )
294 nestgroup_filter_instances( op, ad, f, not, nfn, nfp, negated );
295 }
296 }
297
298 static int
299 nestgroup_check_needed( Operation *op, int attrflags, AttributeDescription *ad )
300 {
301 if ( is_at_operational( ad->ad_type )) {
302 if ( SLAP_OPATTRS( attrflags ))
303 return 1;
304 } else {
305 if ( SLAP_USERATTRS( attrflags ))
306 return 1;
307 }
308 return ( ad_inlist( ad, op->ors_attrs ));
309 }
310
311 typedef struct DNpair {
312 struct berval dp_ndn;
313 struct berval dp_dn;
314 struct DNpair *dp_next;
315 int dp_flag;
316 } DNpair;
317
318 typedef struct gdn_info {
319 TAvlnode *gi_DNs;
320 DNpair *gi_DNlist;
321 nestgroup_info_t *gi_ngi;
322 int gi_numDNs;
323 int gi_saveDN;
324 Attribute *gi_merge;
325 } gdn_info;
326
327 static int
328 nestgroup_dncmp( const void *v1, const void *v2 )
329 {
330 return ber_bvcmp((const struct berval *)v1, (const struct berval *)v2);
331 }
332
333 static int
334 nestgroup_gotDNresp( Operation *op, SlapReply *rs )
335 {
336 if ( rs->sr_type == REP_SEARCH ) {
337 gdn_info *gi = (gdn_info *)(op->o_callback+1);
338 DNpair *dp = op->o_tmpalloc( sizeof(DNpair), op->o_tmpmemctx );
339 dp->dp_ndn = rs->sr_entry->e_nname;
340 if ( ldap_tavl_insert( &gi->gi_DNs, dp, nestgroup_dncmp, ldap_avl_dup_error )) {
341 op->o_tmpfree( dp, op->o_tmpmemctx );
342 } else {
343 ber_dupbv_x( &dp->dp_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
344 if ( gi->gi_saveDN )
345 ber_dupbv_x( &dp->dp_dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
346 gi->gi_numDNs++;
347 dp->dp_next = gi->gi_DNlist;
348 dp->dp_flag = 0;
349 gi->gi_DNlist = dp;
350 }
351 }
352 return 0;
353 }
354
355 static void
356 nestgroup_get_parentDNs( Operation *op, struct berval *ndn )
357 {
358 SlapReply r = { REP_SEARCH };
359 gdn_info *gi = (gdn_info *)(op->o_callback+1);
360 nestgroup_info_t *ngi = gi->gi_ngi;
361 int i;
362
363 op->ors_filter->f_av_value = *ndn;
364 for ( i=0; !BER_BVISEMPTY( &ngi->ngi_ngroupBase[i] ); i++ ) {
365 op->o_req_dn = ngi->ngi_groupBase[i];
366 op->o_req_ndn = ngi->ngi_ngroupBase[i];
367 op->o_bd->be_search( op, &r );
368 }
369 gi->gi_numDNs = 0; /* ignore first count, that's just the original member= result set */
370
371 while ( gi->gi_DNlist ) {
372 int prevnum;
373 DNpair *dp = gi->gi_DNlist;
374 gi->gi_DNlist = NULL;
375 for ( ; dp; dp=dp->dp_next ) {
376 op->ors_filter->f_av_value = dp->dp_ndn;
377 prevnum = gi->gi_numDNs;
378 for ( i=0; !BER_BVISEMPTY( &ngi->ngi_ngroupBase[i] ); i++ ) {
379 op->o_req_dn = ngi->ngi_groupBase[i];
380 op->o_req_ndn = ngi->ngi_ngroupBase[i];
381 op->o_bd->be_search( op, &r );
382 }
383 if ( gi->gi_numDNs > prevnum )
384 dp->dp_flag = 1; /* this group had a parent */
385 }
386 }
387 }
388
389 static void
390 nestgroup_memberFilter( Operation *op, int mbr_nf, nestgroup_filterinst_t *mbr_f )
391 {
392 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
393 nestgroup_info_t *ngi = on->on_bi.bi_private;
394 AttributeDescription *ad = mbr_f[0].nf_f->f_av_desc;
395 slap_callback *sc;
396 gdn_info *gi;
397 Filter mf;
398 AttributeAssertion mava;
399 Operation o = *op;
400 int i;
401
402 o.o_managedsait = SLAP_CONTROL_CRITICAL;
403 sc = op->o_tmpcalloc( 1, sizeof(slap_callback) + sizeof(gdn_info), op->o_tmpmemctx);
404 gi = (gdn_info *)(sc+1);
405 gi->gi_ngi = ngi;
406 o.o_callback = sc;
407 sc->sc_response = nestgroup_gotDNresp;
408 o.ors_attrs = slap_anlist_no_attrs;
409
410 mf.f_choice = LDAP_FILTER_EQUALITY;
411 mf.f_ava = &mava;
412 mf.f_av_desc = ad;
413 mf.f_next = NULL;
414
415 o.ors_scope = LDAP_SCOPE_SUBTREE;
416 o.ors_deref = LDAP_DEREF_NEVER;
417 o.ors_limit = NULL;
418 o.ors_tlimit = SLAP_NO_LIMIT;
419 o.ors_slimit = SLAP_NO_LIMIT;
420 o.ors_filter = &mf;
421 o.o_bd->bd_info = (BackendInfo *)on->on_info;
422
423 for ( i=0; i<mbr_nf; i++ ) {
424 gi->gi_DNs = NULL;
425 gi->gi_numDNs = 0;
426 nestgroup_get_parentDNs( &o, &mbr_f[i].nf_f->f_av_value );
427 if ( gi->gi_numDNs ) {
428 int j;
429 Filter *f, *nf;
430 TAvlnode *t;
431 DNpair *dp;
432
433 f = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
434 f->f_next = NULL;
435 t = ldap_tavl_end( gi->gi_DNs, TAVL_DIR_RIGHT );
436 do {
437 dp = t->avl_data;
438 if ( dp->dp_flag ) {
439 nf = f;
440 nf->f_ava = op->o_tmpcalloc( 1, sizeof( AttributeAssertion ), op->o_tmpmemctx );
441 nf->f_choice = LDAP_FILTER_EQUALITY;
442 nf->f_av_desc = ad;
443 nf->f_av_value = dp->dp_ndn;
444 f = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
445 f->f_next = nf;
446 }
447 t = ldap_tavl_next( t, TAVL_DIR_LEFT );
448 op->o_tmpfree( dp, op->o_tmpmemctx );
449 } while ( t );
450 f->f_choice = LDAP_FILTER_EQUALITY;
451 f->f_ava = mbr_f[i].nf_f->f_ava;
452 mbr_f[i].nf_new = f;
453 }
454 ldap_tavl_free( gi->gi_DNs, NULL );
455 }
456 o.o_bd->bd_info = (BackendInfo *)on->on_info;
457 op->o_tmpfree( sc, op->o_tmpmemctx );
458 }
459
460 static void
461 nestgroup_addUnique( Operation *op, Attribute *old, Attribute *new )
462 {
463 /* strip out any duplicates from new before adding */
464 struct berval *bv, *nbv;
465 int i, j, flags;
466
467 bv = op->o_tmpalloc( (new->a_numvals + 1) * 2 * sizeof(struct berval), op->o_tmpmemctx );
468 nbv = bv + new->a_numvals+1;
469
470 flags = SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH|SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH;
471 for (i=0,j=0; i<new->a_numvals; i++) {
472 int rc = attr_valfind( old, flags, &new->a_nvals[i], NULL, NULL );
473 if ( rc ) {
474 bv[j] = new->a_vals[i];
475 nbv[j++] = new->a_nvals[i];
476 }
477 }
478 BER_BVZERO( &bv[j] );
479 BER_BVZERO( &nbv[j] );
480 attr_valadd( old, bv, nbv, j );
481 op->o_tmpfree( bv, op->o_tmpmemctx );
482 }
483
484 static void
485 nestgroup_get_childDNs( Operation *op, slap_overinst *on, gdn_info *gi, struct berval *ndn )
486 {
487 nestgroup_info_t *ngi = on->on_bi.bi_private;
488 Entry *e;
489 Attribute *a;
490
491 if ( overlay_entry_get_ov( op, ndn, NULL, NULL, 0, &e, on ) != LDAP_SUCCESS || e == NULL )
492 return;
493
494 a = attr_find( e->e_attrs, ngi->ngi_member );
495 if ( a ) {
496 int i, j;
497 for (i = 0; i<a->a_numvals; i++ ) {
498 /* record all group entries */
499 for (j = 0; !BER_BVISEMPTY( &ngi->ngi_groupBase[j] ); j++) {
500 if ( dnIsSuffix( &a->a_nvals[i], &ngi->ngi_ngroupBase[j] )) {
501 DNpair *dp = op->o_tmpalloc( sizeof(DNpair), op->o_tmpmemctx );
502 dp->dp_ndn = a->a_nvals[i];
503 if ( ldap_tavl_insert( &gi->gi_DNs, dp, nestgroup_dncmp, ldap_avl_dup_error )) {
504 op->o_tmpfree( dp, op->o_tmpmemctx );
505 } else {
506 ber_dupbv_x( &dp->dp_ndn, &a->a_nvals[i], op->o_tmpmemctx );
507 gi->gi_numDNs++;
508 dp->dp_next = gi->gi_DNlist;
509 gi->gi_DNlist = dp;
510 }
511 break;
512 }
513 }
514 }
515 if ( gi->gi_merge ) {
516 nestgroup_addUnique( op, gi->gi_merge, a );
517 }
518 }
519 overlay_entry_release_ov( op, e, 0, on );
520 }
521
522 static void
523 nestgroup_memberOfFilter( Operation *op, int mof_nf, nestgroup_filterinst_t *mof_f )
524 {
525 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
526 AttributeDescription *ad = mof_f[0].nf_f->f_av_desc;
527 gdn_info gi = {0};
528 int i;
529
530 for ( i=0; i<mof_nf; i++ ) {
531 gi.gi_DNs = NULL;
532 gi.gi_numDNs = 0;
533 nestgroup_get_childDNs( op, on, &gi, &mof_f[i].nf_f->f_av_value );
534
535 while ( gi.gi_DNlist ) {
536 DNpair *dp = gi.gi_DNlist;
537 gi.gi_DNlist = NULL;
538 for ( ; dp; dp=dp->dp_next ) {
539 nestgroup_get_childDNs( op, on, &gi, &dp->dp_ndn );
540 }
541 }
542
543 if ( gi.gi_numDNs ) {
544 int j;
545 Filter *f, *nf;
546 TAvlnode *t;
547 DNpair *dp;
548
549 f = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
550 f->f_next = NULL;
551 t = ldap_tavl_end( gi.gi_DNs, TAVL_DIR_RIGHT );
552 do {
553 dp = t->avl_data;
554 nf = f;
555 nf->f_ava = op->o_tmpcalloc( 1, sizeof( AttributeAssertion ), op->o_tmpmemctx );
556 nf->f_choice = LDAP_FILTER_EQUALITY;
557 nf->f_av_desc = ad;
558 nf->f_av_value = dp->dp_ndn;
559 f = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
560 f->f_next = nf;
561 t = ldap_tavl_next( t, TAVL_DIR_LEFT );
562 op->o_tmpfree( dp, op->o_tmpmemctx );
563 } while ( t );
564 ldap_tavl_free( gi.gi_DNs, NULL );
565 f->f_choice = LDAP_FILTER_EQUALITY;
566 f->f_ava = mof_f[i].nf_f->f_ava;
567 mof_f[i].nf_new = f;
568 }
569 }
570 }
571
572 static void
573 nestgroup_memberOfVals( Operation *op, slap_overinst *on, Attribute *a )
574 {
575 nestgroup_info_t *ngi = on->on_bi.bi_private;
576 Operation o = *op;
577 slap_callback *sc;
578 gdn_info *gi;
579 Filter mf;
580 AttributeAssertion mava;
581 int i;
582
583 o.o_managedsait = SLAP_CONTROL_CRITICAL;
584 sc = op->o_tmpcalloc( 1, sizeof(slap_callback) + sizeof(gdn_info), op->o_tmpmemctx);
585 gi = (gdn_info *)(sc+1);
586 gi->gi_ngi = ngi;
587 o.o_callback = sc;
588 sc->sc_response = nestgroup_gotDNresp;
589 o.ors_attrs = slap_anlist_no_attrs;
590
591 mf.f_choice = LDAP_FILTER_EQUALITY;
592 mf.f_ava = &mava;
593 mf.f_av_desc = ngi->ngi_member;
594 mf.f_next = NULL;
595
596 o.ors_filter = &mf;
597 o.ors_scope = LDAP_SCOPE_SUBTREE;
598 o.ors_deref = LDAP_DEREF_NEVER;
599 o.ors_limit = NULL;
600 o.ors_tlimit = SLAP_NO_LIMIT;
601 o.ors_slimit = SLAP_NO_LIMIT;
602 o.o_bd->bd_info = (BackendInfo *)on->on_info;
603 gi->gi_saveDN = 1;
604
605 for ( i=0; i<a->a_numvals; i++ ) {
606 nestgroup_get_parentDNs( &o, &a->a_nvals[i] );
607
608 while ( gi->gi_DNlist ) {
609 DNpair *dp = gi->gi_DNlist;
610 gi->gi_DNlist = NULL;
611 for ( ; dp; dp=dp->dp_next ) {
612 nestgroup_get_parentDNs( &o, &dp->dp_ndn );
613 }
614 }
615 }
616 if ( gi->gi_DNs ) {
617 TAvlnode *p = ldap_tavl_end( gi->gi_DNs, TAVL_DIR_LEFT );
618 int flags = SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH|SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH;
619 do {
620 DNpair *dp = p->avl_data;
621 int rc = attr_valfind( a, flags, &dp->dp_ndn, NULL, NULL );
622 if ( rc )
623 attr_valadd( a, &dp->dp_dn, &dp->dp_ndn, 1 );
624 op->o_tmpfree( dp->dp_dn.bv_val, op->o_tmpmemctx );
625 op->o_tmpfree( dp->dp_ndn.bv_val, op->o_tmpmemctx );
626 op->o_tmpfree( dp, op->o_tmpmemctx );
627 p = ldap_tavl_next( p, TAVL_DIR_RIGHT );
628 } while ( p );
629 ldap_tavl_free( gi->gi_DNs, NULL );
630 }
631 o.o_bd->bd_info = (BackendInfo *)on->on_info;
632 op->o_tmpfree( sc, op->o_tmpmemctx );
633 }
634
635 typedef struct nestgroup_cbinfo {
636 slap_overinst *nc_on;
637 int nc_needed;
638 } nestgroup_cbinfo;
639
640 static int
641 nestgroup_searchresp( Operation *op, SlapReply *rs )
642 {
643 if (rs->sr_type == REP_SEARCH ) {
644 nestgroup_cbinfo *nc = op->o_callback->sc_private;
645 slap_overinst *on = nc->nc_on;
646 nestgroup_info_t *ngi = on->on_bi.bi_private;
647 Attribute *a;
648
649 if ( nc->nc_needed & NG_MBR_VALUES ) {
650 a = attr_find( rs->sr_entry->e_attrs, ngi->ngi_member );
651 if ( a ) {
652 gdn_info gi = {0};
653 int i, j;
654 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
655 Entry *e = entry_dup( rs->sr_entry );
656 rs_replace_entry( op, rs, on, e );
657 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
658 a = attr_find( e->e_attrs, ngi->ngi_member );
659 }
660 gi.gi_merge = a;
661
662 for ( i=0; i<a->a_numvals; i++ ) {
663 for ( j=0; !BER_BVISEMPTY( &ngi->ngi_ngroupBase[j] ); j++ ) {
664 if ( dnIsSuffix( &a->a_nvals[i], &ngi->ngi_ngroupBase[j] )) {
665 nestgroup_get_childDNs( op, on, &gi, &a->a_nvals[i] );
666
667 while ( gi.gi_DNlist ) {
668 DNpair *dp = gi.gi_DNlist;
669 gi.gi_DNlist = NULL;
670 for ( ; dp; dp=dp->dp_next ) {
671 nestgroup_get_childDNs( op, on, &gi, &dp->dp_ndn );
672 }
673 }
674 break;
675 }
676 }
677 }
678 if ( gi.gi_numDNs ) {
679 TAvlnode *p = ldap_tavl_end( gi.gi_DNs, TAVL_DIR_LEFT );
680 do {
681 DNpair *dp = p->avl_data;
682 op->o_tmpfree( dp->dp_ndn.bv_val, op->o_tmpmemctx );
683 op->o_tmpfree( dp, op->o_tmpmemctx );
684 p = ldap_tavl_next( p, TAVL_DIR_RIGHT );
685 } while ( p );
686 ldap_tavl_free( gi.gi_DNs, NULL );
687 }
688 }
689 }
690
691 if ( nc->nc_needed & NG_MOF_VALUES ) {
692 a = attr_find( rs->sr_entry->e_attrs, ngi->ngi_memberOf );
693 if ( a ) {
694 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE )) {
695 Entry *e = entry_dup( rs->sr_entry );
696 rs_replace_entry( op, rs, on, e );
697 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
698 a = attr_find( e->e_attrs, ngi->ngi_memberOf );
699 }
700 nestgroup_memberOfVals( op, on, a );
701 }
702 }
703 if (( nc->nc_needed & NG_NEGATED ) &&
704 test_filter( op, rs->sr_entry, op->ors_filter ) != LDAP_COMPARE_TRUE )
705 return 0;
706 }
707 return SLAP_CB_CONTINUE;
708 }
709
710 static int
711 nestgroup_op_search( Operation *op, SlapReply *rs )
712 {
713 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
714 nestgroup_info_t *ngi = on->on_bi.bi_private;
715 int mbr_nf = 0, mof_nf = 0, negated = 0;
716 nestgroup_filterinst_t *mbr_f = NULL, *mof_f = NULL;
717
718 if ( get_manageDSAit( op ))
719 return SLAP_CB_CONTINUE;
720
721 /* groupBase must be explicitly configured */
722 if ( !ngi->ngi_ngroupBase )
723 return SLAP_CB_CONTINUE;
724
725 /* handle attrs in filter */
726 if ( ngi->ngi_flags & NG_MBR_FILTER ) {
727 nestgroup_filter_instances( op, ngi->ngi_member, op->ors_filter, 0, &mbr_nf, &mbr_f, &negated );
728 if ( mbr_nf ) {
729 /* find member=(parent groups) */
730 nestgroup_memberFilter( op, mbr_nf, mbr_f );
731 }
732 }
733 if ( ngi->ngi_flags & NG_MOF_FILTER ) {
734 nestgroup_filter_instances( op, ngi->ngi_memberOf, op->ors_filter, 0, &mof_nf, &mof_f, &negated );
735 if ( mof_nf ) {
736 /* find memberOf=(child groups) */
737 nestgroup_memberOfFilter( op, mof_nf, mof_f );
738 }
739 }
740 if ( mbr_nf ) {
741 int i;
742 for ( i=0; i<mbr_nf; i++ ) {
743 if ( mbr_f[i].nf_new ) {
744 mbr_f[i].nf_f->f_choice = LDAP_FILTER_OR;
745 mbr_f[i].nf_f->f_list = mbr_f[i].nf_new;
746 }
747 }
748 op->o_tmpfree( mbr_f, op->o_tmpmemctx );
749 }
750 if ( mof_nf ) {
751 int i;
752 for ( i=0; i<mof_nf; i++ ) {
753 if ( mof_f[i].nf_new ) {
754 mof_f[i].nf_f->f_choice = LDAP_FILTER_OR;
755 mof_f[i].nf_f->f_list = mof_f[i].nf_new;
756 }
757 }
758 op->o_tmpfree( mof_f, op->o_tmpmemctx );
759 }
760
761 if ( ngi->ngi_flags & ( NG_MBR_VALUES|NG_MOF_VALUES )) {
762 /* check for attrs in attrlist */
763 int attrflags = slap_attr_flags( op->ors_attrs );
764 int needed = 0;
765 if (( ngi->ngi_flags & NG_MBR_VALUES ) &&
766 nestgroup_check_needed( op, attrflags, ngi->ngi_member )) {
767 /* collect all members from child groups */
768 needed |= NG_MBR_VALUES;
769 }
770 if (( ngi->ngi_flags & NG_MOF_VALUES ) &&
771 nestgroup_check_needed( op, attrflags, ngi->ngi_memberOf )) {
772 /* collect DNs of all parent groups */
773 needed |= NG_MOF_VALUES;
774 }
775 if ( needed ) {
776 nestgroup_cbinfo *nc;
777 slap_callback *sc = op->o_tmpcalloc( 1, sizeof(slap_callback)+sizeof(nestgroup_cbinfo), op->o_tmpmemctx );
778 nc = (nestgroup_cbinfo *)(sc+1);
779 sc->sc_private = nc;
780 nc->nc_needed = needed;
781 nc->nc_on = on;
782 sc->sc_response = nestgroup_searchresp;
783 sc->sc_next = op->o_callback;
784 op->o_callback = sc;
785 if ( negated ) nc->nc_needed |= NG_NEGATED;
786 }
787 }
788 return SLAP_CB_CONTINUE;
789 }
790
791 static int
792 nestgroup_db_init(
793 BackendDB *be,
794 ConfigReply *cr)
795 {
796 slap_overinst *on = (slap_overinst *)be->bd_info;
797 nestgroup_info_t *ngi;
798 int rc;
799 const char *text = NULL;
800
801 ngi = (nestgroup_info_t *)ch_calloc( 1, sizeof( *ngi ));
802 on->on_bi.bi_private = ngi;
803
804 if ( !ad_memberOf ) {
805 rc = slap_str2ad( SLAPD_MEMBEROF_ATTR, &ad_memberOf, &text );
806 if ( rc != LDAP_SUCCESS ) {
807 Debug( LDAP_DEBUG_ANY, "nestgroup_db_init: "
808 "unable to find attribute=\"%s\": %s (%d)\n",
809 SLAPD_MEMBEROF_ATTR, text, rc );
810 return rc;
811 }
812 }
813
814 if ( !ad_member ) {
815 rc = slap_str2ad( SLAPD_GROUP_ATTR, &ad_member, &text );
816 if ( rc != LDAP_SUCCESS ) {
817 Debug( LDAP_DEBUG_ANY, "nestgroup_db_init: "
818 "unable to find attribute=\"%s\": %s (%d)\n",
819 SLAPD_GROUP_ATTR, text, rc );
820 return rc;
821 }
822 }
823
824 return 0;
825 }
826
827 static int
828 nestgroup_db_open(
829 BackendDB *be,
830 ConfigReply *cr)
831 {
832 slap_overinst *on = (slap_overinst *)be->bd_info;
833 nestgroup_info_t *ngi = on->on_bi.bi_private;
834
835 if ( !ngi->ngi_member )
836 ngi->ngi_member = ad_member;
837
838 if ( !ngi->ngi_memberOf )
839 ngi->ngi_memberOf = ad_memberOf;
840
841 return 0;
842 }
843
844 static int
845 nestgroup_db_destroy(
846 BackendDB *be,
847 ConfigReply *cr
848 )
849 {
850 slap_overinst *on = (slap_overinst *) be->bd_info;
851 nestgroup_info_t *ngi = on->on_bi.bi_private;
852
853 ber_bvarray_free( ngi->ngi_groupBase );
854 ber_bvarray_free( ngi->ngi_ngroupBase );
855 ch_free( ngi );
856
857 return 0;
858 }
859
860 static slap_overinst nestgroup;
861
862 /* This overlay is set up for dynamic loading via moduleload. For static
863 * configuration, you'll need to arrange for the slap_overinst to be
864 * initialized and registered by some other function inside slapd.
865 */
866
867 int nestgroup_initialize() {
868 int code;
869
870 code = register_at(
871 "( 1.2.840.113556.1.2.102 "
872 "NAME 'memberOf' "
873 "DESC 'Group that the entry belongs to' "
874 "SYNTAX '1.3.6.1.4.1.1466.115.121.1.12' "
875 "EQUALITY distinguishedNameMatch " /* added */
876 "USAGE dSAOperation " /* added; questioned */
877 "NO-USER-MODIFICATION " /* added */
878 "X-ORIGIN 'iPlanet Delegated Administrator' )",
879 &ad_memberOf, 1 );
880 if ( code && code != SLAP_SCHERR_ATTR_DUP ) {
881 Debug( LDAP_DEBUG_ANY,
882 "nestgroup_initialize: register_at (memberOf) failed\n" );
883 return code;
884 }
885
886 nestgroup.on_bi.bi_type = "nestgroup";
887 nestgroup.on_bi.bi_db_init = nestgroup_db_init;
888 nestgroup.on_bi.bi_db_open = nestgroup_db_open;
889 nestgroup.on_bi.bi_db_destroy = nestgroup_db_destroy;
890
891 nestgroup.on_bi.bi_op_search = nestgroup_op_search;
892 /* nestgroup.on_bi.bi_op_compare = nestgroup_op_compare; */
893
894 nestgroup.on_bi.bi_cf_ocs = ngroupocs;
895 code = config_register_schema( ngroupcfg, ngroupocs );
896 if ( code ) return code;
897
898 return overlay_register( &nestgroup );
899 }
900
901 #if SLAPD_OVER_NESTGROUP == SLAPD_MOD_DYNAMIC
902 int
903 init_module( int argc, char *argv[] )
904 {
905 return nestgroup_initialize();
906 }
907 #endif
908
909 #endif /* defined(SLAPD_OVER_NESTGROUP) */