]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
Happy new year
[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-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/errno.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "ldap_pvt.h"
28 #include "slap.h"
29
30
31 int is_at_syntax(
32         AttributeType *at,
33         const char *oid )
34 {
35         for( ; at != NULL; at = at->sat_sup ) {
36                 if( at->sat_syntax_oid ) {
37                         return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
38                 }
39         }
40
41         return 0;
42 }
43
44 int is_at_subtype(
45         AttributeType *sub,
46         AttributeType *sup )
47 {
48         for( ; sub != NULL; sub = sub->sat_sup ) {
49                 if( sub == sup ) return 1;
50         }
51
52         return 0;
53 }
54
55 struct aindexrec {
56         struct berval   air_name;
57         AttributeType   *air_at;
58 };
59
60 static Avlnode  *attr_index = NULL;
61 static LDAP_SLIST_HEAD(ATList, slap_attribute_type) attr_list
62         = LDAP_SLIST_HEAD_INITIALIZER(&attr_list);
63
64 static int
65 attr_index_cmp(
66     const void  *v_air1,
67     const void  *v_air2
68 )
69 {
70         const struct aindexrec  *air1 = v_air1;
71         const struct aindexrec  *air2 = v_air2;
72         int i = air1->air_name.bv_len - air2->air_name.bv_len;
73         if (i)
74                 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 {
84     const struct berval    *type = v_type;
85     const struct aindexrec *air  = v_air;
86         int i = type->bv_len - air->air_name.bv_len;
87         if (i)
88                 return i;
89         return (strncasecmp( type->bv_val, air->air_name.bv_val,
90                 type->bv_len ));
91 }
92
93 AttributeType *
94 at_find(
95     const char          *name
96 )
97 {
98         struct berval bv;
99
100         bv.bv_val = (char *)name;
101         bv.bv_len = strlen( name );
102
103         return at_bvfind( &bv );
104 }
105
106 AttributeType *
107 at_bvfind(
108     struct berval       *name
109 )
110 {
111         struct aindexrec *air;
112
113         air = avl_find( attr_index, name, attr_index_name_cmp );
114
115         return air != NULL ? air->air_at : NULL;
116 }
117
118 int
119 at_append_to_list(
120     AttributeType       *sat,
121     AttributeType       ***listp
122 )
123 {
124         AttributeType   **list;
125         AttributeType   **list1;
126         int             size;
127
128         list = *listp;
129         if ( !list ) {
130                 size = 2;
131                 list = ch_calloc(size, sizeof(AttributeType *));
132                 if ( !list ) {
133                         return -1;
134                 }
135         } else {
136                 size = 0;
137                 list1 = *listp;
138                 while ( *list1 ) {
139                         size++;
140                         list1++;
141                 }
142                 size += 2;
143                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
144                 if ( !list1 ) {
145                         return -1;
146                 }
147                 list = list1;
148         }
149         list[size-2] = sat;
150         list[size-1] = NULL;
151         *listp = list;
152         return 0;
153 }
154
155 int
156 at_delete_from_list(
157     int                 pos,
158     AttributeType       ***listp
159 )
160 {
161         AttributeType   **list;
162         AttributeType   **list1;
163         int             i;
164         int             j;
165
166         if ( pos < 0 ) {
167                 return -2;
168         }
169         list = *listp;
170         for ( i=0; list[i]; i++ )
171                 ;
172         if ( pos >= i ) {
173                 return -2;
174         }
175         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
176                 list[i] = list[j];
177         }
178         list[i] = NULL;
179         /* Tell the runtime this can be shrinked */
180         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
181         if ( !list1 ) {
182                 return -1;
183         }
184         *listp = list1;
185         return 0;
186 }
187
188 int
189 at_find_in_list(
190     AttributeType       *sat,
191     AttributeType       **list
192 )
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_SLIST_EMPTY(&attr_list) ) {
214                 a = LDAP_SLIST_FIRST(&attr_list);
215                 LDAP_SLIST_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_SLIST_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_SLIST_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_SLIST_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 {
270         struct aindexrec        *air;
271         char                    **names;
272
273         LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
274         LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
275
276         if ( sat->sat_oid ) {
277                 air = (struct aindexrec *)
278                         ch_calloc( 1, sizeof(struct aindexrec) );
279                 air->air_name.bv_val = sat->sat_oid;
280                 air->air_name.bv_len = strlen(sat->sat_oid);
281                 air->air_at = sat;
282                 if ( avl_insert( &attr_index, (caddr_t) air,
283                                  attr_index_cmp, avl_dup_error ) ) {
284                         *err = sat->sat_oid;
285                         ldap_memfree(air);
286                         return SLAP_SCHERR_ATTR_DUP;
287                 }
288                 /* FIX: temporal consistency check */
289                 at_bvfind(&air->air_name);
290         }
291
292         if ( (names = sat->sat_names) ) {
293                 while ( *names ) {
294                         air = (struct aindexrec *)
295                                 ch_calloc( 1, sizeof(struct aindexrec) );
296                         air->air_name.bv_val = *names;
297                         air->air_name.bv_len = strlen(*names);
298                         air->air_at = sat;
299                         if ( avl_insert( &attr_index, (caddr_t) air,
300                                          attr_index_cmp, avl_dup_error ) ) {
301                                 *err = *names;
302                                 ldap_memfree(air);
303                                 return SLAP_SCHERR_ATTR_DUP;
304                         }
305                         /* FIX: temporal consistency check */
306                         at_bvfind(&air->air_name);
307                         names++;
308                 }
309         }
310
311         return 0;
312 }
313
314 int
315 at_add(
316     LDAPAttributeType   *at,
317     const char          **err
318 )
319 {
320         AttributeType   *sat;
321         MatchingRule    *mr;
322         Syntax          *syn;
323         int             i;
324         int             code;
325         char    *cname;
326         char    *oid;
327
328         if ( !OID_LEADCHAR( at->at_oid[0] )) {
329                 /* Expand OID macros */
330                 oid = oidm_find( at->at_oid );
331                 if ( !oid ) {
332                         *err = at->at_oid;
333                         return SLAP_SCHERR_OIDM;
334                 }
335                 if ( oid != at->at_oid ) {
336                         ldap_memfree( at->at_oid );
337                         at->at_oid = oid;
338                 }
339         }
340
341         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
342                 /* Expand OID macros */
343                 oid = oidm_find( at->at_syntax_oid );
344                 if ( !oid ) {
345                         *err = at->at_syntax_oid;
346                         return SLAP_SCHERR_OIDM;
347                 }
348                 if ( oid != at->at_syntax_oid ) {
349                         ldap_memfree( at->at_syntax_oid );
350                         at->at_syntax_oid = oid;
351                 }
352         }
353
354         if ( at->at_names && at->at_names[0] ) {
355                 int i;
356
357                 for( i=0; at->at_names[i]; i++ ) {
358                         if( !slap_valid_descr( at->at_names[i] ) ) {
359                                 *err = at->at_names[i];
360                                 return SLAP_SCHERR_BAD_DESCR;
361                         }
362                 }
363
364                 cname = at->at_names[0];
365
366         } else if ( at->at_oid ) {
367                 cname = at->at_oid;
368
369         } else {
370                 *err = "";
371                 return SLAP_SCHERR_ATTR_INCOMPLETE;
372         }
373
374         *err = cname;
375
376         if ( !at->at_usage && at->at_no_user_mod ) {
377                 /* user attribute must be modifable */
378                 return SLAP_SCHERR_ATTR_BAD_USAGE;
379         }
380
381         if ( at->at_collective ) {
382                 if( at->at_usage ) {
383                         /* collective attributes cannot be operational */
384                         return SLAP_SCHERR_ATTR_BAD_USAGE;
385                 }
386
387                 if( at->at_single_value ) {
388                         /* collective attributes cannot be single-valued */
389                         return SLAP_SCHERR_ATTR_BAD_USAGE;
390                 }
391         }
392
393         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
394         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
395
396         sat->sat_cname.bv_val = cname;
397         sat->sat_cname.bv_len = strlen( cname );
398         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
399
400         if ( at->at_sup_oid ) {
401                 AttributeType *supsat = at_find(at->at_sup_oid);
402
403                 if ( supsat == NULL ) {
404                         *err = at->at_sup_oid;
405                         return SLAP_SCHERR_ATTR_NOT_FOUND;
406                 }
407
408                 sat->sat_sup = supsat;
409
410                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
411                         return SLAP_SCHERR_OUTOFMEM;
412                 }
413
414                 if ( sat->sat_usage != supsat->sat_usage ) {
415                         /* subtypes must have same usage as their SUP */
416                         return SLAP_SCHERR_ATTR_BAD_USAGE;
417                 }
418
419                 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
420                         /* subtypes must be obsolete if super is */
421                         return SLAP_SCHERR_ATTR_BAD_SUP;
422                 }
423
424                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
425                         /* cannot subtype a "final" attribute type */
426                         return SLAP_SCHERR_ATTR_BAD_SUP;
427                 }
428         }
429
430         /*
431          * Inherit definitions from superiors.  We only check the
432          * direct superior since that one has already inherited from
433          * its own superiorss
434          */
435         if ( sat->sat_sup ) {
436                 sat->sat_syntax = sat->sat_sup->sat_syntax;
437                 sat->sat_equality = sat->sat_sup->sat_equality;
438                 sat->sat_approx = sat->sat_sup->sat_approx;
439                 sat->sat_ordering = sat->sat_sup->sat_ordering;
440                 sat->sat_substr = sat->sat_sup->sat_substr;
441         }
442
443         if ( at->at_syntax_oid ) {
444                 syn = syn_find(sat->sat_syntax_oid);
445                 if ( syn == NULL ) {
446                         *err = sat->sat_syntax_oid;
447                         return SLAP_SCHERR_SYN_NOT_FOUND;
448                 }
449
450                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
451                         return SLAP_SCHERR_ATTR_BAD_SUP;
452                 }
453
454                 sat->sat_syntax = syn;
455
456         } else if ( sat->sat_syntax == NULL ) {
457                 return SLAP_SCHERR_ATTR_INCOMPLETE;
458         }
459
460         if ( sat->sat_equality_oid ) {
461                 mr = mr_find(sat->sat_equality_oid);
462
463                 if( mr == NULL ) {
464                         *err = sat->sat_equality_oid;
465                         return SLAP_SCHERR_MR_NOT_FOUND;
466                 }
467
468                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
469                         *err = sat->sat_equality_oid;
470                         return SLAP_SCHERR_ATTR_BAD_MR;
471                 }
472
473                 if( sat->sat_syntax != mr->smr_syntax ) {
474                         if( mr->smr_compat_syntaxes == NULL ) {
475                                 *err = sat->sat_equality_oid;
476                                 return SLAP_SCHERR_ATTR_BAD_MR;
477                         }
478
479                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
480                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
481                                         i = -1;
482                                         break;
483                                 }
484                         }
485
486                         if( i >= 0 ) {
487                                 *err = sat->sat_equality_oid;
488                                 return SLAP_SCHERR_ATTR_BAD_MR;
489                         }
490                 }
491
492                 sat->sat_equality = mr;
493                 sat->sat_approx = mr->smr_associated;
494         }
495
496         if ( sat->sat_ordering_oid ) {
497                 if( !sat->sat_equality ) {
498                         *err = sat->sat_ordering_oid;
499                         return SLAP_SCHERR_ATTR_BAD_MR;
500                 }
501
502                 mr = mr_find(sat->sat_ordering_oid);
503
504                 if( mr == NULL ) {
505                         *err = sat->sat_ordering_oid;
506                         return SLAP_SCHERR_MR_NOT_FOUND;
507                 }
508
509                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
510                         *err = sat->sat_ordering_oid;
511                         return SLAP_SCHERR_ATTR_BAD_MR;
512                 }
513
514                 if( sat->sat_syntax != mr->smr_syntax ) {
515                         if( mr->smr_compat_syntaxes == NULL ) {
516                                 *err = sat->sat_ordering_oid;
517                                 return SLAP_SCHERR_ATTR_BAD_MR;
518                         }
519
520                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
521                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
522                                         i = -1;
523                                         break;
524                                 }
525                         }
526
527                         if( i >= 0 ) {
528                                 *err = sat->sat_ordering_oid;
529                                 return SLAP_SCHERR_ATTR_BAD_MR;
530                         }
531                 }
532
533                 sat->sat_ordering = mr;
534         }
535
536         if ( sat->sat_substr_oid ) {
537                 if( !sat->sat_equality ) {
538                         *err = sat->sat_substr_oid;
539                         return SLAP_SCHERR_ATTR_BAD_MR;
540                 }
541
542                 mr = mr_find(sat->sat_substr_oid);
543
544                 if( mr == NULL ) {
545                         *err = sat->sat_substr_oid;
546                         return SLAP_SCHERR_MR_NOT_FOUND;
547                 }
548
549                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
550                         *err = sat->sat_substr_oid;
551                         return SLAP_SCHERR_ATTR_BAD_MR;
552                 }
553
554                 /* due to funky LDAP builtin substring rules, we
555                  * we check against the equality rule assertion
556                  * syntax and compat syntaxes instead of those
557                  * associated with the substrings rule.
558                  */
559                 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
560                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
561                                 *err = sat->sat_substr_oid;
562                                 return SLAP_SCHERR_ATTR_BAD_MR;
563                         }
564
565                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
566                                 if( sat->sat_syntax ==
567                                         sat->sat_equality->smr_compat_syntaxes[i] )
568                                 {
569                                         i = -1;
570                                         break;
571                                 }
572                         }
573
574                         if( i >= 0 ) {
575                                 *err = sat->sat_substr_oid;
576                                 return SLAP_SCHERR_ATTR_BAD_MR;
577                         }
578                 }
579
580                 sat->sat_substr = mr;
581         }
582
583         code = at_insert(sat,err);
584         return code;
585 }
586
587 #ifdef LDAP_DEBUG
588 static int
589 at_index_printnode( void *v_air, void *ignore )
590 {
591         struct aindexrec *air = v_air;
592         printf("%s = %s\n",
593                 air->air_name.bv_val,
594                 ldap_attributetype2str(&air->air_at->sat_atype) );
595         return( 0 );
596 }
597
598 static void
599 at_index_print( void )
600 {
601         printf("Printing attribute type index:\n");
602         (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
603 }
604 #endif
605
606 int
607 at_schema_info( Entry *e )
608 {
609         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
610         AttributeType   *at;
611         struct berval   val;
612         struct berval   nval;
613
614         LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
615                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
616
617                 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
618                         return -1;
619                 }
620
621                 nval.bv_val = at->sat_oid;
622                 nval.bv_len = strlen(at->sat_oid);
623
624                 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
625                 {
626                         return -1;
627                 }
628                 ldap_memfree( val.bv_val );
629         }
630         return 0;
631 }