]> git.sur5r.net Git - openldap/blob - servers/slapd/sets.c
trim use of uninitialized data; please review
[openldap] / servers / slapd / sets.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2005 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->op->o_tmpfree( set, cp->op->o_tmpmemctx );
61                 }
62
63         } else {
64                 ber_bvarray_free_x( set, cp->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->op->o_tmpcalloc( i + 1,
83                                 sizeof(struct berval), 
84                                 cp->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->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->op->o_tmpcalloc( 1,
124                                                         sizeof(struct berval),
125                                                         cp->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->op->o_tmpcalloc( i, sizeof(struct berval), cp->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->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->op->o_tmpfree( rset[ i ].bv_val, cp->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->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->op->o_tmpcalloc( 1, sizeof(struct berval),
192                                         cp->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->op->o_tmpfree( set[ i ].bv_val, cp->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->op->o_tmpcalloc( i * j + 1, sizeof(struct berval),
225                                 cp->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->op->o_tmpalloc( bv.bv_len + 1,
237                                                 cp->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->op->o_tmpfree( bv.bv_val, cp->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                 cp->op->o_tmpfree( lset, cp->op->o_tmpmemctx );
269         }
270
271         if ( !( op_flags & SLAP_SET_RREFARR ) && rset != NULL ) {
272                 cp->op->o_tmpfree( rset, cp->op->o_tmpmemctx );
273         }
274
275         return set;
276 }
277
278 static BerVarray
279 set_chase( SLAP_SET_GATHER gatherer,
280         SetCookie *cp, BerVarray set, AttributeDescription *desc, int closure )
281 {
282         BerVarray       vals, nset;
283         int             i;
284
285         if ( set == NULL ) {
286                 set = cp->op->o_tmpcalloc( 1, sizeof(struct berval),
287                                 cp->op->o_tmpmemctx );
288                 BER_BVZERO( set );
289                 return set;
290         }
291
292         if ( BER_BVISNULL( set ) ) {
293                 return set;
294         }
295
296         nset = cp->op->o_tmpcalloc( 1, sizeof(struct berval), cp->op->o_tmpmemctx );
297         if ( nset == NULL ) {
298                 slap_set_dispose( cp, set, 0 );
299                 return NULL;
300         }
301         for ( i = 0; !BER_BVISNULL( &set[ i ] ); i++ ) {
302                 vals = (gatherer)( cp, &set[ i ], desc );
303                 if ( vals != NULL ) {
304                         nset = slap_set_join( cp, nset, '|', vals );
305                 }
306         }
307         slap_set_dispose( cp, set, 0 );
308
309         if ( closure ) {
310                 for ( i = 0; !BER_BVISNULL( &nset[ i ] ); i++ ) {
311                         vals = (gatherer)( cp, &nset[ i ], desc );
312                         if ( vals != NULL ) {
313                                 nset = slap_set_join( cp, nset, '|', vals );
314                                 if ( nset == NULL ) {
315                                         break;
316                                 }
317                         }
318                 }
319         }
320
321         return nset;
322 }
323
324 int
325 slap_set_filter( SLAP_SET_GATHER gatherer,
326         SetCookie *cp, struct berval *fbv,
327         struct berval *user, struct berval *target, BerVarray *results )
328 {
329 #define STACK_SIZE      64
330 #define IS_SET(x)       ( (unsigned long)(x) >= 256 )
331 #define IS_OP(x)        ( (unsigned long)(x) < 256 )
332 #define SF_ERROR(x)     do { rc = -1; goto _error; } while (0)
333 #define SF_TOP()        ( (BerVarray)( (stp < 0) ? 0 : stack[ stp ] ) )
334 #define SF_POP()        ( (BerVarray)( (stp < 0) ? 0 : stack[ stp-- ] ) )
335 #define SF_PUSH(x)      do { \
336                 if (stp >= (STACK_SIZE - 1)) SF_ERROR(overflow); \
337                 stack[ ++stp ] = (BerVarray)(long)(x); \
338         } while (0)
339
340         BerVarray       set, lset;
341         BerVarray       stack[ STACK_SIZE ] = { 0 };
342         int             len, rc, stp;
343         unsigned        op;
344         char            c, *filter = fbv->bv_val;
345
346         if ( results ) {
347                 *results = NULL;
348         }
349
350         stp = -1;
351         while ( ( c = *filter++ ) ) {
352                 set = NULL;
353                 switch ( c ) {
354                 case ' ':
355                 case '\t':
356                 case '\x0A':
357                 case '\x0D':
358                         break;
359
360                 case '(' /* ) */ :
361                         if ( IS_SET( SF_TOP() ) ) {
362                                 SF_ERROR( syntax );
363                         }
364                         SF_PUSH( c );
365                         break;
366
367                 case /* ( */ ')':
368                         set = SF_POP();
369                         if ( IS_OP( set ) ) {
370                                 SF_ERROR( syntax );
371                         }
372                         if ( SF_TOP() == (void *)'(' /* ) */ ) {
373                                 SF_POP();
374                                 SF_PUSH( set );
375                                 set = NULL;
376
377                         } else if ( IS_OP( SF_TOP() ) ) {
378                                 op = (unsigned)SF_POP();
379                                 lset = SF_POP();
380                                 SF_POP();
381                                 set = slap_set_join( cp, lset, op, set );
382                                 if ( set == NULL ) {
383                                         SF_ERROR(memory);
384                                 }
385                                 SF_PUSH( set );
386                                 set = NULL;
387
388                         } else {
389                                 SF_ERROR( syntax );
390                         }
391                         break;
392
393                 case '|':       /* union */
394                 case '&':       /* intersection */
395                 case '+':       /* string concatenation */
396                         set = SF_POP();
397                         if ( IS_OP( set ) ) {
398                                 SF_ERROR( syntax );
399                         }
400                         if ( SF_TOP() == 0 || SF_TOP() == (void *)'(' /* ) */ ) {
401                                 SF_PUSH( set );
402                                 set = NULL;
403
404                         } else if ( IS_OP( SF_TOP() ) ) {
405                                 op = (unsigned)SF_POP();
406                                 lset = SF_POP();
407                                 set = slap_set_join( cp, lset, op, set );
408                                 if ( set == NULL ) {
409                                         SF_ERROR( memory );
410                                 }
411                                 SF_PUSH( set );
412                                 set = NULL;
413                                 
414                         } else {
415                                 SF_ERROR( syntax );
416                         }
417                         SF_PUSH( c );
418                         break;
419
420                 case '[' /* ] */:
421                         if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
422                                 SF_ERROR( syntax );
423                         }
424                         for ( len = 0; ( c = *filter++ ) && (c != /* [ */ ']'); len++ )
425                                 ;
426                         if ( c == 0 ) {
427                                 SF_ERROR(syntax);
428                         }
429                         
430                         set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
431                                         cp->op->o_tmpmemctx );
432                         if ( set == NULL ) {
433                                 SF_ERROR(memory);
434                         }
435                         set->bv_val = cp->op->o_tmpcalloc( len + 1, sizeof(char),
436                                         cp->op->o_tmpmemctx );
437                         if ( BER_BVISNULL( set ) ) {
438                                 SF_ERROR( memory );
439                         }
440                         AC_MEMCPY( set->bv_val, &filter[ - len - 1 ], len );
441                         set->bv_len = len;
442                         SF_PUSH( set );
443                         set = NULL;
444                         break;
445
446                 case '-':
447                         c = *filter++;
448                         if ( c != '>' ) {
449                                 SF_ERROR( syntax );
450                         }
451                         /* fall through to next case */
452
453                 case '/':
454                         if ( IS_OP( SF_TOP() ) ) {
455                                 SF_ERROR( syntax );
456                         }
457                         SF_PUSH( '/' );
458                         break;
459
460                 default:
461                         if ( ( c != '_' )
462                                         && ( c < 'A' || c > 'Z' )
463                                         && ( c < 'a' || c > 'z' ) )
464                         {
465                                 SF_ERROR( syntax );
466                         }
467                         filter--;
468                         for ( len = 1;
469                                         ( c = filter[ len ] )
470                                                 && ( ( c >= '0' && c <= '9' )
471                                                         || ( c >= 'A' && c <= 'Z' )
472                                                         || ( c >= 'a' && c <= 'z' ) );
473                                         len++ )
474                                 /* count */ ;
475                         if ( len == 4
476                                 && memcmp( "this", filter, len ) == 0 )
477                         {
478                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
479                                         SF_ERROR( syntax );
480                                 }
481                                 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
482                                                 cp->op->o_tmpmemctx );
483                                 if ( set == NULL ) {
484                                         SF_ERROR( memory );
485                                 }
486                                 ber_dupbv_x( set, target, cp->op->o_tmpmemctx );
487                                 if ( BER_BVISNULL( set ) ) {
488                                         SF_ERROR( memory );
489                                 }
490                                 BER_BVZERO( &set[ 1 ] );
491                                 
492                         } else if ( len == 4
493                                 && memcmp( "user", filter, len ) == 0 ) 
494                         {
495                                 if ( ( SF_TOP() == (void *)'/' ) || IS_SET( SF_TOP() ) ) {
496                                         SF_ERROR( syntax );
497                                 }
498                                 set = cp->op->o_tmpcalloc( 2, sizeof(struct berval),
499                                                 cp->op->o_tmpmemctx );
500                                 if ( set == NULL ) {
501                                         SF_ERROR( memory );
502                                 }
503                                 ber_dupbv_x( set, user, cp->op->o_tmpmemctx );
504                                 if ( BER_BVISNULL( set ) ) {
505                                         SF_ERROR( memory );
506                                 }
507                                 BER_BVZERO( &set[ 1 ] );
508                                 
509                         } else if ( SF_TOP() != (void *)'/' ) {
510                                 SF_ERROR( syntax );
511
512                         } else {
513                                 struct berval           fb2;
514                                 AttributeDescription    *ad = NULL;
515                                 const char              *text = NULL;
516
517                                 SF_POP();
518                                 fb2.bv_val = filter;
519                                 fb2.bv_len = len;
520
521                                 if ( slap_bv2ad( &fb2, &ad, &text ) != LDAP_SUCCESS ) {
522                                         SF_ERROR( syntax );
523                                 }
524
525                                 /* NOTE: ad must have distinguishedName syntax
526                                  * or expand in an LDAP URI if c == '*'
527                                  */
528                                 
529                                 set = set_chase( gatherer,
530                                         cp, SF_POP(), ad, c == '*' );
531                                 if ( set == NULL ) {
532                                         SF_ERROR( memory );
533                                 }
534                                 if ( c == '*' ) {
535                                         len++;
536                                 }
537                         }
538                         filter += len;
539                         SF_PUSH( set );
540                         set = NULL;
541                         break;
542                 }
543         }
544
545         set = SF_POP();
546         if ( IS_OP( set ) ) {
547                 SF_ERROR( syntax );
548         }
549         if ( SF_TOP() == 0 ) {
550                 /* FIXME: ok ? */ ;
551
552         } else if ( IS_OP( SF_TOP() ) ) {
553                 op = (unsigned)SF_POP();
554                 lset = SF_POP();
555                 set = slap_set_join( cp, lset, op, set );
556                 if ( set == NULL ) {
557                         SF_ERROR( memory );
558                 }
559                 
560         } else {
561                 SF_ERROR( syntax );
562         }
563
564         rc = slap_set_isempty( set ) ? 0 : 1;
565         if ( results ) {
566                 *results = set;
567                 set = NULL;
568         }
569
570 _error:
571         if ( IS_SET( set ) ) {
572                 slap_set_dispose( cp, set, 0 );
573         }
574         while ( ( set = SF_POP() ) ) {
575                 if ( IS_SET( set ) ) {
576                         slap_set_dispose( cp, set, 0 );
577                 }
578         }
579         return rc;
580 }