1 /* dn2id.c - routines to deal with the dn2id index */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2013 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>.
20 #include <ac/string.h>
26 /* Management routines for a hierarchically structured database.
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 accomodate
36 * RDNs up to length 32767, but that's fine since full DNs are already
39 * The diskNode is a variable length structure. This definition is not
40 * directly usable for in-memory manipulation.
42 typedef struct diskNode {
43 unsigned char nrdnlen[2];
45 char rdn[1]; /* variable placement */
46 unsigned char entryID[sizeof(ID)]; /* variable placement */
49 /* Sort function for the sorted duplicate data items of a dn2id key.
50 * Sorts based on normalized RDN, in length order.
54 const MDB_val *usrkey,
61 un = (diskNode *)usrkey->mv_data;
62 cn = (diskNode *)curkey->mv_data;
64 /* data is not aligned, cannot compare directly */
65 rc = un->nrdnlen[0] - cn->nrdnlen[0];
67 rc = un->nrdnlen[1] - cn->nrdnlen[1];
70 nrlen = (un->nrdnlen[0] << 8) | un->nrdnlen[1];
71 return strncmp( un->nrdn, cn->nrdn, nrlen );
74 /* We add two elements to the DN2ID database - a data item under the parent's
75 * entryID containing the child's RDN and entryID, and an item under the
76 * child's entryID containing the parent's entryID.
86 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
93 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n",
94 e->e_id, e->e_ndn ? e->e_ndn : "", 0 );
96 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
98 rlen = dn_rdnlen( op->o_bd, &e->e_name );
100 nrlen = e->e_nname.bv_len;
101 rlen = e->e_name.bv_len;
104 d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
105 d->nrdnlen[1] = nrlen & 0xff;
106 d->nrdnlen[0] = (nrlen >> 8) | 0x80;
107 ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
109 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
111 memcpy( ptr, &e->e_id, sizeof( ID ));
113 key.mv_size = sizeof(ID);
118 /* Need to make dummy root node once. Subsequent attempts
119 * will fail harmlessly.
122 diskNode dummy = {{0, 0}, "", "", ""};
123 data.mv_data = &dummy;
124 data.mv_size = sizeof(diskNode);
126 mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
130 data.mv_size = sizeof(diskNode) + rlen + nrlen;
132 rc = mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
135 int flag = MDB_NODUPDATA;
137 memcpy( ptr, &pid, sizeof( ID ));
138 d->nrdnlen[0] ^= 0x80;
140 if ((slapMode & SLAP_TOOL_MODE) || (e->e_id == mdb->mi_nextid))
142 rc = mdb_cursor_put( mcd, &key, &data, flag );
146 op->o_tmpfree( d, op->o_tmpmemctx );
147 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
152 /* mc must have been set by mdb_dn2id */
161 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx\n",
164 /* Delete our ID from the parent's list */
165 rc = mdb_cursor_del( mc, 0 );
167 /* Delete our ID from the tree. With sorted duplicates, this
168 * will leave any child nodes still hanging around. This is OK
169 * for modrdn, which will add our info back in later.
173 key.mv_size = sizeof(ID);
175 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
177 rc = mdb_cursor_del( mc, 0 );
180 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", id, rc, 0 );
184 /* return last found ID in *id if no match
185 * If mc is provided, it will be left pointing to the RDN's
186 * record under the parent's ID.
195 struct berval *matched,
196 struct berval *nmatched )
198 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
200 MDB_dbi dbi = mdb->mi_dn2id;
205 char dn[SLAP_LDAPDN_MAXLEN];
209 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val ? in->bv_val : "", 0, 0 );
212 matched->bv_val = dn + sizeof(dn) - 1;
214 *matched->bv_val-- = '\0';
217 nmatched->bv_len = 0;
218 nmatched->bv_val = 0;
229 if ( op->o_bd->be_nsuffix[0].bv_len ) {
230 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
232 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
234 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
235 if (DN_SEPARATOR(*ptr))
238 tmp.bv_len -= ptr - tmp.bv_val;
242 key.mv_size = sizeof(ID);
247 rc = mdb_cursor_open( txn, dbi, &cursor );
255 data.mv_size = sizeof(diskNode) + tmp.bv_len;
256 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
257 d->nrdnlen[1] = tmp.bv_len & 0xff;
258 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
259 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
262 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
263 op->o_tmpfree( d, op->o_tmpmemctx );
266 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
267 memcpy( &nid, ptr, sizeof(ID));
269 /* grab the non-normalized RDN */
273 rlen = data.mv_size - sizeof(diskNode) - tmp.bv_len;
274 matched->bv_len += rlen;
275 matched->bv_val -= rlen + 1;
276 ptr = lutil_strcopy( matched->bv_val, d->rdn + tmp.bv_len );
283 nmatched->bv_val = tmp.bv_val;
286 if ( tmp.bv_val > in->bv_val ) {
287 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
288 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
289 if ( ptr >= in->bv_val ) {
290 if (DN_SEPARATOR(*ptr)) ptr++;
291 tmp.bv_len = tmp.bv_val - ptr - 1;
300 mdb_cursor_close( cursor );
303 if ( matched->bv_len ) {
304 ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
305 strcpy( ptr, matched->bv_val );
306 matched->bv_val = ptr;
308 if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
309 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
311 matched->bv_val = NULL;
316 if ( nmatched->bv_val ) {
317 nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
319 *nmatched = slap_empty_bv;
324 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
325 mdb_strerror( rc ), rc, 0 );
327 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
334 /* return IDs from root to parent of DN */
342 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
344 MDB_dbi dbi = mdb->mi_dn2id;
352 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
360 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
362 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
364 key.mv_size = sizeof(ID);
366 rc = mdb_cursor_open( txn, dbi, &cursor );
373 data.mv_size = sizeof(diskNode) + tmp.bv_len;
374 d = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
375 d->nrdnlen[1] = tmp.bv_len & 0xff;
376 d->nrdnlen[0] = (tmp.bv_len >> 8) | 0x80;
377 ptr = lutil_strncopy( d->nrdn, tmp.bv_val, tmp.bv_len );
380 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
381 op->o_tmpfree( d, op->o_tmpmemctx );
383 mdb_cursor_close( cursor );
386 ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
387 memcpy( &nid, ptr, sizeof(ID));
390 mdb_idl_insert( ids, pid );
392 if ( tmp.bv_val > in->bv_val ) {
393 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
394 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
395 if ( ptr >= in->bv_val ) {
396 if (DN_SEPARATOR(*ptr)) ptr++;
397 tmp.bv_len = tmp.bv_val - ptr - 1;
407 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
408 mdb_strerror( rc ), rc, 0 );
420 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
421 MDB_dbi dbi = mdb->mi_dn2id;
427 key.mv_size = sizeof(ID);
431 rc = mdb_cursor_open( txn, dbi, &cursor );
434 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
437 rc = mdb_cursor_count( cursor, &dkids );
439 if ( dkids < 2 ) rc = MDB_NOTFOUND;
442 mdb_cursor_close( cursor );
453 struct berval *nname )
455 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
456 MDB_dbi dbi = mdb->mi_dn2id;
460 char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
464 key.mv_size = sizeof(ID);
467 rc = mdb_cursor_open( txn, dbi, cursp );
477 unsigned int nrlen, rlen;
481 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
484 ptr += data.mv_size - sizeof(ID);
485 memcpy( &id, ptr, sizeof(ID) );
487 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
488 rlen = data.mv_size - sizeof(diskNode) - nrlen;
489 assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */
494 /* copy name and trailing NUL */
495 memcpy( nptr, d->nrdn, nrlen+1 );
496 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
501 name->bv_len = dptr - dn;
502 nname->bv_len = nptr - ndn;
503 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
504 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
505 memcpy( name->bv_val, dn, name->bv_len );
506 name->bv_val[name->bv_len] = '\0';
507 memcpy( nname->bv_val, ndn, nname->bv_len );
508 nname->bv_val[nname->bv_len] = '\0';
513 /* Find each id in ids that is a child of base and move it to res.
523 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
524 MDB_dbi dbi = mdb->mi_dn2id;
527 ID ida, id, cid, ci0, idc = 0;
531 key.mv_size = sizeof(ID);
535 rc = mdb_cursor_open( txn, dbi, &cursor );
538 ida = mdb_idl_first( ids, &cid );
540 /* Don't bother moving out of ids if it's a range */
541 if (!MDB_IDL_IS_RANGE(ids)) {
546 while (ida != NOID) {
550 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
552 /* not found, move on to next */
561 ptr += data.mv_size - sizeof(ID);
562 memcpy( &id, ptr, sizeof(ID) );
576 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
579 ida = mdb_idl_next( ids, &cid );
581 if (!MDB_IDL_IS_RANGE( ids ))
584 mdb_cursor_close( cursor );
588 /* See if base is a child of any of the scopes
595 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
596 MDB_dbi dbi = mdb->mi_dn2id;
603 unsigned int nrlen, rlen;
606 key.mv_size = sizeof(ID);
609 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
617 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
624 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
625 rlen = data.mv_size - sizeof(diskNode) - nrlen;
626 isc->nrdns[isc->numrdns].bv_len = nrlen;
627 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
628 isc->rdns[isc->numrdns].bv_len = rlen;
629 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
632 if (!rc && id != isc->id) {
635 mdb_id2l_insert( isc->scopes, &id2 );
638 ptr += data.mv_size - sizeof(ID);
639 memcpy( &id, ptr, sizeof(ID) );
640 x = mdb_id2l_search( isc->scopes, id );
641 if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) {
642 if ( !isc->scopes[x].mval.mv_data ) {
646 data = isc->scopes[x].mval;
649 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )