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] ) ) {
42 AttributeDescription **ad,
46 bv.bv_val = (char *) str;
47 bv.bv_len = strlen( str );
49 return slap_bv2ad( &bv, ad, text );
54 AttributeDescription **ad,
57 int rtn = LDAP_UNDEFINED_TYPE;
59 AttributeDescription desc, *d2;
63 assert( *ad == NULL ); /* temporary */
65 if( bv == NULL || bv->bv_len == 0 ) {
66 *text = "empty attribute description";
70 /* make sure description is IA5 */
71 if( ad_keystring( bv ) ) {
72 *text = "attribute description contains inappropriate characters";
76 /* find valid base attribute type; parse in place */
78 options = strchr(name, ';');
79 if (options != NULL) *options = '\0';
80 desc.ad_type = at_find( name );
81 if (options != NULL) *options = ';';
82 if( desc.ad_type == NULL ) {
83 *text = "attribute type undefined";
89 desc.ad_cname.bv_len = options - name;
91 desc.ad_cname.bv_len = strlen(name);
93 desc.ad_flags = SLAP_DESC_NONE;
94 desc.ad_lang.bv_len = 0;
95 desc.ad_lang.bv_val = NULL;
97 /* parse options in place */
98 for( ; options != NULL; ) {
100 options = strchr( name, ';' );
101 if ( options != NULL )
106 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
107 if( slap_ad_is_binary( &desc ) ) {
108 *text = "option \"binary\" specified multiple times";
112 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
113 /* not stored in binary, disallow option */
114 *text = "option \"binary\" with type not supported";
118 desc.ad_flags |= SLAP_DESC_BINARY;
120 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
121 sizeof("lang-")-1 ) == 0)
123 if( desc.ad_lang.bv_len != 0 ) {
124 *text = "multiple language tag options specified";
128 desc.ad_lang.bv_val = name;
129 desc.ad_lang.bv_len = i;
131 *text = "unrecognized option";
136 /* see if a matching description is already cached */
137 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
138 if (d2->ad_flags != desc.ad_flags)
140 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
142 if (d2->ad_lang.bv_len == 0)
144 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
145 desc.ad_lang.bv_len) == 0)
149 /* Not found, add new one */
152 /* uses a single mutex instead of one per attributetype.
153 * I don't believe this is a significant bottleneck. If
154 * necessary, could change to a per-AttrType rwlock.
156 ldap_pvt_thread_mutex_lock( &ad_mutex );
157 /* Check again just in case another thread added it */
158 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
159 if (d2->ad_flags != desc.ad_flags)
161 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
163 if (d2->ad_lang.bv_len == 0)
165 if (strncasecmp(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
166 desc.ad_lang.bv_len) == 0)
169 /* Some other thread added it before we got the lock. */
171 ldap_pvt_thread_mutex_unlock( &ad_mutex );
175 /* Allocate a single contiguous block. If there are no
176 * options, we just need space for the AttrDesc structure.
177 * Otherwise, we need to tack on the full name length +
180 i = sizeof(AttributeDescription);
181 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
182 if (desc.ad_lang.bv_len)
183 dlen = desc.ad_lang.bv_len+1;
184 dlen += strlen(desc.ad_type->sat_cname)+1;
185 if( slap_ad_is_binary( &desc ) ) {
186 dlen += sizeof("binary");
190 d2 = ch_malloc(i + dlen);
191 d2->ad_type = desc.ad_type;
192 d2->ad_flags = desc.ad_flags;
193 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
194 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
196 d2->ad_cname.bv_val = d2->ad_type->sat_cname;
197 d2->ad_lang.bv_val = NULL;
199 d2->ad_cname.bv_val = (char *)(d2+1);
200 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname);
201 if( slap_ad_is_binary( &desc ) ) {
202 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
204 d2->ad_cname.bv_len += sizeof("binary");
206 if( d2->ad_lang.bv_len ) {
207 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
208 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
210 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
212 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
213 ldap_pvt_str2lower(d2->ad_lang.bv_val);
214 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
217 /* Add new desc to list. We always want the bare Desc with
218 * no options to stay at the head of the list, assuming
219 * that one will be used most frequently.
221 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
222 d2->ad_next = desc.ad_type->sat_ad;
223 desc.ad_type->sat_ad = d2;
225 d2->ad_next = desc.ad_type->sat_ad->ad_next;
226 desc.ad_type->sat_ad->ad_next = d2;
228 ldap_pvt_thread_mutex_unlock( &ad_mutex );
244 AttributeDescription *sub,
245 AttributeDescription *super
248 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
252 if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
256 if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
257 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
258 sub->ad_lang.bv_val)))
268 AttributeDescription *desc,
272 for( i=0; attrs[i] != NULL; i++ ) {
273 AttributeDescription *ad = NULL;
277 rc = slap_str2ad( attrs[i], &ad, &text );
279 if( rc != LDAP_SUCCESS ) continue;
281 rc = is_ad_subtype( desc, ad );
290 int slap_str2undef_ad(
292 AttributeDescription **ad,
296 bv.bv_val = (char *) str;
297 bv.bv_len = strlen( str );
299 return slap_bv2undef_ad( &bv, ad, text );
302 int slap_bv2undef_ad(
304 AttributeDescription **ad,
307 AttributeDescription *desc;
309 assert( ad != NULL );
311 if( bv == NULL || bv->bv_len == 0 ) {
312 *text = "empty attribute description";
313 return LDAP_UNDEFINED_TYPE;
316 /* make sure description is IA5 */
317 if( ad_keystring( bv ) ) {
318 *text = "attribute description contains inappropriate characters";
319 return LDAP_UNDEFINED_TYPE;
322 for (desc = slap_schema.si_at_undefined->sat_ad; desc;
324 if (desc->ad_cname.bv_len == bv->bv_len &&
325 !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
329 desc = ch_malloc(sizeof(AttributeDescription) +
332 desc->ad_flags = SLAP_DESC_NONE;
333 desc->ad_lang.bv_val = NULL;
334 desc->ad_lang.bv_len = 0;
336 desc->ad_cname.bv_len = bv->bv_len;
337 desc->ad_cname.bv_val = (char *)(desc+1);
338 strcpy(desc->ad_cname.bv_val, bv->bv_val);
340 /* canonical to upper case */
341 ldap_pvt_str2upper( desc->ad_cname.bv_val );
343 desc->ad_type = slap_schema.si_at_undefined;
344 desc->ad_next = desc->ad_type->sat_ad;
345 desc->ad_type->sat_ad = desc;