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