]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
Fix ITS#3424
[openldap] / servers / slapd / back-bdb / dn2id.c
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-2004 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-bdb.h"
23 #include "idl.h"
24 #include "lutil.h"
25
26 #ifndef BDB_HIER
27 int
28 bdb_dn2id_add(
29         Operation *op,
30         DB_TXN *txn,
31         EntryInfo *eip,
32         Entry           *e )
33 {
34         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
35         DB *db = bdb->bi_dn2id->bdi_db;
36         int             rc;
37         DBT             key, data;
38         ID              nid;
39         char            *buf;
40         struct berval   ptr, pdn;
41
42         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
43                 e->e_ndn, (long) e->e_id, 0 );
44         assert( e->e_id != NOID );
45
46         DBTzero( &key );
47         key.size = e->e_nname.bv_len + 2;
48         key.ulen = key.size;
49         key.flags = DB_DBT_USERMEM;
50         buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
51         key.data = buf;
52         buf[0] = DN_BASE_PREFIX;
53         ptr.bv_val = buf + 1;
54         ptr.bv_len = e->e_nname.bv_len;
55         AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
56         ptr.bv_val[ptr.bv_len] = '\0';
57
58         DBTzero( &data );
59         data.data = &nid;
60         data.size = sizeof( nid );
61         BDB_ID2DISK( e->e_id, &nid );
62
63         /* store it -- don't override */
64         rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
65         if( rc != 0 ) {
66                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
67                         db_strerror(rc), rc, 0 );
68                 goto done;
69         }
70
71 #ifndef BDB_MULTIPLE_SUFFIXES
72         if( !be_issuffix( op->o_bd, &ptr ))
73 #endif
74         {
75                 buf[0] = DN_SUBTREE_PREFIX;
76                 rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
77                 if( rc != 0 ) {
78                         Debug( LDAP_DEBUG_ANY,
79                         "=> bdb_dn2id_add: subtree (%s) put failed: %d\n",
80                         ptr.bv_val, rc, 0 );
81                         goto done;
82                 }
83                 
84 #ifdef BDB_MULTIPLE_SUFFIXES
85         if( !be_issuffix( op->o_bd, &ptr ))
86 #endif
87         {
88                 dnParent( &ptr, &pdn );
89         
90                 key.size = pdn.bv_len + 2;
91                 key.ulen = key.size;
92                 pdn.bv_val[-1] = DN_ONE_PREFIX;
93                 key.data = pdn.bv_val-1;
94                 ptr = pdn;
95
96                 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
97
98                 if( rc != 0 ) {
99                         Debug( LDAP_DEBUG_ANY,
100                                 "=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
101                                         ptr.bv_val, rc, 0 );
102                         goto done;
103                 }
104         }
105
106 #ifndef BDB_MULTIPLE_SUFFIXES
107         while( !be_issuffix( op->o_bd, &ptr ))
108 #else
109         for (;;)
110 #endif
111         {
112                 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
113
114                 rc = bdb_idl_insert_key( op->o_bd, db, txn, &key, e->e_id );
115
116                 if( rc != 0 ) {
117                         Debug( LDAP_DEBUG_ANY,
118                                 "=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
119                                         ptr.bv_val, rc, 0 );
120                         break;
121                 }
122 #ifdef BDB_MULTIPLE_SUFFIXES
123                 if( be_issuffix( op->o_bd, &ptr )) break;
124 #endif
125                 dnParent( &ptr, &pdn );
126
127                 key.size = pdn.bv_len + 2;
128                 key.ulen = key.size;
129                 key.data = pdn.bv_val - 1;
130                 ptr = pdn;
131         }
132         }
133
134 done:
135         op->o_tmpfree( buf, op->o_tmpmemctx );
136         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
137         return rc;
138 }
139
140 int
141 bdb_dn2id_delete(
142         Operation *op,
143         DB_TXN *txn,
144         EntryInfo       *eip,
145         Entry           *e )
146 {
147         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
148         DB *db = bdb->bi_dn2id->bdi_db;
149         int             rc;
150         DBT             key;
151         char            *buf;
152         struct berval   pdn, ptr;
153
154         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
155                 e->e_ndn, e->e_id, 0 );
156
157         DBTzero( &key );
158         key.size = e->e_nname.bv_len + 2;
159         buf = op->o_tmpalloc( key.size, op->o_tmpmemctx );
160         key.data = buf;
161         key.flags = DB_DBT_USERMEM;
162         buf[0] = DN_BASE_PREFIX;
163         ptr.bv_val = buf+1;
164         ptr.bv_len = e->e_nname.bv_len;
165         AC_MEMCPY( ptr.bv_val, e->e_nname.bv_val, e->e_nname.bv_len );
166         ptr.bv_val[ptr.bv_len] = '\0';
167
168         /* delete it */
169         rc = db->del( db, txn, &key, 0 );
170         if( rc != 0 ) {
171                 Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
172                         db_strerror(rc), rc, 0 );
173                 goto done;
174         }
175
176 #ifndef BDB_MULTIPLE_SUFFIXES
177         if( !be_issuffix( op->o_bd, &ptr ))
178 #endif
179         {
180                 buf[0] = DN_SUBTREE_PREFIX;
181                 rc = db->del( db, txn, &key, 0 );
182                 if( rc != 0 ) {
183                         Debug( LDAP_DEBUG_ANY,
184                         "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
185                         ptr.bv_val, rc, 0 );
186                         goto done;
187                 }
188
189 #ifdef BDB_MULTIPLE_SUFFIXES
190         if( !be_issuffix( op->o_bd, &ptr ))
191 #endif
192         {
193                 dnParent( &ptr, &pdn );
194
195                 key.size = pdn.bv_len + 2;
196                 key.ulen = key.size;
197                 pdn.bv_val[-1] = DN_ONE_PREFIX;
198                 key.data = pdn.bv_val - 1;
199                 ptr = pdn;
200
201                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
202
203                 if( rc != 0 ) {
204                         Debug( LDAP_DEBUG_ANY,
205                                 "=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
206                                 ptr.bv_val, rc, 0 );
207                         goto done;
208                 }
209         }
210
211 #ifndef BDB_MULTIPLE_SUFFIXES
212         while( !be_issuffix( op->o_bd, &ptr ))
213 #else
214         for (;;)
215 #endif
216         {
217                 ptr.bv_val[-1] = DN_SUBTREE_PREFIX;
218
219                 rc = bdb_idl_delete_key( op->o_bd, db, txn, &key, e->e_id );
220                 if( rc != 0 ) {
221                         Debug( LDAP_DEBUG_ANY,
222                                 "=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
223                                 ptr.bv_val, rc, 0 );
224                         goto done;
225                 }
226 #ifdef BDB_MULTIPLE_SUFFIXES
227                 if( be_issuffix( op->o_bd, &ptr )) break;
228 #endif
229                 dnParent( &ptr, &pdn );
230
231                 key.size = pdn.bv_len + 2;
232                 key.ulen = key.size;
233                 key.data = pdn.bv_val - 1;
234                 ptr = pdn;
235         }
236         }
237
238 done:
239         op->o_tmpfree( buf, op->o_tmpmemctx );
240         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
241         return rc;
242 }
243
244 int
245 bdb_dn2id(
246         Operation *op,
247         DB_TXN *txn,
248         struct berval   *dn,
249         EntryInfo *ei )
250 {
251         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
252         DB *db = bdb->bi_dn2id->bdi_db;
253         int             rc;
254         DBT             key, data;
255         ID              nid;
256
257         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id(\"%s\")\n", dn->bv_val, 0, 0 );
258         DBTzero( &key );
259         key.size = dn->bv_len + 2;
260         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
261         ((char *)key.data)[0] = DN_BASE_PREFIX;
262         AC_MEMCPY( &((char *)key.data)[1], dn->bv_val, key.size - 1 );
263
264         /* store the ID */
265         DBTzero( &data );
266         data.data = &nid;
267         data.ulen = sizeof(ID);
268         data.flags = DB_DBT_USERMEM;
269
270         /* fetch it */
271         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
272
273         if( rc != 0 ) {
274                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: get failed: %s (%d)\n",
275                         db_strerror( rc ), rc, 0 );
276         } else {
277                 BDB_DISK2ID( &nid, &ei->bei_id );
278                 Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: got id=0x%08lx\n",
279                         ei->bei_id, 0, 0 );
280         }
281
282         op->o_tmpfree( key.data, op->o_tmpmemctx );
283         return rc;
284 }
285
286 int
287 bdb_dn2id_children(
288         Operation *op,
289         DB_TXN *txn,
290         Entry *e )
291 {
292         DBT             key, data;
293         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
294         DB *db = bdb->bi_dn2id->bdi_db;
295         ID              id;
296         int             rc;
297
298         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children(\"%s\")\n",
299                 e->e_nname.bv_val, 0, 0 );
300         DBTzero( &key );
301         key.size = e->e_nname.bv_len + 2;
302         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
303         ((char *)key.data)[0] = DN_ONE_PREFIX;
304         AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
305
306         if ( bdb->bi_idl_cache_size ) {
307                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
308                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
309                         op->o_tmpfree( key.data, op->o_tmpmemctx );
310                         return rc;
311                 }
312         }
313         /* we actually could do a empty get... */
314         DBTzero( &data );
315         data.data = &id;
316         data.ulen = sizeof(id);
317         data.flags = DB_DBT_USERMEM;
318         data.doff = 0;
319         data.dlen = sizeof(id);
320
321         rc = db->get( db, txn, &key, &data, bdb->bi_db_opflags );
322         op->o_tmpfree( key.data, op->o_tmpmemctx );
323
324         Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children(\"%s\"): %s (%d)\n",
325                 e->e_nname.bv_val,
326                 rc == 0 ? "" : ( rc == DB_NOTFOUND ? "no " :
327                         db_strerror(rc) ), rc );
328
329         return rc;
330 }
331
332 int
333 bdb_dn2idl(
334         Operation *op,
335         Entry *e,
336         ID *ids,
337         ID *stack )
338 {
339         int             rc;
340         DBT             key;
341         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
342         DB *db = bdb->bi_dn2id->bdi_db;
343         int prefix = ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
344                 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
345
346         Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2idl(\"%s\")\n",
347                 e->e_nname.bv_val, 0, 0 );
348
349 #ifndef BDB_MULTIPLE_SUFFIXES
350         if ( prefix == DN_SUBTREE_PREFIX && BEI(e)->bei_parent->bei_id == 0 ) {
351                 BDB_IDL_ALL(bdb, ids);
352                 return 0;
353         }
354 #endif
355
356         DBTzero( &key );
357         key.size = e->e_nname.bv_len + 2;
358         key.ulen = key.size;
359         key.flags = DB_DBT_USERMEM;
360         key.data = op->o_tmpalloc( key.size, op->o_tmpmemctx );
361         ((char *)key.data)[0] = prefix;
362         AC_MEMCPY( &((char *)key.data)[1], e->e_nname.bv_val, key.size - 1 );
363
364         rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids, NULL, 0 );
365
366         if( rc != 0 ) {
367                 Debug( LDAP_DEBUG_TRACE,
368                         "<= bdb_dn2idl: get failed: %s (%d)\n",
369                         db_strerror( rc ), rc, 0 );
370
371         } else {
372                 Debug( LDAP_DEBUG_TRACE,
373                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
374                         (long) ids[0],
375                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
376         }
377
378         op->o_tmpfree( key.data, op->o_tmpmemctx );
379         return rc;
380 }
381
382 #else   /* BDB_HIER */
383 /* Experimental management routines for a hierarchically structured database.
384  *
385  * Unsupported! Use at your own risk!
386  * -- Howard Chu, Symas Corp. 2003.
387  *
388  * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
389  * entry in this database is a struct diskNode, keyed by entryID and with
390  * the data containing the RDN and entryID of the node's children. We use
391  * a B-Tree with sorted duplicates to store all the children of a node under
392  * the same key. Also, the first item under the key contains the entry's own
393  * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
394  * well as top-down. To keep this info first in the list, the nrdnlen is set
395  * to the negative of its value.
396  *
397  * The diskNode is a variable length structure. This definition is not
398  * directly usable for in-memory manipulation.
399  */
400 typedef struct diskNode {
401         ID entryID;
402         short nrdnlen;
403         char nrdn[1];
404         char rdn[1];
405 } diskNode;
406
407 /* Sort function for the sorted duplicate data items of a dn2id key.
408  * Sorts based on normalized RDN, in length order.
409  */
410 int
411 hdb_dup_compare(
412         DB *db, 
413         const DBT *usrkey,
414         const DBT *curkey )
415 {
416         signed char *u = (signed char *)&(((diskNode *)(usrkey->data))->nrdnlen);
417         signed char *c = (signed char *)&(((diskNode *)(curkey->data))->nrdnlen);
418         int rc, i;
419
420         /* data is not aligned, cannot compare directly */
421 #ifdef WORDS_BIGENDIAN
422         for( i = 0; i < (int)sizeof(short); i++)
423 #else
424         for( i = sizeof(short)-1; i >= 0; i--)
425 #endif
426         {
427                 rc = u[i] - c[i];
428                 if( rc ) return rc;
429         }
430         return strcmp( u+sizeof(short), c+sizeof(short) );
431 }
432
433 /* This function constructs a full DN for a given entry.
434  */
435 int hdb_fix_dn(
436         Entry *e,
437         int checkit )
438 {
439         EntryInfo *ei;
440         int rlen = 0, nrlen = 0;
441         char *ptr, *nptr;
442         int max = 0;
443
444         /* count length of all DN components */
445         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
446                 rlen += ei->bei_rdn.bv_len + 1;
447                 nrlen += ei->bei_nrdn.bv_len + 1;
448                 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
449         }
450
451         /* See if the entry DN was invalidated by a subtree rename */
452         if ( checkit ) {
453                 if ( BEI(e)->bei_modrdns >= max ) {
454                         return 0;
455                 }
456                 /* We found a mismatch, tell the caller to lock it */
457                 if ( checkit == 1 ) {
458                         return 1;
459                 }
460                 /* checkit == 2. do the fix. */
461                 free( e->e_name.bv_val );
462                 free( e->e_nname.bv_val );
463         }
464
465         e->e_name.bv_len = rlen - 1;
466         e->e_nname.bv_len = nrlen - 1;
467         e->e_name.bv_val = ch_malloc(rlen);
468         e->e_nname.bv_val = ch_malloc(nrlen);
469         ptr = e->e_name.bv_val;
470         nptr = e->e_nname.bv_val;
471         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
472                 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
473                 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
474                 if ( ei->bei_parent ) {
475                         *ptr++ = ',';
476                         *nptr++ = ',';
477                 }
478         }
479         BEI(e)->bei_modrdns = max;
480         ptr[-1] = '\0';
481         nptr[-1] = '\0';
482
483         return 0;
484 }
485
486 /* We add two elements to the DN2ID database - a data item under the parent's
487  * entryID containing the child's RDN and entryID, and an item under the
488  * child's entryID containing the parent's entryID.
489  */
490 int
491 hdb_dn2id_add(
492         Operation       *op,
493         DB_TXN *txn,
494         EntryInfo       *eip,
495         Entry           *e )
496 {
497         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
498         DB *db = bdb->bi_dn2id->bdi_db;
499         DBT             key, data;
500         int             rc, rlen, nrlen;
501         diskNode *d;
502         char *ptr;
503
504         nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
505         if (nrlen) {
506                 rlen = dn_rdnlen( op->o_bd, &e->e_name );
507         } else {
508                 nrlen = e->e_nname.bv_len;
509                 rlen = e->e_name.bv_len;
510         }
511
512         d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
513         d->entryID = e->e_id;
514         d->nrdnlen = nrlen;
515         ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
516         *ptr++ = '\0';
517         ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
518         *ptr = '\0';
519
520         DBTzero(&key);
521         DBTzero(&data);
522         key.data = &eip->bei_id;
523         key.size = sizeof(ID);
524         key.flags = DB_DBT_USERMEM;
525
526         /* Need to make dummy root node once. Subsequent attempts
527          * will fail harmlessly.
528          */
529         if ( eip->bei_id == 0 ) {
530                 diskNode dummy = {0};
531                 data.data = &dummy;
532                 data.size = sizeof(diskNode);
533                 data.flags = DB_DBT_USERMEM;
534
535                 db->put( db, txn, &key, &data, DB_NODUPDATA );
536         }
537
538         if ( bdb->bi_idl_cache_size ) {
539                 bdb_idl_cache_del( bdb, db, &key );
540         }
541         data.data = d;
542         data.size = sizeof(diskNode) + rlen + nrlen;
543         data.flags = DB_DBT_USERMEM;
544
545         rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
546
547         if (rc == 0) {
548                 key.data = &e->e_id;
549                 d->entryID = eip->bei_id;
550                 d->nrdnlen = 0 - nrlen;
551
552                 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
553         }
554
555         op->o_tmpfree( d, op->o_tmpmemctx );
556
557         return rc;
558 }
559
560 int
561 hdb_dn2id_delete(
562         Operation       *op,
563         DB_TXN *txn,
564         EntryInfo       *eip,
565         Entry   *e )
566 {
567         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
568         DB *db = bdb->bi_dn2id->bdi_db;
569         DBT             key, data;
570         DBC     *cursor;
571         diskNode *d;
572         int rc, nrlen;
573
574         DBTzero(&key);
575         key.size = sizeof(ID);
576         key.ulen = key.size;
577         key.data = &eip->bei_id;
578         key.flags = DB_DBT_USERMEM;
579
580         DBTzero(&data);
581         data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len;
582         data.ulen = data.size;
583         data.dlen = data.size;
584         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
585
586         if ( bdb->bi_idl_cache_size ) {
587                 bdb_idl_cache_del( bdb, db, &key );
588         }
589         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
590         if ( rc ) return rc;
591
592         d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
593         d->entryID = e->e_id;
594         d->nrdnlen = BEI(e)->bei_nrdn.bv_len;
595         strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
596         data.data = d;
597
598         /* Delete our ID from the parent's list */
599         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH | DB_RMW );
600         if ( rc == 0 )
601                 rc = cursor->c_del( cursor, 0 );
602
603         /* Delete our ID from the tree. With sorted duplicates, this
604          * will leave any child nodes still hanging around. This is OK
605          * for modrdn, which will add our info back in later.
606          */
607         if ( rc == 0 ) {
608                 key.data = &e->e_id;
609                 rc = cursor->c_get( cursor, &key, &data, DB_SET | DB_RMW );
610                 if ( rc == 0 )
611                         rc = cursor->c_del( cursor, 0 );
612         }
613         cursor->c_close( cursor );
614         op->o_tmpfree( d, op->o_tmpmemctx );
615
616         return rc;
617 }
618
619
620 int
621 hdb_dn2id(
622         Operation       *op,
623         DB_TXN *txn,
624         struct berval   *in,
625         EntryInfo       *ei )
626 {
627         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
628         DB *db = bdb->bi_dn2id->bdi_db;
629         DBT             key, data;
630         DBC     *cursor;
631         int             rc = 0, nrlen;
632         diskNode *d;
633         char    *ptr;
634         ID idp = ei->bei_parent->bei_id;
635
636         nrlen = dn_rdnlen( op->o_bd, in );
637         if (!nrlen) nrlen = in->bv_len;
638
639         DBTzero(&key);
640         key.size = sizeof(ID);
641         key.data = &idp;
642         key.ulen = sizeof(ID);
643         key.flags = DB_DBT_USERMEM;
644
645         DBTzero(&data);
646         data.size = sizeof(diskNode) + nrlen;
647         data.ulen = data.size * 3;
648         data.flags = DB_DBT_USERMEM;
649
650         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
651         if ( rc ) return rc;
652
653         d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
654         d->nrdnlen = nrlen;
655         ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
656         *ptr = '\0';
657         data.data = d;
658
659         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH );
660         if ( rc == 0 ) {
661                 ei->bei_id = d->entryID;
662                 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
663                 ptr = d->nrdn + nrlen + 1;
664                 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
665                 if ( !ei->bei_parent->bei_dkids ) {
666                         db_recno_t dkids;
667                         /* How many children does the parent have? */
668                         /* FIXME: do we need to lock the parent
669                          * entryinfo? Seems safe...
670                          */
671                         cursor->c_count( cursor, &dkids, 0 );
672                         ei->bei_parent->bei_dkids = dkids;
673                 }
674         }
675         cursor->c_close( cursor );
676         op->o_tmpfree( d, op->o_tmpmemctx );
677
678         return rc;
679 }
680
681 int
682 hdb_dn2id_parent(
683         Operation *op,
684         DB_TXN *txn,
685         EntryInfo *ei,
686         ID *idp )
687 {
688         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
689         DB *db = bdb->bi_dn2id->bdi_db;
690         DBT             key, data;
691         DBC     *cursor;
692         int             rc = 0;
693         diskNode *d;
694         char    *ptr;
695         unsigned char *pt2;
696
697         DBTzero(&key);
698         key.size = sizeof(ID);
699         key.data = &ei->bei_id;
700         key.ulen = sizeof(ID);
701         key.flags = DB_DBT_USERMEM;
702
703         DBTzero(&data);
704         data.flags = DB_DBT_USERMEM;
705
706         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
707         if ( rc ) return rc;
708
709         data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
710         d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
711         data.data = d;
712
713         rc = cursor->c_get( cursor, &key, &data, DB_SET );
714         if ( rc == 0 ) {
715                 if (d->nrdnlen >= 0) {
716                         rc = LDAP_OTHER;
717                 } else {
718                         db_recno_t dkids;
719                         *idp = d->entryID;
720                         ei->bei_nrdn.bv_len = 0 - d->nrdnlen;
721                         ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
722                         ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
723                                 ei->bei_nrdn.bv_len;
724                         ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
725                         ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
726                         /* How many children does this node have? */
727                         cursor->c_count( cursor, &dkids, 0 );
728                         ei->bei_dkids = dkids;
729                 }
730         }
731         cursor->c_close( cursor );
732         op->o_tmpfree( d, op->o_tmpmemctx );
733         return rc;
734 }
735
736 int
737 hdb_dn2id_children(
738         Operation *op,
739         DB_TXN *txn,
740         Entry *e )
741 {
742         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
743         DB *db = bdb->bi_dn2id->bdi_db;
744         DBT             key, data;
745         DBC             *cursor;
746         int             rc;
747         ID              id;
748         diskNode d;
749
750         DBTzero(&key);
751         key.size = sizeof(ID);
752         key.data = &e->e_id;
753         key.flags = DB_DBT_USERMEM;
754
755         if ( bdb->bi_idl_cache_size ) {
756                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
757                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
758                         return rc;
759                 }
760         }
761         DBTzero(&data);
762         data.data = &d;
763         data.ulen = sizeof(d);
764         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
765         data.dlen = sizeof(d);
766
767         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
768         if ( rc ) return rc;
769
770         rc = cursor->c_get( cursor, &key, &data, DB_SET );
771         if ( rc == 0 ) {
772                 db_recno_t dkids;
773                 rc = cursor->c_count( cursor, &dkids, 0 );
774                 if ( rc == 0 ) {
775                         BEI(e)->bei_dkids = dkids;
776                         if ( dkids < 2 ) rc = DB_NOTFOUND;
777                 }
778         }
779         cursor->c_close( cursor );
780         return rc;
781 }
782
783 /* bdb_dn2idl:
784  * We can't just use bdb_idl_fetch_key because
785  * 1 - our data items are longer than just an entry ID
786  * 2 - our data items are sorted alphabetically by nrdn, not by ID.
787  *
788  * We descend the tree recursively, so we define this cookie
789  * to hold our necessary state information. The bdb_dn2idl_internal
790  * function uses this cookie when calling itself.
791  */
792
793 struct dn2id_cookie {
794         struct bdb_info *bdb;
795         DB *db;
796         int prefix;
797         int rc;
798         EntryInfo *ei;
799         ID id;
800         ID dbuf;
801         ID *ids;
802         void *ptr;
803         ID tmp[BDB_IDL_DB_SIZE];
804         ID *buf;
805         DBT key;
806         DBT data;
807         DBC *dbc;
808         Operation *op;
809 };
810
811 static int
812 apply_func(
813         void *data,
814         void *arg )
815 {
816         EntryInfo *ei = data;
817         ID *idl = arg;
818
819         bdb_idl_insert( idl, ei->bei_id );
820         return 0;
821 }
822
823 static int
824 hdb_dn2idl_internal(
825         struct dn2id_cookie *cx
826 )
827 {
828         if ( cx->bdb->bi_idl_cache_size ) {
829                 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
830                 if ( cx->rc == DB_NOTFOUND ) {
831                         return cx->rc;
832                 }
833                 if ( cx->rc == LDAP_SUCCESS ) {
834                         goto gotit;
835                 }
836         }
837         BDB_IDL_ZERO( cx->tmp );
838
839         if ( !cx->ei ) {
840                 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
841                 if ( !cx->ei ) {
842                         cx->rc = DB_NOTFOUND;
843                         goto saveit;
844                 }
845         }
846
847         bdb_cache_entryinfo_lock( cx->ei );
848
849         /* If number of kids in the cache differs from on-disk, load
850          * up all the kids from the database
851          */
852         if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
853                 EntryInfo ei;
854                 db_recno_t dkids = cx->ei->bei_dkids;
855                 ei.bei_parent = cx->ei;
856
857                 bdb_cache_entryinfo_unlock( cx->ei );
858
859                 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
860                         cx->bdb->bi_db_opflags );
861                 if ( cx->rc ) return cx->rc;
862
863                 cx->data.data = &cx->dbuf;
864                 cx->data.ulen = sizeof(ID);
865                 cx->data.dlen = sizeof(ID);
866                 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
867
868                 /* The first item holds the parent ID. Ignore it. */
869                 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
870                 if ( cx->rc ) {
871                         cx->dbc->c_close( cx->dbc );
872                         if ( cx->rc == DB_NOTFOUND ) goto saveit;
873                         return cx->rc;
874                 }
875
876                 /* If the on-disk count is zero we've never checked it.
877                  * Count it now.
878                  */
879                 if ( !dkids ) {
880                         cx->dbc->c_count( cx->dbc, &dkids, 0 );
881                         cx->ei->bei_dkids = dkids;
882                 }
883
884                 cx->data.data = cx->buf;
885                 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
886                 cx->data.flags = DB_DBT_USERMEM;
887
888                 /* Fetch the rest of the IDs in a loop... */
889                 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
890                         DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
891                         u_int8_t *j;
892                         size_t len;
893                         DB_MULTIPLE_INIT( cx->ptr, &cx->data );
894                         while (cx->ptr) {
895                                 DB_MULTIPLE_NEXT( cx->ptr, &cx->data, j, len );
896                                 if (j) {
897                                         EntryInfo *ei2;
898                                         diskNode *d = (diskNode *)j;
899                                         short nrlen;
900
901                                         AC_MEMCPY( &ei.bei_id, &d->entryID, sizeof(ID) );
902                                         AC_MEMCPY( &nrlen, &d->nrdnlen, sizeof(d->nrdnlen) );
903                                         ei.bei_nrdn.bv_len = nrlen;
904                                         /* nrdn/rdn are set in-place.
905                                          * hdb_cache_load will copy them as needed
906                                          */
907                                         ei.bei_nrdn.bv_val = d->nrdn;
908                                         ei.bei_rdn.bv_len = len - sizeof(diskNode)
909                                                 - ei.bei_nrdn.bv_len;
910                                         ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
911                                         bdb_idl_insert( cx->tmp, ei.bei_id );
912                                         hdb_cache_load( cx->bdb, &ei, &ei2 );
913                                 }
914                         }
915                 }
916                 cx->rc = cx->dbc->c_close( cx->dbc );
917         } else {
918                 /* The in-memory cache is in sync with the on-disk data.
919                  * do we have any kids?
920                  */
921                 cx->rc = 0;
922                 if ( cx->ei->bei_ckids > 0 ) {
923                         /* Walk the kids tree; order is irrelevant since bdb_idl_insert
924                          * will insert in sorted order.
925                          */
926                         avl_apply( cx->ei->bei_kids, apply_func,
927                                 cx->tmp, -1, AVL_POSTORDER );
928                 }
929                 bdb_cache_entryinfo_unlock( cx->ei );
930         }
931
932 saveit:
933         if ( cx->bdb->bi_idl_cache_max_size ) {
934                 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
935         }
936         ;
937 gotit:
938         if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
939                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
940                         if (cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS) {
941                                 bdb_idl_union( cx->ids, cx->tmp );
942                         } else {
943                                 ID *save, idcurs;
944                                 EntryInfo *ei = cx->ei;
945                                 int nokids = 1;
946                                 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
947                                         cx->op->o_tmpmemctx );
948                                 BDB_IDL_CPY( save, cx->tmp );
949                                 bdb_idl_union( cx->ids, cx->tmp );
950
951                                 idcurs = 0;
952                                 for ( cx->id = bdb_idl_first( save, &idcurs );
953                                         cx->id != NOID;
954                                         cx->id = bdb_idl_next( save, &idcurs )) {
955                                         cx->ei = NULL;
956                                         hdb_dn2idl_internal( cx );
957                                         if ( !BDB_IDL_IS_ZERO( cx->tmp ))
958                                                 nokids = 0;
959                                 }
960                                 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
961                                 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
962                         }
963                         /* Make sure caller knows it had kids! */
964                         cx->tmp[0]=1;
965
966                         cx->rc = 0;
967                 } else {
968                         BDB_IDL_CPY( cx->ids, cx->tmp );
969                 }
970         }
971         return cx->rc;
972 }
973
974 int
975 hdb_dn2idl(
976         Operation       *op,
977         Entry           *e,
978         ID *ids,
979         ID *stack )
980 {
981         struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
982         struct dn2id_cookie cx;
983
984         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
985                 e->e_nname.bv_val, 0, 0 );
986
987 #ifndef BDB_MULTIPLE_SUFFIXES
988         if ( op->ors_scope != LDAP_SCOPE_ONELEVEL && 
989                 BEI(e)->bei_parent->bei_id == 0 )
990         {
991                 BDB_IDL_ALL( bdb, ids );
992                 return 0;
993         }
994 #endif
995
996         cx.id = e->e_id;
997         cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
998         cx.bdb = bdb;
999         cx.db = cx.bdb->bi_dn2id->bdi_db;
1000         cx.prefix = op->ors_scope == LDAP_SCOPE_ONELEVEL
1001                 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1002         cx.ids = ids;
1003         cx.buf = stack;
1004         cx.op = op;
1005
1006         BDB_IDL_ZERO( ids );
1007         if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1008                 bdb_idl_insert( ids, cx.id );
1009         }
1010
1011         DBTzero(&cx.key);
1012         cx.key.data = &cx.id;
1013         cx.key.ulen = sizeof(ID);
1014         cx.key.size = sizeof(ID);
1015         cx.key.flags = DB_DBT_USERMEM;
1016
1017         DBTzero(&cx.data);
1018
1019         return hdb_dn2idl_internal(&cx);
1020 }
1021 #endif  /* BDB_HIER */