1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2015 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 );
216 #ifdef LDAP_COMP_MATCH
222 if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) {
224 snprintf(c_reply->msg, sizeof(c_reply->msg),
225 "index of attribute \"%s\" disallowed", attrs[i] );
226 fprintf( stderr, "%s: line %d: %s\n",
227 fname, lineno, c_reply->msg );
229 rc = LDAP_UNWILLING_TO_PERFORM;
233 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
234 ad->ad_type->sat_approx
235 && ad->ad_type->sat_approx->smr_indexer
236 && ad->ad_type->sat_approx->smr_filter ) )
239 snprintf(c_reply->msg, sizeof(c_reply->msg),
240 "approx index of attribute \"%s\" disallowed", attrs[i] );
241 fprintf( stderr, "%s: line %d: %s\n",
242 fname, lineno, c_reply->msg );
244 rc = LDAP_INAPPROPRIATE_MATCHING;
248 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
249 ad->ad_type->sat_equality
250 && ad->ad_type->sat_equality->smr_indexer
251 && ad->ad_type->sat_equality->smr_filter ) )
254 snprintf(c_reply->msg, sizeof(c_reply->msg),
255 "equality index of attribute \"%s\" disallowed", attrs[i] );
256 fprintf( stderr, "%s: line %d: %s\n",
257 fname, lineno, c_reply->msg );
259 rc = LDAP_INAPPROPRIATE_MATCHING;
263 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
264 ad->ad_type->sat_substr
265 && ad->ad_type->sat_substr->smr_indexer
266 && ad->ad_type->sat_substr->smr_filter ) )
269 snprintf(c_reply->msg, sizeof(c_reply->msg),
270 "substr index of attribute \"%s\" disallowed", attrs[i] );
271 fprintf( stderr, "%s: line %d: %s\n",
272 fname, lineno, c_reply->msg );
274 rc = LDAP_INAPPROPRIATE_MATCHING;
278 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
279 ad->ad_cname.bv_val, mask, 0 );
281 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
283 #ifdef LDAP_COMP_MATCH
288 if ( bdb->bi_flags & BDB_IS_OPEN ) {
290 a->ai_newmask = mask;
292 a->ai_indexmask = mask;
296 #ifdef LDAP_COMP_MATCH
298 a_cr = bdb_attr_mask( bdb, ad );
301 * AttrInfo is already in AVL
302 * just add the extracted component reference
306 rc = insert_component_reference( cr, &a_cr->ai_cr );
307 if ( rc != LDAP_SUCCESS) {
308 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
309 rc = LDAP_PARAM_ERROR;
314 rc = insert_component_reference( cr, &a->ai_cr );
315 if ( rc != LDAP_SUCCESS) {
316 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
317 rc = LDAP_PARAM_ERROR;
324 rc = ainfo_insert( bdb, a );
326 if ( bdb->bi_flags & BDB_IS_OPEN ) {
327 AttrInfo *b = bdb_attr_mask( bdb, ad );
328 /* If there is already an index defined for this attribute
329 * it must be replaced. Otherwise we end up with multiple
330 * olcIndex values for the same attribute */
331 if ( b->ai_indexmask & BDB_INDEX_DELETING ) {
332 /* If we were editing this attr, reset it */
333 b->ai_indexmask &= ~BDB_INDEX_DELETING;
334 /* If this is leftover from a previous add, commit it */
336 b->ai_indexmask = b->ai_newmask;
337 b->ai_newmask = a->ai_newmask;
344 snprintf(c_reply->msg, sizeof(c_reply->msg),
345 "duplicate index definition for attr \"%s\"",
347 fprintf( stderr, "%s: line %d: %s\n",
348 fname, lineno, c_reply->msg );
351 rc = LDAP_PARAM_ERROR;
357 ldap_charray_free( attrs );
358 if ( indexes != NULL ) ldap_charray_free( indexes );
364 bdb_attr_index_unparser( void *v1, void *v2 )
371 slap_index2bvlen( ai->ai_indexmask, &bv );
373 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
374 ptr = ch_malloc( bv.bv_len+1 );
375 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
377 slap_index2bv( ai->ai_indexmask, &bv );
379 ber_bvarray_add( bva, &bv );
384 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
385 static AttrInfo aidef = { &addef };
388 bdb_attr_index_unparse( struct bdb_info *bdb, BerVarray *bva )
392 if ( bdb->bi_defaultmask ) {
393 aidef.ai_indexmask = bdb->bi_defaultmask;
394 bdb_attr_index_unparser( &aidef, bva );
396 for ( i=0; i<bdb->bi_nattrs; i++ )
397 bdb_attr_index_unparser( bdb->bi_attrs[i], bva );
401 bdb_attr_info_free( AttrInfo *ai )
403 #ifdef LDAP_COMP_MATCH
410 bdb_attr_index_destroy( struct bdb_info *bdb )
414 for ( i=0; i<bdb->bi_nattrs; i++ )
415 bdb_attr_info_free( bdb->bi_attrs[i] );
417 free( bdb->bi_attrs );
420 void bdb_attr_index_free( struct bdb_info *bdb, AttributeDescription *ad )
424 i = bdb_attr_slot( bdb, ad, NULL );
426 bdb_attr_info_free( bdb->bi_attrs[i] );
428 for (; i<bdb->bi_nattrs; i++)
429 bdb->bi_attrs[i] = bdb->bi_attrs[i+1];
433 void bdb_attr_flush( struct bdb_info *bdb )
437 for ( i=0; i<bdb->bi_nattrs; i++ ) {
438 if ( bdb->bi_attrs[i]->ai_indexmask & BDB_INDEX_DELETING ) {
440 bdb_attr_info_free( bdb->bi_attrs[i] );
442 for (j=i; j<bdb->bi_nattrs; j++)
443 bdb->bi_attrs[j] = bdb->bi_attrs[j+1];