3 * Copyright 1998-2000 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 AttributeDescription *ad_dup(
22 AttributeDescription *desc )
24 AttributeDescription *ad;
30 ad = (AttributeDescription *) ch_malloc( sizeof(AttributeDescription) );
34 if( ad->ad_cname != NULL ) {
35 ad->ad_cname = ber_bvdup( ad->ad_cname );
38 if( ad->ad_lang != NULL ) {
39 ad->ad_lang = ch_strdup( ad->ad_lang );
46 ad_free( AttributeDescription *ad, int freeit )
48 if( ad == NULL ) return;
50 if( ad->ad_cname != NULL ) {
51 ber_bvfree( ad->ad_cname );
56 if( freeit ) free( ad );
59 static int ad_keystring(
64 if( !AD_CHAR( bv->bv_val[0] ) ) {
68 for( i=1; i<bv->bv_len; i++ ) {
69 if( !AD_CHAR( bv->bv_val[i] ) ) {
78 AttributeDescription **ad,
82 bv.bv_val = (char *) str;
83 bv.bv_len = strlen( str );
85 return slap_bv2ad( &bv, ad, text );
90 AttributeDescription **ad,
93 int rtn = LDAP_UNDEFINED_TYPE;
95 AttributeDescription desc;
99 assert( *ad == NULL ); /* temporary */
101 if( bv == NULL || bv->bv_len == 0 ) {
102 *text = "empty attribute description";
106 /* make sure description is IA5 */
107 if( ad_keystring( bv ) ) {
108 *text = "attribute description contains inappropriate characters";
112 tokens = str2charray( bv->bv_val, ";");
114 if( tokens == NULL || *tokens == NULL ) {
115 *text = "no attribute type";
119 desc.ad_type = at_find( *tokens );
121 if( desc.ad_type == NULL ) {
122 *text = "attribute type undefined";
126 desc.ad_flags = SLAP_DESC_NONE;
129 for( i=1; tokens[i] != NULL; i++ ) {
130 if( strcasecmp( tokens[i], "binary" ) == 0 ) {
131 if( slap_ad_is_binary( &desc ) ) {
132 *text = "option \"binary\" specified multiple times";
136 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
137 /* not stored in binary, disallow option */
138 *text = "option \"binary\" with type not supported";
142 desc.ad_flags |= SLAP_DESC_BINARY;
144 } else if ( strncasecmp( tokens[i], "lang-",
145 sizeof("lang-")-1 ) == 0 && tokens[i][sizeof("lang-")-1] )
147 if( desc.ad_lang != NULL ) {
148 *text = "multiple language tag options specified";
152 desc.ad_lang = ch_strdup( tokens[i] );
154 /* normalize to all lower case, it's easy */
155 ldap_pvt_str2lower( desc.ad_lang );
158 *text = "unrecognized option";
163 desc.ad_cname = ch_malloc( sizeof( struct berval ) );
165 desc.ad_cname->bv_len = strlen( desc.ad_type->sat_cname );
166 if( slap_ad_is_binary( &desc ) ) {
167 desc.ad_cname->bv_len += sizeof("binary");
169 if( desc.ad_lang != NULL ) {
170 desc.ad_cname->bv_len += 1 + strlen( desc.ad_lang );
173 desc.ad_cname->bv_val = ch_malloc( desc.ad_cname->bv_len + 1 );
175 strcpy( desc.ad_cname->bv_val, desc.ad_type->sat_cname );
176 if( slap_ad_is_binary( &desc ) ) {
177 strcat( desc.ad_cname->bv_val, ";binary" );
180 if( desc.ad_lang != NULL ) {
181 strcat( desc.ad_cname->bv_val, ";" );
182 strcat( desc.ad_cname->bv_val, desc.ad_lang );
186 *ad = ch_malloc( sizeof( AttributeDescription ) );
194 charray_free( tokens );
199 AttributeDescription *sub,
200 AttributeDescription *super
203 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
207 if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
211 if( super->ad_lang != NULL && ( sub->ad_lang == NULL
212 || strcasecmp( super->ad_lang, sub->ad_lang )))
222 AttributeDescription *desc,
226 for( i=0; attrs[i] != NULL; i++ ) {
227 AttributeDescription *ad = NULL;
231 rc = slap_str2ad( attrs[i], &ad, &text );
233 if( rc != LDAP_SUCCESS ) continue;
235 rc = is_ad_subtype( desc, ad );
246 int slap_str2undef_ad(
248 AttributeDescription **ad,
252 bv.bv_val = (char *) str;
253 bv.bv_len = strlen( str );
255 return slap_bv2undef_ad( &bv, ad, text );
258 int slap_bv2undef_ad(
260 AttributeDescription **ad,
263 AttributeDescription desc;
265 assert( ad != NULL );
266 assert( *ad == NULL ); /* temporary */
268 if( bv == NULL || bv->bv_len == 0 ) {
269 *text = "empty attribute description";
270 return LDAP_UNDEFINED_TYPE;
273 /* make sure description is IA5 */
274 if( ad_keystring( bv ) ) {
275 *text = "attribute description contains inappropriate characters";
276 return LDAP_UNDEFINED_TYPE;
279 desc.ad_type = slap_schema.si_at_undefined;
280 desc.ad_flags = SLAP_DESC_NONE;
283 desc.ad_cname = ber_bvdup( bv );
285 /* canoncial to upper case */
286 ldap_pvt_str2upper( bv->bv_val );
289 *ad = ch_malloc( sizeof( AttributeDescription ) );