3 * Copyright 1998-2002 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( AttributeDescription *ad )
40 AttributeDescription *n;
42 for (; ad != NULL; ad = 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, ';');
106 if (options != NULL) {
107 desc.ad_cname.bv_len = options - name;
109 desc.ad_type = at_bvfind( &desc.ad_cname );
110 if( desc.ad_type == NULL ) {
111 *text = "attribute type undefined";
115 desc.ad_flags = SLAP_DESC_NONE;
116 desc.ad_lang.bv_len = 0;
117 desc.ad_lang.bv_val = NULL;
119 if( is_at_operational( desc.ad_type ) && options != NULL ) {
120 *text = "operational attribute with options undefined";
124 /* parse options in place */
125 for( ; options != NULL; ) {
127 options = strchr( name, ';' );
128 if ( options != NULL )
131 i = bv->bv_len - (name - bv->bv_val);
133 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
134 if( slap_ad_is_binary( &desc ) ) {
135 *text = "option \"binary\" specified multiple times";
139 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
140 /* not stored in binary, disallow option */
141 *text = "option \"binary\" with type not supported";
145 desc.ad_flags |= SLAP_DESC_BINARY;
147 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
148 sizeof("lang-")-1 ) == 0)
150 if( desc.ad_lang.bv_len != 0 ) {
151 *text = "multiple language tag options specified";
155 desc.ad_lang.bv_val = name;
156 desc.ad_lang.bv_len = i;
158 *text = "unrecognized option";
163 /* see if a matching description is already cached */
164 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
165 if (d2->ad_flags != desc.ad_flags)
167 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
169 if (d2->ad_lang.bv_len == 0)
171 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
172 desc.ad_lang.bv_len) == 0)
176 /* Not found, add new one */
179 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
180 /* check again now that we've locked */
181 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
182 if (d2->ad_flags != desc.ad_flags)
184 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
186 if (d2->ad_lang.bv_len == 0)
188 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
189 desc.ad_lang.bv_len) == 0)
193 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
197 /* Allocate a single contiguous block. If there are no
198 * options, we just need space for the AttrDesc structure.
199 * Otherwise, we need to tack on the full name length +
202 i = sizeof(AttributeDescription);
203 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
204 if (desc.ad_lang.bv_len)
205 dlen = desc.ad_lang.bv_len+1;
206 dlen += desc.ad_type->sat_cname.bv_len+1;
207 if( slap_ad_is_binary( &desc ) ) {
208 dlen += sizeof("binary");
212 d2 = ch_malloc(i + dlen);
213 d2->ad_type = desc.ad_type;
214 d2->ad_flags = desc.ad_flags;
215 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
216 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
218 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
219 d2->ad_lang.bv_val = NULL;
221 d2->ad_cname.bv_val = (char *)(d2+1);
222 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
223 if( slap_ad_is_binary( &desc ) ) {
224 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
226 d2->ad_cname.bv_len += sizeof("binary");
228 if( d2->ad_lang.bv_len ) {
229 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
230 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
232 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
234 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
235 ldap_pvt_str2lower(d2->ad_lang.bv_val);
236 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
239 /* Add new desc to list. We always want the bare Desc with
240 * no options to stay at the head of the list, assuming
241 * that one will be used most frequently.
243 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
244 d2->ad_next = desc.ad_type->sat_ad;
245 desc.ad_type->sat_ad = d2;
247 d2->ad_next = desc.ad_type->sat_ad->ad_next;
248 desc.ad_type->sat_ad->ad_next = d2;
250 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
266 AttributeDescription *sub,
267 AttributeDescription *super
270 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
274 if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
278 if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
279 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
280 sub->ad_lang.bv_val)))
290 AttributeDescription *desc,
291 AttributeName *attrs )
293 if (! attrs ) return 0;
295 for( ; attrs->an_name.bv_val; attrs++ ) {
299 if ( attrs->an_desc ) {
300 if ( is_ad_subtype( desc, attrs->an_desc ))
307 * EXTENSION: see if requested description is an object class
308 * if so, return attributes which the class requires/allows
312 oc = oc_bvfind( &attrs->an_name );
316 if ( oc == slap_schema.si_oc_extensibleObject ) {
317 /* extensibleObject allows the return of anything */
321 if( oc->soc_required ) {
322 /* allow return of required attributes */
324 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
325 rc = is_at_subtype( desc->ad_type,
326 oc->soc_allowed[i] );
330 if( oc->soc_allowed ) {
331 /* allow return of allowed attributes */
333 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
334 rc = is_at_subtype( desc->ad_type,
335 oc->soc_allowed[i] );
340 /* short-circuit this search next time around */
341 if (!slap_schema.si_at_undefined->sat_ad) {
343 slap_bv2undef_ad(&attrs->an_name,
344 &attrs->an_desc, &text);
347 slap_schema.si_at_undefined->sat_ad;
356 int slap_str2undef_ad(
358 AttributeDescription **ad,
362 bv.bv_val = (char *) str;
363 bv.bv_len = strlen( str );
365 return slap_bv2undef_ad( &bv, ad, text );
368 int slap_bv2undef_ad(
370 AttributeDescription **ad,
373 AttributeDescription *desc;
375 assert( ad != NULL );
377 if( bv == NULL || bv->bv_len == 0 ) {
378 *text = "empty attribute description";
379 return LDAP_UNDEFINED_TYPE;
382 /* make sure description is IA5 */
383 if( ad_keystring( bv ) ) {
384 *text = "attribute description contains inappropriate characters";
385 return LDAP_UNDEFINED_TYPE;
388 for (desc = slap_schema.si_at_undefined->sat_ad; desc;
390 if (desc->ad_cname.bv_len == bv->bv_len &&
391 !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
395 desc = ch_malloc(sizeof(AttributeDescription) +
398 desc->ad_flags = SLAP_DESC_NONE;
399 desc->ad_lang.bv_val = NULL;
400 desc->ad_lang.bv_len = 0;
402 desc->ad_cname.bv_len = bv->bv_len;
403 desc->ad_cname.bv_val = (char *)(desc+1);
404 strcpy(desc->ad_cname.bv_val, bv->bv_val);
406 /* canonical to upper case */
407 ldap_pvt_str2upper( desc->ad_cname.bv_val );
409 desc->ad_type = slap_schema.si_at_undefined;
410 desc->ad_next = desc->ad_type->sat_ad;
411 desc->ad_type->sat_ad = desc;
428 if( a == NULL ) return 0;
430 for ( ; a->an_name.bv_val; a++ ) {
431 if ( a->an_name.bv_len != s->bv_len) continue;
432 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
440 /* Convert a delimited string into a list of AttributeNames; Add on
441 * to an existing list if it was given.
444 str2anlist( AttributeName *an, char *in, const char *brkstr )
453 /* find last element in list */
454 for (i = 0; an && an[i].an_name.bv_val; i++);
456 /* protect the input string from strtok */
457 str = ch_strdup( in );
459 /* Count words in string */
461 for ( s = str; *s; s++ ) {
462 if ( strchr( brkstr, *s ) != NULL ) {
467 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
469 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
471 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
473 anew->an_desc = NULL;
475 ber_str2bv(s, 0, 1, &anew->an_name);
476 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
477 if ( !anew->an_desc ) {
478 anew->an_oc = oc_bvfind( &anew->an_name );
479 if ( !anew->an_oc ) {
481 /* overwrites input string on error! */
488 anew->an_name.bv_val = NULL;