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 )
58 ber_bvarray_free_x( set, cp->op->o_tmpmemctx );
62 slap_set_join( SetCookie *cp, BerVarray lset, int op, BerVarray rset )
69 if ( lset == NULL || BER_BVISNULL( lset ) ) {
72 return cp->op->o_tmpcalloc( 1, sizeof(struct berval),
77 slap_set_dispose( cp, lset );
80 if ( rset == NULL || BER_BVISNULL( rset ) ) {
81 slap_set_dispose( cp, rset );
85 i = slap_set_size( lset ) + slap_set_size( rset ) + 1;
86 set = cp->op->o_tmpcalloc( i, sizeof(struct berval), cp->op->o_tmpmemctx );
88 /* set_chase() depends on this routine to
89 * keep the first elements of the result
90 * set the same (and in the same order)
93 for ( i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
96 cp->op->o_tmpfree( lset, cp->op->o_tmpmemctx );
97 for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
98 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
99 if ( dn_match( &rset[ i ], &set[ j ] ) )
101 cp->op->o_tmpfree( rset[ i ].bv_val, cp->op->o_tmpmemctx );
102 BER_BVZERO( &rset[ i ] );
106 if ( !BER_BVISNULL( &rset[ i ] ) ) {
107 set[ j ] = rset[ i ];
110 cp->op->o_tmpfree( rset, cp->op->o_tmpmemctx );
116 if ( lset == NULL || BER_BVISNULL( lset )
117 || rset == NULL || BER_BVISNULL( rset ) )
119 set = cp->op->o_tmpcalloc( 1, sizeof(struct berval),
120 cp->op->o_tmpmemctx );
124 last = slap_set_size( set ) - 1;
125 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
126 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
127 if ( dn_match( &set[ i ], &rset[ j ] ) ) {
131 if ( BER_BVISNULL( &rset[ j ] ) ) {
132 cp->op->o_tmpfree( set[ i ].bv_val, cp->op->o_tmpmemctx );
133 set[ i ] = set[ last ];
134 BER_BVZERO( &set[ last ] );
142 slap_set_dispose( cp, lset );
143 slap_set_dispose( cp, rset );
148 set_chase( SLAP_SET_GATHER gatherer,
149 SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
151 BerVarray vals, nset;
155 return cp->op->o_tmpcalloc( 1, sizeof(struct berval),
156 cp->op->o_tmpmemctx );
158 if ( BER_BVISNULL( set ) )
161 nset = cp->op->o_tmpcalloc( 1, sizeof(struct berval), cp->op->o_tmpmemctx );
162 if ( nset == NULL ) {
163 slap_set_dispose( cp, set );
166 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
167 vals = (gatherer)( cp, &set[ i ], desc );
168 if ( vals != NULL ) {
169 nset = slap_set_join( cp, nset, '|', vals );
172 slap_set_dispose( cp, set );
175 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
176 vals = (gatherer)( cp, &nset[ i ], desc );
177 if ( vals != NULL ) {
178 nset = slap_set_join( cp, nset, '|', vals );
179 if ( nset == NULL ) {
190 slap_set_filter( SLAP_SET_GATHER gatherer,
191 SetCookie *cp, struct berval *fbv,
192 struct berval *user, struct berval *target, BerVarray *results )
194 #define IS_SET(x) ( (unsigned long)(x) >= 256 )
195 #define IS_OP(x) ( (unsigned long)(x) < 256 )
196 #define SF_ERROR(x) do { rc = -1; goto _error; } while (0)
197 #define SF_TOP() ( (BerVarray)( (stp < 0) ? 0 : stack[ stp ] ) )
198 #define SF_POP() ( (BerVarray)( (stp < 0) ? 0 : stack[ stp-- ] ) )
199 #define SF_PUSH(x) do { \
200 if (stp >= 63) SF_ERROR(overflow); \
201 stack[ ++stp ] = (BerVarray)(long)(x); \
205 BerVarray stack[64] = { 0 };
206 int len, op, rc, stp;
207 char c, *filter = fbv->bv_val;
214 while ( ( c = *filter++ ) ) {
224 if ( IS_SET( SF_TOP() ) ) {
232 if ( IS_OP( set ) ) {
235 if ( SF_TOP() == (void *)'(' /* ) */ ) {
240 } else if ( IS_OP( SF_TOP() ) ) {
244 set = slap_set_join( cp, lset, op, set );
259 if ( IS_OP( set ) ) {
262 if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
266 } else if ( IS_OP( SF_TOP() ) ) {
269 set = slap_set_join( cp, lset, op, set );
283 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
286 for ( len = 0; ( c = *filter++ ) && (c != /* [ */ ']'); len++ )
292 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
293 cp->op->o_tmpmemctx );
297 set->bv_val = cp->op->o_tmpcalloc( len + 1, sizeof(char),
298 cp->op->o_tmpmemctx );
299 if ( BER_BVISNULL( set ) ) {
302 AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
313 /* fall through to next case */
316 if ( IS_OP( SF_TOP() ) ) {
324 && ( c < 'A' || c > 'Z' )
325 && ( c < 'a' || c > 'z' ) )
331 ( c = filter[ len ] )
332 && ( ( c >= '0' && c <= '9' )
333 || ( c >= 'A' && c <= 'Z' )
334 || ( c >= 'a' && c <= 'z' ) );
338 && memcmp( "this", filter, len ) == 0 )
340 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
343 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
344 cp->op->o_tmpmemctx );
348 ber_dupbv_x( set, target, cp->op->o_tmpmemctx );
349 if ( BER_BVISNULL( set ) ) {
354 && memcmp( "user", filter, len ) == 0 )
356 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
359 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
360 cp->op->o_tmpmemctx );
364 ber_dupbv_x( set, user, cp->op->o_tmpmemctx );
365 if ( BER_BVISNULL( set ) ) {
369 } else if ( SF_TOP() != (void *)'/' ) {
374 AttributeDescription *ad = NULL;
375 const char *text = NULL;
381 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
385 /* NOTE: ad must have distinguishedName syntax
386 * or expand in an LDAP URI if c == '*'
389 set = set_chase( gatherer,
390 cp, SF_POP(), ad, c == '*' );
406 if ( IS_OP( set ) ) {
409 if ( SF_TOP() == 0 ) {
412 } else if ( IS_OP( SF_TOP() ) ) {
415 set = slap_set_join( cp, lset, op, set );
424 rc = slap_set_isempty( set ) ? 0 : 1;
431 if ( IS_SET( set ) ) {
432 slap_set_dispose( cp, set );
434 while ( ( set = SF_POP() ) ) {
435 if ( IS_SET( set ) ) {
436 slap_set_dispose( cp, set );