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