2000-02-07 05:09:44 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
2000-05-13 10:47:56 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
2000-02-07 05:09:44 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* ad.c - routines for dealing with attribute descriptions */
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
|
|
|
#include "ldap_pvt.h"
|
|
|
|
#include "slap.h"
|
|
|
|
|
2000-02-15 04:57:34 +08:00
|
|
|
static int ad_keystring(
|
|
|
|
struct berval *bv )
|
|
|
|
{
|
|
|
|
ber_len_t i;
|
|
|
|
|
|
|
|
if( !AD_CHAR( bv->bv_val[0] ) ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i=1; i<bv->bv_len; i++ ) {
|
|
|
|
if( !AD_CHAR( bv->bv_val[i] ) ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-07 09:03:49 +08:00
|
|
|
void ad_destroy( void *in )
|
|
|
|
{
|
|
|
|
AttributeDescription *ad = in, *n;
|
|
|
|
|
|
|
|
for (;ad;ad = n) {
|
|
|
|
n = ad->ad_next;
|
|
|
|
ldap_memfree(ad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-16 15:39:14 +08:00
|
|
|
/* Is there an AttributeDescription for this type that uses this language? */
|
|
|
|
AttributeDescription * ad_find_lang(
|
|
|
|
AttributeType *type,
|
|
|
|
struct berval *lang )
|
|
|
|
{
|
|
|
|
AttributeDescription *ad;
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_lock( &type->sat_ad_mutex );
|
|
|
|
for (ad = type->sat_ad; ad; ad=ad->ad_next)
|
|
|
|
{
|
|
|
|
if (ad->ad_lang.bv_len == lang->bv_len &&
|
|
|
|
!strcasecmp(ad->ad_lang.bv_val, lang->bv_val))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_mutex_unlock( &type->sat_ad_mutex );
|
|
|
|
return ad;
|
|
|
|
}
|
|
|
|
|
2000-02-15 04:57:34 +08:00
|
|
|
int slap_str2ad(
|
|
|
|
const char *str,
|
|
|
|
AttributeDescription **ad,
|
2000-05-22 11:46:57 +08:00
|
|
|
const char **text )
|
2000-02-15 04:57:34 +08:00
|
|
|
{
|
|
|
|
struct berval bv;
|
|
|
|
bv.bv_val = (char *) str;
|
|
|
|
bv.bv_len = strlen( str );
|
|
|
|
|
|
|
|
return slap_bv2ad( &bv, ad, text );
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:09:44 +08:00
|
|
|
int slap_bv2ad(
|
|
|
|
struct berval *bv,
|
|
|
|
AttributeDescription **ad,
|
2000-05-22 11:46:57 +08:00
|
|
|
const char **text )
|
2000-02-07 05:09:44 +08:00
|
|
|
{
|
|
|
|
int rtn = LDAP_UNDEFINED_TYPE;
|
|
|
|
int i;
|
2001-10-22 21:23:05 +08:00
|
|
|
AttributeDescription desc, *d2;
|
|
|
|
char *name, *options;
|
2000-02-07 05:09:44 +08:00
|
|
|
|
2000-05-16 02:46:03 +08:00
|
|
|
assert( ad != NULL );
|
2000-05-17 06:04:49 +08:00
|
|
|
assert( *ad == NULL ); /* temporary */
|
2000-02-07 05:09:44 +08:00
|
|
|
|
|
|
|
if( bv == NULL || bv->bv_len == 0 ) {
|
|
|
|
*text = "empty attribute description";
|
2000-05-16 02:46:03 +08:00
|
|
|
return rtn;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure description is IA5 */
|
2000-02-15 04:57:34 +08:00
|
|
|
if( ad_keystring( bv ) ) {
|
|
|
|
*text = "attribute description contains inappropriate characters";
|
2000-05-16 02:46:03 +08:00
|
|
|
return rtn;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
/* find valid base attribute type; parse in place */
|
|
|
|
name = bv->bv_val;
|
|
|
|
options = strchr(name, ';');
|
|
|
|
if (options != NULL) *options = '\0';
|
|
|
|
desc.ad_type = at_find( name );
|
|
|
|
if (options != NULL) *options = ';';
|
2000-02-07 05:09:44 +08:00
|
|
|
if( desc.ad_type == NULL ) {
|
|
|
|
*text = "attribute type undefined";
|
2001-10-22 21:23:05 +08:00
|
|
|
return rtn;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
|
2001-11-14 02:16:07 +08:00
|
|
|
if (options != NULL) {
|
2001-10-22 21:23:05 +08:00
|
|
|
desc.ad_cname.bv_len = options - name;
|
2001-11-14 02:16:07 +08:00
|
|
|
} else {
|
2001-11-12 11:15:40 +08:00
|
|
|
desc.ad_cname.bv_len = bv->bv_len;
|
2001-11-14 02:16:07 +08:00
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
desc.ad_flags = SLAP_DESC_NONE;
|
|
|
|
desc.ad_lang.bv_len = 0;
|
|
|
|
desc.ad_lang.bv_val = NULL;
|
|
|
|
|
|
|
|
/* parse options in place */
|
|
|
|
for( ; options != NULL; ) {
|
|
|
|
name = options+1;
|
|
|
|
options = strchr( name, ';' );
|
|
|
|
if ( options != NULL )
|
|
|
|
i = options - name;
|
|
|
|
else
|
|
|
|
i = strlen(name);
|
|
|
|
|
|
|
|
if( i == sizeof("binary")-1 && strncasecmp( name, "binary", i) == 0 ) {
|
2000-05-19 01:21:42 +08:00
|
|
|
if( slap_ad_is_binary( &desc ) ) {
|
2000-02-07 05:09:44 +08:00
|
|
|
*text = "option \"binary\" specified multiple times";
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2000-05-19 01:21:42 +08:00
|
|
|
if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
|
2000-02-07 05:09:44 +08:00
|
|
|
/* not stored in binary, disallow option */
|
|
|
|
*text = "option \"binary\" with type not supported";
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.ad_flags |= SLAP_DESC_BINARY;
|
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
} else if ( i >= sizeof("lang-") && strncasecmp( name, "lang-",
|
|
|
|
sizeof("lang-")-1 ) == 0)
|
2000-02-07 05:09:44 +08:00
|
|
|
{
|
2001-10-22 21:23:05 +08:00
|
|
|
if( desc.ad_lang.bv_len != 0 ) {
|
2000-02-07 05:09:44 +08:00
|
|
|
*text = "multiple language tag options specified";
|
|
|
|
goto done;
|
|
|
|
}
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
desc.ad_lang.bv_val = name;
|
|
|
|
desc.ad_lang.bv_len = i;
|
2000-02-07 05:09:44 +08:00
|
|
|
} else {
|
|
|
|
*text = "unrecognized option";
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-16 15:39:14 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &desc.ad_type->sat_ad_mutex );
|
2001-10-22 21:23:05 +08:00
|
|
|
/* see if a matching description is already cached */
|
|
|
|
for (d2 = desc.ad_type->sat_ad; d2; d2=d2->ad_next) {
|
|
|
|
if (d2->ad_flags != desc.ad_flags)
|
|
|
|
continue;
|
|
|
|
if (d2->ad_lang.bv_len != desc.ad_lang.bv_len)
|
|
|
|
continue;
|
|
|
|
if (d2->ad_lang.bv_len == 0)
|
|
|
|
break;
|
|
|
|
if (strncasecmp(d2->ad_lang.bv_val, desc.ad_lang.bv_val,
|
|
|
|
desc.ad_lang.bv_len) == 0)
|
|
|
|
break;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
/* Not found, add new one */
|
|
|
|
while (d2 == NULL) {
|
|
|
|
int dlen = 0;
|
2000-02-07 05:09:44 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
/* Allocate a single contiguous block. If there are no
|
|
|
|
* options, we just need space for the AttrDesc structure.
|
|
|
|
* Otherwise, we need to tack on the full name length +
|
|
|
|
* options length.
|
|
|
|
*/
|
|
|
|
i = sizeof(AttributeDescription);
|
|
|
|
if (desc.ad_lang.bv_len || desc.ad_flags != SLAP_DESC_NONE) {
|
|
|
|
if (desc.ad_lang.bv_len)
|
|
|
|
dlen = desc.ad_lang.bv_len+1;
|
2001-11-16 15:39:14 +08:00
|
|
|
dlen += desc.ad_type->sat_cname.bv_len+1;
|
2001-10-22 21:23:05 +08:00
|
|
|
if( slap_ad_is_binary( &desc ) ) {
|
|
|
|
dlen += sizeof("binary");
|
|
|
|
}
|
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
d2 = ch_malloc(i + dlen);
|
|
|
|
d2->ad_type = desc.ad_type;
|
|
|
|
d2->ad_flags = desc.ad_flags;
|
|
|
|
d2->ad_cname.bv_len = desc.ad_cname.bv_len;
|
|
|
|
d2->ad_lang.bv_len = desc.ad_lang.bv_len;
|
|
|
|
if (dlen == 0) {
|
2001-11-16 15:39:14 +08:00
|
|
|
d2->ad_cname.bv_val = d2->ad_type->sat_cname.bv_val;
|
2001-10-22 21:23:05 +08:00
|
|
|
d2->ad_lang.bv_val = NULL;
|
|
|
|
} else {
|
|
|
|
d2->ad_cname.bv_val = (char *)(d2+1);
|
2001-11-16 15:39:14 +08:00
|
|
|
strcpy(d2->ad_cname.bv_val, d2->ad_type->sat_cname.bv_val);
|
2001-10-22 21:23:05 +08:00
|
|
|
if( slap_ad_is_binary( &desc ) ) {
|
|
|
|
strcpy(d2->ad_cname.bv_val+d2->ad_cname.bv_len,
|
|
|
|
";binary");
|
|
|
|
d2->ad_cname.bv_len += sizeof("binary");
|
|
|
|
}
|
|
|
|
if( d2->ad_lang.bv_len ) {
|
|
|
|
d2->ad_cname.bv_val[d2->ad_cname.bv_len++]=';';
|
|
|
|
d2->ad_lang.bv_val = d2->ad_cname.bv_val+
|
|
|
|
d2->ad_cname.bv_len;
|
|
|
|
strncpy(d2->ad_lang.bv_val,desc.ad_lang.bv_val,
|
|
|
|
d2->ad_lang.bv_len);
|
|
|
|
d2->ad_lang.bv_val[d2->ad_lang.bv_len] = '\0';
|
|
|
|
ldap_pvt_str2lower(d2->ad_lang.bv_val);
|
|
|
|
d2->ad_cname.bv_len += d2->ad_lang.bv_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Add new desc to list. We always want the bare Desc with
|
|
|
|
* no options to stay at the head of the list, assuming
|
|
|
|
* that one will be used most frequently.
|
|
|
|
*/
|
|
|
|
if (desc.ad_type->sat_ad == NULL || dlen == 0) {
|
|
|
|
d2->ad_next = desc.ad_type->sat_ad;
|
|
|
|
desc.ad_type->sat_ad = d2;
|
|
|
|
} else {
|
|
|
|
d2->ad_next = desc.ad_type->sat_ad->ad_next;
|
|
|
|
desc.ad_type->sat_ad->ad_next = d2;
|
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
2001-11-16 15:39:14 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &desc.ad_type->sat_ad_mutex );
|
2000-02-07 05:09:44 +08:00
|
|
|
|
2000-02-26 03:36:07 +08:00
|
|
|
if( *ad == NULL ) {
|
2001-10-22 21:23:05 +08:00
|
|
|
*ad = d2;
|
|
|
|
} else {
|
|
|
|
**ad = *d2;
|
2000-02-26 03:36:07 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 05:09:44 +08:00
|
|
|
rtn = LDAP_SUCCESS;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2000-02-29 05:16:05 +08:00
|
|
|
int is_ad_subtype(
|
|
|
|
AttributeDescription *sub,
|
|
|
|
AttributeDescription *super
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-09-14 14:07:23 +08:00
|
|
|
if( super->ad_flags && ( super->ad_flags != sub->ad_flags )) {
|
2000-02-29 05:16:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
if( super->ad_lang.bv_len && (sub->ad_lang.bv_len !=
|
|
|
|
super->ad_lang.bv_len || strcmp( super->ad_lang.bv_val,
|
|
|
|
sub->ad_lang.bv_val)))
|
2000-02-29 05:16:05 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-26 03:36:07 +08:00
|
|
|
int ad_inlist(
|
|
|
|
AttributeDescription *desc,
|
|
|
|
char **attrs )
|
2000-02-07 05:09:44 +08:00
|
|
|
{
|
2000-02-26 03:36:07 +08:00
|
|
|
int i;
|
|
|
|
for( i=0; attrs[i] != NULL; i++ ) {
|
2001-12-06 09:02:11 +08:00
|
|
|
ObjectClass *oc;
|
2000-02-26 03:36:07 +08:00
|
|
|
AttributeDescription *ad = NULL;
|
2000-05-22 11:46:57 +08:00
|
|
|
const char *text;
|
2000-02-29 05:16:05 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = slap_str2ad( attrs[i], &ad, &text );
|
2001-12-06 09:02:11 +08:00
|
|
|
if( rc == LDAP_SUCCESS ) {
|
|
|
|
rc = is_ad_subtype( desc, ad );
|
|
|
|
if( rc ) return 1;
|
|
|
|
continue;
|
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
|
2001-12-06 09:02:11 +08:00
|
|
|
/*
|
|
|
|
* EXTENSION: see if requested description is an object class
|
|
|
|
* if so, return attributes which the class requires/allows
|
|
|
|
*/
|
|
|
|
oc = oc_find( attrs[i] );
|
|
|
|
if( oc != NULL ) {
|
|
|
|
if ( oc == slap_schema.si_oc_extensibleObject ) {
|
|
|
|
/* extensibleObject allows the return of anything */
|
|
|
|
return 1;
|
|
|
|
}
|
2000-02-26 03:36:07 +08:00
|
|
|
|
2001-12-06 09:02:11 +08:00
|
|
|
if( oc->soc_required ) {
|
|
|
|
/* allow return of required attributes */
|
|
|
|
int i;
|
|
|
|
for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
|
|
|
|
rc = is_at_subtype( desc->ad_type,
|
|
|
|
oc->soc_allowed[i] );
|
|
|
|
if( rc ) return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( oc->soc_allowed ) {
|
|
|
|
/* allow return of allowed attributes */
|
|
|
|
int i;
|
|
|
|
for ( i = 0; oc->soc_allowed[i] != NULL; i++ ) {
|
|
|
|
rc = is_at_subtype( desc->ad_type,
|
|
|
|
oc->soc_allowed[i] );
|
|
|
|
if( rc ) return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-26 03:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-03 01:16:18 +08:00
|
|
|
int slap_str2undef_ad(
|
|
|
|
const char *str,
|
|
|
|
AttributeDescription **ad,
|
|
|
|
const char **text )
|
|
|
|
{
|
|
|
|
struct berval bv;
|
|
|
|
bv.bv_val = (char *) str;
|
|
|
|
bv.bv_len = strlen( str );
|
|
|
|
|
|
|
|
return slap_bv2undef_ad( &bv, ad, text );
|
|
|
|
}
|
|
|
|
|
|
|
|
int slap_bv2undef_ad(
|
|
|
|
struct berval *bv,
|
|
|
|
AttributeDescription **ad,
|
|
|
|
const char **text )
|
|
|
|
{
|
2001-10-22 21:23:05 +08:00
|
|
|
AttributeDescription *desc;
|
2000-07-03 01:16:18 +08:00
|
|
|
|
|
|
|
assert( ad != NULL );
|
|
|
|
|
|
|
|
if( bv == NULL || bv->bv_len == 0 ) {
|
|
|
|
*text = "empty attribute description";
|
|
|
|
return LDAP_UNDEFINED_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure description is IA5 */
|
|
|
|
if( ad_keystring( bv ) ) {
|
|
|
|
*text = "attribute description contains inappropriate characters";
|
|
|
|
return LDAP_UNDEFINED_TYPE;
|
|
|
|
}
|
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
for (desc = slap_schema.si_at_undefined->sat_ad; desc;
|
|
|
|
desc=desc->ad_next)
|
|
|
|
if (desc->ad_cname.bv_len == bv->bv_len &&
|
|
|
|
!strcasecmp(desc->ad_cname.bv_val, bv->bv_val))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!desc) {
|
|
|
|
desc = ch_malloc(sizeof(AttributeDescription) +
|
|
|
|
bv->bv_len + 1);
|
|
|
|
|
|
|
|
desc->ad_flags = SLAP_DESC_NONE;
|
|
|
|
desc->ad_lang.bv_val = NULL;
|
|
|
|
desc->ad_lang.bv_len = 0;
|
2000-07-03 01:16:18 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
desc->ad_cname.bv_len = bv->bv_len;
|
|
|
|
desc->ad_cname.bv_val = (char *)(desc+1);
|
|
|
|
strcpy(desc->ad_cname.bv_val, bv->bv_val);
|
2000-07-03 01:16:18 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
/* canonical to upper case */
|
|
|
|
ldap_pvt_str2upper( desc->ad_cname.bv_val );
|
2000-07-03 01:16:18 +08:00
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
desc->ad_type = slap_schema.si_at_undefined;
|
|
|
|
desc->ad_next = desc->ad_type->sat_ad;
|
|
|
|
desc->ad_type->sat_ad = desc;
|
2000-07-03 01:16:18 +08:00
|
|
|
}
|
|
|
|
|
2001-10-22 21:23:05 +08:00
|
|
|
if (!*ad)
|
|
|
|
*ad = desc;
|
|
|
|
else
|
|
|
|
**ad = *desc;
|
2000-07-03 01:16:18 +08:00
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|