1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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;
103 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
105 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
106 "txn_begin failed: %s (%d).",
107 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
108 Debug( LDAP_DEBUG_ANY,
109 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
113 dbis = ch_calloc( 1, mdb->mi_nattrs * sizeof(MDB_dbi) );
118 flags = MDB_DUPSORT|MDB_DUPFIXED|MDB_INTEGERDUP;
119 if ( !(slapMode & SLAP_TOOL_READONLY) )
122 for ( i=0; i<mdb->mi_nattrs; i++ ) {
123 if ( mdb->mi_attrs[i]->ai_dbi ) /* already open */
125 rc = mdb_dbi_open( txn, mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
126 flags, &mdb->mi_attrs[i]->ai_dbi );
128 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
129 "mdb_dbi_open(%s) failed: %s (%d).",
130 be->be_suffix[0].bv_val,
131 mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
132 mdb_strerror(rc), rc );
133 Debug( LDAP_DEBUG_ANY,
134 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
138 /* Remember newly opened DBI handles */
140 dbis[i] = mdb->mi_attrs[i]->ai_dbi;
143 /* Only commit if this is our txn */
146 rc = mdb_txn_commit( txn );
148 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
149 "txn_commit failed: %s (%d).",
150 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
151 Debug( LDAP_DEBUG_ANY,
152 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
156 mdb_txn_abort( txn );
158 /* Something failed, forget anything we just opened */
160 for ( i=0; i<mdb->mi_nattrs; i++ ) {
162 mdb->mi_attrs[i]->ai_dbi = 0;
163 mdb->mi_attrs[i]->ai_indexmask |= MDB_INDEX_DELETING;
166 mdb_attr_flush( mdb );
180 for ( i=0; i<mdb->mi_nattrs; i++ )
181 if ( mdb->mi_attrs[i]->ai_dbi ) {
182 mdb_dbi_close( mdb->mi_dbenv, mdb->mi_attrs[i]->ai_dbi );
183 mdb->mi_attrs[i]->ai_dbi = 0;
188 mdb_attr_index_config(
189 struct mdb_info *mdb,
194 struct config_reply_s *c_reply)
200 char **indexes = NULL;
202 attrs = ldap_str2charray( argv[0], "," );
204 if( attrs == NULL ) {
205 fprintf( stderr, "%s: line %d: "
206 "no attributes specified: %s\n",
207 fname, lineno, argv[0] );
208 return LDAP_PARAM_ERROR;
212 indexes = ldap_str2charray( argv[1], "," );
214 if( indexes == NULL ) {
215 fprintf( stderr, "%s: line %d: "
216 "no indexes specified: %s\n",
217 fname, lineno, argv[1] );
218 rc = LDAP_PARAM_ERROR;
223 if( indexes == NULL ) {
224 mask = mdb->mi_defaultmask;
229 for ( i = 0; indexes[i] != NULL; i++ ) {
231 rc = slap_str2index( indexes[i], &index );
233 if( rc != LDAP_SUCCESS ) {
236 snprintf(c_reply->msg, sizeof(c_reply->msg),
237 "index type \"%s\" undefined", indexes[i] );
239 fprintf( stderr, "%s: line %d: %s\n",
240 fname, lineno, c_reply->msg );
242 rc = LDAP_PARAM_ERROR;
253 snprintf(c_reply->msg, sizeof(c_reply->msg),
254 "no indexes selected" );
255 fprintf( stderr, "%s: line %d: %s\n",
256 fname, lineno, c_reply->msg );
258 rc = LDAP_PARAM_ERROR;
262 for ( i = 0; attrs[i] != NULL; i++ ) {
264 AttributeDescription *ad;
266 #ifdef LDAP_COMP_MATCH
267 ComponentReference* cr = NULL;
268 AttrInfo *a_cr = NULL;
271 if( strcasecmp( attrs[i], "default" ) == 0 ) {
272 mdb->mi_defaultmask |= mask;
276 #ifdef LDAP_COMP_MATCH
277 if ( is_component_reference( attrs[i] ) ) {
278 rc = extract_component_reference( attrs[i], &cr );
279 if ( rc != LDAP_SUCCESS ) {
282 snprintf(c_reply->msg, sizeof(c_reply->msg),
283 "index component reference\"%s\" undefined",
285 fprintf( stderr, "%s: line %d: %s\n",
286 fname, lineno, c_reply->msg );
290 cr->cr_indexmask = mask;
292 * After extracting a component reference
293 * only the name of a attribute will be remaining
300 rc = slap_str2ad( attrs[i], &ad, &text );
302 if( rc != LDAP_SUCCESS ) {
305 snprintf(c_reply->msg, sizeof(c_reply->msg),
306 "index attribute \"%s\" undefined",
309 fprintf( stderr, "%s: line %d: %s\n",
310 fname, lineno, c_reply->msg );
315 if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) {
317 snprintf(c_reply->msg, sizeof(c_reply->msg),
318 "index of attribute \"%s\" disallowed", attrs[i] );
319 fprintf( stderr, "%s: line %d: %s\n",
320 fname, lineno, c_reply->msg );
322 rc = LDAP_UNWILLING_TO_PERFORM;
326 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
327 ad->ad_type->sat_approx
328 && ad->ad_type->sat_approx->smr_indexer
329 && ad->ad_type->sat_approx->smr_filter ) )
332 snprintf(c_reply->msg, sizeof(c_reply->msg),
333 "approx index of attribute \"%s\" disallowed", attrs[i] );
334 fprintf( stderr, "%s: line %d: %s\n",
335 fname, lineno, c_reply->msg );
337 rc = LDAP_INAPPROPRIATE_MATCHING;
341 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
342 ad->ad_type->sat_equality
343 && ad->ad_type->sat_equality->smr_indexer
344 && ad->ad_type->sat_equality->smr_filter ) )
347 snprintf(c_reply->msg, sizeof(c_reply->msg),
348 "equality index of attribute \"%s\" disallowed", attrs[i] );
349 fprintf( stderr, "%s: line %d: %s\n",
350 fname, lineno, c_reply->msg );
352 rc = LDAP_INAPPROPRIATE_MATCHING;
356 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
357 ad->ad_type->sat_substr
358 && ad->ad_type->sat_substr->smr_indexer
359 && ad->ad_type->sat_substr->smr_filter ) )
362 snprintf(c_reply->msg, sizeof(c_reply->msg),
363 "substr index of attribute \"%s\" disallowed", attrs[i] );
364 fprintf( stderr, "%s: line %d: %s\n",
365 fname, lineno, c_reply->msg );
367 rc = LDAP_INAPPROPRIATE_MATCHING;
371 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
372 ad->ad_cname.bv_val, mask, 0 );
374 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
376 #ifdef LDAP_COMP_MATCH
384 if ( mdb->mi_flags & MDB_IS_OPEN ) {
386 a->ai_newmask = mask;
388 a->ai_indexmask = mask;
392 #ifdef LDAP_COMP_MATCH
394 a_cr = mdb_attr_mask( mdb, ad );
397 * AttrInfo is already in AVL
398 * just add the extracted component reference
401 rc = insert_component_reference( cr, &a_cr->ai_cr );
402 if ( rc != LDAP_SUCCESS) {
403 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
404 rc = LDAP_PARAM_ERROR;
409 rc = insert_component_reference( cr, &a->ai_cr );
410 if ( rc != LDAP_SUCCESS) {
411 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
412 rc = LDAP_PARAM_ERROR;
418 rc = ainfo_insert( mdb, a );
420 if ( mdb->mi_flags & MDB_IS_OPEN ) {
421 AttrInfo *b = mdb_attr_mask( mdb, ad );
422 /* If there is already an index defined for this attribute
423 * it must be replaced. Otherwise we end up with multiple
424 * olcIndex values for the same attribute */
425 if ( b->ai_indexmask & MDB_INDEX_DELETING ) {
426 /* If we were editing this attr, reset it */
427 b->ai_indexmask &= ~MDB_INDEX_DELETING;
428 /* If this is leftover from a previous add, commit it */
430 b->ai_indexmask = b->ai_newmask;
431 b->ai_newmask = a->ai_newmask;
438 snprintf(c_reply->msg, sizeof(c_reply->msg),
439 "duplicate index definition for attr \"%s\"",
441 fprintf( stderr, "%s: line %d: %s\n",
442 fname, lineno, c_reply->msg );
445 rc = LDAP_PARAM_ERROR;
451 ldap_charray_free( attrs );
452 if ( indexes != NULL ) ldap_charray_free( indexes );
458 mdb_attr_index_unparser( void *v1, void *v2 )
465 slap_index2bvlen( ai->ai_indexmask, &bv );
467 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
468 ptr = ch_malloc( bv.bv_len+1 );
469 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
471 slap_index2bv( ai->ai_indexmask, &bv );
473 ber_bvarray_add( bva, &bv );
478 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
479 static AttrInfo aidef = { &addef };
482 mdb_attr_index_unparse( struct mdb_info *mdb, BerVarray *bva )
486 if ( mdb->mi_defaultmask ) {
487 aidef.ai_indexmask = mdb->mi_defaultmask;
488 mdb_attr_index_unparser( &aidef, bva );
490 for ( i=0; i<mdb->mi_nattrs; i++ )
491 mdb_attr_index_unparser( mdb->mi_attrs[i], bva );
495 mdb_attr_info_free( AttrInfo *ai )
497 #ifdef LDAP_COMP_MATCH
504 mdb_attr_index_destroy( struct mdb_info *mdb )
508 for ( i=0; i<mdb->mi_nattrs; i++ )
509 mdb_attr_info_free( mdb->mi_attrs[i] );
511 free( mdb->mi_attrs );
514 void mdb_attr_index_free( struct mdb_info *mdb, AttributeDescription *ad )
518 i = mdb_attr_slot( mdb, ad, NULL );
520 mdb_attr_info_free( mdb->mi_attrs[i] );
522 for (; i<mdb->mi_nattrs; i++)
523 mdb->mi_attrs[i] = mdb->mi_attrs[i+1];
527 void mdb_attr_flush( struct mdb_info *mdb )
531 for ( i=0; i<mdb->mi_nattrs; i++ ) {
532 if ( mdb->mi_attrs[i]->ai_indexmask & MDB_INDEX_DELETING ) {
534 mdb_attr_info_free( mdb->mi_attrs[i] );
536 for (j=i; j<mdb->mi_nattrs; j++)
537 mdb->mi_attrs[j] = mdb->mi_attrs[j+1];
543 int mdb_ad_read( struct mdb_info *mdb, MDB_txn *txn )
550 AttributeDescription *ad;
552 rc = mdb_cursor_open( txn, mdb->mi_ad2id, &mc );
554 Debug( LDAP_DEBUG_ANY,
555 "mdb_ad_read: cursor_open failed %s(%d)\n",
556 mdb_strerror(rc), rc, 0);
560 /* our array is 1-based, an index of 0 means no data */
561 i = mdb->mi_numads+1;
562 key.mv_size = sizeof(int);
565 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
567 while ( rc == MDB_SUCCESS ) {
568 bdata.bv_len = data.mv_size;
569 bdata.bv_val = data.mv_data;
571 rc = slap_bv2ad( &bdata, &ad, &text );
573 rc = slap_bv2undef_ad( &bdata, &mdb->mi_ads[i], &text, 0 );
575 if ( ad->ad_index >= MDB_MAXADS ) {
576 Debug( LDAP_DEBUG_ANY,
577 "mdb_adb_read: too many AttributeDescriptions in use\n",
581 mdb->mi_adxs[ad->ad_index] = i;
585 rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT );
587 mdb->mi_numads = i-1;
590 if ( rc == MDB_NOTFOUND )
593 mdb_cursor_close( mc );
598 int mdb_ad_get( struct mdb_info *mdb, MDB_txn *txn, AttributeDescription *ad )
603 rc = mdb_ad_read( mdb, txn );
607 if ( mdb->mi_adxs[ad->ad_index] )
610 i = mdb->mi_numads+1;
611 key.mv_size = sizeof(int);
613 val.mv_size = ad->ad_cname.bv_len;
614 val.mv_data = ad->ad_cname.bv_val;
616 rc = mdb_put( txn, mdb->mi_ad2id, &key, &val, 0 );
617 if ( rc == MDB_SUCCESS ) {
618 mdb->mi_adxs[ad->ad_index] = i;
622 Debug( LDAP_DEBUG_ANY,
623 "mdb_ad_get: mdb_put failed %s(%d)\n",
624 mdb_strerror(rc), rc, 0);