]> git.sur5r.net Git - openldap/blob - servers/slapd/mr.c
Add register_certificate_map_function() native plugin API for registering
[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 (strcasecmp( 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 (strncasecmp( 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                 Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
225                     def->mrd_desc, 0, 0 );
226
227                 return -1;
228         }
229
230         if( def->mrd_associated != NULL ) {
231                 amr = mr_find( def->mrd_associated );
232
233                 if( amr == NULL ) {
234                         Debug( LDAP_DEBUG_ANY, "register_matching_rule: could not locate "
235                                 "associated matching rule %s for %s\n",
236                                 def->mrd_associated, def->mrd_desc, 0 );
237
238                         return -1;
239                 }
240
241                 if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
242                         (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) == SLAP_MR_NONE ))
243                 {
244                         if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
245                                 (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) != SLAP_MR_NONE ))
246                         {
247                                 Debug( LDAP_DEBUG_ANY,
248                                    "register_matching_rule: inappropriate (approx) association "
249                                                 "%s for %s\n",
250                                         def->mrd_associated, def->mrd_desc, 0 );
251                                 return -1;
252                         }
253
254                 } else if (!( amr->smr_usage & SLAP_MR_EQUALITY )) {
255                                 Debug( LDAP_DEBUG_ANY,
256                                    "register_matching_rule: inappropriate (equalilty) association "
257                                                 "%s for %s\n",
258                                         def->mrd_associated, def->mrd_desc, 0 );
259                                 return -1;
260                 }
261         }
262
263         mr = ldap_str2matchingrule( def->mrd_desc, &code, &err,
264                 LDAP_SCHEMA_ALLOW_ALL );
265         if ( !mr ) {
266                 Debug( LDAP_DEBUG_ANY,
267                         "Error in register_matching_rule: %s before %s in %s\n",
268                     ldap_scherr2str(code), err, def->mrd_desc );
269
270                 return -1;
271         }
272
273
274         code = mr_add( mr, def, amr, &err );
275
276         ldap_memfree( mr );
277
278         if ( code ) {
279                 Debug( LDAP_DEBUG_ANY,
280                         "Error in register_matching_rule: %s for %s in %s\n",
281                     scherr2str(code), err, def->mrd_desc );
282
283                 return -1;
284         }
285
286         return 0;
287 }
288
289 void
290 mru_destroy( void )
291 {
292         MatchingRuleUse *m;
293
294         while( !LDAP_SLIST_EMPTY(&mru_list) ) {
295                 m = LDAP_SLIST_FIRST(&mru_list);
296                 LDAP_SLIST_REMOVE_HEAD(&mru_list, smru_next);
297
298                 if ( m->smru_str.bv_val ) {
299                         ch_free( m->smru_str.bv_val );
300                         m->smru_str.bv_val = NULL;
301                 }
302                 /* memory borrowed from m->smru_mr */
303                 m->smru_oid = NULL;
304                 m->smru_names = NULL;
305                 m->smru_desc = NULL;
306
307                 /* free what's left (basically smru_mruleuse.mru_applies_oids) */
308                 ldap_matchingruleuse_free((LDAPMatchingRuleUse *)m);
309         }
310 }
311
312 int
313 matching_rule_use_init( void )
314 {
315         MatchingRule    *mr;
316         MatchingRuleUse **mru_ptr = &LDAP_SLIST_FIRST(&mru_list);
317
318         Debug( LDAP_DEBUG_TRACE, "matching_rule_use_init\n", 0, 0, 0 );
319
320         LDAP_SLIST_FOREACH( mr, &mr_list, smr_next ) {
321                 AttributeType   *at;
322                 MatchingRuleUse mru_storage, *mru = &mru_storage;
323
324                 char            **applies_oids = NULL;
325
326                 mr->smr_mru = NULL;
327
328                 /* hide rules marked as HIDE */
329                 if ( mr->smr_usage & SLAP_MR_HIDE ) {
330                         continue;
331                 }
332
333                 /* hide rules not marked as designed for extensibility */
334                 /* MR_EXT means can be used any attribute type whose
335                  * syntax is same as the assertion syntax.
336                  * Another mechanism is needed where rule can be used
337                  * with attribute of other syntaxes.
338                  * Framework doesn't support this (yet).
339                  */
340
341                 if (!( ( mr->smr_usage & SLAP_MR_EXT )
342                         || mr->smr_compat_syntaxes ) )
343                 {
344                         continue;
345                 }
346
347                 memset( mru, 0, sizeof( MatchingRuleUse ) );
348
349                 /*
350                  * Note: we're using the same values of the corresponding 
351                  * MatchingRule structure; maybe we'd copy them ...
352                  */
353                 mru->smru_mr = mr;
354                 mru->smru_obsolete = mr->smr_obsolete;
355                 mru->smru_applies_oids = NULL;
356                 LDAP_SLIST_NEXT(mru, smru_next) = NULL;
357                 mru->smru_oid = mr->smr_oid;
358                 mru->smru_names = mr->smr_names;
359                 mru->smru_desc = mr->smr_desc;
360
361                 Debug( LDAP_DEBUG_TRACE, "    %s (%s): ", 
362                                 mru->smru_oid, 
363                                 mru->smru_names ? mru->smru_names[ 0 ] : "", 0 );
364
365                 at = NULL;
366                 for ( at_start( &at ); at; at_next( &at ) ) {
367                         if( at->sat_flags & SLAP_AT_HIDE ) continue;
368
369                         if( mr_usable_with_at( mr, at )) {
370                                 ldap_charray_add( &applies_oids, at->sat_cname.bv_val );
371                         }
372                 }
373
374                 /*
375                  * Note: the matchingRules that are not used
376                  * by any attributeType are not listed as
377                  * matchingRuleUse
378                  */
379                 if ( applies_oids != NULL ) {
380                         mru->smru_applies_oids = applies_oids;
381                         {
382                                 char *str = ldap_matchingruleuse2str( &mru->smru_mruleuse );
383                                 Debug( LDAP_DEBUG_TRACE, "matchingRuleUse: %s\n", str, 0, 0 );
384                                 ldap_memfree( str );
385                         }
386
387                         mru = (MatchingRuleUse *)ber_memalloc( sizeof( MatchingRuleUse ) );
388                         /* call-forward from MatchingRule to MatchingRuleUse */
389                         mr->smr_mru = mru;
390                         /* copy static data to newly allocated struct */
391                         *mru = mru_storage;
392                         /* append the struct pointer to the end of the list */
393                         *mru_ptr = mru;
394                         /* update the list head pointer */
395                         mru_ptr = &LDAP_SLIST_NEXT(mru,smru_next);
396                 }
397         }
398
399         return( 0 );
400 }
401
402 int mr_usable_with_at(
403         MatchingRule *mr,
404         AttributeType *at )
405 {
406         if( mr->smr_usage & SLAP_MR_EXT && ( 
407                 mr->smr_syntax == at->sat_syntax ||
408                 mr == at->sat_equality || mr == at->sat_approx ) )
409         {
410                 return 1;
411         }
412
413         if ( mr->smr_compat_syntaxes ) {
414                 int i;
415                 for( i=0; mr->smr_compat_syntaxes[i]; i++ ) {
416                         if( at->sat_syntax == mr->smr_compat_syntaxes[i] ) {
417                                 return 1;
418                         }
419                 }
420         }
421         return 0;
422 }
423
424 int mr_schema_info( Entry *e )
425 {
426         AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;
427         MatchingRule *mr;
428         struct berval nval;
429
430         LDAP_SLIST_FOREACH(mr, &mr_list, smr_next ) {
431                 if ( mr->smr_usage & SLAP_MR_HIDE ) {
432                         /* skip hidden rules */
433                         continue;
434                 }
435
436                 if ( ! mr->smr_match ) {
437                         /* skip rules without matching functions */
438                         continue;
439                 }
440
441                 if ( mr->smr_str.bv_val == NULL ) {
442                         if ( ldap_matchingrule2bv( &mr->smr_mrule, &mr->smr_str ) == NULL ) {
443                                 return -1;
444                         }
445                 }
446 #if 0
447                 Debug( LDAP_DEBUG_TRACE, "Merging mr [%lu] %s\n",
448                         mr->smr_str.bv_len, mr->smr_str.bv_val, 0 );
449 #endif
450
451                 nval.bv_val = mr->smr_oid;
452                 nval.bv_len = strlen(mr->smr_oid);
453                 if( attr_merge_one( e, ad_matchingRules, &mr->smr_str, &nval ) ) {
454                         return -1;
455                 }
456         }
457         return 0;
458 }
459
460 int mru_schema_info( Entry *e )
461 {
462         AttributeDescription *ad_matchingRuleUse 
463                 = slap_schema.si_ad_matchingRuleUse;
464         MatchingRuleUse *mru;
465         struct berval nval;
466
467         LDAP_SLIST_FOREACH( mru, &mru_list, smru_next ) {
468                 assert( !( mru->smru_usage & SLAP_MR_HIDE ) );
469
470                 if ( mru->smru_str.bv_val == NULL ) {
471                         if ( ldap_matchingruleuse2bv( &mru->smru_mruleuse, &mru->smru_str )
472                                         == NULL ) {
473                                 return -1;
474                         }
475                 }
476
477 #if 0
478                 Debug( LDAP_DEBUG_TRACE, "Merging mru [%lu] %s\n",
479                         mru->smru_str.bv_len, mru->smru_str.bv_val, 0 );
480 #endif
481
482                 nval.bv_val = mru->smru_oid;
483                 nval.bv_len = strlen(mru->smru_oid);
484                 if( attr_merge_one( e, ad_matchingRuleUse, &mru->smru_str, &nval ) ) {
485                         return -1;
486                 }
487         }
488         return 0;
489 }