]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
ITS#3226: Clear attribute flags after schema_check failed
[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         const struct aindexrec  *air1 = v_air1;
70         const struct aindexrec  *air2 = v_air2;
71         int i = air1->air_name.bv_len - air2->air_name.bv_len;
72         if (i) return i;
73         return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
74 }
75
76 static int
77 attr_index_name_cmp(
78     const void  *v_type,
79     const void  *v_air )
80 {
81     const struct berval    *type = v_type;
82     const struct aindexrec *air  = v_air;
83         int i = type->bv_len - air->air_name.bv_len;
84         if (i) return i;
85         return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
86 }
87
88 AttributeType *
89 at_find( const char *name )
90 {
91         struct berval bv;
92
93         bv.bv_val = (char *)name;
94         bv.bv_len = strlen( name );
95
96         return at_bvfind( &bv );
97 }
98
99 AttributeType *
100 at_bvfind( struct berval *name )
101 {
102         struct aindexrec *air;
103
104         air = avl_find( attr_index, name, attr_index_name_cmp );
105
106         return air != NULL ? air->air_at : NULL;
107 }
108
109 int
110 at_append_to_list(
111     AttributeType       *sat,
112     AttributeType       ***listp )
113 {
114         AttributeType   **list;
115         AttributeType   **list1;
116         int             size;
117
118         list = *listp;
119         if ( !list ) {
120                 size = 2;
121                 list = ch_calloc(size, sizeof(AttributeType *));
122                 if ( !list ) {
123                         return -1;
124                 }
125         } else {
126                 size = 0;
127                 list1 = *listp;
128                 while ( *list1 ) {
129                         size++;
130                         list1++;
131                 }
132                 size += 2;
133                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
134                 if ( !list1 ) {
135                         return -1;
136                 }
137                 list = list1;
138         }
139         list[size-2] = sat;
140         list[size-1] = NULL;
141         *listp = list;
142         return 0;
143 }
144
145 int
146 at_delete_from_list(
147     int                 pos,
148     AttributeType       ***listp )
149 {
150         AttributeType   **list;
151         AttributeType   **list1;
152         int             i;
153         int             j;
154
155         if ( pos < 0 ) {
156                 return -2;
157         }
158         list = *listp;
159         for ( i=0; list[i]; i++ )
160                 ;
161         if ( pos >= i ) {
162                 return -2;
163         }
164         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
165                 list[i] = list[j];
166         }
167         list[i] = NULL;
168         /* Tell the runtime this can be shrinked */
169         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
170         if ( !list1 ) {
171                 return -1;
172         }
173         *listp = list1;
174         return 0;
175 }
176
177 int
178 at_find_in_list(
179     AttributeType       *sat,
180     AttributeType       **list )
181 {
182         int     i;
183
184         if ( !list ) {
185                 return -1;
186         }
187         for ( i=0; list[i]; i++ ) {
188                 if ( sat == list[i] ) {
189                         return i;
190                 }
191         }
192         return -1;
193 }
194
195 void
196 at_destroy( void )
197 {
198         AttributeType *a;
199         avl_free(attr_index, ldap_memfree);
200
201         while( !LDAP_SLIST_EMPTY(&attr_list) ) {
202                 a = LDAP_SLIST_FIRST(&attr_list);
203                 LDAP_SLIST_REMOVE_HEAD(&attr_list, sat_next);
204
205                 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
206                 ad_destroy(a->sat_ad);
207                 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
208                 ldap_attributetype_free((LDAPAttributeType *)a);
209         }
210
211         if ( slap_schema.si_at_undefined ) {
212                 ad_destroy(slap_schema.si_at_undefined->sat_ad);
213         }
214 }
215
216 int
217 at_start( AttributeType **at )
218 {
219         assert( at );
220
221         *at = LDAP_SLIST_FIRST(&attr_list);
222
223         return (*at != NULL);
224 }
225
226 int
227 at_next( AttributeType **at )
228 {
229         assert( at );
230
231 #if 1   /* pedantic check */
232         {
233                 AttributeType *tmp = NULL;
234
235                 LDAP_SLIST_FOREACH(tmp,&attr_list,sat_next) {
236                         if ( tmp == *at ) {
237                                 break;
238                         }
239                 }
240
241                 assert( tmp );
242         }
243 #endif
244
245         *at = LDAP_SLIST_NEXT(*at,sat_next);
246
247         return (*at != NULL);
248 }
249         
250
251
252 static int
253 at_insert(
254     AttributeType       *sat,
255     const char          **err )
256 {
257         struct aindexrec        *air;
258         char                    **names;
259
260         LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
261         LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
262
263         if ( sat->sat_oid ) {
264                 air = (struct aindexrec *)
265                         ch_calloc( 1, sizeof(struct aindexrec) );
266                 air->air_name.bv_val = sat->sat_oid;
267                 air->air_name.bv_len = strlen(sat->sat_oid);
268                 air->air_at = sat;
269                 if ( avl_insert( &attr_index, (caddr_t) air,
270                                  attr_index_cmp, avl_dup_error ) ) {
271                         *err = sat->sat_oid;
272                         ldap_memfree(air);
273                         return SLAP_SCHERR_ATTR_DUP;
274                 }
275                 /* FIX: temporal consistency check */
276                 at_bvfind(&air->air_name);
277         }
278
279         if ( (names = sat->sat_names) ) {
280                 while ( *names ) {
281                         air = (struct aindexrec *)
282                                 ch_calloc( 1, sizeof(struct aindexrec) );
283                         air->air_name.bv_val = *names;
284                         air->air_name.bv_len = strlen(*names);
285                         air->air_at = sat;
286                         if ( avl_insert( &attr_index, (caddr_t) air,
287                                          attr_index_cmp, avl_dup_error ) ) {
288                                 *err = *names;
289                                 ldap_memfree(air);
290                                 return SLAP_SCHERR_ATTR_DUP;
291                         }
292                         /* FIX: temporal consistency check */
293                         at_bvfind(&air->air_name);
294                         names++;
295                 }
296         }
297
298         return 0;
299 }
300
301 int
302 at_add(
303     LDAPAttributeType   *at,
304     const char          **err )
305 {
306         AttributeType   *sat;
307         MatchingRule    *mr;
308         Syntax          *syn;
309         int             i;
310         int             code;
311         char    *cname;
312         char    *oid;
313
314         if ( !OID_LEADCHAR( at->at_oid[0] )) {
315                 /* Expand OID macros */
316                 oid = oidm_find( at->at_oid );
317                 if ( !oid ) {
318                         *err = at->at_oid;
319                         return SLAP_SCHERR_OIDM;
320                 }
321                 if ( oid != at->at_oid ) {
322                         ldap_memfree( at->at_oid );
323                         at->at_oid = oid;
324                 }
325         }
326
327         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
328                 /* Expand OID macros */
329                 oid = oidm_find( at->at_syntax_oid );
330                 if ( !oid ) {
331                         *err = at->at_syntax_oid;
332                         return SLAP_SCHERR_OIDM;
333                 }
334                 if ( oid != at->at_syntax_oid ) {
335                         ldap_memfree( at->at_syntax_oid );
336                         at->at_syntax_oid = oid;
337                 }
338         }
339
340         if ( at->at_names && at->at_names[0] ) {
341                 int i;
342
343                 for( i=0; at->at_names[i]; i++ ) {
344                         if( !slap_valid_descr( at->at_names[i] ) ) {
345                                 *err = at->at_names[i];
346                                 return SLAP_SCHERR_BAD_DESCR;
347                         }
348                 }
349
350                 cname = at->at_names[0];
351
352         } else if ( at->at_oid ) {
353                 cname = at->at_oid;
354
355         } else {
356                 *err = "";
357                 return SLAP_SCHERR_ATTR_INCOMPLETE;
358         }
359
360         *err = cname;
361
362         if ( !at->at_usage && at->at_no_user_mod ) {
363                 /* user attribute must be modifable */
364                 return SLAP_SCHERR_ATTR_BAD_USAGE;
365         }
366
367         if ( at->at_collective ) {
368                 if( at->at_usage ) {
369                         /* collective attributes cannot be operational */
370                         return SLAP_SCHERR_ATTR_BAD_USAGE;
371                 }
372
373                 if( at->at_single_value ) {
374                         /* collective attributes cannot be single-valued */
375                         return SLAP_SCHERR_ATTR_BAD_USAGE;
376                 }
377         }
378
379         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
380         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
381
382         sat->sat_cname.bv_val = cname;
383         sat->sat_cname.bv_len = strlen( cname );
384         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
385
386         if ( at->at_sup_oid ) {
387                 AttributeType *supsat = at_find(at->at_sup_oid);
388
389                 if ( supsat == NULL ) {
390                         *err = at->at_sup_oid;
391                         return SLAP_SCHERR_ATTR_NOT_FOUND;
392                 }
393
394                 sat->sat_sup = supsat;
395
396                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
397                         return SLAP_SCHERR_OUTOFMEM;
398                 }
399
400                 if ( sat->sat_usage != supsat->sat_usage ) {
401                         /* subtypes must have same usage as their SUP */
402                         return SLAP_SCHERR_ATTR_BAD_USAGE;
403                 }
404
405                 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
406                         /* subtypes must be obsolete if super is */
407                         return SLAP_SCHERR_ATTR_BAD_SUP;
408                 }
409
410                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
411                         /* cannot subtype a "final" attribute type */
412                         return SLAP_SCHERR_ATTR_BAD_SUP;
413                 }
414         }
415
416         /*
417          * Inherit definitions from superiors.  We only check the
418          * direct superior since that one has already inherited from
419          * its own superiorss
420          */
421         if ( sat->sat_sup ) {
422                 sat->sat_syntax = sat->sat_sup->sat_syntax;
423                 sat->sat_equality = sat->sat_sup->sat_equality;
424                 sat->sat_approx = sat->sat_sup->sat_approx;
425                 sat->sat_ordering = sat->sat_sup->sat_ordering;
426                 sat->sat_substr = sat->sat_sup->sat_substr;
427         }
428
429         if ( at->at_syntax_oid ) {
430                 syn = syn_find(sat->sat_syntax_oid);
431                 if ( syn == NULL ) {
432                         *err = sat->sat_syntax_oid;
433                         return SLAP_SCHERR_SYN_NOT_FOUND;
434                 }
435
436                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
437                         return SLAP_SCHERR_ATTR_BAD_SUP;
438                 }
439
440                 sat->sat_syntax = syn;
441
442         } else if ( sat->sat_syntax == NULL ) {
443                 return SLAP_SCHERR_ATTR_INCOMPLETE;
444         }
445
446         if ( sat->sat_equality_oid ) {
447                 mr = mr_find(sat->sat_equality_oid);
448
449                 if( mr == NULL ) {
450                         *err = sat->sat_equality_oid;
451                         return SLAP_SCHERR_MR_NOT_FOUND;
452                 }
453
454                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
455                         *err = sat->sat_equality_oid;
456                         return SLAP_SCHERR_ATTR_BAD_MR;
457                 }
458
459                 if( sat->sat_syntax != mr->smr_syntax ) {
460                         if( mr->smr_compat_syntaxes == NULL ) {
461                                 *err = sat->sat_equality_oid;
462                                 return SLAP_SCHERR_ATTR_BAD_MR;
463                         }
464
465                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
466                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
467                                         i = -1;
468                                         break;
469                                 }
470                         }
471
472                         if( i >= 0 ) {
473                                 *err = sat->sat_equality_oid;
474                                 return SLAP_SCHERR_ATTR_BAD_MR;
475                         }
476                 }
477
478                 sat->sat_equality = mr;
479                 sat->sat_approx = mr->smr_associated;
480         }
481
482         if ( sat->sat_ordering_oid ) {
483                 if( !sat->sat_equality ) {
484                         *err = sat->sat_ordering_oid;
485                         return SLAP_SCHERR_ATTR_BAD_MR;
486                 }
487
488                 mr = mr_find(sat->sat_ordering_oid);
489
490                 if( mr == NULL ) {
491                         *err = sat->sat_ordering_oid;
492                         return SLAP_SCHERR_MR_NOT_FOUND;
493                 }
494
495                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
496                         *err = sat->sat_ordering_oid;
497                         return SLAP_SCHERR_ATTR_BAD_MR;
498                 }
499
500                 if( sat->sat_syntax != mr->smr_syntax ) {
501                         if( mr->smr_compat_syntaxes == NULL ) {
502                                 *err = sat->sat_ordering_oid;
503                                 return SLAP_SCHERR_ATTR_BAD_MR;
504                         }
505
506                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
507                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
508                                         i = -1;
509                                         break;
510                                 }
511                         }
512
513                         if( i >= 0 ) {
514                                 *err = sat->sat_ordering_oid;
515                                 return SLAP_SCHERR_ATTR_BAD_MR;
516                         }
517                 }
518
519                 sat->sat_ordering = mr;
520         }
521
522         if ( sat->sat_substr_oid ) {
523                 if( !sat->sat_equality ) {
524                         *err = sat->sat_substr_oid;
525                         return SLAP_SCHERR_ATTR_BAD_MR;
526                 }
527
528                 mr = mr_find(sat->sat_substr_oid);
529
530                 if( mr == NULL ) {
531                         *err = sat->sat_substr_oid;
532                         return SLAP_SCHERR_MR_NOT_FOUND;
533                 }
534
535                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
536                         *err = sat->sat_substr_oid;
537                         return SLAP_SCHERR_ATTR_BAD_MR;
538                 }
539
540                 /* due to funky LDAP builtin substring rules,
541                  * we check against the equality rule assertion
542                  * syntax and compat syntaxes instead of those
543                  * associated with the substrings rule.
544                  */
545                 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
546                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
547                                 *err = sat->sat_substr_oid;
548                                 return SLAP_SCHERR_ATTR_BAD_MR;
549                         }
550
551                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
552                                 if( sat->sat_syntax ==
553                                         sat->sat_equality->smr_compat_syntaxes[i] )
554                                 {
555                                         i = -1;
556                                         break;
557                                 }
558                         }
559
560                         if( i >= 0 ) {
561                                 *err = sat->sat_substr_oid;
562                                 return SLAP_SCHERR_ATTR_BAD_MR;
563                         }
564                 }
565
566                 sat->sat_substr = mr;
567         }
568
569         code = at_insert(sat,err);
570         return code;
571 }
572
573 #ifdef LDAP_DEBUG
574 static int
575 at_index_printnode( void *v_air, void *ignore )
576 {
577         struct aindexrec *air = v_air;
578         printf("%s = %s\n",
579                 air->air_name.bv_val,
580                 ldap_attributetype2str(&air->air_at->sat_atype) );
581         return( 0 );
582 }
583
584 static void
585 at_index_print( void )
586 {
587         printf("Printing attribute type index:\n");
588         (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
589 }
590 #endif
591
592 int
593 at_schema_info( Entry *e )
594 {
595         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
596         AttributeType   *at;
597         struct berval   val;
598         struct berval   nval;
599
600         LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
601                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
602
603                 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
604                         return -1;
605                 }
606
607                 nval.bv_val = at->sat_oid;
608                 nval.bv_len = strlen(at->sat_oid);
609
610                 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
611                 {
612                         return -1;
613                 }
614                 ldap_memfree( val.bv_val );
615         }
616         return 0;
617 }