1 /* ad.c - routines for dealing with attribute descriptions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/socket.h>
24 #include <ac/string.h>
31 typedef struct Attr_option {
32 struct berval name; /* option name or prefix */
33 int prefix; /* NAME is a tag and range prefix */
36 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
38 /* Options sorted by name, and number of options */
39 static Attr_option *options = &lang_option;
40 static int option_count = 1;
42 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
44 static int ad_keystring(
49 if( !AD_CHAR( bv->bv_val[0] ) ) {
53 for( i=1; i<bv->bv_len; i++ ) {
54 if( !AD_CHAR( bv->bv_val[i] ) ) {
61 void ad_destroy( AttributeDescription *ad )
63 AttributeDescription *n;
65 for (; ad != NULL; ad = n) {
71 /* Is there an AttributeDescription for this type that uses these tags? */
72 AttributeDescription * ad_find_tags(
76 AttributeDescription *ad;
78 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
79 for (ad = type->sat_ad; ad; ad=ad->ad_next)
81 if (ad->ad_tags.bv_len == tags->bv_len &&
82 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
85 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
91 AttributeDescription **ad,
95 bv.bv_val = (char *) str;
96 bv.bv_len = strlen( str );
98 return slap_bv2ad( &bv, ad, text );
101 static char *strchrlen(
108 for( i=0; p[i]; i++ ) {
111 return (char *) &p[i];
121 AttributeDescription **ad,
124 int rtn = LDAP_UNDEFINED_TYPE;
125 AttributeDescription desc, *d2;
126 char *name, *options;
131 /* hardcoded limits for speed */
132 #define MAX_TAGGING_OPTIONS 128
133 struct berval tags[MAX_TAGGING_OPTIONS+1];
134 #define MAX_TAGS_LEN 1024
135 char tagbuf[MAX_TAGS_LEN];
137 assert( ad != NULL );
138 assert( *ad == NULL ); /* temporary */
140 if( bv == NULL || bv->bv_len == 0 ) {
141 *text = "empty attribute description";
145 /* make sure description is IA5 */
146 if( ad_keystring( bv ) ) {
147 *text = "attribute description contains inappropriate characters";
151 /* find valid base attribute type; parse in place */
152 memset( &desc, 0, sizeof( desc ));
155 options = strchr(name, ';');
156 if( options != NULL ) {
157 desc.ad_cname.bv_len = options - name;
159 desc.ad_type = at_bvfind( &desc.ad_cname );
160 if( desc.ad_type == NULL ) {
161 *text = "attribute type undefined";
165 if( is_at_operational( desc.ad_type ) && options != NULL ) {
166 *text = "operational attribute with options undefined";
171 * parse options in place
174 memset( tags, 0, sizeof( tags ));
177 for( opt=options; opt != NULL; opt=next ) {
180 next = strchrlen( opt, ';', &optlen );
183 *text = "zero length option is invalid";
186 } else if ( optlen == sizeof("binary")-1 &&
187 strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
190 if( slap_ad_is_binary( &desc ) ) {
191 *text = "option \"binary\" specified multiple times";
195 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
196 /* not stored in binary, disallow option */
197 *text = "option \"binary\" not supported with type";
201 desc.ad_flags |= SLAP_DESC_BINARY;
204 } else if ( ad_find_option_definition( opt, optlen ) ) {
207 if( opt[optlen-1] == '-' ) {
208 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
211 if( ntags >= MAX_TAGGING_OPTIONS ) {
212 *text = "too many tagging options";
217 * tags should be presented in sorted order,
218 * so run the array in reverse.
220 for( i=ntags-1; i>=0; i-- ) {
223 rc = strncasecmp( opt, tags[i].bv_val,
224 (unsigned) optlen < tags[i].bv_len
225 ? optlen : tags[i].bv_len );
227 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
228 /* duplicate (ignore) */
231 } else if ( rc > 0 ||
232 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
234 AC_MEMCPY( &tags[i+2], &tags[i+1],
235 (ntags-i-1)*sizeof(struct berval) );
236 tags[i+1].bv_val = opt;
237 tags[i+1].bv_len = optlen;
243 AC_MEMCPY( &tags[1], &tags[0],
244 ntags*sizeof(struct berval) );
246 tags[0].bv_val = opt;
247 tags[0].bv_len = optlen;
250 tagslen += optlen + 1;
254 *text = "unrecognized option";
262 if( tagslen > MAX_TAGS_LEN ) {
263 *text = "tagging options too long";
267 desc.ad_tags.bv_val = tagbuf;
270 for( i=0; i<ntags; i++ ) {
271 AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
272 tags[i].bv_val, tags[i].bv_len );
274 tagslen += tags[i].bv_len;
275 desc.ad_tags.bv_val[tagslen++] = ';';
278 desc.ad_tags.bv_val[--tagslen] = '\0';
279 desc.ad_tags.bv_len = tagslen;
282 /* see if a matching description is already cached */
283 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
284 if( d2->ad_flags != desc.ad_flags ) {
287 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
290 if( d2->ad_tags.bv_len == 0 ) {
293 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
294 desc.ad_tags.bv_len ) == 0 )
300 /* Not found, add new one */
303 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
304 /* check again now that we've locked */
305 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
306 if (d2->ad_flags != desc.ad_flags)
308 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
310 if (d2->ad_tags.bv_len == 0)
312 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
313 desc.ad_tags.bv_len) == 0)
317 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
321 /* Allocate a single contiguous block. If there are no
322 * options, we just need space for the AttrDesc structure.
323 * Otherwise, we need to tack on the full name length +
324 * options length, + maybe tagging options length again.
326 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
327 dlen = desc.ad_type->sat_cname.bv_len + 1;
328 if (desc.ad_tags.bv_len) {
329 dlen += 1+desc.ad_tags.bv_len;
331 if( slap_ad_is_binary( &desc ) ) {
332 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
336 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
338 d2->ad_type = desc.ad_type;
339 d2->ad_flags = desc.ad_flags;
340 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
341 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
344 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
345 d2->ad_tags.bv_val = NULL;
349 d2->ad_cname.bv_val = (char *)(d2+1);
350 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
351 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
352 if( slap_ad_is_binary( &desc ) ) {
355 if( desc.ad_tags.bv_len ) {
356 lp = desc.ad_tags.bv_val;
357 while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
358 && (lp = strchr( lp, ';' )) != NULL )
360 if( lp != desc.ad_tags.bv_val ) {
363 ? lp - desc.ad_tags.bv_val - 1
364 : strlen( desc.ad_tags.bv_val ));
365 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
368 cp = lutil_strcopy(cp, ";binary");
371 cp = lutil_strcopy(cp, lp);
373 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
374 if( desc.ad_tags.bv_len )
375 ldap_pvt_str2lower(op);
380 if( desc.ad_tags.bv_len ) {
381 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
384 d2->ad_tags.bv_val = lp;
385 strcpy(lp, desc.ad_tags.bv_val);
386 ldap_pvt_str2lower(lp);
388 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
391 /* Add new desc to list. We always want the bare Desc with
392 * no options to stay at the head of the list, assuming
393 * that one will be used most frequently.
395 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
396 d2->ad_next = desc.ad_type->sat_ad;
397 desc.ad_type->sat_ad = d2;
399 d2->ad_next = desc.ad_type->sat_ad->ad_next;
400 desc.ad_type->sat_ad->ad_next = d2;
402 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
414 static int is_ad_subtags(
415 struct berval *subtagsbv,
416 struct berval *suptagsbv )
418 const char *suptags, *supp, *supdelimp;
419 const char *subtags, *subp, *subdelimp;
422 subtags =subtagsbv->bv_val;
423 suptags =suptagsbv->bv_val;
425 for( supp=suptags ; supp; supp=supdelimp ) {
426 supdelimp = strchrlen( supp, ';', &suplen );
427 if( supdelimp ) supdelimp++;
429 for( subp=subtags ; subp; subp=subdelimp ) {
430 subdelimp = strchrlen( subp, ';', &sublen );
431 if( subdelimp ) subdelimp++;
434 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
435 && strncmp( supp, subp, sublen ) == 0 )
436 : ( ( suplen == sublen || supp[suplen-1] == '-' )
437 && strncmp( supp, subp, suplen ) == 0 ) )
450 AttributeDescription *sub,
451 AttributeDescription *super
457 for ( a = sub->ad_type; a; a=a->sat_sup ) {
458 if ( a == super->ad_type ) break;
464 /* ensure sub does support all flags of super */
465 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
466 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
470 /* check for tagging options */
471 if ( super->ad_tags.bv_len == 0 )
473 if ( sub->ad_tags.bv_len == 0 )
476 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
480 AttributeDescription *desc,
481 AttributeName *attrs )
483 if (! attrs ) return 0;
485 for( ; attrs->an_name.bv_val; attrs++ ) {
490 if ( attrs->an_desc ) {
493 if ( desc == attrs->an_desc ) {
498 * EXTENSION: if requested description is preceeded by
499 * a '-' character, do not match on subtypes.
501 if ( attrs->an_name.bv_val[0] == '-' ) {
505 /* Is this a subtype of the requested attr? */
506 for (a = desc->ad_type; a; a=a->sat_sup) {
507 if ( a == attrs->an_desc->ad_type )
513 /* Does desc support all the requested flags? */
514 lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
515 if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
516 != attrs->an_desc->ad_flags ) {
519 /* Do the descs have compatible tags? */
520 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
523 if ( desc->ad_tags.bv_len == 0) {
526 if ( is_ad_subtags( &desc->ad_tags,
527 &attrs->an_desc->ad_tags ) ) {
534 * EXTENSION: see if requested description is @objectClass
535 * if so, return attributes which the class requires/allows
536 * else if requested description is !objectClass, return
537 * attributes which the class does not require/allow
540 if( oc == NULL && attrs->an_name.bv_val ) {
541 switch( attrs->an_name.bv_val[0] ) {
542 case '@': /* @objectClass */
543 case '+': /* +objectClass (deprecated) */
544 case '!': { /* exclude */
545 struct berval ocname;
546 ocname.bv_len = attrs->an_name.bv_len - 1;
547 ocname.bv_val = &attrs->an_name.bv_val[1];
548 oc = oc_bvfind( &ocname );
549 attrs->an_oc_exclude = 0;
550 if ( oc && attrs->an_name.bv_val[0] == '!' ) {
551 attrs->an_oc_exclude = 1;
555 default: /* old (deprecated) way */
556 oc = oc_bvfind( &attrs->an_name );
561 if ( attrs->an_oc_exclude ) {
564 if ( oc == slap_schema.si_oc_extensibleObject ) {
565 /* extensibleObject allows the return of anything */
569 if( oc->soc_required ) {
570 /* allow return of required attributes */
573 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
574 for (a = desc->ad_type; a; a=a->sat_sup) {
575 if ( a == oc->soc_required[i] ) {
582 if( oc->soc_allowed ) {
583 /* allow return of allowed attributes */
585 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
586 for (a = desc->ad_type; a; a=a->sat_sup) {
587 if ( a == oc->soc_allowed[i] ) {
597 if ( oc == slap_schema.si_oc_extensibleObject ) {
598 /* extensibleObject allows the return of anything */
602 if( oc->soc_required ) {
603 /* allow return of required attributes */
606 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
607 for (a = desc->ad_type; a; a=a->sat_sup) {
608 if ( a == oc->soc_required[i] ) {
615 if( oc->soc_allowed ) {
616 /* allow return of allowed attributes */
618 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
619 for (a = desc->ad_type; a; a=a->sat_sup) {
620 if ( a == oc->soc_allowed[i] ) {
628 /* short-circuit this search next time around */
629 if (!slap_schema.si_at_undefined->sat_ad) {
631 slap_bv2undef_ad(&attrs->an_name,
632 &attrs->an_desc, &text);
635 slap_schema.si_at_undefined->sat_ad;
644 int slap_str2undef_ad(
646 AttributeDescription **ad,
650 bv.bv_val = (char *) str;
651 bv.bv_len = strlen( str );
653 return slap_bv2undef_ad( &bv, ad, text );
656 int slap_bv2undef_ad(
658 AttributeDescription **ad,
661 AttributeDescription *desc;
663 assert( ad != NULL );
665 if( bv == NULL || bv->bv_len == 0 ) {
666 *text = "empty attribute description";
667 return LDAP_UNDEFINED_TYPE;
670 /* make sure description is IA5 */
671 if( ad_keystring( bv ) ) {
672 *text = "attribute description contains inappropriate characters";
673 return LDAP_UNDEFINED_TYPE;
676 for( desc = slap_schema.si_at_undefined->sat_ad; desc;
679 if( desc->ad_cname.bv_len == bv->bv_len &&
680 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
687 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
690 desc->ad_flags = SLAP_DESC_NONE;
691 desc->ad_tags.bv_val = NULL;
692 desc->ad_tags.bv_len = 0;
694 desc->ad_cname.bv_len = bv->bv_len;
695 desc->ad_cname.bv_val = (char *)(desc+1);
696 strcpy(desc->ad_cname.bv_val, bv->bv_val);
698 /* canonical to upper case */
699 ldap_pvt_str2upper( desc->ad_cname.bv_val );
701 desc->ad_type = slap_schema.si_at_undefined;
702 desc->ad_next = desc->ad_type->sat_ad;
703 desc->ad_type->sat_ad = desc;
721 if( a == NULL ) return 0;
723 for ( ; a->an_name.bv_val; a++ ) {
724 if ( a->an_name.bv_len != s->bv_len) continue;
725 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
734 * Convert a delimited string into a list of AttributeNames; add
735 * on to an existing list if it was given. If the string is not
736 * a valid attribute name, if a '-' is prepended it is skipped
737 * and the remaining name is tried again; if a '@' (or '+') is
738 * prepended, an objectclass name is searched instead; if a '!'
739 * is prepended, the objectclass name is negated.
741 * NOTE: currently, if a valid attribute name is not found, the
742 * same string is also checked as valid objectclass name; however,
743 * this behavior is deprecated.
746 str2anlist( AttributeName *an, char *in, const char *brkstr )
755 /* find last element in list */
756 for (i = 0; an && an[i].an_name.bv_val; i++);
758 /* protect the input string from strtok */
759 str = ch_strdup( in );
761 /* Count words in string */
763 for ( s = str; *s; s++ ) {
764 if ( strchr( brkstr, *s ) != NULL ) {
769 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
771 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
773 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
775 anew->an_desc = NULL;
777 anew->an_oc_exclude = 0;
778 ber_str2bv(s, 0, 1, &anew->an_name);
779 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
780 if ( !anew->an_desc ) {
781 switch( anew->an_name.bv_val[0] ) {
783 struct berval adname;
784 adname.bv_len = anew->an_name.bv_len - 1;
785 adname.bv_val = &anew->an_name.bv_val[1];
786 slap_bv2ad(&adname, &anew->an_desc, &text);
787 if ( !anew->an_desc ) {
790 * overwrites input string
799 case '+': /* (deprecated) */
801 struct berval ocname;
802 ocname.bv_len = anew->an_name.bv_len - 1;
803 ocname.bv_val = &anew->an_name.bv_val[1];
804 anew->an_oc = oc_bvfind( &ocname );
805 if ( !anew->an_oc ) {
808 * overwrites input string
815 if ( anew->an_name.bv_val[0] == '!' ) {
816 anew->an_oc_exclude = 1;
821 /* old (deprecated) way */
822 anew->an_oc = oc_bvfind( &anew->an_name );
823 if ( !anew->an_oc ) {
825 /* overwrites input string on error! */
834 anew->an_name.bv_val = NULL;
840 /* Define an attribute option. */
842 ad_define_option( const char *name, const char *fname, int lineno )
847 if ( options == &lang_option ) {
856 if ( !DESC_CHAR( name[optlen] ) ) {
858 LDAP_LOG( CONFIG, CRIT,
859 "%s: line %d: illegal option name \"%s\"\n",
860 fname, lineno, name );
862 Debug( LDAP_DEBUG_ANY,
863 "%s: line %d: illegal option name \"%s\"\n",
864 fname, lineno, name );
868 } while ( name[++optlen] );
870 options = ch_realloc( options,
871 (option_count+1) * sizeof(Attr_option) );
873 if ( strcasecmp( name, "binary" ) == 0
874 || ad_find_option_definition( name, optlen ) ) {
876 LDAP_LOG( CONFIG, CRIT,
877 "%s: line %d: option \"%s\" is already defined\n",
878 fname, lineno, name );
880 Debug( LDAP_DEBUG_ANY,
881 "%s: line %d: option \"%s\" is already defined\n",
882 fname, lineno, name );
887 for ( i = option_count; i; --i ) {
888 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
890 options[i] = options[i-1];
893 options[i].name.bv_val = ch_strdup( name );
894 options[i].name.bv_len = optlen;
895 options[i].prefix = (name[optlen-1] == '-');
897 if ( i != option_count &&
899 optlen < options[i+1].name.bv_len &&
900 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
902 LDAP_LOG( CONFIG, CRIT,
903 "%s: line %d: option \"%s\" overrides previous option\n",
904 fname, lineno, name );
906 Debug( LDAP_DEBUG_ANY,
907 "%s: line %d: option \"%s\" overrides previous option\n",
908 fname, lineno, name );
917 /* Find the definition of the option name or prefix matching the arguments */
919 ad_find_option_definition( const char *opt, int optlen )
921 int top = 0, bot = option_count;
922 while ( top < bot ) {
923 int mid = (top + bot) / 2;
924 int mlen = options[mid].name.bv_len;
925 char *mname = options[mid].name.bv_val;
927 if ( optlen < mlen ) {
928 j = strncasecmp( opt, mname, optlen ) - 1;
930 j = strncasecmp( opt, mname, mlen );
931 if ( j==0 && (optlen==mlen || options[mid].prefix) )
932 return &options[mid];
943 AttributeDescription *ad,
946 switch( usage & SLAP_MR_TYPE_MASK ) {
948 case SLAP_MR_EQUALITY:
949 return ad->ad_type->sat_equality;
951 case SLAP_MR_ORDERING:
952 return ad->ad_type->sat_ordering;
955 return ad->ad_type->sat_substr;
959 assert( 0 /* ad_mr: bad usage */);