]> git.sur5r.net Git - openldap/blob - servers/slapd/matchedValues.c
Fix cursor initialization, scope IDs
[openldap] / servers / slapd / matchedValues.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2003 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11 #include "portable.h"
12
13 #include <stdio.h>
14
15 #include <ac/string.h>
16 #include <ac/socket.h>
17
18 #include "slap.h"
19
20 #include "../../libraries/liblber/lber-int.h"
21
22 static int
23 test_mra_vrFilter(
24         Operation       *op,
25         Attribute       *a,
26         MatchingRuleAssertion *mra,
27         char            ***e_flags
28 );
29
30 static int
31 test_substrings_vrFilter(
32         Operation       *op,
33         Attribute       *a,
34         ValuesReturnFilter *f,
35         char            ***e_flags
36 );
37
38 static int
39 test_presence_vrFilter(
40         Operation       *op,
41         Attribute       *a,
42         AttributeDescription *desc,
43         char            ***e_flags
44 );
45
46 static int
47 test_ava_vrFilter(
48         Operation       *op,
49         Attribute       *a,
50         AttributeAssertion *ava,
51         int             type,
52         char            ***e_flags
53 );
54
55
56 int
57 filter_matched_values( 
58         Operation       *op,
59         Attribute       *a,
60         char            ***e_flags
61 )
62 {
63         ValuesReturnFilter *vrf;
64         int             rc = LDAP_SUCCESS;
65
66 #ifdef NEW_LOGGING
67         LDAP_LOG( FILTER, ENTRY, "filter_matched_values: begin\n", 0, 0, 0 );
68 #else
69         Debug( LDAP_DEBUG_FILTER, "=> filter_matched_values\n", 0, 0, 0 );
70 #endif
71
72         for ( vrf = op->vrFilter; vrf != NULL; vrf = vrf->vrf_next ) {
73                 switch ( vrf->vrf_choice ) {
74                 case SLAPD_FILTER_COMPUTED:
75 #ifdef NEW_LOGGING
76                         LDAP_LOG( FILTER, DETAIL1, 
77                                 "test_vrFilter: COMPUTED %s (%d)\n",
78                                 vrf->vrf_result == LDAP_COMPARE_FALSE ? "false" :
79                                 vrf->vrf_result == LDAP_COMPARE_TRUE     ? "true"  :
80                                 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
81                                 "error", vrf->vrf_result, 0 );
82 #else
83                         Debug( LDAP_DEBUG_FILTER, "     COMPUTED %s (%d)\n",
84                                 vrf->vrf_result == LDAP_COMPARE_FALSE ? "false" :
85                                 vrf->vrf_result == LDAP_COMPARE_TRUE ? "true" :
86                                 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
87                                 vrf->vrf_result, 0 );
88 #endif
89                         /*This type of filter does not affect the result */
90                         rc = LDAP_SUCCESS;
91                 break;
92
93                 case LDAP_FILTER_EQUALITY:
94 #ifdef NEW_LOGGING
95                         LDAP_LOG( FILTER, DETAIL1, "test_vrFilter: EQUALITY\n", 0, 0, 0 );
96 #else
97                         Debug( LDAP_DEBUG_FILTER, "     EQUALITY\n", 0, 0, 0 );
98 #endif
99                         rc = test_ava_vrFilter( op, a, vrf->vrf_ava,
100                                 LDAP_FILTER_EQUALITY, e_flags );
101                         if( rc == -1 ) {
102                                 return rc;
103                         }
104                         break;
105
106                 case LDAP_FILTER_SUBSTRINGS:
107 #ifdef NEW_LOGGING
108                         LDAP_LOG( FILTER, DETAIL1, "test_vrFilter  SUBSTRINGS\n", 0, 0, 0 );
109 #else
110                         Debug( LDAP_DEBUG_FILTER, "     SUBSTRINGS\n", 0, 0, 0 );
111 #endif
112
113                         rc = test_substrings_vrFilter( op, a,
114                                 vrf, e_flags );
115                         if( rc == -1 ) {
116                                 return rc;
117                         }
118                         break;
119
120                 case LDAP_FILTER_PRESENT:
121 #ifdef NEW_LOGGING
122                         LDAP_LOG( FILTER, DETAIL1, "test_vrFilter:      PRESENT\n", 0, 0, 0 );
123 #else
124                         Debug( LDAP_DEBUG_FILTER, "     PRESENT\n", 0, 0, 0 );
125 #endif
126                         rc = test_presence_vrFilter( op, a,
127                                 vrf->vrf_desc, e_flags );
128                         if( rc == -1 ) {
129                                 return rc;
130                         }
131                         break;
132
133                 case LDAP_FILTER_GE:
134                         rc = test_ava_vrFilter( op, a, vrf->vrf_ava,
135                                 LDAP_FILTER_GE, e_flags );
136                         if( rc == -1 ) {
137                                 return rc;
138                         }
139                         break;
140
141                 case LDAP_FILTER_LE:
142                         rc = test_ava_vrFilter( op, a, vrf->vrf_ava,
143                                 LDAP_FILTER_LE, e_flags );
144                         if( rc == -1 ) {
145                                 return rc;
146                         }
147                         break;
148
149                 case LDAP_FILTER_EXT:
150 #ifdef NEW_LOGGING
151                         LDAP_LOG( FILTER, DETAIL1, "test_vrFilter:      EXT\n", 0, 0, 0 );
152 #else
153                         Debug( LDAP_DEBUG_FILTER, "     EXT\n", 0, 0, 0 );
154 #endif
155                         rc = test_mra_vrFilter( op, a,
156                                 vrf->vrf_mra, e_flags );
157                         if( rc == -1 ) {
158                                 return rc;
159                         }
160                         break;
161
162                 default:
163 #ifdef NEW_LOGGING
164                         LDAP_LOG( FILTER, INFO, 
165                                 "test_vrFilter:  unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
166 #else
167                         Debug( LDAP_DEBUG_ANY, "        unknown filter type %lu\n",
168                                 vrf->vrf_choice, 0, 0 );
169 #endif
170                         rc = LDAP_PROTOCOL_ERROR;
171                 } 
172         }
173
174 #ifdef NEW_LOGGING
175         LDAP_LOG( FILTER, ENTRY, "filter_matched_values:  return=%d\n", rc, 0, 0 );
176 #else
177         Debug( LDAP_DEBUG_FILTER, "<= filter_matched_values %d\n", rc, 0, 0 );
178 #endif
179         return( rc );
180 }
181
182 static int
183 test_ava_vrFilter(
184         Operation       *op,
185         Attribute       *a,
186         AttributeAssertion *ava,
187         int             type,
188         char            ***e_flags
189 )
190 {
191         int             i, j;
192
193         for ( i=0; a != NULL; a = a->a_next, i++ ) {
194
195                 MatchingRule *mr;
196                 struct berval *bv;
197         
198                 if ( !is_ad_subtype( a->a_desc, ava->aa_desc ) ) {
199                         continue;
200                 }
201
202                 switch ( type ) {
203                 case LDAP_FILTER_APPROX:
204                         mr = a->a_desc->ad_type->sat_approx;
205                         if( mr != NULL ) break;
206
207                 /* use EQUALITY matching rule if no APPROX rule */
208                 case LDAP_FILTER_EQUALITY:
209                         mr = a->a_desc->ad_type->sat_equality;
210                         break;
211                 
212                 case LDAP_FILTER_GE:
213                 case LDAP_FILTER_LE:
214                         mr = a->a_desc->ad_type->sat_ordering;
215                         break;
216
217                 default:
218                         mr = NULL;
219                 }
220
221                 if( mr == NULL ) {
222                         continue;
223
224                 }
225
226                 bv = a->a_nvals;
227                 for ( j=0; bv->bv_val != NULL; bv++, j++ ) {
228                         int ret;
229                         int rc;
230                         const char *text;
231
232                         rc = value_match( &ret, a->a_desc, mr, 0,
233                                 bv, &ava->aa_value, &text );
234                         if( rc != LDAP_SUCCESS ) {
235                                 return rc;
236                         }
237
238                         switch ( type ) {
239                         case LDAP_FILTER_EQUALITY:
240                         case LDAP_FILTER_APPROX:
241                                 if ( ret == 0 ) {
242                                         (*e_flags)[i][j] = 1;
243                                 }
244                                 break;
245         
246                         case LDAP_FILTER_GE:
247                                 if ( ret >= 0 ) {
248                                         (*e_flags)[i][j] = 1;
249                                 }
250                                 break;
251         
252                         case LDAP_FILTER_LE:
253                                 if ( ret <= 0 ) {
254                                         (*e_flags)[i][j] = 1;
255                                 }
256                                 break;
257                         }
258                 }
259         }
260         return( LDAP_SUCCESS );
261 }
262
263 static int
264 test_presence_vrFilter(
265         Operation       *op,
266         Attribute       *a,
267         AttributeDescription *desc,
268         char            ***e_flags
269 )
270 {
271         int i, j;
272
273         for ( i=0; a != NULL; a = a->a_next, i++ ) {
274                 struct berval *bv;
275
276                 if ( !is_ad_subtype( a->a_desc, desc ) ) {
277                         continue;
278                 }
279
280                 for ( bv = a->a_vals, j=0; bv->bv_val != NULL; bv++, j++ );
281                 memset( (*e_flags)[i], 1, j);
282         }
283
284         return( LDAP_SUCCESS );
285 }
286
287 static int
288 test_substrings_vrFilter(
289         Operation       *op,
290         Attribute       *a,
291         ValuesReturnFilter *vrf,
292         char            ***e_flags
293 )
294 {
295         int i, j;
296
297         for ( i=0; a != NULL; a = a->a_next, i++ ) {
298                 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
299                 struct berval *bv;
300
301                 if ( !is_ad_subtype( a->a_desc, vrf->vrf_sub_desc ) ) {
302                         continue;
303                 }
304
305                 if( mr == NULL ) {
306                         continue;
307                 }
308
309                 bv = a->a_nvals;
310                 for ( j = 0; bv->bv_val != NULL; bv++, j++ ) {
311                         int ret;
312                         int rc;
313                         const char *text;
314
315                         rc = value_match( &ret, a->a_desc, mr, 0,
316                                 bv, vrf->vrf_sub, &text );
317
318                         if( rc != LDAP_SUCCESS ) {
319                                 return rc;
320                         }
321
322                         if ( ret == 0 ) {
323                                 (*e_flags)[i][j] = 1;
324                         }
325                 }
326         }
327
328         return LDAP_SUCCESS;
329 }
330
331 static int
332 test_mra_vrFilter(
333         Operation       *op,
334         Attribute       *a,
335         MatchingRuleAssertion *mra,
336         char            ***e_flags
337 )
338 {
339         int i, j;
340
341         for ( i=0; a != NULL; a = a->a_next, i++ ) {
342                 struct berval *bv, value;
343
344                 if ( mra->ma_desc ) {
345                         if ( !is_ad_subtype( a->a_desc, mra->ma_desc ) ) {
346                                 continue;
347                         }
348                         value = mra->ma_value;
349
350                 } else {
351                         int rc;
352                         const char      *text = NULL;
353
354                         /* check if matching is appropriate */
355                         if ( strcmp( mra->ma_rule->smr_syntax->ssyn_oid,
356                                 a->a_desc->ad_type->sat_syntax->ssyn_oid ) != 0 ) {
357                                 continue;
358                         }
359
360                         rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
361                                 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
362                                 &mra->ma_value, &value, &text );
363
364                         if( rc != LDAP_SUCCESS ) continue;
365                 }
366
367                 bv = a->a_nvals;
368                 for ( j = 0; bv->bv_val != NULL; bv++, j++ ) {
369                         int ret;
370                         int rc;
371                         const char *text;
372
373                         rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
374                                 bv, &value, &text );
375                         if( rc != LDAP_SUCCESS ) {
376                                 return rc;
377                         }
378
379                         if ( ret == 0 ) {
380                                 (*e_flags)[i][j] = 1;
381                         }
382                 }
383         }
384
385         return LDAP_SUCCESS;
386 }