]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
219b66b9b17820d73bb13ef84df09c0e19fd8e42
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ad.c - routines for dealing with attribute descriptions */
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 static int ad_keystring(
22         struct berval *bv )
23 {
24         ber_len_t i;
25
26         if( !AD_CHAR( bv->bv_val[0] ) ) {
27                 return 1;
28         }
29
30         for( i=1; i<bv->bv_len; i++ ) {
31                 if( !AD_CHAR( bv->bv_val[i] ) ) {
32                         return 1;
33                 }
34         }
35         return 0;
36 }
37
38 void ad_destroy( void *in )
39 {
40         AttributeDescription *ad = in, *n;
41
42         for (;ad;ad = n) {
43                 n = ad->ad_next;
44                 ldap_memfree(ad);
45         }
46 }
47
48 /* Is there an AttributeDescription for this type that uses this language? */
49 AttributeDescription * ad_find_lang(
50         AttributeType *type,
51         struct berval *lang )
52 {
53         AttributeDescription *ad;
54
55         ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
56         for (ad = type->sat_ad; ad; ad=ad->ad_next)
57         {
58                 if (ad->ad_lang.bv_len == lang->bv_len &&
59                         !strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
60                         break;
61         }
62         ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
63         return ad;
64 }
65
66 int slap_str2ad(
67         const char *str,
68         AttributeDescription **ad,
69         const char **text )
70 {
71         struct berval bv;
72         bv.bv_val = (char *) str;
73         bv.bv_len = strlen( str );
74
75         return slap_bv2ad( &bv, ad, text );
76 }
77
78 int slap_bv2ad(
79         struct berval *bv,
80         AttributeDescription **ad,
81         const char **text )
82 {
83         int rtn = LDAP_UNDEFINED_TYPE;
84         int i;
85         AttributeDescription desc, *d2;
86         char *name, *options;
87
88         assert( ad != NULL );
89         assert( *ad == NULL ); /* temporary */
90
91         if( bv == NULL || bv->bv_len == 0 ) {
92                 *text = "empty attribute description";
93                 return rtn;
94         }
95
96         /* make sure description is IA5 */
97         if( ad_keystring( bv ) ) {
98                 *text = "attribute description contains inappropriate characters";
99                 return rtn;
100         }
101
102         /* find valid base attribute type; parse in place */
103         desc.ad_cname = *bv;
104         name = bv->bv_val;
105         options = strchr(name, ';');
106         if (options != NULL)
107                 desc.ad_cname.bv_len = options - name;
108         desc.ad_type = at_bvfind( &desc.ad_cname );
109         if( desc.ad_type == NULL ) {
110                 *text = "attribute type undefined";
111                 return rtn;
112         }
113
114         desc.ad_flags = SLAP_DESC_NONE;
115         desc.ad_lang.bv_len = 0;
116         desc.ad_lang.bv_val = NULL;
117
118         /* parse options in place */
119         for( ; options != NULL; ) {
120                 name = options+1;
121                 options = strchr( name, ';' );
122                 if ( options != NULL )
123                         i = options - name;
124                 else
125                         i = bv->bv_len - (name - bv->bv_val);
126
127                 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
128                         if( slap_ad_is_binary( &desc ) ) {
129                                 *text = "option \"binary\" specified multiple times";
130                                 goto done;
131                         }
132
133                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
134                                 /* not stored in binary, disallow option */
135                                 *text = "option \"binary\" with type not supported";
136                                 goto done;
137                         }
138
139                         desc.ad_flags |= SLAP_DESC_BINARY;
140
141                 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
142                         sizeof("lang-")-1 ) == 0)
143                 {
144                         if( desc.ad_lang.bv_len != 0 ) {
145                                 *text = "multiple language tag options specified";
146                                 goto done;
147                         }
148
149                         desc.ad_lang.bv_val = name;
150                         desc.ad_lang.bv_len = i;
151                 } else {
152                         *text = "unrecognized option";
153                         goto done;
154                 }
155         }
156
157         ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
158         /* see if a matching description is already cached */
159         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
160                 if (d2->ad_flags != desc.ad_flags)
161                         continue;
162                 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
163                         continue;
164                 if (d2->ad_lang.bv_len == 0)
165                         break;
166                 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
167                         desc.ad_lang.bv_len) == 0)
168                         break;
169         }
170
171         /* Not found, add new one */
172         while (d2 == NULL) {
173                 int dlen = 0;
174
175                 /* Allocate a single contiguous block. If there are no
176                  * options, we just need space for the AttrDesc structure.
177                  * Otherwise, we need to tack on the full name length +
178                  * options length.
179                  */
180                 i = sizeof(AttributeDescription);
181                 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
182                         if (desc.ad_lang.bv_len)
183                                 dlen = desc.ad_lang.bv_len+1;
184                         dlen += desc.ad_type->sat_cname.bv_len+1;
185                         if( slap_ad_is_binary( &desc ) ) {
186                                 dlen += sizeof("binary");
187                         }
188                 }
189
190                 d2 = ch_malloc(i + dlen);
191                 d2->ad_type = desc.ad_type;
192                 d2->ad_flags = desc.ad_flags;
193                 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
194                 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
195                 if (dlen == 0) {
196                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
197                         d2->ad_lang.bv_val = NULL;
198                 } else {
199                         d2->ad_cname.bv_val = (char *)(d2+1);
200                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
201                         if( slap_ad_is_binary( &desc ) ) {
202                                 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
203                                         ";binary");
204                                 d2->ad_cname.bv_len += sizeof("binary");
205                         }
206                         if( d2->ad_lang.bv_len ) {
207                                 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
208                                 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
209                                         d2->ad_cname.bv_len;
210                                 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
211                                         d2->ad_lang.bv_len);
212                                 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
213                                 ldap_pvt_str2lower(d2->ad_lang.bv_val);
214                                 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
215                         }
216                 }
217                 /* Add new desc to list. We always want the bare Desc with
218                  * no options to stay at the head of the list, assuming
219                  * that one will be used most frequently.
220                  */
221                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
222                         d2->ad_next = desc.ad_type->sat_ad;
223                         desc.ad_type->sat_ad = d2;
224                 } else {
225                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
226                         desc.ad_type->sat_ad->ad_next = d2;
227                 }
228         }
229         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
230
231         if( *ad == NULL ) {
232                 *ad = d2;
233         } else {
234                 **ad = *d2;
235         }
236
237         rtn = LDAP_SUCCESS;
238
239 done:
240         return rtn;
241 }
242
243 int is_ad_subtype(
244         AttributeDescription *sub,
245         AttributeDescription *super
246 )
247 {
248         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
249                 return 0;
250         }
251
252         if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
253                 return 0;
254         }
255
256         if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
257                 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
258                 sub->ad_lang.bv_val)))
259         {
260                 return 0;
261         }
262
263         return 1;
264 }
265
266
267 int ad_inlist(
268         AttributeDescription *desc,
269         struct berval **attrs )
270 {
271         int i;
272         for( i=0; attrs[i] != NULL; i++ ) {
273                 ObjectClass *oc;
274                 AttributeDescription *ad = NULL;
275                 const char *text;
276                 int rc;
277                 
278                 rc = slap_bv2ad( attrs[i], &ad, &text );
279                 if( rc == LDAP_SUCCESS ) {
280                         rc = is_ad_subtype( desc, ad );
281                         if( rc ) return 1;
282                         continue;
283                 }
284
285                 /*
286                  * EXTENSION: see if requested description is an object class
287                  * if so, return attributes which the class requires/allows
288                  */
289                 oc = oc_bvfind( attrs[i] );
290                 if( oc != NULL ) {
291                         if ( oc == slap_schema.si_oc_extensibleObject ) {
292                                 /* extensibleObject allows the return of anything */
293                                 return 1;
294                         }
295
296                         if( oc->soc_required ) {
297                                 /* allow return of required attributes */
298                                 int i;
299                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
300                                         rc = is_at_subtype( desc->ad_type,
301                                                 oc->soc_allowed[i] );
302                                         if( rc ) return 1;
303                                 }
304                         }
305                         if( oc->soc_allowed ) {
306                                 /* allow return of allowed attributes */
307                                 int i;
308                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
309                                         rc = is_at_subtype( desc->ad_type,
310                                                 oc->soc_allowed[i] );
311                                         if( rc ) return 1;
312                                 }
313                         }
314                 }
315         }
316
317         return 0;
318 }
319
320
321 int slap_str2undef_ad(
322         const char *str,
323         AttributeDescription **ad,
324         const char **text )
325 {
326         struct berval bv;
327         bv.bv_val = (char *) str;
328         bv.bv_len = strlen( str );
329
330         return slap_bv2undef_ad( &bv, ad, text );
331 }
332
333 int slap_bv2undef_ad(
334         struct berval *bv,
335         AttributeDescription **ad,
336         const char **text )
337 {
338         AttributeDescription *desc;
339
340         assert( ad != NULL );
341
342         if( bv == NULL || bv->bv_len == 0 ) {
343                 *text = "empty attribute description";
344                 return LDAP_UNDEFINED_TYPE;
345         }
346
347         /* make sure description is IA5 */
348         if( ad_keystring( bv ) ) {
349                 *text = "attribute description contains inappropriate characters";
350                 return LDAP_UNDEFINED_TYPE;
351         }
352
353         for (desc = slap_schema.si_at_undefined->sat_ad; desc;
354                 desc=desc->ad_next)
355                 if (desc->ad_cname.bv_len == bv->bv_len &&
356                     !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
357                         break;
358         
359         if (!desc) {
360                 desc = ch_malloc(sizeof(AttributeDescription) +
361                         bv->bv_len + 1);
362                 
363                 desc->ad_flags = SLAP_DESC_NONE;
364                 desc->ad_lang.bv_val = NULL;
365                 desc->ad_lang.bv_len = 0;
366
367                 desc->ad_cname.bv_len = bv->bv_len;
368                 desc->ad_cname.bv_val = (char *)(desc+1);
369                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
370
371                 /* canonical to upper case */
372                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
373
374                 desc->ad_type = slap_schema.si_at_undefined;
375                 desc->ad_next = desc->ad_type->sat_ad;
376                 desc->ad_type->sat_ad = desc;
377         }
378
379         if (!*ad)
380                 *ad = desc;
381         else
382                 **ad = *desc;
383
384         return LDAP_SUCCESS;
385 }