| 1 |
/* dn2id.c - routines to deal with the dn2id index */ |
| 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 |
#include <ac/string.h> |
| 21 |
|
| 22 |
#include "back-mdb.h" |
| 23 |
#include "idl.h" |
| 24 |
#include "lutil.h" |
| 25 |
|
| 26 |
/* Management routines for a hierarchically structured database. |
| 27 |
* |
| 28 |
* Instead of a ldbm-style dn2id database, we use a hierarchical one. Each |
| 29 |
* entry in this database is a struct diskNode, keyed by entryID and with |
| 30 |
* the data containing the RDN and entryID of the node's children. We use |
| 31 |
* a B-Tree with sorted duplicates to store all the children of a node under |
| 32 |
* the same key. Also, the first item under the key contains the entry's own |
| 33 |
* rdn and the ID of the node's parent, to allow bottom-up tree traversal as |
| 34 |
* well as top-down. To keep this info first in the list, the high bit of all |
| 35 |
* subsequent nrdnlen's is always set. This means we can only accommodate |
| 36 |
* RDNs up to length 32767, but that's fine since full DNs are already |
| 37 |
* restricted to 8192. |
| 38 |
* |
| 39 |
* Also each child node contains a count of the number of entries in |
| 40 |
* its subtree, appended after its entryID. |
| 41 |
* |
| 42 |
* The diskNode is a variable length structure. This definition is not |
| 43 |
* directly usable for in-memory manipulation. |
| 44 |
*/ |
| 45 |
typedef struct diskNode { |
| 46 |
unsigned char nrdnlen[2]; |
| 47 |
char nrdn[1]; |
| 48 |
char rdn[1]; /* variable placement */ |
| 49 |
unsigned char entryID[sizeof(ID)]; /* variable placement */ |
| 50 |
/* unsigned char nsubs[sizeof(ID)]; in child nodes only */ |
| 51 |
} diskNode; |
| 52 |
|
| 53 |
/* Sort function for the sorted duplicate data items of a dn2id key. |
| 54 |
* Sorts based on normalized RDN, in length order. |
| 55 |
*/ |
| 56 |
int |
| 57 |
mdb_dup_compare( |
| 58 |
const MDB_val *usrkey, |
| 59 |
const MDB_val *curkey |
| 60 |
) |
| 61 |
{ |
| 62 |
diskNode *un, *cn; |
| 63 |
int rc, nrlen; |
| 64 |
|
| 65 |
un = (diskNode *)usrkey->mv_data; |
| 66 |
cn = (diskNode *)curkey->mv_data; |
| 67 |
|
| 68 |
/* data is not aligned, cannot compare directly */ |
| 69 |
rc = un->nrdnlen[0] - cn->nrdnlen[0]; |
| 70 |
if ( rc ) return rc; |
| 71 |
rc = un->nrdnlen[1] - cn->nrdnlen[1]; |
| 72 |
if ( rc ) return rc; |
| 73 |
|
| 74 |
nrlen = ((un->nrdnlen[0] & 0x7f) << 8) | un->nrdnlen[1]; |
| 75 |
return strncmp( un->nrdn, cn->nrdn, nrlen ); |
| 76 |
} |
| 77 |
|
| 78 |
/* We add two elements to the DN2ID database - a data item under the parent's |
| 79 |
* entryID containing the child's RDN and entryID, and an item under the |
| 80 |
* child's entryID containing the parent's entryID. |
| 81 |
*/ |
| 82 |
int |
| 83 |
mdb_dn2id_add( |
| 84 |
Operation *op, |
| 85 |
MDB_cursor *mcp, |
| 86 |
MDB_cursor *mcd, |
| 87 |
ID pid, |
| 88 |
ID nsubs, |
| 89 |
int upsub, |
| 90 |
Entry *e ) |
| 91 |
{ |
| 92 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 93 |
MDB_val key, data; |
| 94 |
ID nid; |
| 95 |
int rc, rlen, nrlen; |
| 96 |
diskNode *d; |
| 97 |
char *ptr; |
| 98 |
|
| 99 |
Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n", |
| 100 |
e->e_id, e->e_ndn ? e->e_ndn : "" ); |
| 101 |
|
| 102 |
nrlen = dn_rdnlen( op->o_bd, &e->e_nname ); |
| 103 |
if (nrlen) { |
| 104 |
rlen = dn_rdnlen( op->o_bd, &e->e_name ); |
| 105 |
} else { |
| 106 |
nrlen = e->e_nname.bv_len; |
| 107 |
rlen = e->e_name.bv_len; |
| 108 |
} |
| 109 |
|
| 110 |
d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen + sizeof(ID), op->o_tmpmemctx); |
| 111 |
d->nrdnlen[1] = nrlen & 0xff; |
| 112 |
d->nrdnlen[0] = (nrlen >> 8) | 0x80; |
| 113 |
ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen ); |
| 114 |
*ptr++ = '\0'; |
| 115 |
ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen ); |
| 116 |
*ptr++ = '\0'; |
| 117 |
memcpy( ptr, &e->e_id, sizeof( ID )); |
| 118 |
ptr += sizeof( ID ); |
| 119 |
memcpy( ptr, &nsubs, sizeof( ID )); |
| 120 |
|
| 121 |
key.mv_size = sizeof(ID); |
| 122 |
key.mv_data = &nid; |
| 123 |
|
| 124 |
nid = pid; |
| 125 |
|
| 126 |
/* Need to make dummy root node once. Subsequent attempts |
| 127 |
* will fail harmlessly. |
| 128 |
*/ |
| 129 |
if ( pid == 0 ) { |
| 130 |
diskNode dummy = {{0, 0}, "", "", ""}; |
| 131 |
data.mv_data = &dummy; |
| 132 |
data.mv_size = sizeof(diskNode); |
| 133 |
|
| 134 |
mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA ); |
| 135 |
} |
| 136 |
|
| 137 |
data.mv_data = d; |
| 138 |
data.mv_size = sizeof(diskNode) + rlen + nrlen + sizeof( ID ); |
| 139 |
|
| 140 |
/* Add our child node under parent's key */ |
| 141 |
rc = mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA ); |
| 142 |
|
| 143 |
/* Add our own node */ |
| 144 |
if (rc == 0) { |
| 145 |
int flag = MDB_NODUPDATA; |
| 146 |
nid = e->e_id; |
| 147 |
/* drop subtree count */ |
| 148 |
data.mv_size -= sizeof( ID ); |
| 149 |
ptr -= sizeof( ID ); |
| 150 |
memcpy( ptr, &pid, sizeof( ID )); |
| 151 |
d->nrdnlen[0] ^= 0x80; |
| 152 |
|
| 153 |
if ((slapMode & SLAP_TOOL_MODE) || (e->e_id == mdb->mi_nextid)) |
| 154 |
flag |= MDB_APPEND; |
| 155 |
rc = mdb_cursor_put( mcd, &key, &data, flag ); |
| 156 |
} |
| 157 |
op->o_tmpfree( d, op->o_tmpmemctx ); |
| 158 |
|
| 159 |
/* Add our subtree count to all superiors */ |
| 160 |
if ( rc == 0 && upsub && pid ) { |
| 161 |
ID subs; |
| 162 |
nid = pid; |
| 163 |
do { |
| 164 |
/* Get parent's RDN */ |
| 165 |
rc = mdb_cursor_get( mcp, &key, &data, MDB_SET ); |
| 166 |
if ( !rc ) { |
| 167 |
char *p2; |
| 168 |
ptr = (char *)data.mv_data + data.mv_size - sizeof( ID ); |
| 169 |
memcpy( &nid, ptr, sizeof( ID )); |
| 170 |
/* Get parent's node under grandparent */ |
| 171 |
d = data.mv_data; |
| 172 |
rlen = ( d->nrdnlen[0] << 8 ) | d->nrdnlen[1]; |
| 173 |
p2 = op->o_tmpalloc( rlen + 2, op->o_tmpmemctx ); |
| 174 |
memcpy( p2, data.mv_data, rlen+2 ); |
| 175 |
*p2 ^= 0x80; |
| 176 |
data.mv_data = p2; |
| 177 |
rc = mdb_cursor_get( mcp, &key, &data, MDB_GET_BOTH ); |
| 178 |
op->o_tmpfree( p2, op->o_tmpmemctx ); |
| 179 |
if ( !rc ) { |
| 180 |
/* Get parent's subtree count */ |
| 181 |
ptr = (char *)data.mv_data + data.mv_size - sizeof( ID ); |
| 182 |
memcpy( &subs, ptr, sizeof( ID )); |
| 183 |
subs += nsubs; |
| 184 |
p2 = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx ); |
| 185 |
memcpy( p2, data.mv_data, data.mv_size - sizeof( ID )); |
| 186 |
memcpy( p2+data.mv_size - sizeof( ID ), &subs, sizeof( ID )); |
| 187 |
data.mv_data = p2; |
| 188 |
rc = mdb_cursor_put( mcp, &key, &data, MDB_CURRENT ); |
| 189 |
op->o_tmpfree( p2, op->o_tmpmemctx ); |
| 190 |
} |
| 191 |
} |
| 192 |
if ( rc ) |
| 193 |
break; |
| 194 |
} while ( nid ); |
| 195 |
} |
| 196 |
|
| 197 |
Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc ); |
| 198 |
|
| 199 |
return rc; |
| 200 |
} |
| 201 |
|
| 202 |
/* mc must have been set by mdb_dn2id */ |
| 203 |
int |
| 204 |
mdb_dn2id_delete( |
| 205 |
Operation *op, |
| 206 |
MDB_cursor *mc, |
| 207 |
ID id, |
| 208 |
ID nsubs ) |
| 209 |
{ |
| 210 |
ID nid; |
| 211 |
char *ptr; |
| 212 |
int rc; |
| 213 |
|
| 214 |
Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx\n", |
| 215 |
id ); |
| 216 |
|
| 217 |
/* Delete our ID from the parent's list */ |
| 218 |
rc = mdb_cursor_del( mc, 0 ); |
| 219 |
|
| 220 |
/* Delete our ID from the tree. With sorted duplicates, this |
| 221 |
* will leave any child nodes still hanging around. This is OK |
| 222 |
* for modrdn, which will add our info back in later. |
| 223 |
*/ |
| 224 |
if ( rc == 0 ) { |
| 225 |
MDB_val key, data; |
| 226 |
if ( nsubs ) { |
| 227 |
mdb_cursor_get( mc, &key, NULL, MDB_GET_CURRENT ); |
| 228 |
memcpy( &nid, key.mv_data, sizeof( ID )); |
| 229 |
} |
| 230 |
key.mv_size = sizeof(ID); |
| 231 |
key.mv_data = &id; |
| 232 |
rc = mdb_cursor_get( mc, &key, &data, MDB_SET ); |
| 233 |
if ( rc == 0 ) |
| 234 |
rc = mdb_cursor_del( mc, 0 ); |
| 235 |
} |
| 236 |
|
| 237 |
/* Delete our subtree count from all superiors */ |
| 238 |
if ( rc == 0 && nsubs && nid ) { |
| 239 |
MDB_val key, data; |
| 240 |
ID subs; |
| 241 |
key.mv_data = &nid; |
| 242 |
key.mv_size = sizeof( ID ); |
| 243 |
do { |
| 244 |
rc = mdb_cursor_get( mc, &key, &data, MDB_SET ); |
| 245 |
if ( !rc ) { |
| 246 |
char *p2; |
| 247 |
diskNode *d; |
| 248 |
int rlen; |
| 249 |
ptr = (char *)data.mv_data + data.mv_size - sizeof( ID ); |
| 250 |
memcpy( &nid, ptr, sizeof( ID )); |
| 251 |
/* Get parent's node under grandparent */ |
| 252 |
d = data.mv_data; |
| 253 |
rlen = ( d->nrdnlen[0] << 8 ) | d->nrdnlen[1]; |
| 254 |
p2 = op->o_tmpalloc( rlen + 2, op->o_tmpmemctx ); |
| 255 |
memcpy( p2, data.mv_data, rlen+2 ); |
| 256 |
*p2 ^= 0x80; |
| 257 |
data.mv_data = p2; |
| 258 |
rc = mdb_cursor_get( mc, &key, &data, MDB_GET_BOTH ); |
| 259 |
op->o_tmpfree( p2, op->o_tmpmemctx ); |
| 260 |
if ( !rc ) { |
| 261 |
/* Get parent's subtree count */ |
| 262 |
ptr = (char *)data.mv_data + data.mv_size - sizeof( ID ); |
| 263 |
memcpy( &subs, ptr, sizeof( ID )); |
| 264 |
subs -= nsubs; |
| 265 |
p2 = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx ); |
| 266 |
memcpy( p2, data.mv_data, data.mv_size - sizeof( ID )); |
| 267 |
memcpy( p2+data.mv_size - sizeof( ID ), &subs, sizeof( ID )); |
| 268 |
data.mv_data = p2; |
| 269 |
rc = mdb_cursor_put( mc, &key, &data, MDB_CURRENT ); |
| 270 |
op->o_tmpfree( p2, op->o_tmpmemctx ); |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
if ( rc ) |
| 275 |
break; |
| 276 |
} while ( nid ); |
| 277 |
} |
| 278 |
|
| 279 |
Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", id, rc ); |
| 280 |
return rc; |
| 281 |
} |
| 282 |
|
| 283 |
/* return last found ID in *id if no match |
| 284 |
* If mc is provided, it will be left pointing to the RDN's |
| 285 |
* record under the parent's ID. If nsubs is provided, return |
| 286 |
* the number of entries in this entry's subtree. |
| 287 |
*/ |
| 288 |
int |
| 289 |
mdb_dn2id( |
| 290 |
Operation *op, |
| 291 |
MDB_txn *txn, |
| 292 |
MDB_cursor *mc, |
| 293 |
struct berval *in, |
| 294 |
ID *id, |
| 295 |
ID *nsubs, |
| 296 |
struct berval *matched, |
| 297 |
struct berval *nmatched ) |
| 298 |
{ |
| 299 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 300 |
MDB_cursor *cursor; |
| 301 |
MDB_dbi dbi = mdb->mi_dn2id; |
| 302 |
MDB_val key, data; |
| 303 |
int rc = 0, nrlen; |
| 304 |
diskNode *d; |
| 305 |
char *ptr; |
| 306 |
char dn[SLAP_LDAPDN_MAXLEN]; |
| 307 |
ID pid, nid; |
| 308 |
struct berval tmp; |
| 309 |
|
| 310 |
Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val ? in->bv_val : "" ); |
| 311 |
|
| 312 |
if ( matched ) { |
| 313 |
matched->bv_val = dn + sizeof(dn) - 1; |
| 314 |
matched->bv_len = 0; |
| 315 |
*matched->bv_val-- = '\0'; |
| 316 |
} |
| 317 |
if ( nmatched ) { |
| 318 |
nmatched->bv_len = 0; |
| 319 |
nmatched->bv_val = 0; |
| 320 |
} |
| 321 |
|
| 322 |
if ( !in->bv_len ) { |
| 323 |
*id = 0; |
| 324 |
nid = 0; |
| 325 |
goto done; |
| 326 |
} |
| 327 |
|
| 328 |
tmp = *in; |
| 329 |
|
| 330 |
if ( op->o_bd->be_nsuffix[0].bv_len ) { |
| 331 |
nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len; |
| 332 |
tmp.bv_val += nrlen; |
| 333 |
tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len; |
| 334 |
} else { |
| 335 |
for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- ) |
| 336 |
if (DN_SEPARATOR(*ptr)) |
| 337 |
break; |
| 338 |
ptr++; |
| 339 |
tmp.bv_len -= ptr - tmp.bv_val; |
| 340 |
tmp.bv_val = ptr; |
| 341 |
} |
| 342 |
nid = 0; |
| 343 |
key.mv_size = sizeof(ID); |
| 344 |
|
| 345 |
if ( mc ) { |
| 346 |
cursor = mc; |
| 347 |
} else { |
| 348 |
rc = mdb_cursor_open( txn, dbi, &cursor ); |
| 349 |
if ( rc ) goto done; |
| 350 |
} |
| 351 |
|
| 352 |
for (;;) { |
| 353 |
key.mv_data = &pid; |
| 354 |
pid = nid; |
| 355 |
|
| 356 |
data.mv_size = sizeof(diskNode) + tmp.bv_len; |
| 357 |
d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx ); |
| 358 |
d->nrdnlen[1] = tmp.bv_len & 0xff; |
| 359 |
d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80; |
| 360 |
ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len ); |
| 361 |
*ptr = '\0'; |
| 362 |
data.mv_data = d; |
| 363 |
rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH ); |
| 364 |
op->o_tmpfree( d, op->o_tmpmemctx ); |
| 365 |
if ( rc ) |
| 366 |
break; |
| 367 |
ptr = (char *) data.mv_data + data.mv_size - 2*sizeof(ID); |
| 368 |
memcpy( &nid, ptr, sizeof(ID)); |
| 369 |
|
| 370 |
/* grab the non-normalized RDN */ |
| 371 |
if ( matched ) { |
| 372 |
int rlen; |
| 373 |
d = data.mv_data; |
| 374 |
rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len - sizeof(ID); |
| 375 |
matched->bv_len += rlen; |
| 376 |
matched->bv_val -= rlen + 1; |
| 377 |
ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len ); |
| 378 |
if ( pid ) { |
| 379 |
*ptr = ','; |
| 380 |
matched->bv_len++; |
| 381 |
} |
| 382 |
} |
| 383 |
if ( nmatched ) { |
| 384 |
nmatched->bv_val = tmp.bv_val; |
| 385 |
} |
| 386 |
|
| 387 |
if ( tmp.bv_val > in->bv_val ) { |
| 388 |
for (ptr = tmp.bv_val - 2; ptr > in->bv_val && |
| 389 |
!DN_SEPARATOR(*ptr); ptr--) /* empty */; |
| 390 |
if ( ptr >= in->bv_val ) { |
| 391 |
if (DN_SEPARATOR(*ptr)) ptr++; |
| 392 |
tmp.bv_len = tmp.bv_val - ptr - 1; |
| 393 |
tmp.bv_val = ptr; |
| 394 |
} |
| 395 |
} else { |
| 396 |
break; |
| 397 |
} |
| 398 |
} |
| 399 |
*id = nid; |
| 400 |
/* return subtree count if requested */ |
| 401 |
if ( !rc && nsubs ) { |
| 402 |
ptr = (char *)data.mv_data + data.mv_size - sizeof(ID); |
| 403 |
memcpy( nsubs, ptr, sizeof( ID )); |
| 404 |
} |
| 405 |
if ( !mc ) |
| 406 |
mdb_cursor_close( cursor ); |
| 407 |
done: |
| 408 |
if ( matched ) { |
| 409 |
if ( matched->bv_len ) { |
| 410 |
ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx ); |
| 411 |
strcpy( ptr, matched->bv_val ); |
| 412 |
matched->bv_val = ptr; |
| 413 |
} else { |
| 414 |
if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) { |
| 415 |
ber_dupbv( matched, (struct berval *)&slap_empty_bv ); |
| 416 |
} else { |
| 417 |
matched->bv_val = NULL; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
if ( nmatched ) { |
| 422 |
if ( nmatched->bv_val ) { |
| 423 |
nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val); |
| 424 |
} else { |
| 425 |
*nmatched = slap_empty_bv; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
if( rc != 0 ) { |
| 430 |
Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n", |
| 431 |
mdb_strerror( rc ), rc ); |
| 432 |
} else { |
| 433 |
Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n", |
| 434 |
nid ); |
| 435 |
} |
| 436 |
|
| 437 |
return rc; |
| 438 |
} |
| 439 |
|
| 440 |
/* return IDs from root to parent of DN */ |
| 441 |
int |
| 442 |
mdb_dn2sups( |
| 443 |
Operation *op, |
| 444 |
MDB_txn *txn, |
| 445 |
struct berval *in, |
| 446 |
ID *ids ) |
| 447 |
{ |
| 448 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 449 |
MDB_cursor *cursor; |
| 450 |
MDB_dbi dbi = mdb->mi_dn2id; |
| 451 |
MDB_val key, data; |
| 452 |
int rc = 0, nrlen; |
| 453 |
diskNode *d; |
| 454 |
char *ptr; |
| 455 |
ID pid, nid; |
| 456 |
struct berval tmp; |
| 457 |
|
| 458 |
Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val ); |
| 459 |
|
| 460 |
if ( !in->bv_len ) { |
| 461 |
goto done; |
| 462 |
} |
| 463 |
|
| 464 |
tmp = *in; |
| 465 |
|
| 466 |
nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len; |
| 467 |
tmp.bv_val += nrlen; |
| 468 |
tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len; |
| 469 |
nid = 0; |
| 470 |
key.mv_size = sizeof(ID); |
| 471 |
|
| 472 |
rc = mdb_cursor_open( txn, dbi, &cursor ); |
| 473 |
if ( rc ) goto done; |
| 474 |
|
| 475 |
for (;;) { |
| 476 |
key.mv_data = &pid; |
| 477 |
pid = nid; |
| 478 |
|
| 479 |
data.mv_size = sizeof(diskNode) + tmp.bv_len; |
| 480 |
d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx ); |
| 481 |
d->nrdnlen[1] = tmp.bv_len & 0xff; |
| 482 |
d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80; |
| 483 |
ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len ); |
| 484 |
*ptr = '\0'; |
| 485 |
data.mv_data = d; |
| 486 |
rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH ); |
| 487 |
op->o_tmpfree( d, op->o_tmpmemctx ); |
| 488 |
if ( rc ) |
| 489 |
break; |
| 490 |
ptr = (char *) data.mv_data + data.mv_size - 2*sizeof(ID); |
| 491 |
memcpy( &nid, ptr, sizeof(ID)); |
| 492 |
|
| 493 |
if ( pid ) |
| 494 |
mdb_idl_insert( ids, pid ); |
| 495 |
|
| 496 |
if ( tmp.bv_val > in->bv_val ) { |
| 497 |
for (ptr = tmp.bv_val - 2; ptr > in->bv_val && |
| 498 |
!DN_SEPARATOR(*ptr); ptr--) /* empty */; |
| 499 |
if ( ptr >= in->bv_val ) { |
| 500 |
if (DN_SEPARATOR(*ptr)) ptr++; |
| 501 |
tmp.bv_len = tmp.bv_val - ptr - 1; |
| 502 |
tmp.bv_val = ptr; |
| 503 |
} |
| 504 |
} else { |
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
mdb_cursor_close( cursor ); |
| 509 |
done: |
| 510 |
if( rc != 0 ) { |
| 511 |
Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n", |
| 512 |
mdb_strerror( rc ), rc ); |
| 513 |
} |
| 514 |
|
| 515 |
return rc; |
| 516 |
} |
| 517 |
|
| 518 |
int |
| 519 |
mdb_dn2id_children( |
| 520 |
Operation *op, |
| 521 |
MDB_txn *txn, |
| 522 |
Entry *e ) |
| 523 |
{ |
| 524 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 525 |
MDB_dbi dbi = mdb->mi_dn2id; |
| 526 |
MDB_val key, data; |
| 527 |
MDB_cursor *cursor; |
| 528 |
int rc; |
| 529 |
ID id; |
| 530 |
|
| 531 |
key.mv_size = sizeof(ID); |
| 532 |
key.mv_data = &id; |
| 533 |
id = e->e_id; |
| 534 |
|
| 535 |
rc = mdb_cursor_open( txn, dbi, &cursor ); |
| 536 |
if ( rc ) return rc; |
| 537 |
|
| 538 |
rc = mdb_cursor_get( cursor, &key, &data, MDB_SET ); |
| 539 |
if ( rc == 0 ) { |
| 540 |
size_t dkids; |
| 541 |
rc = mdb_cursor_count( cursor, &dkids ); |
| 542 |
if ( rc == 0 ) { |
| 543 |
if ( dkids < 2 ) rc = MDB_NOTFOUND; |
| 544 |
} |
| 545 |
} |
| 546 |
mdb_cursor_close( cursor ); |
| 547 |
return rc; |
| 548 |
} |
| 549 |
|
| 550 |
int |
| 551 |
mdb_id2name( |
| 552 |
Operation *op, |
| 553 |
MDB_txn *txn, |
| 554 |
MDB_cursor **cursp, |
| 555 |
ID id, |
| 556 |
struct berval *name, |
| 557 |
struct berval *nname ) |
| 558 |
{ |
| 559 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 560 |
MDB_dbi dbi = mdb->mi_dn2id; |
| 561 |
MDB_val key, data; |
| 562 |
MDB_cursor *cursor; |
| 563 |
int rc, len, nlen; |
| 564 |
char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr; |
| 565 |
char *dptr, *nptr; |
| 566 |
diskNode *d; |
| 567 |
|
| 568 |
key.mv_size = sizeof(ID); |
| 569 |
|
| 570 |
if ( !*cursp ) { |
| 571 |
rc = mdb_cursor_open( txn, dbi, cursp ); |
| 572 |
if ( rc ) return rc; |
| 573 |
} |
| 574 |
cursor = *cursp; |
| 575 |
|
| 576 |
len = 0; |
| 577 |
nlen = 0; |
| 578 |
dptr = dn; |
| 579 |
nptr = ndn; |
| 580 |
while (id) { |
| 581 |
unsigned int nrlen, rlen; |
| 582 |
key.mv_data = &id; |
| 583 |
data.mv_size = 0; |
| 584 |
data.mv_data = ""; |
| 585 |
rc = mdb_cursor_get( cursor, &key, &data, MDB_SET ); |
| 586 |
if ( rc ) break; |
| 587 |
ptr = data.mv_data; |
| 588 |
ptr += data.mv_size - sizeof(ID); |
| 589 |
memcpy( &id, ptr, sizeof(ID) ); |
| 590 |
d = data.mv_data; |
| 591 |
nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1]; |
| 592 |
rlen = data.mv_size - sizeof(diskNode) - nrlen; |
| 593 |
assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */ |
| 594 |
if (nptr > ndn) { |
| 595 |
*nptr++ = ','; |
| 596 |
*dptr++ = ','; |
| 597 |
} |
| 598 |
/* copy name and trailing NUL */ |
| 599 |
memcpy( nptr, d->nrdn, nrlen+1 ); |
| 600 |
memcpy( dptr, d->nrdn+nrlen+1, rlen+1 ); |
| 601 |
nptr += nrlen; |
| 602 |
dptr += rlen; |
| 603 |
} |
| 604 |
if ( rc == 0 ) { |
| 605 |
name->bv_len = dptr - dn; |
| 606 |
nname->bv_len = nptr - ndn; |
| 607 |
name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx ); |
| 608 |
nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx ); |
| 609 |
memcpy( name->bv_val, dn, name->bv_len ); |
| 610 |
name->bv_val[name->bv_len] = '\0'; |
| 611 |
memcpy( nname->bv_val, ndn, nname->bv_len ); |
| 612 |
nname->bv_val[nname->bv_len] = '\0'; |
| 613 |
} |
| 614 |
return rc; |
| 615 |
} |
| 616 |
|
| 617 |
/* Find each id in ids that is a child of base and move it to res. |
| 618 |
*/ |
| 619 |
int |
| 620 |
mdb_idscope( |
| 621 |
Operation *op, |
| 622 |
MDB_txn *txn, |
| 623 |
ID base, |
| 624 |
ID *ids, |
| 625 |
ID *res ) |
| 626 |
{ |
| 627 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 628 |
MDB_dbi dbi = mdb->mi_dn2id; |
| 629 |
MDB_val key, data; |
| 630 |
MDB_cursor *cursor; |
| 631 |
ID ida, id, cid = 0, ci0 = 0, idc = 0; |
| 632 |
char *ptr; |
| 633 |
int rc, copy; |
| 634 |
|
| 635 |
key.mv_size = sizeof(ID); |
| 636 |
|
| 637 |
MDB_IDL_ZERO( res ); |
| 638 |
|
| 639 |
rc = mdb_cursor_open( txn, dbi, &cursor ); |
| 640 |
if ( rc ) return rc; |
| 641 |
|
| 642 |
/* first see if base has any children at all */ |
| 643 |
key.mv_data = &base; |
| 644 |
rc = mdb_cursor_get( cursor, &key, &data, MDB_SET ); |
| 645 |
if ( rc ) { |
| 646 |
goto leave; |
| 647 |
} |
| 648 |
{ |
| 649 |
size_t dkids; |
| 650 |
rc = mdb_cursor_count( cursor, &dkids ); |
| 651 |
if ( rc == 0 ) { |
| 652 |
if ( dkids < 2 ) { |
| 653 |
goto leave; |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
ida = mdb_idl_first( ids, &cid ); |
| 659 |
|
| 660 |
/* Don't bother moving out of ids if it's a range */ |
| 661 |
if (!MDB_IDL_IS_RANGE(ids)) { |
| 662 |
idc = ids[0]; |
| 663 |
ci0 = cid; |
| 664 |
} |
| 665 |
|
| 666 |
while (ida != NOID) { |
| 667 |
copy = 1; |
| 668 |
id = ida; |
| 669 |
while (id) { |
| 670 |
key.mv_data = &id; |
| 671 |
rc = mdb_cursor_get( cursor, &key, &data, MDB_SET ); |
| 672 |
if ( rc ) { |
| 673 |
/* not found, drop this from ids */ |
| 674 |
copy = 0; |
| 675 |
break; |
| 676 |
} |
| 677 |
ptr = data.mv_data; |
| 678 |
ptr += data.mv_size - sizeof(ID); |
| 679 |
memcpy( &id, ptr, sizeof(ID) ); |
| 680 |
if ( id == base ) { |
| 681 |
if ( res[0] >= MDB_idl_db_max ) { |
| 682 |
/* too many aliases in scope. Fallback to range */ |
| 683 |
MDB_IDL_RANGE( res, MDB_IDL_FIRST( ids ), MDB_IDL_LAST( ids )); |
| 684 |
goto leave; |
| 685 |
} |
| 686 |
res[0]++; |
| 687 |
res[res[0]] = ida; |
| 688 |
copy = 0; |
| 689 |
break; |
| 690 |
} |
| 691 |
if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) |
| 692 |
break; |
| 693 |
} |
| 694 |
if (idc) { |
| 695 |
if (copy) { |
| 696 |
if (ci0 != cid) |
| 697 |
ids[ci0] = ids[cid]; |
| 698 |
ci0++; |
| 699 |
} else |
| 700 |
idc--; |
| 701 |
} |
| 702 |
ida = mdb_idl_next( ids, &cid ); |
| 703 |
} |
| 704 |
if (!MDB_IDL_IS_RANGE( ids )) |
| 705 |
ids[0] = idc; |
| 706 |
|
| 707 |
leave: |
| 708 |
mdb_cursor_close( cursor ); |
| 709 |
return rc; |
| 710 |
} |
| 711 |
|
| 712 |
/* See if base is a child of any of the scopes |
| 713 |
*/ |
| 714 |
int |
| 715 |
mdb_idscopes( |
| 716 |
Operation *op, |
| 717 |
IdScopes *isc ) |
| 718 |
{ |
| 719 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 720 |
MDB_dbi dbi = mdb->mi_dn2id; |
| 721 |
MDB_val key, data; |
| 722 |
ID id, prev; |
| 723 |
ID2 id2; |
| 724 |
char *ptr; |
| 725 |
int rc = 0; |
| 726 |
unsigned int x; |
| 727 |
unsigned int nrlen, rlen; |
| 728 |
diskNode *d; |
| 729 |
|
| 730 |
key.mv_size = sizeof(ID); |
| 731 |
|
| 732 |
if ( !isc->mc ) { |
| 733 |
rc = mdb_cursor_open( isc->mt, dbi, &isc->mc ); |
| 734 |
if ( rc ) return rc; |
| 735 |
} |
| 736 |
|
| 737 |
id = isc->id; |
| 738 |
|
| 739 |
/* Catch entries from deref'd aliases */ |
| 740 |
x = mdb_id2l_search( isc->scopes, id ); |
| 741 |
if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) { |
| 742 |
isc->nscope = x; |
| 743 |
return MDB_SUCCESS; |
| 744 |
} |
| 745 |
|
| 746 |
isc->sctmp[0].mid = 0; |
| 747 |
while (id) { |
| 748 |
if ( !rc ) { |
| 749 |
key.mv_data = &id; |
| 750 |
rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET ); |
| 751 |
if ( rc ) |
| 752 |
return rc; |
| 753 |
|
| 754 |
/* save RDN info */ |
| 755 |
} |
| 756 |
d = data.mv_data; |
| 757 |
nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1]; |
| 758 |
rlen = data.mv_size - sizeof(diskNode) - nrlen; |
| 759 |
isc->nrdns[isc->numrdns].bv_len = nrlen; |
| 760 |
isc->nrdns[isc->numrdns].bv_val = d->nrdn; |
| 761 |
isc->rdns[isc->numrdns].bv_len = rlen; |
| 762 |
isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1; |
| 763 |
isc->numrdns++; |
| 764 |
|
| 765 |
if (!rc && id != isc->id) { |
| 766 |
/* remember our chain of parents */ |
| 767 |
id2.mid = id; |
| 768 |
id2.mval = data; |
| 769 |
mdb_id2l_insert( isc->sctmp, &id2 ); |
| 770 |
} |
| 771 |
ptr = data.mv_data; |
| 772 |
ptr += data.mv_size - sizeof(ID); |
| 773 |
prev = id; |
| 774 |
memcpy( &id, ptr, sizeof(ID) ); |
| 775 |
/* If we didn't advance, some parent is missing */ |
| 776 |
if ( id == prev ) |
| 777 |
return MDB_NOTFOUND; |
| 778 |
|
| 779 |
x = mdb_id2l_search( isc->scopes, id ); |
| 780 |
if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) { |
| 781 |
if ( !isc->scopes[x].mval.mv_data ) { |
| 782 |
/* This node is in scope, add parent chain to scope */ |
| 783 |
int i; |
| 784 |
for ( i = 1; i <= isc->sctmp[0].mid; i++ ) { |
| 785 |
rc = mdb_id2l_insert( isc->scopes, &isc->sctmp[i] ); |
| 786 |
if ( rc ) |
| 787 |
break; |
| 788 |
} |
| 789 |
/* check id again since inserts may have changed its position */ |
| 790 |
if ( isc->scopes[x].mid != id ) |
| 791 |
x = mdb_id2l_search( isc->scopes, id ); |
| 792 |
isc->nscope = x; |
| 793 |
return MDB_SUCCESS; |
| 794 |
} |
| 795 |
data = isc->scopes[x].mval; |
| 796 |
rc = 1; |
| 797 |
} |
| 798 |
if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) |
| 799 |
break; |
| 800 |
} |
| 801 |
return MDB_SUCCESS; |
| 802 |
} |
| 803 |
|
| 804 |
/* See if ID is a child of any of the scopes, |
| 805 |
* return MDB_KEYEXIST if so. |
| 806 |
*/ |
| 807 |
int |
| 808 |
mdb_idscopechk( |
| 809 |
Operation *op, |
| 810 |
IdScopes *isc ) |
| 811 |
{ |
| 812 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 813 |
MDB_val key, data; |
| 814 |
ID id, prev; |
| 815 |
char *ptr; |
| 816 |
int rc = 0; |
| 817 |
unsigned int x; |
| 818 |
|
| 819 |
key.mv_size = sizeof(ID); |
| 820 |
|
| 821 |
if ( !isc->mc ) { |
| 822 |
rc = mdb_cursor_open( isc->mt, mdb->mi_dn2id, &isc->mc ); |
| 823 |
if ( rc ) return rc; |
| 824 |
} |
| 825 |
|
| 826 |
id = isc->id; |
| 827 |
|
| 828 |
while (id) { |
| 829 |
if ( !rc ) { |
| 830 |
key.mv_data = &id; |
| 831 |
rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET ); |
| 832 |
if ( rc ) |
| 833 |
return rc; |
| 834 |
} |
| 835 |
|
| 836 |
ptr = data.mv_data; |
| 837 |
ptr += data.mv_size - sizeof(ID); |
| 838 |
prev = id; |
| 839 |
memcpy( &id, ptr, sizeof(ID) ); |
| 840 |
/* If we didn't advance, some parent is missing */ |
| 841 |
if ( id == prev ) |
| 842 |
return MDB_NOTFOUND; |
| 843 |
|
| 844 |
x = mdb_id2l_search( isc->scopes, id ); |
| 845 |
if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) |
| 846 |
return MDB_KEYEXIST; |
| 847 |
} |
| 848 |
return MDB_SUCCESS; |
| 849 |
} |
| 850 |
|
| 851 |
int |
| 852 |
mdb_dn2id_walk( |
| 853 |
Operation *op, |
| 854 |
IdScopes *isc |
| 855 |
) |
| 856 |
{ |
| 857 |
MDB_val key, data; |
| 858 |
diskNode *d; |
| 859 |
char *ptr; |
| 860 |
int rc, n; |
| 861 |
ID nsubs; |
| 862 |
|
| 863 |
if ( !isc->numrdns ) { |
| 864 |
key.mv_data = &isc->id; |
| 865 |
key.mv_size = sizeof(ID); |
| 866 |
rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET ); |
| 867 |
isc->scopes[0].mid = isc->id; |
| 868 |
isc->numrdns++; |
| 869 |
isc->nscope = 0; |
| 870 |
/* skip base if not a subtree walk */ |
| 871 |
if ( isc->oscope == LDAP_SCOPE_SUBTREE || |
| 872 |
isc->oscope == LDAP_SCOPE_BASE ) |
| 873 |
return rc; |
| 874 |
} |
| 875 |
if ( isc->oscope == LDAP_SCOPE_BASE ) |
| 876 |
return MDB_NOTFOUND; |
| 877 |
|
| 878 |
for (;;) { |
| 879 |
/* Get next sibling */ |
| 880 |
rc = mdb_cursor_get( isc->mc, &key, &data, MDB_NEXT_DUP ); |
| 881 |
if ( !rc ) { |
| 882 |
ptr = (char *)data.mv_data + data.mv_size - 2*sizeof(ID); |
| 883 |
d = data.mv_data; |
| 884 |
memcpy( &isc->id, ptr, sizeof(ID)); |
| 885 |
|
| 886 |
/* If we're pushing down, see if there's any children to find */ |
| 887 |
if ( isc->nscope ) { |
| 888 |
ptr += sizeof(ID); |
| 889 |
memcpy( &nsubs, ptr, sizeof(ID)); |
| 890 |
/* No children, go to next sibling */ |
| 891 |
if ( nsubs < 2 ) |
| 892 |
continue; |
| 893 |
} |
| 894 |
n = isc->numrdns; |
| 895 |
isc->scopes[n].mid = isc->id; |
| 896 |
n--; |
| 897 |
isc->nrdns[n].bv_len = ((d->nrdnlen[0] & 0x7f) << 8) | d->nrdnlen[1]; |
| 898 |
isc->nrdns[n].bv_val = d->nrdn; |
| 899 |
isc->rdns[n].bv_val = d->nrdn+isc->nrdns[n].bv_len+1; |
| 900 |
isc->rdns[n].bv_len = data.mv_size - sizeof(diskNode) - isc->nrdns[n].bv_len - sizeof(ID); |
| 901 |
/* return this ID to caller */ |
| 902 |
if ( !isc->nscope ) |
| 903 |
break; |
| 904 |
|
| 905 |
/* push down to child */ |
| 906 |
key.mv_data = &isc->id; |
| 907 |
mdb_cursor_get( isc->mc, &key, &data, MDB_SET ); |
| 908 |
isc->nscope = 0; |
| 909 |
isc->numrdns++; |
| 910 |
continue; |
| 911 |
|
| 912 |
} else if ( rc == MDB_NOTFOUND ) { |
| 913 |
if ( !isc->nscope && isc->oscope != LDAP_SCOPE_ONELEVEL ) { |
| 914 |
/* reset to first dup */ |
| 915 |
mdb_cursor_get( isc->mc, &key, NULL, MDB_GET_CURRENT ); |
| 916 |
mdb_cursor_get( isc->mc, &key, &data, MDB_SET ); |
| 917 |
isc->nscope = 1; |
| 918 |
continue; |
| 919 |
} else { |
| 920 |
isc->numrdns--; |
| 921 |
/* stack is empty? */ |
| 922 |
if ( !isc->numrdns ) |
| 923 |
break; |
| 924 |
/* pop up to prev node */ |
| 925 |
n = isc->numrdns - 1; |
| 926 |
key.mv_data = &isc->scopes[n].mid; |
| 927 |
key.mv_size = sizeof(ID); |
| 928 |
data.mv_data = isc->nrdns[n].bv_val - 2; |
| 929 |
data.mv_size = 1; /* just needs to be non-zero, mdb_dup_compare doesn't care */ |
| 930 |
mdb_cursor_get( isc->mc, &key, &data, MDB_GET_BOTH ); |
| 931 |
continue; |
| 932 |
} |
| 933 |
} else { |
| 934 |
break; |
| 935 |
} |
| 936 |
} |
| 937 |
return rc; |
| 938 |
} |
| 939 |
|
| 940 |
/* restore the nrdn/rdn pointers after a txn reset */ |
| 941 |
void mdb_dn2id_wrestore ( |
| 942 |
Operation *op, |
| 943 |
IdScopes *isc |
| 944 |
) |
| 945 |
{ |
| 946 |
MDB_val key, data; |
| 947 |
diskNode *d; |
| 948 |
int rc, n, nrlen; |
| 949 |
char *ptr; |
| 950 |
|
| 951 |
/* We only need to restore up to the n-1th element, |
| 952 |
* the nth element will be replaced anyway |
| 953 |
*/ |
| 954 |
key.mv_size = sizeof(ID); |
| 955 |
for ( n=0; n<isc->numrdns-1; n++ ) { |
| 956 |
key.mv_data = &isc->scopes[n+1].mid; |
| 957 |
rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET ); |
| 958 |
if ( rc ) |
| 959 |
continue; |
| 960 |
/* we can't use this data directly since its nrlen |
| 961 |
* is missing the high bit setting, so copy it and |
| 962 |
* set it properly. we just copy enough to satisfy |
| 963 |
* mdb_dup_compare. |
| 964 |
*/ |
| 965 |
d = data.mv_data; |
| 966 |
nrlen = ((d->nrdnlen[0] & 0x7f) << 8) | d->nrdnlen[1]; |
| 967 |
ptr = op->o_tmpalloc( nrlen+2, op->o_tmpmemctx ); |
| 968 |
memcpy( ptr, data.mv_data, nrlen+2 ); |
| 969 |
key.mv_data = &isc->scopes[n].mid; |
| 970 |
data.mv_data = ptr; |
| 971 |
data.mv_size = 1; |
| 972 |
*ptr |= 0x80; |
| 973 |
mdb_cursor_get( isc->mc, &key, &data, MDB_GET_BOTH ); |
| 974 |
op->o_tmpfree( ptr, op->o_tmpmemctx ); |
| 975 |
|
| 976 |
/* now we're back to where we wanted to be */ |
| 977 |
d = data.mv_data; |
| 978 |
isc->nrdns[n].bv_val = d->nrdn; |
| 979 |
isc->rdns[n].bv_val = d->nrdn+isc->nrdns[n].bv_len+1; |
| 980 |
} |
| 981 |
} |