]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
make sure NULL pointers are not dereferenced
[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 && ( 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 ( !at->at_oid ) {
461                 *err = "";
462                 return SLAP_SCHERR_ATTR_INCOMPLETE;
463         }
464
465         if ( !OID_LEADCHAR( at->at_oid[0] )) {
466                 char    *oid;
467
468                 /* Expand OID macros */
469                 oid = oidm_find( at->at_oid );
470                 if ( !oid ) {
471                         *err = at->at_oid;
472                         return SLAP_SCHERR_OIDM;
473                 }
474                 if ( oid != at->at_oid ) {
475                         oidm = at->at_oid;
476                         at->at_oid = oid;
477                 }
478         }
479
480         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
481                 char    *oid;
482
483                 /* Expand OID macros */
484                 oid = oidm_find( at->at_syntax_oid );
485                 if ( !oid ) {
486                         *err = at->at_syntax_oid;
487                         code = SLAP_SCHERR_OIDM;
488                         goto error_return;
489                 }
490                 if ( oid != at->at_syntax_oid ) {
491                         ldap_memfree( at->at_syntax_oid );
492                         at->at_syntax_oid = oid;
493                 }
494         }
495
496         if ( at->at_names && at->at_names[0] ) {
497                 int i;
498
499                 for( i=0; at->at_names[i]; i++ ) {
500                         if( !slap_valid_descr( at->at_names[i] ) ) {
501                                 *err = at->at_names[i];
502                                 code = SLAP_SCHERR_BAD_DESCR;
503                                 goto error_return;
504                         }
505                 }
506
507                 cname = at->at_names[0];
508
509         } else {
510                 cname = at->at_oid;
511
512         }
513
514         *err = cname;
515
516         if ( !at->at_usage && at->at_no_user_mod ) {
517                 /* user attribute must be modifable */
518                 code = SLAP_SCHERR_ATTR_BAD_USAGE;
519                 goto error_return;
520         }
521
522         if ( at->at_collective ) {
523                 if( at->at_usage ) {
524                         /* collective attributes cannot be operational */
525                         code = SLAP_SCHERR_ATTR_BAD_USAGE;
526                         goto error_return;
527                 }
528
529                 if( at->at_single_value ) {
530                         /* collective attributes cannot be single-valued */
531                         code = SLAP_SCHERR_ATTR_BAD_USAGE;
532                         goto error_return;
533                 }
534         }
535
536         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
537         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
538
539         sat->sat_cname.bv_val = cname;
540         sat->sat_cname.bv_len = strlen( cname );
541         sat->sat_oidmacro = oidm;
542         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
543
544         if ( at->at_sup_oid ) {
545                 AttributeType *supsat = at_find(at->at_sup_oid);
546
547                 if ( supsat == NULL ) {
548                         *err = at->at_sup_oid;
549                         code = SLAP_SCHERR_ATTR_NOT_FOUND;
550                         goto error_return;
551                 }
552
553                 sat->sat_sup = supsat;
554
555                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
556                         code = SLAP_SCHERR_OUTOFMEM;
557                         goto error_return;
558                 }
559
560                 if ( sat->sat_usage != supsat->sat_usage ) {
561                         /* subtypes must have same usage as their SUP */
562                         code = SLAP_SCHERR_ATTR_BAD_USAGE;
563                         goto error_return;
564                 }
565
566                 if ( supsat->sat_obsolete && !sat->sat_obsolete ) {
567                         /* subtypes must be obsolete if super is */
568                         code = SLAP_SCHERR_ATTR_BAD_SUP;
569                         goto error_return;
570                 }
571
572                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
573                         /* cannot subtype a "final" attribute type */
574                         code = SLAP_SCHERR_ATTR_BAD_SUP;
575                         goto error_return;
576                 }
577         }
578
579         /*
580          * Inherit definitions from superiors.  We only check the
581          * direct superior since that one has already inherited from
582          * its own superiorss
583          */
584         if ( sat->sat_sup ) {
585                 sat->sat_syntax = sat->sat_sup->sat_syntax;
586                 sat->sat_equality = sat->sat_sup->sat_equality;
587                 sat->sat_approx = sat->sat_sup->sat_approx;
588                 sat->sat_ordering = sat->sat_sup->sat_ordering;
589                 sat->sat_substr = sat->sat_sup->sat_substr;
590         }
591
592         /*
593          * check for X-ORDERED attributes
594          */
595         if ( sat->sat_extensions ) {
596                 for (i=0; sat->sat_extensions[i]; i++) {
597                         if (!strcasecmp( sat->sat_extensions[i]->lsei_name,
598                                 "X-ORDERED" ) && sat->sat_extensions[i]->lsei_values ) {
599                                 if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
600                                         "VALUES" )) {
601                                         sat->sat_flags |= SLAP_AT_ORDERED_VAL;
602                                         break;
603                                 } else if ( !strcasecmp( sat->sat_extensions[i]->lsei_values[0],
604                                         "SIBLINGS" )) {
605                                         sat->sat_flags |= SLAP_AT_ORDERED_SIB;
606                                         break;
607                                 }
608                         }
609                 }
610         }
611
612         if ( !user )
613                 sat->sat_flags |= SLAP_AT_HARDCODE;
614
615         if ( at->at_syntax_oid ) {
616                 syn = syn_find(sat->sat_syntax_oid);
617                 if ( syn == NULL ) {
618                         *err = sat->sat_syntax_oid;
619                         code = SLAP_SCHERR_SYN_NOT_FOUND;
620                         goto error_return;
621                 }
622
623                 if( sat->sat_syntax != NULL && sat->sat_syntax != syn ) {
624                         code = SLAP_SCHERR_ATTR_BAD_SUP;
625                         goto error_return;
626                 }
627
628                 sat->sat_syntax = syn;
629
630         } else if ( sat->sat_syntax == NULL ) {
631                 code = SLAP_SCHERR_ATTR_INCOMPLETE;
632                 goto error_return;
633         }
634
635         if ( sat->sat_equality_oid ) {
636                 mr = mr_find(sat->sat_equality_oid);
637
638                 if( mr == NULL ) {
639                         *err = sat->sat_equality_oid;
640                         code = SLAP_SCHERR_MR_NOT_FOUND;
641                         goto error_return;
642                 }
643
644                 if(( mr->smr_usage & SLAP_MR_EQUALITY ) != SLAP_MR_EQUALITY ) {
645                         *err = sat->sat_equality_oid;
646                         code = SLAP_SCHERR_ATTR_BAD_MR;
647                         goto error_return;
648                 }
649
650                 if( sat->sat_syntax != mr->smr_syntax ) {
651                         if( mr->smr_compat_syntaxes == NULL ) {
652                                 *err = sat->sat_equality_oid;
653                                 code = SLAP_SCHERR_ATTR_BAD_MR;
654                                 goto error_return;
655                         }
656
657                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
658                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
659                                         i = -1;
660                                         break;
661                                 }
662                         }
663
664                         if( i >= 0 ) {
665                                 *err = sat->sat_equality_oid;
666                                 code = SLAP_SCHERR_ATTR_BAD_MR;
667                                 goto error_return;
668                         }
669                 }
670
671                 sat->sat_equality = mr;
672                 sat->sat_approx = mr->smr_associated;
673         }
674
675         if ( sat->sat_ordering_oid ) {
676                 if( !sat->sat_equality ) {
677                         *err = sat->sat_ordering_oid;
678                         code = SLAP_SCHERR_ATTR_BAD_MR;
679                         goto error_return;
680                 }
681
682                 mr = mr_find(sat->sat_ordering_oid);
683
684                 if( mr == NULL ) {
685                         *err = sat->sat_ordering_oid;
686                         code = SLAP_SCHERR_MR_NOT_FOUND;
687                         goto error_return;
688                 }
689
690                 if(( mr->smr_usage & SLAP_MR_ORDERING ) != SLAP_MR_ORDERING ) {
691                         *err = sat->sat_ordering_oid;
692                         code = SLAP_SCHERR_ATTR_BAD_MR;
693                         goto error_return;
694                 }
695
696                 if( sat->sat_syntax != mr->smr_syntax ) {
697                         if( mr->smr_compat_syntaxes == NULL ) {
698                                 *err = sat->sat_ordering_oid;
699                                 code = SLAP_SCHERR_ATTR_BAD_MR;
700                                 goto error_return;
701                         }
702
703                         for(i=0; mr->smr_compat_syntaxes[i]; i++) {
704                                 if( sat->sat_syntax == mr->smr_compat_syntaxes[i] ) {
705                                         i = -1;
706                                         break;
707                                 }
708                         }
709
710                         if( i >= 0 ) {
711                                 *err = sat->sat_ordering_oid;
712                                 code = SLAP_SCHERR_ATTR_BAD_MR;
713                                 goto error_return;
714                         }
715                 }
716
717                 sat->sat_ordering = mr;
718         }
719
720         if ( sat->sat_substr_oid ) {
721                 if( !sat->sat_equality ) {
722                         *err = sat->sat_substr_oid;
723                         code = SLAP_SCHERR_ATTR_BAD_MR;
724                         goto error_return;
725                 }
726
727                 mr = mr_find(sat->sat_substr_oid);
728
729                 if( mr == NULL ) {
730                         *err = sat->sat_substr_oid;
731                         code = SLAP_SCHERR_MR_NOT_FOUND;
732                         goto error_return;
733                 }
734
735                 if(( mr->smr_usage & SLAP_MR_SUBSTR ) != SLAP_MR_SUBSTR ) {
736                         *err = sat->sat_substr_oid;
737                         code = SLAP_SCHERR_ATTR_BAD_MR;
738                         goto error_return;
739                 }
740
741                 /* due to funky LDAP builtin substring rules,
742                  * we check against the equality rule assertion
743                  * syntax and compat syntaxes instead of those
744                  * associated with the substrings rule.
745                  */
746                 if( sat->sat_syntax != sat->sat_equality->smr_syntax ) {
747                         if( sat->sat_equality->smr_compat_syntaxes == NULL ) {
748                                 *err = sat->sat_substr_oid;
749                                 code = SLAP_SCHERR_ATTR_BAD_MR;
750                                 goto error_return;
751                         }
752
753                         for(i=0; sat->sat_equality->smr_compat_syntaxes[i]; i++) {
754                                 if( sat->sat_syntax ==
755                                         sat->sat_equality->smr_compat_syntaxes[i] )
756                                 {
757                                         i = -1;
758                                         break;
759                                 }
760                         }
761
762                         if( i >= 0 ) {
763                                 *err = sat->sat_substr_oid;
764                                 code = SLAP_SCHERR_ATTR_BAD_MR;
765                                 goto error_return;
766                         }
767                 }
768
769                 sat->sat_substr = mr;
770         }
771
772         code = at_insert( sat, err );
773         if ( code != 0 ) {
774 error_return:;
775                 if ( sat ) {
776                         ldap_pvt_thread_mutex_destroy( &sat->sat_ad_mutex );
777                         ch_free( sat );
778                 }
779
780                 if ( oidm ) {
781                         SLAP_FREE( at->at_oid );
782                         at->at_oid = oidm;
783                 }
784
785         } else if ( rsat ) {
786                 *rsat = sat;
787         }
788
789         return code;
790 }
791
792 #ifdef LDAP_DEBUG
793 #ifdef SLAPD_UNUSED
794 static int
795 at_index_printnode( void *v_air, void *ignore )
796 {
797         struct aindexrec *air = v_air;
798         printf("%s = %s\n",
799                 air->air_name.bv_val,
800                 ldap_attributetype2str(&air->air_at->sat_atype) );
801         return( 0 );
802 }
803
804 static void
805 at_index_print( void )
806 {
807         printf("Printing attribute type index:\n");
808         (void) avl_apply( attr_index, at_index_printnode, 0, -1, AVL_INORDER );
809 }
810 #endif
811 #endif
812
813 void
814 at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
815 {
816         AttributeType *at;
817         int i, num;
818         struct berval bv, *bva = NULL, idx;
819         char ibuf[32];
820
821         if ( !start )
822                 start = LDAP_STAILQ_FIRST( &attr_list );
823
824         /* count the result size */
825         i = 0;
826         for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
827                 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
828                 i++;
829                 if ( at == end ) break;
830         }
831         if (!i) return;
832
833         num = i;
834         bva = ch_malloc( (num+1) * sizeof(struct berval) );
835         BER_BVZERO( bva );
836         idx.bv_val = ibuf;
837         if ( sys ) {
838                 idx.bv_len = 0;
839                 ibuf[0] = '\0';
840         }
841         i = 0;
842         for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
843                 LDAPAttributeType lat, *latp;
844                 if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
845                 if ( at->sat_oidmacro ) {
846                         lat = at->sat_atype;
847                         lat.at_oid = at->sat_oidmacro;
848                         latp = &lat;
849                 } else {
850                         latp = &at->sat_atype;
851                 }
852                 if ( ldap_attributetype2bv( latp, &bv ) == NULL ) {
853                         ber_bvarray_free( bva );
854                 }
855                 if ( !sys ) {
856                         idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
857                 }
858                 bva[i].bv_len = idx.bv_len + bv.bv_len;
859                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
860                 strcpy( bva[i].bv_val, ibuf );
861                 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
862                 i++;
863                 bva[i].bv_val = NULL;
864                 ldap_memfree( bv.bv_val );
865                 if ( at == end ) break;
866         }
867         *res = bva;
868 }
869
870 int
871 at_schema_info( Entry *e )
872 {
873         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
874         AttributeType   *at;
875         struct berval   val;
876         struct berval   nval;
877
878         LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
879                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
880
881                 if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
882                         return -1;
883                 }
884
885                 ber_str2bv( at->sat_oid, 0, 0, &nval );
886
887                 if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
888                 {
889                         return -1;
890                 }
891                 ldap_memfree( val.bv_val );
892         }
893         return 0;
894 }