2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2004 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 );
28 slap_set_size( BerVarray set )
34 while ( !BER_BVISNULL( &set[ i ] ) ) {
42 slap_set_isempty( BerVarray set )
48 if ( !BER_BVISNULL( &set[ 0 ] ) ) {
56 slap_set_dispose( SetCookie *cp, BerVarray set, unsigned flags )
58 if ( flags & SLAP_SET_REFVAL ) {
59 if ( ! ( flags & SLAP_SET_REFARR ) ) {
60 cp->op->o_tmpfree( set, cp->op->o_tmpmemctx );
64 ber_bvarray_free_x( set, cp->op->o_tmpmemctx );
69 set_dup( SetCookie *cp, BerVarray set, unsigned flags )
71 BerVarray newset = NULL;
77 if ( flags & SLAP_SET_REFARR ) {
80 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ )
82 newset = cp->op->o_tmpcalloc( i + 1,
83 sizeof(struct berval),
84 cp->op->o_tmpmemctx );
85 if ( newset == NULL ) {
89 if ( flags & SLAP_SET_REFVAL ) {
90 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
91 ber_dupbv_x( &newset[ i ], &set[ i ],
92 cp->op->o_tmpmemctx );
96 AC_MEMCPY( newset, set, ( i + 1 ) * sizeof( struct berval ) );
115 unsigned op = ( op_flags & SLAP_SET_OPMASK );
120 if ( lset == NULL || BER_BVISNULL( lset ) ) {
121 if ( rset == NULL ) {
122 if ( lset == NULL ) {
123 return cp->op->o_tmpcalloc( 1,
124 sizeof(struct berval),
125 cp->op->o_tmpmemctx );
127 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
129 slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
130 return set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
132 if ( rset == NULL || BER_BVISNULL( rset ) ) {
133 slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
134 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
137 i = slap_set_size( lset ) + slap_set_size( rset ) + 1;
138 set = cp->op->o_tmpcalloc( i, sizeof(struct berval), cp->op->o_tmpmemctx );
140 /* set_chase() depends on this routine to
141 * keep the first elements of the result
142 * set the same (and in the same order)
145 for ( i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
146 if ( op_flags & SLAP_SET_LREFVAL ) {
147 ber_dupbv_x( &set[ i ], &lset[ i ], cp->op->o_tmpmemctx );
150 set[ i ] = lset[ i ];
154 for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
156 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
157 if ( dn_match( &rset[ i ], &set[ j ] ) )
159 if ( !( op_flags & SLAP_SET_RREFVAL ) ) {
160 cp->op->o_tmpfree( rset[ i ].bv_val, cp->op->o_tmpmemctx );
161 BER_BVZERO( &rset[ i ] );
169 if ( op_flags & SLAP_SET_RREFVAL ) {
170 ber_dupbv_x( &set[ j ], &rset[ i ], cp->op->o_tmpmemctx );
173 set[ j ] = rset[ i ];
181 if ( lset == NULL || BER_BVISNULL( lset )
182 || rset == NULL || BER_BVISNULL( rset ) )
184 set = cp->op->o_tmpcalloc( 1, sizeof(struct berval),
185 cp->op->o_tmpmemctx );
188 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
193 last = slap_set_size( set ) - 1;
194 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
195 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
196 if ( dn_match( &set[ i ], &rset[ j ] ) ) {
201 if ( BER_BVISNULL( &rset[ j ] ) ) {
202 cp->op->o_tmpfree( set[ i ].bv_val, cp->op->o_tmpmemctx );
203 set[ i ] = set[ last ];
204 BER_BVZERO( &set[ last ] );
216 if ( !( op_flags & SLAP_SET_LREFARR ) && lset != NULL ) {
217 cp->op->o_tmpfree( lset, cp->op->o_tmpmemctx );
220 if ( !( op_flags & SLAP_SET_RREFARR ) && rset != NULL ) {
221 cp->op->o_tmpfree( rset, cp->op->o_tmpmemctx );
228 set_chase( SLAP_SET_GATHER gatherer,
229 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
231 BerVarray vals, nset;
235 return cp->op->o_tmpcalloc( 1, sizeof(struct berval),
236 cp->op->o_tmpmemctx );
238 if ( BER_BVISNULL( set ) )
241 nset = cp->op->o_tmpcalloc( 1, sizeof(struct berval), cp->op->o_tmpmemctx );
242 if ( nset == NULL ) {
243 slap_set_dispose( cp, set, 0 );
246 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
247 vals = (gatherer)( cp, &set[ i ], desc );
248 if ( vals != NULL ) {
249 nset = slap_set_join( cp, nset, '|', vals );
252 slap_set_dispose( cp, set, 0 );
255 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
256 vals = (gatherer)( cp, &nset[ i ], desc );
257 if ( vals != NULL ) {
258 nset = slap_set_join( cp, nset, '|', vals );
259 if ( nset == NULL ) {
270 slap_set_filter( SLAP_SET_GATHER gatherer,
271 SetCookie *cp, struct berval *fbv,
272 struct berval *user, struct berval *target, BerVarray *results )
274 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
275 #define IS_OP(x) ( (unsigned long)(x) < 256 )
276 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
277 #define SF_TOP() ( (BerVarray)( (stp < 0) ? 0 : stack[ stp ] ) )
278 #define SF_POP() ( (BerVarray)( (stp < 0) ? 0 : stack[ stp-- ] ) )
279 #define SF_PUSH(x) do { \
280 if (stp >= 63) SF_ERROR(overflow); \
281 stack[ ++stp ] = (BerVarray)(long)(x); \
285 BerVarray stack[64] = { 0 };
288 char c, *filter = fbv->bv_val;
295 while ( ( c = *filter++ ) ) {
305 if ( IS_SET( SF_TOP() ) ) {
313 if ( IS_OP( set ) ) {
316 if ( SF_TOP() == (void *)'(' /* ) */ ) {
321 } else if ( IS_OP( SF_TOP() ) ) {
322 op = (unsigned)SF_POP();
325 set = slap_set_join( cp, lset, op, set );
340 if ( IS_OP( set ) ) {
343 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
347 } else if ( IS_OP( SF_TOP() ) ) {
348 op = (unsigned)SF_POP();
350 set = slap_set_join( cp, lset, op, set );
364 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
367 for ( len = 0; ( c = *filter++ ) && (c != /* [ */ ']'); len++ )
373 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
374 cp->op->o_tmpmemctx );
378 set->bv_val = cp->op->o_tmpcalloc( len + 1, sizeof(char),
379 cp->op->o_tmpmemctx );
380 if ( BER_BVISNULL( set ) ) {
383 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
394 /* fall through to next case */
397 if ( IS_OP( SF_TOP() ) ) {
405 && ( c < 'A' || c > 'Z' )
406 && ( c < 'a' || c > 'z' ) )
412 ( c = filter[ len ] )
413 && ( ( c >= '0' && c <= '9' )
414 || ( c >= 'A' && c <= 'Z' )
415 || ( c >= 'a' && c <= 'z' ) );
419 && memcmp( "this", filter, len ) == 0 )
421 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
424 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
425 cp->op->o_tmpmemctx );
429 ber_dupbv_x( set, target, cp->op->o_tmpmemctx );
430 if ( BER_BVISNULL( set ) ) {
435 && memcmp( "user", filter, len ) == 0 )
437 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
440 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
441 cp->op->o_tmpmemctx );
445 ber_dupbv_x( set, user, cp->op->o_tmpmemctx );
446 if ( BER_BVISNULL( set ) ) {
450 } else if ( SF_TOP() != (void *)'/' ) {
455 AttributeDescription *ad = NULL;
456 const char *text = NULL;
462 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
466 /* NOTE: ad must have distinguishedName syntax
467 * or expand in an LDAP URI if c == '*'
470 set = set_chase( gatherer,
471 cp, SF_POP(), ad, c == '*' );
487 if ( IS_OP( set ) ) {
490 if ( SF_TOP() == 0 ) {
493 } else if ( IS_OP( SF_TOP() ) ) {
494 op = (unsigned)SF_POP();
496 set = slap_set_join( cp, lset, op, set );
505 rc = slap_set_isempty( set ) ? 0 : 1;
512 if ( IS_SET( set ) ) {
513 slap_set_dispose( cp, set, 0 );
515 while ( ( set = SF_POP() ) ) {
516 if ( IS_SET( set ) ) {
517 slap_set_dispose( cp, set, 0 );