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