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