]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
ITS#4040 move initialization
[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-2005 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         BDB_IDL_ZERO( ids );
365         rc = bdb_idl_fetch_key( op->o_bd, db, NULL, &key, ids, NULL, 0 );
366
367         if( rc != 0 ) {
368                 Debug( LDAP_DEBUG_TRACE,
369                         "<= bdb_dn2idl: get failed: %s (%d)\n",
370                         db_strerror( rc ), rc, 0 );
371
372         } else {
373                 Debug( LDAP_DEBUG_TRACE,
374                         "<= bdb_dn2idl: id=%ld first=%ld last=%ld\n",
375                         (long) ids[0],
376                         (long) BDB_IDL_FIRST( ids ), (long) BDB_IDL_LAST( ids ) );
377         }
378
379         op->o_tmpfree( key.data, op->o_tmpmemctx );
380         return rc;
381 }
382
383 #else   /* BDB_HIER */
384 /* Management routines for a hierarchically structured database.
385  *
386  * Instead of a ldbm-style dn2id database, we use a hierarchical one. Each
387  * entry in this database is a struct diskNode, keyed by entryID and with
388  * the data containing the RDN and entryID of the node's children. We use
389  * a B-Tree with sorted duplicates to store all the children of a node under
390  * the same key. Also, the first item under the key contains the entry's own
391  * rdn and the ID of the node's parent, to allow bottom-up tree traversal as
392  * well as top-down. To keep this info first in the list, the high bit of all
393  * subsequent nrdnlen's is always set. This means we can only accomodate
394  * RDNs up to length 32767, but that's fine since full DNs are already
395  * restricted to 8192.
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         unsigned char nrdnlen[2];
402         char nrdn[1];
403         char rdn[1];                        /* variable placement */
404         unsigned char entryID[sizeof(ID)];  /* variable placement */
405 } diskNode;
406
407 /* This function constructs a full DN for a given entry.
408  */
409 int hdb_fix_dn(
410         Entry *e,
411         int checkit )
412 {
413         EntryInfo *ei;
414         int rlen = 0, nrlen = 0;
415         char *ptr, *nptr;
416         int max = 0;
417
418         if ( !e->e_id )
419                 return 0;
420
421         /* count length of all DN components */
422         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
423                 rlen += ei->bei_rdn.bv_len + 1;
424                 nrlen += ei->bei_nrdn.bv_len + 1;
425                 if (ei->bei_modrdns > max) max = ei->bei_modrdns;
426         }
427
428         /* See if the entry DN was invalidated by a subtree rename */
429         if ( checkit ) {
430                 if ( BEI(e)->bei_modrdns >= max ) {
431                         return 0;
432                 }
433                 /* We found a mismatch, tell the caller to lock it */
434                 if ( checkit == 1 ) {
435                         return 1;
436                 }
437                 /* checkit == 2. do the fix. */
438                 free( e->e_name.bv_val );
439                 free( e->e_nname.bv_val );
440         }
441
442         e->e_name.bv_len = rlen - 1;
443         e->e_nname.bv_len = nrlen - 1;
444         e->e_name.bv_val = ch_malloc(rlen);
445         e->e_nname.bv_val = ch_malloc(nrlen);
446         ptr = e->e_name.bv_val;
447         nptr = e->e_nname.bv_val;
448         for ( ei = BEI(e); ei && ei->bei_id; ei=ei->bei_parent ) {
449                 ptr = lutil_strcopy(ptr, ei->bei_rdn.bv_val);
450                 nptr = lutil_strcopy(nptr, ei->bei_nrdn.bv_val);
451                 if ( ei->bei_parent ) {
452                         *ptr++ = ',';
453                         *nptr++ = ',';
454                 }
455         }
456         BEI(e)->bei_modrdns = max;
457         ptr[-1] = '\0';
458         nptr[-1] = '\0';
459
460         return 0;
461 }
462
463 /* We add two elements to the DN2ID database - a data item under the parent's
464  * entryID containing the child's RDN and entryID, and an item under the
465  * child's entryID containing the parent's entryID.
466  */
467 int
468 hdb_dn2id_add(
469         Operation       *op,
470         DB_TXN *txn,
471         EntryInfo       *eip,
472         Entry           *e )
473 {
474         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
475         DB *db = bdb->bi_dn2id->bdi_db;
476         DBT             key, data;
477         ID              nid;
478         int             rc, rlen, nrlen;
479         diskNode *d;
480         char *ptr;
481
482         nrlen = dn_rdnlen( op->o_bd, &e->e_nname );
483         if (nrlen) {
484                 rlen = dn_rdnlen( op->o_bd, &e->e_name );
485         } else {
486                 nrlen = e->e_nname.bv_len;
487                 rlen = e->e_name.bv_len;
488         }
489
490         d = op->o_tmpalloc(sizeof(diskNode) + rlen + nrlen, op->o_tmpmemctx);
491         d->nrdnlen[1] = nrlen & 0xff;
492         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
493         ptr = lutil_strncopy( d->nrdn, e->e_nname.bv_val, nrlen );
494         *ptr++ = '\0';
495         ptr = lutil_strncopy( ptr, e->e_name.bv_val, rlen );
496         *ptr++ = '\0';
497         BDB_ID2DISK( e->e_id, ptr );
498
499         DBTzero(&key);
500         DBTzero(&data);
501         key.size = sizeof(ID);
502         key.flags = DB_DBT_USERMEM;
503         BDB_ID2DISK( eip->bei_id, &nid );
504
505         /* Delete parent's IDL cache entry */
506         if ( bdb->bi_idl_cache_size ) {
507                 key.data = &eip->bei_id;
508                 bdb_idl_cache_del( bdb, db, &key );
509         }
510         key.data = &nid;
511
512         /* Need to make dummy root node once. Subsequent attempts
513          * will fail harmlessly.
514          */
515         if ( eip->bei_id == 0 ) {
516                 diskNode dummy = {{0, 0}, "", "", ""};
517                 data.data = &dummy;
518                 data.size = sizeof(diskNode);
519                 data.flags = DB_DBT_USERMEM;
520
521                 db->put( db, txn, &key, &data, DB_NODUPDATA );
522         }
523
524         data.data = d;
525         data.size = sizeof(diskNode) + rlen + nrlen;
526         data.flags = DB_DBT_USERMEM;
527
528         rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
529
530         if (rc == 0) {
531                 BDB_ID2DISK( e->e_id, &nid );
532                 BDB_ID2DISK( eip->bei_id, ptr );
533                 d->nrdnlen[0] ^= 0x80;
534
535                 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
536         }
537
538         op->o_tmpfree( d, op->o_tmpmemctx );
539
540         return rc;
541 }
542
543 int
544 hdb_dn2id_delete(
545         Operation       *op,
546         DB_TXN *txn,
547         EntryInfo       *eip,
548         Entry   *e )
549 {
550         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
551         DB *db = bdb->bi_dn2id->bdi_db;
552         DBT             key, data;
553         DBC     *cursor;
554         diskNode *d;
555         int rc;
556         ID      nid;
557         unsigned char dlen[2];
558
559         DBTzero(&key);
560         key.size = sizeof(ID);
561         key.ulen = key.size;
562         key.flags = DB_DBT_USERMEM;
563         BDB_ID2DISK( eip->bei_id, &nid );
564
565         DBTzero(&data);
566         data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
567         data.ulen = data.size;
568         data.dlen = data.size;
569         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
570
571         /* Delete IDL cache entries */
572         if ( bdb->bi_idl_cache_size ) {
573                 /* Ours */
574                 key.data = &e->e_id;
575                 bdb_idl_cache_del( bdb, db, &key );
576                 /* Parent's */
577                 key.data = &eip->bei_id;
578                 bdb_idl_cache_del( bdb, db, &key );
579         }
580         key.data = &nid;
581         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
582         if ( rc ) return rc;
583
584         d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
585         d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
586         d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
587         dlen[0] = d->nrdnlen[0];
588         dlen[1] = d->nrdnlen[1];
589         strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
590         data.data = d;
591
592         /* Delete our ID from the parent's list */
593         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
594         if ( rc == 0 ) {
595                 if ( dlen[1] == d->nrdnlen[1] && dlen[0] == d->nrdnlen[0] &&
596                         !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
597                         rc = cursor->c_del( cursor, 0 );
598                 else
599                         rc = DB_NOTFOUND;
600         }
601
602         /* Delete our ID from the tree. With sorted duplicates, this
603          * will leave any child nodes still hanging around. This is OK
604          * for modrdn, which will add our info back in later.
605          */
606         if ( rc == 0 ) {
607                 BDB_ID2DISK( e->e_id, &nid );
608                 rc = cursor->c_get( cursor, &key, &data, DB_SET );
609                 if ( rc == 0 )
610                         rc = cursor->c_del( cursor, 0 );
611         }
612         cursor->c_close( cursor );
613         op->o_tmpfree( d, op->o_tmpmemctx );
614
615         return rc;
616 }
617
618
619 int
620 hdb_dn2id(
621         Operation       *op,
622         DB_TXN *txn,
623         struct berval   *in,
624         EntryInfo       *ei )
625 {
626         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
627         DB *db = bdb->bi_dn2id->bdi_db;
628         DBT             key, data;
629         DBC     *cursor;
630         int             rc = 0, nrlen;
631         diskNode *d;
632         char    *ptr;
633         unsigned char dlen[2];
634         ID idp, parentID;
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         parentID = ( ei->bei_parent != NULL ) ? ei->bei_parent->bei_id : 0;
645         BDB_ID2DISK( parentID, &idp );
646
647         DBTzero(&data);
648         data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
649         data.ulen = data.size * 3;
650         data.dlen = data.ulen;
651         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
652
653         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
654         if ( rc ) return rc;
655
656         d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
657         d->nrdnlen[1] = nrlen & 0xff;
658         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
659         dlen[0] = d->nrdnlen[0];
660         dlen[1] = d->nrdnlen[1];
661         ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
662         *ptr = '\0';
663         data.data = d;
664
665         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
666         if ( rc == 0 && (dlen[1] != d->nrdnlen[1] || dlen[0] != d->nrdnlen[0] ||
667                 strncmp( d->nrdn, in->bv_val, nrlen ))) {
668                 rc = DB_NOTFOUND;
669         }
670         if ( rc == 0 ) {
671                 ptr = (char *) data.data + data.size - sizeof(ID);
672                 BDB_DISK2ID( ptr, &ei->bei_id );
673                 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
674                 ptr = d->nrdn + nrlen + 1;
675                 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
676                 if ( ei->bei_parent != NULL && !ei->bei_parent->bei_dkids ) {
677                         db_recno_t dkids;
678                         /* How many children does the parent have? */
679                         /* FIXME: do we need to lock the parent
680                          * entryinfo? Seems safe...
681                          */
682                         cursor->c_count( cursor, &dkids, 0 );
683                         ei->bei_parent->bei_dkids = dkids;
684                 }
685         }
686         cursor->c_close( cursor );
687         op->o_tmpfree( d, op->o_tmpmemctx );
688
689         return rc;
690 }
691
692 int
693 hdb_dn2id_parent(
694         Operation *op,
695         DB_TXN *txn,
696         EntryInfo *ei,
697         ID *idp )
698 {
699         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
700         DB *db = bdb->bi_dn2id->bdi_db;
701         DBT             key, data;
702         DBC     *cursor;
703         int             rc = 0;
704         diskNode *d;
705         char    *ptr;
706         ID      nid;
707
708         DBTzero(&key);
709         key.size = sizeof(ID);
710         key.data = &nid;
711         key.ulen = sizeof(ID);
712         key.flags = DB_DBT_USERMEM;
713         BDB_ID2DISK( ei->bei_id, &nid );
714
715         DBTzero(&data);
716         data.flags = DB_DBT_USERMEM;
717
718         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
719         if ( rc ) return rc;
720
721         data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
722         d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
723         data.data = d;
724
725         rc = cursor->c_get( cursor, &key, &data, DB_SET );
726         if ( rc == 0 ) {
727                 if (d->nrdnlen[0] & 0x80) {
728                         rc = LDAP_OTHER;
729                 } else {
730                         db_recno_t dkids;
731                         ptr = (char *) data.data + data.size - sizeof(ID);
732                         BDB_DISK2ID( ptr, idp );
733                         ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
734                         ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
735                         ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
736                                 ei->bei_nrdn.bv_len;
737                         ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
738                         ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
739                         /* How many children does this node have? */
740                         cursor->c_count( cursor, &dkids, 0 );
741                         ei->bei_dkids = dkids;
742                 }
743         }
744         cursor->c_close( cursor );
745         op->o_tmpfree( d, op->o_tmpmemctx );
746         return rc;
747 }
748
749 int
750 hdb_dn2id_children(
751         Operation *op,
752         DB_TXN *txn,
753         Entry *e )
754 {
755         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
756         DB *db = bdb->bi_dn2id->bdi_db;
757         DBT             key, data;
758         DBC             *cursor;
759         int             rc;
760         ID              id;
761         diskNode d;
762
763         DBTzero(&key);
764         key.size = sizeof(ID);
765         key.data = &e->e_id;
766         key.flags = DB_DBT_USERMEM;
767         BDB_ID2DISK( e->e_id, &id );
768
769         /* IDL cache is in host byte order */
770         if ( bdb->bi_idl_cache_size ) {
771                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
772                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
773                         return rc;
774                 }
775         }
776
777         key.data = &id;
778         DBTzero(&data);
779         data.data = &d;
780         data.ulen = sizeof(d);
781         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
782         data.dlen = sizeof(d);
783
784         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
785         if ( rc ) return rc;
786
787         rc = cursor->c_get( cursor, &key, &data, DB_SET );
788         if ( rc == 0 ) {
789                 db_recno_t dkids;
790                 rc = cursor->c_count( cursor, &dkids, 0 );
791                 if ( rc == 0 ) {
792                         BEI(e)->bei_dkids = dkids;
793                         if ( dkids < 2 ) rc = DB_NOTFOUND;
794                 }
795         }
796         cursor->c_close( cursor );
797         return rc;
798 }
799
800 /* bdb_dn2idl:
801  * We can't just use bdb_idl_fetch_key because
802  * 1 - our data items are longer than just an entry ID
803  * 2 - our data items are sorted alphabetically by nrdn, not by ID.
804  *
805  * We descend the tree recursively, so we define this cookie
806  * to hold our necessary state information. The bdb_dn2idl_internal
807  * function uses this cookie when calling itself.
808  */
809
810 struct dn2id_cookie {
811         struct bdb_info *bdb;
812         DB *db;
813         int prefix;
814         int rc;
815         EntryInfo *ei;
816         ID id;
817         ID nid;
818         ID dbuf;
819         ID *ids;
820         void *ptr;
821         ID *tmp;
822         ID *buf;
823         DBT key;
824         DBT data;
825         DBC *dbc;
826         Operation *op;
827         int need_sort;
828 };
829
830 static int
831 apply_func(
832         void *data,
833         void *arg )
834 {
835         EntryInfo *ei = data;
836         ID *idl = arg;
837
838         bdb_idl_append_one( idl, ei->bei_id );
839         return 0;
840 }
841
842 static int
843 hdb_dn2idl_internal(
844         struct dn2id_cookie *cx
845 )
846 {
847         BDB_IDL_ZERO( cx->tmp );
848
849         if ( !cx->ei ) {
850                 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
851                 if ( !cx->ei ) {
852                         cx->rc = DB_NOTFOUND;
853                         goto saveit;
854                 }
855         }
856
857         if ( cx->bdb->bi_idl_cache_size ) {
858                 cx->key.data = &cx->id;
859                 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
860                 if ( cx->rc == DB_NOTFOUND ) {
861                         return cx->rc;
862                 }
863                 if ( cx->rc == LDAP_SUCCESS ) {
864                         goto gotit;
865                 }
866         }
867
868         bdb_cache_entryinfo_lock( cx->ei );
869
870         /* If number of kids in the cache differs from on-disk, load
871          * up all the kids from the database
872          */
873         if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
874                 EntryInfo ei;
875                 db_recno_t dkids = cx->ei->bei_dkids;
876                 ei.bei_parent = cx->ei;
877
878                 bdb_cache_entryinfo_unlock( cx->ei );
879
880                 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
881                         cx->bdb->bi_db_opflags );
882                 if ( cx->rc ) return cx->rc;
883
884                 cx->data.data = &cx->dbuf;
885                 cx->data.ulen = sizeof(ID);
886                 cx->data.dlen = sizeof(ID);
887                 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
888
889                 /* The first item holds the parent ID. Ignore it. */
890                 cx->key.data = &cx->nid;
891                 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
892                 if ( cx->rc ) {
893                         cx->dbc->c_close( cx->dbc );
894                         if ( cx->rc == DB_NOTFOUND ) goto saveit;
895                         return cx->rc;
896                 }
897
898                 /* If the on-disk count is zero we've never checked it.
899                  * Count it now.
900                  */
901                 if ( !dkids ) {
902                         cx->dbc->c_count( cx->dbc, &dkids, 0 );
903                         cx->ei->bei_dkids = dkids;
904                 }
905
906                 cx->data.data = cx->buf;
907                 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
908                 cx->data.flags = DB_DBT_USERMEM;
909
910                 /* Fetch the rest of the IDs in a loop... */
911                 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
912                         DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
913                         u_int8_t *j;
914                         size_t len;
915                         DB_MULTIPLE_INIT( cx->ptr, &cx->data );
916                         while (cx->ptr) {
917                                 DB_MULTIPLE_NEXT( cx->ptr, &cx->data, j, len );
918                                 if (j) {
919                                         EntryInfo *ei2;
920                                         diskNode *d = (diskNode *)j;
921                                         short nrlen;
922
923                                         BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
924                                         nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
925                                         ei.bei_nrdn.bv_len = nrlen;
926                                         /* nrdn/rdn are set in-place.
927                                          * hdb_cache_load will copy them as needed
928                                          */
929                                         ei.bei_nrdn.bv_val = d->nrdn;
930                                         ei.bei_rdn.bv_len = len - sizeof(diskNode)
931                                                 - ei.bei_nrdn.bv_len;
932                                         ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
933                                         bdb_idl_append_one( cx->tmp, ei.bei_id );
934                                         hdb_cache_load( cx->bdb, &ei, &ei2 );
935                                 }
936                         }
937                 }
938                 cx->rc = cx->dbc->c_close( cx->dbc );
939         } else {
940                 /* The in-memory cache is in sync with the on-disk data.
941                  * do we have any kids?
942                  */
943                 cx->rc = 0;
944                 if ( cx->ei->bei_ckids > 0 ) {
945                         /* Walk the kids tree; order is irrelevant since bdb_idl_insert
946                          * will insert in sorted order.
947                          */
948                         avl_apply( cx->ei->bei_kids, apply_func,
949                                 cx->tmp, -1, AVL_POSTORDER );
950                 }
951                 bdb_cache_entryinfo_unlock( cx->ei );
952         }
953
954 saveit:
955         if ( !BDB_IDL_IS_RANGE( cx->tmp ) && cx->tmp[0] > 3 )
956                 bdb_idl_sort( cx->tmp, cx->buf );
957         if ( cx->bdb->bi_idl_cache_max_size ) {
958                 cx->key.data = &cx->id;
959                 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
960         }
961
962 gotit:
963         if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
964                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
965                         bdb_idl_append( cx->ids, cx->tmp );
966                         cx->need_sort = 1;
967                         if ( !(cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS)) {
968                                 ID *save, idcurs;
969                                 EntryInfo *ei = cx->ei;
970                                 int nokids = 1;
971                                 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
972                                         cx->op->o_tmpmemctx );
973                                 BDB_IDL_CPY( save, cx->tmp );
974
975                                 idcurs = 0;
976                                 for ( cx->id = bdb_idl_first( save, &idcurs );
977                                         cx->id != NOID;
978                                         cx->id = bdb_idl_next( save, &idcurs )) {
979                                         BDB_ID2DISK( cx->id, &cx->nid );
980                                         cx->ei = NULL;
981                                         hdb_dn2idl_internal( cx );
982                                         if ( !BDB_IDL_IS_ZERO( cx->tmp ))
983                                                 nokids = 0;
984                                 }
985                                 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
986                                 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
987                         }
988                         /* Make sure caller knows it had kids! */
989                         cx->tmp[0]=1;
990
991                         cx->rc = 0;
992                 } else {
993                         BDB_IDL_CPY( cx->ids, cx->tmp );
994                 }
995         }
996         return cx->rc;
997 }
998
999 int
1000 hdb_dn2idl(
1001         Operation       *op,
1002         Entry           *e,
1003         ID *ids,
1004         ID *stack )
1005 {
1006         struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
1007         struct dn2id_cookie cx;
1008
1009         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
1010                 e->e_nname.bv_val, 0, 0 );
1011
1012 #ifndef BDB_MULTIPLE_SUFFIXES
1013         if ( op->ors_scope != LDAP_SCOPE_ONELEVEL && 
1014                 BEI(e)->bei_parent->bei_id == 0 )
1015         {
1016                 BDB_IDL_ALL( bdb, ids );
1017                 return 0;
1018         }
1019 #endif
1020
1021         cx.id = e->e_id;
1022         BDB_ID2DISK( cx.id, &cx.nid );
1023         cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1024         cx.bdb = bdb;
1025         cx.db = cx.bdb->bi_dn2id->bdi_db;
1026         cx.prefix = (op->ors_scope == LDAP_SCOPE_ONELEVEL) ?
1027                 DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1028         cx.ids = ids;
1029         cx.tmp = stack;
1030         cx.buf = stack + BDB_IDL_UM_SIZE;
1031         cx.op = op;
1032         cx.need_sort = 0;
1033
1034         BDB_IDL_ZERO( ids );
1035         if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1036                 bdb_idl_insert( ids, cx.id );
1037         }
1038
1039         DBTzero(&cx.key);
1040         cx.key.ulen = sizeof(ID);
1041         cx.key.size = sizeof(ID);
1042         cx.key.flags = DB_DBT_USERMEM;
1043
1044         DBTzero(&cx.data);
1045
1046         hdb_dn2idl_internal(&cx);
1047         if ( cx.need_sort && !BDB_IDL_IS_RANGE( cx.ids ) && cx.ids[0] > 3 )
1048                 bdb_idl_sort( cx.ids, cx.tmp );
1049
1050         return cx.rc;
1051 }
1052 #endif  /* BDB_HIER */