3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* ad.c - routines for dealing with attribute descriptions */
14 #include <ac/socket.h>
15 #include <ac/string.h>
21 static int ad_keystring(
26 if( !AD_CHAR( bv->bv_val[0] ) ) {
30 for( i=1; i<bv->bv_len; i++ ) {
31 if( !AD_CHAR( bv->bv_val[i] ) ) {
38 void ad_destroy( void *in )
40 AttributeDescription *ad = in, *n;
48 /* Is there an AttributeDescription for this type that uses this language? */
49 AttributeDescription * ad_find_lang(
53 AttributeDescription *ad;
55 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
56 for (ad = type->sat_ad; ad; ad=ad->ad_next)
58 if (ad->ad_lang.bv_len == lang->bv_len &&
59 !strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
62 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
68 AttributeDescription **ad,
72 bv.bv_val = (char *) str;
73 bv.bv_len = strlen( str );
75 return slap_bv2ad( &bv, ad, text );
80 AttributeDescription **ad,
83 int rtn = LDAP_UNDEFINED_TYPE;
85 AttributeDescription desc, *d2;
89 assert( *ad == NULL ); /* temporary */
91 if( bv == NULL || bv->bv_len == 0 ) {
92 *text = "empty attribute description";
96 /* make sure description is IA5 */
97 if( ad_keystring( bv ) ) {
98 *text = "attribute description contains inappropriate characters";
102 /* find valid base attribute type; parse in place */
105 options = strchr(name, ';');
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";
114 desc.ad_flags = SLAP_DESC_NONE;
115 desc.ad_lang.bv_len = 0;
116 desc.ad_lang.bv_val = NULL;
118 /* parse options in place */
119 for( ; options != NULL; ) {
121 options = strchr( name, ';' );
122 if ( options != NULL )
125 i = bv->bv_len - (name - bv->bv_val);
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";
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";
139 desc.ad_flags |= SLAP_DESC_BINARY;
141 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
142 sizeof("lang-")-1 ) == 0)
144 if( desc.ad_lang.bv_len != 0 ) {
145 *text = "multiple language tag options specified";
149 desc.ad_lang.bv_val = name;
150 desc.ad_lang.bv_len = i;
152 *text = "unrecognized option";
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)
161 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
163 if (d2->ad_lang.bv_len == 0)
165 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
166 desc.ad_lang.bv_len) == 0)
170 /* Not found, add new one */
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)
178 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
180 if (d2->ad_lang.bv_len == 0)
182 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
183 desc.ad_lang.bv_len) == 0)
187 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
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 +
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");
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;
212 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
213 d2->ad_lang.bv_val = NULL;
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,
220 d2->ad_cname.bv_len += sizeof("binary");
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+
226 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
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;
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.
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;
241 d2->ad_next = desc.ad_type->sat_ad->ad_next;
242 desc.ad_type->sat_ad->ad_next = d2;
244 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
260 AttributeDescription *sub,
261 AttributeDescription *super
264 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
268 if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
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)))
284 AttributeDescription *desc,
285 AttributeName *attrs )
287 for( ; attrs; attrs=attrs->an_next ) {
291 if ( attrs->an_desc ) {
292 if ( is_ad_subtype( desc, attrs->an_desc ))
299 * EXTENSION: see if requested description is an object class
300 * if so, return attributes which the class requires/allows
302 oc = oc_bvfind( &attrs->an_name );
304 if ( oc == slap_schema.si_oc_extensibleObject ) {
305 /* extensibleObject allows the return of anything */
309 if( oc->soc_required ) {
310 /* allow return of required attributes */
312 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
313 rc = is_at_subtype( desc->ad_type,
314 oc->soc_allowed[i] );
318 if( oc->soc_allowed ) {
319 /* allow return of allowed attributes */
321 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
322 rc = is_at_subtype( desc->ad_type,
323 oc->soc_allowed[i] );
328 /* short-circuit this search next time around */
329 if (!slap_schema.si_at_undefined->sat_ad) {
331 slap_bv2undef_ad(&attrs->an_name,
332 &attrs->an_desc, &text);
335 slap_schema.si_at_undefined->sat_ad;
344 int slap_str2undef_ad(
346 AttributeDescription **ad,
350 bv.bv_val = (char *) str;
351 bv.bv_len = strlen( str );
353 return slap_bv2undef_ad( &bv, ad, text );
356 int slap_bv2undef_ad(
358 AttributeDescription **ad,
361 AttributeDescription *desc;
363 assert( ad != NULL );
365 if( bv == NULL || bv->bv_len == 0 ) {
366 *text = "empty attribute description";
367 return LDAP_UNDEFINED_TYPE;
370 /* make sure description is IA5 */
371 if( ad_keystring( bv ) ) {
372 *text = "attribute description contains inappropriate characters";
373 return LDAP_UNDEFINED_TYPE;
376 for (desc = slap_schema.si_at_undefined->sat_ad; desc;
378 if (desc->ad_cname.bv_len == bv->bv_len &&
379 !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
383 desc = ch_malloc(sizeof(AttributeDescription) +
386 desc->ad_flags = SLAP_DESC_NONE;
387 desc->ad_lang.bv_val = NULL;
388 desc->ad_lang.bv_len = 0;
390 desc->ad_cname.bv_len = bv->bv_len;
391 desc->ad_cname.bv_val = (char *)(desc+1);
392 strcpy(desc->ad_cname.bv_val, bv->bv_val);
394 /* canonical to upper case */
395 ldap_pvt_str2upper( desc->ad_cname.bv_val );
397 desc->ad_type = slap_schema.si_at_undefined;
398 desc->ad_next = desc->ad_type->sat_ad;
399 desc->ad_type->sat_ad = desc;
416 if( a == NULL ) return 0;
418 for ( ; a; a=a->an_next ) {
419 if ( a->an_name.bv_len != s->bv_len) continue;
420 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
428 /* Convert a delimited string into a list of AttributeNames; Add on
429 * to an existing list if it was given.
432 str2anlist( AttributeName *an, const char *in, const char *brkstr )
438 AttributeName *a, *anew;
440 /* protect the input string from strtok */
441 str = ch_strdup( in );
443 /* find last element in list */
444 for (a = an; a && a->an_next; a=a->an_next);
446 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
448 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
450 anew = ch_malloc( sizeof( AttributeName ) );
451 anew->an_next = NULL;
452 anew->an_desc = NULL;
453 ber_str2bv(s, 0, 1, &anew->an_name);
454 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);