1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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 /* Find the ad, return -1 if not found,
30 * set point for insertion if ins is non-NULL
33 bdb_attr_slot( struct bdb_info *bdb, AttributeDescription *ad, int *ins )
35 unsigned base = 0, cursor = 0;
36 unsigned n = bdb->bi_nattrs;
40 unsigned pivot = n >> 1;
41 cursor = base + pivot;
43 val = SLAP_PTRCMP( ad, bdb->bi_attrs[cursor]->ai_desc );
46 } else if ( val > 0 ) {
62 ainfo_insert( struct bdb_info *bdb, AttrInfo *a )
65 int i = bdb_attr_slot( bdb, a->ai_desc, &x );
71 bdb->bi_attrs = ch_realloc( bdb->bi_attrs, ( bdb->bi_nattrs+1 ) *
72 sizeof( AttrInfo * ));
73 if ( x < bdb->bi_nattrs )
74 AC_MEMCPY( &bdb->bi_attrs[x+1], &bdb->bi_attrs[x],
75 ( bdb->bi_nattrs - x ) * sizeof( AttrInfo *));
84 AttributeDescription *desc )
86 int i = bdb_attr_slot( bdb, desc, NULL );
87 return i < 0 ? NULL : bdb->bi_attrs[i];
91 bdb_attr_index_config(
97 struct config_reply_s *c_reply)
103 char **indexes = NULL;
105 attrs = ldap_str2charray( argv[0], "," );
107 if( attrs == NULL ) {
108 fprintf( stderr, "%s: line %d: "
109 "no attributes specified: %s\n",
110 fname, lineno, argv[0] );
111 return LDAP_PARAM_ERROR;
115 indexes = ldap_str2charray( argv[1], "," );
117 if( indexes == NULL ) {
118 fprintf( stderr, "%s: line %d: "
119 "no indexes specified: %s\n",
120 fname, lineno, argv[1] );
121 rc = LDAP_PARAM_ERROR;
126 if( indexes == NULL ) {
127 mask = bdb->bi_defaultmask;
132 for ( i = 0; indexes[i] != NULL; i++ ) {
134 rc = slap_str2index( indexes[i], &index );
136 if( rc != LDAP_SUCCESS ) {
139 snprintf(c_reply->msg, sizeof(c_reply->msg),
140 "index type \"%s\" undefined", indexes[i] );
142 fprintf( stderr, "%s: line %d: %s\n",
143 fname, lineno, c_reply->msg );
145 rc = LDAP_PARAM_ERROR;
156 snprintf(c_reply->msg, sizeof(c_reply->msg),
157 "no indexes selected" );
158 fprintf( stderr, "%s: line %d: %s\n",
159 fname, lineno, c_reply->msg );
161 rc = LDAP_PARAM_ERROR;
165 for ( i = 0; attrs[i] != NULL; i++ ) {
167 AttributeDescription *ad;
169 #ifdef LDAP_COMP_MATCH
170 ComponentReference* cr = NULL;
171 AttrInfo *a_cr = NULL;
174 if( strcasecmp( attrs[i], "default" ) == 0 ) {
175 bdb->bi_defaultmask |= mask;
179 #ifdef LDAP_COMP_MATCH
180 if ( is_component_reference( attrs[i] ) ) {
181 rc = extract_component_reference( attrs[i], &cr );
182 if ( rc != LDAP_SUCCESS ) {
185 snprintf(c_reply->msg, sizeof(c_reply->msg),
186 "index component reference\"%s\" undefined",
188 fprintf( stderr, "%s: line %d: %s\n",
189 fname, lineno, c_reply->msg );
193 cr->cr_indexmask = mask;
195 * After extracting a component reference
196 * only the name of a attribute will be remaining
203 rc = slap_str2ad( attrs[i], &ad, &text );
205 if( rc != LDAP_SUCCESS ) {
208 snprintf(c_reply->msg, sizeof(c_reply->msg),
209 "index attribute \"%s\" undefined",
212 fprintf( stderr, "%s: line %d: %s\n",
213 fname, lineno, c_reply->msg );
218 if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) {
220 snprintf(c_reply->msg, sizeof(c_reply->msg),
221 "index of attribute \"%s\" disallowed", attrs[i] );
222 fprintf( stderr, "%s: line %d: %s\n",
223 fname, lineno, c_reply->msg );
225 rc = LDAP_UNWILLING_TO_PERFORM;
229 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
230 ad->ad_type->sat_approx
231 && ad->ad_type->sat_approx->smr_indexer
232 && ad->ad_type->sat_approx->smr_filter ) )
235 snprintf(c_reply->msg, sizeof(c_reply->msg),
236 "approx index of attribute \"%s\" disallowed", attrs[i] );
237 fprintf( stderr, "%s: line %d: %s\n",
238 fname, lineno, c_reply->msg );
240 rc = LDAP_INAPPROPRIATE_MATCHING;
244 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
245 ad->ad_type->sat_equality
246 && ad->ad_type->sat_equality->smr_indexer
247 && ad->ad_type->sat_equality->smr_filter ) )
250 snprintf(c_reply->msg, sizeof(c_reply->msg),
251 "equality index of attribute \"%s\" disallowed", attrs[i] );
252 fprintf( stderr, "%s: line %d: %s\n",
253 fname, lineno, c_reply->msg );
255 rc = LDAP_INAPPROPRIATE_MATCHING;
259 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
260 ad->ad_type->sat_substr
261 && ad->ad_type->sat_substr->smr_indexer
262 && ad->ad_type->sat_substr->smr_filter ) )
265 snprintf(c_reply->msg, sizeof(c_reply->msg),
266 "substr index of attribute \"%s\" disallowed", attrs[i] );
267 fprintf( stderr, "%s: line %d: %s\n",
268 fname, lineno, c_reply->msg );
270 rc = LDAP_INAPPROPRIATE_MATCHING;
274 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
275 ad->ad_cname.bv_val, mask, 0 );
277 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
279 #ifdef LDAP_COMP_MATCH
284 if ( bdb->bi_flags & BDB_IS_OPEN ) {
286 a->ai_newmask = mask;
288 a->ai_indexmask = mask;
292 #ifdef LDAP_COMP_MATCH
294 a_cr = bdb_attr_mask( bdb, ad );
297 * AttrInfo is already in AVL
298 * just add the extracted component reference
301 rc = insert_component_reference( cr, &a_cr->ai_cr );
302 if ( rc != LDAP_SUCCESS) {
303 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
304 rc = LDAP_PARAM_ERROR;
309 rc = insert_component_reference( cr, &a->ai_cr );
310 if ( rc != LDAP_SUCCESS) {
311 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
312 rc = LDAP_PARAM_ERROR;
318 rc = ainfo_insert( bdb, a );
320 if ( bdb->bi_flags & BDB_IS_OPEN ) {
321 AttrInfo *b = bdb_attr_mask( bdb, ad );
322 /* If there is already an index defined for this attribute
323 * it must be replaced. Otherwise we end up with multiple
324 * olcIndex values for the same attribute */
325 if ( b->ai_indexmask & BDB_INDEX_DELETING ) {
326 /* If we were editing this attr, reset it */
327 b->ai_indexmask &= ~BDB_INDEX_DELETING;
328 /* If this is leftover from a previous add, commit it */
330 b->ai_indexmask = b->ai_newmask;
331 b->ai_newmask = a->ai_newmask;
338 snprintf(c_reply->msg, sizeof(c_reply->msg),
339 "duplicate index definition for attr \"%s\"",
341 fprintf( stderr, "%s: line %d: %s\n",
342 fname, lineno, c_reply->msg );
345 rc = LDAP_PARAM_ERROR;
351 ldap_charray_free( attrs );
352 if ( indexes != NULL ) ldap_charray_free( indexes );
358 bdb_attr_index_unparser( void *v1, void *v2 )
365 slap_index2bvlen( ai->ai_indexmask, &bv );
367 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
368 ptr = ch_malloc( bv.bv_len+1 );
369 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
371 slap_index2bv( ai->ai_indexmask, &bv );
373 ber_bvarray_add( bva, &bv );
378 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
379 static AttrInfo aidef = { &addef };
382 bdb_attr_index_unparse( struct bdb_info *bdb, BerVarray *bva )
386 if ( bdb->bi_defaultmask ) {
387 aidef.ai_indexmask = bdb->bi_defaultmask;
388 bdb_attr_index_unparser( &aidef, bva );
390 for ( i=0; i<bdb->bi_nattrs; i++ )
391 bdb_attr_index_unparser( bdb->bi_attrs[i], bva );
395 bdb_attr_info_free( AttrInfo *ai )
397 #ifdef LDAP_COMP_MATCH
404 bdb_attr_index_destroy( struct bdb_info *bdb )
408 for ( i=0; i<bdb->bi_nattrs; i++ )
409 bdb_attr_info_free( bdb->bi_attrs[i] );
411 free( bdb->bi_attrs );
414 void bdb_attr_index_free( struct bdb_info *bdb, AttributeDescription *ad )
418 i = bdb_attr_slot( bdb, ad, NULL );
420 bdb_attr_info_free( bdb->bi_attrs[i] );
422 for (; i<bdb->bi_nattrs; i++)
423 bdb->bi_attrs[i] = bdb->bi_attrs[i+1];
427 void bdb_attr_flush( struct bdb_info *bdb )
431 for ( i=0; i<bdb->bi_nattrs; i++ ) {
432 if ( bdb->bi_attrs[i]->ai_indexmask & BDB_INDEX_DELETING ) {
434 bdb_attr_info_free( bdb->bi_attrs[i] );
436 for (j=i; j<bdb->bi_nattrs; j++)
437 bdb->bi_attrs[j] = bdb->bi_attrs[j+1];