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