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>
21 typedef struct Attr_option {
22 struct berval name; /* option name or prefix */
23 int prefix; /* NAME is a tag and range prefix */
26 static Attr_option lang_option = { { sizeof("lang-")-1, "lang-" }, 1 };
28 /* Options sorted by name, and number of options */
29 static Attr_option *options = &lang_option;
30 static int option_count = 1;
32 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
34 static int ad_keystring(
39 if( !AD_CHAR( bv->bv_val[0] ) ) {
43 for( i=1; i<bv->bv_len; i++ ) {
44 if( !AD_CHAR( bv->bv_val[i] ) ) {
51 void ad_destroy( AttributeDescription *ad )
53 AttributeDescription *n;
55 for (; ad != NULL; ad = n) {
61 /* Is there an AttributeDescription for this type that uses these tags? */
62 AttributeDescription * ad_find_tags(
66 AttributeDescription *ad;
68 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
69 for (ad = type->sat_ad; ad; ad=ad->ad_next)
71 if (ad->ad_tags.bv_len == tags->bv_len &&
72 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
75 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
81 AttributeDescription **ad,
85 bv.bv_val = (char *) str;
86 bv.bv_len = strlen( str );
88 return slap_bv2ad( &bv, ad, text );
91 static char *strchrlen(
98 for( i=0; p[i]; i++ ) {
101 return (char *) &p[i];
111 AttributeDescription **ad,
114 int rtn = LDAP_UNDEFINED_TYPE;
115 AttributeDescription desc, *d2;
116 char *name, *options;
121 /* hardcoded limits for speed */
122 #define MAX_TAGGING_OPTIONS 128
123 struct berval tags[MAX_TAGGING_OPTIONS+1];
124 #define MAX_TAGS_LEN 1024
125 char tagbuf[MAX_TAGS_LEN];
127 assert( ad != NULL );
128 assert( *ad == NULL ); /* temporary */
130 if( bv == NULL || bv->bv_len == 0 ) {
131 *text = "empty attribute description";
135 /* make sure description is IA5 */
136 if( ad_keystring( bv ) ) {
137 *text = "attribute description contains inappropriate characters";
141 /* find valid base attribute type; parse in place */
142 memset( &desc, 0, sizeof( desc ));
145 options = strchr(name, ';');
146 if( options != NULL ) {
147 desc.ad_cname.bv_len = options - name;
149 desc.ad_type = at_bvfind( &desc.ad_cname );
150 if( desc.ad_type == NULL ) {
151 *text = "attribute type undefined";
155 if( is_at_operational( desc.ad_type ) && options != NULL ) {
156 *text = "operational attribute with options undefined";
161 * parse options in place
164 memset( tags, 0, sizeof( tags ));
167 for( opt=options; opt != NULL; opt=next ) {
170 next = strchrlen( opt, ';', &optlen );
173 *text = "zero length option is invalid";
176 } else if ( optlen == sizeof("binary")-1 &&
177 strncasecmp( opt, "binary", sizeof("binary")-1 ) == 0 )
180 if( slap_ad_is_binary( &desc ) ) {
181 *text = "option \"binary\" specified multiple times";
185 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
186 /* not stored in binary, disallow option */
187 *text = "option \"binary\" not supported with type";
191 desc.ad_flags |= SLAP_DESC_BINARY;
194 } 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+1], &tags[i],
226 (ntags-i)*sizeof(struct berval) );
227 tags[i].bv_val = opt;
228 tags[i].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);
328 d2->ad_type = desc.ad_type;
329 d2->ad_flags = desc.ad_flags;
330 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
331 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
334 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
335 d2->ad_tags.bv_val = NULL;
339 d2->ad_cname.bv_val = (char *)(d2+1);
340 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
341 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
342 if( slap_ad_is_binary( &desc ) ) {
345 if( desc.ad_tags.bv_len ) {
346 lp = desc.ad_tags.bv_val;
347 while( strncasecmp(lp, "binary", sizeof("binary")-1) < 0
348 && (lp = strchr( lp, ';' )) != NULL )
350 if( lp != desc.ad_tags.bv_val ) {
353 ? lp - desc.ad_tags.bv_val - 1
354 : strlen( desc.ad_tags.bv_val ));
355 strncpy(cp, desc.ad_tags.bv_val, j);
359 strcpy(cp, ";binary");
360 cp += sizeof(";binary")-1;
366 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
367 if( desc.ad_tags.bv_len )
368 ldap_pvt_str2lower(op);
373 if( desc.ad_tags.bv_len ) {
374 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
377 d2->ad_tags.bv_val = lp;
378 strcpy(lp, desc.ad_tags.bv_val);
379 ldap_pvt_str2lower(lp);
381 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
384 /* Add new desc to list. We always want the bare Desc with
385 * no options to stay at the head of the list, assuming
386 * that one will be used most frequently.
388 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
389 d2->ad_next = desc.ad_type->sat_ad;
390 desc.ad_type->sat_ad = d2;
392 d2->ad_next = desc.ad_type->sat_ad->ad_next;
393 desc.ad_type->sat_ad->ad_next = d2;
395 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
407 static int is_ad_subtags(
408 struct berval *subtagsbv,
409 struct berval *suptagsbv )
411 const char *suptags, *supp, *supdelimp;
412 const char *subtags, *subp, *subdelimp;
415 if( suptagsbv->bv_len == 0 ) return 1;
416 if( subtagsbv->bv_len == 0 ) return 0;
418 subtags =subtagsbv->bv_val;
419 suptags =suptagsbv->bv_val;
421 for( supp=suptags ; supp; supp=supdelimp ) {
422 supdelimp = strchrlen( supp, ';', &suplen );
423 if( supdelimp ) supdelimp++;
425 for( subp=subtags ; subp; subp=subdelimp ) {
426 subdelimp = strchrlen( subp, ';', &sublen );
427 if( subdelimp ) subdelimp++;
430 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
431 && strncmp( supp, subp, sublen ) == 0 )
432 : ( ( suplen == sublen || supp[suplen-1] == '-' )
433 && strncmp( supp, subp, suplen ) == 0 ) )
446 AttributeDescription *sub,
447 AttributeDescription *super
452 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
456 /* ensure sub does support all flags of super */
457 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
458 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
462 /* check for tagging options */
463 if ( !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++ ) {
480 if ( attrs->an_desc ) {
481 if ( desc == attrs->an_desc ) {
486 * EXTENSION: if requested description is preceeded by an
487 * a '-' character, do not match on subtypes.
489 if ( attrs->an_name.bv_val[0] != '-' &&
490 is_ad_subtype( desc, attrs->an_desc ))
499 * EXTENSION: see if requested description is +objectClass
500 * if so, return attributes which the class requires/allows
503 if( oc == NULL && attrs->an_name.bv_val ) {
504 switch( attrs->an_name.bv_val[0] ) {
505 case '+': { /* new way */
506 struct berval ocname;
507 ocname.bv_len = attrs->an_name.bv_len - 1;
508 ocname.bv_val = &attrs->an_name.bv_val[1];
509 oc = oc_bvfind( &ocname );
511 default: /* old (deprecated) way */
512 oc = oc_bvfind( &attrs->an_name );
517 if ( oc == slap_schema.si_oc_extensibleObject ) {
518 /* extensibleObject allows the return of anything */
522 if( oc->soc_required ) {
523 /* allow return of required attributes */
525 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
526 rc = is_at_subtype( desc->ad_type,
527 oc->soc_required[i] );
532 if( oc->soc_allowed ) {
533 /* allow return of allowed attributes */
535 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
536 rc = is_at_subtype( desc->ad_type,
537 oc->soc_allowed[i] );
543 /* short-circuit this search next time around */
544 if (!slap_schema.si_at_undefined->sat_ad) {
546 slap_bv2undef_ad(&attrs->an_name,
547 &attrs->an_desc, &text);
550 slap_schema.si_at_undefined->sat_ad;
559 int slap_str2undef_ad(
561 AttributeDescription **ad,
565 bv.bv_val = (char *) str;
566 bv.bv_len = strlen( str );
568 return slap_bv2undef_ad( &bv, ad, text );
571 int slap_bv2undef_ad(
573 AttributeDescription **ad,
576 AttributeDescription *desc;
578 assert( ad != NULL );
580 if( bv == NULL || bv->bv_len == 0 ) {
581 *text = "empty attribute description";
582 return LDAP_UNDEFINED_TYPE;
585 /* make sure description is IA5 */
586 if( ad_keystring( bv ) ) {
587 *text = "attribute description contains inappropriate characters";
588 return LDAP_UNDEFINED_TYPE;
591 for( desc = slap_schema.si_at_undefined->sat_ad; desc;
594 if( desc->ad_cname.bv_len == bv->bv_len &&
595 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ))
602 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
605 desc->ad_flags = SLAP_DESC_NONE;
606 desc->ad_tags.bv_val = NULL;
607 desc->ad_tags.bv_len = 0;
609 desc->ad_cname.bv_len = bv->bv_len;
610 desc->ad_cname.bv_val = (char *)(desc+1);
611 strcpy(desc->ad_cname.bv_val, bv->bv_val);
613 /* canonical to upper case */
614 ldap_pvt_str2upper( desc->ad_cname.bv_val );
616 desc->ad_type = slap_schema.si_at_undefined;
617 desc->ad_next = desc->ad_type->sat_ad;
618 desc->ad_type->sat_ad = desc;
636 if( a == NULL ) return 0;
638 for ( ; a->an_name.bv_val; a++ ) {
639 if ( a->an_name.bv_len != s->bv_len) continue;
640 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
649 * Convert a delimited string into a list of AttributeNames;
650 * add on to an existing list if it was given. If the string
651 * is not a valid attribute name, if a '-' is prepended it is
652 * skipped and the remaining name is tried again; if a '+' is
653 * prepended, an objectclass name is searched instead.
655 * NOTE: currently, if a valid attribute name is not found,
656 * the same string is also checked as valid objectclass name;
657 * however, this behavior is deprecated.
660 str2anlist( AttributeName *an, char *in, const char *brkstr )
669 /* find last element in list */
670 for (i = 0; an && an[i].an_name.bv_val; i++);
672 /* protect the input string from strtok */
673 str = ch_strdup( in );
675 /* Count words in string */
677 for ( s = str; *s; s++ ) {
678 if ( strchr( brkstr, *s ) != NULL ) {
683 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
685 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
687 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
689 anew->an_desc = NULL;
691 ber_str2bv(s, 0, 1, &anew->an_name);
692 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
693 if ( !anew->an_desc ) {
694 switch( anew->an_name.bv_val[0] ) {
696 struct berval adname;
697 adname.bv_len = anew->an_name.bv_len - 1;
698 adname.bv_val = &anew->an_name.bv_val[1];
699 slap_bv2ad(&adname, &anew->an_desc, &text);
700 if ( !anew->an_desc ) {
703 * overwrites input string
712 struct berval ocname;
713 ocname.bv_len = anew->an_name.bv_len - 1;
714 ocname.bv_val = &anew->an_name.bv_val[1];
715 anew->an_oc = oc_bvfind( &ocname );
716 if ( !anew->an_oc ) {
719 * overwrites input string
728 /* old (deprecated) way */
729 anew->an_oc = oc_bvfind( &anew->an_name );
730 if ( !anew->an_oc ) {
732 /* overwrites input string on error! */
741 anew->an_name.bv_val = NULL;
747 /* Define an attribute option. */
749 ad_define_option( const char *name, const char *fname, int lineno )
751 int i, j, len, optlen;
753 if ( options == &lang_option ) {
762 if ( !DESC_CHAR( name[optlen] ) ) {
764 LDAP_LOG( CONFIG, CRIT,
765 "%s: line %d: illegal option name \"%s\"\n",
766 fname, lineno, name );
768 Debug( LDAP_DEBUG_ANY,
769 "%s: line %d: illegal option name \"%s\"\n",
770 fname, lineno, name );
774 } while ( name[++optlen] );
776 options = ch_realloc( options,
777 (option_count+1) * sizeof(Attr_option) );
779 if ( strcasecmp( name, "binary" ) == 0
780 || ad_find_option_definition( name, optlen ) ) {
782 LDAP_LOG( CONFIG, CRIT,
783 "%s: line %d: option \"%s\" is already defined\n",
784 fname, lineno, name );
786 Debug( LDAP_DEBUG_ANY,
787 "%s: line %d: option \"%s\" is already defined\n",
788 fname, lineno, name );
793 for ( i = option_count; i; --i ) {
794 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
796 options[i] = options[i-1];
799 options[i].name.bv_val = ch_strdup( name );
800 options[i].name.bv_len = optlen;
801 options[i].prefix = (name[optlen-1] == '-');
803 if ( i != option_count &&
805 optlen < options[i+1].name.bv_len &&
806 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
808 LDAP_LOG( CONFIG, CRIT,
809 "%s: line %d: option \"%s\" overrides previous option\n",
810 fname, lineno, name );
812 Debug( LDAP_DEBUG_ANY,
813 "%s: line %d: option \"%s\" overrides previous option\n",
814 fname, lineno, name );
823 /* Find the definition of the option name or prefix matching the arguments */
825 ad_find_option_definition( const char *opt, int optlen )
827 int top = 0, bot = option_count;
828 while ( top < bot ) {
829 int mid = (top + bot) / 2;
830 int mlen = options[mid].name.bv_len;
831 char *mname = options[mid].name.bv_val;
833 if ( optlen < mlen ) {
834 j = strncasecmp( opt, mname, optlen ) - 1;
836 j = strncasecmp( opt, mname, mlen );
837 if ( j==0 && (optlen==mlen || options[mid].prefix) )
838 return &options[mid];