]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
ITS#3538: improved substrings matching (spaces handled
[openldap] / servers / slapd / at.c
1 /* at.c - routines for dealing with attribute types */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 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/errno.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "slap.h"
28
29
30 int is_at_syntax(
31         AttributeType *at,
32         const char *oid )
33 {
34         for( ; at != NULL; at = at->sat_sup ) {
35                 if( at->sat_syntax_oid ) {
36                         return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
37                 }
38         }
39
40         return 0;
41 }
42
43 int is_at_subtype(
44         AttributeType *sub,
45         AttributeType *sup )
46 {
47         for( ; sub != NULL; sub = sub->sat_sup ) {
48                 if( sub == sup ) return 1;
49         }
50
51         return 0;
52 }
53
54 struct aindexrec {
55         struct berval   air_name;
56         AttributeType   *air_at;
57 };
58
59 static Avlnode  *attr_index = NULL;
60 static Avlnode  *attr_cache = NULL;
61 static LDAP_STAILQ_HEAD(ATList, slap_attribute_type) attr_list
62         = LDAP_STAILQ_HEAD_INITIALIZER(attr_list);
63
64 int at_oc_cache;
65
66 static int
67 attr_index_cmp(
68     const void  *v_air1,
69     const void  *v_air2 )
70 {
71         const struct aindexrec  *air1 = v_air1;
72         const struct aindexrec  *air2 = v_air2;
73         int i = air1->air_name.bv_len - air2->air_name.bv_len;
74         if (i) return i;
75         return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
76 }
77
78 static int
79 attr_index_name_cmp(
80     const void  *v_type,
81     const void  *v_air )
82 {
83     const struct berval    *type = v_type;
84     const struct aindexrec *air  = v_air;
85         int i = type->bv_len - air->air_name.bv_len;
86         if (i) return i;
87         return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
88 }
89
90 AttributeType *
91 at_find( const char *name )
92 {
93         struct berval bv;
94
95         bv.bv_val = (char *)name;
96         bv.bv_len = strlen( name );
97
98         return at_bvfind( &bv );
99 }
100
101 AttributeType *
102 at_bvfind( struct berval *name )
103 {
104         struct aindexrec *air;
105
106         if ( attr_cache ) {
107                 air = avl_find( attr_cache, name, attr_index_name_cmp );
108                 if ( air ) return air->air_at;
109         }
110
111         air = avl_find( attr_index, name, attr_index_name_cmp );
112
113         if ( air && ( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
114                 avl_insert( &attr_cache, (caddr_t) air,
115                         attr_index_cmp, avl_dup_error );
116         }
117
118         return air != NULL ? air->air_at : NULL;
119 }
120
121 int
122 at_append_to_list(
123     AttributeType       *sat,
124     AttributeType       ***listp )
125 {
126         AttributeType   **list;
127         AttributeType   **list1;
128         int             size;
129
130         list = *listp;
131         if ( !list ) {
132                 size = 2;
133                 list = ch_calloc(size, sizeof(AttributeType *));
134                 if ( !list ) {
135                         return -1;
136                 }
137         } else {
138                 size = 0;
139                 list1 = *listp;
140                 while ( *list1 ) {
141                         size++;
142                         list1++;
143                 }
144                 size += 2;
145                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
146                 if ( !list1 ) {
147                         return -1;
148                 }
149                 list = list1;
150         }
151         list[size-2] = sat;
152         list[size-1] = NULL;
153         *listp = list;
154         return 0;
155 }
156
157 int
158 at_delete_from_list(
159     int                 pos,
160     AttributeType       ***listp )
161 {
162         AttributeType   **list;
163         AttributeType   **list1;
164         int             i;
165         int             j;
166
167         if ( pos < 0 ) {
168                 return -2;
169         }
170         list = *listp;
171         for ( i=0; list[i]; i++ )
172                 ;
173         if ( pos >= i ) {
174                 return -2;
175         }
176         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
177                 list[i] = list[j];
178         }
179         list[i] = NULL;
180         /* Tell the runtime this can be shrinked */
181         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
182         if ( !list1 ) {
183                 return -1;
184         }
185         *listp = list1;
186         return 0;
187 }
188
189 int
190 at_find_in_list(
191     AttributeType       *sat,
192     AttributeType       **list )
193 {
194         int     i;
195
196         if ( !list ) {
197                 return -1;
198         }
199         for ( i=0; list[i]; i++ ) {
200                 if ( sat == list[i] ) {
201                         return i;
202                 }
203         }
204         return -1;
205 }
206
207 void
208 at_destroy( void )
209 {
210         AttributeType *a;
211         avl_free(attr_index, ldap_memfree);
212
213         while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
214                 a = LDAP_STAILQ_FIRST(&attr_list);
215                 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
216
217                 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
218                 ad_destroy(a->sat_ad);
219                 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
220                 ldap_attributetype_free((LDAPAttributeType *)a);
221         }
222
223         if ( slap_schema.si_at_undefined ) {
224                 ad_destroy(slap_schema.si_at_undefined->sat_ad);
225         }
226 }
227
228 int
229 at_start( AttributeType **at )
230 {
231         assert( at );
232
233         *at = LDAP_STAILQ_FIRST(&attr_list);
234
235         return (*at != NULL);
236 }
237
238 int
239 at_next( AttributeType **at )
240 {
241         assert( at );
242
243 #if 1   /* pedantic check */
244         {
245                 AttributeType *tmp = NULL;
246
247                 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
248                         if ( tmp == *at ) {
249                                 break;
250                         }
251                 }
252
253                 assert( tmp );
254         }
255 #endif
256
257         *at = LDAP_STAILQ_NEXT(*at,sat_next);
258
259         return (*at != NULL);
260 }
261         
262
263
264 static int
265 at_insert(
266     AttributeType       *sat,
267     const char          **err )
268 {
269         struct aindexrec        *air;
270         char                    **names;
271
272         if ( sat->sat_oid ) {
273                 air = (struct aindexrec *)
274                         ch_calloc( 1, sizeof(struct aindexrec) );
275                 air->air_name.bv_val = sat->sat_oid;
276                 air->air_name.bv_len = strlen(sat->sat_oid);
277                 air->air_at = sat;
278                 if ( avl_insert( &attr_index, (caddr_t) air,
279                                  attr_index_cmp, avl_dup_error ) ) {
280                         *err = sat->sat_oid;
281                         ldap_memfree(air);
282                         return SLAP_SCHERR_ATTR_DUP;
283                 }
284                 /* FIX: temporal consistency check */
285                 at_bvfind(&air->air_name);
286         }
287
288         if ( (names = sat->sat_names) ) {
289                 while ( *names ) {
290                         air = (struct aindexrec *)
291                                 ch_calloc( 1, sizeof(struct aindexrec) );
292                         air->air_name.bv_val = *names;
293                         air->air_name.bv_len = strlen(*names);
294                         air->air_at = sat;
295                         if ( avl_insert( &attr_index, (caddr_t) air,
296                                          attr_index_cmp, avl_dup_error ) ) {
297                                 *err = *names;
298                                 ldap_memfree(air);
299                                 return SLAP_SCHERR_ATTR_DUP;
300                         }
301                         /* FIX: temporal consistency check */
302                         at_bvfind(&air->air_name);
303                         names++;
304                 }
305         }
306
307         LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
308
309         return 0;
310 }
311
312 int
313 at_add(
314     LDAPAttributeType   *at,
315         int                             user,
316         AttributeType   **rsat,
317     const char          **err )
318 {
319         AttributeType   *sat;
320         MatchingRule    *mr;
321         Syntax          *syn;
322         int             i;
323         int             code;
324         char    *cname;
325         char    *oid;
326
327         if ( !OID_LEADCHAR( at->at_oid[0] )) {
328                 /* Expand OID macros */
329                 oid = oidm_find( at->at_oid );
330                 if ( !oid ) {
331                         *err = at->at_oid;
332                         return SLAP_SCHERR_OIDM;
333                 }
334                 if ( oid != at->at_oid ) {
335                         ldap_memfree( at->at_oid );
336                         at->at_oid = oid;
337                 }
338         }
339
340         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
341                 /* Expand OID macros */
342                 oid = oidm_find( at->at_syntax_oid );
343                 if ( !oid ) {
344                         *err = at->at_syntax_oid;
345                         return SLAP_SCHERR_OIDM;
346                 }
347                 if ( oid != at->at_syntax_oid ) {
348                         ldap_memfree( at->at_syntax_oid );
349                         at->at_syntax_oid = oid;
350                 }
351         }
352
353         if ( at->at_names && at->at_names[0] ) {
354                 int i;
355
356                 for( i=0; at->at_names[i]; i++ ) {
357                         if( !slap_valid_descr( at->at_names[i] ) ) {
358                                 *err = at->at_names[i];
359                                 return SLAP_SCHERR_BAD_DESCR;
360                         }
361                 }
362
363                 cname = at->at_names[0];
364
365         } else if ( at->at_oid ) {
366                 cname = at->at_oid;
367
368         } else {
369                 *err = "";
370                 return SLAP_SCHERR_ATTR_INCOMPLETE;
371         }
372
373         *err = cname;
374
375         if ( !at->at_usage && at->at_no_user_mod ) {
376                 /* user attribute must be modifable */
377                 return SLAP_SCHERR_ATTR_BAD_USAGE;
378         }
379
380         if ( at->at_collective ) {
381                 if( at->at_usage ) {
382                         /* collective attributes cannot be operational */
383                         return SLAP_SCHERR_ATTR_BAD_USAGE;
384                 }
385
386                 if( at->at_single_value ) {
387                         /* collective attributes cannot be single-valued */
388                         return SLAP_SCHERR_ATTR_BAD_USAGE;
389                 }
390         }
391
392         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
393         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
394
395         sat->sat_cname.bv_val = cname;
396         sat->sat_cname.bv_len = strlen( cname );
397         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
398
399         if ( at->at_sup_oid ) {
400                 AttributeType *supsat = at_find(at->at_sup_oid);
401
402                 if ( supsat == NULL ) {
403                         *err = at->at_sup_oid;
404                         return SLAP_SCHERR_ATTR_NOT_FOUND;
405                 }
406
407                 sat->sat_sup = supsat;
408
409                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
410                         return SLAP_SCHERR_OUTOFMEM;
411                 }
412
413                 if ( sat->sat_usage != supsat->sat_usage ) {
414                         /* subtypes must have same usage as their SUP */
415                         return SLAP_SCHERR_ATTR_BAD_USAGE;
416                 }
417
418                 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
419                         /* subtypes must be obsolete if super is */
420                         return SLAP_SCHERR_ATTR_BAD_SUP;
421                 }
422
423                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
424                         /* cannot subtype a "final" attribute type */
425                         return SLAP_SCHERR_ATTR_BAD_SUP;
426                 }
427         }
428
429         /*
430          * Inherit definitions from superiors.  We only check the
431          * direct superior since that one has already inherited from
432          * its own superiorss
433          */
434         if ( sat->sat_sup ) {
435                 sat->sat_syntax = sat->sat_sup->sat_syntax;
436                 sat->sat_equality = sat->sat_sup->sat_equality;
437                 sat->sat_approx = sat->sat_sup->sat_approx;
438                 sat->sat_ordering = sat->sat_sup->sat_ordering;
439                 sat->sat_substr = sat->sat_sup->sat_substr;
440         }
441
442         /*
443          * check for X-ORDERED attributes
444          */
445         if ( sat->sat_extensions ) {
446                 for (i=0; sat->sat_extensions[i]; i++) {
447                         if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
448                                 "X-ORDERED" )) {
449                                 sat->sat_flags |= SLAP_AT_ORDERED;
450                                 break;
451                         }
452                 }
453         }
454
455         if ( !user )
456                 sat->sat_flags |= SLAP_AT_HARDCODE;
457
458         if ( at->at_syntax_oid ) {
459                 syn = syn_find(sat->sat_syntax_oid);
460                 if ( syn == NULL ) {
461                         *err = sat->sat_syntax_oid;
462                         return SLAP_SCHERR_SYN_NOT_FOUND;
463                 }
464
465                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
466                         return SLAP_SCHERR_ATTR_BAD_SUP;
467                 }
468
469                 sat->sat_syntax = syn;
470
471         } else if ( sat->sat_syntax == NULL ) {
472                 return SLAP_SCHERR_ATTR_INCOMPLETE;
473         }
474
475         if ( sat->sat_equality_oid ) {
476                 mr = mr_find(sat->sat_equality_oid);
477
478                 if( mr == NULL ) {
479                         *err = sat->sat_equality_oid;
480                         return SLAP_SCHERR_MR_NOT_FOUND;
481                 }
482
483                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
484                         *err = sat->sat_equality_oid;
485                         return SLAP_SCHERR_ATTR_BAD_MR;
486                 }
487
488                 if( sat->sat_syntax != mr->smr_syntax ) {
489                         if( mr->smr_compat_syntaxes == NULL ) {
490                                 *err = sat->sat_equality_oid;
491                                 return SLAP_SCHERR_ATTR_BAD_MR;
492                         }
493
494                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
495                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
496                                         i = -1;
497                                         break;
498                                 }
499                         }
500
501                         if( i >= 0 ) {
502                                 *err = sat->sat_equality_oid;
503                                 return SLAP_SCHERR_ATTR_BAD_MR;
504                         }
505                 }
506
507                 sat->sat_equality = mr;
508                 sat->sat_approx = mr->smr_associated;
509         }
510
511         if ( sat->sat_ordering_oid ) {
512                 if( !sat->sat_equality ) {
513                         *err = sat->sat_ordering_oid;
514                         return SLAP_SCHERR_ATTR_BAD_MR;
515                 }
516
517                 mr = mr_find(sat->sat_ordering_oid);
518
519                 if( mr == NULL ) {
520                         *err = sat->sat_ordering_oid;
521                         return SLAP_SCHERR_MR_NOT_FOUND;
522                 }
523
524                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
525                         *err = sat->sat_ordering_oid;
526                         return SLAP_SCHERR_ATTR_BAD_MR;
527                 }
528
529                 if( sat->sat_syntax != mr->smr_syntax ) {
530                         if( mr->smr_compat_syntaxes == NULL ) {
531                                 *err = sat->sat_ordering_oid;
532                                 return SLAP_SCHERR_ATTR_BAD_MR;
533                         }
534
535                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
536                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
537                                         i = -1;
538                                         break;
539                                 }
540                         }
541
542                         if( i >= 0 ) {
543                                 *err = sat->sat_ordering_oid;
544                                 return SLAP_SCHERR_ATTR_BAD_MR;
545                         }
546                 }
547
548                 sat->sat_ordering = mr;
549         }
550
551         if ( sat->sat_substr_oid ) {
552                 if( !sat->sat_equality ) {
553                         *err = sat->sat_substr_oid;
554                         return SLAP_SCHERR_ATTR_BAD_MR;
555                 }
556
557                 mr = mr_find(sat->sat_substr_oid);
558
559                 if( mr == NULL ) {
560                         *err = sat->sat_substr_oid;
561                         return SLAP_SCHERR_MR_NOT_FOUND;
562                 }
563
564                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
565                         *err = sat->sat_substr_oid;
566                         return SLAP_SCHERR_ATTR_BAD_MR;
567                 }
568
569                 /* due to funky LDAP builtin substring rules,
570                  * we check against the equality rule assertion
571                  * syntax and compat syntaxes instead of those
572                  * associated with the substrings rule.
573                  */
574                 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
575                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
576                                 *err = sat->sat_substr_oid;
577                                 return SLAP_SCHERR_ATTR_BAD_MR;
578                         }
579
580                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
581                                 if( sat->sat_syntax ==
582                                         sat->sat_equality->smr_compat_syntaxes[i] )
583                                 {
584                                         i = -1;
585                                         break;
586                                 }
587                         }
588
589                         if( i >= 0 ) {
590                                 *err = sat->sat_substr_oid;
591                                 return SLAP_SCHERR_ATTR_BAD_MR;
592                         }
593                 }
594
595                 sat->sat_substr = mr;
596         }
597
598         code = at_insert(sat,err);
599         if ( code == 0 && rsat )
600                 *rsat = sat;
601         return code;
602 }
603
604 #ifdef LDAP_DEBUG
605 static int
606 at_index_printnode( void *v_air, void *ignore )
607 {
608         struct aindexrec *air = v_air;
609         printf("%s = %s\n",
610                 air->air_name.bv_val,
611                 ldap_attributetype2str(&air->air_at->sat_atype) );
612         return( 0 );
613 }
614
615 static void
616 at_index_print( void )
617 {
618         printf("Printing attribute type index:\n");
619         (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
620 }
621 #endif
622
623 void
624 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
625 {
626         AttributeType *at;
627         int i, num;
628         struct berval bv, *bva = NULL, idx;
629         char ibuf[32], *ptr;
630
631         if ( !start )
632                 start = LDAP_STAILQ_FIRST( &attr_list );
633
634         /* count the result size */
635         i = 0;
636         for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
637                 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
638                 i++;
639                 if ( at == end ) break;
640         }
641         if (!i) return;
642
643         num = i;
644         bva = ch_malloc( (num+1) * sizeof(struct berval) );
645         BER_BVZERO( bva );
646         idx.bv_val = ibuf;
647         if ( sys ) {
648                 idx.bv_len = 0;
649                 ibuf[0] = '\0';
650         }
651         i = 0;
652         for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
653                 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
654                 if ( ldap_attributetype2bv( &at->sat_atype, &bv ) == NULL ) {
655                         ber_bvarray_free( bva );
656                 }
657                 if ( !sys ) {
658                         idx.bv_len = sprintf(idx.bv_val, "{%02d}", i);
659                 }
660                 bva[i].bv_len = idx.bv_len + bv.bv_len;
661                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
662                 strcpy( bva[i].bv_val, ibuf );
663                 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
664                 i++;
665                 bva[i].bv_val = NULL;
666                 ldap_memfree( bv.bv_val );
667                 if ( at == end ) break;
668         }
669         *res = bva;
670 }
671
672 int
673 at_schema_info( Entry *e )
674 {
675         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
676         AttributeType   *at;
677         struct berval   val;
678         struct berval   nval;
679
680         LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
681                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
682
683                 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
684                         return -1;
685                 }
686
687                 ber_str2bv( at->sat_oid, 0, 0, &nval );
688
689                 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
690                 {
691                         return -1;
692                 }
693                 ldap_memfree( val.bv_val );
694         }
695         return 0;
696 }