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