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 );
78 static char *strchrlen(
85 for( i=0; p[i]; i++ ) {
88 return (char *) &p[i];
98 AttributeDescription **ad,
101 int rtn = LDAP_UNDEFINED_TYPE;
102 AttributeDescription desc, *d2;
103 char *name, *options;
108 /* hardcoded limits for speed */
109 #define MAX_LANG_OPTIONS 128
110 struct berval langs[MAX_LANG_OPTIONS+1];
111 #define MAX_LANG_LEN 1024
112 char langbuf[MAX_LANG_LEN];
114 assert( ad != NULL );
115 assert( *ad == NULL ); /* temporary */
117 if( bv == NULL || bv->bv_len == 0 ) {
118 *text = "empty attribute description";
122 /* make sure description is IA5 */
123 if( ad_keystring( bv ) ) {
124 *text = "attribute description contains inappropriate characters";
128 /* find valid base attribute type; parse in place */
129 memset( &desc, 0, sizeof( desc ));
132 options = strchr(name, ';');
133 if( options != NULL ) {
134 desc.ad_cname.bv_len = options - name;
136 desc.ad_type = at_bvfind( &desc.ad_cname );
137 if( desc.ad_type == NULL ) {
138 *text = "attribute type undefined";
142 if( is_at_operational( desc.ad_type ) && options != NULL ) {
143 *text = "operational attribute with options undefined";
148 * parse options in place
151 memset( langs, 0, sizeof( langs ));
154 for( opt=options; opt != NULL; opt=next ) {
157 next = strchrlen( opt, ';', &optlen );
160 *text = "zero length option is invalid";
163 } else if ( optlen == sizeof("binary")-1 &&
164 strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
167 if( slap_ad_is_binary( &desc ) ) {
168 *text = "option \"binary\" specified multiple times";
172 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
173 /* not stored in binary, disallow option */
174 *text = "option \"binary\" not supported with type";
178 desc.ad_flags |= SLAP_DESC_BINARY;
181 } else if ( optlen >= sizeof("lang-")-1 &&
182 strncasecmp( opt, "lang-", sizeof("lang-")-1 ) == 0 )
186 if( opt[optlen-1] == '-' ) {
187 desc.ad_flags |= SLAP_DESC_LANG_RANGE;
190 if( nlang >= MAX_LANG_OPTIONS ) {
191 *text = "too many language options";
196 * tags should be presented in sorted order,
197 * so run the array in reverse.
199 for( i=nlang-1; i>=0; i-- ) {
202 rc = strncasecmp( opt, langs[i].bv_val,
203 (unsigned) optlen < langs[i].bv_len
204 ? optlen : langs[i].bv_len );
206 if( rc == 0 && (unsigned)optlen == langs[i].bv_len ) {
207 /* duplicate (ignore) */
210 } else if ( rc > 0 ||
211 ( rc == 0 && (unsigned)optlen > langs[i].bv_len ))
213 AC_MEMCPY( &langs[i+1], &langs[i],
214 (nlang-i)*sizeof(struct berval) );
215 langs[i].bv_val = opt;
216 langs[i].bv_len = optlen;
222 AC_MEMCPY( &langs[1], &langs[0],
223 nlang*sizeof(struct berval) );
225 langs[0].bv_val = opt;
226 langs[0].bv_len = optlen;
229 langlen += optlen + 1;
233 *text = "unrecognized option";
241 if( langlen > MAX_LANG_LEN ) {
242 *text = "language options too long";
246 desc.ad_lang.bv_val = langbuf;
249 for( i=0; i<nlang; i++ ) {
250 AC_MEMCPY( &desc.ad_lang.bv_val[langlen],
251 langs[i].bv_val, langs[i].bv_len );
253 langlen += langs[i].bv_len;
254 desc.ad_lang.bv_val[langlen++] = ';';
257 desc.ad_lang.bv_val[--langlen] = '\0';
258 desc.ad_lang.bv_len = langlen;
261 /* see if a matching description is already cached */
262 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
263 if( d2->ad_flags != desc.ad_flags ) {
266 if( d2->ad_lang.bv_len != desc.ad_lang.bv_len ) {
269 if( d2->ad_lang.bv_len == 0 ) {
272 if( strncasecmp( d2->ad_lang.bv_val, desc.ad_lang.bv_val,
273 desc.ad_lang.bv_len ) == 0 )
279 /* Not found, add new one */
282 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
283 /* check again now that we've locked */
284 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
285 if (d2->ad_flags != desc.ad_flags)
287 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
289 if (d2->ad_lang.bv_len == 0)
291 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
292 desc.ad_lang.bv_len) == 0)
296 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
300 /* Allocate a single contiguous block. If there are no
301 * options, we just need space for the AttrDesc structure.
302 * Otherwise, we need to tack on the full name length +
305 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
306 dlen = desc.ad_type->sat_cname.bv_len;
307 if (desc.ad_lang.bv_len) {
308 dlen += 1+desc.ad_lang.bv_len;
310 if( slap_ad_is_binary( &desc ) ) {
311 dlen += sizeof(";binary")-1;
315 d2 = ch_malloc(sizeof(AttributeDescription) + dlen + 1);
316 d2->ad_type = desc.ad_type;
317 d2->ad_flags = desc.ad_flags;
318 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
319 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
322 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
323 d2->ad_lang.bv_val = NULL;
325 d2->ad_cname.bv_val = (char *)(d2+1);
326 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
327 if( slap_ad_is_binary( &desc ) ) {
328 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
330 d2->ad_cname.bv_len += sizeof(";binary")-1;
332 if( d2->ad_lang.bv_len ) {
333 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
334 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
336 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
338 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
339 ldap_pvt_str2lower(d2->ad_lang.bv_val);
340 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
343 /* Add new desc to list. We always want the bare Desc with
344 * no options to stay at the head of the list, assuming
345 * that one will be used most frequently.
347 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
348 d2->ad_next = desc.ad_type->sat_ad;
349 desc.ad_type->sat_ad = d2;
351 d2->ad_next = desc.ad_type->sat_ad->ad_next;
352 desc.ad_type->sat_ad->ad_next = d2;
354 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
366 static int is_ad_sublang(
367 struct berval *sublangbv,
368 struct berval *suplangbv )
370 const char *suplang, *supp, *supdelimp;
371 const char *sublang, *subp, *subdelimp;
374 if( suplangbv->bv_len == 0 ) return 1;
375 if( sublangbv->bv_len == 0 ) return 0;
377 sublang =sublangbv->bv_val;
378 suplang =suplangbv->bv_val;
380 for( supp=suplang ; supp; supp=supdelimp ) {
381 supdelimp = strchrlen( supp, ';', &suplen );
382 if( supdelimp ) supdelimp++;
384 for( subp=sublang ; subp; subp=subdelimp ) {
385 subdelimp = strchrlen( subp, ';', &sublen );
386 if( subdelimp ) subdelimp++;
388 if ((( suplen < sublen && supp[suplen-1] == '-' ) ||
389 suplen == sublen ) && strncmp( supp, subp, suplen ) == 0 )
402 AttributeDescription *sub,
403 AttributeDescription *super
408 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
412 /* ensure sub does support all flags of super */
413 lr = sub->ad_lang.bv_len ? SLAP_DESC_LANG_RANGE : 0;
414 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
418 /* check for language tags */
419 if ( !is_ad_sublang( &sub->ad_lang, &super->ad_lang )) {
427 AttributeDescription *desc,
428 AttributeName *attrs )
430 if (! attrs ) return 0;
432 for( ; attrs->an_name.bv_val; attrs++ ) {
436 if ( attrs->an_desc ) {
437 if ( is_ad_subtype( desc, attrs->an_desc ))
443 * EXTENSION: see if requested description is an object class
444 * if so, return attributes which the class requires/allows
448 oc = oc_bvfind( &attrs->an_name );
452 if ( oc == slap_schema.si_oc_extensibleObject ) {
453 /* extensibleObject allows the return of anything */
457 if( oc->soc_required ) {
458 /* allow return of required attributes */
460 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
461 rc = is_at_subtype( desc->ad_type,
462 oc->soc_required[i] );
467 if( oc->soc_allowed ) {
468 /* allow return of allowed attributes */
470 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
471 rc = is_at_subtype( desc->ad_type,
472 oc->soc_allowed[i] );
478 /* short-circuit this search next time around */
479 if (!slap_schema.si_at_undefined->sat_ad) {
481 slap_bv2undef_ad(&attrs->an_name,
482 &attrs->an_desc, &text);
485 slap_schema.si_at_undefined->sat_ad;
494 int slap_str2undef_ad(
496 AttributeDescription **ad,
500 bv.bv_val = (char *) str;
501 bv.bv_len = strlen( str );
503 return slap_bv2undef_ad( &bv, ad, text );
506 int slap_bv2undef_ad(
508 AttributeDescription **ad,
511 AttributeDescription *desc;
513 assert( ad != NULL );
515 if( bv == NULL || bv->bv_len == 0 ) {
516 *text = "empty attribute description";
517 return LDAP_UNDEFINED_TYPE;
520 /* make sure description is IA5 */
521 if( ad_keystring( bv ) ) {
522 *text = "attribute description contains inappropriate characters";
523 return LDAP_UNDEFINED_TYPE;
526 for( desc = slap_schema.si_at_undefined->sat_ad; desc;
529 if( desc->ad_cname.bv_len == bv->bv_len &&
530 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
537 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
540 desc->ad_flags = SLAP_DESC_NONE;
541 desc->ad_lang.bv_val = NULL;
542 desc->ad_lang.bv_len = 0;
544 desc->ad_cname.bv_len = bv->bv_len;
545 desc->ad_cname.bv_val = (char *)(desc+1);
546 strcpy(desc->ad_cname.bv_val, bv->bv_val);
548 /* canonical to upper case */
549 ldap_pvt_str2upper( desc->ad_cname.bv_val );
551 desc->ad_type = slap_schema.si_at_undefined;
552 desc->ad_next = desc->ad_type->sat_ad;
553 desc->ad_type->sat_ad = desc;
571 if( a == NULL ) return 0;
573 for ( ; a->an_name.bv_val; a++ ) {
574 if ( a->an_name.bv_len != s->bv_len) continue;
575 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
583 /* Convert a delimited string into a list of AttributeNames; Add on
584 * to an existing list if it was given.
587 str2anlist( AttributeName *an, char *in, const char *brkstr )
596 /* find last element in list */
597 for (i = 0; an && an[i].an_name.bv_val; i++);
599 /* protect the input string from strtok */
600 str = ch_strdup( in );
602 /* Count words in string */
604 for ( s = str; *s; s++ ) {
605 if ( strchr( brkstr, *s ) != NULL ) {
610 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
612 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
614 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
616 anew->an_desc = NULL;
618 ber_str2bv(s, 0, 1, &anew->an_name);
619 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
620 if ( !anew->an_desc ) {
621 anew->an_oc = oc_bvfind( &anew->an_name );
622 if ( !anew->an_oc ) {
624 /* overwrites input string on error! */
631 anew->an_name.bv_val = NULL;