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 static AttributeName anlist_no_attrs[] = {
32 { BER_BVC( LDAP_NO_ATTRS ), NULL, 0, NULL },
33 { BER_BVNULL, NULL, 0, NULL }
36 static AttributeName anlist_all_user_attributes[] = {
37 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
38 { BER_BVNULL, NULL, 0, NULL }
41 static AttributeName anlist_all_operational_attributes[] = {
42 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
43 { BER_BVNULL, NULL, 0, NULL }
46 static AttributeName anlist_all_attributes[] = {
47 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
48 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
49 { BER_BVNULL, NULL, 0, NULL }
52 AttributeName *slap_anlist_no_attrs = anlist_no_attrs;
53 AttributeName *slap_anlist_all_user_attributes = anlist_all_user_attributes;
54 AttributeName *slap_anlist_all_operational_attributes = anlist_all_operational_attributes;
55 AttributeName *slap_anlist_all_attributes = anlist_all_attributes;
57 typedef struct Attr_option {
58 struct berval name; /* option name or prefix */
59 int prefix; /* NAME is a tag and range prefix */
62 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
64 /* Options sorted by name, and number of options */
65 static Attr_option *options = &lang_option;
66 static int option_count = 1;
68 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
70 static int ad_keystring(
75 if( !AD_LEADCHAR( bv->bv_val[0] ) ) {
79 for( i=1; i<bv->bv_len; i++ ) {
80 if( !AD_CHAR( bv->bv_val[i] ) ) {
87 void ad_destroy( AttributeDescription *ad )
89 AttributeDescription *n;
91 for (; ad != NULL; ad = n) {
97 /* Is there an AttributeDescription for this type that uses these tags? */
98 AttributeDescription * ad_find_tags(
100 struct berval *tags )
102 AttributeDescription *ad;
104 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
105 for (ad = type->sat_ad; ad; ad=ad->ad_next)
107 if (ad->ad_tags.bv_len == tags->bv_len &&
108 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
111 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
117 AttributeDescription **ad,
121 bv.bv_val = (char *) str;
122 bv.bv_len = strlen( str );
124 return slap_bv2ad( &bv, ad, text );
127 static char *strchrlen(
134 for( i=0; p[i]; i++ ) {
137 return (char *) &p[i];
147 AttributeDescription **ad,
150 int rtn = LDAP_UNDEFINED_TYPE;
151 AttributeDescription desc, *d2;
152 char *name, *options;
157 /* hardcoded limits for speed */
158 #define MAX_TAGGING_OPTIONS 128
159 struct berval tags[MAX_TAGGING_OPTIONS+1];
160 #define MAX_TAGS_LEN 1024
161 char tagbuf[MAX_TAGS_LEN];
163 assert( ad != NULL );
164 assert( *ad == NULL ); /* temporary */
166 if( bv == NULL || BER_BVISNULL( bv ) || BER_BVISEMPTY( bv ) ) {
167 *text = "empty AttributeDescription";
171 /* make sure description is IA5 */
172 if( ad_keystring( bv ) ) {
173 *text = "AttributeDescription contains inappropriate characters";
177 /* find valid base attribute type; parse in place */
178 memset( &desc, 0, sizeof( desc ));
181 options = strchr(name, ';');
182 if( options != NULL ) {
183 desc.ad_cname.bv_len = options - name;
185 desc.ad_type = at_bvfind( &desc.ad_cname );
186 if( desc.ad_type == NULL ) {
187 *text = "attribute type undefined";
191 if( is_at_operational( desc.ad_type ) && options != NULL ) {
192 *text = "operational attribute with options undefined";
197 * parse options in place
200 memset( tags, 0, sizeof( tags ));
203 for( opt=options; opt != NULL; opt=next ) {
206 next = strchrlen( opt, ';', &optlen );
209 *text = "zero length option is invalid";
212 } else if ( optlen == sizeof("binary")-1 &&
213 strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
216 if( slap_ad_is_binary( &desc ) ) {
217 *text = "option \"binary\" specified multiple times";
221 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
222 /* not stored in binary, disallow option */
223 *text = "option \"binary\" not supported with type";
227 desc.ad_flags |= SLAP_DESC_BINARY;
230 } else if ( ad_find_option_definition( opt, optlen ) ) {
233 if( opt[optlen-1] == '-' ) {
234 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
237 if( ntags >= MAX_TAGGING_OPTIONS ) {
238 *text = "too many tagging options";
243 * tags should be presented in sorted order,
244 * so run the array in reverse.
246 for( i=ntags-1; i>=0; i-- ) {
249 rc = strncasecmp( opt, tags[i].bv_val,
250 (unsigned) optlen < tags[i].bv_len
251 ? optlen : tags[i].bv_len );
253 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
254 /* duplicate (ignore) */
257 } else if ( rc > 0 ||
258 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
260 AC_MEMCPY( &tags[i+2], &tags[i+1],
261 (ntags-i-1)*sizeof(struct berval) );
262 tags[i+1].bv_val = opt;
263 tags[i+1].bv_len = optlen;
269 AC_MEMCPY( &tags[1], &tags[0],
270 ntags*sizeof(struct berval) );
272 tags[0].bv_val = opt;
273 tags[0].bv_len = optlen;
276 tagslen += optlen + 1;
280 *text = "unrecognized option";
288 if( tagslen > MAX_TAGS_LEN ) {
289 *text = "tagging options too long";
293 desc.ad_tags.bv_val = tagbuf;
296 for( i=0; i<ntags; i++ ) {
297 AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
298 tags[i].bv_val, tags[i].bv_len );
300 tagslen += tags[i].bv_len;
301 desc.ad_tags.bv_val[tagslen++] = ';';
304 desc.ad_tags.bv_val[--tagslen] = '\0';
305 desc.ad_tags.bv_len = tagslen;
308 /* see if a matching description is already cached */
309 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
310 if( d2->ad_flags != desc.ad_flags ) {
313 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
316 if( d2->ad_tags.bv_len == 0 ) {
319 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
320 desc.ad_tags.bv_len ) == 0 )
326 /* Not found, add new one */
329 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
330 /* check again now that we've locked */
331 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
332 if (d2->ad_flags != desc.ad_flags)
334 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
336 if (d2->ad_tags.bv_len == 0)
338 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
339 desc.ad_tags.bv_len) == 0)
343 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
347 /* Allocate a single contiguous block. If there are no
348 * options, we just need space for the AttrDesc structure.
349 * Otherwise, we need to tack on the full name length +
350 * options length, + maybe tagging options length again.
352 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
353 dlen = desc.ad_type->sat_cname.bv_len + 1;
354 if (desc.ad_tags.bv_len) {
355 dlen += 1+desc.ad_tags.bv_len;
357 if( slap_ad_is_binary( &desc ) ) {
358 dlen += sizeof(";binary")+desc.ad_tags.bv_len;
362 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
364 d2->ad_type = desc.ad_type;
365 d2->ad_flags = desc.ad_flags;
366 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
367 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
370 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
371 d2->ad_tags.bv_val = NULL;
375 d2->ad_cname.bv_val = (char *)(d2+1);
376 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
377 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
378 if( slap_ad_is_binary( &desc ) ) {
381 if( desc.ad_tags.bv_len ) {
382 lp = desc.ad_tags.bv_val;
383 while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
384 && (lp = strchr( lp, ';' )) != NULL )
386 if( lp != desc.ad_tags.bv_val ) {
389 ? lp - desc.ad_tags.bv_val - 1
390 : strlen( desc.ad_tags.bv_val ));
391 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
394 cp = lutil_strcopy(cp, ";binary");
397 cp = lutil_strcopy(cp, lp);
399 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
400 if( desc.ad_tags.bv_len )
401 ldap_pvt_str2lower(op);
406 if( desc.ad_tags.bv_len ) {
407 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
410 d2->ad_tags.bv_val = lp;
411 strcpy(lp, desc.ad_tags.bv_val);
412 ldap_pvt_str2lower(lp);
414 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
417 /* Add new desc to list. We always want the bare Desc with
418 * no options to stay at the head of the list, assuming
419 * that one will be used most frequently.
421 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
422 d2->ad_next = desc.ad_type->sat_ad;
423 desc.ad_type->sat_ad = d2;
425 d2->ad_next = desc.ad_type->sat_ad->ad_next;
426 desc.ad_type->sat_ad->ad_next = d2;
428 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
440 static int is_ad_subtags(
441 struct berval *subtagsbv,
442 struct berval *suptagsbv )
444 const char *suptags, *supp, *supdelimp;
445 const char *subtags, *subp, *subdelimp;
448 subtags =subtagsbv->bv_val;
449 suptags =suptagsbv->bv_val;
451 for( supp=suptags ; supp; supp=supdelimp ) {
452 supdelimp = strchrlen( supp, ';', &suplen );
453 if( supdelimp ) supdelimp++;
455 for( subp=subtags ; subp; subp=subdelimp ) {
456 subdelimp = strchrlen( subp, ';', &sublen );
457 if( subdelimp ) subdelimp++;
460 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
461 && strncmp( supp, subp, sublen ) == 0 )
462 : ( ( suplen == sublen || supp[suplen-1] == '-' )
463 && strncmp( supp, subp, suplen ) == 0 ) )
476 AttributeDescription *sub,
477 AttributeDescription *super
483 for ( a = sub->ad_type; a; a=a->sat_sup ) {
484 if ( a == super->ad_type ) break;
490 /* ensure sub does support all flags of super */
491 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
492 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
496 /* check for tagging options */
497 if ( super->ad_tags.bv_len == 0 )
499 if ( sub->ad_tags.bv_len == 0 )
502 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
506 AttributeDescription *desc,
507 AttributeName *attrs )
509 if (! attrs ) return 0;
511 for( ; attrs->an_name.bv_val; attrs++ ) {
516 if ( attrs->an_desc ) {
519 if ( desc == attrs->an_desc ) {
524 * EXTENSION: if requested description is preceeded by
525 * a '-' character, do not match on subtypes.
527 if ( attrs->an_name.bv_val[0] == '-' ) {
531 /* Is this a subtype of the requested attr? */
532 for (a = desc->ad_type; a; a=a->sat_sup) {
533 if ( a == attrs->an_desc->ad_type )
539 /* Does desc support all the requested flags? */
540 lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
541 if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
542 != attrs->an_desc->ad_flags ) {
545 /* Do the descs have compatible tags? */
546 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
549 if ( desc->ad_tags.bv_len == 0) {
552 if ( is_ad_subtags( &desc->ad_tags,
553 &attrs->an_desc->ad_tags ) ) {
560 * EXTENSION: see if requested description is @objectClass
561 * if so, return attributes which the class requires/allows
562 * else if requested description is !objectClass, return
563 * attributes which the class does not require/allow
566 if( oc == NULL && attrs->an_name.bv_val ) {
567 switch( attrs->an_name.bv_val[0] ) {
568 case '@': /* @objectClass */
569 case '+': /* +objectClass (deprecated) */
570 case '!': { /* exclude */
571 struct berval ocname;
572 ocname.bv_len = attrs->an_name.bv_len - 1;
573 ocname.bv_val = &attrs->an_name.bv_val[1];
574 oc = oc_bvfind( &ocname );
575 attrs->an_oc_exclude = 0;
576 if ( oc && attrs->an_name.bv_val[0] == '!' ) {
577 attrs->an_oc_exclude = 1;
581 default: /* old (deprecated) way */
582 oc = oc_bvfind( &attrs->an_name );
587 if ( attrs->an_oc_exclude ) {
590 if ( oc == slap_schema.si_oc_extensibleObject ) {
591 /* extensibleObject allows the return of anything */
595 if( oc->soc_required ) {
596 /* allow return of required attributes */
599 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
600 for (a = desc->ad_type; a; a=a->sat_sup) {
601 if ( a == oc->soc_required[i] ) {
608 if( oc->soc_allowed ) {
609 /* allow return of allowed attributes */
611 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
612 for (a = desc->ad_type; a; a=a->sat_sup) {
613 if ( a == oc->soc_allowed[i] ) {
623 if ( oc == slap_schema.si_oc_extensibleObject ) {
624 /* extensibleObject allows the return of anything */
628 if( oc->soc_required ) {
629 /* allow return of required attributes */
632 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
633 for (a = desc->ad_type; a; a=a->sat_sup) {
634 if ( a == oc->soc_required[i] ) {
641 if( oc->soc_allowed ) {
642 /* allow return of allowed attributes */
644 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
645 for (a = desc->ad_type; a; a=a->sat_sup) {
646 if ( a == oc->soc_allowed[i] ) {
654 /* short-circuit this search next time around */
655 if (!slap_schema.si_at_undefined->sat_ad) {
657 slap_bv2undef_ad(&attrs->an_name,
658 &attrs->an_desc, &text);
661 slap_schema.si_at_undefined->sat_ad;
670 int slap_str2undef_ad(
672 AttributeDescription **ad,
676 bv.bv_val = (char *) str;
677 bv.bv_len = strlen( str );
679 return slap_bv2undef_ad( &bv, ad, text );
682 int slap_bv2undef_ad(
684 AttributeDescription **ad,
687 AttributeDescription *desc;
689 assert( ad != NULL );
691 if( bv == NULL || bv->bv_len == 0 ) {
692 *text = "empty AttributeDescription";
693 return LDAP_UNDEFINED_TYPE;
696 /* make sure description is IA5 */
697 if( ad_keystring( bv ) ) {
698 *text = "AttributeDescription contains inappropriate characters";
699 return LDAP_UNDEFINED_TYPE;
702 for( desc = slap_schema.si_at_undefined->sat_ad; desc;
705 if( desc->ad_cname.bv_len == bv->bv_len &&
706 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
713 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
716 desc->ad_flags = SLAP_DESC_NONE;
717 desc->ad_tags.bv_val = NULL;
718 desc->ad_tags.bv_len = 0;
720 desc->ad_cname.bv_len = bv->bv_len;
721 desc->ad_cname.bv_val = (char *)(desc+1);
722 strcpy(desc->ad_cname.bv_val, bv->bv_val);
724 /* canonical to upper case */
725 ldap_pvt_str2upper( desc->ad_cname.bv_val );
727 desc->ad_type = slap_schema.si_at_undefined;
728 desc->ad_next = desc->ad_type->sat_ad;
729 desc->ad_type->sat_ad = desc;
747 if( a == NULL ) return 0;
749 for ( ; a->an_name.bv_val; a++ ) {
750 if ( a->an_name.bv_len != s->bv_len) continue;
751 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
760 * Convert a delimited string into a list of AttributeNames; add
761 * on to an existing list if it was given. If the string is not
762 * a valid attribute name, if a '-' is prepended it is skipped
763 * and the remaining name is tried again; if a '@' (or '+') is
764 * prepended, an objectclass name is searched instead; if a '!'
765 * is prepended, the objectclass name is negated.
767 * NOTE: currently, if a valid attribute name is not found, the
768 * same string is also checked as valid objectclass name; however,
769 * this behavior is deprecated.
772 str2anlist( AttributeName *an, char *in, const char *brkstr )
781 /* find last element in list */
782 for (i = 0; an && an[i].an_name.bv_val; i++);
784 /* protect the input string from strtok */
785 str = ch_strdup( in );
787 /* Count words in string */
789 for ( s = str; *s; s++ ) {
790 if ( strchr( brkstr, *s ) != NULL ) {
795 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
797 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
799 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
801 anew->an_desc = NULL;
803 anew->an_oc_exclude = 0;
804 ber_str2bv(s, 0, 1, &anew->an_name);
805 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
806 if ( !anew->an_desc ) {
807 switch( anew->an_name.bv_val[0] ) {
809 struct berval adname;
810 adname.bv_len = anew->an_name.bv_len - 1;
811 adname.bv_val = &anew->an_name.bv_val[1];
812 slap_bv2ad(&adname, &anew->an_desc, &text);
813 if ( !anew->an_desc ) {
816 * overwrites input string
825 case '+': /* (deprecated) */
827 struct berval ocname;
828 ocname.bv_len = anew->an_name.bv_len - 1;
829 ocname.bv_val = &anew->an_name.bv_val[1];
830 anew->an_oc = oc_bvfind( &ocname );
831 if ( !anew->an_oc ) {
834 * overwrites input string
841 if ( anew->an_name.bv_val[0] == '!' ) {
842 anew->an_oc_exclude = 1;
847 /* old (deprecated) way */
848 anew->an_oc = oc_bvfind( &anew->an_name );
849 if ( !anew->an_oc ) {
851 /* overwrites input string on error! */
860 anew->an_name.bv_val = NULL;
866 /* Define an attribute option. */
868 ad_define_option( const char *name, const char *fname, int lineno )
873 if ( options == &lang_option ) {
882 if ( !DESC_CHAR( name[optlen] ) ) {
884 LDAP_LOG( CONFIG, CRIT,
885 "%s: line %d: illegal option name \"%s\"\n",
886 fname, lineno, name );
888 Debug( LDAP_DEBUG_ANY,
889 "%s: line %d: illegal option name \"%s\"\n",
890 fname, lineno, name );
894 } while ( name[++optlen] );
896 options = ch_realloc( options,
897 (option_count+1) * sizeof(Attr_option) );
899 if ( strcasecmp( name, "binary" ) == 0
900 || ad_find_option_definition( name, optlen ) ) {
902 LDAP_LOG( CONFIG, CRIT,
903 "%s: line %d: option \"%s\" is already defined\n",
904 fname, lineno, name );
906 Debug( LDAP_DEBUG_ANY,
907 "%s: line %d: option \"%s\" is already defined\n",
908 fname, lineno, name );
913 for ( i = option_count; i; --i ) {
914 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
916 options[i] = options[i-1];
919 options[i].name.bv_val = ch_strdup( name );
920 options[i].name.bv_len = optlen;
921 options[i].prefix = (name[optlen-1] == '-');
923 if ( i != option_count &&
925 optlen < options[i+1].name.bv_len &&
926 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
928 LDAP_LOG( CONFIG, CRIT,
929 "%s: line %d: option \"%s\" overrides previous option\n",
930 fname, lineno, name );
932 Debug( LDAP_DEBUG_ANY,
933 "%s: line %d: option \"%s\" overrides previous option\n",
934 fname, lineno, name );
943 /* Find the definition of the option name or prefix matching the arguments */
945 ad_find_option_definition( const char *opt, int optlen )
947 int top = 0, bot = option_count;
948 while ( top < bot ) {
949 int mid = (top + bot) / 2;
950 int mlen = options[mid].name.bv_len;
951 char *mname = options[mid].name.bv_val;
953 if ( optlen < mlen ) {
954 j = strncasecmp( opt, mname, optlen ) - 1;
956 j = strncasecmp( opt, mname, mlen );
957 if ( j==0 && (optlen==mlen || options[mid].prefix) )
958 return &options[mid];
969 AttributeDescription *ad,
972 switch( usage & SLAP_MR_TYPE_MASK ) {
974 case SLAP_MR_EQUALITY:
975 return ad->ad_type->sat_equality;
977 case SLAP_MR_ORDERING:
978 return ad->ad_type->sat_ordering;
981 return ad->ad_type->sat_substr;
985 assert( 0 /* ad_mr: bad usage */);