]> git.sur5r.net Git - openldap/blob - servers/slapd/sets.c
happy new year
[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 static long
28 slap_set_size( BerVarray set )
29 {
30         long    i;
31
32         i = 0;
33         if ( set != NULL ) {
34                 while ( !BER_BVISNULL( &set[ i ] ) ) {
35                         i++;
36                 }
37         }
38         return i;
39 }
40
41 static int
42 slap_set_isempty( BerVarray set )
43 {
44         if ( set == NULL ) {
45                 return 1;
46         }
47
48         if ( !BER_BVISNULL( &set[ 0 ] ) ) {
49                 return 0;
50         }
51
52         return 1;
53 }
54
55 static void
56 slap_set_dispose( SetCookie *cp, BerVarray set, unsigned flags )
57 {
58         if ( flags & SLAP_SET_REFVAL ) {
59                 if ( ! ( flags & SLAP_SET_REFARR ) ) {
60                         cp->set_op->o_tmpfree( set, cp->set_op->o_tmpmemctx );
61                 }
62
63         } else {
64                 ber_bvarray_free_x( set, cp->set_op->o_tmpmemctx );
65         }
66 }
67
68 static BerVarray
69 set_dup( SetCookie *cp, BerVarray set, unsigned flags )
70 {
71         BerVarray       newset = NULL;
72
73         if ( set == NULL ) {
74                 return NULL;
75         }
76
77         if ( flags & SLAP_SET_REFARR ) {
78                 int     i;
79
80                 for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ )
81                         ;
82                 newset = cp->set_op->o_tmpcalloc( i + 1,
83                                 sizeof( struct berval ), 
84                                 cp->set_op->o_tmpmemctx );
85                 if ( newset == NULL ) {
86                         return NULL;
87                 }
88
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->set_op->o_tmpmemctx );
93                         }
94
95                 } else {
96                         AC_MEMCPY( newset, set, ( i + 1 ) * sizeof( struct berval ) );
97                 }
98                 
99         } else {
100                 newset = set;
101         }
102
103         return newset;
104 }
105
106 BerVarray
107 slap_set_join(
108         SetCookie       *cp,
109         BerVarray       lset,
110         unsigned        op_flags,
111         BerVarray       rset )
112 {
113         BerVarray       set;
114         long            i, j, last;
115         unsigned        op = ( op_flags & SLAP_SET_OPMASK );
116
117         set = NULL;
118         switch ( op ) {
119         case '|':       /* union */
120                 if ( lset == NULL || BER_BVISNULL( lset ) ) {
121                         if ( rset == NULL ) {
122                                 if ( lset == NULL ) {
123                                         set = cp->set_op->o_tmpcalloc( 1,
124                                                         sizeof( struct berval ),
125                                                         cp->set_op->o_tmpmemctx );
126                                         BER_BVZERO( set );
127                                         return set;
128                                 }
129                                 return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
130                         }
131                         slap_set_dispose( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
132                         return set_dup( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
133                 }
134                 if ( rset == NULL || BER_BVISNULL( rset ) ) {
135                         slap_set_dispose( cp, rset, SLAP_SET_RREF2REF( op_flags ) );
136                         return set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
137                 }
138
139                 i = slap_set_size( lset ) + slap_set_size( rset ) + 1;
140                 set = cp->set_op->o_tmpcalloc( i, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
141                 if ( set != NULL ) {
142                         /* set_chase() depends on this routine to
143                          * keep the first elements of the result
144                          * set the same (and in the same order)
145                          * as the left-set.
146                          */
147                         for ( i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
148                                 if ( op_flags & SLAP_SET_LREFVAL ) {
149                                         ber_dupbv_x( &set[ i ], &lset[ i ], cp->set_op->o_tmpmemctx );
150
151                                 } else {
152                                         set[ i ] = lset[ i ];
153                                 }
154                         }
155
156                         last = i;
157
158                         for ( i = 0; !BER_BVISNULL( &rset[ i ] ); i++ ) {
159                                 int     exists = 0;
160
161                                 for ( j = 0; !BER_BVISNULL( &set[ j ] ); j++ ) {
162                                         if ( bvmatch( &rset[ i ], &set[ j ] ) )
163                                         {
164                                                 if ( !( op_flags & SLAP_SET_RREFVAL ) ) {
165                                                         cp->set_op->o_tmpfree( rset[ i ].bv_val, cp->set_op->o_tmpmemctx );
166                                                         BER_BVZERO( &rset[ i ] );
167                                                 }
168                                                 exists = 1;
169                                                 break;          
170                                         }       
171                                 }
172
173                                 if ( !exists ) {
174                                         if ( op_flags & SLAP_SET_RREFVAL ) {
175                                                 ber_dupbv_x( &set[ last ], &rset[ i ], cp->set_op->o_tmpmemctx );
176
177                                         } else {
178                                                 set[ last ] = rset[ i ];
179                                         }
180                                         last++;
181                                 }
182                         }
183                         BER_BVZERO( &set[ last ] );
184                 }
185                 break;
186
187         case '&':       /* intersection */
188                 if ( lset == NULL || BER_BVISNULL( lset )
189                                 || rset == NULL || BER_BVISNULL( rset ) )
190                 {
191                         set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
192                                         cp->set_op->o_tmpmemctx );
193                         BER_BVZERO( set );
194
195                 } else {
196                         set = set_dup( cp, lset, SLAP_SET_LREF2REF( op_flags ) );
197                         if ( set == NULL ) {
198                                 break;
199                         }
200                         lset = NULL;
201                         last = slap_set_size( set ) - 1;
202                         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
203                                 for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
204                                         if ( bvmatch( &set[ i ], &rset[ j ] ) ) {
205                                                 break;
206                                         }
207                                 }
208
209                                 if ( BER_BVISNULL( &rset[ j ] ) ) {
210                                         cp->set_op->o_tmpfree( set[ i ].bv_val, cp->set_op->o_tmpmemctx );
211                                         set[ i ] = set[ last ];
212                                         BER_BVZERO( &set[ last ] );
213                                         last--;
214                                         i--;
215                                 }
216                         }
217                 }
218                 break;
219
220         case '+':       /* string concatenation */
221                 i = slap_set_size( rset );
222                 j = slap_set_size( lset );
223
224                 set = cp->set_op->o_tmpcalloc( i * j + 1, sizeof( struct berval ),
225                                 cp->set_op->o_tmpmemctx );
226                 if ( set == NULL ) {
227                         break;
228                 }
229
230                 for ( last = 0, i = 0; !BER_BVISNULL( &lset[ i ] ); i++ ) {
231                         for ( j = 0; !BER_BVISNULL( &rset[ j ] ); j++ ) {
232                                 struct berval   bv;
233                                 long            k;
234
235                                 bv.bv_len = lset[ i ].bv_len + rset[ j ].bv_len;
236                                 bv.bv_val = cp->set_op->o_tmpalloc( bv.bv_len + 1,
237                                                 cp->set_op->o_tmpmemctx );
238                                 if ( bv.bv_val == NULL ) {
239                                         slap_set_dispose( cp, set, 0 );
240                                         set = NULL;
241                                         goto done;
242                                 }
243                                 AC_MEMCPY( bv.bv_val, lset[ i ].bv_val, lset[ i ].bv_len );
244                                 AC_MEMCPY( &bv.bv_val[ lset[ i ].bv_len ], rset[ j ].bv_val, rset[ j ].bv_len );
245                                 bv.bv_val[ bv.bv_len ] = '\0';
246
247                                 for ( k = 0; k < last; k++ ) {
248                                         if ( bvmatch( &set[ k ], &bv ) ) {
249                                                 cp->set_op->o_tmpfree( bv.bv_val, cp->set_op->o_tmpmemctx );
250                                                 break;
251                                         }
252                                 }
253
254                                 if ( k == last ) {
255                                         set[ last++ ] = bv;
256                                 }
257                         }
258                 }
259                 BER_BVZERO( &set[ last ] );
260                 break;
261
262         default:
263                 break;
264         }
265
266 done:;
267         if ( !( op_flags & SLAP_SET_LREFARR ) && lset != NULL ) {
268                 if ( !( op_flags & SLAP_SET_LREFVAL ))
269                         cp->set_op->o_tmpfree( lset->bv_val, cp->set_op->o_tmpmemctx );
270                 cp->set_op->o_tmpfree( lset, cp->set_op->o_tmpmemctx );
271         }
272
273         if ( !( op_flags & SLAP_SET_RREFARR ) && rset != NULL ) {
274                 if ( !( op_flags & SLAP_SET_RREFVAL ))
275                         cp->set_op->o_tmpfree( rset->bv_val, cp->set_op->o_tmpmemctx );
276                 cp->set_op->o_tmpfree( rset, cp->set_op->o_tmpmemctx );
277         }
278
279         return set;
280 }
281
282 static BerVarray
283 set_chase( SLAP_SET_GATHER gatherer,
284         SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
285 {
286         BerVarray       vals, nset;
287         int             i;
288
289         if ( set == NULL ) {
290                 set = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ),
291                                 cp->set_op->o_tmpmemctx );
292                 BER_BVZERO( set );
293                 return set;
294         }
295
296         if ( BER_BVISNULL( set ) ) {
297                 return set;
298         }
299
300         nset = cp->set_op->o_tmpcalloc( 1, sizeof( struct berval ), cp->set_op->o_tmpmemctx );
301         if ( nset == NULL ) {
302                 slap_set_dispose( cp, set, 0 );
303                 return NULL;
304         }
305         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
306                 vals = gatherer( cp, &set[ i ], desc );
307                 if ( vals != NULL ) {
308                         nset = slap_set_join( cp, nset, '|', vals );
309                 }
310         }
311         slap_set_dispose( cp, set, 0 );
312
313         if ( closure ) {
314                 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
315                         vals = gatherer( cp, &nset[ i ], desc );
316                         if ( vals != NULL ) {
317                                 nset = slap_set_join( cp, nset, '|', vals );
318                                 if ( nset == NULL ) {
319                                         break;
320                                 }
321                         }
322                 }
323         }
324
325         return nset;
326 }
327
328 int
329 slap_set_filter( SLAP_SET_GATHER gatherer,
330         SetCookie *cp, struct berval *fbv,
331         struct berval *user, struct berval *target, BerVarray *results )
332 {
333 #define STACK_SIZE      64
334 #define IS_SET(x)       ( (unsigned long)(x) >= 256 )
335 #define IS_OP(x)        ( (unsigned long)(x) < 256 )
336 #define SF_ERROR(x)     do { rc = -1; goto _error; } while ( 0 )
337 #define SF_TOP()        ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp ] ) )
338 #define SF_POP()        ( (BerVarray)( ( stp < 0 ) ? 0 : stack[ stp-- ] ) )
339 #define SF_PUSH(x)      do { \
340                 if ( stp >= ( STACK_SIZE - 1 ) ) SF_ERROR( overflow ); \
341                 stack[ ++stp ] = (BerVarray)(long)(x); \
342         } while ( 0 )
343
344         BerVarray       set, lset;
345         BerVarray       stack[ STACK_SIZE ] = { 0 };
346         int             len, rc, stp;
347         unsigned long   op;
348         char            c, *filter = fbv->bv_val;
349
350         if ( results ) {
351                 *results = NULL;
352         }
353
354         stp = -1;
355         while ( ( c = *filter++ ) ) {
356                 set = NULL;
357                 switch ( c ) {
358                 case ' ':
359                 case '\t':
360                 case '\x0A':
361                 case '\x0D':
362                         break;
363
364                 case '(' /* ) */ :
365                         if ( IS_SET( SF_TOP() ) ) {
366                                 SF_ERROR( syntax );
367                         }
368                         SF_PUSH( c );
369                         break;
370
371                 case /* ( */ ')':
372                         set = SF_POP();
373                         if ( IS_OP( set ) ) {
374                                 SF_ERROR( syntax );
375                         }
376                         if ( SF_TOP() == (void *)'(' /* ) */ ) {
377                                 SF_POP();
378                                 SF_PUSH( set );
379                                 set = NULL;
380
381                         } else if ( IS_OP( SF_TOP() ) ) {
382                                 op = (unsigned long)SF_POP();
383                                 lset = SF_POP();
384                                 SF_POP();
385                                 set = slap_set_join( cp, lset, op, set );
386                                 if ( set == NULL ) {
387                                         SF_ERROR( memory );
388                                 }
389                                 SF_PUSH( set );
390                                 set = NULL;
391
392                         } else {
393                                 SF_ERROR( syntax );
394                         }
395                         break;
396
397                 case '|':       /* union */
398                 case '&':       /* intersection */
399                 case '+':       /* string concatenation */
400                         set = SF_POP();
401                         if ( IS_OP( set ) ) {
402                                 SF_ERROR( syntax );
403                         }
404                         if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
405                                 SF_PUSH( set );
406                                 set = NULL;
407
408                         } else if ( IS_OP( SF_TOP() ) ) {
409                                 op = (unsigned long)SF_POP();
410                                 lset = SF_POP();
411                                 set = slap_set_join( cp, lset, op, set );
412                                 if ( set == NULL ) {
413                                         SF_ERROR( memory );
414                                 }
415                                 SF_PUSH( set );
416                                 set = NULL;
417                                 
418                         } else {
419                                 SF_ERROR( syntax );
420                         }
421                         SF_PUSH( c );
422                         break;
423
424                 case '[' /* ] */:
425                         if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
426                                 SF_ERROR( syntax );
427                         }
428                         for ( len = 0; ( c = *filter++ ) && ( c != /* [ */ ']' ); len++ )
429                                 ;
430                         if ( c == 0 ) {
431                                 SF_ERROR( syntax );
432                         }
433                         
434                         set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
435                                         cp->set_op->o_tmpmemctx );
436                         if ( set == NULL ) {
437                                 SF_ERROR( memory );
438                         }
439                         set->bv_val = cp->set_op->o_tmpcalloc( len + 1, sizeof( char ),
440                                         cp->set_op->o_tmpmemctx );
441                         if ( BER_BVISNULL( set ) ) {
442                                 SF_ERROR( memory );
443                         }
444                         AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
445                         set->bv_len = len;
446                         SF_PUSH( set );
447                         set = NULL;
448                         break;
449
450                 case '-':
451                         c = *filter++;
452                         if ( c != '>' ) {
453                                 SF_ERROR( syntax );
454                         }
455                         /* fall through to next case */
456
457                 case '/':
458                         if ( IS_OP( SF_TOP() ) ) {
459                                 SF_ERROR( syntax );
460                         }
461                         SF_PUSH( '/' );
462                         break;
463
464                 default:
465                         if ( ( c != '_' )
466                                         && ( c < 'A' || c > 'Z' )
467                                         && ( c < 'a' || c > 'z' ) )
468                         {
469                                 SF_ERROR( syntax );
470                         }
471                         filter--;
472                         for ( len = 1;
473                                         ( c = filter[ len ] )
474                                                 && ( ( c >= '0' && c <= '9' )
475                                                         || ( c >= 'A' && c <= 'Z' )
476                                                         || ( c >= 'a' && c <= 'z' ) );
477                                         len++ )
478                                 /* count */ ;
479                         if ( len == 4
480                                 && memcmp( "this", filter, len ) == 0 )
481                         {
482                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
483                                         SF_ERROR( syntax );
484                                 }
485                                 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
486                                                 cp->set_op->o_tmpmemctx );
487                                 if ( set == NULL ) {
488                                         SF_ERROR( memory );
489                                 }
490                                 ber_dupbv_x( set, target, cp->set_op->o_tmpmemctx );
491                                 if ( BER_BVISNULL( set ) ) {
492                                         SF_ERROR( memory );
493                                 }
494                                 BER_BVZERO( &set[ 1 ] );
495                                 
496                         } else if ( len == 4
497                                 && memcmp( "user", filter, len ) == 0 ) 
498                         {
499                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
500                                         SF_ERROR( syntax );
501                                 }
502                                 set = cp->set_op->o_tmpcalloc( 2, sizeof( struct berval ),
503                                                 cp->set_op->o_tmpmemctx );
504                                 if ( set == NULL ) {
505                                         SF_ERROR( memory );
506                                 }
507                                 ber_dupbv_x( set, user, cp->set_op->o_tmpmemctx );
508                                 if ( BER_BVISNULL( set ) ) {
509                                         SF_ERROR( memory );
510                                 }
511                                 BER_BVZERO( &set[ 1 ] );
512                                 
513                         } else if ( SF_TOP() != (void *)'/' ) {
514                                 SF_ERROR( syntax );
515
516                         } else {
517                                 struct berval           fb2;
518                                 AttributeDescription    *ad = NULL;
519                                 const char              *text = NULL;
520
521                                 SF_POP();
522                                 fb2.bv_val = filter;
523                                 fb2.bv_len = len;
524
525                                 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
526                                         SF_ERROR( syntax );
527                                 }
528
529                                 /* NOTE: ad must have distinguishedName syntax
530                                  * or expand in an LDAP URI if c == '*'
531                                  */
532                                 
533                                 set = set_chase( gatherer,
534                                         cp, SF_POP(), ad, c == '*' );
535                                 if ( set == NULL ) {
536                                         SF_ERROR( memory );
537                                 }
538                                 if ( c == '*' ) {
539                                         len++;
540                                 }
541                         }
542                         filter += len;
543                         SF_PUSH( set );
544                         set = NULL;
545                         break;
546                 }
547         }
548
549         set = SF_POP();
550         if ( IS_OP( set ) ) {
551                 SF_ERROR( syntax );
552         }
553         if ( SF_TOP() == 0 ) {
554                 /* FIXME: ok ? */ ;
555
556         } else if ( IS_OP( SF_TOP() ) ) {
557                 op = (unsigned long)SF_POP();
558                 lset = SF_POP();
559                 set = slap_set_join( cp, lset, op, set );
560                 if ( set == NULL ) {
561                         SF_ERROR( memory );
562                 }
563                 
564         } else {
565                 SF_ERROR( syntax );
566         }
567
568         rc = slap_set_isempty( set ) ? 0 : 1;
569         if ( results ) {
570                 *results = set;
571                 set = NULL;
572         }
573
574 _error:
575         if ( IS_SET( set ) ) {
576                 slap_set_dispose( cp, set, 0 );
577         }
578         while ( ( set = SF_POP() ) ) {
579                 if ( IS_SET( set ) ) {
580                         slap_set_dispose( cp, set, 0 );
581                 }
582         }
583         return rc;
584 }