2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/string.h>
24 static BerVarray set_chase( SLAP_SET_GATHER gatherer,
25 SetCookie *cookie, BerVarray set, AttributeDescription *desc, int closure );
27 /* Count the array members */
29 slap_set_size( BerVarray set )
34 while ( !BER_BVISNULL( &set[ i ] ) ) {
42 /* Return 0 if there is at least one array member, non-zero otherwise */
44 slap_set_isempty( BerVarray set )
50 if ( !BER_BVISNULL( &set[ 0 ] ) ) {
57 /* Dispose of the contents of the array and the array itself according
58 * to the flags value. If SLAP_SET_REFVAL, don't dispose of values;
59 * if SLAP_SET_REFARR, don't dispose of the array itself. In case of
60 * binary operators, there are LEFT flags and RIGHT flags, referring to
61 * the first and the second operator arguments, respectively. In this
62 * case, flags must be transformed using macros SLAP_SET_LREF2REF() and
63 * SLAP_SET_RREF2REF() before calling this function.
66 slap_set_dispose( SetCookie *cp, BerVarray set, unsigned flags )
68 if ( flags & SLAP_SET_REFVAL ) {
69 if ( ! ( flags & SLAP_SET_REFARR ) ) {
70 cp->set_op->o_tmpfree( set, cp->set_op->o_tmpmemctx );
74 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
78 /* Duplicate a set. If SLAP_SET_REFARR, is not set, the original array
79 * with the original values is returned, otherwise the array is duplicated;
80 * if SLAP_SET_REFVAL is set, also the values are duplicated.
83 set_dup( SetCookie *cp, BerVarray set, unsigned flags )
85 BerVarray newset = NULL;
91 if ( flags & SLAP_SET_REFARR ) {
94 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ )
96 newset = cp->set_op->o_tmpcalloc( i + 1,
97 sizeof( struct berval ),
98 cp->set_op->o_tmpmemctx );
99 if ( newset == NULL ) {
103 if ( flags & SLAP_SET_REFVAL ) {
104 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
105 ber_dupbv_x( &newset[ i ], &set[ i ],
106 cp->set_op->o_tmpmemctx );
110 AC_MEMCPY( newset, set, ( i + 1 ) * sizeof( struct berval ) );
120 /* Join two sets according to operator op and flags op_flags.
122 * '|' (or): the union between the two sets is returned,
123 * eliminating diplicates
124 * '&' (and): the intersection between the two sets
126 * '+' (add): the inner product of the two sets is returned,
127 * namely a set containing the concatenation of
128 * all combinations of the two sets members,
129 * except for duplicates.
130 * The two sets are disposed of according to the flags as described
131 * for slap_set_dispose().
141 long i, j, last, rlast;
142 unsigned op = ( op_flags & SLAP_SET_OPMASK );
146 case '|': /* union */
147 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] ) ) {
148 if ( rset == NULL ) {
149 if ( lset == NULL ) {
150 set = cp->set_op->o_tmpcalloc( 1,
151 sizeof( struct berval ),
152 cp->set_op->o_tmpmemctx );
153 BER_BVZERO( &set[ 0 ] );
156 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
158 slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
159 return set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
161 if ( rset == NULL || BER_BVISNULL( &rset[ 0 ] ) ) {
162 slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
163 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
166 /* worst scenario: no duplicates */
167 rlast = slap_set_size( rset );
168 i = slap_set_size( lset ) + rlast + 1;
169 set = cp->set_op->o_tmpcalloc( i, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
171 /* set_chase() depends on this routine to
172 * keep the first elements of the result
173 * set the same (and in the same order)
176 for ( i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
177 if ( op_flags & SLAP_SET_LREFVAL ) {
178 ber_dupbv_x( &set[ i ], &lset[ i ], cp->set_op->o_tmpmemctx );
181 set[ i ] = lset[ i ];
187 for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
190 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
191 if ( bvmatch( &rset[ i ], &set[ j ] ) )
193 if ( !( op_flags & SLAP_SET_RREFVAL ) ) {
194 cp->set_op->o_tmpfree( rset[ i ].bv_val, cp->set_op->o_tmpmemctx );
195 rset[ i ] = rset[ --rlast ];
196 BER_BVZERO( &rset[ rlast ] );
204 if ( op_flags & SLAP_SET_RREFVAL ) {
205 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
208 set[ last ] = rset[ i ];
213 BER_BVZERO( &set[ last ] );
217 case '&': /* intersection */
218 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] )
219 || rset == NULL || BER_BVISNULL( &rset[ 0 ] ) )
221 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
222 cp->set_op->o_tmpmemctx );
223 BER_BVZERO( &set[ 0 ] );
230 llen = slap_set_size( lset );
231 rlen = slap_set_size( rset );
233 /* dup the shortest */
235 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
240 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
249 last = slap_set_size( set );
250 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
251 for ( j = 0; !BER_BVISNULL( &sset[ j ] ); j++ ) {
252 if ( bvmatch( &set[ i ], &sset[ j ] ) ) {
257 if ( BER_BVISNULL( &sset[ j ] ) ) {
258 cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
259 set[ i ] = set[ --last ];
260 BER_BVZERO( &set[ last ] );
267 case '+': /* string concatenation */
268 i = slap_set_size( rset );
269 j = slap_set_size( lset );
271 /* handle empty set cases */
274 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
275 cp->set_op->o_tmpmemctx );
279 BER_BVZERO( &set[ 0 ] );
283 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
287 } else if ( j == 0 ) {
288 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
292 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
293 cp->set_op->o_tmpmemctx );
298 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
299 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
303 /* don't concatenate with the empty string */
304 if ( BER_BVISEMPTY( &lset[ i ] ) ) {
305 ber_dupbv_x( &bv, &rset[ j ], cp->set_op->o_tmpmemctx );
306 if ( bv.bv_val == NULL ) {
307 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
312 } else if ( BER_BVISEMPTY( &rset[ j ] ) ) {
313 ber_dupbv_x( &bv, &lset[ i ], cp->set_op->o_tmpmemctx );
314 if ( bv.bv_val == NULL ) {
315 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
321 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
322 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
323 cp->set_op->o_tmpmemctx );
324 if ( bv.bv_val == NULL ) {
325 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
329 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
330 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
331 bv.bv_val[ bv.bv_len ] = '\0';
334 for ( k = 0; k < last; k++ ) {
335 if ( bvmatch( &set[ k ], &bv ) ) {
336 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
346 BER_BVZERO( &set[ last ] );
354 if ( lset ) slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
355 if ( rset ) slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
361 set_chase( SLAP_SET_GATHER gatherer,
362 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
364 BerVarray vals, nset;
368 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
369 cp->set_op->o_tmpmemctx );
371 BER_BVZERO( &set[ 0 ] );
376 if ( BER_BVISNULL( set ) ) {
380 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
381 if ( nset == NULL ) {
382 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
385 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
386 vals = gatherer( cp, &set[ i ], desc );
387 if ( vals != NULL ) {
388 nset = slap_set_join( cp, nset, '|', vals );
391 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
394 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
395 vals = gatherer( cp, &nset[ i ], desc );
396 if ( vals != NULL ) {
397 nset = slap_set_join( cp, nset, '|', vals );
398 if ( nset == NULL ) {
410 set_parents( SetCookie *cp, BerVarray set )
413 struct berval bv, pbv;
414 BerVarray nset, vals;
417 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
418 cp->set_op->o_tmpmemctx );
420 BER_BVZERO( &set[ 0 ] );
425 if ( BER_BVISNULL( &set[ 0 ] ) ) {
429 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
430 if ( nset == NULL ) {
431 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
435 BER_BVZERO( &nset[ 0 ] );
437 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
441 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
446 vals = cp->set_op->o_tmpcalloc( level + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
447 if ( vals == NULL ) {
448 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
449 ber_bvarray_free_x( nset, cp->set_op->o_tmpmemctx );
452 BER_BVZERO( &vals[ 0 ] );
456 for ( j = 0 ; j < level ; j++ ) {
457 ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
459 dnParent( &bv, &bv );
461 BER_BVZERO( &vals[ last ] );
463 nset = slap_set_join( cp, nset, '|', vals );
466 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
474 set_parent( SetCookie *cp, BerVarray set, int level )
481 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
482 cp->set_op->o_tmpmemctx );
484 BER_BVZERO( &set[ 0 ] );
489 if ( BER_BVISNULL( &set[ 0 ] ) ) {
493 nset = cp->set_op->o_tmpcalloc( slap_set_size( set ) + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
494 if ( nset == NULL ) {
495 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
499 BER_BVZERO( &nset[ 0 ] );
502 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
505 for ( j = 0 ; j < level ; j++ ) {
506 dnParent( &bv, &bv );
509 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
510 if ( bvmatch( &bv, &nset[ j ] ) )
516 if ( BER_BVISNULL( &nset[ j ] ) ) {
517 ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
522 BER_BVZERO( &nset[ last ] );
524 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
530 slap_set_filter( SLAP_SET_GATHER gatherer,
531 SetCookie *cp, struct berval *fbv,
532 struct berval *user, struct berval *target, BerVarray *results )
534 #define STACK_SIZE 64
535 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
536 #define IS_OP(x) ( (unsigned long)(x) < 256 )
537 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
538 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
539 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
540 #define SF_PUSH(x) do { \
541 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
542 stack[ ++stp ] = (BerVarray)(long)(x); \
546 BerVarray stack[ STACK_SIZE ] = { 0 };
549 char c, *filter = fbv->bv_val;
556 while ( ( c = *filter++ ) ) {
566 if ( IS_SET( SF_TOP() ) ) {
574 if ( IS_OP( set ) ) {
577 if ( SF_TOP() == (void *)'(' /* ) */ ) {
582 } else if ( IS_OP( SF_TOP() ) ) {
583 op = (unsigned long)SF_POP();
586 set = slap_set_join( cp, lset, op, set );
598 case '|': /* union */
599 case '&': /* intersection */
600 case '+': /* string concatenation */
602 if ( IS_OP( set ) ) {
605 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
609 } else if ( IS_OP( SF_TOP() ) ) {
610 op = (unsigned long)SF_POP();
612 set = slap_set_join( cp, lset, op, set );
626 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
629 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
635 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
636 cp->set_op->o_tmpmemctx );
640 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
641 cp->set_op->o_tmpmemctx );
642 if ( BER_BVISNULL( set ) ) {
645 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
652 if ( ( SF_TOP() == (void *)'/' )
653 && ( *filter == '*' || ASCII_DIGIT( *filter ) ) )
657 if ( *filter == '*' ) {
658 set = set_parents( cp, SF_POP() );
663 long parent = strtol( filter, &next, 10 );
665 if ( next == filter ) {
671 set = set_parent( cp, set, parent );
688 /* fall through to next case */
692 if ( IS_OP( SF_TOP() ) ) {
700 && ( c < 'A' || c > 'Z' )
701 && ( c < 'a' || c > 'z' ) )
707 ( c = filter[ len ] )
708 && ( ( c >= '0' && c <= '9' )
709 || ( c >= 'A' && c <= 'Z' )
710 || ( c >= 'a' && c <= 'z' ) );
714 && memcmp( "this", filter, len ) == 0 )
716 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
719 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
720 cp->set_op->o_tmpmemctx );
724 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
725 if ( BER_BVISNULL( set ) ) {
728 BER_BVZERO( &set[ 1 ] );
731 && memcmp( "user", filter, len ) == 0 )
733 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
736 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
737 cp->set_op->o_tmpmemctx );
741 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
742 if ( BER_BVISNULL( set ) ) {
745 BER_BVZERO( &set[ 1 ] );
747 } else if ( SF_TOP() != (void *)'/' ) {
752 AttributeDescription *ad = NULL;
753 const char *text = NULL;
759 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
763 /* NOTE: ad must have distinguishedName syntax
764 * or expand in an LDAP URI if c == '*'
767 set = set_chase( gatherer,
768 cp, SF_POP(), ad, c == '*' );
784 if ( IS_OP( set ) ) {
787 if ( SF_TOP() == 0 ) {
790 } else if ( IS_OP( SF_TOP() ) ) {
791 op = (unsigned long)SF_POP();
793 set = slap_set_join( cp, lset, op, set );
802 rc = slap_set_isempty( set ) ? 0 : 1;
809 if ( IS_SET( set ) ) {
810 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
812 while ( ( set = SF_POP() ) ) {
813 if ( IS_SET( set ) ) {
814 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );