1 /* ad.c - routines for dealing with attribute descriptions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 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>
30 static struct berval bv_no_attrs = BER_BVC( LDAP_NO_ATTRS );
31 static struct berval bv_all_user_attrs = BER_BVC( "*" );
32 static struct berval bv_all_operational_attrs = BER_BVC( "+" );
34 static AttributeName anlist_no_attrs[] = {
35 { BER_BVC( LDAP_NO_ATTRS ), NULL, 0, NULL },
36 { BER_BVNULL, NULL, 0, NULL }
39 static AttributeName anlist_all_user_attributes[] = {
40 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
41 { BER_BVNULL, NULL, 0, NULL }
44 static AttributeName anlist_all_operational_attributes[] = {
45 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
46 { BER_BVNULL, NULL, 0, NULL }
49 static AttributeName anlist_all_attributes[] = {
50 { BER_BVC( LDAP_ALL_USER_ATTRIBUTES ), NULL, 0, NULL },
51 { BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ), NULL, 0, NULL },
52 { BER_BVNULL, NULL, 0, NULL }
55 AttributeName *slap_anlist_no_attrs = anlist_no_attrs;
56 AttributeName *slap_anlist_all_user_attributes = anlist_all_user_attributes;
57 AttributeName *slap_anlist_all_operational_attributes = anlist_all_operational_attributes;
58 AttributeName *slap_anlist_all_attributes = anlist_all_attributes;
60 struct berval * slap_bv_no_attrs = &bv_no_attrs;
61 struct berval * slap_bv_all_user_attrs = &bv_all_user_attrs;
62 struct berval * slap_bv_all_operational_attrs = &bv_all_operational_attrs;
64 typedef struct Attr_option {
65 struct berval name; /* option name or prefix */
66 int prefix; /* NAME is a tag and range prefix */
69 static Attr_option lang_option = { BER_BVC("lang-"), 1 };
71 /* Options sorted by name, and number of options */
72 static Attr_option *options = &lang_option;
73 static int option_count = 1;
75 static int msad_range_hack = 0;
79 static Attr_option *ad_find_option_definition( const char *opt, int optlen );
86 if( !AD_LEADCHAR( bv->bv_val[0] ) ) {
90 for( i=1; i<bv->bv_len; i++ ) {
91 if( !AD_CHAR( bv->bv_val[i] )) {
92 if ( msad_range_hack && bv->bv_val[i] == '=' )
100 void ad_destroy( AttributeDescription *ad )
102 AttributeDescription *n;
104 for (; ad != NULL; ad = n) {
110 /* Is there an AttributeDescription for this type that uses these tags? */
111 AttributeDescription * ad_find_tags(
113 struct berval *tags )
115 AttributeDescription *ad;
117 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
118 for (ad = type->sat_ad; ad; ad=ad->ad_next)
120 if (ad->ad_tags.bv_len == tags->bv_len &&
121 !strcasecmp(ad->ad_tags.bv_val, tags->bv_val))
124 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
130 AttributeDescription **ad,
134 bv.bv_val = (char *) str;
135 bv.bv_len = strlen( str );
137 return slap_bv2ad( &bv, ad, text );
140 static char *strchrlen(
148 for( p=beg; *p && p < end; p++ ) {
161 AttributeDescription **ad,
164 int rtn = LDAP_UNDEFINED_TYPE;
165 AttributeDescription desc, *d2;
166 char *name, *options, *optn;
171 /* hardcoded limits for speed */
172 #define MAX_TAGGING_OPTIONS 128
173 struct berval tags[MAX_TAGGING_OPTIONS+1];
174 #define MAX_TAGS_LEN 1024
175 char tagbuf[MAX_TAGS_LEN];
177 assert( ad != NULL );
178 assert( *ad == NULL ); /* temporary */
180 if( bv == NULL || BER_BVISNULL( bv ) || BER_BVISEMPTY( bv ) ) {
181 *text = "empty AttributeDescription";
185 /* make sure description is IA5 */
186 if( ad_keystring( bv ) ) {
187 *text = "AttributeDescription contains inappropriate characters";
191 /* find valid base attribute type; parse in place */
194 BER_BVZERO( &desc.ad_tags );
196 options = ber_bvchr( bv, ';' );
197 if ( options != NULL && (unsigned) ( options - name ) < bv->bv_len ) {
198 /* don't go past the end of the berval! */
199 desc.ad_cname.bv_len = options - name;
203 desc.ad_type = at_bvfind( &desc.ad_cname );
204 if( desc.ad_type == NULL ) {
205 *text = "attribute type undefined";
209 if( is_at_operational( desc.ad_type ) && options != NULL ) {
210 *text = "operational attribute with options undefined";
215 * parse options in place
219 optn = bv->bv_val + bv->bv_len;
221 for( opt=options; opt != NULL; opt=next ) {
224 next = strchrlen( opt, optn, ';', &optlen );
227 *text = "zero length option is invalid";
230 } else if ( optlen == STRLENOF("binary") &&
231 strncasecmp( opt, "binary", STRLENOF("binary") ) == 0 )
234 if( slap_ad_is_binary( &desc ) ) {
235 *text = "option \"binary\" specified multiple times";
239 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
240 /* not stored in binary, disallow option */
241 *text = "option \"binary\" not supported with type";
245 desc.ad_flags |= SLAP_DESC_BINARY;
248 } else if ( ad_find_option_definition( opt, optlen ) ) {
251 if( opt[optlen-1] == '-' ||
252 ( opt[optlen-1] == '=' && msad_range_hack )) {
253 desc.ad_flags |= SLAP_DESC_TAG_RANGE;
256 if( ntags >= MAX_TAGGING_OPTIONS ) {
257 *text = "too many tagging options";
262 * tags should be presented in sorted order,
263 * so run the array in reverse.
265 for( i=ntags-1; i>=0; i-- ) {
268 rc = strncasecmp( opt, tags[i].bv_val,
269 (unsigned) optlen < tags[i].bv_len
270 ? (unsigned) optlen : tags[i].bv_len );
272 if( rc == 0 && (unsigned)optlen == tags[i].bv_len ) {
273 /* duplicate (ignore) */
277 } else if ( rc > 0 ||
278 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
280 AC_MEMCPY( &tags[i+2], &tags[i+1],
281 (ntags-i-1)*sizeof(struct berval) );
282 tags[i+1].bv_val = opt;
283 tags[i+1].bv_len = optlen;
289 AC_MEMCPY( &tags[1], &tags[0],
290 ntags*sizeof(struct berval) );
292 tags[0].bv_val = opt;
293 tags[0].bv_len = optlen;
296 tagslen += optlen + 1;
300 *text = "unrecognized option";
308 if( tagslen > MAX_TAGS_LEN ) {
309 *text = "tagging options too long";
313 desc.ad_tags.bv_val = tagbuf;
316 for( i=0; i<ntags; i++ ) {
317 AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
318 tags[i].bv_val, tags[i].bv_len );
320 tagslen += tags[i].bv_len;
321 desc.ad_tags.bv_val[tagslen++] = ';';
324 desc.ad_tags.bv_val[--tagslen] = '\0';
325 desc.ad_tags.bv_len = tagslen;
328 /* see if a matching description is already cached */
329 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
330 if( d2->ad_flags != desc.ad_flags ) {
333 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
336 if( d2->ad_tags.bv_len == 0 ) {
339 if( strncasecmp( d2->ad_tags.bv_val, desc.ad_tags.bv_val,
340 desc.ad_tags.bv_len ) == 0 )
346 /* Not found, add new one */
349 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
350 /* check again now that we've locked */
351 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
352 if (d2->ad_flags != desc.ad_flags)
354 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
356 if (d2->ad_tags.bv_len == 0)
358 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
359 desc.ad_tags.bv_len) == 0)
363 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
367 /* Allocate a single contiguous block. If there are no
368 * options, we just need space for the AttrDesc structure.
369 * Otherwise, we need to tack on the full name length +
370 * options length, + maybe tagging options length again.
372 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
373 dlen = desc.ad_type->sat_cname.bv_len + 1;
374 if (desc.ad_tags.bv_len) {
375 dlen += 1 + desc.ad_tags.bv_len;
377 if ( slap_ad_is_binary( &desc ) ) {
378 dlen += 1 + STRLENOF(";binary") + desc.ad_tags.bv_len;
382 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
384 d2->ad_type = desc.ad_type;
385 d2->ad_flags = desc.ad_flags;
386 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
387 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
388 ldap_pvt_thread_mutex_lock( &ad_index_mutex );
389 d2->ad_index = ++ad_count;
390 ldap_pvt_thread_mutex_unlock( &ad_index_mutex );
393 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
394 d2->ad_tags.bv_val = NULL;
398 d2->ad_cname.bv_val = (char *)(d2+1);
399 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
400 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
401 if( slap_ad_is_binary( &desc ) ) {
404 if( desc.ad_tags.bv_len ) {
405 lp = desc.ad_tags.bv_val;
406 while( strncasecmp(lp, "binary", STRLENOF("binary")) < 0
407 && (lp = strchr( lp, ';' )) != NULL )
409 if( lp != desc.ad_tags.bv_val ) {
412 ? (unsigned) (lp - desc.ad_tags.bv_val - 1)
413 : strlen( desc.ad_tags.bv_val ));
414 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
417 cp = lutil_strcopy(cp, ";binary");
420 cp = lutil_strcopy(cp, lp);
422 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
423 if( desc.ad_tags.bv_len )
424 ldap_pvt_str2lower(op);
429 if( desc.ad_tags.bv_len ) {
430 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
433 d2->ad_tags.bv_val = lp;
434 strcpy(lp, desc.ad_tags.bv_val);
435 ldap_pvt_str2lower(lp);
437 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
440 /* Add new desc to list. We always want the bare Desc with
441 * no options to stay at the head of the list, assuming
442 * that one will be used most frequently.
444 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
445 d2->ad_next = desc.ad_type->sat_ad;
446 desc.ad_type->sat_ad = d2;
448 d2->ad_next = desc.ad_type->sat_ad->ad_next;
449 desc.ad_type->sat_ad->ad_next = d2;
451 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
463 static int is_ad_subtags(
464 struct berval *subtagsbv,
465 struct berval *suptagsbv )
467 const char *suptags, *supp, *supdelimp, *supn;
468 const char *subtags, *subp, *subdelimp, *subn;
471 subtags =subtagsbv->bv_val;
472 suptags =suptagsbv->bv_val;
473 subn = subtags + subtagsbv->bv_len;
474 supn = suptags + suptagsbv->bv_len;
476 for( supp=suptags ; supp; supp=supdelimp ) {
477 supdelimp = strchrlen( supp, supn, ';', &suplen );
478 if( supdelimp ) supdelimp++;
480 for( subp=subtags ; subp; subp=subdelimp ) {
481 subdelimp = strchrlen( subp, subn, ';', &sublen );
482 if( subdelimp ) subdelimp++;
485 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
486 && strncmp( supp, subp, sublen ) == 0 )
487 : ( ( suplen == sublen || supp[suplen-1] == '-' )
488 && strncmp( supp, subp, suplen ) == 0 ) )
501 AttributeDescription *sub,
502 AttributeDescription *super
508 for ( a = sub->ad_type; a; a=a->sat_sup ) {
509 if ( a == super->ad_type ) break;
515 /* ensure sub does support all flags of super */
516 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
517 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
521 /* check for tagging options */
522 if ( super->ad_tags.bv_len == 0 )
524 if ( sub->ad_tags.bv_len == 0 )
527 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
531 AttributeDescription *desc,
532 AttributeName *attrs )
534 if (! attrs ) return 0;
536 for( ; attrs->an_name.bv_val; attrs++ ) {
540 if ( attrs->an_desc ) {
543 if ( desc == attrs->an_desc ) {
548 * EXTENSION: if requested description is preceeded by
549 * a '-' character, do not match on subtypes.
551 if ( attrs->an_name.bv_val[0] == '-' ) {
555 /* Is this a subtype of the requested attr? */
556 for (a = desc->ad_type; a; a=a->sat_sup) {
557 if ( a == attrs->an_desc->ad_type )
563 /* Does desc support all the requested flags? */
564 lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
565 if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
566 != attrs->an_desc->ad_flags ) {
569 /* Do the descs have compatible tags? */
570 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
573 if ( desc->ad_tags.bv_len == 0) {
576 if ( is_ad_subtags( &desc->ad_tags,
577 &attrs->an_desc->ad_tags ) ) {
583 if ( ber_bvccmp( &attrs->an_name, '*' ) ) {
584 if ( !is_at_operational( desc->ad_type ) ) {
590 if ( ber_bvccmp( &attrs->an_name, '+' ) ) {
591 if ( is_at_operational( desc->ad_type ) ) {
598 * EXTENSION: see if requested description is @objectClass
599 * if so, return attributes which the class requires/allows
600 * else if requested description is !objectClass, return
601 * attributes which the class does not require/allow
603 if ( !( attrs->an_flags & SLAP_AN_OCINITED )) {
604 if( attrs->an_name.bv_val ) {
605 switch( attrs->an_name.bv_val[0] ) {
606 case '@': /* @objectClass */
607 case '+': /* +objectClass (deprecated) */
608 case '!': { /* exclude */
609 struct berval ocname;
610 ocname.bv_len = attrs->an_name.bv_len - 1;
611 ocname.bv_val = &attrs->an_name.bv_val[1];
612 oc = oc_bvfind( &ocname );
613 if ( oc && attrs->an_name.bv_val[0] == '!' ) {
614 attrs->an_flags |= SLAP_AN_OCEXCLUDE;
616 attrs->an_flags &= ~SLAP_AN_OCEXCLUDE;
620 default: /* old (deprecated) way */
621 oc = oc_bvfind( &attrs->an_name );
625 attrs->an_flags |= SLAP_AN_OCINITED;
629 if ( attrs->an_flags & SLAP_AN_OCEXCLUDE ) {
630 if ( oc == slap_schema.si_oc_extensibleObject ) {
631 /* extensibleObject allows the return of anything */
635 if( oc->soc_required ) {
636 /* allow return of required attributes */
639 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
640 for (a = desc->ad_type; a; a=a->sat_sup) {
641 if ( a == oc->soc_required[i] ) {
648 if( oc->soc_allowed ) {
649 /* allow return of allowed attributes */
651 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
652 for (a = desc->ad_type; a; a=a->sat_sup) {
653 if ( a == oc->soc_allowed[i] ) {
663 if ( oc == slap_schema.si_oc_extensibleObject ) {
664 /* extensibleObject allows the return of anything */
668 if( oc->soc_required ) {
669 /* allow return of required attributes */
672 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
673 for (a = desc->ad_type; a; a=a->sat_sup) {
674 if ( a == oc->soc_required[i] ) {
681 if( oc->soc_allowed ) {
682 /* allow return of allowed attributes */
684 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
685 for (a = desc->ad_type; a; a=a->sat_sup) {
686 if ( a == oc->soc_allowed[i] ) {
696 /* give it a chance of being retrieved by a proxy... */
697 (void)slap_bv2undef_ad( &attrs->an_name,
698 &attrs->an_desc, &text,
699 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
707 int slap_str2undef_ad(
709 AttributeDescription **ad,
714 bv.bv_val = (char *) str;
715 bv.bv_len = strlen( str );
717 return slap_bv2undef_ad( &bv, ad, text, flags );
720 int slap_bv2undef_ad(
722 AttributeDescription **ad,
726 AttributeDescription *desc;
729 assert( ad != NULL );
731 if( bv == NULL || bv->bv_len == 0 ) {
732 *text = "empty AttributeDescription";
733 return LDAP_UNDEFINED_TYPE;
736 /* make sure description is IA5 */
737 if( ad_keystring( bv ) ) {
738 *text = "AttributeDescription contains inappropriate characters";
739 return LDAP_UNDEFINED_TYPE;
742 /* use the appropriate type */
743 if ( flags & SLAP_AD_PROXIED ) {
744 at = slap_schema.si_at_proxied;
747 at = slap_schema.si_at_undefined;
750 for( desc = at->sat_ad; desc; desc=desc->ad_next ) {
751 if( desc->ad_cname.bv_len == bv->bv_len &&
752 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ) )
759 if ( flags & SLAP_AD_NOINSERT ) {
761 return LDAP_UNDEFINED_TYPE;
764 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
767 desc->ad_flags = SLAP_DESC_NONE;
768 BER_BVZERO( &desc->ad_tags );
770 desc->ad_cname.bv_len = bv->bv_len;
771 desc->ad_cname.bv_val = (char *)(desc+1);
772 strcpy(desc->ad_cname.bv_val, bv->bv_val);
774 /* canonical to upper case */
775 ldap_pvt_str2upper( desc->ad_cname.bv_val );
777 /* shouldn't we protect this for concurrency? */
780 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
781 desc->ad_next = desc->ad_type->sat_ad;
782 desc->ad_type->sat_ad = desc;
783 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
785 Debug( LDAP_DEBUG_ANY,
786 "%s attributeDescription \"%s\" inserted.\n",
787 ( flags & SLAP_AD_PROXIED ) ? "PROXIED" : "UNKNOWN",
788 desc->ad_cname.bv_val, 0 );
800 AttributeDescription *
805 AttributeDescription *ad =
806 slap_sl_mfuncs.bmf_malloc( sizeof(AttributeDescription) +
807 bv->bv_len + 1, memctx );
809 ad->ad_cname.bv_val = (char *)(ad+1);
810 strncpy( ad->ad_cname.bv_val, bv->bv_val, bv->bv_len+1 );
811 ad->ad_cname.bv_len = bv->bv_len;
812 ad->ad_flags = SLAP_DESC_TEMPORARY;
813 ad->ad_type = slap_schema.si_at_undefined;
824 AttributeDescription **u_ad, **n_ad;
826 /* Get to last ad on the new type */
827 for ( n_ad = &nat->sat_ad; *n_ad; n_ad = &(*n_ad)->ad_next ) ;
829 for ( u_ad = &at->sat_ad; *u_ad; ) {
832 ber_str2bv( name, 0, 0, &bv );
834 /* remove iff undef == name or undef == name;tag */
835 if ( (*u_ad)->ad_cname.bv_len >= bv.bv_len
836 && strncasecmp( (*u_ad)->ad_cname.bv_val, bv.bv_val, bv.bv_len ) == 0
837 && ( (*u_ad)->ad_cname.bv_val[ bv.bv_len ] == '\0'
838 || (*u_ad)->ad_cname.bv_val[ bv.bv_len ] == ';' ) )
840 AttributeDescription *tmp = *u_ad;
842 *u_ad = (*u_ad)->ad_next;
846 /* ad_cname was contiguous, no leak here */
847 tmp->ad_cname = nat->sat_cname;
848 ldap_pvt_thread_mutex_lock( &ad_index_mutex );
849 tmp->ad_index = ++ad_count;
850 ldap_pvt_thread_mutex_unlock( &ad_index_mutex );
852 n_ad = &tmp->ad_next;
854 u_ad = &(*u_ad)->ad_next;
862 slap_ad_undef_promote(
868 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
870 rc = undef_promote( slap_schema.si_at_undefined, name, at );
872 rc = undef_promote( slap_schema.si_at_proxied, name, at );
875 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
886 if( a == NULL ) return 0;
888 for ( ; a->an_name.bv_val; a++ ) {
889 if ( a->an_name.bv_len != s->bv_len) continue;
890 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
899 * Convert a delimited string into a list of AttributeNames; add
900 * on to an existing list if it was given. If the string is not
901 * a valid attribute name, if a '-' is prepended it is skipped
902 * and the remaining name is tried again; if a '@' (or '+') is
903 * prepended, an objectclass name is searched instead; if a '!'
904 * is prepended, the objectclass name is negated.
906 * NOTE: currently, if a valid attribute name is not found, the
907 * same string is also checked as valid objectclass name; however,
908 * this behavior is deprecated.
911 str2anlist( AttributeName *an, char *in, const char *brkstr )
920 /* find last element in list */
923 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++)
927 /* protect the input string from strtok */
928 str = ch_strdup( in );
930 /* Count words in string */
932 for ( s = str; *s; s++ ) {
933 if ( strchr( brkstr, *s ) != NULL ) {
938 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
940 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
942 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
944 /* put a stop mark */
945 BER_BVZERO( &anew[1].an_name );
947 anew->an_desc = NULL;
950 ber_str2bv(s, 0, 1, &anew->an_name);
951 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
952 if ( !anew->an_desc ) {
953 switch( anew->an_name.bv_val[0] ) {
955 struct berval adname;
956 adname.bv_len = anew->an_name.bv_len - 1;
957 adname.bv_val = &anew->an_name.bv_val[1];
958 slap_bv2ad(&adname, &anew->an_desc, &text);
959 if ( !anew->an_desc ) {
965 case '+': /* (deprecated) */
967 struct berval ocname;
968 ocname.bv_len = anew->an_name.bv_len - 1;
969 ocname.bv_val = &anew->an_name.bv_val[1];
970 anew->an_oc = oc_bvfind( &ocname );
971 if ( !anew->an_oc ) {
975 if ( anew->an_name.bv_val[0] == '!' ) {
976 anew->an_flags |= SLAP_AN_OCEXCLUDE;
981 /* old (deprecated) way */
982 anew->an_oc = oc_bvfind( &anew->an_name );
983 if ( !anew->an_oc ) {
988 anew->an_flags |= SLAP_AN_OCINITED;
992 BER_BVZERO( &anew->an_name );
997 anlist_free( an, 1, NULL );
1000 * overwrites input string
1009 anlist_free( AttributeName *an, int freename, void *ctx )
1018 for ( i = 0; an[i].an_name.bv_val; i++ ) {
1019 ber_memfree_x( an[i].an_name.bv_val, ctx );
1023 ber_memfree_x( an, ctx );
1026 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
1032 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
1034 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
1035 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
1037 attrs[i] = ch_strdup( an[i].an_name.bv_val );
1039 attrs[i] = an[i].an_name.bv_val;
1049 char **anlist2charray( AttributeName *an, int dup )
1051 return anlist2charray_x( an, dup, NULL );
1055 anlist2attrs( AttributeName * anlist )
1062 if ( anlist == NULL )
1065 for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
1066 if ( ( oc = anlist[i].an_oc ) ) {
1067 for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
1069 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
1077 attrs = anlist2charray( anlist, 1 );
1082 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
1084 for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
1085 if ( ( oc = anlist[i].an_oc ) ) {
1086 for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
1087 attrs[n++] = ch_strdup(
1088 oc->soc_required[j]->sat_cname.bv_val );
1090 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
1091 attrs[n++] = ch_strdup(
1092 oc->soc_allowed[j]->sat_cname.bv_val );
1101 while ( attrs && attrs[i] ) {
1102 if ( *attrs[i] == '@' ) {
1103 ch_free( attrs[i] );
1104 for ( j = i; attrs[j]; j++ ) {
1105 attrs[j] = attrs[j+1];
1112 for ( i = 0; attrs && attrs[i]; i++ ) {
1114 while ( attrs && attrs[j] ) {
1115 if ( !strcmp( attrs[i], attrs[j] )) {
1116 ch_free( attrs[j] );
1117 for ( k = j; attrs && attrs[k]; k++ ) {
1118 attrs[k] = attrs[k+1];
1127 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
1134 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
1140 size_t lmax = LBUFSIZ;
1142 fp = fopen( fname, "r" );
1144 Debug( LDAP_DEBUG_ANY,
1145 "get_attrs_from_file: failed to open attribute list file "
1146 "\"%s\": %s\n", fname, strerror(errno), 0 );
1150 lcur = line = (char *) ch_malloc( lmax );
1152 Debug( LDAP_DEBUG_ANY,
1153 "get_attrs_from_file: could not allocate memory\n",
1159 while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
1160 if ( ( c = strchr( lcur, '\n' ) ) ) {
1163 } else if ( *(c-1) == '\r' ) {
1170 line = (char *) ch_realloc( line, lmax );
1172 Debug( LDAP_DEBUG_ANY,
1173 "get_attrs_from_file: could not allocate memory\n",
1178 lcur = line + strlen( line );
1181 an = str2anlist( an, line, brkstr );
1192 /* Define an attribute option. */
1194 ad_define_option( const char *name, const char *fname, int lineno )
1197 unsigned int optlen;
1199 if ( options == &lang_option ) {
1208 if ( !DESC_CHAR( name[optlen] ) ) {
1209 /* allow trailing '=', same as '-' */
1210 if ( name[optlen] == '=' && !name[optlen+1] ) {
1211 msad_range_hack = 1;
1214 Debug( LDAP_DEBUG_ANY,
1215 "%s: line %d: illegal option name \"%s\"\n",
1216 fname, lineno, name );
1219 } while ( name[++optlen] );
1221 options = ch_realloc( options,
1222 (option_count+1) * sizeof(Attr_option) );
1224 if ( strcasecmp( name, "binary" ) == 0
1225 || ad_find_option_definition( name, optlen ) ) {
1226 Debug( LDAP_DEBUG_ANY,
1227 "%s: line %d: option \"%s\" is already defined\n",
1228 fname, lineno, name );
1232 for ( i = option_count; i; --i ) {
1233 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1235 options[i] = options[i-1];
1238 options[i].name.bv_val = ch_strdup( name );
1239 options[i].name.bv_len = optlen;
1240 options[i].prefix = (name[optlen-1] == '-') ||
1241 (name[optlen-1] == '=');
1243 if ( i != option_count &&
1244 options[i].prefix &&
1245 optlen < options[i+1].name.bv_len &&
1246 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1247 Debug( LDAP_DEBUG_ANY,
1248 "%s: line %d: option \"%s\" overrides previous option\n",
1249 fname, lineno, name );
1258 ad_unparse_options( BerVarray *res )
1261 for ( i = 0; i < option_count; i++ ) {
1262 value_add_one( res, &options[i].name );
1266 /* Find the definition of the option name or prefix matching the arguments */
1267 static Attr_option *
1268 ad_find_option_definition( const char *opt, int optlen )
1270 int top = 0, bot = option_count;
1271 while ( top < bot ) {
1272 int mid = (top + bot) / 2;
1273 int mlen = options[mid].name.bv_len;
1274 char *mname = options[mid].name.bv_val;
1276 if ( optlen < mlen ) {
1277 j = strncasecmp( opt, mname, optlen ) - 1;
1279 j = strncasecmp( opt, mname, mlen );
1280 if ( j==0 && (optlen==mlen || options[mid].prefix) )
1281 return &options[mid];
1291 MatchingRule *ad_mr(
1292 AttributeDescription *ad,
1295 switch( usage & SLAP_MR_TYPE_MASK ) {
1297 case SLAP_MR_EQUALITY:
1298 return ad->ad_type->sat_equality;
1300 case SLAP_MR_ORDERING:
1301 return ad->ad_type->sat_ordering;
1303 case SLAP_MR_SUBSTR:
1304 return ad->ad_type->sat_substr;
1308 assert( 0 /* ad_mr: bad usage */);