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 mdb_attr_slot( struct mdb_info *mdb, AttributeDescription *ad, int *ins )
35 unsigned base = 0, cursor = 0;
36 unsigned n = mdb->mi_nattrs;
40 unsigned pivot = n >> 1;
41 cursor = base + pivot;
43 val = SLAP_PTRCMP( ad, mdb->mi_attrs[cursor]->ai_desc );
46 } else if ( val > 0 ) {
62 ainfo_insert( struct mdb_info *mdb, AttrInfo *a )
65 int i = mdb_attr_slot( mdb, a->ai_desc, &x );
71 mdb->mi_attrs = ch_realloc( mdb->mi_attrs, ( mdb->mi_nattrs+1 ) *
72 sizeof( AttrInfo * ));
73 if ( x < mdb->mi_nattrs )
74 AC_MEMCPY( &mdb->mi_attrs[x+1], &mdb->mi_attrs[x],
75 ( mdb->mi_nattrs - x ) * sizeof( AttrInfo *));
84 AttributeDescription *desc )
86 int i = mdb_attr_slot( mdb, desc, NULL );
87 return i < 0 ? NULL : mdb->mi_attrs[i];
90 /* Open all un-opened index DB handles */
93 BackendDB *be, MDB_txn *tx0, ConfigReply *cr )
95 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
102 rc = mdb_txn_begin( mdb->mi_dbenv, 0, &txn );
104 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
105 "txn_begin failed: %s (%d).",
106 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
107 Debug( LDAP_DEBUG_ANY,
108 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
114 flags = MDB_DUPSORT|MDB_DUPFIXED|MDB_INTEGERDUP;
115 if ( !(slapMode & SLAP_TOOL_READONLY) )
118 for ( i=0; i<mdb->mi_nattrs; i++ ) {
119 if ( mdb->mi_attrs[i]->ai_dbi ) /* already open */
121 rc = mdb_open( txn, mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
122 flags, &mdb->mi_attrs[i]->ai_dbi );
124 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
125 "mdb_open(%s) failed: %s (%d).",
126 be->be_suffix[0].bv_val,
127 mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
128 mdb_strerror(rc), rc );
129 Debug( LDAP_DEBUG_ANY,
130 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
136 /* Only commit if this is our txn */
139 rc = mdb_txn_commit( txn );
141 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
142 "txn_commit failed: %s (%d).",
143 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
144 Debug( LDAP_DEBUG_ANY,
145 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
149 mdb_txn_abort( txn );
158 struct mdb_info *mdb,
163 for ( i=0; i<mdb->mi_nattrs; i++ )
164 if ( mdb->mi_attrs[i]->ai_dbi )
165 mdb_close( txn, mdb->mi_attrs[i]->ai_dbi );
169 mdb_attr_index_config(
170 struct mdb_info *mdb,
175 struct config_reply_s *c_reply)
181 char **indexes = NULL;
183 attrs = ldap_str2charray( argv[0], "," );
185 if( attrs == NULL ) {
186 fprintf( stderr, "%s: line %d: "
187 "no attributes specified: %s\n",
188 fname, lineno, argv[0] );
189 return LDAP_PARAM_ERROR;
193 indexes = ldap_str2charray( argv[1], "," );
195 if( indexes == NULL ) {
196 fprintf( stderr, "%s: line %d: "
197 "no indexes specified: %s\n",
198 fname, lineno, argv[1] );
199 rc = LDAP_PARAM_ERROR;
204 if( indexes == NULL ) {
205 mask = mdb->mi_defaultmask;
210 for ( i = 0; indexes[i] != NULL; i++ ) {
212 rc = slap_str2index( indexes[i], &index );
214 if( rc != LDAP_SUCCESS ) {
217 snprintf(c_reply->msg, sizeof(c_reply->msg),
218 "index type \"%s\" undefined", indexes[i] );
220 fprintf( stderr, "%s: line %d: %s\n",
221 fname, lineno, c_reply->msg );
223 rc = LDAP_PARAM_ERROR;
234 snprintf(c_reply->msg, sizeof(c_reply->msg),
235 "no indexes selected" );
236 fprintf( stderr, "%s: line %d: %s\n",
237 fname, lineno, c_reply->msg );
239 rc = LDAP_PARAM_ERROR;
243 for ( i = 0; attrs[i] != NULL; i++ ) {
245 AttributeDescription *ad;
247 #ifdef LDAP_COMP_MATCH
248 ComponentReference* cr = NULL;
249 AttrInfo *a_cr = NULL;
252 if( strcasecmp( attrs[i], "default" ) == 0 ) {
253 mdb->mi_defaultmask |= mask;
257 #ifdef LDAP_COMP_MATCH
258 if ( is_component_reference( attrs[i] ) ) {
259 rc = extract_component_reference( attrs[i], &cr );
260 if ( rc != LDAP_SUCCESS ) {
263 snprintf(c_reply->msg, sizeof(c_reply->msg),
264 "index component reference\"%s\" undefined",
266 fprintf( stderr, "%s: line %d: %s\n",
267 fname, lineno, c_reply->msg );
271 cr->cr_indexmask = mask;
273 * After extracting a component reference
274 * only the name of a attribute will be remaining
281 rc = slap_str2ad( attrs[i], &ad, &text );
283 if( rc != LDAP_SUCCESS ) {
286 snprintf(c_reply->msg, sizeof(c_reply->msg),
287 "index attribute \"%s\" undefined",
290 fprintf( stderr, "%s: line %d: %s\n",
291 fname, lineno, c_reply->msg );
296 if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) {
298 snprintf(c_reply->msg, sizeof(c_reply->msg),
299 "index of attribute \"%s\" disallowed", attrs[i] );
300 fprintf( stderr, "%s: line %d: %s\n",
301 fname, lineno, c_reply->msg );
303 rc = LDAP_UNWILLING_TO_PERFORM;
307 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
308 ad->ad_type->sat_approx
309 && ad->ad_type->sat_approx->smr_indexer
310 && ad->ad_type->sat_approx->smr_filter ) )
313 snprintf(c_reply->msg, sizeof(c_reply->msg),
314 "approx index of attribute \"%s\" disallowed", attrs[i] );
315 fprintf( stderr, "%s: line %d: %s\n",
316 fname, lineno, c_reply->msg );
318 rc = LDAP_INAPPROPRIATE_MATCHING;
322 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
323 ad->ad_type->sat_equality
324 && ad->ad_type->sat_equality->smr_indexer
325 && ad->ad_type->sat_equality->smr_filter ) )
328 snprintf(c_reply->msg, sizeof(c_reply->msg),
329 "equality index of attribute \"%s\" disallowed", attrs[i] );
330 fprintf( stderr, "%s: line %d: %s\n",
331 fname, lineno, c_reply->msg );
333 rc = LDAP_INAPPROPRIATE_MATCHING;
337 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
338 ad->ad_type->sat_substr
339 && ad->ad_type->sat_substr->smr_indexer
340 && ad->ad_type->sat_substr->smr_filter ) )
343 snprintf(c_reply->msg, sizeof(c_reply->msg),
344 "substr index of attribute \"%s\" disallowed", attrs[i] );
345 fprintf( stderr, "%s: line %d: %s\n",
346 fname, lineno, c_reply->msg );
348 rc = LDAP_INAPPROPRIATE_MATCHING;
352 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
353 ad->ad_cname.bv_val, mask, 0 );
355 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
357 #ifdef LDAP_COMP_MATCH
362 if ( mdb->mi_flags & MDB_IS_OPEN ) {
364 a->ai_newmask = mask;
366 a->ai_indexmask = mask;
370 #ifdef LDAP_COMP_MATCH
372 a_cr = mdb_attr_mask( mdb, ad );
375 * AttrInfo is already in AVL
376 * just add the extracted component reference
379 rc = insert_component_reference( cr, &a_cr->ai_cr );
380 if ( rc != LDAP_SUCCESS) {
381 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
382 rc = LDAP_PARAM_ERROR;
387 rc = insert_component_reference( cr, &a->ai_cr );
388 if ( rc != LDAP_SUCCESS) {
389 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
390 rc = LDAP_PARAM_ERROR;
396 rc = ainfo_insert( mdb, a );
398 if ( mdb->mi_flags & MDB_IS_OPEN ) {
399 AttrInfo *b = mdb_attr_mask( mdb, ad );
400 /* If there is already an index defined for this attribute
401 * it must be replaced. Otherwise we end up with multiple
402 * olcIndex values for the same attribute */
403 if ( b->ai_indexmask & MDB_INDEX_DELETING ) {
404 /* If we were editing this attr, reset it */
405 b->ai_indexmask &= ~MDB_INDEX_DELETING;
406 /* If this is leftover from a previous add, commit it */
408 b->ai_indexmask = b->ai_newmask;
409 b->ai_newmask = a->ai_newmask;
416 snprintf(c_reply->msg, sizeof(c_reply->msg),
417 "duplicate index definition for attr \"%s\"",
419 fprintf( stderr, "%s: line %d: %s\n",
420 fname, lineno, c_reply->msg );
423 rc = LDAP_PARAM_ERROR;
429 ldap_charray_free( attrs );
430 if ( indexes != NULL ) ldap_charray_free( indexes );
436 mdb_attr_index_unparser( void *v1, void *v2 )
443 slap_index2bvlen( ai->ai_indexmask, &bv );
445 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
446 ptr = ch_malloc( bv.bv_len+1 );
447 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
449 slap_index2bv( ai->ai_indexmask, &bv );
451 ber_bvarray_add( bva, &bv );
456 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
457 static AttrInfo aidef = { &addef };
460 mdb_attr_index_unparse( struct mdb_info *mdb, BerVarray *bva )
464 if ( mdb->mi_defaultmask ) {
465 aidef.ai_indexmask = mdb->mi_defaultmask;
466 mdb_attr_index_unparser( &aidef, bva );
468 for ( i=0; i<mdb->mi_nattrs; i++ )
469 mdb_attr_index_unparser( mdb->mi_attrs[i], bva );
473 mdb_attr_info_free( AttrInfo *ai )
475 #ifdef LDAP_COMP_MATCH
482 mdb_attr_index_destroy( struct mdb_info *mdb )
486 for ( i=0; i<mdb->mi_nattrs; i++ )
487 mdb_attr_info_free( mdb->mi_attrs[i] );
489 free( mdb->mi_attrs );
492 void mdb_attr_index_free( struct mdb_info *mdb, AttributeDescription *ad )
496 i = mdb_attr_slot( mdb, ad, NULL );
498 mdb_attr_info_free( mdb->mi_attrs[i] );
500 for (; i<mdb->mi_nattrs; i++)
501 mdb->mi_attrs[i] = mdb->mi_attrs[i+1];
505 void mdb_attr_flush( struct mdb_info *mdb )
509 for ( i=0; i<mdb->mi_nattrs; i++ ) {
510 if ( mdb->mi_attrs[i]->ai_indexmask & MDB_INDEX_DELETING ) {
512 mdb_attr_info_free( mdb->mi_attrs[i] );
514 for (j=i; j<mdb->mi_nattrs; j++)
515 mdb->mi_attrs[j] = mdb->mi_attrs[j+1];