1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2016 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 );
313 #ifdef LDAP_COMP_MATCH
319 if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) {
321 snprintf(c_reply->msg, sizeof(c_reply->msg),
322 "index of attribute \"%s\" disallowed", attrs[i] );
323 fprintf( stderr, "%s: line %d: %s\n",
324 fname, lineno, c_reply->msg );
326 rc = LDAP_UNWILLING_TO_PERFORM;
330 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
331 ad->ad_type->sat_approx
332 && ad->ad_type->sat_approx->smr_indexer
333 && ad->ad_type->sat_approx->smr_filter ) )
336 snprintf(c_reply->msg, sizeof(c_reply->msg),
337 "approx index of attribute \"%s\" disallowed", attrs[i] );
338 fprintf( stderr, "%s: line %d: %s\n",
339 fname, lineno, c_reply->msg );
341 rc = LDAP_INAPPROPRIATE_MATCHING;
345 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
346 ad->ad_type->sat_equality
347 && ad->ad_type->sat_equality->smr_indexer
348 && ad->ad_type->sat_equality->smr_filter ) )
351 snprintf(c_reply->msg, sizeof(c_reply->msg),
352 "equality index of attribute \"%s\" disallowed", attrs[i] );
353 fprintf( stderr, "%s: line %d: %s\n",
354 fname, lineno, c_reply->msg );
356 rc = LDAP_INAPPROPRIATE_MATCHING;
360 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
361 ad->ad_type->sat_substr
362 && ad->ad_type->sat_substr->smr_indexer
363 && ad->ad_type->sat_substr->smr_filter ) )
366 snprintf(c_reply->msg, sizeof(c_reply->msg),
367 "substr index of attribute \"%s\" disallowed", attrs[i] );
368 fprintf( stderr, "%s: line %d: %s\n",
369 fname, lineno, c_reply->msg );
371 rc = LDAP_INAPPROPRIATE_MATCHING;
375 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
376 ad->ad_cname.bv_val, mask, 0 );
378 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
380 #ifdef LDAP_COMP_MATCH
388 if ( mdb->mi_flags & MDB_IS_OPEN ) {
390 a->ai_newmask = mask;
392 a->ai_indexmask = mask;
396 #ifdef LDAP_COMP_MATCH
398 a_cr = mdb_attr_mask( mdb, ad );
401 * AttrInfo is already in AVL
402 * just add the extracted component reference
406 rc = insert_component_reference( cr, &a_cr->ai_cr );
407 if ( rc != LDAP_SUCCESS) {
408 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
409 rc = LDAP_PARAM_ERROR;
414 rc = insert_component_reference( cr, &a->ai_cr );
415 if ( rc != LDAP_SUCCESS) {
416 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
417 rc = LDAP_PARAM_ERROR;
424 rc = ainfo_insert( mdb, a );
426 if ( mdb->mi_flags & MDB_IS_OPEN ) {
427 AttrInfo *b = mdb_attr_mask( mdb, ad );
428 /* If there is already an index defined for this attribute
429 * it must be replaced. Otherwise we end up with multiple
430 * olcIndex values for the same attribute */
431 if ( b->ai_indexmask & MDB_INDEX_DELETING ) {
432 /* If we were editing this attr, reset it */
433 b->ai_indexmask &= ~MDB_INDEX_DELETING;
434 /* If this is leftover from a previous add, commit it */
436 b->ai_indexmask = b->ai_newmask;
437 b->ai_newmask = a->ai_newmask;
444 snprintf(c_reply->msg, sizeof(c_reply->msg),
445 "duplicate index definition for attr \"%s\"",
447 fprintf( stderr, "%s: line %d: %s\n",
448 fname, lineno, c_reply->msg );
451 rc = LDAP_PARAM_ERROR;
457 ldap_charray_free( attrs );
458 if ( indexes != NULL ) ldap_charray_free( indexes );
464 mdb_attr_index_unparser( void *v1, void *v2 )
471 slap_index2bvlen( ai->ai_indexmask, &bv );
473 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
474 ptr = ch_malloc( bv.bv_len+1 );
475 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
477 slap_index2bv( ai->ai_indexmask, &bv );
479 ber_bvarray_add( bva, &bv );
484 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
485 static AttrInfo aidef = { &addef };
488 mdb_attr_index_unparse( struct mdb_info *mdb, BerVarray *bva )
492 if ( mdb->mi_defaultmask ) {
493 aidef.ai_indexmask = mdb->mi_defaultmask;
494 mdb_attr_index_unparser( &aidef, bva );
496 for ( i=0; i<mdb->mi_nattrs; i++ )
497 mdb_attr_index_unparser( mdb->mi_attrs[i], bva );
501 mdb_attr_info_free( AttrInfo *ai )
503 #ifdef LDAP_COMP_MATCH
510 mdb_attr_index_destroy( struct mdb_info *mdb )
514 for ( i=0; i<mdb->mi_nattrs; i++ )
515 mdb_attr_info_free( mdb->mi_attrs[i] );
517 free( mdb->mi_attrs );
520 void mdb_attr_index_free( struct mdb_info *mdb, AttributeDescription *ad )
524 i = mdb_attr_slot( mdb, ad, NULL );
526 mdb_attr_info_free( mdb->mi_attrs[i] );
528 for (; i<mdb->mi_nattrs; i++)
529 mdb->mi_attrs[i] = mdb->mi_attrs[i+1];
533 void mdb_attr_flush( struct mdb_info *mdb )
537 for ( i=0; i<mdb->mi_nattrs; i++ ) {
538 if ( mdb->mi_attrs[i]->ai_indexmask & MDB_INDEX_DELETING ) {
540 mdb_attr_info_free( mdb->mi_attrs[i] );
542 for (j=i; j<mdb->mi_nattrs; j++)
543 mdb->mi_attrs[j] = mdb->mi_attrs[j+1];
549 int mdb_ad_read( struct mdb_info *mdb, MDB_txn *txn )
556 AttributeDescription *ad;
558 rc = mdb_cursor_open( txn, mdb->mi_ad2id, &mc );
560 Debug( LDAP_DEBUG_ANY,
561 "mdb_ad_read: cursor_open failed %s(%d)\n",
562 mdb_strerror(rc), rc, 0);
566 /* our array is 1-based, an index of 0 means no data */
567 i = mdb->mi_numads+1;
568 key.mv_size = sizeof(int);
571 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
573 while ( rc == MDB_SUCCESS ) {
574 bdata.bv_len = data.mv_size;
575 bdata.bv_val = data.mv_data;
577 rc = slap_bv2ad( &bdata, &ad, &text );
579 rc = slap_bv2undef_ad( &bdata, &mdb->mi_ads[i], &text, 0 );
581 if ( ad->ad_index >= MDB_MAXADS ) {
582 Debug( LDAP_DEBUG_ANY,
583 "mdb_adb_read: too many AttributeDescriptions in use\n",
587 mdb->mi_adxs[ad->ad_index] = i;
591 rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT );
593 mdb->mi_numads = i-1;
596 if ( rc == MDB_NOTFOUND )
599 mdb_cursor_close( mc );
604 int mdb_ad_get( struct mdb_info *mdb, MDB_txn *txn, AttributeDescription *ad )
609 rc = mdb_ad_read( mdb, txn );
613 if ( mdb->mi_adxs[ad->ad_index] )
616 i = mdb->mi_numads+1;
617 key.mv_size = sizeof(int);
619 val.mv_size = ad->ad_cname.bv_len;
620 val.mv_data = ad->ad_cname.bv_val;
622 rc = mdb_put( txn, mdb->mi_ad2id, &key, &val, 0 );
623 if ( rc == MDB_SUCCESS ) {
624 mdb->mi_adxs[ad->ad_index] = i;
628 Debug( LDAP_DEBUG_ANY,
629 "mdb_ad_get: mdb_put failed %s(%d)\n",
630 mdb_strerror(rc), rc, 0);