]> git.sur5r.net Git - openldap/blob - servers/slapd/sets.c
Fix typo
[openldap] / servers / slapd / sets.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2007 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/string.h>
20
21 #include "slap.h"
22 #include "sets.h"
23
24 static BerVarray set_chase( SLAP_SET_GATHER gatherer,
25         SetCookie *cookie, BerVarray set, AttributeDescription *desc, int closure );
26
27 /* Count the array members */
28 static long
29 slap_set_size( BerVarray set )
30 {
31         long    i = 0;
32
33         if ( set != NULL ) {
34                 while ( !BER_BVISNULL( &set[ i ] ) ) {
35                         i++;
36                 }
37         }
38
39         return i;
40 }
41
42 /* Return 0 if there is at least one array member, non-zero otherwise */
43 static int
44 slap_set_isempty( BerVarray set )
45 {
46         if ( set == NULL ) {
47                 return 1;
48         }
49
50         if ( !BER_BVISNULL( &set[ 0 ] ) ) {
51                 return 0;
52         }
53
54         return 1;
55 }
56
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.
64  */
65 static void
66 slap_set_dispose( SetCookie *cp, BerVarray set, unsigned flags )
67 {
68         if ( flags & SLAP_SET_REFVAL ) {
69                 if ( ! ( flags & SLAP_SET_REFARR ) ) {
70                         cp->set_op->o_tmpfree( set, cp->set_op->o_tmpmemctx );
71                 }
72
73         } else {
74                 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
75         }
76 }
77
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.
81  */
82 static BerVarray
83 set_dup( SetCookie *cp, BerVarray set, unsigned flags )
84 {
85         BerVarray       newset = NULL;
86
87         if ( set == NULL ) {
88                 return NULL;
89         }
90
91         if ( flags & SLAP_SET_REFARR ) {
92                 int     i;
93
94                 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ )
95                         ;
96                 newset = cp->set_op->o_tmpcalloc( i + 1,
97                                 sizeof( struct berval ), 
98                                 cp->set_op->o_tmpmemctx );
99                 if ( newset == NULL ) {
100                         return NULL;
101                 }
102
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 );
107                         }
108
109                 } else {
110                         AC_MEMCPY( newset, set, ( i + 1 ) * sizeof( struct berval ) );
111                 }
112                 
113         } else {
114                 newset = set;
115         }
116
117         return newset;
118 }
119
120 /* Join two sets according to operator op and flags op_flags.
121  * op can be:
122  *      '|' (or):       the union between the two sets is returned,
123  *                      eliminating diplicates
124  *      '&' (and):      the intersection between the two sets
125  *                      is returned
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().
132  */
133 BerVarray
134 slap_set_join(
135         SetCookie       *cp,
136         BerVarray       lset,
137         unsigned        op_flags,
138         BerVarray       rset )
139 {
140         BerVarray       set;
141         long            i, j, last, rlast;
142         unsigned        op = ( op_flags & SLAP_SET_OPMASK );
143
144         set = NULL;
145         switch ( op ) {
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 ] );
154                                         return set;
155                                 }
156                                 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
157                         }
158                         slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
159                         return set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
160                 }
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 ) );
164                 }
165
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 );
170                 if ( set != NULL ) {
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)
174                          * as the left-set.
175                          */
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 );
179
180                                 } else {
181                                         set[ i ] = lset[ i ];
182                                 }
183                         }
184
185                         /* pointers to values have been used in set - don't free twice */
186                         op_flags |= SLAP_SET_LREFVAL;
187
188                         last = i;
189
190                         for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
191                                 int     exists = 0;
192
193                                 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
194                                         if ( bvmatch( &rset[ i ], &set[ j ] ) )
195                                         {
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 ] );
200                                                 }
201                                                 exists = 1;
202                                                 break;
203                                         }
204                                 }
205
206                                 if ( !exists ) {
207                                         if ( op_flags & SLAP_SET_RREFVAL ) {
208                                                 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
209
210                                         } else {
211                                                 set[ last ] = rset[ i ];
212                                         }
213                                         last++;
214                                 }
215                         }
216
217                         /* pointers to values have been used in set - don't free twice */
218                         op_flags |= SLAP_SET_RREFVAL;
219
220                         BER_BVZERO( &set[ last ] );
221                 }
222                 break;
223
224         case '&':       /* intersection */
225                 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] )
226                         || rset == NULL || BER_BVISNULL( &rset[ 0 ] ) )
227                 {
228                         set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
229                                         cp->set_op->o_tmpmemctx );
230                         BER_BVZERO( &set[ 0 ] );
231                         break;
232
233                 } else {
234                         long llen, rlen;
235                         BerVarray sset;
236
237                         llen = slap_set_size( lset );
238                         rlen = slap_set_size( rset );
239
240                         /* dup the shortest */
241                         if ( llen < rlen ) {
242                                 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
243                                 lset = NULL;
244                                 sset = rset;
245
246                         } else {
247                                 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
248                                 rset = NULL;
249                                 sset = lset;
250                         }
251
252                         if ( set == NULL ) {
253                                 break;
254                         }
255
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 ] ) ) {
260                                                 break;
261                                         }
262                                 }
263
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 ] );
268                                         i--;
269                                 }
270                         }
271                 }
272                 break;
273
274         case '+':       /* string concatenation */
275                 i = slap_set_size( rset );
276                 j = slap_set_size( lset );
277
278                 /* handle empty set cases */
279                 if ( i == 0 ) {
280                         if ( j == 0 ) {
281                                 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
282                                                 cp->set_op->o_tmpmemctx );
283                                 if ( set == NULL ) {
284                                         break;
285                                 }
286                                 BER_BVZERO( &set[ 0 ] );
287                                 break;
288
289                         } else {
290                                 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
291                                 break;
292                         }
293
294                 } else if ( j == 0 ) {
295                         set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
296                         break;
297                 }
298
299                 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
300                                 cp->set_op->o_tmpmemctx );
301                 if ( set == NULL ) {
302                         break;
303                 }
304
305                 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
306                         for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
307                                 struct berval   bv;
308                                 long            k;
309
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 );
315                                                 set = NULL;
316                                                 goto done;
317                                         }
318
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 );
323                                                 set = NULL;
324                                                 goto done;
325                                         }
326
327                                 } else {
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 );
333                                                 set = NULL;
334                                                 goto done;
335                                         }
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';
339                                 }
340
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 );
344                                                 break;
345                                         }
346                                 }
347
348                                 if ( k == last ) {
349                                         set[ last++ ] = bv;
350                                 }
351                         }
352                 }
353                 BER_BVZERO( &set[ last ] );
354                 break;
355
356         default:
357                 break;
358         }
359
360 done:;
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 ) );
363
364         return set;
365 }
366
367 static BerVarray
368 set_chase( SLAP_SET_GATHER gatherer,
369         SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
370 {
371         BerVarray       vals, nset;
372         int             i;
373
374         if ( set == NULL ) {
375                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
376                                 cp->set_op->o_tmpmemctx );
377                 if ( set != NULL ) {
378                         BER_BVZERO( &set[ 0 ] );
379                 }
380                 return set;
381         }
382
383         if ( BER_BVISNULL( set ) ) {
384                 return set;
385         }
386
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 );
390                 return NULL;
391         }
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 );
396                 }
397         }
398         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
399
400         if ( closure ) {
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 ) {
406                                         break;
407                                 }
408                         }
409                 }
410         }
411
412         return nset;
413 }
414
415
416 static BerVarray
417 set_parents( SetCookie *cp, BerVarray set )
418 {
419         int             i, j, last;
420         struct berval   bv, pbv;
421         BerVarray       nset, vals;
422
423         if ( set == NULL ) {
424                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
425                                 cp->set_op->o_tmpmemctx );
426                 if ( set != NULL ) {
427                         BER_BVZERO( &set[ 0 ] );
428                 }
429                 return set;
430         }
431
432         if ( BER_BVISNULL( &set[ 0 ] ) ) {
433                 return set;
434         }
435
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 );
439                 return NULL;
440         }
441
442         BER_BVZERO( &nset[ 0 ] );
443
444         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
445                 int     level = 1;
446
447                 pbv = bv = set[ i ];
448                 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
449                         level++;
450                         bv = pbv;
451                 }
452
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 );
457                         return NULL;
458                 }
459                 BER_BVZERO( &vals[ 0 ] );
460                 last = 0;
461
462                 bv = set[ i ];
463                 for ( j = 0 ; j < level ; j++ ) {
464                         ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
465                         last++;
466                         dnParent( &bv, &bv );
467                 }
468                 BER_BVZERO( &vals[ last ] );
469
470                 nset = slap_set_join( cp, nset, '|', vals );
471         }
472
473         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
474
475         return nset;
476 }
477
478
479
480 static BerVarray
481 set_parent( SetCookie *cp, BerVarray set, int level )
482 {
483         int             i, j, last;
484         struct berval   bv;
485         BerVarray       nset;
486
487         if ( set == NULL ) {
488                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
489                                 cp->set_op->o_tmpmemctx );
490                 if ( set != NULL ) {
491                         BER_BVZERO( &set[ 0 ] );
492                 }
493                 return set;
494         }
495
496         if ( BER_BVISNULL( &set[ 0 ] ) ) {
497                 return set;
498         }
499
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 );
503                 return NULL;
504         }
505
506         BER_BVZERO( &nset[ 0 ] );
507         last = 0;
508
509         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
510                 bv = set[ i ];
511
512                 for ( j = 0 ; j < level ; j++ ) {
513                         dnParent( &bv, &bv );
514                 }
515
516                 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
517                         if ( bvmatch( &bv, &nset[ j ] ) )
518                         {
519                                 break;          
520                         }       
521                 }
522
523                 if ( BER_BVISNULL( &nset[ j ] ) ) {
524                         ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
525                         last++;
526                 }
527         }
528
529         BER_BVZERO( &nset[ last ] );
530
531         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
532
533         return nset;
534 }
535
536 int
537 slap_set_filter( SLAP_SET_GATHER gatherer,
538         SetCookie *cp, struct berval *fbv,
539         struct berval *user, struct berval *target, BerVarray *results )
540 {
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); \
550         } while ( 0 )
551
552         BerVarray       set, lset;
553         BerVarray       stack[ STACK_SIZE ] = { 0 };
554         int             len, rc, stp;
555         unsigned long   op;
556         char            c, *filter = fbv->bv_val;
557
558         if ( results ) {
559                 *results = NULL;
560         }
561
562         stp = -1;
563         while ( ( c = *filter++ ) ) {
564                 set = NULL;
565                 switch ( c ) {
566                 case ' ':
567                 case '\t':
568                 case '\x0A':
569                 case '\x0D':
570                         break;
571
572                 case '(' /* ) */ :
573                         if ( IS_SET( SF_TOP() ) ) {
574                                 SF_ERROR( syntax );
575                         }
576                         SF_PUSH( c );
577                         break;
578
579                 case /* ( */ ')':
580                         set = SF_POP();
581                         if ( IS_OP( set ) ) {
582                                 SF_ERROR( syntax );
583                         }
584                         if ( SF_TOP() == (void *)'(' /* ) */ ) {
585                                 SF_POP();
586                                 SF_PUSH( set );
587                                 set = NULL;
588
589                         } else if ( IS_OP( SF_TOP() ) ) {
590                                 op = (unsigned long)SF_POP();
591                                 lset = SF_POP();
592                                 SF_POP();
593                                 set = slap_set_join( cp, lset, op, set );
594                                 if ( set == NULL ) {
595                                         SF_ERROR( memory );
596                                 }
597                                 SF_PUSH( set );
598                                 set = NULL;
599
600                         } else {
601                                 SF_ERROR( syntax );
602                         }
603                         break;
604
605                 case '|':       /* union */
606                 case '&':       /* intersection */
607                 case '+':       /* string concatenation */
608                         set = SF_POP();
609                         if ( IS_OP( set ) ) {
610                                 SF_ERROR( syntax );
611                         }
612                         if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
613                                 SF_PUSH( set );
614                                 set = NULL;
615
616                         } else if ( IS_OP( SF_TOP() ) ) {
617                                 op = (unsigned long)SF_POP();
618                                 lset = SF_POP();
619                                 set = slap_set_join( cp, lset, op, set );
620                                 if ( set == NULL ) {
621                                         SF_ERROR( memory );
622                                 }
623                                 SF_PUSH( set );
624                                 set = NULL;
625                                 
626                         } else {
627                                 SF_ERROR( syntax );
628                         }
629                         SF_PUSH( c );
630                         break;
631
632                 case '[' /* ] */:
633                         if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
634                                 SF_ERROR( syntax );
635                         }
636                         for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
637                                 ;
638                         if ( c == 0 ) {
639                                 SF_ERROR( syntax );
640                         }
641                         
642                         set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
643                                         cp->set_op->o_tmpmemctx );
644                         if ( set == NULL ) {
645                                 SF_ERROR( memory );
646                         }
647                         set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
648                                         cp->set_op->o_tmpmemctx );
649                         if ( BER_BVISNULL( set ) ) {
650                                 SF_ERROR( memory );
651                         }
652                         AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
653                         set->bv_len = len;
654                         SF_PUSH( set );
655                         set = NULL;
656                         break;
657
658                 case '-':
659                         if ( ( SF_TOP() == (void *)'/' )
660                                 && ( *filter == '*' || ASCII_DIGIT( *filter ) ) )
661                         {
662                                 SF_POP();
663
664                                 if ( *filter == '*' ) {
665                                         set = set_parents( cp, SF_POP() );
666                                         filter++;
667
668                                 } else {
669                                         char *next = NULL;
670                                         long parent = strtol( filter, &next, 10 );
671
672                                         if ( next == filter ) {
673                                                 SF_ERROR( syntax );
674                                         }
675
676                                         set = SF_POP();
677                                         if ( parent != 0 ) {
678                                                 set = set_parent( cp, set, parent );
679                                         }
680                                         filter = next;
681                                 }
682
683                                 if ( set == NULL ) {
684                                         SF_ERROR( memory );
685                                 }
686
687                                 SF_PUSH( set );
688                                 set = NULL;
689                                 break;
690                         } else {
691                                 c = *filter++;
692                                 if ( c != '>' ) {
693                                         SF_ERROR( syntax );
694                                 }
695                                 /* fall through to next case */
696                         }
697
698                 case '/':
699                         if ( IS_OP( SF_TOP() ) ) {
700                                 SF_ERROR( syntax );
701                         }
702                         SF_PUSH( '/' );
703                         break;
704
705                 default:
706                         if ( ( c != '_' )
707                                         && ( c < 'A' || c > 'Z' )
708                                         && ( c < 'a' || c > 'z' ) )
709                         {
710                                 SF_ERROR( syntax );
711                         }
712                         filter--;
713                         for ( len = 1;
714                                         ( c = filter[ len ] )
715                                                 && ( ( c >= '0' && c <= '9' )
716                                                         || ( c >= 'A' && c <= 'Z' )
717                                                         || ( c >= 'a' && c <= 'z' ) );
718                                         len++ )
719                                 /* count */ ;
720                         if ( len == 4
721                                 && memcmp( "this", filter, len ) == 0 )
722                         {
723                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
724                                         SF_ERROR( syntax );
725                                 }
726                                 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
727                                                 cp->set_op->o_tmpmemctx );
728                                 if ( set == NULL ) {
729                                         SF_ERROR( memory );
730                                 }
731                                 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
732                                 if ( BER_BVISNULL( set ) ) {
733                                         SF_ERROR( memory );
734                                 }
735                                 BER_BVZERO( &set[ 1 ] );
736                                 
737                         } else if ( len == 4
738                                 && memcmp( "user", filter, len ) == 0 ) 
739                         {
740                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
741                                         SF_ERROR( syntax );
742                                 }
743                                 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
744                                                 cp->set_op->o_tmpmemctx );
745                                 if ( set == NULL ) {
746                                         SF_ERROR( memory );
747                                 }
748                                 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
749                                 if ( BER_BVISNULL( set ) ) {
750                                         SF_ERROR( memory );
751                                 }
752                                 BER_BVZERO( &set[ 1 ] );
753                                 
754                         } else if ( SF_TOP() != (void *)'/' ) {
755                                 SF_ERROR( syntax );
756
757                         } else {
758                                 struct berval           fb2;
759                                 AttributeDescription    *ad = NULL;
760                                 const char              *text = NULL;
761
762                                 SF_POP();
763                                 fb2.bv_val = filter;
764                                 fb2.bv_len = len;
765
766                                 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
767                                         SF_ERROR( syntax );
768                                 }
769
770                                 /* NOTE: ad must have distinguishedName syntax
771                                  * or expand in an LDAP URI if c == '*'
772                                  */
773                                 
774                                 set = set_chase( gatherer,
775                                         cp, SF_POP(), ad, c == '*' );
776                                 if ( set == NULL ) {
777                                         SF_ERROR( memory );
778                                 }
779                                 if ( c == '*' ) {
780                                         len++;
781                                 }
782                         }
783                         filter += len;
784                         SF_PUSH( set );
785                         set = NULL;
786                         break;
787                 }
788         }
789
790         set = SF_POP();
791         if ( IS_OP( set ) ) {
792                 SF_ERROR( syntax );
793         }
794         if ( SF_TOP() == 0 ) {
795                 /* FIXME: ok ? */ ;
796
797         } else if ( IS_OP( SF_TOP() ) ) {
798                 op = (unsigned long)SF_POP();
799                 lset = SF_POP();
800                 set = slap_set_join( cp, lset, op, set );
801                 if ( set == NULL ) {
802                         SF_ERROR( memory );
803                 }
804                 
805         } else {
806                 SF_ERROR( syntax );
807         }
808
809         rc = slap_set_isempty( set ) ? 0 : 1;
810         if ( results ) {
811                 *results = set;
812                 set = NULL;
813         }
814
815 _error:
816         if ( IS_SET( set ) ) {
817                 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
818         }
819         while ( ( set = SF_POP() ) ) {
820                 if ( IS_SET( set ) ) {
821                         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
822                 }
823         }
824         return rc;
825 }