1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
29 bdb_attr_slot( struct bdb_info *bdb, AttributeDescription *ad )
31 unsigned base = 0, cursor = 0;
32 unsigned n = bdb->bi_nattrs;
37 cursor = base + pivot;
39 val = SLAP_PTRCMP( ad, bdb->bi_attrs[cursor]->ai_desc );
42 } else if ( val > 0 ) {
55 ainfo_insert( struct bdb_info *bdb, AttrInfo *a )
57 unsigned x = bdb_attr_slot( bdb, a->ai_desc );
60 if ( x < bdb->bi_nattrs && bdb->bi_attrs[x]->ai_desc == a->ai_desc )
63 bdb->bi_attrs = ch_realloc( bdb->bi_attrs, ( bdb->bi_nattrs+1 ) *
64 sizeof( AttrInfo * ));
65 if ( x < bdb->bi_nattrs )
66 AC_MEMCPY( &bdb->bi_attrs[x+1], &bdb->bi_attrs[x],
67 ( bdb->bi_nattrs - x ) * sizeof( AttrInfo *));
76 AttributeDescription *desc )
78 unsigned i = bdb_attr_slot( bdb, desc );
79 return ( i < bdb->bi_nattrs && bdb->bi_attrs[i]->ai_desc == desc ) ?
80 bdb->bi_attrs[i] : NULL;
84 bdb_attr_index_config(
95 char **indexes = NULL;
97 attrs = ldap_str2charray( argv[0], "," );
100 fprintf( stderr, "%s: line %d: "
101 "no attributes specified: %s\n",
102 fname, lineno, argv[0] );
103 return LDAP_PARAM_ERROR;
107 indexes = ldap_str2charray( argv[1], "," );
109 if( indexes == NULL ) {
110 fprintf( stderr, "%s: line %d: "
111 "no indexes specified: %s\n",
112 fname, lineno, argv[1] );
113 return LDAP_PARAM_ERROR;
117 if( indexes == NULL ) {
118 mask = bdb->bi_defaultmask;
123 for ( i = 0; indexes[i] != NULL; i++ ) {
125 rc = slap_str2index( indexes[i], &index );
127 if( rc != LDAP_SUCCESS ) {
128 fprintf( stderr, "%s: line %d: "
129 "index type \"%s\" undefined\n",
130 fname, lineno, indexes[i] );
131 return LDAP_PARAM_ERROR;
139 fprintf( stderr, "%s: line %d: "
140 "no indexes selected\n",
142 return LDAP_PARAM_ERROR;
145 for ( i = 0; attrs[i] != NULL; i++ ) {
147 AttributeDescription *ad;
149 #ifdef LDAP_COMP_MATCH
150 ComponentReference* cr = NULL;
151 AttrInfo *a_cr = NULL;
154 if( strcasecmp( attrs[i], "default" ) == 0 ) {
155 bdb->bi_defaultmask |= mask;
159 #ifdef LDAP_COMP_MATCH
160 if ( is_component_reference( attrs[i] ) ) {
161 rc = extract_component_reference( attrs[i], &cr );
162 if ( rc != LDAP_SUCCESS ) {
163 fprintf( stderr, "%s: line %d: "
164 "index component reference\"%s\" undefined\n",
165 fname, lineno, attrs[i] );
168 cr->cr_indexmask = mask;
170 * After extracting a component reference
171 * only the name of a attribute will be remaining
177 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
179 #ifdef LDAP_COMP_MATCH
183 rc = slap_str2ad( attrs[i], &ad, &text );
185 if( rc != LDAP_SUCCESS ) {
186 fprintf( stderr, "%s: line %d: "
187 "index attribute \"%s\" undefined\n",
188 fname, lineno, attrs[i] );
192 if( slap_ad_is_binary( ad ) ) {
193 fprintf( stderr, "%s: line %d: "
194 "index of attribute \"%s\" disallowed\n",
195 fname, lineno, attrs[i] );
196 return LDAP_UNWILLING_TO_PERFORM;
199 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
200 ad->ad_type->sat_approx
201 && ad->ad_type->sat_approx->smr_indexer
202 && ad->ad_type->sat_approx->smr_filter ) )
204 fprintf( stderr, "%s: line %d: "
205 "approx index of attribute \"%s\" disallowed\n",
206 fname, lineno, attrs[i] );
207 return LDAP_INAPPROPRIATE_MATCHING;
210 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
211 ad->ad_type->sat_equality
212 && ad->ad_type->sat_equality->smr_indexer
213 && ad->ad_type->sat_equality->smr_filter ) )
215 fprintf( stderr, "%s: line %d: "
216 "equality index of attribute \"%s\" disallowed\n",
217 fname, lineno, attrs[i] );
218 return LDAP_INAPPROPRIATE_MATCHING;
221 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
222 ad->ad_type->sat_substr
223 && ad->ad_type->sat_substr->smr_indexer
224 && ad->ad_type->sat_substr->smr_filter ) )
226 fprintf( stderr, "%s: line %d: "
227 "substr index of attribute \"%s\" disallowed\n",
228 fname, lineno, attrs[i] );
229 return LDAP_INAPPROPRIATE_MATCHING;
232 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
233 ad->ad_cname.bv_val, mask, 0 );
237 if ( bdb->bi_flags & BDB_IS_OPEN ) {
239 a->ai_newmask = mask;
241 a->ai_indexmask = mask;
245 #ifdef LDAP_COMP_MATCH
247 a_cr = bdb_attr_mask( bdb, ad );
250 * AttrInfo is already in AVL
251 * just add the extracted component reference
254 rc = insert_component_reference( cr, &a_cr->ai_cr );
255 if ( rc != LDAP_SUCCESS) {
256 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
257 return LDAP_PARAM_ERROR;
261 rc = insert_component_reference( cr, &a->ai_cr );
262 if ( rc != LDAP_SUCCESS) {
263 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
264 return LDAP_PARAM_ERROR;
269 rc = ainfo_insert( bdb, a );
271 if ( bdb->bi_flags & BDB_IS_OPEN ) {
272 AttrInfo *b = bdb_attr_mask( bdb, ad );
273 /* If we were editing this attr, reset it */
274 b->ai_indexmask &= ~BDB_INDEX_DELETING;
275 /* If this is leftover from a previous add, commit it */
277 b->ai_indexmask = b->ai_newmask;
278 b->ai_newmask = a->ai_newmask;
282 fprintf( stderr, "%s: line %d: duplicate index definition "
283 "for attr \"%s\"" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
284 fname, lineno, attrs[i] );
286 return LDAP_PARAM_ERROR;
290 ldap_charray_free( attrs );
291 if ( indexes != NULL ) ldap_charray_free( indexes );
297 bdb_attr_index_unparser( void *v1, void *v2 )
304 slap_index2bvlen( ai->ai_indexmask, &bv );
306 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
307 ptr = ch_malloc( bv.bv_len+1 );
308 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
310 slap_index2bv( ai->ai_indexmask, &bv );
312 ber_bvarray_add( bva, &bv );
317 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
318 static AttrInfo aidef = { &addef };
321 bdb_attr_index_unparse( struct bdb_info *bdb, BerVarray *bva )
325 if ( bdb->bi_defaultmask ) {
326 aidef.ai_indexmask = bdb->bi_defaultmask;
327 bdb_attr_index_unparser( &aidef, bva );
329 for ( i=0; i<bdb->bi_nattrs; i++ )
330 bdb_attr_index_unparser( bdb->bi_attrs[i], bva );
334 bdb_attr_info_free( AttrInfo *ai )
336 #ifdef LDAP_COMP_MATCH
343 bdb_attr_index_destroy( struct bdb_info *bdb )
347 for ( i=0; i<bdb->bi_nattrs; i++ )
348 bdb_attr_info_free( bdb->bi_attrs[i] );
350 free( bdb->bi_attrs );
353 void bdb_attr_index_free( struct bdb_info *bdb, AttributeDescription *ad )
357 i = bdb_attr_slot( bdb, ad );
358 if ( i < bdb->bi_nattrs && bdb->bi_attrs[i]->ai_desc == ad ) {
359 bdb_attr_info_free( bdb->bi_attrs[i] );
361 for (; i<bdb->bi_nattrs; i++)
362 bdb->bi_attrs[i] = bdb->bi_attrs[i+1];
366 void bdb_attr_flush( struct bdb_info *bdb )
370 for ( i=0; i<bdb->bi_nattrs; i++ ) {
371 if ( bdb->bi_attrs[i]->ai_indexmask & BDB_INDEX_DELETING ) {
373 bdb_attr_info_free( bdb->bi_attrs[i] );
375 for (j=i; j<bdb->bi_nattrs; j++)
376 bdb->bi_attrs[j] = bdb->bi_attrs[j+1];