3 * Copyright 1998-2003 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>
22 typedef struct Attr_option {
23 struct berval name; /* option name or prefix */
24 int prefix; /* NAME is a tag and range prefix */
27 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
29 /* Options sorted by name, and number of options */
30 static Attr_option *options = &lang_option;
31 static int option_count = 1;
33 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
35 static int ad_keystring(
40 if( !AD_CHAR( bv->bv_val[0] ) ) {
44 for( i=1; i<bv->bv_len; i++ ) {
45 if( !AD_CHAR( bv->bv_val[i] ) ) {
52 void ad_destroy( AttributeDescription *ad )
54 AttributeDescription *n;
56 for (; ad != NULL; ad = n) {
62 /* Is there an AttributeDescription for this type that uses these tags? */
63 AttributeDescription * ad_find_tags(
67 AttributeDescription *ad;
69 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
70 for (ad = type->sat_ad; ad; ad=ad->ad_next)
72 if (ad->ad_tags.bv_len == tags->bv_len &&
73 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
76 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
82 AttributeDescription **ad,
86 bv.bv_val = (char *) str;
87 bv.bv_len = strlen( str );
89 return slap_bv2ad( &bv, ad, text );
92 static char *strchrlen(
99 for( i=0; p[i]; i++ ) {
102 return (char *) &p[i];
112 AttributeDescription **ad,
115 int rtn = LDAP_UNDEFINED_TYPE;
116 AttributeDescription desc, *d2;
117 char *name, *options;
122 /* hardcoded limits for speed */
123 #define MAX_TAGGING_OPTIONS 128
124 struct berval tags[MAX_TAGGING_OPTIONS+1];
125 #define MAX_TAGS_LEN 1024
126 char tagbuf[MAX_TAGS_LEN];
128 assert( ad != NULL );
129 assert( *ad == NULL ); /* temporary */
131 if( bv == NULL || bv->bv_len == 0 ) {
132 *text = "empty attribute description";
136 /* make sure description is IA5 */
137 if( ad_keystring( bv ) ) {
138 *text = "attribute description contains inappropriate characters";
142 /* find valid base attribute type; parse in place */
143 memset( &desc, 0, sizeof( desc ));
146 options = strchr(name, ';');
147 if( options != NULL ) {
148 desc.ad_cname.bv_len = options - name;
150 desc.ad_type = at_bvfind( &desc.ad_cname );
151 if( desc.ad_type == NULL ) {
152 *text = "attribute type undefined";
156 if( is_at_operational( desc.ad_type ) && options != NULL ) {
157 *text = "operational attribute with options undefined";
162 * parse options in place
165 memset( tags, 0, sizeof( tags ));
168 for( opt=options; opt != NULL; opt=next ) {
171 next = strchrlen( opt, ';', &optlen );
174 *text = "zero length option is invalid";
177 } else if ( optlen == sizeof("binary")-1 &&
178 strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
181 if( slap_ad_is_binary( &desc ) ) {
182 *text = "option \"binary\" specified multiple times";
186 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
187 /* not stored in binary, disallow option */
188 *text = "option \"binary\" not supported with type";
192 desc.ad_flags |= SLAP_DESC_BINARY;
195 } else if ( ad_find_option_definition( opt, optlen ) ) {
198 if( opt[optlen-1] == '-' ) {
199 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
202 if( ntags >= MAX_TAGGING_OPTIONS ) {
203 *text = "too many tagging options";
208 * tags should be presented in sorted order,
209 * so run the array in reverse.
211 for( i=ntags-1; i>=0; i-- ) {
214 rc = strncasecmp( opt, tags[i].bv_val,
215 (unsigned) optlen < tags[i].bv_len
216 ? optlen : tags[i].bv_len );
218 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
219 /* duplicate (ignore) */
222 } else if ( rc > 0 ||
223 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
225 AC_MEMCPY( &tags[i+2], &tags[i+1],
226 (ntags-i-1)*sizeof(struct berval) );
227 tags[i+1].bv_val = opt;
228 tags[i+1].bv_len = optlen;
234 AC_MEMCPY( &tags[1], &tags[0],
235 ntags*sizeof(struct berval) );
237 tags[0].bv_val = opt;
238 tags[0].bv_len = optlen;
241 tagslen += optlen + 1;
245 *text = "unrecognized option";
253 if( tagslen > MAX_TAGS_LEN ) {
254 *text = "tagging options too long";
258 desc.ad_tags.bv_val = tagbuf;
261 for( i=0; i<ntags; i++ ) {
262 AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
263 tags[i].bv_val, tags[i].bv_len );
265 tagslen += tags[i].bv_len;
266 desc.ad_tags.bv_val[tagslen++] = ';';
269 desc.ad_tags.bv_val[--tagslen] = '\0';
270 desc.ad_tags.bv_len = tagslen;
273 /* see if a matching description is already cached */
274 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
275 if( d2->ad_flags != desc.ad_flags ) {
278 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
281 if( d2->ad_tags.bv_len == 0 ) {
284 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
285 desc.ad_tags.bv_len ) == 0 )
291 /* Not found, add new one */
294 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
295 /* check again now that we've locked */
296 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
297 if (d2->ad_flags != desc.ad_flags)
299 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
301 if (d2->ad_tags.bv_len == 0)
303 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
304 desc.ad_tags.bv_len) == 0)
308 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
312 /* Allocate a single contiguous block. If there are no
313 * options, we just need space for the AttrDesc structure.
314 * Otherwise, we need to tack on the full name length +
315 * options length, + maybe tagging options length again.
317 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
318 dlen = desc.ad_type->sat_cname.bv_len + 1;
319 if (desc.ad_tags.bv_len) {
320 dlen += 1+desc.ad_tags.bv_len;
322 if( slap_ad_is_binary( &desc ) ) {
323 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
327 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
329 d2->ad_type = desc.ad_type;
330 d2->ad_flags = desc.ad_flags;
331 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
332 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
335 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
336 d2->ad_tags.bv_val = NULL;
340 d2->ad_cname.bv_val = (char *)(d2+1);
341 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
342 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
343 if( slap_ad_is_binary( &desc ) ) {
346 if( desc.ad_tags.bv_len ) {
347 lp = desc.ad_tags.bv_val;
348 while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
349 && (lp = strchr( lp, ';' )) != NULL )
351 if( lp != desc.ad_tags.bv_val ) {
354 ? lp - desc.ad_tags.bv_val - 1
355 : strlen( desc.ad_tags.bv_val ));
356 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
359 cp = lutil_strcopy(cp, ";binary");
362 cp = lutil_strcopy(cp, lp);
364 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
365 if( desc.ad_tags.bv_len )
366 ldap_pvt_str2lower(op);
371 if( desc.ad_tags.bv_len ) {
372 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
375 d2->ad_tags.bv_val = lp;
376 strcpy(lp, desc.ad_tags.bv_val);
377 ldap_pvt_str2lower(lp);
379 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
382 /* Add new desc to list. We always want the bare Desc with
383 * no options to stay at the head of the list, assuming
384 * that one will be used most frequently.
386 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
387 d2->ad_next = desc.ad_type->sat_ad;
388 desc.ad_type->sat_ad = d2;
390 d2->ad_next = desc.ad_type->sat_ad->ad_next;
391 desc.ad_type->sat_ad->ad_next = d2;
393 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
405 static int is_ad_subtags(
406 struct berval *subtagsbv,
407 struct berval *suptagsbv )
409 const char *suptags, *supp, *supdelimp;
410 const char *subtags, *subp, *subdelimp;
413 subtags =subtagsbv->bv_val;
414 suptags =suptagsbv->bv_val;
416 for( supp=suptags ; supp; supp=supdelimp ) {
417 supdelimp = strchrlen( supp, ';', &suplen );
418 if( supdelimp ) supdelimp++;
420 for( subp=subtags ; subp; subp=subdelimp ) {
421 subdelimp = strchrlen( subp, ';', &sublen );
422 if( subdelimp ) subdelimp++;
425 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
426 && strncmp( supp, subp, sublen ) == 0 )
427 : ( ( suplen == sublen || supp[suplen-1] == '-' )
428 && strncmp( supp, subp, suplen ) == 0 ) )
441 AttributeDescription *sub,
442 AttributeDescription *super
448 for ( a = sub->ad_type; a; a=a->sat_sup ) {
449 if ( a == super->ad_type ) break;
455 /* ensure sub does support all flags of super */
456 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
457 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
461 /* check for tagging options */
462 if ( super->ad_tags.bv_len == 0 )
464 if ( sub->ad_tags.bv_len == 0 )
467 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
471 AttributeDescription *desc,
472 AttributeName *attrs )
474 if (! attrs ) return 0;
476 for( ; attrs->an_name.bv_val; attrs++ ) {
481 if ( attrs->an_desc ) {
484 if ( desc == attrs->an_desc ) {
489 * EXTENSION: if requested description is preceeded by
490 * a '-' character, do not match on subtypes.
492 if ( attrs->an_name.bv_val[0] == '-' ) {
496 /* Is this a subtype of the requested attr? */
497 for (a = desc->ad_type; a; a=a->sat_sup) {
498 if ( a == attrs->an_desc->ad_type )
504 /* Does desc support all the requested flags? */
505 lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
506 if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
507 != attrs->an_desc->ad_flags ) {
510 /* Do the descs have compatible tags? */
511 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
514 if ( desc->ad_tags.bv_len == 0) {
517 if ( is_ad_subtags( &desc->ad_tags,
518 &attrs->an_desc->ad_tags ) ) {
525 * EXTENSION: see if requested description is +objectClass
526 * if so, return attributes which the class requires/allows
529 if( oc == NULL && attrs->an_name.bv_val ) {
530 switch( attrs->an_name.bv_val[0] ) {
531 case '+': { /* new way */
532 struct berval ocname;
533 ocname.bv_len = attrs->an_name.bv_len - 1;
534 ocname.bv_val = &attrs->an_name.bv_val[1];
535 oc = oc_bvfind( &ocname );
537 default: /* old (deprecated) way */
538 oc = oc_bvfind( &attrs->an_name );
543 if ( oc == slap_schema.si_oc_extensibleObject ) {
544 /* extensibleObject allows the return of anything */
548 if( oc->soc_required ) {
549 /* allow return of required attributes */
551 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
552 for (a = desc->ad_type; a; a=a->sat_sup) {
553 if ( a == oc->soc_required[i] ) {
560 if( oc->soc_allowed ) {
561 /* allow return of allowed attributes */
563 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
564 for (a = desc->ad_type; a; a=a->sat_sup) {
565 if ( a == oc->soc_allowed[i] ) {
573 /* short-circuit this search next time around */
574 if (!slap_schema.si_at_undefined->sat_ad) {
576 slap_bv2undef_ad(&attrs->an_name,
577 &attrs->an_desc, &text);
580 slap_schema.si_at_undefined->sat_ad;
589 int slap_str2undef_ad(
591 AttributeDescription **ad,
595 bv.bv_val = (char *) str;
596 bv.bv_len = strlen( str );
598 return slap_bv2undef_ad( &bv, ad, text );
601 int slap_bv2undef_ad(
603 AttributeDescription **ad,
606 AttributeDescription *desc;
608 assert( ad != NULL );
610 if( bv == NULL || bv->bv_len == 0 ) {
611 *text = "empty attribute description";
612 return LDAP_UNDEFINED_TYPE;
615 /* make sure description is IA5 */
616 if( ad_keystring( bv ) ) {
617 *text = "attribute description contains inappropriate characters";
618 return LDAP_UNDEFINED_TYPE;
621 for( desc = slap_schema.si_at_undefined->sat_ad; desc;
624 if( desc->ad_cname.bv_len == bv->bv_len &&
625 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
632 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
635 desc->ad_flags = SLAP_DESC_NONE;
636 desc->ad_tags.bv_val = NULL;
637 desc->ad_tags.bv_len = 0;
639 desc->ad_cname.bv_len = bv->bv_len;
640 desc->ad_cname.bv_val = (char *)(desc+1);
641 strcpy(desc->ad_cname.bv_val, bv->bv_val);
643 /* canonical to upper case */
644 ldap_pvt_str2upper( desc->ad_cname.bv_val );
646 desc->ad_type = slap_schema.si_at_undefined;
647 desc->ad_next = desc->ad_type->sat_ad;
648 desc->ad_type->sat_ad = desc;
666 if( a == NULL ) return 0;
668 for ( ; a->an_name.bv_val; a++ ) {
669 if ( a->an_name.bv_len != s->bv_len) continue;
670 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
679 * Convert a delimited string into a list of AttributeNames;
680 * add on to an existing list if it was given. If the string
681 * is not a valid attribute name, if a '-' is prepended it is
682 * skipped and the remaining name is tried again; if a '+' is
683 * prepended, an objectclass name is searched instead.
685 * NOTE: currently, if a valid attribute name is not found,
686 * the same string is also checked as valid objectclass name;
687 * however, this behavior is deprecated.
690 str2anlist( AttributeName *an, char *in, const char *brkstr )
699 /* find last element in list */
700 for (i = 0; an && an[i].an_name.bv_val; i++);
702 /* protect the input string from strtok */
703 str = ch_strdup( in );
705 /* Count words in string */
707 for ( s = str; *s; s++ ) {
708 if ( strchr( brkstr, *s ) != NULL ) {
713 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
715 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
717 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
719 anew->an_desc = NULL;
721 ber_str2bv(s, 0, 1, &anew->an_name);
722 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
723 if ( !anew->an_desc ) {
724 switch( anew->an_name.bv_val[0] ) {
726 struct berval adname;
727 adname.bv_len = anew->an_name.bv_len - 1;
728 adname.bv_val = &anew->an_name.bv_val[1];
729 slap_bv2ad(&adname, &anew->an_desc, &text);
730 if ( !anew->an_desc ) {
733 * overwrites input string
742 struct berval ocname;
743 ocname.bv_len = anew->an_name.bv_len - 1;
744 ocname.bv_val = &anew->an_name.bv_val[1];
745 anew->an_oc = oc_bvfind( &ocname );
746 if ( !anew->an_oc ) {
749 * overwrites input string
758 /* old (deprecated) way */
759 anew->an_oc = oc_bvfind( &anew->an_name );
760 if ( !anew->an_oc ) {
762 /* overwrites input string on error! */
771 anew->an_name.bv_val = NULL;
777 /* Define an attribute option. */
779 ad_define_option( const char *name, const char *fname, int lineno )
784 if ( options == &lang_option ) {
793 if ( !DESC_CHAR( name[optlen] ) ) {
795 LDAP_LOG( CONFIG, CRIT,
796 "%s: line %d: illegal option name \"%s\"\n",
797 fname, lineno, name );
799 Debug( LDAP_DEBUG_ANY,
800 "%s: line %d: illegal option name \"%s\"\n",
801 fname, lineno, name );
805 } while ( name[++optlen] );
807 options = ch_realloc( options,
808 (option_count+1) * sizeof(Attr_option) );
810 if ( strcasecmp( name, "binary" ) == 0
811 || ad_find_option_definition( name, optlen ) ) {
813 LDAP_LOG( CONFIG, CRIT,
814 "%s: line %d: option \"%s\" is already defined\n",
815 fname, lineno, name );
817 Debug( LDAP_DEBUG_ANY,
818 "%s: line %d: option \"%s\" is already defined\n",
819 fname, lineno, name );
824 for ( i = option_count; i; --i ) {
825 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
827 options[i] = options[i-1];
830 options[i].name.bv_val = ch_strdup( name );
831 options[i].name.bv_len = optlen;
832 options[i].prefix = (name[optlen-1] == '-');
834 if ( i != option_count &&
836 optlen < options[i+1].name.bv_len &&
837 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
839 LDAP_LOG( CONFIG, CRIT,
840 "%s: line %d: option \"%s\" overrides previous option\n",
841 fname, lineno, name );
843 Debug( LDAP_DEBUG_ANY,
844 "%s: line %d: option \"%s\" overrides previous option\n",
845 fname, lineno, name );
854 /* Find the definition of the option name or prefix matching the arguments */
856 ad_find_option_definition( const char *opt, int optlen )
858 int top = 0, bot = option_count;
859 while ( top < bot ) {
860 int mid = (top + bot) / 2;
861 int mlen = options[mid].name.bv_len;
862 char *mname = options[mid].name.bv_val;
864 if ( optlen < mlen ) {
865 j = strncasecmp( opt, mname, optlen ) - 1;
867 j = strncasecmp( opt, mname, mlen );
868 if ( j==0 && (optlen==mlen || options[mid].prefix) )
869 return &options[mid];
880 AttributeDescription *ad,
883 switch( usage & SLAP_MR_TYPE_MASK ) {
885 case SLAP_MR_EQUALITY:
886 return ad->ad_type->sat_equality;
888 case SLAP_MR_ORDERING:
889 return ad->ad_type->sat_ordering;
892 return ad->ad_type->sat_substr;
896 assert( 0 /* ad_mr: bad usage */);