]> git.sur5r.net Git - openldap/blob - servers/slapd/mr.c
Add SLAP_MR_ORDERED_INDEX - support for inequality indexing. Currently
[openldap] / servers / slapd / mr.c
1 /* mr.c - routines to manage matching rule definitions */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26
27 struct mindexrec {
28         struct berval   mir_name;
29         MatchingRule    *mir_mr;
30 };
31
32 static Avlnode  *mr_index = NULL;
33 static LDAP_SLIST_HEAD(MRList, slap_matching_rule) mr_list
34         = LDAP_SLIST_HEAD_INITIALIZER(&mr_list);
35 static LDAP_SLIST_HEAD(MRUList, slap_matching_rule_use) mru_list
36         = LDAP_SLIST_HEAD_INITIALIZER(&mru_list);
37
38 static int
39 mr_index_cmp(
40     const void  *v_mir1,
41     const void  *v_mir2
42 )
43 {
44         const struct mindexrec  *mir1 = v_mir1;
45         const struct mindexrec  *mir2 = v_mir2;
46         int i = mir1->mir_name.bv_len - mir2->mir_name.bv_len;
47         if (i) return i;
48         return (strcasecmp( mir1->mir_name.bv_val, mir2->mir_name.bv_val ));
49 }
50
51 static int
52 mr_index_name_cmp(
53     const void  *v_name,
54     const void  *v_mir
55 )
56 {
57         const struct berval    *name = v_name;
58         const struct mindexrec *mir  = v_mir;
59         int i = name->bv_len - mir->mir_name.bv_len;
60         if (i) return i;
61         return (strncasecmp( name->bv_val, mir->mir_name.bv_val, name->bv_len ));
62 }
63
64 MatchingRule *
65 mr_find( const char *mrname )
66 {
67         struct berval bv;
68
69         bv.bv_val = (char *)mrname;
70         bv.bv_len = strlen( mrname );
71         return mr_bvfind( &bv );
72 }
73
74 MatchingRule *
75 mr_bvfind( struct berval *mrname )
76 {
77         struct mindexrec        *mir = NULL;
78
79         if ( (mir = avl_find( mr_index, mrname, mr_index_name_cmp )) != NULL ) {
80                 return( mir->mir_mr );
81         }
82         return( NULL );
83 }
84
85 void
86 mr_destroy( void )
87 {
88         MatchingRule *m;
89
90         avl_free(mr_index, ldap_memfree);
91         while( !LDAP_SLIST_EMPTY(&mr_list) ) {
92                 m = LDAP_SLIST_FIRST(&mr_list);
93                 LDAP_SLIST_REMOVE_HEAD(&mr_list, smr_next);
94                 ch_free( m->smr_str.bv_val );
95                 ch_free( m->smr_compat_syntaxes );
96                 ldap_matchingrule_free((LDAPMatchingRule *)m);
97         }
98 }
99
100 static int
101 mr_insert(
102     MatchingRule        *smr,
103     const char          **err
104 )
105 {
106         struct mindexrec        *mir;
107         char                    **names;
108
109         LDAP_SLIST_NEXT( smr, smr_next ) = NULL;
110         LDAP_SLIST_INSERT_HEAD(&mr_list, smr, smr_next);
111
112         if ( smr->smr_oid ) {
113                 mir = (struct mindexrec *)
114                         ch_calloc( 1, sizeof(struct mindexrec) );
115                 mir->mir_name.bv_val = smr->smr_oid;
116                 mir->mir_name.bv_len = strlen( smr->smr_oid );
117                 mir->mir_mr = smr;
118                 if ( avl_insert( &mr_index, (caddr_t) mir,
119                                  mr_index_cmp, avl_dup_error ) ) {
120                         *err = smr->smr_oid;
121                         ldap_memfree(mir);
122                         return SLAP_SCHERR_MR_DUP;
123                 }
124                 /* FIX: temporal consistency check */
125                 mr_bvfind(&mir->mir_name);
126         }
127         if ( (names = smr->smr_names) ) {
128                 while ( *names ) {
129                         mir = (struct mindexrec *)
130                                 ch_calloc( 1, sizeof(struct mindexrec) );
131                         mir->mir_name.bv_val = *names;
132                         mir->mir_name.bv_len = strlen( *names );
133                         mir->mir_mr = smr;
134                         if ( avl_insert( &mr_index, (caddr_t) mir,
135                                          mr_index_cmp, avl_dup_error ) ) {
136                                 *err = *names;
137                                 ldap_memfree(mir);
138                                 return SLAP_SCHERR_MR_DUP;
139                         }
140                         /* FIX: temporal consistency check */
141                         mr_bvfind(&mir->mir_name);
142                         names++;
143                 }
144         }
145         return 0;
146 }
147
148 int
149 mr_add(
150     LDAPMatchingRule            *mr,
151     slap_mrule_defs_rec *def,
152         MatchingRule    *amr,
153     const char          **err
154 )
155 {
156         MatchingRule    *smr;
157         Syntax          *syn;
158         Syntax          **compat_syn = NULL;
159         int             code;
160
161         if( def->mrd_compat_syntaxes ) {
162                 int i;
163                 for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
164                         /* just count em */
165                 }
166
167                 compat_syn = ch_malloc( sizeof(Syntax *) * (i+1) );
168
169                 for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
170                         compat_syn[i] = syn_find( def->mrd_compat_syntaxes[i] );
171                         if( compat_syn[i] == NULL ) {
172                                 return SLAP_SCHERR_SYN_NOT_FOUND;
173                         }
174                 }
175
176                 compat_syn[i] = NULL;
177         }
178
179         smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
180         AC_MEMCPY( &smr->smr_mrule, mr, sizeof(LDAPMatchingRule));
181
182         /*
183          * note: smr_bvoid uses the same memory of smr_mrule.mr_oid;
184          * smr_oidlen is #defined as smr_bvoid.bv_len
185          */
186         smr->smr_bvoid.bv_val = smr->smr_mrule.mr_oid;
187         smr->smr_oidlen = strlen( mr->mr_oid );
188         smr->smr_usage = def->mrd_usage;
189         smr->smr_compat_syntaxes = compat_syn;
190         smr->smr_normalize = def->mrd_normalize;
191         smr->smr_match = def->mrd_match;
192         smr->smr_indexer = def->mrd_indexer;
193         smr->smr_filter = def->mrd_filter;
194         smr->smr_associated = amr;
195
196         if ( smr->smr_syntax_oid ) {
197                 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
198                         smr->smr_syntax = syn;
199                 } else {
200                         *err = smr->smr_syntax_oid;
201                         return SLAP_SCHERR_SYN_NOT_FOUND;
202                 }
203         } else {
204                 *err = "";
205                 return SLAP_SCHERR_MR_INCOMPLETE;
206         }
207         code = mr_insert(smr,err);
208         return code;
209 }
210
211 int
212 register_matching_rule(
213         slap_mrule_defs_rec *def )
214 {
215         LDAPMatchingRule *mr;
216         MatchingRule *amr = NULL;
217         int             code;
218         const char      *err;
219
220         if( def->mrd_usage == SLAP_MR_NONE &&
221                 def->mrd_compat_syntaxes == NULL )
222         {
223                 Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
224                     def->mrd_desc, 0, 0 );
225
226                 return -1;
227         }
228
229         if( def->mrd_associated != NULL ) {
230                 amr = mr_find( def->mrd_associated );
231
232                 if( amr == NULL ) {
233                         Debug( LDAP_DEBUG_ANY, "register_matching_rule: could not locate "
234                                 "associated matching rule %s for %s\n",
235                                 def->mrd_associated, def->mrd_desc, 0 );
236
237                         return -1;
238                 }
239
240                 if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
241                         (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) == SLAP_MR_NONE ))
242                 {
243                         if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
244                                 (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) != SLAP_MR_NONE ))
245                         {
246                                 Debug( LDAP_DEBUG_ANY,
247                                    "register_matching_rule: inappropriate (approx) association "
248                                                 "%s for %s\n",
249                                         def->mrd_associated, def->mrd_desc, 0 );
250                                 return -1;
251                         }
252
253                 } else if (!( amr->smr_usage & SLAP_MR_EQUALITY )) {
254                                 Debug( LDAP_DEBUG_ANY,
255                                    "register_matching_rule: inappropriate (equalilty) association "
256                                                 "%s for %s\n",
257                                         def->mrd_associated, def->mrd_desc, 0 );
258                                 return -1;
259                 }
260         }
261
262         mr = ldap_str2matchingrule( def->mrd_desc, &code, &err,
263                 LDAP_SCHEMA_ALLOW_ALL );
264         if ( !mr ) {
265                 Debug( LDAP_DEBUG_ANY,
266                         "Error in register_matching_rule: %s before %s in %s\n",
267                     ldap_scherr2str(code), err, def->mrd_desc );
268
269                 return -1;
270         }
271
272
273         code = mr_add( mr, def, amr, &err );
274
275         ldap_memfree( mr );
276
277         if ( code ) {
278                 Debug( LDAP_DEBUG_ANY,
279                         "Error in register_matching_rule: %s for %s in %s\n",
280                     scherr2str(code), err, def->mrd_desc );
281
282                 return -1;
283         }
284
285         return 0;
286 }
287
288 void
289 mru_destroy( void )
290 {
291         MatchingRuleUse *m;
292
293         while( !LDAP_SLIST_EMPTY(&mru_list) ) {
294                 m = LDAP_SLIST_FIRST(&mru_list);
295                 LDAP_SLIST_REMOVE_HEAD(&mru_list, smru_next);
296
297                 if ( m->smru_str.bv_val ) {
298                         ch_free( m->smru_str.bv_val );
299                         m->smru_str.bv_val = NULL;
300                 }
301                 /* memory borrowed from m->smru_mr */
302                 m->smru_oid = NULL;
303                 m->smru_names = NULL;
304                 m->smru_desc = NULL;
305
306                 /* free what's left (basically smru_mruleuse.mru_applies_oids) */
307                 ldap_matchingruleuse_free((LDAPMatchingRuleUse *)m);
308         }
309 }
310
311 int
312 matching_rule_use_init( void )
313 {
314         MatchingRule    *mr;
315         MatchingRuleUse **mru_ptr = &LDAP_SLIST_FIRST(&mru_list);
316
317         Debug( LDAP_DEBUG_TRACE, "matching_rule_use_init\n", 0, 0, 0 );
318
319         LDAP_SLIST_FOREACH( mr, &mr_list, smr_next ) {
320                 AttributeType   *at;
321                 MatchingRuleUse mru_storage, *mru = &mru_storage;
322
323                 char            **applies_oids = NULL;
324
325                 mr->smr_mru = NULL;
326
327                 /* hide rules marked as HIDE */
328                 if ( mr->smr_usage & SLAP_MR_HIDE ) {
329                         continue;
330                 }
331
332                 /* hide rules not marked as designed for extensibility */
333                 /* MR_EXT means can be used any attribute type whose
334                  * syntax is same as the assertion syntax.
335                  * Another mechanism is needed where rule can be used
336                  * with attribute of other syntaxes.
337                  * Framework doesn't support this (yet).
338                  */
339
340                 if (!( ( mr->smr_usage & SLAP_MR_EXT )
341                         || mr->smr_compat_syntaxes ) )
342                 {
343                         continue;
344                 }
345
346                 memset( mru, 0, sizeof( MatchingRuleUse ) );
347
348                 /*
349                  * Note: we're using the same values of the corresponding 
350                  * MatchingRule structure; maybe we'd copy them ...
351                  */
352                 mru->smru_mr = mr;
353                 mru->smru_obsolete = mr->smr_obsolete;
354                 mru->smru_applies_oids = NULL;
355                 LDAP_SLIST_NEXT(mru, smru_next) = NULL;
356                 mru->smru_oid = mr->smr_oid;
357                 mru->smru_names = mr->smr_names;
358                 mru->smru_desc = mr->smr_desc;
359
360                 Debug( LDAP_DEBUG_TRACE, "    %s (%s): ", 
361                                 mru->smru_oid, 
362                                 mru->smru_names ? mru->smru_names[ 0 ] : "", 0 );
363
364                 at = NULL;
365                 for ( at_start( &at ); at; at_next( &at ) ) {
366                         if( at->sat_flags & SLAP_AT_HIDE ) continue;
367
368                         if( mr_usable_with_at( mr, at )) {
369                                 ldap_charray_add( &applies_oids, at->sat_cname.bv_val );
370                         }
371                 }
372
373                 /*
374                  * Note: the matchingRules that are not used
375                  * by any attributeType are not listed as
376                  * matchingRuleUse
377                  */
378                 if ( applies_oids != NULL ) {
379                         mru->smru_applies_oids = applies_oids;
380                         {
381                                 char *str = ldap_matchingruleuse2str( &mru->smru_mruleuse );
382                                 Debug( LDAP_DEBUG_TRACE, "matchingRuleUse: %s\n", str, 0, 0 );
383                                 ldap_memfree( str );
384                         }
385
386                         mru = (MatchingRuleUse *)ber_memalloc( sizeof( MatchingRuleUse ) );
387                         /* call-forward from MatchingRule to MatchingRuleUse */
388                         mr->smr_mru = mru;
389                         /* copy static data to newly allocated struct */
390                         *mru = mru_storage;
391                         /* append the struct pointer to the end of the list */
392                         *mru_ptr = mru;
393                         /* update the list head pointer */
394                         mru_ptr = &LDAP_SLIST_NEXT(mru,smru_next);
395                 }
396         }
397
398         return( 0 );
399 }
400
401 int mr_usable_with_at(
402         MatchingRule *mr,
403         AttributeType *at )
404 {
405         if( mr->smr_usage & SLAP_MR_EXT && ( 
406                 mr->smr_syntax == at->sat_syntax ||
407                 mr == at->sat_equality || mr == at->sat_approx ) )
408         {
409                 return 1;
410         }
411
412         if ( mr->smr_compat_syntaxes ) {
413                 int i;
414                 for( i=0; mr->smr_compat_syntaxes[i]; i++ ) {
415                         if( at->sat_syntax == mr->smr_compat_syntaxes[i] ) {
416                                 return 1;
417                         }
418                 }
419         }
420         return 0;
421 }
422
423 int mr_schema_info( Entry *e )
424 {
425         AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;
426         MatchingRule *mr;
427         struct berval nval;
428
429         LDAP_SLIST_FOREACH(mr, &mr_list, smr_next ) {
430                 if ( mr->smr_usage & SLAP_MR_HIDE ) {
431                         /* skip hidden rules */
432                         continue;
433                 }
434
435                 if ( ! mr->smr_match ) {
436                         /* skip rules without matching functions */
437                         continue;
438                 }
439
440                 if ( mr->smr_str.bv_val == NULL ) {
441                         if ( ldap_matchingrule2bv( &mr->smr_mrule, &mr->smr_str ) == NULL ) {
442                                 return -1;
443                         }
444                 }
445 #if 0
446                 Debug( LDAP_DEBUG_TRACE, "Merging mr [%lu] %s\n",
447                         mr->smr_str.bv_len, mr->smr_str.bv_val, 0 );
448 #endif
449
450                 nval.bv_val = mr->smr_oid;
451                 nval.bv_len = strlen(mr->smr_oid);
452                 if( attr_merge_one( e, ad_matchingRules, &mr->smr_str, &nval ) ) {
453                         return -1;
454                 }
455         }
456         return 0;
457 }
458
459 int mru_schema_info( Entry *e )
460 {
461         AttributeDescription *ad_matchingRuleUse 
462                 = slap_schema.si_ad_matchingRuleUse;
463         MatchingRuleUse *mru;
464         struct berval nval;
465
466         LDAP_SLIST_FOREACH( mru, &mru_list, smru_next ) {
467                 assert( !( mru->smru_usage & SLAP_MR_HIDE ) );
468
469                 if ( mru->smru_str.bv_val == NULL ) {
470                         if ( ldap_matchingruleuse2bv( &mru->smru_mruleuse, &mru->smru_str )
471                                         == NULL ) {
472                                 return -1;
473                         }
474                 }
475
476 #if 0
477                 Debug( LDAP_DEBUG_TRACE, "Merging mru [%lu] %s\n",
478                         mru->smru_str.bv_len, mru->smru_str.bv_val, 0 );
479 #endif
480
481                 nval.bv_val = mru->smru_oid;
482                 nval.bv_len = strlen(mru->smru_oid);
483                 if( attr_merge_one( e, ad_matchingRuleUse, &mru->smru_str, &nval ) ) {
484                         return -1;
485                 }
486         }
487         return 0;
488 }