]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
Simpler fix for NO_THREADS
[openldap] / servers / slapd / at.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 AttributeType *attr_list = NULL;
53
54 static int
55 attr_index_cmp(
56     struct aindexrec    *air1,
57     struct aindexrec    *air2
58 )
59 {
60         int i = air1->air_name.bv_len - air2->air_name.bv_len;
61         if (i)
62                 return i;
63         return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
64 }
65
66 static int
67 attr_index_name_cmp(
68     struct berval       *type,
69     struct aindexrec    *air
70 )
71 {
72         int i = type->bv_len - air->air_name.bv_len;
73         if (i)
74                 return i;
75         return (strncasecmp( type->bv_val, air->air_name.bv_val,
76                 type->bv_len ));
77 }
78
79 AttributeType *
80 at_find(
81     const char          *name
82 )
83 {
84         struct berval bv;
85
86         bv.bv_val = (char *)name;
87         bv.bv_len = strlen( name );
88
89         return at_bvfind( &bv );
90 }
91
92 AttributeType *
93 at_bvfind(
94     struct berval       *name
95 )
96 {
97         struct aindexrec *air;
98
99         air = (struct aindexrec *) avl_find( attr_index, name,
100             (AVL_CMP) attr_index_name_cmp );
101
102         return air != NULL ? air->air_at : NULL;
103 }
104
105 int
106 at_append_to_list(
107     AttributeType       *sat,
108     AttributeType       ***listp
109 )
110 {
111         AttributeType   **list;
112         AttributeType   **list1;
113         int             size;
114
115         list = *listp;
116         if ( !list ) {
117                 size = 2;
118                 list = ch_calloc(size, sizeof(AttributeType *));
119                 if ( !list ) {
120                         return -1;
121                 }
122         } else {
123                 size = 0;
124                 list1 = *listp;
125                 while ( *list1 ) {
126                         size++;
127                         list1++;
128                 }
129                 size += 2;
130                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
131                 if ( !list1 ) {
132                         return -1;
133                 }
134                 list = list1;
135         }
136         list[size-2] = sat;
137         list[size-1] = NULL;
138         *listp = list;
139         return 0;
140 }
141
142 int
143 at_delete_from_list(
144     int                 pos,
145     AttributeType       ***listp
146 )
147 {
148         AttributeType   **list;
149         AttributeType   **list1;
150         int             i;
151         int             j;
152
153         if ( pos < 0 ) {
154                 return -2;
155         }
156         list = *listp;
157         for ( i=0; list[i]; i++ )
158                 ;
159         if ( pos >= i ) {
160                 return -2;
161         }
162         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
163                 list[i] = list[j];
164         }
165         list[i] = NULL;
166         /* Tell the runtime this can be shrinked */
167         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
168         if ( !list1 ) {
169                 return -1;
170         }
171         *listp = list1;
172         return 0;
173 }
174
175 int
176 at_find_in_list(
177     AttributeType       *sat,
178     AttributeType       **list
179 )
180 {
181         int     i;
182
183         if ( !list ) {
184                 return -1;
185         }
186         for ( i=0; list[i]; i++ ) {
187                 if ( sat == list[i] ) {
188                         return i;
189                 }
190         }
191         return -1;
192 }
193
194 void
195 at_destroy( void )
196 {
197         AttributeType *a, *n;
198         avl_free(attr_index, ldap_memfree);
199
200         for (a=attr_list; a; a=n) {
201                 n = a->sat_next;
202                 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
203                 ad_destroy(a->sat_ad);
204                 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
205                 ldap_attributetype_free((LDAPAttributeType *)a);
206         }
207         if ( slap_schema.si_at_undefined )
208                 ad_destroy(slap_schema.si_at_undefined->sat_ad);
209 }
210
211 int
212 at_start( AttributeType **at )
213 {
214         assert( at );
215
216         *at = attr_list;
217
218         return (*at != NULL);
219 }
220
221 int
222 at_next( AttributeType **at )
223 {
224         assert( at );
225
226 #if 1   /* pedantic check */
227         {
228                 AttributeType *tmp;
229
230                 for ( tmp = attr_list; tmp; tmp = tmp->sat_next ) {
231                         if ( tmp == *at ) {
232                                 break;
233                         }
234                 }
235
236                 assert( tmp );
237         }
238 #endif
239
240         *at = (*at)->sat_next;
241
242         return (*at != NULL);
243 }
244         
245
246
247 static int
248 at_insert(
249     AttributeType       *sat,
250     const char          **err
251 )
252 {
253         AttributeType           **atp;
254         struct aindexrec        *air;
255         char                    **names;
256
257         atp = &attr_list;
258         while ( *atp != NULL ) {
259                 atp = &(*atp)->sat_next;
260         }
261         *atp = sat;
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                                  (AVL_CMP) attr_index_cmp,
271                                  (AVL_DUP) avl_dup_error ) ) {
272                         *err = sat->sat_oid;
273                         ldap_memfree(air);
274                         return SLAP_SCHERR_ATTR_DUP;
275                 }
276                 /* FIX: temporal consistency check */
277                 at_bvfind(&air->air_name);
278         }
279
280         if ( (names = sat->sat_names) ) {
281                 while ( *names ) {
282                         air = (struct aindexrec *)
283                                 ch_calloc( 1, sizeof(struct aindexrec) );
284                         air->air_name.bv_val = *names;
285                         air->air_name.bv_len = strlen(*names);
286                         air->air_at = sat;
287                         if ( avl_insert( &attr_index, (caddr_t) air,
288                                          (AVL_CMP) attr_index_cmp,
289                                          (AVL_DUP) avl_dup_error ) ) {
290                                 *err = *names;
291                                 ldap_memfree(air);
292                                 return SLAP_SCHERR_ATTR_DUP;
293                         }
294                         /* FIX: temporal consistency check */
295                         at_bvfind(&air->air_name);
296                         names++;
297                 }
298         }
299
300         return 0;
301 }
302
303 int
304 at_add(
305     LDAPAttributeType   *at,
306     const char          **err
307 )
308 {
309         AttributeType   *sat;
310         MatchingRule    *mr;
311         Syntax          *syn;
312         int             i;
313         int             code;
314         char    *cname;
315         char    *oid;
316
317         if ( !OID_LEADCHAR( at->at_oid[0] )) {
318                 /* Expand OID macros */
319                 oid = oidm_find( at->at_oid );
320                 if ( !oid ) {
321                         *err = at->at_oid;
322                         return SLAP_SCHERR_OIDM;
323                 }
324                 if ( oid != at->at_oid ) {
325                         ldap_memfree( at->at_oid );
326                         at->at_oid = oid;
327                 }
328         }
329
330         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
331                 /* Expand OID macros */
332                 oid = oidm_find( at->at_syntax_oid );
333                 if ( !oid ) {
334                         *err = at->at_syntax_oid;
335                         return SLAP_SCHERR_OIDM;
336                 }
337                 if ( oid != at->at_syntax_oid ) {
338                         ldap_memfree( at->at_syntax_oid );
339                         at->at_syntax_oid = oid;
340                 }
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 ( sat->sat_flags & SLAP_AT_FINAL ) {
410                         /* cannot subtype a "final" attribute type */
411                         return SLAP_SCHERR_ATTR_BAD_SUP;
412                 }
413         }
414
415         /*
416          * Inherit definitions from superiors.  We only check the
417          * direct superior since that one has already inherited from
418          * its own superiorss
419          */
420         if ( sat->sat_sup ) {
421                 sat->sat_syntax = sat->sat_sup->sat_syntax;
422                 sat->sat_equality = sat->sat_sup->sat_equality;
423                 sat->sat_approx = sat->sat_sup->sat_approx;
424                 sat->sat_ordering = sat->sat_sup->sat_ordering;
425                 sat->sat_substr = sat->sat_sup->sat_substr;
426         }
427
428         if ( at->at_syntax_oid ) {
429                 syn = syn_find(sat->sat_syntax_oid);
430                 if ( syn == NULL ) {
431                         *err = sat->sat_syntax_oid;
432                         return SLAP_SCHERR_SYN_NOT_FOUND;
433                 }
434
435                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
436                         return SLAP_SCHERR_ATTR_BAD_SUP;
437                 }
438
439                 sat->sat_syntax = syn;
440
441         } else if ( sat->sat_syntax == NULL ) {
442                 return SLAP_SCHERR_ATTR_INCOMPLETE;
443         }
444
445         if ( sat->sat_equality_oid ) {
446                 mr = mr_find(sat->sat_equality_oid);
447
448                 if( mr == NULL ) {
449                         *err = sat->sat_equality_oid;
450                         return SLAP_SCHERR_MR_NOT_FOUND;
451                 }
452
453                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
454                         *err = sat->sat_equality_oid;
455                         return SLAP_SCHERR_ATTR_BAD_MR;
456                 }
457
458                 if( sat->sat_syntax != mr->smr_syntax ) {
459                         if( mr->smr_compat_syntaxes == NULL ) {
460                                 *err = sat->sat_equality_oid;
461                                 return SLAP_SCHERR_ATTR_BAD_MR;
462                         }
463
464                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
465                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
466                                         i = -1;
467                                         break;
468                                 }
469                         }
470
471                         if( i >= 0 ) {
472                                 *err = sat->sat_equality_oid;
473                                 return SLAP_SCHERR_ATTR_BAD_MR;
474                         }
475                 }
476
477                 sat->sat_equality = mr;
478                 sat->sat_approx = mr->smr_associated;
479         }
480
481         if ( sat->sat_ordering_oid ) {
482                 mr = mr_find(sat->sat_ordering_oid);
483
484                 if( mr == NULL ) {
485                         *err = sat->sat_ordering_oid;
486                         return SLAP_SCHERR_MR_NOT_FOUND;
487                 }
488
489                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
490                         *err = sat->sat_ordering_oid;
491                         return SLAP_SCHERR_ATTR_BAD_MR;
492                 }
493
494                 if( sat->sat_syntax != mr->smr_syntax ) {
495                         if( mr->smr_compat_syntaxes == NULL ) {
496                                 *err = sat->sat_ordering_oid;
497                                 return SLAP_SCHERR_ATTR_BAD_MR;
498                         }
499
500                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
501                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
502                                         i = -1;
503                                         break;
504                                 }
505                         }
506
507                         if( i >= 0 ) {
508                                 *err = sat->sat_ordering_oid;
509                                 return SLAP_SCHERR_ATTR_BAD_MR;
510                         }
511                 }
512
513                 sat->sat_ordering = mr;
514         }
515
516         if ( sat->sat_substr_oid ) {
517                 mr = mr_find(sat->sat_substr_oid);
518
519                 if( mr == NULL ) {
520                         *err = sat->sat_substr_oid;
521                         return SLAP_SCHERR_MR_NOT_FOUND;
522                 }
523
524                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
525                         *err = sat->sat_substr_oid;
526                         return SLAP_SCHERR_ATTR_BAD_MR;
527                 }
528
529                 /* due to funky LDAP builtin substring rules, we
530                  * we check against the equality rule assertion
531                  * syntax and compat syntaxes instead of those
532                  * associated with the substrings rule.
533                  */
534                 if( sat->sat_equality &&
535                         sat->sat_syntax != sat->sat_equality->smr_syntax )
536                 {
537                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
538                                 *err = sat->sat_substr_oid;
539                                 return SLAP_SCHERR_ATTR_BAD_MR;
540                         }
541
542                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
543                                 if( sat->sat_syntax ==
544                                         sat->sat_equality->smr_compat_syntaxes[i] )
545                                 {
546                                         i = -1;
547                                         break;
548                                 }
549                         }
550
551                         if( i >= 0 ) {
552                                 *err = sat->sat_substr_oid;
553                                 return SLAP_SCHERR_ATTR_BAD_MR;
554                         }
555                 }
556
557                 sat->sat_substr = mr;
558         }
559
560         code = at_insert(sat,err);
561         return code;
562 }
563
564 #ifdef LDAP_DEBUG
565 static int
566 at_index_printnode( struct aindexrec *air )
567 {
568
569         printf("%s = %s\n",
570                 air->air_name.bv_val,
571                 ldap_attributetype2str(&air->air_at->sat_atype) );
572         return( 0 );
573 }
574
575 static void
576 at_index_print( void )
577 {
578         printf("Printing attribute type index:\n");
579         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
580                 0, -1, AVL_INORDER );
581 }
582 #endif
583
584 #if defined( SLAPD_SCHEMA_DN )
585 int
586 at_schema_info( Entry *e )
587 {
588         struct berval   vals[2];
589         AttributeType   *at;
590
591         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
592
593         vals[1].bv_val = NULL;
594
595         for ( at = attr_list; at; at = at->sat_next ) {
596                 if ( ldap_attributetype2bv( &at->sat_atype, vals ) == NULL ) {
597                         return -1;
598                 }
599
600                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
601
602 #if 0
603                 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
604                        (long) vals[0].bv_len, vals[0].bv_val, 0 );
605 #endif
606                 attr_merge( e, ad_attributeTypes, vals );
607                 ldap_memfree( vals[0].bv_val );
608         }
609         return 0;
610 }
611 #endif