]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
streamline group attr specification/diagnostics
[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-2007 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 "slap.h"
28
29
30 const char *
31 at_syntax(
32         AttributeType   *at )
33 {
34         for ( ; at != NULL; at = at->sat_sup ) {
35                 if ( at->sat_syntax_oid ) {
36                         return at->sat_syntax_oid;
37                 }
38         }
39
40         assert( 0 );
41
42         return NULL;
43 }
44
45 int
46 is_at_syntax(
47         AttributeType   *at,
48         const char      *oid )
49 {
50         const char *syn_oid = at_syntax( at );
51
52         if ( syn_oid ) {
53                 return strcmp( syn_oid, oid ) == 0;
54         }
55
56         return 0;
57 }
58
59 int is_at_subtype(
60         AttributeType *sub,
61         AttributeType *sup )
62 {
63         for( ; sub != NULL; sub = sub->sat_sup ) {
64                 if( sub == sup ) return 1;
65         }
66
67         return 0;
68 }
69
70 struct aindexrec {
71         struct berval   air_name;
72         AttributeType   *air_at;
73 };
74
75 static Avlnode  *attr_index = NULL;
76 static Avlnode  *attr_cache = NULL;
77 static LDAP_STAILQ_HEAD(ATList, slap_attribute_type) attr_list
78         = LDAP_STAILQ_HEAD_INITIALIZER(attr_list);
79
80 /* Last hardcoded attribute registered */
81 AttributeType *at_sys_tail;
82
83 int at_oc_cache;
84
85 static int
86 attr_index_cmp(
87     const void  *v_air1,
88     const void  *v_air2 )
89 {
90         const struct aindexrec  *air1 = v_air1;
91         const struct aindexrec  *air2 = v_air2;
92         int i = air1->air_name.bv_len - air2->air_name.bv_len;
93         if (i) return i;
94         return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
95 }
96
97 static int
98 attr_index_name_cmp(
99     const void  *v_type,
100     const void  *v_air )
101 {
102     const struct berval    *type = v_type;
103     const struct aindexrec *air  = v_air;
104         int i = type->bv_len - air->air_name.bv_len;
105         if (i) return i;
106         return (strncasecmp( type->bv_val, air->air_name.bv_val, type->bv_len ));
107 }
108
109 AttributeType *
110 at_find( const char *name )
111 {
112         struct berval bv;
113
114         bv.bv_val = (char *)name;
115         bv.bv_len = strlen( name );
116
117         return at_bvfind( &bv );
118 }
119
120 AttributeType *
121 at_bvfind( struct berval *name )
122 {
123         struct aindexrec *air;
124
125         if ( attr_cache ) {
126                 air = avl_find( attr_cache, name, attr_index_name_cmp );
127                 if ( air ) return air->air_at;
128         }
129
130         air = avl_find( attr_index, name, attr_index_name_cmp );
131
132         if ( air ) {
133                 if ( air->air_at->sat_flags & SLAP_AT_DELETED ) {
134                         air = NULL;
135                 } else if (( slapMode & SLAP_TOOL_MODE ) && at_oc_cache ) {
136                         avl_insert( &attr_cache, (caddr_t) air,
137                                 attr_index_cmp, avl_dup_error );
138                 }
139         }
140
141         return air != NULL ? air->air_at : NULL;
142 }
143
144 int
145 at_append_to_list(
146     AttributeType       *sat,
147     AttributeType       ***listp )
148 {
149         AttributeType   **list;
150         AttributeType   **list1;
151         int             size;
152
153         list = *listp;
154         if ( !list ) {
155                 size = 2;
156                 list = ch_calloc(size, sizeof(AttributeType *));
157                 if ( !list ) {
158                         return -1;
159                 }
160         } else {
161                 size = 0;
162                 list1 = *listp;
163                 while ( *list1 ) {
164                         size++;
165                         list1++;
166                 }
167                 size += 2;
168                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
169                 if ( !list1 ) {
170                         return -1;
171                 }
172                 list = list1;
173         }
174         list[size-2] = sat;
175         list[size-1] = NULL;
176         *listp = list;
177         return 0;
178 }
179
180 int
181 at_delete_from_list(
182     int                 pos,
183     AttributeType       ***listp )
184 {
185         AttributeType   **list;
186         AttributeType   **list1;
187         int             i;
188         int             j;
189
190         if ( pos < 0 ) {
191                 return -2;
192         }
193         list = *listp;
194         for ( i=0; list[i]; i++ )
195                 ;
196         if ( pos >= i ) {
197                 return -2;
198         }
199         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
200                 list[i] = list[j];
201         }
202         list[i] = NULL;
203         /* Tell the runtime this can be shrinked */
204         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
205         if ( !list1 ) {
206                 return -1;
207         }
208         *listp = list1;
209         return 0;
210 }
211
212 int
213 at_find_in_list(
214     AttributeType       *sat,
215     AttributeType       **list )
216 {
217         int     i;
218
219         if ( !list ) {
220                 return -1;
221         }
222         for ( i=0; list[i]; i++ ) {
223                 if ( sat == list[i] ) {
224                         return i;
225                 }
226         }
227         return -1;
228 }
229
230 static void
231 at_delete_names( AttributeType *at )
232 {
233         char                    **names = at->sat_names;
234
235         while (*names) {
236                 struct aindexrec        tmpair, *air;
237
238                 ber_str2bv( *names, 0, 0, &tmpair.air_name );
239                 tmpair.air_at = at;
240                 air = (struct aindexrec *)avl_delete( &attr_index,
241                         (caddr_t)&tmpair, attr_index_cmp );
242                 assert( air != NULL );
243                 ldap_memfree( air );
244                 names++;
245         }
246 }
247
248 /* Mark the attribute as deleted, remove from list, and remove all its
249  * names from the AVL tree. Leave the OID in the tree.
250  */
251 void
252 at_delete( AttributeType *at )
253 {
254         at->sat_flags |= SLAP_AT_DELETED;
255
256         LDAP_STAILQ_REMOVE(&attr_list,at,slap_attribute_type,sat_next);
257
258         at_delete_names( at );
259 }
260
261 static void
262 at_clean( AttributeType *a )
263 {
264         if ( a->sat_equality ) {
265                 MatchingRule    *mr;
266
267                 mr = mr_find( a->sat_equality->smr_oid );
268                 assert( mr != NULL );
269                 if ( mr != a->sat_equality ) {
270                         ch_free( a->sat_equality );
271                         a->sat_equality = NULL;
272                 }
273         }
274
275         assert( a->sat_syntax != NULL );
276         if ( a->sat_syntax != NULL ) {
277                 Syntax          *syn;
278
279                 syn = syn_find( a->sat_syntax->ssyn_oid );
280                 assert( syn != NULL );
281                 if ( syn != a->sat_syntax ) {
282                         ch_free( a->sat_syntax );
283                         a->sat_syntax = NULL;
284                 }
285         }
286
287         if ( a->sat_oidmacro ) ldap_memfree( a->sat_oidmacro );
288         if ( a->sat_subtypes ) ldap_memfree( a->sat_subtypes );
289 }
290
291 static void
292 at_destroy_one( void *v )
293 {
294         struct aindexrec *air = v;
295         AttributeType *a = air->air_at;
296
297         at_clean( a );
298         ad_destroy(a->sat_ad);
299         ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
300         ldap_attributetype_free((LDAPAttributeType *)a);
301         ldap_memfree(air);
302 }
303
304 void
305 at_destroy( void )
306 {
307         AttributeType *a;
308
309         while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
310                 a = LDAP_STAILQ_FIRST(&attr_list);
311                 LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
312
313                 at_delete_names( a );
314         }
315
316         avl_free(attr_index, at_destroy_one);
317
318         if ( slap_schema.si_at_undefined ) {
319                 ad_destroy(slap_schema.si_at_undefined->sat_ad);
320         }
321
322         if ( slap_schema.si_at_proxied ) {
323                 ad_destroy(slap_schema.si_at_proxied->sat_ad);
324         }
325 }
326
327 int
328 at_start( AttributeType **at )
329 {
330         assert( at != NULL );
331
332         *at = LDAP_STAILQ_FIRST(&attr_list);
333
334         return (*at != NULL);
335 }
336
337 int
338 at_next( AttributeType **at )
339 {
340         assert( at != NULL );
341
342 #if 1   /* pedantic check */
343         {
344                 AttributeType *tmp = NULL;
345
346                 LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
347                         if ( tmp == *at ) {
348                                 break;
349                         }
350                 }
351
352                 assert( tmp != NULL );
353         }
354 #endif
355
356         *at = LDAP_STAILQ_NEXT(*at,sat_next);
357
358         return (*at != NULL);
359 }
360
361 /*
362  * check whether the two attributeTypes actually __are__ identical,
363  * or rather inconsistent
364  */
365 static int
366 at_check_dup(
367         AttributeType           *sat,
368         AttributeType           *new_sat )
369 {
370         if ( new_sat->sat_oid != NULL ) {
371                 if ( sat->sat_oid == NULL ) {
372                         return SLAP_SCHERR_ATTR_INCONSISTENT;
373                 }
374
375                 if ( strcmp( sat->sat_oid, new_sat->sat_oid ) != 0 ) {
376                         return SLAP_SCHERR_ATTR_INCONSISTENT;
377                 }
378
379         } else {
380                 if ( sat->sat_oid != NULL ) {
381                         return SLAP_SCHERR_ATTR_INCONSISTENT;
382                 }
383         }
384
385         if ( new_sat->sat_names ) {
386                 int     i;
387
388                 if ( sat->sat_names == NULL ) {
389                         return SLAP_SCHERR_ATTR_INCONSISTENT;
390                 }
391
392                 for ( i = 0; new_sat->sat_names[ i ]; i++ ) {
393                         if ( sat->sat_names[ i ] == NULL ) {
394                                 return SLAP_SCHERR_ATTR_INCONSISTENT;
395                         }
396                         
397                         if ( strcasecmp( sat->sat_names[ i ],
398                                         new_sat->sat_names[ i ] ) != 0 )
399                         {
400                                 return SLAP_SCHERR_ATTR_INCONSISTENT;
401                         }
402                 }
403         } else {
404                 if ( sat->sat_names != NULL ) {
405                         return SLAP_SCHERR_ATTR_INCONSISTENT;
406                 }
407         }
408
409         return SLAP_SCHERR_ATTR_DUP;
410 }
411
412 static struct aindexrec *air_old;
413
414 static int
415 at_dup_error( void *left, void *right )
416 {
417         air_old = left;
418         return -1;
419 }
420
421 static int
422 at_insert(
423     AttributeType       **rat,
424         AttributeType   *prev,
425     const char          **err )
426 {
427         struct aindexrec        *air;
428         char                    **names = NULL;
429         AttributeType   *sat = *rat;
430
431         if ( sat->sat_oid ) {
432                 air = (struct aindexrec *)
433                         ch_calloc( 1, sizeof(struct aindexrec) );
434                 ber_str2bv( sat->sat_oid, 0, 0, &air->air_name );
435                 air->air_at = sat;
436                 air_old = NULL;
437
438                 if ( avl_insert( &attr_index, (caddr_t) air,
439                                  attr_index_cmp, at_dup_error ) )
440                 {
441                         AttributeType   *old_sat;
442                         int             rc;
443
444                         *err = sat->sat_oid;
445
446                         assert( air_old != NULL );
447                         old_sat = air_old->air_at;
448
449                         /* replacing a deleted definition? */
450                         if ( old_sat->sat_flags & SLAP_AT_DELETED ) {
451                                 AttributeType tmp;
452                                 
453                                 /* Keep old oid, free new oid;
454                                  * Keep old ads, free new ads;
455                                  * Keep new everything else, free old
456                                  */
457                                 tmp = *old_sat;
458                                 *old_sat = *sat;
459                                 old_sat->sat_oid = tmp.sat_oid;
460                                 tmp.sat_oid = sat->sat_oid;
461                                 old_sat->sat_ad = tmp.sat_ad;
462                                 tmp.sat_ad = sat->sat_ad;
463                                 *sat = tmp;
464
465                                 at_clean( sat );
466                                 at_destroy_one( air );
467
468                                 air = air_old;
469                                 sat = old_sat;
470                                 *rat = sat;
471                         } else {
472                                 ldap_memfree( air );
473
474                                 rc = at_check_dup( old_sat, sat );
475
476                                 return rc;
477                         }
478                 }
479                 /* FIX: temporal consistency check */
480                 at_bvfind( &air->air_name );
481         }
482
483         names = sat->sat_names;
484         if ( names ) {
485                 while ( *names ) {
486                         air = (struct aindexrec *)
487                                 ch_calloc( 1, sizeof(struct aindexrec) );
488                         ber_str2bv( *names, 0, 0, &air->air_name );
489                         air->air_at = sat;
490                         if ( avl_insert( &attr_index, (caddr_t) air,
491                                          attr_index_cmp, avl_dup_error ) )
492                         {
493                                 AttributeType   *old_sat;
494                                 int             rc;
495
496                                 *err = *names;
497
498                                 old_sat = at_bvfind( &air->air_name );
499                                 assert( old_sat != NULL );
500                                 rc = at_check_dup( old_sat, sat );
501
502                                 ldap_memfree(air);
503
504                                 while ( names > sat->sat_names ) {
505                                         struct aindexrec        tmpair;
506
507                                         names--;
508                                         ber_str2bv( *names, 0, 0, &tmpair.air_name );
509                                         tmpair.air_at = sat;
510                                         air = (struct aindexrec *)avl_delete( &attr_index,
511                                                 (caddr_t)&tmpair, attr_index_cmp );
512                                         assert( air != NULL );
513                                         ldap_memfree( air );
514                                 }
515
516                                 if ( sat->sat_oid ) {
517                                         struct aindexrec        tmpair;
518
519                                         ber_str2bv( sat->sat_oid, 0, 0, &tmpair.air_name );
520                                         tmpair.air_at = sat;
521                                         air = (struct aindexrec *)avl_delete( &attr_index,
522                                                 (caddr_t)&tmpair, attr_index_cmp );
523                                         assert( air != NULL );
524                                         ldap_memfree( air );
525                                 }
526
527                                 return rc;
528                         }
529                         /* FIX: temporal consistency check */
530                         at_bvfind(&air->air_name);
531                         names++;
532                 }
533         }
534
535         if ( sat->sat_oid ) {
536                 slap_ad_undef_promote( sat->sat_oid, sat );
537         }
538
539         names = sat->sat_names;
540         if ( names ) {
541                 while ( *names ) {
542                         slap_ad_undef_promote( *names, sat );
543                         names++;
544                 }
545         }
546
547         if ( sat->sat_flags & SLAP_AT_HARDCODE ) {
548                 prev = at_sys_tail;
549                 at_sys_tail = sat;
550         }
551         if ( prev ) {
552                 LDAP_STAILQ_INSERT_AFTER( &attr_list, prev, sat, sat_next );
553         } else {
554                 LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
555         }
556
557         return 0;
558 }
559
560 int
561 at_add(
562         LDAPAttributeType       *at,
563         int                     user,
564         AttributeType           **rsat,
565         AttributeType   *prev,
566         const char              **err )
567 {
568         AttributeType   *sat = NULL;
569         MatchingRule    *mr = NULL;
570         Syntax          *syn = NULL;
571         int             i;
572         int             code = LDAP_SUCCESS;
573         char            *cname = NULL;
574         char            *oidm = NULL;
575
576         if ( !at->at_oid ) {
577                 *err = "";
578                 return SLAP_SCHERR_ATTR_INCOMPLETE;
579         }
580
581         if ( !OID_LEADCHAR( at->at_oid[0] )) {
582                 char    *oid;
583
584                 /* Expand OID macros */
585                 oid = oidm_find( at->at_oid );
586                 if ( !oid ) {
587                         *err = at->at_oid;
588                         return SLAP_SCHERR_OIDM;
589                 }
590                 if ( oid != at->at_oid ) {
591                         oidm = at->at_oid;
592                         at->at_oid = oid;
593                 }
594         }
595
596         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
597                 char    *oid;
598
599                 /* Expand OID macros */
600                 oid = oidm_find( at->at_syntax_oid );
601                 if ( !oid ) {
602                         *err = at->at_syntax_oid;
603                         code = SLAP_SCHERR_OIDM;
604                         goto error_return;
605                 }
606                 if ( oid != at->at_syntax_oid ) {
607                         ldap_memfree( at->at_syntax_oid );
608                         at->at_syntax_oid = oid;
609                 }
610         }
611
612         if ( at->at_names && at->at_names[0] ) {
613                 int i;
614
615                 for( i=0; at->at_names[i]; i++ ) {
616                         if( !slap_valid_descr( at->at_names[i] ) ) {
617                                 *err = at->at_names[i];
618                                 code = SLAP_SCHERR_BAD_DESCR;
619                                 goto error_return;
620                         }
621                 }
622
623                 cname = at->at_names[0];
624
625         } else {
626                 cname = at->at_oid;
627
628         }
629
630         *err = cname;
631
632         if ( !at->at_usage && at->at_no_user_mod ) {
633                 /* user attribute must be modifable */
634                 code = SLAP_SCHERR_ATTR_BAD_USAGE;
635                 goto error_return;
636         }
637
638         if ( at->at_collective ) {
639                 if( at->at_usage ) {
640                         /* collective attributes cannot be operational */
641                         code = SLAP_SCHERR_ATTR_BAD_USAGE;
642                         goto error_return;
643                 }
644
645                 if( at->at_single_value ) {
646                         /* collective attributes cannot be single-valued */
647                         code = SLAP_SCHERR_ATTR_BAD_USAGE;
648                         goto error_return;
649                 }
650         }
651
652         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
653         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
654
655         sat->sat_cname.bv_val = cname;
656         sat->sat_cname.bv_len = strlen( cname );
657         sat->sat_oidmacro = oidm;
658         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
659
660         if ( at->at_sup_oid ) {
661                 AttributeType *supsat = at_find(at->at_sup_oid);
662
663                 if ( supsat == NULL ) {
664                         *err = at->at_sup_oid;
665                         code = SLAP_SCHERR_ATTR_NOT_FOUND;
666                         goto error_return;
667                 }
668
669                 sat->sat_sup = supsat;
670
671                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
672                         code = SLAP_SCHERR_OUTOFMEM;
673                         goto error_return;
674                 }
675
676                 if ( sat->sat_usage != supsat->sat_usage ) {
677                         /* subtypes must have same usage as their SUP */
678                         code = SLAP_SCHERR_ATTR_BAD_USAGE;
679                         goto error_return;
680                 }
681
682                 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
683                         /* subtypes must be obsolete if super is */
684                         code = SLAP_SCHERR_ATTR_BAD_SUP;
685                         goto error_return;
686                 }
687
688                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
689                         /* cannot subtype a "final" attribute type */
690                         code = SLAP_SCHERR_ATTR_BAD_SUP;
691                         goto error_return;
692                 }
693         }
694
695         /*
696          * Inherit definitions from superiors.  We only check the
697          * direct superior since that one has already inherited from
698          * its own superiorss
699          */
700         if ( sat->sat_sup ) {
701                 sat->sat_syntax = sat->sat_sup->sat_syntax;
702                 sat->sat_equality = sat->sat_sup->sat_equality;
703                 sat->sat_approx = sat->sat_sup->sat_approx;
704                 sat->sat_ordering = sat->sat_sup->sat_ordering;
705                 sat->sat_substr = sat->sat_sup->sat_substr;
706         }
707
708         /*
709          * check for X-ORDERED attributes
710          */
711         if ( sat->sat_extensions ) {
712                 for (i=0; sat->sat_extensions[i]; i++) {
713                         if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
714                                 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
715                                 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
716                                         "VALUES" )) {
717                                         sat->sat_flags |= SLAP_AT_ORDERED_VAL;
718                                         break;
719                                 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
720                                         "SIBLINGS" )) {
721                                         sat->sat_flags |= SLAP_AT_ORDERED_SIB;
722                                         break;
723                                 }
724                         }
725                 }
726         }
727
728         if ( !user )
729                 sat->sat_flags |= SLAP_AT_HARDCODE;
730
731         if ( at->at_syntax_oid ) {
732                 syn = syn_find(sat->sat_syntax_oid);
733                 if ( syn == NULL ) {
734                         *err = sat->sat_syntax_oid;
735                         code = SLAP_SCHERR_SYN_NOT_FOUND;
736                         goto error_return;
737                 }
738
739                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
740                         code = SLAP_SCHERR_ATTR_BAD_SUP;
741                         goto error_return;
742                 }
743
744                 sat->sat_syntax = syn;
745
746         } else if ( sat->sat_syntax == NULL ) {
747                 code = SLAP_SCHERR_ATTR_INCOMPLETE;
748                 goto error_return;
749         }
750
751         if ( sat->sat_equality_oid ) {
752                 mr = mr_find(sat->sat_equality_oid);
753
754                 if( mr == NULL ) {
755                         *err = sat->sat_equality_oid;
756                         code = SLAP_SCHERR_MR_NOT_FOUND;
757                         goto error_return;
758                 }
759
760                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
761                         *err = sat->sat_equality_oid;
762                         code = SLAP_SCHERR_ATTR_BAD_MR;
763                         goto error_return;
764                 }
765
766                 if( sat->sat_syntax != mr->smr_syntax ) {
767                         if( mr->smr_compat_syntaxes == NULL ) {
768                                 *err = sat->sat_equality_oid;
769                                 code = SLAP_SCHERR_ATTR_BAD_MR;
770                                 goto error_return;
771                         }
772
773                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
774                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
775                                         i = -1;
776                                         break;
777                                 }
778                         }
779
780                         if( i >= 0 ) {
781                                 *err = sat->sat_equality_oid;
782                                 code = SLAP_SCHERR_ATTR_BAD_MR;
783                                 goto error_return;
784                         }
785                 }
786
787                 sat->sat_equality = mr;
788                 sat->sat_approx = mr->smr_associated;
789         }
790
791         if ( sat->sat_ordering_oid ) {
792                 if( !sat->sat_equality ) {
793                         *err = sat->sat_ordering_oid;
794                         code = SLAP_SCHERR_ATTR_BAD_MR;
795                         goto error_return;
796                 }
797
798                 mr = mr_find(sat->sat_ordering_oid);
799
800                 if( mr == NULL ) {
801                         *err = sat->sat_ordering_oid;
802                         code = SLAP_SCHERR_MR_NOT_FOUND;
803                         goto error_return;
804                 }
805
806                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
807                         *err = sat->sat_ordering_oid;
808                         code = SLAP_SCHERR_ATTR_BAD_MR;
809                         goto error_return;
810                 }
811
812                 if( sat->sat_syntax != mr->smr_syntax ) {
813                         if( mr->smr_compat_syntaxes == NULL ) {
814                                 *err = sat->sat_ordering_oid;
815                                 code = SLAP_SCHERR_ATTR_BAD_MR;
816                                 goto error_return;
817                         }
818
819                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
820                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
821                                         i = -1;
822                                         break;
823                                 }
824                         }
825
826                         if( i >= 0 ) {
827                                 *err = sat->sat_ordering_oid;
828                                 code = SLAP_SCHERR_ATTR_BAD_MR;
829                                 goto error_return;
830                         }
831                 }
832
833                 sat->sat_ordering = mr;
834         }
835
836         if ( sat->sat_substr_oid ) {
837                 if( !sat->sat_equality ) {
838                         *err = sat->sat_substr_oid;
839                         code = SLAP_SCHERR_ATTR_BAD_MR;
840                         goto error_return;
841                 }
842
843                 mr = mr_find(sat->sat_substr_oid);
844
845                 if( mr == NULL ) {
846                         *err = sat->sat_substr_oid;
847                         code = SLAP_SCHERR_MR_NOT_FOUND;
848                         goto error_return;
849                 }
850
851                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
852                         *err = sat->sat_substr_oid;
853                         code = SLAP_SCHERR_ATTR_BAD_MR;
854                         goto error_return;
855                 }
856
857                 /* due to funky LDAP builtin substring rules,
858                  * we check against the equality rule assertion
859                  * syntax and compat syntaxes instead of those
860                  * associated with the substrings rule.
861                  */
862                 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
863                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
864                                 *err = sat->sat_substr_oid;
865                                 code = SLAP_SCHERR_ATTR_BAD_MR;
866                                 goto error_return;
867                         }
868
869                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
870                                 if( sat->sat_syntax ==
871                                         sat->sat_equality->smr_compat_syntaxes[i] )
872                                 {
873                                         i = -1;
874                                         break;
875                                 }
876                         }
877
878                         if( i >= 0 ) {
879                                 *err = sat->sat_substr_oid;
880                                 code = SLAP_SCHERR_ATTR_BAD_MR;
881                                 goto error_return;
882                         }
883                 }
884
885                 sat->sat_substr = mr;
886         }
887
888         code = at_insert( &sat, prev, err );
889         if ( code != 0 ) {
890 error_return:;
891                 if ( sat ) {
892                         ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
893                         ch_free( sat );
894                 }
895
896                 if ( oidm ) {
897                         SLAP_FREE( at->at_oid );
898                         at->at_oid = oidm;
899                 }
900
901         } else if ( rsat ) {
902                 *rsat = sat;
903         }
904
905         return code;
906 }
907
908 #ifdef LDAP_DEBUG
909 #ifdef SLAPD_UNUSED
910 static int
911 at_index_printnode( void *v_air, void *ignore )
912 {
913         struct aindexrec *air = v_air;
914         printf("%s = %s\n",
915                 air->air_name.bv_val,
916                 ldap_attributetype2str(&air->air_at->sat_atype) );
917         return( 0 );
918 }
919
920 static void
921 at_index_print( void )
922 {
923         printf("Printing attribute type index:\n");
924         (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
925 }
926 #endif
927 #endif
928
929 void
930 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
931 {
932         AttributeType *at;
933         int i, num;
934         struct berval bv, *bva = NULL, idx;
935         char ibuf[32];
936
937         if ( !start )
938                 start = LDAP_STAILQ_FIRST( &attr_list );
939
940         /* count the result size */
941         i = 0;
942         for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
943                 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
944                 i++;
945                 if ( at == end ) break;
946         }
947         if (!i) return;
948
949         num = i;
950         bva = ch_malloc( (num+1) * sizeof(struct berval) );
951         BER_BVZERO( bva );
952         idx.bv_val = ibuf;
953         if ( sys ) {
954                 idx.bv_len = 0;
955                 ibuf[0] = '\0';
956         }
957         i = 0;
958         for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
959                 LDAPAttributeType lat, *latp;
960                 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) break;
961                 if ( at->sat_oidmacro ) {
962                         lat = at->sat_atype;
963                         lat.at_oid = at->sat_oidmacro;
964                         latp = &lat;
965                 } else {
966                         latp = &at->sat_atype;
967                 }
968                 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
969                         ber_bvarray_free( bva );
970                 }
971                 if ( !sys ) {
972                         idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
973                 }
974                 bva[i].bv_len = idx.bv_len + bv.bv_len;
975                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
976                 strcpy( bva[i].bv_val, ibuf );
977                 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
978                 i++;
979                 bva[i].bv_val = NULL;
980                 ldap_memfree( bv.bv_val );
981                 if ( at == end ) break;
982         }
983         *res = bva;
984 }
985
986 int
987 at_schema_info( Entry *e )
988 {
989         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
990         AttributeType   *at;
991         struct berval   val;
992         struct berval   nval;
993
994         LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
995                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
996
997                 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
998                         return -1;
999                 }
1000
1001                 ber_str2bv( at->sat_oid, 0, 0, &nval );
1002
1003                 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
1004                 {
1005                         return -1;
1006                 }
1007                 ldap_memfree( val.bv_val );
1008         }
1009         return 0;
1010 }
1011
1012 int
1013 register_at( char *def, AttributeDescription **rad, int dupok )
1014 {
1015         LDAPAttributeType *at;
1016         int code, freeit = 0;
1017         const char *err;
1018         AttributeDescription *ad = NULL;
1019
1020         at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1021         if ( !at ) {
1022                 Debug( LDAP_DEBUG_ANY,
1023                         "register_at: AttributeType \"%s\": %s, %s\n",
1024                                 def, ldap_scherr2str(code), err );
1025                 return code;
1026         }
1027
1028         code = at_add( at, 0, NULL, NULL, &err );
1029         if ( code ) {
1030                 if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) {
1031                         freeit = 1;
1032
1033                 } else {
1034                         ldap_attributetype_free( at );
1035                         Debug( LDAP_DEBUG_ANY,
1036                                 "register_at: AttributeType \"%s\": %s, %s\n",
1037                                 def, scherr2str(code), err );
1038                         return code;
1039                 }
1040         }
1041         code = slap_str2ad( at->at_names[0], &ad, &err );
1042         if ( freeit || code ) {
1043                 ldap_attributetype_free( at );
1044         } else {
1045                 ldap_memfree( at );
1046         }
1047         if ( code ) {
1048                 Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n",
1049                         def, err, 0 );
1050         }
1051         if ( rad ) *rad = ad;
1052         return code;
1053 }