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