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 */
242 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
247 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
256 last = slap_set_size( set );
257 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
258 for ( j = 0; !BER_BVISNULL( &sset[ j ] ); j++ ) {
259 if ( bvmatch( &set[ i ], &sset[ j ] ) ) {
264 if ( BER_BVISNULL( &sset[ j ] ) ) {
265 cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
266 set[ i ] = set[ --last ];
267 BER_BVZERO( &set[ last ] );
274 case '+': /* string concatenation */
275 i = slap_set_size( rset );
276 j = slap_set_size( lset );
278 /* handle empty set cases */
281 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
282 cp->set_op->o_tmpmemctx );
286 BER_BVZERO( &set[ 0 ] );
290 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
294 } else if ( j == 0 ) {
295 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
299 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
300 cp->set_op->o_tmpmemctx );
305 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
306 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
310 /* don't concatenate with the empty string */
311 if ( BER_BVISEMPTY( &lset[ i ] ) ) {
312 ber_dupbv_x( &bv, &rset[ j ], cp->set_op->o_tmpmemctx );
313 if ( bv.bv_val == NULL ) {
314 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
319 } else if ( BER_BVISEMPTY( &rset[ j ] ) ) {
320 ber_dupbv_x( &bv, &lset[ i ], cp->set_op->o_tmpmemctx );
321 if ( bv.bv_val == NULL ) {
322 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
328 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
329 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
330 cp->set_op->o_tmpmemctx );
331 if ( bv.bv_val == NULL ) {
332 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
336 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
337 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
338 bv.bv_val[ bv.bv_len ] = '\0';
341 for ( k = 0; k < last; k++ ) {
342 if ( bvmatch( &set[ k ], &bv ) ) {
343 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
353 BER_BVZERO( &set[ last ] );
361 if ( lset ) slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
362 if ( rset ) slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
368 set_chase( SLAP_SET_GATHER gatherer,
369 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
371 BerVarray vals, nset;
375 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
376 cp->set_op->o_tmpmemctx );
378 BER_BVZERO( &set[ 0 ] );
383 if ( BER_BVISNULL( set ) ) {
387 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
388 if ( nset == NULL ) {
389 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
392 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
393 vals = gatherer( cp, &set[ i ], desc );
394 if ( vals != NULL ) {
395 nset = slap_set_join( cp, nset, '|', vals );
398 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
401 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
402 vals = gatherer( cp, &nset[ i ], desc );
403 if ( vals != NULL ) {
404 nset = slap_set_join( cp, nset, '|', vals );
405 if ( nset == NULL ) {
417 set_parents( SetCookie *cp, BerVarray set )
420 struct berval bv, pbv;
421 BerVarray nset, vals;
424 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
425 cp->set_op->o_tmpmemctx );
427 BER_BVZERO( &set[ 0 ] );
432 if ( BER_BVISNULL( &set[ 0 ] ) ) {
436 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
437 if ( nset == NULL ) {
438 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
442 BER_BVZERO( &nset[ 0 ] );
444 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
448 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
453 vals = cp->set_op->o_tmpcalloc( level + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
454 if ( vals == NULL ) {
455 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
456 ber_bvarray_free_x( nset, cp->set_op->o_tmpmemctx );
459 BER_BVZERO( &vals[ 0 ] );
463 for ( j = 0 ; j < level ; j++ ) {
464 ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
466 dnParent( &bv, &bv );
468 BER_BVZERO( &vals[ last ] );
470 nset = slap_set_join( cp, nset, '|', vals );
473 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
481 set_parent( SetCookie *cp, BerVarray set, int level )
488 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
489 cp->set_op->o_tmpmemctx );
491 BER_BVZERO( &set[ 0 ] );
496 if ( BER_BVISNULL( &set[ 0 ] ) ) {
500 nset = cp->set_op->o_tmpcalloc( slap_set_size( set ) + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
501 if ( nset == NULL ) {
502 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
506 BER_BVZERO( &nset[ 0 ] );
509 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
512 for ( j = 0 ; j < level ; j++ ) {
513 dnParent( &bv, &bv );
516 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
517 if ( bvmatch( &bv, &nset[ j ] ) )
523 if ( BER_BVISNULL( &nset[ j ] ) ) {
524 ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
529 BER_BVZERO( &nset[ last ] );
531 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
537 slap_set_filter( SLAP_SET_GATHER gatherer,
538 SetCookie *cp, struct berval *fbv,
539 struct berval *user, struct berval *target, BerVarray *results )
541 #define STACK_SIZE 64
542 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
543 #define IS_OP(x) ( (unsigned long)(x) < 256 )
544 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
545 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
546 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
547 #define SF_PUSH(x) do { \
548 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
549 stack[ ++stp ] = (BerVarray)(long)(x); \
553 BerVarray stack[ STACK_SIZE ] = { 0 };
556 char c, *filter = fbv->bv_val;
563 while ( ( c = *filter++ ) ) {
573 if ( IS_SET( SF_TOP() ) ) {
581 if ( IS_OP( set ) ) {
584 if ( SF_TOP() == (void *)'(' /* ) */ ) {
589 } else if ( IS_OP( SF_TOP() ) ) {
590 op = (unsigned long)SF_POP();
593 set = slap_set_join( cp, lset, op, set );
605 case '|': /* union */
606 case '&': /* intersection */
607 case '+': /* string concatenation */
609 if ( IS_OP( set ) ) {
612 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
616 } else if ( IS_OP( SF_TOP() ) ) {
617 op = (unsigned long)SF_POP();
619 set = slap_set_join( cp, lset, op, set );
633 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
636 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
642 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
643 cp->set_op->o_tmpmemctx );
647 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
648 cp->set_op->o_tmpmemctx );
649 if ( BER_BVISNULL( set ) ) {
652 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
659 if ( ( SF_TOP() == (void *)'/' )
660 && ( *filter == '*' || ASCII_DIGIT( *filter ) ) )
664 if ( *filter == '*' ) {
665 set = set_parents( cp, SF_POP() );
670 long parent = strtol( filter, &next, 10 );
672 if ( next == filter ) {
678 set = set_parent( cp, set, parent );
695 /* fall through to next case */
699 if ( IS_OP( SF_TOP() ) ) {
707 && ( c < 'A' || c > 'Z' )
708 && ( c < 'a' || c > 'z' ) )
714 ( c = filter[ len ] )
715 && ( ( c >= '0' && c <= '9' )
716 || ( c >= 'A' && c <= 'Z' )
717 || ( c >= 'a' && c <= 'z' ) );
721 && memcmp( "this", filter, len ) == 0 )
723 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
726 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
727 cp->set_op->o_tmpmemctx );
731 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
732 if ( BER_BVISNULL( set ) ) {
735 BER_BVZERO( &set[ 1 ] );
738 && memcmp( "user", filter, len ) == 0 )
740 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
743 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
744 cp->set_op->o_tmpmemctx );
748 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
749 if ( BER_BVISNULL( set ) ) {
752 BER_BVZERO( &set[ 1 ] );
754 } else if ( SF_TOP() != (void *)'/' ) {
759 AttributeDescription *ad = NULL;
760 const char *text = NULL;
766 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
770 /* NOTE: ad must have distinguishedName syntax
771 * or expand in an LDAP URI if c == '*'
774 set = set_chase( gatherer,
775 cp, SF_POP(), ad, c == '*' );
791 if ( IS_OP( set ) ) {
794 if ( SF_TOP() == 0 ) {
797 } else if ( IS_OP( SF_TOP() ) ) {
798 op = (unsigned long)SF_POP();
800 set = slap_set_join( cp, lset, op, set );
809 rc = slap_set_isempty( set ) ? 0 : 1;
816 if ( IS_SET( set ) ) {
817 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
819 while ( ( set = SF_POP() ) ) {
820 if ( IS_SET( set ) ) {
821 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );