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