| 1 |
/* attr.c - backend routines for dealing with attributes */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2000-2024 The OpenLDAP Foundation. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted only as authorized by the OpenLDAP |
| 10 |
* Public License. |
| 11 |
* |
| 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>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "portable.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#include <ac/socket.h> |
| 22 |
#include <ac/string.h> |
| 23 |
|
| 24 |
#include "slap.h" |
| 25 |
#include "back-mdb.h" |
| 26 |
#include "slap-config.h" |
| 27 |
#include "lutil.h" |
| 28 |
|
| 29 |
/* Find the ad, return -1 if not found, |
| 30 |
* set point for insertion if ins is non-NULL |
| 31 |
*/ |
| 32 |
int |
| 33 |
mdb_attr_slot( struct mdb_info *mdb, AttributeDescription *ad, int *ins ) |
| 34 |
{ |
| 35 |
unsigned base = 0, cursor = 0; |
| 36 |
unsigned n = mdb->mi_nattrs; |
| 37 |
int val = 0; |
| 38 |
|
| 39 |
while ( 0 < n ) { |
| 40 |
unsigned pivot = n >> 1; |
| 41 |
cursor = base + pivot; |
| 42 |
|
| 43 |
val = SLAP_PTRCMP( ad, mdb->mi_attrs[cursor]->ai_desc ); |
| 44 |
if ( val < 0 ) { |
| 45 |
n = pivot; |
| 46 |
} else if ( val > 0 ) { |
| 47 |
base = cursor + 1; |
| 48 |
n -= pivot + 1; |
| 49 |
} else { |
| 50 |
return cursor; |
| 51 |
} |
| 52 |
} |
| 53 |
if ( ins ) { |
| 54 |
if ( val > 0 ) |
| 55 |
++cursor; |
| 56 |
*ins = cursor; |
| 57 |
} |
| 58 |
return -1; |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
ainfo_insert( struct mdb_info *mdb, AttrInfo *a ) |
| 63 |
{ |
| 64 |
int x; |
| 65 |
int i = mdb_attr_slot( mdb, a->ai_desc, &x ); |
| 66 |
|
| 67 |
/* Is it a dup? */ |
| 68 |
if ( i >= 0 ) |
| 69 |
return -1; |
| 70 |
|
| 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 *)); |
| 76 |
mdb->mi_attrs[x] = a; |
| 77 |
mdb->mi_nattrs++; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
AttrInfo * |
| 82 |
mdb_attr_mask( |
| 83 |
struct mdb_info *mdb, |
| 84 |
AttributeDescription *desc ) |
| 85 |
{ |
| 86 |
int i = mdb_attr_slot( mdb, desc, NULL ); |
| 87 |
return i < 0 ? NULL : mdb->mi_attrs[i]; |
| 88 |
} |
| 89 |
|
| 90 |
/* Open all un-opened index DB handles */ |
| 91 |
int |
| 92 |
mdb_attr_dbs_open( |
| 93 |
BackendDB *be, MDB_txn *tx0, ConfigReply *cr ) |
| 94 |
{ |
| 95 |
struct mdb_info *mdb = (struct mdb_info *) be->be_private; |
| 96 |
MDB_txn *txn; |
| 97 |
MDB_dbi *dbis = NULL; |
| 98 |
int i, flags; |
| 99 |
int rc; |
| 100 |
|
| 101 |
if ( !mdb->mi_nattrs ) |
| 102 |
return 0; |
| 103 |
|
| 104 |
txn = tx0; |
| 105 |
if ( txn == NULL ) { |
| 106 |
rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn ); |
| 107 |
if ( rc ) { |
| 108 |
snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": " |
| 109 |
"txn_begin failed: %s (%d).", |
| 110 |
be->be_suffix[0].bv_val, mdb_strerror(rc), rc ); |
| 111 |
Debug( LDAP_DEBUG_ANY, |
| 112 |
LDAP_XSTRING(mdb_attr_dbs) ": %s\n", |
| 113 |
cr->msg ); |
| 114 |
return rc; |
| 115 |
} |
| 116 |
dbis = ch_calloc( 1, mdb->mi_nattrs * sizeof(MDB_dbi) ); |
| 117 |
} else { |
| 118 |
rc = 0; |
| 119 |
} |
| 120 |
|
| 121 |
flags = MDB_DUPSORT|MDB_DUPFIXED|MDB_INTEGERDUP; |
| 122 |
if ( !(slapMode & SLAP_TOOL_READONLY) ) |
| 123 |
flags |= MDB_CREATE; |
| 124 |
|
| 125 |
for ( i=0; i<mdb->mi_nattrs; i++ ) { |
| 126 |
if ( mdb->mi_attrs[i]->ai_dbi ) /* already open */ |
| 127 |
continue; |
| 128 |
if ( !( mdb->mi_attrs[i]->ai_indexmask || mdb->mi_attrs[i]->ai_newmask )) /* not an index record */ |
| 129 |
continue; |
| 130 |
rc = mdb_dbi_open( txn, mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val, |
| 131 |
flags, &mdb->mi_attrs[i]->ai_dbi ); |
| 132 |
if ( rc ) { |
| 133 |
snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": " |
| 134 |
"mdb_dbi_open(%s) failed: %s (%d).", |
| 135 |
be->be_suffix[0].bv_val, |
| 136 |
mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val, |
| 137 |
mdb_strerror(rc), rc ); |
| 138 |
Debug( LDAP_DEBUG_ANY, |
| 139 |
LDAP_XSTRING(mdb_attr_dbs) ": %s\n", |
| 140 |
cr->msg ); |
| 141 |
break; |
| 142 |
} |
| 143 |
/* Remember newly opened DBI handles */ |
| 144 |
if ( dbis ) |
| 145 |
dbis[i] = mdb->mi_attrs[i]->ai_dbi; |
| 146 |
} |
| 147 |
|
| 148 |
/* Only commit if this is our txn */ |
| 149 |
if ( tx0 == NULL ) { |
| 150 |
if ( !rc ) { |
| 151 |
rc = mdb_txn_commit( txn ); |
| 152 |
if ( rc ) { |
| 153 |
snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": " |
| 154 |
"txn_commit failed: %s (%d).", |
| 155 |
be->be_suffix[0].bv_val, mdb_strerror(rc), rc ); |
| 156 |
Debug( LDAP_DEBUG_ANY, |
| 157 |
LDAP_XSTRING(mdb_attr_dbs) ": %s\n", |
| 158 |
cr->msg ); |
| 159 |
} |
| 160 |
} else { |
| 161 |
mdb_txn_abort( txn ); |
| 162 |
} |
| 163 |
/* Something failed, forget anything we just opened */ |
| 164 |
if ( rc ) { |
| 165 |
for ( i=0; i<mdb->mi_nattrs; i++ ) { |
| 166 |
if ( dbis[i] ) { |
| 167 |
mdb->mi_attrs[i]->ai_dbi = 0; |
| 168 |
mdb->mi_attrs[i]->ai_indexmask |= MDB_INDEX_DELETING; |
| 169 |
} |
| 170 |
} |
| 171 |
mdb_attr_flush( mdb ); |
| 172 |
} |
| 173 |
ch_free( dbis ); |
| 174 |
} |
| 175 |
|
| 176 |
return rc; |
| 177 |
} |
| 178 |
|
| 179 |
void |
| 180 |
mdb_attr_dbs_close( |
| 181 |
struct mdb_info *mdb |
| 182 |
) |
| 183 |
{ |
| 184 |
int i; |
| 185 |
for ( i=0; i<mdb->mi_nattrs; i++ ) |
| 186 |
if ( mdb->mi_attrs[i]->ai_dbi ) { |
| 187 |
mdb_dbi_close( mdb->mi_dbenv, mdb->mi_attrs[i]->ai_dbi ); |
| 188 |
mdb->mi_attrs[i]->ai_dbi = 0; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
int |
| 193 |
mdb_attr_index_config( |
| 194 |
struct mdb_info *mdb, |
| 195 |
const char *fname, |
| 196 |
int lineno, |
| 197 |
int argc, |
| 198 |
char **argv, |
| 199 |
struct config_reply_s *c_reply) |
| 200 |
{ |
| 201 |
int rc = 0; |
| 202 |
int i; |
| 203 |
slap_mask_t mask; |
| 204 |
char **attrs; |
| 205 |
char **indexes = NULL; |
| 206 |
|
| 207 |
attrs = ldap_str2charray( argv[0], "," ); |
| 208 |
|
| 209 |
if( attrs == NULL ) { |
| 210 |
fprintf( stderr, "%s: line %d: " |
| 211 |
"no attributes specified: %s\n", |
| 212 |
fname, lineno, argv[0] ); |
| 213 |
return LDAP_PARAM_ERROR; |
| 214 |
} |
| 215 |
|
| 216 |
if ( argc > 1 ) { |
| 217 |
indexes = ldap_str2charray( argv[1], "," ); |
| 218 |
|
| 219 |
if( indexes == NULL ) { |
| 220 |
fprintf( stderr, "%s: line %d: " |
| 221 |
"no indexes specified: %s\n", |
| 222 |
fname, lineno, argv[1] ); |
| 223 |
rc = LDAP_PARAM_ERROR; |
| 224 |
goto done; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if( indexes == NULL ) { |
| 229 |
mask = mdb->mi_defaultmask; |
| 230 |
|
| 231 |
} else { |
| 232 |
mask = 0; |
| 233 |
|
| 234 |
for ( i = 0; indexes[i] != NULL; i++ ) { |
| 235 |
slap_mask_t index; |
| 236 |
rc = slap_str2index( indexes[i], &index ); |
| 237 |
|
| 238 |
if( rc != LDAP_SUCCESS ) { |
| 239 |
if ( c_reply ) |
| 240 |
{ |
| 241 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 242 |
"index type \"%s\" undefined", indexes[i] ); |
| 243 |
|
| 244 |
fprintf( stderr, "%s: line %d: %s\n", |
| 245 |
fname, lineno, c_reply->msg ); |
| 246 |
} |
| 247 |
rc = LDAP_PARAM_ERROR; |
| 248 |
goto done; |
| 249 |
} |
| 250 |
|
| 251 |
mask |= index; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if( !mask ) { |
| 256 |
if ( c_reply ) |
| 257 |
{ |
| 258 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 259 |
"no indexes selected" ); |
| 260 |
fprintf( stderr, "%s: line %d: %s\n", |
| 261 |
fname, lineno, c_reply->msg ); |
| 262 |
} |
| 263 |
rc = LDAP_PARAM_ERROR; |
| 264 |
goto done; |
| 265 |
} |
| 266 |
|
| 267 |
for ( i = 0; attrs[i] != NULL; i++ ) { |
| 268 |
AttrInfo *a; |
| 269 |
AttributeDescription *ad; |
| 270 |
const char *text; |
| 271 |
#ifdef LDAP_COMP_MATCH |
| 272 |
ComponentReference* cr = NULL; |
| 273 |
AttrInfo *a_cr = NULL; |
| 274 |
#endif |
| 275 |
|
| 276 |
if( strcasecmp( attrs[i], "default" ) == 0 ) { |
| 277 |
mdb->mi_defaultmask |= mask; |
| 278 |
continue; |
| 279 |
} |
| 280 |
|
| 281 |
#ifdef LDAP_COMP_MATCH |
| 282 |
if ( is_component_reference( attrs[i] ) ) { |
| 283 |
rc = extract_component_reference( attrs[i], &cr ); |
| 284 |
if ( rc != LDAP_SUCCESS ) { |
| 285 |
if ( c_reply ) |
| 286 |
{ |
| 287 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 288 |
"index component reference\"%s\" undefined", |
| 289 |
attrs[i] ); |
| 290 |
fprintf( stderr, "%s: line %d: %s\n", |
| 291 |
fname, lineno, c_reply->msg ); |
| 292 |
} |
| 293 |
goto done; |
| 294 |
} |
| 295 |
cr->cr_indexmask = mask; |
| 296 |
/* |
| 297 |
* After extracting a component reference |
| 298 |
* only the name of a attribute will be remaining |
| 299 |
*/ |
| 300 |
} else { |
| 301 |
cr = NULL; |
| 302 |
} |
| 303 |
#endif |
| 304 |
ad = NULL; |
| 305 |
rc = slap_str2ad( attrs[i], &ad, &text ); |
| 306 |
|
| 307 |
if( rc != LDAP_SUCCESS ) { |
| 308 |
if ( c_reply ) |
| 309 |
{ |
| 310 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 311 |
"index attribute \"%s\" undefined", |
| 312 |
attrs[i] ); |
| 313 |
|
| 314 |
fprintf( stderr, "%s: line %d: %s\n", |
| 315 |
fname, lineno, c_reply->msg ); |
| 316 |
} |
| 317 |
fail: |
| 318 |
#ifdef LDAP_COMP_MATCH |
| 319 |
ch_free( cr ); |
| 320 |
#endif |
| 321 |
goto done; |
| 322 |
} |
| 323 |
|
| 324 |
if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) { |
| 325 |
if (c_reply) { |
| 326 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 327 |
"index of attribute \"%s\" disallowed", attrs[i] ); |
| 328 |
fprintf( stderr, "%s: line %d: %s\n", |
| 329 |
fname, lineno, c_reply->msg ); |
| 330 |
} |
| 331 |
rc = LDAP_UNWILLING_TO_PERFORM; |
| 332 |
goto fail; |
| 333 |
} |
| 334 |
|
| 335 |
if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !( |
| 336 |
ad->ad_type->sat_approx |
| 337 |
&& ad->ad_type->sat_approx->smr_indexer |
| 338 |
&& ad->ad_type->sat_approx->smr_filter ) ) |
| 339 |
{ |
| 340 |
if (c_reply) { |
| 341 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 342 |
"approx index of attribute \"%s\" disallowed", attrs[i] ); |
| 343 |
fprintf( stderr, "%s: line %d: %s\n", |
| 344 |
fname, lineno, c_reply->msg ); |
| 345 |
} |
| 346 |
rc = LDAP_INAPPROPRIATE_MATCHING; |
| 347 |
goto fail; |
| 348 |
} |
| 349 |
|
| 350 |
if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !( |
| 351 |
ad->ad_type->sat_equality |
| 352 |
&& ad->ad_type->sat_equality->smr_indexer |
| 353 |
&& ad->ad_type->sat_equality->smr_filter ) ) |
| 354 |
{ |
| 355 |
if (c_reply) { |
| 356 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 357 |
"equality index of attribute \"%s\" disallowed", attrs[i] ); |
| 358 |
fprintf( stderr, "%s: line %d: %s\n", |
| 359 |
fname, lineno, c_reply->msg ); |
| 360 |
} |
| 361 |
rc = LDAP_INAPPROPRIATE_MATCHING; |
| 362 |
goto fail; |
| 363 |
} |
| 364 |
|
| 365 |
if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !( |
| 366 |
ad->ad_type->sat_substr |
| 367 |
&& ad->ad_type->sat_substr->smr_indexer |
| 368 |
&& ad->ad_type->sat_substr->smr_filter ) ) |
| 369 |
{ |
| 370 |
if (c_reply) { |
| 371 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 372 |
"substr index of attribute \"%s\" disallowed", attrs[i] ); |
| 373 |
fprintf( stderr, "%s: line %d: %s\n", |
| 374 |
fname, lineno, c_reply->msg ); |
| 375 |
} |
| 376 |
rc = LDAP_INAPPROPRIATE_MATCHING; |
| 377 |
goto fail; |
| 378 |
} |
| 379 |
|
| 380 |
Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n", |
| 381 |
ad->ad_cname.bv_val, mask ); |
| 382 |
|
| 383 |
a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) ); |
| 384 |
|
| 385 |
#ifdef LDAP_COMP_MATCH |
| 386 |
a->ai_cr = NULL; |
| 387 |
#endif |
| 388 |
a->ai_cursor = NULL; |
| 389 |
a->ai_root = NULL; |
| 390 |
a->ai_desc = ad; |
| 391 |
a->ai_dbi = 0; |
| 392 |
a->ai_multi_hi = UINT_MAX; |
| 393 |
a->ai_multi_lo = UINT_MAX; |
| 394 |
|
| 395 |
if ( mdb->mi_flags & MDB_IS_OPEN ) { |
| 396 |
a->ai_indexmask = 0; |
| 397 |
a->ai_newmask = mask; |
| 398 |
} else { |
| 399 |
a->ai_indexmask = mask; |
| 400 |
a->ai_newmask = 0; |
| 401 |
} |
| 402 |
|
| 403 |
#ifdef LDAP_COMP_MATCH |
| 404 |
if ( cr ) { |
| 405 |
a_cr = mdb_attr_mask( mdb, ad ); |
| 406 |
if ( a_cr ) { |
| 407 |
/* |
| 408 |
* AttrInfo is already in AVL |
| 409 |
* just add the extracted component reference |
| 410 |
* in the AttrInfo |
| 411 |
*/ |
| 412 |
ch_free( a ); |
| 413 |
rc = insert_component_reference( cr, &a_cr->ai_cr ); |
| 414 |
if ( rc != LDAP_SUCCESS) { |
| 415 |
fprintf( stderr, " error during inserting component reference in %s ", attrs[i]); |
| 416 |
rc = LDAP_PARAM_ERROR; |
| 417 |
goto fail; |
| 418 |
} |
| 419 |
continue; |
| 420 |
} else { |
| 421 |
rc = insert_component_reference( cr, &a->ai_cr ); |
| 422 |
if ( rc != LDAP_SUCCESS) { |
| 423 |
fprintf( stderr, " error during inserting component reference in %s ", attrs[i]); |
| 424 |
rc = LDAP_PARAM_ERROR; |
| 425 |
ch_free( a ); |
| 426 |
goto fail; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
#endif |
| 431 |
rc = ainfo_insert( mdb, a ); |
| 432 |
if( rc ) { |
| 433 |
AttrInfo *b = mdb_attr_mask( mdb, ad ); |
| 434 |
/* If this is just a multival record, reuse it for index info */ |
| 435 |
if ( !( b->ai_indexmask || b->ai_newmask ) && b->ai_multi_lo < UINT_MAX ) { |
| 436 |
b->ai_indexmask = a->ai_indexmask; |
| 437 |
b->ai_newmask = a->ai_newmask; |
| 438 |
ch_free( a ); |
| 439 |
rc = 0; |
| 440 |
continue; |
| 441 |
} |
| 442 |
if ( mdb->mi_flags & MDB_IS_OPEN ) { |
| 443 |
/* If there is already an index defined for this attribute |
| 444 |
* it must be replaced. Otherwise we end up with multiple |
| 445 |
* olcIndex values for the same attribute */ |
| 446 |
if ( b->ai_indexmask & MDB_INDEX_DELETING ) { |
| 447 |
/* If we were editing this attr, reset it */ |
| 448 |
b->ai_indexmask &= ~MDB_INDEX_DELETING; |
| 449 |
/* If this is leftover from a previous add, commit it */ |
| 450 |
if ( b->ai_newmask ) |
| 451 |
b->ai_indexmask = b->ai_newmask; |
| 452 |
/* If the mask changed, remember it */ |
| 453 |
if ( b->ai_indexmask != a->ai_newmask ) |
| 454 |
b->ai_newmask = a->ai_newmask; |
| 455 |
else /* else ignore it */ |
| 456 |
b->ai_newmask = 0; |
| 457 |
ch_free( a ); |
| 458 |
rc = 0; |
| 459 |
continue; |
| 460 |
} |
| 461 |
} |
| 462 |
if (c_reply) { |
| 463 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 464 |
"duplicate index definition for attr \"%s\"", |
| 465 |
attrs[i] ); |
| 466 |
fprintf( stderr, "%s: line %d: %s\n", |
| 467 |
fname, lineno, c_reply->msg ); |
| 468 |
} |
| 469 |
|
| 470 |
rc = LDAP_PARAM_ERROR; |
| 471 |
goto done; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
done: |
| 476 |
ldap_charray_free( attrs ); |
| 477 |
if ( indexes != NULL ) ldap_charray_free( indexes ); |
| 478 |
|
| 479 |
return rc; |
| 480 |
} |
| 481 |
|
| 482 |
static int |
| 483 |
mdb_attr_index_unparser( void *v1, void *v2 ) |
| 484 |
{ |
| 485 |
AttrInfo *ai = v1; |
| 486 |
BerVarray *bva = v2; |
| 487 |
struct berval bv; |
| 488 |
char *ptr; |
| 489 |
|
| 490 |
slap_index2bvlen( ai->ai_indexmask, &bv ); |
| 491 |
if ( bv.bv_len ) { |
| 492 |
bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1; |
| 493 |
ptr = ch_malloc( bv.bv_len+1 ); |
| 494 |
bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val ); |
| 495 |
*bv.bv_val++ = ' '; |
| 496 |
slap_index2bv( ai->ai_indexmask, &bv ); |
| 497 |
bv.bv_val = ptr; |
| 498 |
ber_bvarray_add( bva, &bv ); |
| 499 |
} |
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
static AttributeDescription addef = { NULL, NULL, BER_BVC("default") }; |
| 504 |
static AttrInfo aidef = { &addef }; |
| 505 |
|
| 506 |
void |
| 507 |
mdb_attr_index_unparse( struct mdb_info *mdb, BerVarray *bva ) |
| 508 |
{ |
| 509 |
int i; |
| 510 |
|
| 511 |
if ( mdb->mi_defaultmask ) { |
| 512 |
aidef.ai_indexmask = mdb->mi_defaultmask; |
| 513 |
mdb_attr_index_unparser( &aidef, bva ); |
| 514 |
} |
| 515 |
for ( i=0; i<mdb->mi_nattrs; i++ ) |
| 516 |
if ( mdb->mi_attrs[i]->ai_indexmask ) |
| 517 |
mdb_attr_index_unparser( mdb->mi_attrs[i], bva ); |
| 518 |
} |
| 519 |
|
| 520 |
int |
| 521 |
mdb_attr_multi_config( |
| 522 |
struct mdb_info *mdb, |
| 523 |
const char *fname, |
| 524 |
int lineno, |
| 525 |
int argc, |
| 526 |
char **argv, |
| 527 |
struct config_reply_s *c_reply) |
| 528 |
{ |
| 529 |
int rc = 0; |
| 530 |
int i; |
| 531 |
unsigned hi,lo; |
| 532 |
char **attrs, *next, *s; |
| 533 |
|
| 534 |
attrs = ldap_str2charray( argv[0], "," ); |
| 535 |
|
| 536 |
if( attrs == NULL ) { |
| 537 |
fprintf( stderr, "%s: line %d: " |
| 538 |
"no attributes specified: %s\n", |
| 539 |
fname, lineno, argv[0] ); |
| 540 |
return LDAP_PARAM_ERROR; |
| 541 |
} |
| 542 |
|
| 543 |
hi = strtoul( argv[1], &next, 10 ); |
| 544 |
if ( next == argv[1] || next[0] != ',' ) |
| 545 |
goto badval; |
| 546 |
s = next+1; |
| 547 |
lo = strtoul( s, &next, 10 ); |
| 548 |
if ( next == s || next[0] != '\0' ) |
| 549 |
goto badval; |
| 550 |
|
| 551 |
if ( lo > hi ) { |
| 552 |
badval: |
| 553 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 554 |
"invalid hi/lo thresholds" ); |
| 555 |
fprintf( stderr, "%s: line %d: %s\n", |
| 556 |
fname, lineno, c_reply->msg ); |
| 557 |
return LDAP_PARAM_ERROR; |
| 558 |
} |
| 559 |
|
| 560 |
for ( i = 0; attrs[i] != NULL; i++ ) { |
| 561 |
AttrInfo *a; |
| 562 |
AttributeDescription *ad; |
| 563 |
const char *text; |
| 564 |
|
| 565 |
if( strcasecmp( attrs[i], "default" ) == 0 ) { |
| 566 |
mdb->mi_multi_hi = hi; |
| 567 |
mdb->mi_multi_lo = lo; |
| 568 |
continue; |
| 569 |
} |
| 570 |
|
| 571 |
ad = NULL; |
| 572 |
rc = slap_str2ad( attrs[i], &ad, &text ); |
| 573 |
|
| 574 |
if( rc != LDAP_SUCCESS ) { |
| 575 |
if ( c_reply ) |
| 576 |
{ |
| 577 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 578 |
"multival attribute \"%s\" undefined", |
| 579 |
attrs[i] ); |
| 580 |
|
| 581 |
fprintf( stderr, "%s: line %d: %s\n", |
| 582 |
fname, lineno, c_reply->msg ); |
| 583 |
} |
| 584 |
fail: |
| 585 |
goto done; |
| 586 |
} |
| 587 |
|
| 588 |
a = (AttrInfo *) ch_calloc( 1, sizeof(AttrInfo) ); |
| 589 |
|
| 590 |
a->ai_desc = ad; |
| 591 |
a->ai_multi_hi = hi; |
| 592 |
a->ai_multi_lo = lo; |
| 593 |
|
| 594 |
rc = ainfo_insert( mdb, a ); |
| 595 |
if( rc ) { |
| 596 |
AttrInfo *b = mdb_attr_mask( mdb, ad ); |
| 597 |
/* If this is just an index record, reuse it for multival info */ |
| 598 |
if ( b->ai_multi_lo == UINT_MAX ) { |
| 599 |
b->ai_multi_hi = a->ai_multi_hi; |
| 600 |
b->ai_multi_lo = a->ai_multi_lo; |
| 601 |
ch_free( a ); |
| 602 |
rc = 0; |
| 603 |
continue; |
| 604 |
} |
| 605 |
if (c_reply) { |
| 606 |
snprintf(c_reply->msg, sizeof(c_reply->msg), |
| 607 |
"duplicate multival definition for attr \"%s\"", |
| 608 |
attrs[i] ); |
| 609 |
fprintf( stderr, "%s: line %d: %s\n", |
| 610 |
fname, lineno, c_reply->msg ); |
| 611 |
} |
| 612 |
|
| 613 |
rc = LDAP_PARAM_ERROR; |
| 614 |
goto done; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
done: |
| 619 |
ldap_charray_free( attrs ); |
| 620 |
|
| 621 |
return rc; |
| 622 |
} |
| 623 |
|
| 624 |
static int |
| 625 |
mdb_attr_multi_unparser( void *v1, void *v2 ) |
| 626 |
{ |
| 627 |
AttrInfo *ai = v1; |
| 628 |
BerVarray *bva = v2; |
| 629 |
struct berval bv; |
| 630 |
char digbuf[sizeof("4294967296,4294967296")]; |
| 631 |
char *ptr; |
| 632 |
|
| 633 |
bv.bv_len = snprintf( digbuf, sizeof(digbuf), "%u,%u", |
| 634 |
ai->ai_multi_hi, ai->ai_multi_lo ); |
| 635 |
if ( bv.bv_len ) { |
| 636 |
bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1; |
| 637 |
ptr = ch_malloc( bv.bv_len+1 ); |
| 638 |
bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val ); |
| 639 |
*bv.bv_val++ = ' '; |
| 640 |
strcpy(bv.bv_val, digbuf); |
| 641 |
bv.bv_val = ptr; |
| 642 |
ber_bvarray_add( bva, &bv ); |
| 643 |
} |
| 644 |
return 0; |
| 645 |
} |
| 646 |
|
| 647 |
void |
| 648 |
mdb_attr_multi_unparse( struct mdb_info *mdb, BerVarray *bva ) |
| 649 |
{ |
| 650 |
int i; |
| 651 |
|
| 652 |
if ( mdb->mi_multi_hi < UINT_MAX ) { |
| 653 |
aidef.ai_multi_hi = mdb->mi_multi_hi; |
| 654 |
aidef.ai_multi_lo = mdb->mi_multi_lo; |
| 655 |
mdb_attr_multi_unparser( &aidef, bva ); |
| 656 |
} |
| 657 |
for ( i=0; i<mdb->mi_nattrs; i++ ) |
| 658 |
if ( mdb->mi_attrs[i]->ai_multi_hi < UINT_MAX ) |
| 659 |
mdb_attr_multi_unparser( mdb->mi_attrs[i], bva ); |
| 660 |
} |
| 661 |
|
| 662 |
void |
| 663 |
mdb_attr_multi_thresh( struct mdb_info *mdb, AttributeDescription *ad, unsigned *hi, unsigned *lo ) |
| 664 |
{ |
| 665 |
AttrInfo *ai = mdb_attr_mask( mdb, ad ); |
| 666 |
if ( ai && ai->ai_multi_hi < UINT_MAX ) |
| 667 |
{ |
| 668 |
if ( hi ) |
| 669 |
*hi = ai->ai_multi_hi; |
| 670 |
if ( lo ) |
| 671 |
*lo = ai->ai_multi_lo; |
| 672 |
} else |
| 673 |
{ |
| 674 |
if ( hi ) |
| 675 |
*hi = mdb->mi_multi_hi; |
| 676 |
if ( lo ) |
| 677 |
*lo = mdb->mi_multi_lo; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
void |
| 682 |
mdb_attr_info_free( AttrInfo *ai ) |
| 683 |
{ |
| 684 |
#ifdef LDAP_COMP_MATCH |
| 685 |
free( ai->ai_cr ); |
| 686 |
#endif |
| 687 |
free( ai ); |
| 688 |
} |
| 689 |
|
| 690 |
void |
| 691 |
mdb_attr_index_destroy( struct mdb_info *mdb ) |
| 692 |
{ |
| 693 |
int i; |
| 694 |
|
| 695 |
for ( i=0; i<mdb->mi_nattrs; i++ ) |
| 696 |
mdb_attr_info_free( mdb->mi_attrs[i] ); |
| 697 |
|
| 698 |
free( mdb->mi_attrs ); |
| 699 |
} |
| 700 |
|
| 701 |
void mdb_attr_index_free( struct mdb_info *mdb, AttributeDescription *ad ) |
| 702 |
{ |
| 703 |
int i; |
| 704 |
|
| 705 |
i = mdb_attr_slot( mdb, ad, NULL ); |
| 706 |
if ( i >= 0 ) { |
| 707 |
mdb_attr_info_free( mdb->mi_attrs[i] ); |
| 708 |
mdb->mi_nattrs--; |
| 709 |
for (; i<mdb->mi_nattrs; i++) |
| 710 |
mdb->mi_attrs[i] = mdb->mi_attrs[i+1]; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
void mdb_attr_flush( struct mdb_info *mdb ) |
| 715 |
{ |
| 716 |
int i; |
| 717 |
|
| 718 |
for ( i=0; i<mdb->mi_nattrs; i++ ) { |
| 719 |
if ( mdb->mi_attrs[i]->ai_indexmask & MDB_INDEX_DELETING ) { |
| 720 |
/* if this is also a multival rec, just clear index */ |
| 721 |
if ( mdb->mi_attrs[i]->ai_multi_lo < UINT_MAX ) { |
| 722 |
mdb->mi_attrs[i]->ai_indexmask = 0; |
| 723 |
mdb->mi_attrs[i]->ai_newmask = 0; |
| 724 |
} else { |
| 725 |
int j; |
| 726 |
mdb_attr_info_free( mdb->mi_attrs[i] ); |
| 727 |
mdb->mi_nattrs--; |
| 728 |
for (j=i; j<mdb->mi_nattrs; j++) |
| 729 |
mdb->mi_attrs[j] = mdb->mi_attrs[j+1]; |
| 730 |
i--; |
| 731 |
} |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
int mdb_ad_read( struct mdb_info *mdb, MDB_txn *txn ) |
| 737 |
{ |
| 738 |
int i, rc; |
| 739 |
MDB_cursor *mc; |
| 740 |
MDB_val key, data; |
| 741 |
struct berval bdata; |
| 742 |
const char *text; |
| 743 |
AttributeDescription *ad; |
| 744 |
|
| 745 |
rc = mdb_cursor_open( txn, mdb->mi_ad2id, &mc ); |
| 746 |
if ( rc ) { |
| 747 |
Debug( LDAP_DEBUG_ANY, |
| 748 |
"mdb_ad_read: cursor_open failed %s(%d)\n", |
| 749 |
mdb_strerror(rc), rc ); |
| 750 |
return rc; |
| 751 |
} |
| 752 |
|
| 753 |
/* our array is 1-based, an index of 0 means no data */ |
| 754 |
i = mdb->mi_numads+1; |
| 755 |
key.mv_size = sizeof(int); |
| 756 |
key.mv_data = &i; |
| 757 |
|
| 758 |
rc = mdb_cursor_get( mc, &key, &data, MDB_SET ); |
| 759 |
|
| 760 |
while ( rc == MDB_SUCCESS ) { |
| 761 |
bdata.bv_len = data.mv_size; |
| 762 |
bdata.bv_val = data.mv_data; |
| 763 |
ad = NULL; |
| 764 |
rc = slap_bv2ad( &bdata, &ad, &text ); |
| 765 |
if ( rc ) { |
| 766 |
rc = slap_bv2undef_ad( &bdata, &mdb->mi_ads[i], &text, 0 ); |
| 767 |
} else { |
| 768 |
if ( ad->ad_index >= MDB_MAXADS ) { |
| 769 |
Debug( LDAP_DEBUG_ANY, |
| 770 |
"mdb_adb_read: too many AttributeDescriptions in use\n" ); |
| 771 |
return LDAP_OTHER; |
| 772 |
} |
| 773 |
mdb->mi_adxs[ad->ad_index] = i; |
| 774 |
mdb->mi_ads[i] = ad; |
| 775 |
} |
| 776 |
i++; |
| 777 |
rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT ); |
| 778 |
} |
| 779 |
mdb->mi_numads = i-1; |
| 780 |
|
| 781 |
done: |
| 782 |
if ( rc == MDB_NOTFOUND ) |
| 783 |
rc = 0; |
| 784 |
|
| 785 |
mdb_cursor_close( mc ); |
| 786 |
|
| 787 |
return rc; |
| 788 |
} |
| 789 |
|
| 790 |
int mdb_ad_get( struct mdb_info *mdb, MDB_txn *txn, AttributeDescription *ad ) |
| 791 |
{ |
| 792 |
int i, rc; |
| 793 |
MDB_val key, val; |
| 794 |
|
| 795 |
rc = mdb_ad_read( mdb, txn ); |
| 796 |
if (rc) |
| 797 |
return rc; |
| 798 |
|
| 799 |
if ( mdb->mi_adxs[ad->ad_index] ) |
| 800 |
return 0; |
| 801 |
|
| 802 |
i = mdb->mi_numads+1; |
| 803 |
key.mv_size = sizeof(int); |
| 804 |
key.mv_data = &i; |
| 805 |
val.mv_size = ad->ad_cname.bv_len; |
| 806 |
val.mv_data = ad->ad_cname.bv_val; |
| 807 |
|
| 808 |
rc = mdb_put( txn, mdb->mi_ad2id, &key, &val, 0 ); |
| 809 |
if ( rc == MDB_SUCCESS ) { |
| 810 |
mdb->mi_adxs[ad->ad_index] = i; |
| 811 |
mdb->mi_ads[i] = ad; |
| 812 |
mdb->mi_numads = i; |
| 813 |
} else { |
| 814 |
Debug( LDAP_DEBUG_ANY, |
| 815 |
"mdb_ad_get: mdb_put failed %s(%d)\n", |
| 816 |
mdb_strerror(rc), rc ); |
| 817 |
} |
| 818 |
|
| 819 |
return rc; |
| 820 |
} |
| 821 |
|
| 822 |
void mdb_ad_unwind( struct mdb_info *mdb, int prev_ads ) |
| 823 |
{ |
| 824 |
int i; |
| 825 |
|
| 826 |
for (i=mdb->mi_numads; i>prev_ads; i--) { |
| 827 |
mdb->mi_adxs[mdb->mi_ads[i]->ad_index] = 0; |
| 828 |
mdb->mi_ads[i] = NULL; |
| 829 |
} |
| 830 |
mdb->mi_numads = i; |
| 831 |
} |