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 extern ldap_pvt_thread_mutex_t ad_mutex; /* init.c */
23 static int ad_keystring(
28 if( !AD_CHAR( bv->bv_val[0] ) ) {
32 for( i=1; i<bv->bv_len; i++ ) {
33 if( !AD_CHAR( bv->bv_val[i] ) ) {
40 void ad_destroy( void *in )
42 AttributeDescription *ad = in, *n;
52 AttributeDescription **ad,
56 bv.bv_val = (char *) str;
57 bv.bv_len = strlen( str );
59 return slap_bv2ad( &bv, ad, text );
64 AttributeDescription **ad,
67 int rtn = LDAP_UNDEFINED_TYPE;
69 AttributeDescription desc, *d2;
73 assert( *ad == NULL ); /* temporary */
75 if( bv == NULL || bv->bv_len == 0 ) {
76 *text = "empty attribute description";
80 /* make sure description is IA5 */
81 if( ad_keystring( bv ) ) {
82 *text = "attribute description contains inappropriate characters";
86 /* find valid base attribute type; parse in place */
88 options = strchr(name, ';');
89 if (options != NULL) *options = '\0';
90 desc.ad_type = at_find( name );
91 if (options != NULL) *options = ';';
92 if( desc.ad_type == NULL ) {
93 *text = "attribute type undefined";
97 if (options != NULL) {
98 desc.ad_cname.bv_len = options - name;
100 desc.ad_cname.bv_len = bv->bv_len;
103 desc.ad_flags = SLAP_DESC_NONE;
104 desc.ad_lang.bv_len = 0;
105 desc.ad_lang.bv_val = NULL;
107 /* parse options in place */
108 for( ; options != NULL; ) {
110 options = strchr( name, ';' );
111 if ( options != NULL )
116 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
117 if( slap_ad_is_binary( &desc ) ) {
118 *text = "option \"binary\" specified multiple times";
122 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
123 /* not stored in binary, disallow option */
124 *text = "option \"binary\" with type not supported";
128 desc.ad_flags |= SLAP_DESC_BINARY;
130 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
131 sizeof("lang-")-1 ) == 0)
133 if( desc.ad_lang.bv_len != 0 ) {
134 *text = "multiple language tag options specified";
138 desc.ad_lang.bv_val = name;
139 desc.ad_lang.bv_len = i;
141 *text = "unrecognized option";
146 /* see if a matching description is already cached */
147 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
148 if (d2->ad_flags != desc.ad_flags)
150 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
152 if (d2->ad_lang.bv_len == 0)
154 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
155 desc.ad_lang.bv_len) == 0)
159 /* Not found, add new one */
162 /* uses a single mutex instead of one per attributetype.
163 * I don't believe this is a significant bottleneck. If
164 * necessary, could change to a per-AttrType rwlock.
166 ldap_pvt_thread_mutex_lock( &ad_mutex );
167 /* Check again just in case another thread added it */
168 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
169 if (d2->ad_flags != desc.ad_flags)
171 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
173 if (d2->ad_lang.bv_len == 0)
175 if (strncasecmp(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
176 desc.ad_lang.bv_len) == 0)
179 /* Some other thread added it before we got the lock. */
181 ldap_pvt_thread_mutex_unlock( &ad_mutex );
185 /* Allocate a single contiguous block. If there are no
186 * options, we just need space for the AttrDesc structure.
187 * Otherwise, we need to tack on the full name length +
190 i = sizeof(AttributeDescription);
191 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
192 if (desc.ad_lang.bv_len)
193 dlen = desc.ad_lang.bv_len+1;
194 dlen += strlen(desc.ad_type->sat_cname)+1;
195 if( slap_ad_is_binary( &desc ) ) {
196 dlen += sizeof("binary");
200 d2 = ch_malloc(i + dlen);
201 d2->ad_type = desc.ad_type;
202 d2->ad_flags = desc.ad_flags;
203 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
204 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
206 d2->ad_cname.bv_val = d2->ad_type->sat_cname;
207 d2->ad_lang.bv_val = NULL;
209 d2->ad_cname.bv_val = (char *)(d2+1);
210 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname);
211 if( slap_ad_is_binary( &desc ) ) {
212 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
214 d2->ad_cname.bv_len += sizeof("binary");
216 if( d2->ad_lang.bv_len ) {
217 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
218 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
220 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
222 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
223 ldap_pvt_str2lower(d2->ad_lang.bv_val);
224 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
227 /* Add new desc to list. We always want the bare Desc with
228 * no options to stay at the head of the list, assuming
229 * that one will be used most frequently.
231 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
232 d2->ad_next = desc.ad_type->sat_ad;
233 desc.ad_type->sat_ad = d2;
235 d2->ad_next = desc.ad_type->sat_ad->ad_next;
236 desc.ad_type->sat_ad->ad_next = d2;
238 ldap_pvt_thread_mutex_unlock( &ad_mutex );
254 AttributeDescription *sub,
255 AttributeDescription *super
258 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
262 if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
266 if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
267 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
268 sub->ad_lang.bv_val)))
278 AttributeDescription *desc,
282 for( i=0; attrs[i] != NULL; i++ ) {
283 AttributeDescription *ad = NULL;
287 rc = slap_str2ad( attrs[i], &ad, &text );
289 if( rc != LDAP_SUCCESS ) continue;
291 rc = is_ad_subtype( desc, ad );
300 int slap_str2undef_ad(
302 AttributeDescription **ad,
306 bv.bv_val = (char *) str;
307 bv.bv_len = strlen( str );
309 return slap_bv2undef_ad( &bv, ad, text );
312 int slap_bv2undef_ad(
314 AttributeDescription **ad,
317 AttributeDescription *desc;
319 assert( ad != NULL );
321 if( bv == NULL || bv->bv_len == 0 ) {
322 *text = "empty attribute description";
323 return LDAP_UNDEFINED_TYPE;
326 /* make sure description is IA5 */
327 if( ad_keystring( bv ) ) {
328 *text = "attribute description contains inappropriate characters";
329 return LDAP_UNDEFINED_TYPE;
332 for (desc = slap_schema.si_at_undefined->sat_ad; desc;
334 if (desc->ad_cname.bv_len == bv->bv_len &&
335 !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
339 desc = ch_malloc(sizeof(AttributeDescription) +
342 desc->ad_flags = SLAP_DESC_NONE;
343 desc->ad_lang.bv_val = NULL;
344 desc->ad_lang.bv_len = 0;
346 desc->ad_cname.bv_len = bv->bv_len;
347 desc->ad_cname.bv_val = (char *)(desc+1);
348 strcpy(desc->ad_cname.bv_val, bv->bv_val);
350 /* canonical to upper case */
351 ldap_pvt_str2upper( desc->ad_cname.bv_val );
353 desc->ad_type = slap_schema.si_at_undefined;
354 desc->ad_next = desc->ad_type->sat_ad;
355 desc->ad_type->sat_ad = desc;