]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Update copyright statements
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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         /* see if a matching description is already cached */
158         for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
159                 if (d2->ad_flags != desc.ad_flags)
160                         continue;
161                 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
162                         continue;
163                 if (d2->ad_lang.bv_len == 0)
164                         break;
165                 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
166                         desc.ad_lang.bv_len) == 0)
167                         break;
168         }
169
170         /* Not found, add new one */
171         while (d2 == NULL) {
172                 int dlen = 0;
173                 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
174                 /* check again now that we've locked */
175                 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
176                         if (d2->ad_flags != desc.ad_flags)
177                                 continue;
178                         if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
179                                 continue;
180                         if (d2->ad_lang.bv_len == 0)
181                                 break;
182                         if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
183                                 desc.ad_lang.bv_len) == 0)
184                                 break;
185                 }
186                 if (d2) {
187                         ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
188                         break;
189                 }
190
191                 /* Allocate a single contiguous block. If there are no
192                  * options, we just need space for the AttrDesc structure.
193                  * Otherwise, we need to tack on the full name length +
194                  * options length.
195                  */
196                 i = sizeof(AttributeDescription);
197                 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
198                         if (desc.ad_lang.bv_len)
199                                 dlen = desc.ad_lang.bv_len+1;
200                         dlen += desc.ad_type->sat_cname.bv_len+1;
201                         if( slap_ad_is_binary( &desc ) ) {
202                                 dlen += sizeof("binary");
203                         }
204                 }
205
206                 d2 = ch_malloc(i + dlen);
207                 d2->ad_type = desc.ad_type;
208                 d2->ad_flags = desc.ad_flags;
209                 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
210                 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
211                 if (dlen == 0) {
212                         d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
213                         d2->ad_lang.bv_val = NULL;
214                 } else {
215                         d2->ad_cname.bv_val = (char *)(d2+1);
216                         strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
217                         if( slap_ad_is_binary( &desc ) ) {
218                                 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
219                                         ";binary");
220                                 d2->ad_cname.bv_len += sizeof("binary");
221                         }
222                         if( d2->ad_lang.bv_len ) {
223                                 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
224                                 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
225                                         d2->ad_cname.bv_len;
226                                 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
227                                         d2->ad_lang.bv_len);
228                                 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
229                                 ldap_pvt_str2lower(d2->ad_lang.bv_val);
230                                 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
231                         }
232                 }
233                 /* Add new desc to list. We always want the bare Desc with
234                  * no options to stay at the head of the list, assuming
235                  * that one will be used most frequently.
236                  */
237                 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
238                         d2->ad_next = desc.ad_type->sat_ad;
239                         desc.ad_type->sat_ad = d2;
240                 } else {
241                         d2->ad_next = desc.ad_type->sat_ad->ad_next;
242                         desc.ad_type->sat_ad->ad_next = d2;
243                 }
244                 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
245         }
246
247         if( *ad == NULL ) {
248                 *ad = d2;
249         } else {
250                 **ad = *d2;
251         }
252
253         rtn = LDAP_SUCCESS;
254
255 done:
256         return rtn;
257 }
258
259 int is_ad_subtype(
260         AttributeDescription *sub,
261         AttributeDescription *super
262 )
263 {
264         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
265                 return 0;
266         }
267
268         if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
269                 return 0;
270         }
271
272         if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
273                 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
274                 sub->ad_lang.bv_val)))
275         {
276                 return 0;
277         }
278
279         return 1;
280 }
281
282
283 int ad_inlist(
284         AttributeDescription *desc,
285         AttributeName *attrs )
286 {
287         if (! attrs ) return 0;
288
289         for( ; attrs->an_name.bv_val; attrs++ ) {
290                 ObjectClass *oc;
291                 int rc;
292                 
293                 if ( attrs->an_desc ) {
294                         if ( is_ad_subtype( desc, attrs->an_desc ))
295                                 return 1;
296                         continue;
297                 }
298
299
300                 /*
301                  * EXTENSION: see if requested description is an object class
302                  * if so, return attributes which the class requires/allows
303                  */
304                 oc = oc_bvfind( &attrs->an_name );
305                 if( oc != NULL ) {
306                         if ( oc == slap_schema.si_oc_extensibleObject ) {
307                                 /* extensibleObject allows the return of anything */
308                                 return 1;
309                         }
310
311                         if( oc->soc_required ) {
312                                 /* allow return of required attributes */
313                                 int i;
314                                 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
315                                         rc = is_at_subtype( desc->ad_type,
316                                                 oc->soc_allowed[i] );
317                                         if( rc ) return 1;
318                                 }
319                         }
320                         if( oc->soc_allowed ) {
321                                 /* allow return of allowed attributes */
322                                 int i;
323                                 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
324                                         rc = is_at_subtype( desc->ad_type,
325                                                 oc->soc_allowed[i] );
326                                         if( rc ) return 1;
327                                 }
328                         }
329                 } else {
330                         /* short-circuit this search next time around */
331                         if (!slap_schema.si_at_undefined->sat_ad) {
332                                 const char *text;
333                                 slap_bv2undef_ad(&attrs->an_name,
334                                         &attrs->an_desc, &text);
335                         } else {
336                                 attrs->an_desc =
337                                         slap_schema.si_at_undefined->sat_ad;
338                         }
339                 }
340         }
341
342         return 0;
343 }
344
345
346 int slap_str2undef_ad(
347         const char *str,
348         AttributeDescription **ad,
349         const char **text )
350 {
351         struct berval bv;
352         bv.bv_val = (char *) str;
353         bv.bv_len = strlen( str );
354
355         return slap_bv2undef_ad( &bv, ad, text );
356 }
357
358 int slap_bv2undef_ad(
359         struct berval *bv,
360         AttributeDescription **ad,
361         const char **text )
362 {
363         AttributeDescription *desc;
364
365         assert( ad != NULL );
366
367         if( bv == NULL || bv->bv_len == 0 ) {
368                 *text = "empty attribute description";
369                 return LDAP_UNDEFINED_TYPE;
370         }
371
372         /* make sure description is IA5 */
373         if( ad_keystring( bv ) ) {
374                 *text = "attribute description contains inappropriate characters";
375                 return LDAP_UNDEFINED_TYPE;
376         }
377
378         for (desc = slap_schema.si_at_undefined->sat_ad; desc;
379                 desc=desc->ad_next)
380                 if (desc->ad_cname.bv_len == bv->bv_len &&
381                     !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
382                         break;
383         
384         if (!desc) {
385                 desc = ch_malloc(sizeof(AttributeDescription) +
386                         bv->bv_len + 1);
387                 
388                 desc->ad_flags = SLAP_DESC_NONE;
389                 desc->ad_lang.bv_val = NULL;
390                 desc->ad_lang.bv_len = 0;
391
392                 desc->ad_cname.bv_len = bv->bv_len;
393                 desc->ad_cname.bv_val = (char *)(desc+1);
394                 strcpy(desc->ad_cname.bv_val, bv->bv_val);
395
396                 /* canonical to upper case */
397                 ldap_pvt_str2upper( desc->ad_cname.bv_val );
398
399                 desc->ad_type = slap_schema.si_at_undefined;
400                 desc->ad_next = desc->ad_type->sat_ad;
401                 desc->ad_type->sat_ad = desc;
402         }
403
404         if (!*ad)
405                 *ad = desc;
406         else
407                 **ad = *desc;
408
409         return LDAP_SUCCESS;
410 }
411
412 int
413 an_find(
414     AttributeName *a,
415     struct berval *s
416 )
417 {
418         if( a == NULL ) return 0;
419
420         for ( ; a->an_name.bv_val; a++ ) {
421                 if ( a->an_name.bv_len != s->bv_len) continue;
422                 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
423                         return( 1 );
424                 }
425         }
426
427         return( 0 );
428 }
429
430 /* Convert a delimited string into a list of AttributeNames; Add on
431  * to an existing list if it was given.
432  */
433 AttributeName *
434 str2anlist( AttributeName *an, const char *in, const char *brkstr )
435 {
436         char    *str;
437         char    *s;
438         char    *lasts;
439         int     i, j;
440         const char *text;
441         AttributeName *anew;
442
443         /* find last element in list */
444         for (i = 0; an && an[i].an_name.bv_val; i++);
445         
446         /* protect the input string from strtok */
447         str = ch_strdup( in );
448
449         /* Count words in string */
450         j=1;
451         for ( s = str; *s; s++ ) {
452                 if ( strchr( brkstr, *s ) != NULL ) {
453                         j++;
454                 }
455         }
456
457         an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
458         anew = an + i;
459         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
460                 s != NULL;
461                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
462         {
463                 anew->an_desc = NULL;
464                 ber_str2bv(s, 0, 1, &anew->an_name);
465                 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
466                 anew++;
467         }
468         anew->an_name.bv_val = NULL;
469
470         free( str );
471         return( an );
472 }