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 * Also each child node contains a count of the number of entries in
40 * its subtree, appended after its entryID.
42 * The diskNode is a variable length structure. This definition is not
43 * directly usable for in-memory manipulation.
45 typedef struct diskNode {
46 unsigned char nrdnlen[2];
48 char rdn[1]; /* variable placement */
49 unsigned char entryID[sizeof(ID)]; /* variable placement */
50 /* unsigned char nsubs[sizeof(ID)]; in child nodes only */
53 /* Sort function for the sorted duplicate data items of a dn2id key.
54 * Sorts based on normalized RDN, in length order.
58 const MDB_val *usrkey,
65 un = (diskNode *)usrkey->mv_data;
66 cn = (diskNode *)curkey->mv_data;
68 /* data is not aligned, cannot compare directly */
69 rc = un->nrdnlen[0] - cn->nrdnlen[0];
71 rc = un->nrdnlen[1] - cn->nrdnlen[1];
74 nrlen = ((un->nrdnlen[0] & 0x7f) << 8) | un->nrdnlen[1];
75 return strncmp( un->nrdn, cn->nrdn, nrlen );
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.
92 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
99 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_add 0x%lx: \"%s\"\n",
100 e->e_id, e->e_ndn ? e->e_ndn : "", 0 );
102 nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
104 rlen = dn_rdnlen( op->o_bd, &e->e_name );
106 nrlen = e->e_nname.bv_len;
107 rlen = e->e_name.bv_len;
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 );
115 ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
117 memcpy( ptr, &e->e_id, sizeof( ID ));
119 memcpy( ptr, &nsubs, sizeof( ID ));
121 key.mv_size = sizeof(ID);
126 /* Need to make dummy root node once. Subsequent attempts
127 * will fail harmlessly.
130 diskNode dummy = {{0, 0}, "", "", ""};
131 data.mv_data = &dummy;
132 data.mv_size = sizeof(diskNode);
134 mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
138 data.mv_size = sizeof(diskNode) + rlen + nrlen + sizeof( ID );
140 /* Add our child node under parent's key */
141 rc = mdb_cursor_put( mcp, &key, &data, MDB_NODUPDATA );
143 /* Add our own node */
145 int flag = MDB_NODUPDATA;
147 /* drop subtree count */
148 data.mv_size -= sizeof( ID );
150 memcpy( ptr, &pid, sizeof( ID ));
151 d->nrdnlen[0] ^= 0x80;
153 if ((slapMode & SLAP_TOOL_MODE) || (e->e_id == mdb->mi_nextid))
155 rc = mdb_cursor_put( mcd, &key, &data, flag );
157 op->o_tmpfree( d, op->o_tmpmemctx );
159 /* Add our subtree count to all superiors */
160 if ( rc == 0 && upsub && pid ) {
164 /* Get parent's RDN */
165 rc = mdb_cursor_get( mcp, &key, &data, MDB_SET );
168 ptr = (char *)data.mv_data + data.mv_size - sizeof( ID );
169 memcpy( &nid, ptr, sizeof( ID ));
170 /* Get parent's node under grandparent */
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 );
177 rc = mdb_cursor_get( mcp, &key, &data, MDB_GET_BOTH );
178 op->o_tmpfree( p2, op->o_tmpmemctx );
180 /* Get parent's subtree count */
181 ptr = (char *)data.mv_data + data.mv_size - sizeof( ID );
182 memcpy( &subs, ptr, sizeof( ID ));
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 ));
188 rc = mdb_cursor_put( mcp, &key, &data, MDB_CURRENT );
189 op->o_tmpfree( p2, op->o_tmpmemctx );
197 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_add 0x%lx: %d\n", e->e_id, rc, 0 );
202 /* mc must have been set by mdb_dn2id */
214 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id_delete 0x%lx\n",
217 /* Delete our ID from the parent's list */
218 rc = mdb_cursor_del( mc, 0 );
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.
227 mdb_cursor_get( mc, &key, NULL, MDB_GET_CURRENT );
228 memcpy( &nid, key.mv_data, sizeof( ID ));
230 key.mv_size = sizeof(ID);
232 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
234 rc = mdb_cursor_del( mc, 0 );
237 /* Delete our subtree count from all superiors */
238 if ( rc == 0 && nsubs && nid ) {
242 key.mv_size = sizeof( ID );
244 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
249 ptr = (char *)data.mv_data + data.mv_size - sizeof( ID );
250 memcpy( &nid, ptr, sizeof( ID ));
251 /* Get parent's node under grandparent */
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 );
258 rc = mdb_cursor_get( mc, &key, &data, MDB_GET_BOTH );
259 op->o_tmpfree( p2, op->o_tmpmemctx );
261 /* Get parent's subtree count */
262 ptr = (char *)data.mv_data + data.mv_size - sizeof( ID );
263 memcpy( &subs, ptr, sizeof( ID ));
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 ));
269 rc = mdb_cursor_put( mc, &key, &data, MDB_CURRENT );
270 op->o_tmpfree( p2, op->o_tmpmemctx );
279 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id_delete 0x%lx: %d\n", id, rc, 0 );
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.
296 struct berval *matched,
297 struct berval *nmatched )
299 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
301 MDB_dbi dbi = mdb->mi_dn2id;
306 char dn[SLAP_LDAPDN_MAXLEN];
310 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2id(\"%s\")\n", in->bv_val ? in->bv_val : "", 0, 0 );
313 matched->bv_val = dn + sizeof(dn) - 1;
315 *matched->bv_val-- = '\0';
318 nmatched->bv_len = 0;
319 nmatched->bv_val = 0;
330 if ( op->o_bd->be_nsuffix[0].bv_len ) {
331 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
333 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
335 for ( ptr = tmp.bv_val + tmp.bv_len - 1; ptr >= tmp.bv_val; ptr-- )
336 if (DN_SEPARATOR(*ptr))
339 tmp.bv_len -= ptr - tmp.bv_val;
343 key.mv_size = sizeof(ID);
348 rc = mdb_cursor_open( txn, dbi, &cursor );
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 );
363 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
364 op->o_tmpfree( d, op->o_tmpmemctx );
367 ptr = (char *) data.mv_data + data.mv_size - 2*sizeof(ID);
368 memcpy( &nid, ptr, sizeof(ID));
370 /* grab the non-normalized RDN */
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 );
384 nmatched->bv_val = tmp.bv_val;
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;
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 ));
406 mdb_cursor_close( cursor );
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;
414 if ( BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) && !nid ) {
415 ber_dupbv( matched, (struct berval *)&slap_empty_bv );
417 matched->bv_val = NULL;
422 if ( nmatched->bv_val ) {
423 nmatched->bv_len = in->bv_len - (nmatched->bv_val - in->bv_val);
425 *nmatched = slap_empty_bv;
430 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: get failed: %s (%d)\n",
431 mdb_strerror( rc ), rc, 0 );
433 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2id: got id=0x%lx\n",
440 /* return IDs from root to parent of DN */
448 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
450 MDB_dbi dbi = mdb->mi_dn2id;
458 Debug( LDAP_DEBUG_TRACE, "=> mdb_dn2sups(\"%s\")\n", in->bv_val, 0, 0 );
466 nrlen = tmp.bv_len - op->o_bd->be_nsuffix[0].bv_len;
468 tmp.bv_len = op->o_bd->be_nsuffix[0].bv_len;
470 key.mv_size = sizeof(ID);
472 rc = mdb_cursor_open( txn, dbi, &cursor );
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 );
486 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
487 op->o_tmpfree( d, op->o_tmpmemctx );
489 mdb_cursor_close( cursor );
492 ptr = (char *) data.mv_data + data.mv_size - 2*sizeof(ID);
493 memcpy( &nid, ptr, sizeof(ID));
496 mdb_idl_insert( ids, pid );
498 if ( tmp.bv_val > in->bv_val ) {
499 for (ptr = tmp.bv_val - 2; ptr > in->bv_val &&
500 !DN_SEPARATOR(*ptr); ptr--) /* empty */;
501 if ( ptr >= in->bv_val ) {
502 if (DN_SEPARATOR(*ptr)) ptr++;
503 tmp.bv_len = tmp.bv_val - ptr - 1;
513 Debug( LDAP_DEBUG_TRACE, "<= mdb_dn2sups: get failed: %s (%d)\n",
514 mdb_strerror( rc ), rc, 0 );
526 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
527 MDB_dbi dbi = mdb->mi_dn2id;
533 key.mv_size = sizeof(ID);
537 rc = mdb_cursor_open( txn, dbi, &cursor );
540 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
543 rc = mdb_cursor_count( cursor, &dkids );
545 if ( dkids < 2 ) rc = MDB_NOTFOUND;
548 mdb_cursor_close( cursor );
559 struct berval *nname )
561 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
562 MDB_dbi dbi = mdb->mi_dn2id;
566 char dn[SLAP_LDAPDN_MAXLEN], ndn[SLAP_LDAPDN_MAXLEN], *ptr;
570 key.mv_size = sizeof(ID);
573 rc = mdb_cursor_open( txn, dbi, cursp );
583 unsigned int nrlen, rlen;
587 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
590 ptr += data.mv_size - sizeof(ID);
591 memcpy( &id, ptr, sizeof(ID) );
593 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
594 rlen = data.mv_size - sizeof(diskNode) - nrlen;
595 assert( nrlen < 1024 && rlen < 1024 ); /* FIXME: Sanity check */
600 /* copy name and trailing NUL */
601 memcpy( nptr, d->nrdn, nrlen+1 );
602 memcpy( dptr, d->nrdn+nrlen+1, rlen+1 );
607 name->bv_len = dptr - dn;
608 nname->bv_len = nptr - ndn;
609 name->bv_val = op->o_tmpalloc( name->bv_len + 1, op->o_tmpmemctx );
610 nname->bv_val = op->o_tmpalloc( nname->bv_len + 1, op->o_tmpmemctx );
611 memcpy( name->bv_val, dn, name->bv_len );
612 name->bv_val[name->bv_len] = '\0';
613 memcpy( nname->bv_val, ndn, nname->bv_len );
614 nname->bv_val[nname->bv_len] = '\0';
619 /* Find each id in ids that is a child of base and move it to res.
629 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
630 MDB_dbi dbi = mdb->mi_dn2id;
633 ID ida, id, cid = 0, ci0 = 0, idc = 0;
637 key.mv_size = sizeof(ID);
641 rc = mdb_cursor_open( txn, dbi, &cursor );
644 ida = mdb_idl_first( ids, &cid );
646 /* Don't bother moving out of ids if it's a range */
647 if (!MDB_IDL_IS_RANGE(ids)) {
652 while (ida != NOID) {
657 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
659 /* not found, drop this from ids */
664 ptr += data.mv_size - sizeof(ID);
665 memcpy( &id, ptr, sizeof(ID) );
672 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
683 ida = mdb_idl_next( ids, &cid );
685 if (!MDB_IDL_IS_RANGE( ids ))
688 mdb_cursor_close( cursor );
692 /* See if base is a child of any of the scopes
699 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
700 MDB_dbi dbi = mdb->mi_dn2id;
707 unsigned int nrlen, rlen;
710 key.mv_size = sizeof(ID);
713 rc = mdb_cursor_open( isc->mt, dbi, &isc->mc );
719 /* Catch entries from deref'd aliases */
720 x = mdb_id2l_search( isc->scopes, id );
721 if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) {
729 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
736 nrlen = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
737 rlen = data.mv_size - sizeof(diskNode) - nrlen;
738 isc->nrdns[isc->numrdns].bv_len = nrlen;
739 isc->nrdns[isc->numrdns].bv_val = d->nrdn;
740 isc->rdns[isc->numrdns].bv_len = rlen;
741 isc->rdns[isc->numrdns].bv_val = d->nrdn+nrlen+1;
744 if (!rc && id != isc->id) {
747 mdb_id2l_insert( isc->scopes, &id2 );
750 ptr += data.mv_size - sizeof(ID);
751 memcpy( &id, ptr, sizeof(ID) );
752 x = mdb_id2l_search( isc->scopes, id );
753 if ( x <= isc->scopes[0].mid && isc->scopes[x].mid == id ) {
754 if ( !isc->scopes[x].mval.mv_data ) {
758 data = isc->scopes[x].mval;
761 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
779 if ( !isc->numrdns ) {
780 key.mv_data = &isc->id;
781 key.mv_size = sizeof(ID);
782 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
783 isc->scopes[0].mid = isc->id;
786 /* skip base if not a subtree walk */
787 if ( isc->oscope == LDAP_SCOPE_SUBTREE ||
788 isc->oscope == LDAP_SCOPE_BASE )
791 if ( isc->oscope == LDAP_SCOPE_BASE )
795 /* Get next sibling */
796 rc = mdb_cursor_get( isc->mc, &key, &data, MDB_NEXT_DUP );
798 ptr = (char *)data.mv_data + data.mv_size - 2*sizeof(ID);
800 memcpy( &isc->id, ptr, sizeof(ID));
802 /* If we're pushing down, see if there's any children to find */
805 memcpy( &nsubs, ptr, sizeof(ID));
806 /* No children, go to next sibling */
811 isc->scopes[n].mid = isc->id;
813 isc->nrdns[n].bv_len = ((d->nrdnlen[0] & 0x7f) << 8) | d->nrdnlen[1];
814 isc->nrdns[n].bv_val = d->nrdn;
815 isc->rdns[n].bv_val = d->nrdn+isc->nrdns[n].bv_len+1;
816 isc->rdns[n].bv_len = data.mv_size - sizeof(diskNode) - isc->nrdns[n].bv_len - sizeof(ID);
817 /* return this ID to caller */
821 /* push down to child */
822 key.mv_data = &isc->id;
823 mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
828 } else if ( rc == MDB_NOTFOUND ) {
829 if ( !isc->nscope && isc->oscope != LDAP_SCOPE_ONELEVEL ) {
830 /* reset to first dup */
831 mdb_cursor_get( isc->mc, &key, NULL, MDB_GET_CURRENT );
832 mdb_cursor_get( isc->mc, &key, &data, MDB_SET );
837 /* stack is empty? */
840 /* pop up to prev node */
841 n = isc->numrdns - 1;
842 key.mv_data = &isc->scopes[n].mid;
843 key.mv_size = sizeof(ID);
844 data.mv_data = isc->nrdns[n].bv_val - 2;
845 data.mv_size = 1; /* just needs to be non-zero, mdb_dup_compare doesn't care */
846 mdb_cursor_get( isc->mc, &key, &data, MDB_GET_BOTH );