]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
Changed search attrs to struct berval **.
[openldap] / servers / slapd / at.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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         char            *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         return (strcasecmp( air1->air_name, air2->air_name ));
61 }
62
63 static int
64 attr_index_name_cmp(
65     const char          *type,
66     struct aindexrec    *air
67 )
68 {
69         return (strcasecmp( type, air->air_name ));
70 }
71
72 /* Uses strncasecmp to allow the input type to be non-terminated */
73 static int
74 attr_index_bvname_cmp(
75     struct berval       *type,
76     struct aindexrec    *air
77 )
78 {
79         int rc = strncasecmp( type->bv_val, air->air_name, type->bv_len );
80         if (rc)
81                 return rc;
82         return air->air_name[type->bv_len] ? -1 : 0;
83 }
84
85 AttributeType *
86 at_find(
87     const char          *name
88 )
89 {
90         struct aindexrec *air;
91
92         air = (struct aindexrec *) avl_find( attr_index, name,
93             (AVL_CMP) attr_index_name_cmp );
94
95         return air != NULL ? air->air_at : NULL;
96 }
97
98 AttributeType *
99 at_bvfind(
100     struct berval       *name
101 )
102 {
103         struct aindexrec *air;
104
105         air = (struct aindexrec *) avl_find( attr_index, name,
106             (AVL_CMP) attr_index_bvname_cmp );
107
108         return air != NULL ? air->air_at : NULL;
109 }
110
111 int
112 at_append_to_list(
113     AttributeType       *sat,
114     AttributeType       ***listp
115 )
116 {
117         AttributeType   **list;
118         AttributeType   **list1;
119         int             size;
120
121         list = *listp;
122         if ( !list ) {
123                 size = 2;
124                 list = ch_calloc(size, sizeof(AttributeType *));
125                 if ( !list ) {
126                         return -1;
127                 }
128         } else {
129                 size = 0;
130                 list1 = *listp;
131                 while ( *list1 ) {
132                         size++;
133                         list1++;
134                 }
135                 size += 2;
136                 list1 = ch_realloc(list, size*sizeof(AttributeType *));
137                 if ( !list1 ) {
138                         return -1;
139                 }
140                 list = list1;
141         }
142         list[size-2] = sat;
143         list[size-1] = NULL;
144         *listp = list;
145         return 0;
146 }
147
148 int
149 at_delete_from_list(
150     int                 pos,
151     AttributeType       ***listp
152 )
153 {
154         AttributeType   **list;
155         AttributeType   **list1;
156         int             i;
157         int             j;
158
159         if ( pos < 0 ) {
160                 return -2;
161         }
162         list = *listp;
163         for ( i=0; list[i]; i++ )
164                 ;
165         if ( pos >= i ) {
166                 return -2;
167         }
168         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
169                 list[i] = list[j];
170         }
171         list[i] = NULL;
172         /* Tell the runtime this can be shrinked */
173         list1 = ch_realloc(list, (i+1)*sizeof(AttributeType **));
174         if ( !list1 ) {
175                 return -1;
176         }
177         *listp = list1;
178         return 0;
179 }
180
181 int
182 at_find_in_list(
183     AttributeType       *sat,
184     AttributeType       **list
185 )
186 {
187         int     i;
188
189         if ( !list ) {
190                 return -1;
191         }
192         for ( i=0; list[i]; i++ ) {
193                 if ( sat == list[i] ) {
194                         return i;
195                 }
196         }
197         return -1;
198 }
199
200 void
201 at_destroy( void )
202 {
203         AttributeType *a, *n;
204         avl_free(attr_index, ldap_memfree);
205
206         for (a=attr_list; a; a=n) {
207                 n = a->sat_next;
208                 ldap_memfree(a->sat_subtypes);
209                 ad_destroy(a->sat_ad);
210                 ldap_pvt_thread_mutex_destroy(&a->sat_ad_mutex);
211                 ldap_attributetype_free((LDAPAttributeType *)a);
212         }
213 }
214
215 static int
216 at_insert(
217     AttributeType       *sat,
218     const char          **err
219 )
220 {
221         AttributeType           **atp;
222         struct aindexrec        *air;
223         char                    **names;
224
225         atp = &attr_list;
226         while ( *atp != NULL ) {
227                 atp = &(*atp)->sat_next;
228         }
229         *atp = sat;
230
231         if ( sat->sat_oid ) {
232                 air = (struct aindexrec *)
233                         ch_calloc( 1, sizeof(struct aindexrec) );
234                 air->air_name = sat->sat_oid;
235                 air->air_at = sat;
236                 if ( avl_insert( &attr_index, (caddr_t) air,
237                                  (AVL_CMP) attr_index_cmp,
238                                  (AVL_DUP) avl_dup_error ) ) {
239                         *err = sat->sat_oid;
240                         ldap_memfree(air);
241                         return SLAP_SCHERR_DUP_ATTR;
242                 }
243                 /* FIX: temporal consistency check */
244                 at_find(air->air_name);
245         }
246
247         if ( (names = sat->sat_names) ) {
248                 while ( *names ) {
249                         air = (struct aindexrec *)
250                                 ch_calloc( 1, sizeof(struct aindexrec) );
251                         air->air_name = *names;
252                         air->air_at = sat;
253                         if ( avl_insert( &attr_index, (caddr_t) air,
254                                          (AVL_CMP) attr_index_cmp,
255                                          (AVL_DUP) avl_dup_error ) ) {
256                                 *err = *names;
257                                 ldap_memfree(air);
258                                 return SLAP_SCHERR_DUP_ATTR;
259                         }
260                         /* FIX: temporal consistency check */
261                         at_find(air->air_name);
262                         names++;
263                 }
264         }
265
266         return 0;
267 }
268
269 int
270 at_add(
271     LDAPAttributeType   *at,
272     const char          **err
273 )
274 {
275         AttributeType   *sat;
276         MatchingRule    *mr;
277         Syntax          *syn;
278         int             code;
279         char                    *cname;
280
281         if ( at->at_names && at->at_names[0] ) {
282                 int i;
283
284                 for( i=0; at->at_names[i]; i++ ) {
285                         if( !slap_valid_descr( at->at_names[i] ) ) {
286                                 return SLAP_SCHERR_BAD_DESCR;
287                         }
288                 }
289
290                 cname = at->at_names[0];
291
292         } else if ( at->at_oid ) {
293                 cname = at->at_oid;
294         } else {
295                 return SLAP_SCHERR_ATTR_INCOMPLETE;
296         }
297
298         if ( at->at_collective ) {
299                 return SLAP_SCHERR_NOT_SUPPORTED;
300         }
301
302         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
303         AC_MEMCPY( &sat->sat_atype, at, sizeof(LDAPAttributeType));
304
305         sat->sat_cname.bv_val = cname;
306         sat->sat_cname.bv_len = strlen( cname );
307         ldap_pvt_thread_mutex_init(&sat->sat_ad_mutex);
308
309         if ( at->at_sup_oid ) {
310                 AttributeType *supsat = at_find(at->at_sup_oid);
311
312                 if ( (supsat == NULL ) ) {
313                         *err = at->at_sup_oid;
314                         return SLAP_SCHERR_ATTR_NOT_FOUND;
315                 }
316
317                 sat->sat_sup = supsat;
318
319                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
320                         *err = cname;
321                         return SLAP_SCHERR_OUTOFMEM;
322                 }
323         }
324
325         /*
326          * Inherit definitions from superiors.  We only check the
327          * direct superior since that one has already inherited from
328          * its own superiorss
329          */
330         if ( sat->sat_sup ) {
331                 sat->sat_syntax = sat->sat_sup->sat_syntax;
332                 sat->sat_equality = sat->sat_sup->sat_equality;
333                 sat->sat_approx = sat->sat_sup->sat_approx;
334                 sat->sat_ordering = sat->sat_sup->sat_ordering;
335                 sat->sat_substr = sat->sat_sup->sat_substr;
336         }
337
338         if ( at->at_syntax_oid ) {
339                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
340                         sat->sat_syntax = syn;
341                 } else {
342                         *err = sat->sat_syntax_oid;
343                         return SLAP_SCHERR_SYN_NOT_FOUND;
344                 }
345
346
347         } else if ( sat->sat_syntax == NULL ) {
348                 return SLAP_SCHERR_ATTR_INCOMPLETE;
349         }
350
351         if ( sat->sat_equality_oid ) {
352                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
353                         sat->sat_equality = mr;
354                         sat->sat_approx = mr->smr_associated;
355                 } else {
356                         *err = sat->sat_equality_oid;
357                         return SLAP_SCHERR_MR_NOT_FOUND;
358                 }
359
360         }
361
362         if ( sat->sat_ordering_oid ) {
363                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
364                         sat->sat_ordering = mr;
365                 } else {
366                         *err = sat->sat_ordering_oid;
367                         return SLAP_SCHERR_MR_NOT_FOUND;
368                 }
369         }
370
371         if ( sat->sat_substr_oid ) {
372                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
373                         sat->sat_substr = mr;
374                 } else {
375                         *err = sat->sat_substr_oid;
376                         return SLAP_SCHERR_MR_NOT_FOUND;
377                 }
378         }
379
380         code = at_insert(sat,err);
381         return code;
382 }
383
384 #ifdef LDAP_DEBUG
385 static int
386 at_index_printnode( struct aindexrec *air )
387 {
388
389         printf("%s = %s\n",
390                 air->air_name,
391                 ldap_attributetype2str(&air->air_at->sat_atype) );
392         return( 0 );
393 }
394
395 static void
396 at_index_print( void )
397 {
398         printf("Printing attribute type index:\n");
399         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
400                 0, -1, AVL_INORDER );
401 }
402 #endif
403
404 #if defined( SLAPD_SCHEMA_DN )
405 int
406 at_schema_info( Entry *e )
407 {
408         struct berval   val;
409         struct berval   *vals[2];
410         AttributeType   *at;
411
412         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
413
414         vals[0] = &val;
415         vals[1] = NULL;
416
417         for ( at = attr_list; at; at = at->sat_next ) {
418                 val.bv_val = ldap_attributetype2str( &at->sat_atype );
419                 if ( val.bv_val == NULL ) {
420                         return -1;
421                 }
422                 val.bv_len = strlen( val.bv_val );
423 #if 0
424                 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
425                        (long) val.bv_len, val.bv_val, 0 );
426 #endif
427                 attr_merge( e, ad_attributeTypes, vals );
428                 ldap_memfree( val.bv_val );
429         }
430         return 0;
431 }
432 #endif