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