]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
added slapi_operation_set_pb
[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 AttributeType *attr_list = NULL;
53
54 static int
55 attr_index_cmp(
56     const void  *v_air1,
57     const void  *v_air2
58 )
59 {
60         const struct aindexrec  *air1 = v_air1;
61         const struct aindexrec  *air2 = v_air2;
62         int i = air1->air_name.bv_len - air2->air_name.bv_len;
63         if (i)
64                 return i;
65         return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
66 }
67
68 static int
69 attr_index_name_cmp(
70     const void  *v_type,
71     const void  *v_air
72 )
73 {
74     const struct berval    *type = v_type;
75     const struct aindexrec *air  = v_air;
76         int i = type->bv_len - air->air_name.bv_len;
77         if (i)
78                 return i;
79         return (strncasecmp( type->bv_val, air->air_name.bv_val,
80                 type->bv_len ));
81 }
82
83 AttributeType *
84 at_find(
85     const char          *name
86 )
87 {
88         struct berval bv;
89
90         bv.bv_val = (char *)name;
91         bv.bv_len = strlen( name );
92
93         return at_bvfind( &bv );
94 }
95
96 AttributeType *
97 at_bvfind(
98     struct berval       *name
99 )
100 {
101         struct aindexrec *air;
102
103         air = avl_find( attr_index, name, attr_index_name_cmp );
104
105         return air != NULL ? air->air_at : NULL;
106 }
107
108 int
109 at_append_to_list(
110     AttributeType       *sat,
111     AttributeType       ***listp
112 )
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 {
151         AttributeType   **list;
152         AttributeType   **list1;
153         int             i;
154         int             j;
155
156         if ( pos < 0 ) {
157                 return -2;
158         }
159         list = *listp;
160         for ( i=0; list[i]; i++ )
161                 ;
162         if ( pos >= i ) {
163                 return -2;
164         }
165         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
166                 list[i] = list[j];
167         }
168         list[i] = NULL;
169         /* Tell the runtime this can be shrinked */
170         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
171         if ( !list1 ) {
172                 return -1;
173         }
174         *listp = list1;
175         return 0;
176 }
177
178 int
179 at_find_in_list(
180     AttributeType       *sat,
181     AttributeType       **list
182 )
183 {
184         int     i;
185
186         if ( !list ) {
187                 return -1;
188         }
189         for ( i=0; list[i]; i++ ) {
190                 if ( sat == list[i] ) {
191                         return i;
192                 }
193         }
194         return -1;
195 }
196
197 void
198 at_destroy( void )
199 {
200         AttributeType *a, *n;
201         avl_free(attr_index, ldap_memfree);
202
203         for (a=attr_list; a; a=n) {
204                 n = a->sat_next;
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         if ( slap_schema.si_at_undefined )
211                 ad_destroy(slap_schema.si_at_undefined->sat_ad);
212 }
213
214 int
215 at_start( AttributeType **at )
216 {
217         assert( at );
218
219         *at = attr_list;
220
221         return (*at != NULL);
222 }
223
224 int
225 at_next( AttributeType **at )
226 {
227         assert( at );
228
229 #if 1   /* pedantic check */
230         {
231                 AttributeType *tmp;
232
233                 for ( tmp = attr_list; tmp; tmp = tmp->sat_next ) {
234                         if ( tmp == *at ) {
235                                 break;
236                         }
237                 }
238
239                 assert( tmp );
240         }
241 #endif
242
243         *at = (*at)->sat_next;
244
245         return (*at != NULL);
246 }
247         
248
249
250 static int
251 at_insert(
252     AttributeType       *sat,
253     const char          **err
254 )
255 {
256         AttributeType           **atp;
257         struct aindexrec        *air;
258         char                    **names;
259
260         atp = &attr_list;
261         while ( *atp != NULL ) {
262                 atp = &(*atp)->sat_next;
263         }
264         *atp = sat;
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         for ( at = attr_list; at; at = at->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 }