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 static int ad_keystring(
26 if( !AD_CHAR( bv->bv_val[0] ) ) {
30 for( i=1; i<bv->bv_len; i++ ) {
31 if( !AD_CHAR( bv->bv_val[i] ) ) {
38 void ad_destroy( void *in )
40 AttributeDescription *ad = in, *n;
48 /* Is there an AttributeDescription for this type that uses this language? */
49 AttributeDescription * ad_find_lang(
53 AttributeDescription *ad;
55 ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
56 for (ad = type->sat_ad; ad; ad=ad->ad_next)
58 if (ad->ad_lang.bv_len == lang->bv_len &&
59 !strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
62 ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
68 AttributeDescription **ad,
72 bv.bv_val = (char *) str;
73 bv.bv_len = strlen( str );
75 return slap_bv2ad( &bv, ad, text );
80 AttributeDescription **ad,
83 int rtn = LDAP_UNDEFINED_TYPE;
85 AttributeDescription desc, *d2;
89 assert( *ad == NULL ); /* temporary */
91 if( bv == NULL || bv->bv_len == 0 ) {
92 *text = "empty attribute description";
96 /* make sure description is IA5 */
97 if( ad_keystring( bv ) ) {
98 *text = "attribute description contains inappropriate characters";
102 /* find valid base attribute type; parse in place */
104 options = strchr(name, ';');
105 if (options != NULL) *options = '\0';
106 desc.ad_type = at_find( name );
107 if (options != NULL) *options = ';';
108 if( desc.ad_type == NULL ) {
109 *text = "attribute type undefined";
113 if (options != NULL) {
114 desc.ad_cname.bv_len = options - name;
116 desc.ad_cname.bv_len = bv->bv_len;
119 desc.ad_flags = SLAP_DESC_NONE;
120 desc.ad_lang.bv_len = 0;
121 desc.ad_lang.bv_val = NULL;
123 /* parse options in place */
124 for( ; options != NULL; ) {
126 options = strchr( name, ';' );
127 if ( options != NULL )
132 if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
133 if( slap_ad_is_binary( &desc ) ) {
134 *text = "option \"binary\" specified multiple times";
138 if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
139 /* not stored in binary, disallow option */
140 *text = "option \"binary\" with type not supported";
144 desc.ad_flags |= SLAP_DESC_BINARY;
146 } else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
147 sizeof("lang-")-1 ) == 0)
149 if( desc.ad_lang.bv_len != 0 ) {
150 *text = "multiple language tag options specified";
154 desc.ad_lang.bv_val = name;
155 desc.ad_lang.bv_len = i;
157 *text = "unrecognized option";
162 ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
163 /* see if a matching description is already cached */
164 for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
165 if (d2->ad_flags != desc.ad_flags)
167 if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
169 if (d2->ad_lang.bv_len == 0)
171 if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
172 desc.ad_lang.bv_len) == 0)
176 /* Not found, add new one */
180 /* Allocate a single contiguous block. If there are no
181 * options, we just need space for the AttrDesc structure.
182 * Otherwise, we need to tack on the full name length +
185 i = sizeof(AttributeDescription);
186 if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
187 if (desc.ad_lang.bv_len)
188 dlen = desc.ad_lang.bv_len+1;
189 dlen += desc.ad_type->sat_cname.bv_len+1;
190 if( slap_ad_is_binary( &desc ) ) {
191 dlen += sizeof("binary");
195 d2 = ch_malloc(i + dlen);
196 d2->ad_type = desc.ad_type;
197 d2->ad_flags = desc.ad_flags;
198 d2->ad_cname.bv_len = desc.ad_cname.bv_len;
199 d2->ad_lang.bv_len = desc.ad_lang.bv_len;
201 d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
202 d2->ad_lang.bv_val = NULL;
204 d2->ad_cname.bv_val = (char *)(d2+1);
205 strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
206 if( slap_ad_is_binary( &desc ) ) {
207 strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
209 d2->ad_cname.bv_len += sizeof("binary");
211 if( d2->ad_lang.bv_len ) {
212 d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
213 d2->ad_lang.bv_val = d2->ad_cname.bv_val+
215 strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
217 d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
218 ldap_pvt_str2lower(d2->ad_lang.bv_val);
219 d2->ad_cname.bv_len += d2->ad_lang.bv_len;
222 /* Add new desc to list. We always want the bare Desc with
223 * no options to stay at the head of the list, assuming
224 * that one will be used most frequently.
226 if (desc.ad_type->sat_ad == NULL || dlen == 0) {
227 d2->ad_next = desc.ad_type->sat_ad;
228 desc.ad_type->sat_ad = d2;
230 d2->ad_next = desc.ad_type->sat_ad->ad_next;
231 desc.ad_type->sat_ad->ad_next = d2;
234 ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
249 AttributeDescription *sub,
250 AttributeDescription *super
253 if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
257 if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
261 if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
262 super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
263 sub->ad_lang.bv_val)))
273 AttributeDescription *desc,
277 for( i=0; attrs[i] != NULL; i++ ) {
279 AttributeDescription *ad = NULL;
283 rc = slap_str2ad( attrs[i], &ad, &text );
284 if( rc == LDAP_SUCCESS ) {
285 rc = is_ad_subtype( desc, ad );
291 * EXTENSION: see if requested description is an object class
292 * if so, return attributes which the class requires/allows
294 oc = oc_find( attrs[i] );
296 if ( oc == slap_schema.si_oc_extensibleObject ) {
297 /* extensibleObject allows the return of anything */
301 if( oc->soc_required ) {
302 /* allow return of required attributes */
304 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
305 rc = is_at_subtype( desc->ad_type,
306 oc->soc_allowed[i] );
310 if( oc->soc_allowed ) {
311 /* allow return of allowed attributes */
313 for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
314 rc = is_at_subtype( desc->ad_type,
315 oc->soc_allowed[i] );
326 int slap_str2undef_ad(
328 AttributeDescription **ad,
332 bv.bv_val = (char *) str;
333 bv.bv_len = strlen( str );
335 return slap_bv2undef_ad( &bv, ad, text );
338 int slap_bv2undef_ad(
340 AttributeDescription **ad,
343 AttributeDescription *desc;
345 assert( ad != NULL );
347 if( bv == NULL || bv->bv_len == 0 ) {
348 *text = "empty attribute description";
349 return LDAP_UNDEFINED_TYPE;
352 /* make sure description is IA5 */
353 if( ad_keystring( bv ) ) {
354 *text = "attribute description contains inappropriate characters";
355 return LDAP_UNDEFINED_TYPE;
358 for (desc = slap_schema.si_at_undefined->sat_ad; desc;
360 if (desc->ad_cname.bv_len == bv->bv_len &&
361 !strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
365 desc = ch_malloc(sizeof(AttributeDescription) +
368 desc->ad_flags = SLAP_DESC_NONE;
369 desc->ad_lang.bv_val = NULL;
370 desc->ad_lang.bv_len = 0;
372 desc->ad_cname.bv_len = bv->bv_len;
373 desc->ad_cname.bv_val = (char *)(desc+1);
374 strcpy(desc->ad_cname.bv_val, bv->bv_val);
376 /* canonical to upper case */
377 ldap_pvt_str2upper( desc->ad_cname.bv_val );
379 desc->ad_type = slap_schema.si_at_undefined;
380 desc->ad_next = desc->ad_type->sat_ad;
381 desc->ad_type->sat_ad = desc;