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