]> git.sur5r.net Git - openldap/blob - servers/slapd/sets.c
cleanup sets code; should fix a potential leak and possibly address ITS#4873. Please...
[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                         last = i;
186
187                         for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
188                                 int     exists = 0;
189
190                                 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
191                                         if ( bvmatch( &rset[ i ], &set[ j ] ) )
192                                         {
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 ] );
197                                                 }
198                                                 exists = 1;
199                                                 break;
200                                         }
201                                 }
202
203                                 if ( !exists ) {
204                                         if ( op_flags & SLAP_SET_RREFVAL ) {
205                                                 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
206
207                                         } else {
208                                                 set[ last ] = rset[ i ];
209                                         }
210                                         last++;
211                                 }
212                         }
213                         BER_BVZERO( &set[ last ] );
214                 }
215                 break;
216
217         case '&':       /* intersection */
218                 if ( lset == NULL || BER_BVISNULL( &lset[ 0 ] )
219                         || rset == NULL || BER_BVISNULL( &rset[ 0 ] ) )
220                 {
221                         set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
222                                         cp->set_op->o_tmpmemctx );
223                         BER_BVZERO( &set[ 0 ] );
224                         break;
225
226                 } else {
227                         long llen, rlen;
228                         BerVarray sset;
229
230                         llen = slap_set_size( lset );
231                         rlen = slap_set_size( rset );
232
233                         /* dup the shortest */
234                         if ( llen < rlen ) {
235                                 set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
236                                 lset = NULL;
237                                 sset = rset;
238
239                         } else {
240                                 set = set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
241                                 rset = NULL;
242                                 sset = lset;
243                         }
244
245                         if ( set == NULL ) {
246                                 break;
247                         }
248
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 ] ) ) {
253                                                 break;
254                                         }
255                                 }
256
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 ] );
261                                         i--;
262                                 }
263                         }
264                 }
265                 break;
266
267         case '+':       /* string concatenation */
268                 i = slap_set_size( rset );
269                 j = slap_set_size( lset );
270
271                 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
272                                 cp->set_op->o_tmpmemctx );
273                 if ( set == NULL ) {
274                         break;
275                 }
276
277                 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
278                         for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
279                                 struct berval   bv;
280                                 long            k;
281
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 );
287                                         set = NULL;
288                                         goto done;
289                                 }
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';
293
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 );
297                                                 break;
298                                         }
299                                 }
300
301                                 if ( k == last ) {
302                                         set[ last++ ] = bv;
303                                 }
304                         }
305                 }
306                 BER_BVZERO( &set[ last ] );
307                 break;
308
309         default:
310                 break;
311         }
312
313 done:;
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 ) );
316
317         return set;
318 }
319
320 static BerVarray
321 set_chase( SLAP_SET_GATHER gatherer,
322         SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
323 {
324         BerVarray       vals, nset;
325         int             i;
326
327         if ( set == NULL ) {
328                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
329                                 cp->set_op->o_tmpmemctx );
330                 if ( set != NULL ) {
331                         BER_BVZERO( &set[ 0 ] );
332                 }
333                 return set;
334         }
335
336         if ( BER_BVISNULL( set ) ) {
337                 return set;
338         }
339
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 );
343                 return NULL;
344         }
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 );
349                 }
350         }
351         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
352
353         if ( closure ) {
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 ) {
359                                         break;
360                                 }
361                         }
362                 }
363         }
364
365         return nset;
366 }
367
368
369 static BerVarray
370 set_parents( SetCookie *cp, BerVarray set )
371 {
372         int             i, j, last;
373         struct berval   bv, pbv;
374         BerVarray       nset, vals;
375
376         if ( set == NULL ) {
377                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
378                                 cp->set_op->o_tmpmemctx );
379                 if ( set != NULL ) {
380                         BER_BVZERO( &set[ 0 ] );
381                 }
382                 return set;
383         }
384
385         if ( BER_BVISNULL( &set[ 0 ] ) ) {
386                 return set;
387         }
388
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 );
392                 return NULL;
393         }
394
395         BER_BVZERO( &nset[ 0 ] );
396
397         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
398                 int     level = 1;
399
400                 pbv = bv = set[ i ];
401                 for ( ; !BER_BVISEMPTY( &pbv ); dnParent( &bv, &pbv ) ) {
402                         level++;
403                         bv = pbv;
404                 }
405
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 );
410                         return NULL;
411                 }
412                 BER_BVZERO( &vals[ 0 ] );
413                 last = 0;
414
415                 bv = set[ i ];
416                 for ( j = 0 ; j < level ; j++ ) {
417                         ber_dupbv_x( &vals[ last ], &bv, cp->set_op->o_tmpmemctx );
418                         last++;
419                         dnParent( &bv, &bv );
420                 }
421                 BER_BVZERO( &vals[ last ] );
422
423                 nset = slap_set_join( cp, nset, '|', vals );
424         }
425
426         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
427
428         return nset;
429 }
430
431
432
433 static BerVarray
434 set_parent( SetCookie *cp, BerVarray set, int level )
435 {
436         int             i, j, last;
437         struct berval   bv;
438         BerVarray       nset;
439
440         if ( set == NULL ) {
441                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
442                                 cp->set_op->o_tmpmemctx );
443                 if ( set != NULL ) {
444                         BER_BVZERO( &set[ 0 ] );
445                 }
446                 return set;
447         }
448
449         if ( BER_BVISNULL( &set[ 0 ] ) ) {
450                 return set;
451         }
452
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 );
456                 return NULL;
457         }
458
459         BER_BVZERO( &nset[ 0 ] );
460         last = 0;
461
462         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
463                 bv = set[ i ];
464
465                 for ( j = 0 ; j < level ; j++ ) {
466                         dnParent( &bv, &bv );
467                 }
468
469                 for ( j = 0; !BER_BVISNULL( &nset[ j ] ); j++ ) {
470                         if ( bvmatch( &bv, &nset[ j ] ) )
471                         {
472                                 break;          
473                         }       
474                 }
475
476                 if ( BER_BVISNULL( &nset[ j ] ) ) {
477                         ber_dupbv_x( &nset[ last ], &bv, cp->set_op->o_tmpmemctx );
478                         last++;
479                 }
480         }
481
482         BER_BVZERO( &nset[ last ] );
483
484         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
485
486         return nset;
487 }
488
489 int
490 slap_set_filter( SLAP_SET_GATHER gatherer,
491         SetCookie *cp, struct berval *fbv,
492         struct berval *user, struct berval *target, BerVarray *results )
493 {
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); \
503         } while ( 0 )
504
505         BerVarray       set, lset;
506         BerVarray       stack[ STACK_SIZE ] = { 0 };
507         int             len, rc, stp;
508         unsigned long   op;
509         char            c, *filter = fbv->bv_val;
510
511         if ( results ) {
512                 *results = NULL;
513         }
514
515         stp = -1;
516         while ( ( c = *filter++ ) ) {
517                 set = NULL;
518                 switch ( c ) {
519                 case ' ':
520                 case '\t':
521                 case '\x0A':
522                 case '\x0D':
523                         break;
524
525                 case '(' /* ) */ :
526                         if ( IS_SET( SF_TOP() ) ) {
527                                 SF_ERROR( syntax );
528                         }
529                         SF_PUSH( c );
530                         break;
531
532                 case /* ( */ ')':
533                         set = SF_POP();
534                         if ( IS_OP( set ) ) {
535                                 SF_ERROR( syntax );
536                         }
537                         if ( SF_TOP() == (void *)'(' /* ) */ ) {
538                                 SF_POP();
539                                 SF_PUSH( set );
540                                 set = NULL;
541
542                         } else if ( IS_OP( SF_TOP() ) ) {
543                                 op = (unsigned long)SF_POP();
544                                 lset = SF_POP();
545                                 SF_POP();
546                                 set = slap_set_join( cp, lset, op, set );
547                                 if ( set == NULL ) {
548                                         SF_ERROR( memory );
549                                 }
550                                 SF_PUSH( set );
551                                 set = NULL;
552
553                         } else {
554                                 SF_ERROR( syntax );
555                         }
556                         break;
557
558                 case '|':       /* union */
559                 case '&':       /* intersection */
560                 case '+':       /* string concatenation */
561                         set = SF_POP();
562                         if ( IS_OP( set ) ) {
563                                 SF_ERROR( syntax );
564                         }
565                         if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
566                                 SF_PUSH( set );
567                                 set = NULL;
568
569                         } else if ( IS_OP( SF_TOP() ) ) {
570                                 op = (unsigned long)SF_POP();
571                                 lset = SF_POP();
572                                 set = slap_set_join( cp, lset, op, set );
573                                 if ( set == NULL ) {
574                                         SF_ERROR( memory );
575                                 }
576                                 SF_PUSH( set );
577                                 set = NULL;
578                                 
579                         } else {
580                                 SF_ERROR( syntax );
581                         }
582                         SF_PUSH( c );
583                         break;
584
585                 case '[' /* ] */:
586                         if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
587                                 SF_ERROR( syntax );
588                         }
589                         for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
590                                 ;
591                         if ( c == 0 ) {
592                                 SF_ERROR( syntax );
593                         }
594                         
595                         set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
596                                         cp->set_op->o_tmpmemctx );
597                         if ( set == NULL ) {
598                                 SF_ERROR( memory );
599                         }
600                         set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
601                                         cp->set_op->o_tmpmemctx );
602                         if ( BER_BVISNULL( set ) ) {
603                                 SF_ERROR( memory );
604                         }
605                         AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
606                         set->bv_len = len;
607                         SF_PUSH( set );
608                         set = NULL;
609                         break;
610
611                 case '-':
612                         if ( ( SF_TOP() == (void *)'/' )
613                                         && ( *filter >= '0' || *filter <= '9' ) )
614                         {
615                                 int parent = 0;
616                                 int count = 1;
617                                 int i;
618                                 for ( len = 1;
619                                                 ( c = filter[ len ] )
620                                                         && ( c >= '0' && c <= '9' );
621                                                 len++ )
622                                         /* count */ ;
623                                 for ( i = len ; i > 0 ; i-- ) {
624                                         parent += (int)( filter[ i - 1 ] - '0' ) * count;
625                                         count *= 10;
626                                 }
627                                 SF_POP();
628                                 if ( parent == 0 ) {
629                                         set = set_parents( cp, SF_POP() );
630                                 } else {
631                                         set = set_parent( cp, SF_POP(), parent );
632                                 }
633                                 filter += len;
634                                 SF_PUSH( set );
635                                 set = NULL;
636                                 break;
637                         } else {
638                                 c = *filter++;
639                                 if ( c != '>' ) {
640                                         SF_ERROR( syntax );
641                                 }
642                                 /* fall through to next case */
643                         }
644
645                 case '/':
646                         if ( IS_OP( SF_TOP() ) ) {
647                                 SF_ERROR( syntax );
648                         }
649                         SF_PUSH( '/' );
650                         break;
651
652                 default:
653                         if ( ( c != '_' )
654                                         && ( c < 'A' || c > 'Z' )
655                                         && ( c < 'a' || c > 'z' ) )
656                         {
657                                 SF_ERROR( syntax );
658                         }
659                         filter--;
660                         for ( len = 1;
661                                         ( c = filter[ len ] )
662                                                 && ( ( c >= '0' && c <= '9' )
663                                                         || ( c >= 'A' && c <= 'Z' )
664                                                         || ( c >= 'a' && c <= 'z' ) );
665                                         len++ )
666                                 /* count */ ;
667                         if ( len == 4
668                                 && memcmp( "this", filter, len ) == 0 )
669                         {
670                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
671                                         SF_ERROR( syntax );
672                                 }
673                                 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
674                                                 cp->set_op->o_tmpmemctx );
675                                 if ( set == NULL ) {
676                                         SF_ERROR( memory );
677                                 }
678                                 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
679                                 if ( BER_BVISNULL( set ) ) {
680                                         SF_ERROR( memory );
681                                 }
682                                 BER_BVZERO( &set[ 1 ] );
683                                 
684                         } else if ( len == 4
685                                 && memcmp( "user", filter, len ) == 0 ) 
686                         {
687                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
688                                         SF_ERROR( syntax );
689                                 }
690                                 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
691                                                 cp->set_op->o_tmpmemctx );
692                                 if ( set == NULL ) {
693                                         SF_ERROR( memory );
694                                 }
695                                 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
696                                 if ( BER_BVISNULL( set ) ) {
697                                         SF_ERROR( memory );
698                                 }
699                                 BER_BVZERO( &set[ 1 ] );
700                                 
701                         } else if ( SF_TOP() != (void *)'/' ) {
702                                 SF_ERROR( syntax );
703
704                         } else {
705                                 struct berval           fb2;
706                                 AttributeDescription    *ad = NULL;
707                                 const char              *text = NULL;
708
709                                 SF_POP();
710                                 fb2.bv_val = filter;
711                                 fb2.bv_len = len;
712
713                                 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
714                                         SF_ERROR( syntax );
715                                 }
716
717                                 /* NOTE: ad must have distinguishedName syntax
718                                  * or expand in an LDAP URI if c == '*'
719                                  */
720                                 
721                                 set = set_chase( gatherer,
722                                         cp, SF_POP(), ad, c == '*' );
723                                 if ( set == NULL ) {
724                                         SF_ERROR( memory );
725                                 }
726                                 if ( c == '*' ) {
727                                         len++;
728                                 }
729                         }
730                         filter += len;
731                         SF_PUSH( set );
732                         set = NULL;
733                         break;
734                 }
735         }
736
737         set = SF_POP();
738         if ( IS_OP( set ) ) {
739                 SF_ERROR( syntax );
740         }
741         if ( SF_TOP() == 0 ) {
742                 /* FIXME: ok ? */ ;
743
744         } else if ( IS_OP( SF_TOP() ) ) {
745                 op = (unsigned long)SF_POP();
746                 lset = SF_POP();
747                 set = slap_set_join( cp, lset, op, set );
748                 if ( set == NULL ) {
749                         SF_ERROR( memory );
750                 }
751                 
752         } else {
753                 SF_ERROR( syntax );
754         }
755
756         rc = slap_set_isempty( set ) ? 0 : 1;
757         if ( results ) {
758                 *results = set;
759                 set = NULL;
760         }
761
762 _error:
763         if ( IS_SET( set ) ) {
764                 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
765         }
766         while ( ( set = SF_POP() ) ) {
767                 if ( IS_SET( set ) ) {
768                         ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
769                 }
770         }
771         return rc;
772 }