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 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
272 cp->set_op->o_tmpmemctx );
277 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
278 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
282 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
283 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
284 cp->set_op->o_tmpmemctx );
285 if ( bv.bv_val == NULL ) {
286 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
290 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
291 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
292 bv.bv_val[ bv.bv_len ] = '\0';
294 for ( k = 0; k < last; k++ ) {
295 if ( bvmatch( &set[ k ], &bv ) ) {
296 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
306 BER_BVZERO( &set[ last ] );
314 if ( lset ) slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
315 if ( rset ) slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
321 set_chase( SLAP_SET_GATHER gatherer,
322 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
324 BerVarray vals, nset;
328 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
329 cp->set_op->o_tmpmemctx );
331 BER_BVZERO( &set[ 0 ] );
336 if ( BER_BVISNULL( set ) ) {
340 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
341 if ( nset == NULL ) {
342 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
345 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
346 vals = gatherer( cp, &set[ i ], desc );
347 if ( vals != NULL ) {
348 nset = slap_set_join( cp, nset, '|', vals );
351 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
354 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
355 vals = gatherer( cp, &nset[ i ], desc );
356 if ( vals != NULL ) {
357 nset = slap_set_join( cp, nset, '|', vals );
358 if ( nset == NULL ) {
370 set_parents( SetCookie *cp, BerVarray set )
373 struct berval bv, pbv;
374 BerVarray nset, vals;
377 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
378 cp->set_op->o_tmpmemctx );
380 BER_BVZERO( &set[ 0 ] );
385 if ( BER_BVISNULL( &set[ 0 ] ) ) {
389 nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
390 if ( nset == NULL ) {
391 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
395 BER_BVZERO( &nset[ 0 ] );
397 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
401 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
406 vals = cp->set_op->o_tmpcalloc( level + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
407 if ( vals == NULL ) {
408 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
409 ber_bvarray_free_x( nset, cp->set_op->o_tmpmemctx );
412 BER_BVZERO( &vals[ 0 ] );
416 for ( j = 0 ; j < level ; j++ ) {
417 ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
419 dnParent( &bv, &bv );
421 BER_BVZERO( &vals[ last ] );
423 nset = slap_set_join( cp, nset, '|', vals );
426 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
434 set_parent( SetCookie *cp, BerVarray set, int level )
441 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
442 cp->set_op->o_tmpmemctx );
444 BER_BVZERO( &set[ 0 ] );
449 if ( BER_BVISNULL( &set[ 0 ] ) ) {
453 nset = cp->set_op->o_tmpcalloc( slap_set_size( set ) + 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
454 if ( nset == NULL ) {
455 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
459 BER_BVZERO( &nset[ 0 ] );
462 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
465 for ( j = 0 ; j < level ; j++ ) {
466 dnParent( &bv, &bv );
469 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
470 if ( bvmatch( &bv, &nset[ j ] ) )
476 if ( BER_BVISNULL( &nset[ j ] ) ) {
477 ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
482 BER_BVZERO( &nset[ last ] );
484 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
490 slap_set_filter( SLAP_SET_GATHER gatherer,
491 SetCookie *cp, struct berval *fbv,
492 struct berval *user, struct berval *target, BerVarray *results )
494 #define STACK_SIZE 64
495 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
496 #define IS_OP(x) ( (unsigned long)(x) < 256 )
497 #define SF_ERROR(x) do { rc = -1; goto _error; } while ( 0 )
498 #define SF_TOP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
499 #define SF_POP() ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
500 #define SF_PUSH(x) do { \
501 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
502 stack[ ++stp ] = (BerVarray)(long)(x); \
506 BerVarray stack[ STACK_SIZE ] = { 0 };
509 char c, *filter = fbv->bv_val;
516 while ( ( c = *filter++ ) ) {
526 if ( IS_SET( SF_TOP() ) ) {
534 if ( IS_OP( set ) ) {
537 if ( SF_TOP() == (void *)'(' /* ) */ ) {
542 } else if ( IS_OP( SF_TOP() ) ) {
543 op = (unsigned long)SF_POP();
546 set = slap_set_join( cp, lset, op, set );
558 case '|': /* union */
559 case '&': /* intersection */
560 case '+': /* string concatenation */
562 if ( IS_OP( set ) ) {
565 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
569 } else if ( IS_OP( SF_TOP() ) ) {
570 op = (unsigned long)SF_POP();
572 set = slap_set_join( cp, lset, op, set );
586 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
589 for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
595 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
596 cp->set_op->o_tmpmemctx );
600 set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
601 cp->set_op->o_tmpmemctx );
602 if ( BER_BVISNULL( set ) ) {
605 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
612 if ( ( SF_TOP() == (void *)'/' )
613 && ( *filter >= '0' || *filter <= '9' ) )
619 ( c = filter[ len ] )
620 && ( c >= '0' && c <= '9' );
623 for ( i = len ; i > 0 ; i-- ) {
624 parent += (int)( filter[ i - 1 ] - '0' ) * count;
629 set = set_parents( cp, SF_POP() );
631 set = set_parent( cp, SF_POP(), parent );
642 /* fall through to next case */
646 if ( IS_OP( SF_TOP() ) ) {
654 && ( c < 'A' || c > 'Z' )
655 && ( c < 'a' || c > 'z' ) )
661 ( c = filter[ len ] )
662 && ( ( c >= '0' && c <= '9' )
663 || ( c >= 'A' && c <= 'Z' )
664 || ( c >= 'a' && c <= 'z' ) );
668 && memcmp( "this", filter, len ) == 0 )
670 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
673 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
674 cp->set_op->o_tmpmemctx );
678 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
679 if ( BER_BVISNULL( set ) ) {
682 BER_BVZERO( &set[ 1 ] );
685 && memcmp( "user", filter, len ) == 0 )
687 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
690 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
691 cp->set_op->o_tmpmemctx );
695 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
696 if ( BER_BVISNULL( set ) ) {
699 BER_BVZERO( &set[ 1 ] );
701 } else if ( SF_TOP() != (void *)'/' ) {
706 AttributeDescription *ad = NULL;
707 const char *text = NULL;
713 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
717 /* NOTE: ad must have distinguishedName syntax
718 * or expand in an LDAP URI if c == '*'
721 set = set_chase( gatherer,
722 cp, SF_POP(), ad, c == '*' );
738 if ( IS_OP( set ) ) {
741 if ( SF_TOP() == 0 ) {
744 } else if ( IS_OP( SF_TOP() ) ) {
745 op = (unsigned long)SF_POP();
747 set = slap_set_join( cp, lset, op, set );
756 rc = slap_set_isempty( set ) ? 0 : 1;
763 if ( IS_SET( set ) ) {
764 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
766 while ( ( set = SF_POP() ) ) {
767 if ( IS_SET( set ) ) {
768 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );