1 /* ad.c - routines for dealing with attribute descriptions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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
538 if( oc == NULL && attrs->an_name.bv_val ) {
539 switch( attrs->an_name.bv_val[0] ) {
540 case '@': /* @objectClass */
541 case '+': /* +objectClass (deprecated) */
543 struct berval ocname;
544 ocname.bv_len = attrs->an_name.bv_len - 1;
545 ocname.bv_val = &attrs->an_name.bv_val[1];
546 oc = oc_bvfind( &ocname );
548 default: /* old (deprecated) way */
549 oc = oc_bvfind( &attrs->an_name );
554 if ( oc == slap_schema.si_oc_extensibleObject ) {
555 /* extensibleObject allows the return of anything */
559 if( oc->soc_required ) {
560 /* allow return of required attributes */
563 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
564 for (a = desc->ad_type; a; a=a->sat_sup) {
565 if ( a == oc->soc_required[i] ) {
572 if( oc->soc_allowed ) {
573 /* allow return of allowed attributes */
575 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
576 for (a = desc->ad_type; a; a=a->sat_sup) {
577 if ( a == oc->soc_allowed[i] ) {
585 /* short-circuit this search next time around */
586 if (!slap_schema.si_at_undefined->sat_ad) {
588 slap_bv2undef_ad(&attrs->an_name,
589 &attrs->an_desc, &text);
592 slap_schema.si_at_undefined->sat_ad;
601 int slap_str2undef_ad(
603 AttributeDescription **ad,
607 bv.bv_val = (char *) str;
608 bv.bv_len = strlen( str );
610 return slap_bv2undef_ad( &bv, ad, text );
613 int slap_bv2undef_ad(
615 AttributeDescription **ad,
618 AttributeDescription *desc;
620 assert( ad != NULL );
622 if( bv == NULL || bv->bv_len == 0 ) {
623 *text = "empty attribute description";
624 return LDAP_UNDEFINED_TYPE;
627 /* make sure description is IA5 */
628 if( ad_keystring( bv ) ) {
629 *text = "attribute description contains inappropriate characters";
630 return LDAP_UNDEFINED_TYPE;
633 for( desc = slap_schema.si_at_undefined->sat_ad; desc;
636 if( desc->ad_cname.bv_len == bv->bv_len &&
637 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
644 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
647 desc->ad_flags = SLAP_DESC_NONE;
648 desc->ad_tags.bv_val = NULL;
649 desc->ad_tags.bv_len = 0;
651 desc->ad_cname.bv_len = bv->bv_len;
652 desc->ad_cname.bv_val = (char *)(desc+1);
653 strcpy(desc->ad_cname.bv_val, bv->bv_val);
655 /* canonical to upper case */
656 ldap_pvt_str2upper( desc->ad_cname.bv_val );
658 desc->ad_type = slap_schema.si_at_undefined;
659 desc->ad_next = desc->ad_type->sat_ad;
660 desc->ad_type->sat_ad = desc;
678 if( a == NULL ) return 0;
680 for ( ; a->an_name.bv_val; a++ ) {
681 if ( a->an_name.bv_len != s->bv_len) continue;
682 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
691 * Convert a delimited string into a list of AttributeNames; add
692 * on to an existing list if it was given. If the string is not
693 * a valid attribute name, if a '-' is prepended it is skipped
694 * and the remaining name is tried again; if a '@' (or '+') is
695 * prepended, an objectclass name is searched instead.
697 * NOTE: currently, if a valid attribute name is not found, the
698 * same string is also checked as valid objectclass name; however,
699 * this behavior is deprecated.
702 str2anlist( AttributeName *an, char *in, const char *brkstr )
711 /* find last element in list */
712 for (i = 0; an && an[i].an_name.bv_val; i++);
714 /* protect the input string from strtok */
715 str = ch_strdup( in );
717 /* Count words in string */
719 for ( s = str; *s; s++ ) {
720 if ( strchr( brkstr, *s ) != NULL ) {
725 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
727 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
729 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
731 anew->an_desc = NULL;
733 ber_str2bv(s, 0, 1, &anew->an_name);
734 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
735 if ( !anew->an_desc ) {
736 switch( anew->an_name.bv_val[0] ) {
738 struct berval adname;
739 adname.bv_len = anew->an_name.bv_len - 1;
740 adname.bv_val = &anew->an_name.bv_val[1];
741 slap_bv2ad(&adname, &anew->an_desc, &text);
742 if ( !anew->an_desc ) {
745 * overwrites input string
754 case '+': /* (deprecated) */
756 struct berval ocname;
757 ocname.bv_len = anew->an_name.bv_len - 1;
758 ocname.bv_val = &anew->an_name.bv_val[1];
759 anew->an_oc = oc_bvfind( &ocname );
760 if ( !anew->an_oc ) {
763 * overwrites input string
772 /* old (deprecated) way */
773 anew->an_oc = oc_bvfind( &anew->an_name );
774 if ( !anew->an_oc ) {
776 /* overwrites input string on error! */
785 anew->an_name.bv_val = NULL;
791 /* Define an attribute option. */
793 ad_define_option( const char *name, const char *fname, int lineno )
798 if ( options == &lang_option ) {
807 if ( !DESC_CHAR( name[optlen] ) ) {
809 LDAP_LOG( CONFIG, CRIT,
810 "%s: line %d: illegal option name \"%s\"\n",
811 fname, lineno, name );
813 Debug( LDAP_DEBUG_ANY,
814 "%s: line %d: illegal option name \"%s\"\n",
815 fname, lineno, name );
819 } while ( name[++optlen] );
821 options = ch_realloc( options,
822 (option_count+1) * sizeof(Attr_option) );
824 if ( strcasecmp( name, "binary" ) == 0
825 || ad_find_option_definition( name, optlen ) ) {
827 LDAP_LOG( CONFIG, CRIT,
828 "%s: line %d: option \"%s\" is already defined\n",
829 fname, lineno, name );
831 Debug( LDAP_DEBUG_ANY,
832 "%s: line %d: option \"%s\" is already defined\n",
833 fname, lineno, name );
838 for ( i = option_count; i; --i ) {
839 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
841 options[i] = options[i-1];
844 options[i].name.bv_val = ch_strdup( name );
845 options[i].name.bv_len = optlen;
846 options[i].prefix = (name[optlen-1] == '-');
848 if ( i != option_count &&
850 optlen < options[i+1].name.bv_len &&
851 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
853 LDAP_LOG( CONFIG, CRIT,
854 "%s: line %d: option \"%s\" overrides previous option\n",
855 fname, lineno, name );
857 Debug( LDAP_DEBUG_ANY,
858 "%s: line %d: option \"%s\" overrides previous option\n",
859 fname, lineno, name );
868 /* Find the definition of the option name or prefix matching the arguments */
870 ad_find_option_definition( const char *opt, int optlen )
872 int top = 0, bot = option_count;
873 while ( top < bot ) {
874 int mid = (top + bot) / 2;
875 int mlen = options[mid].name.bv_len;
876 char *mname = options[mid].name.bv_val;
878 if ( optlen < mlen ) {
879 j = strncasecmp( opt, mname, optlen ) - 1;
881 j = strncasecmp( opt, mname, mlen );
882 if ( j==0 && (optlen==mlen || options[mid].prefix) )
883 return &options[mid];
894 AttributeDescription *ad,
897 switch( usage & SLAP_MR_TYPE_MASK ) {
899 case SLAP_MR_EQUALITY:
900 return ad->ad_type->sat_equality;
902 case SLAP_MR_ORDERING:
903 return ad->ad_type->sat_ordering;
906 return ad->ad_type->sat_substr;
910 assert( 0 /* ad_mr: bad usage */);