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