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 ];
185 /* pointers to values have been used in set - don't free twice */
186 op_flags |= SLAP_SET_LREFVAL;
190 for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
193 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
194 if ( bvmatch( &rset[ i ], &set[ j ] ) )
196 if ( !( op_flags & SLAP_SET_RREFVAL ) ) {
197 cp->set_op->o_tmpfree( rset[ i ].bv_val, cp->set_op->o_tmpmemctx );
198 rset[ i ] = rset[ --rlast ];
199 BER_BVZERO( &rset[ rlast ] );
207 if ( op_flags & SLAP_SET_RREFVAL ) {
208 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
211 set[ last ] = rset[ i ];
217 /* pointers to values have been used in set - don't free twice */
218 op_flags |= SLAP_SET_RREFVAL;
220 BER_BVZERO( &set[ last ] );
224 case '&': /* intersection */
225 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] )
226 || rset == NULL || BER_BVISNULL( &rset[ 0 ] ) )
228 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
229 cp->set_op->o_tmpmemctx );
230 BER_BVZERO( &set[ 0 ] );
237 llen = slap_set_size( lset );
238 rlen = slap_set_size( rset );
240 /* dup the shortest */
243 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
249 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
258 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
259 for ( j = 0; !BER_BVISNULL( &sset[ j ] ); j++ ) {
260 if ( bvmatch( &set[ i ], &sset[ j ] ) ) {
265 if ( BER_BVISNULL( &sset[ j ] ) ) {
266 cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
267 set[ i ] = set[ --last ];
268 BER_BVZERO( &set[ last ] );
275 case '+': /* string concatenation */
276 i = slap_set_size( rset );
277 j = slap_set_size( lset );
279 /* handle empty set cases */
282 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
283 cp->set_op->o_tmpmemctx );
287 BER_BVZERO( &set[ 0 ] );
291 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
296 } else if ( j == 0 ) {
297 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
302 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
303 cp->set_op->o_tmpmemctx );
308 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
309 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
313 /* don't concatenate with the empty string */
314 if ( BER_BVISEMPTY( &lset[ i ] ) ) {
315 ber_dupbv_x( &bv, &rset[ j ], cp->set_op->o_tmpmemctx );
316 if ( bv.bv_val == NULL ) {
317 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
322 } else if ( BER_BVISEMPTY( &rset[ j ] ) ) {
323 ber_dupbv_x( &bv, &lset[ i ], cp->set_op->o_tmpmemctx );
324 if ( bv.bv_val == NULL ) {
325 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
331 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
332 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
333 cp->set_op->o_tmpmemctx );
334 if ( bv.bv_val == NULL ) {
335 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
339 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
340 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
341 bv.bv_val[ bv.bv_len ] = '\0';
344 for ( k = 0; k < last; k++ ) {
345 if ( bvmatch( &set[ k ], &bv ) ) {
346 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
356 BER_BVZERO( &set[ last ] );
364 if ( lset ) slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
365 if ( rset ) slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
371 set_chase( SLAP_SET_GATHER gatherer,
372 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
374 BerVarray vals, nset;
378 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
379 cp->set_op->o_tmpmemctx );
381 BER_BVZERO( &set[ 0 ] );
386 if ( BER_BVISNULL( set ) ) {
390 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
391 if ( nset == NULL ) {
392 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
395 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
396 vals = gatherer( cp, &set[ i ], desc );
397 if ( vals != NULL ) {
398 nset = slap_set_join( cp, nset, '|', vals );
401 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
404 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
405 vals = gatherer( cp, &nset[ i ], desc );
406 if ( vals != NULL ) {
407 nset = slap_set_join( cp, nset, '|', vals );
408 if ( nset == NULL ) {
420 set_parents( SetCookie *cp, BerVarray set )
423 struct berval bv, pbv;
424 BerVarray nset, vals;
427 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
428 cp->set_op->o_tmpmemctx );
430 BER_BVZERO( &set[ 0 ] );
435 if ( BER_BVISNULL( &set[ 0 ] ) ) {
439 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
440 if ( nset == NULL ) {
441 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
445 BER_BVZERO( &nset[ 0 ] );
447 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
451 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
456 vals = cp->set_op->o_tmpcalloc( level + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
457 if ( vals == NULL ) {
458 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
459 ber_bvarray_free_x( nset, cp->set_op->o_tmpmemctx );
462 BER_BVZERO( &vals[ 0 ] );
466 for ( j = 0 ; j < level ; j++ ) {
467 ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
469 dnParent( &bv, &bv );
471 BER_BVZERO( &vals[ last ] );
473 nset = slap_set_join( cp, nset, '|', vals );
476 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
484 set_parent( SetCookie *cp, BerVarray set, int level )
491 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
492 cp->set_op->o_tmpmemctx );
494 BER_BVZERO( &set[ 0 ] );
499 if ( BER_BVISNULL( &set[ 0 ] ) ) {
503 nset = cp->set_op->o_tmpcalloc( slap_set_size( set ) + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
504 if ( nset == NULL ) {
505 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
509 BER_BVZERO( &nset[ 0 ] );
512 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
515 for ( j = 0 ; j < level ; j++ ) {
516 dnParent( &bv, &bv );
519 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
520 if ( bvmatch( &bv, &nset[ j ] ) )
526 if ( BER_BVISNULL( &nset[ j ] ) ) {
527 ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
532 BER_BVZERO( &nset[ last ] );
534 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
540 slap_set_filter( SLAP_SET_GATHER gatherer,
541 SetCookie *cp, struct berval *fbv,
542 struct berval *user, struct berval *target, BerVarray *results )
544 #define STACK_SIZE 64
545 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
546 #define IS_OP(x) ( (unsigned long)(x) < 256 )
547 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
548 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
549 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
550 #define SF_PUSH(x) do { \
551 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
552 stack[ ++stp ] = (BerVarray)(long)(x); \
556 BerVarray stack[ STACK_SIZE ] = { 0 };
559 char c, *filter = fbv->bv_val;
566 while ( ( c = *filter++ ) ) {
576 if ( IS_SET( SF_TOP() ) ) {
584 if ( IS_OP( set ) ) {
587 if ( SF_TOP() == (void *)'(' /* ) */ ) {
592 } else if ( IS_OP( SF_TOP() ) ) {
593 op = (unsigned long)SF_POP();
596 set = slap_set_join( cp, lset, op, set );
608 case '|': /* union */
609 case '&': /* intersection */
610 case '+': /* string concatenation */
612 if ( IS_OP( set ) ) {
615 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
619 } else if ( IS_OP( SF_TOP() ) ) {
620 op = (unsigned long)SF_POP();
622 set = slap_set_join( cp, lset, op, set );
636 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
639 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
645 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
646 cp->set_op->o_tmpmemctx );
650 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
651 cp->set_op->o_tmpmemctx );
652 if ( BER_BVISNULL( set ) ) {
655 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
662 if ( ( SF_TOP() == (void *)'/' )
663 && ( *filter == '*' || ASCII_DIGIT( *filter ) ) )
667 if ( *filter == '*' ) {
668 set = set_parents( cp, SF_POP() );
673 long parent = strtol( filter, &next, 10 );
675 if ( next == filter ) {
681 set = set_parent( cp, set, parent );
698 /* fall through to next case */
702 if ( IS_OP( SF_TOP() ) ) {
710 && ( c < 'A' || c > 'Z' )
711 && ( c < 'a' || c > 'z' ) )
717 ( c = filter[ len ] )
718 && ( ( c >= '0' && c <= '9' )
719 || ( c >= 'A' && c <= 'Z' )
720 || ( c >= 'a' && c <= 'z' ) );
724 && memcmp( "this", filter, len ) == 0 )
726 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
729 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
730 cp->set_op->o_tmpmemctx );
734 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
735 if ( BER_BVISNULL( set ) ) {
738 BER_BVZERO( &set[ 1 ] );
741 && memcmp( "user", filter, len ) == 0 )
743 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
746 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
747 cp->set_op->o_tmpmemctx );
751 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
752 if ( BER_BVISNULL( set ) ) {
755 BER_BVZERO( &set[ 1 ] );
757 } else if ( SF_TOP() != (void *)'/' ) {
762 AttributeDescription *ad = NULL;
763 const char *text = NULL;
769 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
773 /* NOTE: ad must have distinguishedName syntax
774 * or expand in an LDAP URI if c == '*'
777 set = set_chase( gatherer,
778 cp, SF_POP(), ad, c == '*' );
794 if ( IS_OP( set ) ) {
797 if ( SF_TOP() == 0 ) {
800 } else if ( IS_OP( SF_TOP() ) ) {
801 op = (unsigned long)SF_POP();
803 set = slap_set_join( cp, lset, op, set );
812 rc = slap_set_isempty( set ) ? 0 : 1;
819 if ( IS_SET( set ) ) {
820 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
822 while ( ( set = SF_POP() ) ) {
823 if ( IS_SET( set ) ) {
824 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );