1 /* ad.c - routines for dealing with attribute descriptions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2012 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) */
276 } else if ( rc > 0 ||
277 ( rc == 0 && (unsigned)optlen > tags[i].bv_len ))
279 AC_MEMCPY( &tags[i+2], &tags[i+1],
280 (ntags-i-1)*sizeof(struct berval) );
281 tags[i+1].bv_val = opt;
282 tags[i+1].bv_len = optlen;
288 AC_MEMCPY( &tags[1], &tags[0],
289 ntags*sizeof(struct berval) );
291 tags[0].bv_val = opt;
292 tags[0].bv_len = optlen;
295 tagslen += optlen + 1;
299 *text = "unrecognized option";
307 if( tagslen > MAX_TAGS_LEN ) {
308 *text = "tagging options too long";
312 desc.ad_tags.bv_val = tagbuf;
315 for( i=0; i<ntags; i++ ) {
316 AC_MEMCPY( &desc.ad_tags.bv_val[tagslen],
317 tags[i].bv_val, tags[i].bv_len );
319 tagslen += tags[i].bv_len;
320 desc.ad_tags.bv_val[tagslen++] = ';';
323 desc.ad_tags.bv_val[--tagslen] = '\0';
324 desc.ad_tags.bv_len = tagslen;
327 /* see if a matching description is already cached */
328 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
329 if( d2->ad_flags != desc.ad_flags ) {
332 if( d2->ad_tags.bv_len != desc.ad_tags.bv_len ) {
335 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 )
345 /* Not found, add new one */
348 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
349 /* check again now that we've locked */
350 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
351 if (d2->ad_flags != desc.ad_flags)
353 if (d2->ad_tags.bv_len != desc.ad_tags.bv_len)
355 if (d2->ad_tags.bv_len == 0)
357 if (strncasecmp(d2->ad_tags.bv_val, desc.ad_tags.bv_val,
358 desc.ad_tags.bv_len) == 0)
362 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
366 /* Allocate a single contiguous block. If there are no
367 * options, we just need space for the AttrDesc structure.
368 * Otherwise, we need to tack on the full name length +
369 * options length, + maybe tagging options length again.
371 if (desc.ad_tags.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
372 dlen = desc.ad_type->sat_cname.bv_len + 1;
373 if (desc.ad_tags.bv_len) {
374 dlen += 1 + desc.ad_tags.bv_len;
376 if ( slap_ad_is_binary( &desc ) ) {
377 dlen += 1 + STRLENOF(";binary") + desc.ad_tags.bv_len;
381 d2 = ch_malloc(sizeof(AttributeDescription) + dlen);
383 d2->ad_type = desc.ad_type;
384 d2->ad_flags = desc.ad_flags;
385 d2->ad_cname.bv_len = desc.ad_type->sat_cname.bv_len;
386 d2->ad_tags.bv_len = desc.ad_tags.bv_len;
387 ldap_pvt_thread_mutex_lock( &ad_index_mutex );
388 d2->ad_index = ++ad_count;
389 ldap_pvt_thread_mutex_unlock( &ad_index_mutex );
392 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
393 d2->ad_tags.bv_val = NULL;
397 d2->ad_cname.bv_val = (char *)(d2+1);
398 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
399 cp = d2->ad_cname.bv_val + d2->ad_cname.bv_len;
400 if( slap_ad_is_binary( &desc ) ) {
403 if( desc.ad_tags.bv_len ) {
404 lp = desc.ad_tags.bv_val;
405 while( strncasecmp(lp, "binary", STRLENOF("binary")) < 0
406 && (lp = strchr( lp, ';' )) != NULL )
408 if( lp != desc.ad_tags.bv_val ) {
411 ? (unsigned) (lp - desc.ad_tags.bv_val - 1)
412 : strlen( desc.ad_tags.bv_val ));
413 cp = lutil_strncopy(cp, desc.ad_tags.bv_val, j);
416 cp = lutil_strcopy(cp, ";binary");
419 cp = lutil_strcopy(cp, lp);
421 d2->ad_cname.bv_len = cp - d2->ad_cname.bv_val;
422 if( desc.ad_tags.bv_len )
423 ldap_pvt_str2lower(op);
428 if( desc.ad_tags.bv_len ) {
429 lp = d2->ad_cname.bv_val + d2->ad_cname.bv_len + j;
432 d2->ad_tags.bv_val = lp;
433 strcpy(lp, desc.ad_tags.bv_val);
434 ldap_pvt_str2lower(lp);
436 d2->ad_cname.bv_len += 1 + desc.ad_tags.bv_len;
439 /* Add new desc to list. We always want the bare Desc with
440 * no options to stay at the head of the list, assuming
441 * that one will be used most frequently.
443 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
444 d2->ad_next = desc.ad_type->sat_ad;
445 desc.ad_type->sat_ad = d2;
447 d2->ad_next = desc.ad_type->sat_ad->ad_next;
448 desc.ad_type->sat_ad->ad_next = d2;
450 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
462 static int is_ad_subtags(
463 struct berval *subtagsbv,
464 struct berval *suptagsbv )
466 const char *suptags, *supp, *supdelimp, *supn;
467 const char *subtags, *subp, *subdelimp, *subn;
470 subtags =subtagsbv->bv_val;
471 suptags =suptagsbv->bv_val;
472 subn = subtags + subtagsbv->bv_len;
473 supn = suptags + suptagsbv->bv_len;
475 for( supp=suptags ; supp; supp=supdelimp ) {
476 supdelimp = strchrlen( supp, supn, ';', &suplen );
477 if( supdelimp ) supdelimp++;
479 for( subp=subtags ; subp; subp=subdelimp ) {
480 subdelimp = strchrlen( subp, subn, ';', &sublen );
481 if( subdelimp ) subdelimp++;
484 ? ( suplen-1 == sublen && supp[suplen-1] == '-'
485 && strncmp( supp, subp, sublen ) == 0 )
486 : ( ( suplen == sublen || supp[suplen-1] == '-' )
487 && strncmp( supp, subp, suplen ) == 0 ) )
500 AttributeDescription *sub,
501 AttributeDescription *super
507 for ( a = sub->ad_type; a; a=a->sat_sup ) {
508 if ( a == super->ad_type ) break;
514 /* ensure sub does support all flags of super */
515 lr = sub->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
516 if(( super->ad_flags & ( sub->ad_flags | lr )) != super->ad_flags ) {
520 /* check for tagging options */
521 if ( super->ad_tags.bv_len == 0 )
523 if ( sub->ad_tags.bv_len == 0 )
526 return is_ad_subtags( &sub->ad_tags, &super->ad_tags );
530 AttributeDescription *desc,
531 AttributeName *attrs )
533 if (! attrs ) return 0;
535 for( ; attrs->an_name.bv_val; attrs++ ) {
539 if ( attrs->an_desc ) {
542 if ( desc == attrs->an_desc ) {
547 * EXTENSION: if requested description is preceeded by
548 * a '-' character, do not match on subtypes.
550 if ( attrs->an_name.bv_val[0] == '-' ) {
554 /* Is this a subtype of the requested attr? */
555 for (a = desc->ad_type; a; a=a->sat_sup) {
556 if ( a == attrs->an_desc->ad_type )
562 /* Does desc support all the requested flags? */
563 lr = desc->ad_tags.bv_len ? SLAP_DESC_TAG_RANGE : 0;
564 if(( attrs->an_desc->ad_flags & (desc->ad_flags | lr))
565 != attrs->an_desc->ad_flags ) {
568 /* Do the descs have compatible tags? */
569 if ( attrs->an_desc->ad_tags.bv_len == 0 ) {
572 if ( desc->ad_tags.bv_len == 0) {
575 if ( is_ad_subtags( &desc->ad_tags,
576 &attrs->an_desc->ad_tags ) ) {
582 if ( ber_bvccmp( &attrs->an_name, '*' ) ) {
583 if ( !is_at_operational( desc->ad_type ) ) {
589 if ( ber_bvccmp( &attrs->an_name, '+' ) ) {
590 if ( is_at_operational( desc->ad_type ) ) {
597 * EXTENSION: see if requested description is @objectClass
598 * if so, return attributes which the class requires/allows
599 * else if requested description is !objectClass, return
600 * attributes which the class does not require/allow
602 if ( !( attrs->an_flags & SLAP_AN_OCINITED )) {
603 if( attrs->an_name.bv_val ) {
604 switch( attrs->an_name.bv_val[0] ) {
605 case '@': /* @objectClass */
606 case '+': /* +objectClass (deprecated) */
607 case '!': { /* exclude */
608 struct berval ocname;
609 ocname.bv_len = attrs->an_name.bv_len - 1;
610 ocname.bv_val = &attrs->an_name.bv_val[1];
611 oc = oc_bvfind( &ocname );
612 if ( oc && attrs->an_name.bv_val[0] == '!' ) {
613 attrs->an_flags |= SLAP_AN_OCEXCLUDE;
615 attrs->an_flags &= ~SLAP_AN_OCEXCLUDE;
619 default: /* old (deprecated) way */
620 oc = oc_bvfind( &attrs->an_name );
624 attrs->an_flags |= SLAP_AN_OCINITED;
628 if ( attrs->an_flags & SLAP_AN_OCEXCLUDE ) {
629 if ( oc == slap_schema.si_oc_extensibleObject ) {
630 /* extensibleObject allows the return of anything */
634 if( oc->soc_required ) {
635 /* allow return of required attributes */
638 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
639 for (a = desc->ad_type; a; a=a->sat_sup) {
640 if ( a == oc->soc_required[i] ) {
647 if( oc->soc_allowed ) {
648 /* allow return of allowed attributes */
650 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
651 for (a = desc->ad_type; a; a=a->sat_sup) {
652 if ( a == oc->soc_allowed[i] ) {
662 if ( oc == slap_schema.si_oc_extensibleObject ) {
663 /* extensibleObject allows the return of anything */
667 if( oc->soc_required ) {
668 /* allow return of required attributes */
671 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
672 for (a = desc->ad_type; a; a=a->sat_sup) {
673 if ( a == oc->soc_required[i] ) {
680 if( oc->soc_allowed ) {
681 /* allow return of allowed attributes */
683 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
684 for (a = desc->ad_type; a; a=a->sat_sup) {
685 if ( a == oc->soc_allowed[i] ) {
695 /* give it a chance of being retrieved by a proxy... */
696 (void)slap_bv2undef_ad( &attrs->an_name,
697 &attrs->an_desc, &text,
698 SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
706 int slap_str2undef_ad(
708 AttributeDescription **ad,
713 bv.bv_val = (char *) str;
714 bv.bv_len = strlen( str );
716 return slap_bv2undef_ad( &bv, ad, text, flags );
719 int slap_bv2undef_ad(
721 AttributeDescription **ad,
725 AttributeDescription *desc;
728 assert( ad != NULL );
730 if( bv == NULL || bv->bv_len == 0 ) {
731 *text = "empty AttributeDescription";
732 return LDAP_UNDEFINED_TYPE;
735 /* make sure description is IA5 */
736 if( ad_keystring( bv ) ) {
737 *text = "AttributeDescription contains inappropriate characters";
738 return LDAP_UNDEFINED_TYPE;
741 /* use the appropriate type */
742 if ( flags & SLAP_AD_PROXIED ) {
743 at = slap_schema.si_at_proxied;
746 at = slap_schema.si_at_undefined;
749 for( desc = at->sat_ad; desc; desc=desc->ad_next ) {
750 if( desc->ad_cname.bv_len == bv->bv_len &&
751 !strcasecmp( desc->ad_cname.bv_val, bv->bv_val ) )
758 if ( flags & SLAP_AD_NOINSERT ) {
760 return LDAP_UNDEFINED_TYPE;
763 desc = ch_malloc(sizeof(AttributeDescription) + 1 +
766 desc->ad_flags = SLAP_DESC_NONE;
767 BER_BVZERO( &desc->ad_tags );
769 desc->ad_cname.bv_len = bv->bv_len;
770 desc->ad_cname.bv_val = (char *)(desc+1);
771 strcpy(desc->ad_cname.bv_val, bv->bv_val);
773 /* canonical to upper case */
774 ldap_pvt_str2upper( desc->ad_cname.bv_val );
776 /* shouldn't we protect this for concurrency? */
779 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
780 desc->ad_next = desc->ad_type->sat_ad;
781 desc->ad_type->sat_ad = desc;
782 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
784 Debug( LDAP_DEBUG_ANY,
785 "%s attributeDescription \"%s\" inserted.\n",
786 ( flags & SLAP_AD_PROXIED ) ? "PROXIED" : "UNKNOWN",
787 desc->ad_cname.bv_val, 0 );
799 AttributeDescription *
804 AttributeDescription *ad =
805 slap_sl_mfuncs.bmf_malloc( sizeof(AttributeDescription) +
806 bv->bv_len + 1, memctx );
808 ad->ad_cname.bv_val = (char *)(ad+1);
809 strncpy( ad->ad_cname.bv_val, bv->bv_val, bv->bv_len+1 );
810 ad->ad_cname.bv_len = bv->bv_len;
811 ad->ad_flags = SLAP_DESC_TEMPORARY;
812 ad->ad_type = slap_schema.si_at_undefined;
823 AttributeDescription **u_ad, **n_ad;
825 /* Get to last ad on the new type */
826 for ( n_ad = &nat->sat_ad; *n_ad; n_ad = &(*n_ad)->ad_next ) ;
828 for ( u_ad = &at->sat_ad; *u_ad; ) {
831 ber_str2bv( name, 0, 0, &bv );
833 /* remove iff undef == name or undef == name;tag */
834 if ( (*u_ad)->ad_cname.bv_len >= bv.bv_len
835 && strncasecmp( (*u_ad)->ad_cname.bv_val, bv.bv_val, bv.bv_len ) == 0
836 && ( (*u_ad)->ad_cname.bv_val[ bv.bv_len ] == '\0'
837 || (*u_ad)->ad_cname.bv_val[ bv.bv_len ] == ';' ) )
839 AttributeDescription *tmp = *u_ad;
841 *u_ad = (*u_ad)->ad_next;
845 /* ad_cname was contiguous, no leak here */
846 tmp->ad_cname = nat->sat_cname;
848 n_ad = &tmp->ad_next;
850 u_ad = &(*u_ad)->ad_next;
858 slap_ad_undef_promote(
864 ldap_pvt_thread_mutex_lock( &ad_undef_mutex );
866 rc = undef_promote( slap_schema.si_at_undefined, name, at );
868 rc = undef_promote( slap_schema.si_at_proxied, name, at );
871 ldap_pvt_thread_mutex_unlock( &ad_undef_mutex );
882 if( a == NULL ) return 0;
884 for ( ; a->an_name.bv_val; a++ ) {
885 if ( a->an_name.bv_len != s->bv_len) continue;
886 if ( strcasecmp( s->bv_val, a->an_name.bv_val ) == 0 ) {
895 * Convert a delimited string into a list of AttributeNames; add
896 * on to an existing list if it was given. If the string is not
897 * a valid attribute name, if a '-' is prepended it is skipped
898 * and the remaining name is tried again; if a '@' (or '+') is
899 * prepended, an objectclass name is searched instead; if a '!'
900 * is prepended, the objectclass name is negated.
902 * NOTE: currently, if a valid attribute name is not found, the
903 * same string is also checked as valid objectclass name; however,
904 * this behavior is deprecated.
907 str2anlist( AttributeName *an, char *in, const char *brkstr )
916 /* find last element in list */
919 for ( i = 0; !BER_BVISNULL( &an[ i ].an_name ) ; i++)
923 /* protect the input string from strtok */
924 str = ch_strdup( in );
926 /* Count words in string */
928 for ( s = str; *s; s++ ) {
929 if ( strchr( brkstr, *s ) != NULL ) {
934 an = ch_realloc( an, ( i + j + 1 ) * sizeof( AttributeName ) );
936 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
938 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
940 /* put a stop mark */
941 BER_BVZERO( &anew[1].an_name );
943 anew->an_desc = NULL;
946 ber_str2bv(s, 0, 1, &anew->an_name);
947 slap_bv2ad(&anew->an_name, &anew->an_desc, &text);
948 if ( !anew->an_desc ) {
949 switch( anew->an_name.bv_val[0] ) {
951 struct berval adname;
952 adname.bv_len = anew->an_name.bv_len - 1;
953 adname.bv_val = &anew->an_name.bv_val[1];
954 slap_bv2ad(&adname, &anew->an_desc, &text);
955 if ( !anew->an_desc ) {
961 case '+': /* (deprecated) */
963 struct berval ocname;
964 ocname.bv_len = anew->an_name.bv_len - 1;
965 ocname.bv_val = &anew->an_name.bv_val[1];
966 anew->an_oc = oc_bvfind( &ocname );
967 if ( !anew->an_oc ) {
971 if ( anew->an_name.bv_val[0] == '!' ) {
972 anew->an_flags |= SLAP_AN_OCEXCLUDE;
977 /* old (deprecated) way */
978 anew->an_oc = oc_bvfind( &anew->an_name );
979 if ( !anew->an_oc ) {
984 anew->an_flags |= SLAP_AN_OCINITED;
988 BER_BVZERO( &anew->an_name );
993 anlist_free( an, 1, NULL );
996 * overwrites input string
1005 anlist_free( AttributeName *an, int freename, void *ctx )
1014 for ( i = 0; an[i].an_name.bv_val; i++ ) {
1015 ber_memfree_x( an[i].an_name.bv_val, ctx );
1019 ber_memfree_x( an, ctx );
1022 char **anlist2charray_x( AttributeName *an, int dup, void *ctx )
1028 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
1030 attrs = (char **) slap_sl_malloc( (i + 1) * sizeof(char *), ctx );
1031 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
1033 attrs[i] = ch_strdup( an[i].an_name.bv_val );
1035 attrs[i] = an[i].an_name.bv_val;
1045 char **anlist2charray( AttributeName *an, int dup )
1047 return anlist2charray_x( an, dup, NULL );
1051 anlist2attrs( AttributeName * anlist )
1058 if ( anlist == NULL )
1061 for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
1062 if ( ( oc = anlist[i].an_oc ) ) {
1063 for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) ;
1065 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) ;
1073 attrs = anlist2charray( anlist, 1 );
1078 attrs = (char **) ch_realloc( attrs, (i + k + 1) * sizeof( char * ));
1080 for ( i = 0; anlist[i].an_name.bv_val; i++ ) {
1081 if ( ( oc = anlist[i].an_oc ) ) {
1082 for ( j = 0; oc->soc_required && oc->soc_required[j]; j++ ) {
1083 attrs[n++] = ch_strdup(
1084 oc->soc_required[j]->sat_cname.bv_val );
1086 for ( j = 0; oc->soc_allowed && oc->soc_allowed[j]; j++ ) {
1087 attrs[n++] = ch_strdup(
1088 oc->soc_allowed[j]->sat_cname.bv_val );
1097 while ( attrs && attrs[i] ) {
1098 if ( *attrs[i] == '@' ) {
1099 ch_free( attrs[i] );
1100 for ( j = i; attrs[j]; j++ ) {
1101 attrs[j] = attrs[j+1];
1108 for ( i = 0; attrs && attrs[i]; i++ ) {
1110 while ( attrs && attrs[j] ) {
1111 if ( !strcmp( attrs[i], attrs[j] )) {
1112 ch_free( attrs[j] );
1113 for ( k = j; attrs && attrs[k]; k++ ) {
1114 attrs[k] = attrs[k+1];
1123 attrs = (char **) ch_realloc( attrs, (i+1) * sizeof( char * ));
1130 file2anlist( AttributeName *an, const char *fname, const char *brkstr )
1136 size_t lmax = LBUFSIZ;
1138 fp = fopen( fname, "r" );
1140 Debug( LDAP_DEBUG_ANY,
1141 "get_attrs_from_file: failed to open attribute list file "
1142 "\"%s\": %s\n", fname, strerror(errno), 0 );
1146 lcur = line = (char *) ch_malloc( lmax );
1148 Debug( LDAP_DEBUG_ANY,
1149 "get_attrs_from_file: could not allocate memory\n",
1155 while ( fgets( lcur, LBUFSIZ, fp ) != NULL ) {
1156 if ( ( c = strchr( lcur, '\n' ) ) ) {
1159 } else if ( *(c-1) == '\r' ) {
1166 line = (char *) ch_realloc( line, lmax );
1168 Debug( LDAP_DEBUG_ANY,
1169 "get_attrs_from_file: could not allocate memory\n",
1174 lcur = line + strlen( line );
1177 an = str2anlist( an, line, brkstr );
1188 /* Define an attribute option. */
1190 ad_define_option( const char *name, const char *fname, int lineno )
1193 unsigned int optlen;
1195 if ( options == &lang_option ) {
1204 if ( !DESC_CHAR( name[optlen] ) ) {
1205 /* allow trailing '=', same as '-' */
1206 if ( name[optlen] == '=' && !name[optlen+1] ) {
1207 msad_range_hack = 1;
1210 Debug( LDAP_DEBUG_ANY,
1211 "%s: line %d: illegal option name \"%s\"\n",
1212 fname, lineno, name );
1215 } while ( name[++optlen] );
1217 options = ch_realloc( options,
1218 (option_count+1) * sizeof(Attr_option) );
1220 if ( strcasecmp( name, "binary" ) == 0
1221 || ad_find_option_definition( name, optlen ) ) {
1222 Debug( LDAP_DEBUG_ANY,
1223 "%s: line %d: option \"%s\" is already defined\n",
1224 fname, lineno, name );
1228 for ( i = option_count; i; --i ) {
1229 if ( strcasecmp( name, options[i-1].name.bv_val ) >= 0 )
1231 options[i] = options[i-1];
1234 options[i].name.bv_val = ch_strdup( name );
1235 options[i].name.bv_len = optlen;
1236 options[i].prefix = (name[optlen-1] == '-') ||
1237 (name[optlen-1] == '=');
1239 if ( i != option_count &&
1240 options[i].prefix &&
1241 optlen < options[i+1].name.bv_len &&
1242 strncasecmp( name, options[i+1].name.bv_val, optlen ) == 0 ) {
1243 Debug( LDAP_DEBUG_ANY,
1244 "%s: line %d: option \"%s\" overrides previous option\n",
1245 fname, lineno, name );
1254 ad_unparse_options( BerVarray *res )
1257 for ( i = 0; i < option_count; i++ ) {
1258 value_add_one( res, &options[i].name );
1262 /* Find the definition of the option name or prefix matching the arguments */
1263 static Attr_option *
1264 ad_find_option_definition( const char *opt, int optlen )
1266 int top = 0, bot = option_count;
1267 while ( top < bot ) {
1268 int mid = (top + bot) / 2;
1269 int mlen = options[mid].name.bv_len;
1270 char *mname = options[mid].name.bv_val;
1272 if ( optlen < mlen ) {
1273 j = strncasecmp( opt, mname, optlen ) - 1;
1275 j = strncasecmp( opt, mname, mlen );
1276 if ( j==0 && (optlen==mlen || options[mid].prefix) )
1277 return &options[mid];
1287 MatchingRule *ad_mr(
1288 AttributeDescription *ad,
1291 switch( usage & SLAP_MR_TYPE_MASK ) {
1293 case SLAP_MR_EQUALITY:
1294 return ad->ad_type->sat_equality;
1296 case SLAP_MR_ORDERING:
1297 return ad->ad_type->sat_ordering;
1299 case SLAP_MR_SUBSTR:
1300 return ad->ad_type->sat_substr;
1304 assert( 0 /* ad_mr: bad usage */);