]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
To conform to the SLAPI spec, slapi_filter_get_ava() should not duplicate
[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_INSERT_HEAD( &attr_list, sat, sat_next );
265
266         if ( sat->sat_oid ) {
267                 air = (struct aindexrec *)
268                         ch_calloc( 1, sizeof(struct aindexrec) );
269                 air->air_name.bv_val = sat->sat_oid;
270                 air->air_name.bv_len = strlen(sat->sat_oid);
271                 air->air_at = sat;
272                 if ( avl_insert( &attr_index, (caddr_t) air,
273                                  attr_index_cmp, avl_dup_error ) ) {
274                         *err = sat->sat_oid;
275                         ldap_memfree(air);
276                         return SLAP_SCHERR_ATTR_DUP;
277                 }
278                 /* FIX: temporal consistency check */
279                 at_bvfind(&air->air_name);
280         }
281
282         if ( (names = sat->sat_names) ) {
283                 while ( *names ) {
284                         air = (struct aindexrec *)
285                                 ch_calloc( 1, sizeof(struct aindexrec) );
286                         air->air_name.bv_val = *names;
287                         air->air_name.bv_len = strlen(*names);
288                         air->air_at = sat;
289                         if ( avl_insert( &attr_index, (caddr_t) air,
290                                          attr_index_cmp, avl_dup_error ) ) {
291                                 *err = *names;
292                                 ldap_memfree(air);
293                                 return SLAP_SCHERR_ATTR_DUP;
294                         }
295                         /* FIX: temporal consistency check */
296                         at_bvfind(&air->air_name);
297                         names++;
298                 }
299         }
300
301         return 0;
302 }
303
304 int
305 at_add(
306     LDAPAttributeType   *at,
307     const char          **err
308 )
309 {
310         AttributeType   *sat;
311         MatchingRule    *mr;
312         Syntax          *syn;
313         int             i;
314         int             code;
315         char    *cname;
316         char    *oid;
317
318         if ( !OID_LEADCHAR( at->at_oid[0] )) {
319                 /* Expand OID macros */
320                 oid = oidm_find( at->at_oid );
321                 if ( !oid ) {
322                         *err = at->at_oid;
323                         return SLAP_SCHERR_OIDM;
324                 }
325                 if ( oid != at->at_oid ) {
326                         ldap_memfree( at->at_oid );
327                         at->at_oid = oid;
328                 }
329         }
330
331         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
332                 /* Expand OID macros */
333                 oid = oidm_find( at->at_syntax_oid );
334                 if ( !oid ) {
335                         *err = at->at_syntax_oid;
336                         return SLAP_SCHERR_OIDM;
337                 }
338                 if ( oid != at->at_syntax_oid ) {
339                         ldap_memfree( at->at_syntax_oid );
340                         at->at_syntax_oid = oid;
341                 }
342         }
343
344         if ( at->at_names && at->at_names[0] ) {
345                 int i;
346
347                 for( i=0; at->at_names[i]; i++ ) {
348                         if( !slap_valid_descr( at->at_names[i] ) ) {
349                                 *err = at->at_names[i];
350                                 return SLAP_SCHERR_BAD_DESCR;
351                         }
352                 }
353
354                 cname = at->at_names[0];
355
356         } else if ( at->at_oid ) {
357                 cname = at->at_oid;
358
359         } else {
360                 *err = "";
361                 return SLAP_SCHERR_ATTR_INCOMPLETE;
362         }
363
364         *err = cname;
365
366         if ( !at->at_usage && at->at_no_user_mod ) {
367                 /* user attribute must be modifable */
368                 return SLAP_SCHERR_ATTR_BAD_USAGE;
369         }
370
371         if ( at->at_collective ) {
372                 if( at->at_usage ) {
373                         /* collective attributes cannot be operational */
374                         return SLAP_SCHERR_ATTR_BAD_USAGE;
375                 }
376
377                 if( at->at_single_value ) {
378                         /* collective attributes cannot be single-valued */
379                         return SLAP_SCHERR_ATTR_BAD_USAGE;
380                 }
381         }
382
383         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
384         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
385
386         sat->sat_cname.bv_val = cname;
387         sat->sat_cname.bv_len = strlen( cname );
388         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
389
390         if ( at->at_sup_oid ) {
391                 AttributeType *supsat = at_find(at->at_sup_oid);
392
393                 if ( supsat == NULL ) {
394                         *err = at->at_sup_oid;
395                         return SLAP_SCHERR_ATTR_NOT_FOUND;
396                 }
397
398                 sat->sat_sup = supsat;
399
400                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
401                         return SLAP_SCHERR_OUTOFMEM;
402                 }
403
404                 if ( sat->sat_usage != supsat->sat_usage ) {
405                         /* subtypes must have same usage as their SUP */
406                         return SLAP_SCHERR_ATTR_BAD_USAGE;
407                 }
408
409                 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
410                         /* subtypes must be obsolete if super is */
411                         return SLAP_SCHERR_ATTR_BAD_SUP;
412                 }
413
414                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
415                         /* cannot subtype a "final" attribute type */
416                         return SLAP_SCHERR_ATTR_BAD_SUP;
417                 }
418         }
419
420         /*
421          * Inherit definitions from superiors.  We only check the
422          * direct superior since that one has already inherited from
423          * its own superiorss
424          */
425         if ( sat->sat_sup ) {
426                 sat->sat_syntax = sat->sat_sup->sat_syntax;
427                 sat->sat_equality = sat->sat_sup->sat_equality;
428                 sat->sat_approx = sat->sat_sup->sat_approx;
429                 sat->sat_ordering = sat->sat_sup->sat_ordering;
430                 sat->sat_substr = sat->sat_sup->sat_substr;
431         }
432
433         if ( at->at_syntax_oid ) {
434                 syn = syn_find(sat->sat_syntax_oid);
435                 if ( syn == NULL ) {
436                         *err = sat->sat_syntax_oid;
437                         return SLAP_SCHERR_SYN_NOT_FOUND;
438                 }
439
440                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
441                         return SLAP_SCHERR_ATTR_BAD_SUP;
442                 }
443
444                 sat->sat_syntax = syn;
445
446         } else if ( sat->sat_syntax == NULL ) {
447                 return SLAP_SCHERR_ATTR_INCOMPLETE;
448         }
449
450         if ( sat->sat_equality_oid ) {
451                 mr = mr_find(sat->sat_equality_oid);
452
453                 if( mr == NULL ) {
454                         *err = sat->sat_equality_oid;
455                         return SLAP_SCHERR_MR_NOT_FOUND;
456                 }
457
458                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
459                         *err = sat->sat_equality_oid;
460                         return SLAP_SCHERR_ATTR_BAD_MR;
461                 }
462
463                 if( sat->sat_syntax != mr->smr_syntax ) {
464                         if( mr->smr_compat_syntaxes == NULL ) {
465                                 *err = sat->sat_equality_oid;
466                                 return SLAP_SCHERR_ATTR_BAD_MR;
467                         }
468
469                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
470                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
471                                         i = -1;
472                                         break;
473                                 }
474                         }
475
476                         if( i >= 0 ) {
477                                 *err = sat->sat_equality_oid;
478                                 return SLAP_SCHERR_ATTR_BAD_MR;
479                         }
480                 }
481
482                 sat->sat_equality = mr;
483                 sat->sat_approx = mr->smr_associated;
484         }
485
486         if ( sat->sat_ordering_oid ) {
487                 mr = mr_find(sat->sat_ordering_oid);
488
489                 if( mr == NULL ) {
490                         *err = sat->sat_ordering_oid;
491                         return SLAP_SCHERR_MR_NOT_FOUND;
492                 }
493
494                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
495                         *err = sat->sat_ordering_oid;
496                         return SLAP_SCHERR_ATTR_BAD_MR;
497                 }
498
499                 if( sat->sat_syntax != mr->smr_syntax ) {
500                         if( mr->smr_compat_syntaxes == NULL ) {
501                                 *err = sat->sat_ordering_oid;
502                                 return SLAP_SCHERR_ATTR_BAD_MR;
503                         }
504
505                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
506                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
507                                         i = -1;
508                                         break;
509                                 }
510                         }
511
512                         if( i >= 0 ) {
513                                 *err = sat->sat_ordering_oid;
514                                 return SLAP_SCHERR_ATTR_BAD_MR;
515                         }
516                 }
517
518                 sat->sat_ordering = mr;
519         }
520
521         if ( sat->sat_substr_oid ) {
522                 mr = mr_find(sat->sat_substr_oid);
523
524                 if( mr == NULL ) {
525                         *err = sat->sat_substr_oid;
526                         return SLAP_SCHERR_MR_NOT_FOUND;
527                 }
528
529                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
530                         *err = sat->sat_substr_oid;
531                         return SLAP_SCHERR_ATTR_BAD_MR;
532                 }
533
534                 /* due to funky LDAP builtin substring rules, we
535                  * we check against the equality rule assertion
536                  * syntax and compat syntaxes instead of those
537                  * associated with the substrings rule.
538                  */
539                 if( sat->sat_equality &&
540                         sat->sat_syntax != sat->sat_equality->smr_syntax )
541                 {
542                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
543                                 *err = sat->sat_substr_oid;
544                                 return SLAP_SCHERR_ATTR_BAD_MR;
545                         }
546
547                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
548                                 if( sat->sat_syntax ==
549                                         sat->sat_equality->smr_compat_syntaxes[i] )
550                                 {
551                                         i = -1;
552                                         break;
553                                 }
554                         }
555
556                         if( i >= 0 ) {
557                                 *err = sat->sat_substr_oid;
558                                 return SLAP_SCHERR_ATTR_BAD_MR;
559                         }
560                 }
561
562                 sat->sat_substr = mr;
563         }
564
565         code = at_insert(sat,err);
566         return code;
567 }
568
569 #ifdef LDAP_DEBUG
570 static int
571 at_index_printnode( void *v_air, void *ignore )
572 {
573         struct aindexrec *air = v_air;
574         printf("%s = %s\n",
575                 air->air_name.bv_val,
576                 ldap_attributetype2str(&air->air_at->sat_atype) );
577         return( 0 );
578 }
579
580 static void
581 at_index_print( void )
582 {
583         printf("Printing attribute type index:\n");
584         (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
585 }
586 #endif
587
588 int
589 at_schema_info( Entry *e )
590 {
591         struct berval   vals[2];
592         AttributeType   *at;
593
594         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
595
596         vals[1].bv_val = NULL;
597
598         LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
599                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
600
601                 if ( ldap_attributetype2bv( &at->sat_atype, vals ) == NULL ) {
602                         return -1;
603                 }
604
605                 if( attr_merge( e, ad_attributeTypes, vals ) )
606                         return -1;
607                 ldap_memfree( vals[0].bv_val );
608         }
609         return 0;
610 }