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