]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
Memory alloc issues
[openldap] / servers / slapd / at.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* at.c - routines for dealing with attribute types */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19 #include "slap.h"
20
21
22 int is_at_syntax(
23         AttributeType *at,
24         const char *oid )
25 {
26         for( ; at != NULL; at = at->sat_sup ) {
27                 if( at->sat_syntax_oid ) {
28                         return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
29                 }
30         }
31
32         return 0;
33 }
34
35 int is_at_subtype(
36         AttributeType *sub,
37         AttributeType *sup )
38 {
39         for( ; sub != NULL; sub = sub->sat_sup ) {
40                 if( sub == sup ) return 1;
41         }
42
43         return 0;
44 }
45
46 struct aindexrec {
47         struct berval   air_name;
48         AttributeType   *air_at;
49 };
50
51 static Avlnode  *attr_index = NULL;
52 static AttributeType *attr_list = NULL;
53
54 static int
55 attr_index_cmp(
56     struct aindexrec    *air1,
57     struct aindexrec    *air2
58 )
59 {
60         int i = air1->air_name.bv_len - air2->air_name.bv_len;
61         if (i)
62                 return i;
63         return (strcasecmp( air1->air_name.bv_val, air2->air_name.bv_val ));
64 }
65
66 static int
67 attr_index_name_cmp(
68     struct berval       *type,
69     struct aindexrec    *air
70 )
71 {
72         int i = type->bv_len - air->air_name.bv_len;
73         if (i)
74                 return i;
75         return (strncasecmp( type->bv_val, air->air_name.bv_val,
76                 type->bv_len ));
77 }
78
79 AttributeType *
80 at_find(
81     const char          *name
82 )
83 {
84         struct berval bv;
85
86         bv.bv_val = (char *)name;
87         bv.bv_len = strlen( name );
88
89         return at_bvfind( &bv );
90 }
91
92 AttributeType *
93 at_bvfind(
94     struct berval       *name
95 )
96 {
97         struct aindexrec *air;
98
99         air = (struct aindexrec *) avl_find( attr_index, name,
100             (AVL_CMP) attr_index_name_cmp );
101
102         return air != NULL ? air->air_at : NULL;
103 }
104
105 int
106 at_append_to_list(
107     AttributeType       *sat,
108     AttributeType       ***listp
109 )
110 {
111         AttributeType   **list;
112         AttributeType   **list1;
113         int             size;
114
115         list = *listp;
116         if ( !list ) {
117                 size = 2;
118                 list = ch_calloc(size, sizeof(AttributeType *));
119                 if ( !list ) {
120                         return -1;
121                 }
122         } else {
123                 size = 0;
124                 list1 = *listp;
125                 while ( *list1 ) {
126                         size++;
127                         list1++;
128                 }
129                 size += 2;
130                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
131                 if ( !list1 ) {
132                         return -1;
133                 }
134                 list = list1;
135         }
136         list[size-2] = sat;
137         list[size-1] = NULL;
138         *listp = list;
139         return 0;
140 }
141
142 int
143 at_delete_from_list(
144     int                 pos,
145     AttributeType       ***listp
146 )
147 {
148         AttributeType   **list;
149         AttributeType   **list1;
150         int             i;
151         int             j;
152
153         if ( pos < 0 ) {
154                 return -2;
155         }
156         list = *listp;
157         for ( i=0; list[i]; i++ )
158                 ;
159         if ( pos >= i ) {
160                 return -2;
161         }
162         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
163                 list[i] = list[j];
164         }
165         list[i] = NULL;
166         /* Tell the runtime this can be shrinked */
167         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
168         if ( !list1 ) {
169                 return -1;
170         }
171         *listp = list1;
172         return 0;
173 }
174
175 int
176 at_find_in_list(
177     AttributeType       *sat,
178     AttributeType       **list
179 )
180 {
181         int     i;
182
183         if ( !list ) {
184                 return -1;
185         }
186         for ( i=0; list[i]; i++ ) {
187                 if ( sat == list[i] ) {
188                         return i;
189                 }
190         }
191         return -1;
192 }
193
194 void
195 at_destroy( void )
196 {
197         AttributeType *a, *n;
198         avl_free(attr_index, ldap_memfree);
199
200         for (a=attr_list; a; a=n) {
201                 n = a->sat_next;
202                 if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
203                 ad_destroy(a->sat_ad);
204                 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
205                 ldap_attributetype_free((LDAPAttributeType *)a);
206         }
207         if ( slap_schema.si_at_undefined )
208                 ad_destroy(slap_schema.si_at_undefined->sat_ad);
209 }
210
211 static int
212 at_insert(
213     AttributeType       *sat,
214     const char          **err
215 )
216 {
217         AttributeType           **atp;
218         struct aindexrec        *air;
219         char                    **names;
220
221         atp = &attr_list;
222         while ( *atp != NULL ) {
223                 atp = &(*atp)->sat_next;
224         }
225         *atp = sat;
226
227         if ( sat->sat_oid ) {
228                 air = (struct aindexrec *)
229                         ch_calloc( 1, sizeof(struct aindexrec) );
230                 air->air_name.bv_val = sat->sat_oid;
231                 air->air_name.bv_len = strlen(sat->sat_oid);
232                 air->air_at = sat;
233                 if ( avl_insert( &attr_index, (caddr_t) air,
234                                  (AVL_CMP) attr_index_cmp,
235                                  (AVL_DUP) avl_dup_error ) ) {
236                         *err = sat->sat_oid;
237                         ldap_memfree(air);
238                         return SLAP_SCHERR_ATTR_DUP;
239                 }
240                 /* FIX: temporal consistency check */
241                 at_bvfind(&air->air_name);
242         }
243
244         if ( (names = sat->sat_names) ) {
245                 while ( *names ) {
246                         air = (struct aindexrec *)
247                                 ch_calloc( 1, sizeof(struct aindexrec) );
248                         air->air_name.bv_val = *names;
249                         air->air_name.bv_len = strlen(*names);
250                         air->air_at = sat;
251                         if ( avl_insert( &attr_index, (caddr_t) air,
252                                          (AVL_CMP) attr_index_cmp,
253                                          (AVL_DUP) avl_dup_error ) ) {
254                                 *err = *names;
255                                 ldap_memfree(air);
256                                 return SLAP_SCHERR_ATTR_DUP;
257                         }
258                         /* FIX: temporal consistency check */
259                         at_bvfind(&air->air_name);
260                         names++;
261                 }
262         }
263
264         return 0;
265 }
266
267 int
268 at_add(
269     LDAPAttributeType   *at,
270     const char          **err
271 )
272 {
273         AttributeType   *sat;
274         MatchingRule    *mr;
275         Syntax          *syn;
276         int             code;
277         char    *cname;
278         char    *oid;
279
280         if ( !OID_LEADCHAR( at->at_oid[0] )) {
281                 /* Expand OID macros */
282                 oid = oidm_find( at->at_oid );
283                 if ( !oid ) {
284                         *err = at->at_oid;
285                         return SLAP_SCHERR_OIDM;
286                 }
287                 if ( oid != at->at_oid ) {
288                         ldap_memfree( at->at_oid );
289                         at->at_oid = oid;
290                 }
291         }
292
293         if ( at->at_syntax_oid && !OID_LEADCHAR( at->at_syntax_oid[0] )) {
294                 /* Expand OID macros */
295                 oid = oidm_find( at->at_syntax_oid );
296                 if ( !oid ) {
297                         *err = at->at_syntax_oid;
298                         return SLAP_SCHERR_OIDM;
299                 }
300                 if ( oid != at->at_syntax_oid ) {
301                         ldap_memfree( at->at_syntax_oid );
302                         at->at_syntax_oid = oid;
303                 }
304
305         }
306
307         if ( at->at_names && at->at_names[0] ) {
308                 int i;
309
310                 for( i=0; at->at_names[i]; i++ ) {
311                         if( !slap_valid_descr( at->at_names[i] ) ) {
312                                 *err = at->at_names[i];
313                                 return SLAP_SCHERR_BAD_DESCR;
314                         }
315                 }
316
317                 cname = at->at_names[0];
318
319         } else if ( at->at_oid ) {
320                 cname = at->at_oid;
321
322         } else {
323                 *err = "";
324                 return SLAP_SCHERR_ATTR_INCOMPLETE;
325         }
326
327         *err = cname;
328
329         if ( !at->at_usage && at->at_no_user_mod ) {
330                 /* user attribute must be modifable */
331                 return SLAP_SCHERR_ATTR_BAD_USAGE;
332         }
333
334         if ( at->at_collective ) {
335                 if( at->at_usage ) {
336                         /* collective attributes cannot be operational */
337                         return SLAP_SCHERR_ATTR_BAD_USAGE;
338                 }
339
340                 if( at->at_single_value ) {
341                         /* collective attributes cannot be single-valued */
342                         return SLAP_SCHERR_ATTR_BAD_USAGE;
343                 }
344
345                 /* collective attributes not supported */
346                 return SLAP_SCHERR_NOT_SUPPORTED;
347         }
348
349         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
350         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
351
352         sat->sat_cname.bv_val = cname;
353         sat->sat_cname.bv_len = strlen( cname );
354         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
355
356         if ( at->at_sup_oid ) {
357                 AttributeType *supsat = at_find(at->at_sup_oid);
358
359                 if ( (supsat == NULL ) ) {
360                         *err = at->at_sup_oid;
361                         return SLAP_SCHERR_ATTR_NOT_FOUND;
362                 }
363
364                 sat->sat_sup = supsat;
365
366                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
367                         return SLAP_SCHERR_OUTOFMEM;
368                 }
369
370                 if ( sat->sat_usage != supsat->sat_usage ) {
371                         /* subtypes must have same usage as their SUP */
372                         return SLAP_SCHERR_ATTR_BAD_USAGE;
373                 }
374
375                 if ( sat->sat_flags & SLAP_AT_FINAL ) {
376                         /* cannot subtype a "final" attribute type */
377                         return SLAP_SCHERR_ATTR_BAD_SUP;
378                 }
379         }
380
381         /*
382          * Inherit definitions from superiors.  We only check the
383          * direct superior since that one has already inherited from
384          * its own superiorss
385          */
386         if ( sat->sat_sup ) {
387                 sat->sat_syntax = sat->sat_sup->sat_syntax;
388                 sat->sat_equality = sat->sat_sup->sat_equality;
389                 sat->sat_approx = sat->sat_sup->sat_approx;
390                 sat->sat_ordering = sat->sat_sup->sat_ordering;
391                 sat->sat_substr = sat->sat_sup->sat_substr;
392         }
393
394         if ( at->at_syntax_oid ) {
395                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
396                         sat->sat_syntax = syn;
397                 } else {
398                         *err = sat->sat_syntax_oid;
399                         return SLAP_SCHERR_SYN_NOT_FOUND;
400                 }
401
402
403         } else if ( sat->sat_syntax == NULL ) {
404                 return SLAP_SCHERR_ATTR_INCOMPLETE;
405         }
406
407         if ( sat->sat_equality_oid ) {
408                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
409                         sat->sat_equality = mr;
410                         sat->sat_approx = mr->smr_associated;
411                 } else {
412                         *err = sat->sat_equality_oid;
413                         return SLAP_SCHERR_MR_NOT_FOUND;
414                 }
415
416         }
417
418         if ( sat->sat_ordering_oid ) {
419                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
420                         sat->sat_ordering = mr;
421                 } else {
422                         *err = sat->sat_ordering_oid;
423                         return SLAP_SCHERR_MR_NOT_FOUND;
424                 }
425         }
426
427         if ( sat->sat_substr_oid ) {
428                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
429                         sat->sat_substr = mr;
430                 } else {
431                         *err = sat->sat_substr_oid;
432                         return SLAP_SCHERR_MR_NOT_FOUND;
433                 }
434         }
435
436         code = at_insert(sat,err);
437         return code;
438 }
439
440 #ifdef LDAP_DEBUG
441 static int
442 at_index_printnode( struct aindexrec *air )
443 {
444
445         printf("%s = %s\n",
446                 air->air_name.bv_val,
447                 ldap_attributetype2str(&air->air_at->sat_atype) );
448         return( 0 );
449 }
450
451 static void
452 at_index_print( void )
453 {
454         printf("Printing attribute type index:\n");
455         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
456                 0, -1, AVL_INORDER );
457 }
458 #endif
459
460 #if defined( SLAPD_SCHEMA_DN )
461 int
462 at_schema_info( Entry *e )
463 {
464         struct berval   vals[2];
465         AttributeType   *at;
466
467         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
468
469         vals[1].bv_val = NULL;
470
471         for ( at = attr_list; at; at = at->sat_next ) {
472                 if ( ldap_attributetype2bv( &at->sat_atype, vals ) == NULL ) {
473                         return -1;
474                 }
475
476                 if( at->sat_flags & SLAP_AT_HIDE ) continue;
477
478 #if 0
479                 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
480                        (long) vals[0].bv_len, vals[0].bv_val, 0 );
481 #endif
482                 attr_merge( e, ad_attributeTypes, vals );
483                 ldap_memfree( vals[0].bv_val );
484         }
485         return 0;
486 }
487 #endif