]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/dn2id.c
14f587f0fe1cbf25c1b2aae116816134639c7e4f
[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         /* 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.data = &nid;
502         key.size = sizeof(ID);
503         key.flags = DB_DBT_USERMEM;
504         BDB_ID2DISK( eip->bei_id, &nid );
505
506         /* Need to make dummy root node once. Subsequent attempts
507          * will fail harmlessly.
508          */
509         if ( eip->bei_id == 0 ) {
510                 diskNode dummy = {0};
511                 data.data = &dummy;
512                 data.size = sizeof(diskNode);
513                 data.flags = DB_DBT_USERMEM;
514
515                 db->put( db, txn, &key, &data, DB_NODUPDATA );
516         }
517
518         if ( bdb->bi_idl_cache_size ) {
519                 bdb_idl_cache_del( bdb, db, &key );
520         }
521         data.data = d;
522         data.size = sizeof(diskNode) + rlen + nrlen;
523         data.flags = DB_DBT_USERMEM;
524
525         rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
526
527         if (rc == 0) {
528                 BDB_ID2DISK( e->e_id, &nid );
529                 BDB_ID2DISK( eip->bei_id, ptr );
530                 d->nrdnlen[0] ^= 0x80;
531
532                 rc = db->put( db, txn, &key, &data, DB_NODUPDATA );
533         }
534
535         op->o_tmpfree( d, op->o_tmpmemctx );
536
537         return rc;
538 }
539
540 int
541 hdb_dn2id_delete(
542         Operation       *op,
543         DB_TXN *txn,
544         EntryInfo       *eip,
545         Entry   *e )
546 {
547         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
548         DB *db = bdb->bi_dn2id->bdi_db;
549         DBT             key, data;
550         DBC     *cursor;
551         diskNode *d;
552         int rc, nrlen;
553         ID      nid;
554
555         DBTzero(&key);
556         key.size = sizeof(ID);
557         key.ulen = key.size;
558         key.data = &nid;
559         key.flags = DB_DBT_USERMEM;
560         BDB_ID2DISK( eip->bei_id, &nid );
561
562         DBTzero(&data);
563         data.size = sizeof(diskNode) + BEI(e)->bei_nrdn.bv_len - sizeof(ID) - 1;
564         data.ulen = data.size;
565         data.dlen = data.size;
566         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
567
568         if ( bdb->bi_idl_cache_size ) {
569                 bdb_idl_cache_del( bdb, db, &key );
570         }
571         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
572         if ( rc ) return rc;
573
574         d = op->o_tmpalloc( data.size, op->o_tmpmemctx );
575         d->nrdnlen[1] = BEI(e)->bei_nrdn.bv_len & 0xff;
576         d->nrdnlen[0] = (BEI(e)->bei_nrdn.bv_len >> 8) | 0x80;
577         strcpy( d->nrdn, BEI(e)->bei_nrdn.bv_val );
578         data.data = d;
579
580         /* Delete our ID from the parent's list */
581         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
582         if ( rc == 0 ) {
583                 if ( !strcmp( d->nrdn, BEI(e)->bei_nrdn.bv_val ))
584                         rc = cursor->c_del( cursor, 0 );
585                 else
586                         rc = DB_NOTFOUND;
587         }
588
589         /* Delete our ID from the tree. With sorted duplicates, this
590          * will leave any child nodes still hanging around. This is OK
591          * for modrdn, which will add our info back in later.
592          */
593         if ( rc == 0 ) {
594                 BDB_ID2DISK( e->e_id, &nid );
595                 rc = cursor->c_get( cursor, &key, &data, DB_SET );
596                 if ( rc == 0 )
597                         rc = cursor->c_del( cursor, 0 );
598         }
599         cursor->c_close( cursor );
600         op->o_tmpfree( d, op->o_tmpmemctx );
601
602         return rc;
603 }
604
605
606 int
607 hdb_dn2id(
608         Operation       *op,
609         DB_TXN *txn,
610         struct berval   *in,
611         EntryInfo       *ei )
612 {
613         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
614         DB *db = bdb->bi_dn2id->bdi_db;
615         DBT             key, data;
616         DBC     *cursor;
617         int             rc = 0, nrlen;
618         diskNode *d;
619         char    *ptr;
620         ID idp;
621
622         nrlen = dn_rdnlen( op->o_bd, in );
623         if (!nrlen) nrlen = in->bv_len;
624
625         DBTzero(&key);
626         key.size = sizeof(ID);
627         key.data = &idp;
628         key.ulen = sizeof(ID);
629         key.flags = DB_DBT_USERMEM;
630         BDB_ID2DISK( ei->bei_parent->bei_id, &idp );
631
632         DBTzero(&data);
633         data.size = sizeof(diskNode) + nrlen - sizeof(ID) - 1;
634         data.ulen = data.size * 3;
635         data.dlen = data.ulen;
636         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
637
638         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
639         if ( rc ) return rc;
640
641         d = op->o_tmpalloc( data.size * 3, op->o_tmpmemctx );
642         d->nrdnlen[1] = nrlen & 0xff;
643         d->nrdnlen[0] = (nrlen >> 8) | 0x80;
644         ptr = lutil_strncopy( d->nrdn, in->bv_val, nrlen );
645         *ptr = '\0';
646         data.data = d;
647
648         rc = cursor->c_get( cursor, &key, &data, DB_GET_BOTH_RANGE );
649         if ( rc == 0 && strncmp( d->nrdn, in->bv_val, nrlen )) {
650                 rc = DB_NOTFOUND;
651         }
652         if ( rc == 0 ) {
653                 ptr = data.data + data.size - sizeof(ID);
654                 BDB_DISK2ID( ptr, &ei->bei_id );
655                 ei->bei_rdn.bv_len = data.size - sizeof(diskNode) - nrlen;
656                 ptr = d->nrdn + nrlen + 1;
657                 ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
658                 if ( !ei->bei_parent->bei_dkids ) {
659                         db_recno_t dkids;
660                         /* How many children does the parent have? */
661                         /* FIXME: do we need to lock the parent
662                          * entryinfo? Seems safe...
663                          */
664                         cursor->c_count( cursor, &dkids, 0 );
665                         ei->bei_parent->bei_dkids = dkids;
666                 }
667         }
668         cursor->c_close( cursor );
669         op->o_tmpfree( d, op->o_tmpmemctx );
670
671         return rc;
672 }
673
674 int
675 hdb_dn2id_parent(
676         Operation *op,
677         DB_TXN *txn,
678         EntryInfo *ei,
679         ID *idp )
680 {
681         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
682         DB *db = bdb->bi_dn2id->bdi_db;
683         DBT             key, data;
684         DBC     *cursor;
685         int             rc = 0;
686         diskNode *d;
687         char    *ptr;
688         unsigned char *pt2;
689         ID      nid;
690
691         DBTzero(&key);
692         key.size = sizeof(ID);
693         key.data = &nid;
694         key.ulen = sizeof(ID);
695         key.flags = DB_DBT_USERMEM;
696         BDB_ID2DISK( ei->bei_id, &nid );
697
698         DBTzero(&data);
699         data.flags = DB_DBT_USERMEM;
700
701         rc = db->cursor( db, txn, &cursor, bdb->bi_db_opflags );
702         if ( rc ) return rc;
703
704         data.ulen = sizeof(diskNode) + (SLAP_LDAPDN_MAXLEN * 2);
705         d = op->o_tmpalloc( data.ulen, op->o_tmpmemctx );
706         data.data = d;
707
708         rc = cursor->c_get( cursor, &key, &data, DB_SET );
709         if ( rc == 0 ) {
710                 if (d->nrdnlen[0] & 0x80) {
711                         rc = LDAP_OTHER;
712                 } else {
713                         db_recno_t dkids;
714                         ptr = data.data + data.size - sizeof(ID);
715                         BDB_DISK2ID( ptr, idp );
716                         ei->bei_nrdn.bv_len = (d->nrdnlen[0] << 8) | d->nrdnlen[1];
717                         ber_str2bv( d->nrdn, ei->bei_nrdn.bv_len, 1, &ei->bei_nrdn );
718                         ei->bei_rdn.bv_len = data.size - sizeof(diskNode) -
719                                 ei->bei_nrdn.bv_len;
720                         ptr = d->nrdn + ei->bei_nrdn.bv_len + 1;
721                         ber_str2bv( ptr, ei->bei_rdn.bv_len, 1, &ei->bei_rdn );
722                         /* How many children does this node have? */
723                         cursor->c_count( cursor, &dkids, 0 );
724                         ei->bei_dkids = dkids;
725                 }
726         }
727         cursor->c_close( cursor );
728         op->o_tmpfree( d, op->o_tmpmemctx );
729         return rc;
730 }
731
732 int
733 hdb_dn2id_children(
734         Operation *op,
735         DB_TXN *txn,
736         Entry *e )
737 {
738         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
739         DB *db = bdb->bi_dn2id->bdi_db;
740         DBT             key, data;
741         DBC             *cursor;
742         int             rc;
743         ID              id;
744         diskNode d;
745
746         DBTzero(&key);
747         key.size = sizeof(ID);
748         key.data = &e->e_id;
749         key.flags = DB_DBT_USERMEM;
750         BDB_ID2DISK( e->e_id, &id );
751
752         /* IDL cache is in host byte order */
753         if ( bdb->bi_idl_cache_size ) {
754                 rc = bdb_idl_cache_get( bdb, db, &key, NULL );
755                 if ( rc != LDAP_NO_SUCH_OBJECT ) {
756                         return rc;
757                 }
758         }
759
760         key.data = &id;
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 nid;
801         ID dbuf;
802         ID *ids;
803         void *ptr;
804         ID tmp[BDB_IDL_DB_SIZE];
805         ID *buf;
806         DBT key;
807         DBT data;
808         DBC *dbc;
809         Operation *op;
810 };
811
812 static int
813 apply_func(
814         void *data,
815         void *arg )
816 {
817         EntryInfo *ei = data;
818         ID *idl = arg;
819
820         bdb_idl_insert( idl, ei->bei_id );
821         return 0;
822 }
823
824 static int
825 hdb_dn2idl_internal(
826         struct dn2id_cookie *cx
827 )
828 {
829         BDB_IDL_ZERO( cx->tmp );
830
831         if ( !cx->ei ) {
832                 cx->ei = bdb_cache_find_info( cx->bdb, cx->id );
833                 if ( !cx->ei ) {
834                         cx->rc = DB_NOTFOUND;
835                         goto saveit;
836                 }
837         }
838
839         if ( cx->bdb->bi_idl_cache_size ) {
840                 cx->key.data = &cx->id;
841                 cx->rc = bdb_idl_cache_get(cx->bdb, cx->db, &cx->key, cx->tmp);
842                 if ( cx->rc == DB_NOTFOUND ) {
843                         return cx->rc;
844                 }
845                 if ( cx->rc == LDAP_SUCCESS ) {
846                         goto gotit;
847                 }
848         }
849
850         bdb_cache_entryinfo_lock( cx->ei );
851
852         /* If number of kids in the cache differs from on-disk, load
853          * up all the kids from the database
854          */
855         if ( cx->ei->bei_ckids+1 != cx->ei->bei_dkids ) {
856                 EntryInfo ei;
857                 db_recno_t dkids = cx->ei->bei_dkids;
858                 ei.bei_parent = cx->ei;
859
860                 bdb_cache_entryinfo_unlock( cx->ei );
861
862                 cx->rc = cx->db->cursor( cx->db, NULL, &cx->dbc,
863                         cx->bdb->bi_db_opflags );
864                 if ( cx->rc ) return cx->rc;
865
866                 cx->data.data = &cx->dbuf;
867                 cx->data.ulen = sizeof(ID);
868                 cx->data.dlen = sizeof(ID);
869                 cx->data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
870
871                 /* The first item holds the parent ID. Ignore it. */
872                 cx->key.data = &cx->nid;
873                 cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data, DB_SET );
874                 if ( cx->rc ) {
875                         cx->dbc->c_close( cx->dbc );
876                         if ( cx->rc == DB_NOTFOUND ) goto saveit;
877                         return cx->rc;
878                 }
879
880                 /* If the on-disk count is zero we've never checked it.
881                  * Count it now.
882                  */
883                 if ( !dkids ) {
884                         cx->dbc->c_count( cx->dbc, &dkids, 0 );
885                         cx->ei->bei_dkids = dkids;
886                 }
887
888                 cx->data.data = cx->buf;
889                 cx->data.ulen = BDB_IDL_UM_SIZE * sizeof(ID);
890                 cx->data.flags = DB_DBT_USERMEM;
891
892                 /* Fetch the rest of the IDs in a loop... */
893                 while ( (cx->rc = cx->dbc->c_get( cx->dbc, &cx->key, &cx->data,
894                         DB_MULTIPLE | DB_NEXT_DUP )) == 0 ) {
895                         u_int8_t *j;
896                         size_t len;
897                         DB_MULTIPLE_INIT( cx->ptr, &cx->data );
898                         while (cx->ptr) {
899                                 DB_MULTIPLE_NEXT( cx->ptr, &cx->data, j, len );
900                                 if (j) {
901                                         EntryInfo *ei2;
902                                         diskNode *d = (diskNode *)j;
903                                         short nrlen;
904
905                                         BDB_DISK2ID( j + len - sizeof(ID), &ei.bei_id );
906                                         nrlen = ((d->nrdnlen[0] ^ 0x80) << 8) | d->nrdnlen[1];
907                                         ei.bei_nrdn.bv_len = nrlen;
908                                         /* nrdn/rdn are set in-place.
909                                          * hdb_cache_load will copy them as needed
910                                          */
911                                         ei.bei_nrdn.bv_val = d->nrdn;
912                                         ei.bei_rdn.bv_len = len - sizeof(diskNode)
913                                                 - ei.bei_nrdn.bv_len;
914                                         ei.bei_rdn.bv_val = d->nrdn + ei.bei_nrdn.bv_len + 1;
915                                         bdb_idl_insert( cx->tmp, ei.bei_id );
916                                         hdb_cache_load( cx->bdb, &ei, &ei2 );
917                                 }
918                         }
919                 }
920                 cx->rc = cx->dbc->c_close( cx->dbc );
921         } else {
922                 /* The in-memory cache is in sync with the on-disk data.
923                  * do we have any kids?
924                  */
925                 cx->rc = 0;
926                 if ( cx->ei->bei_ckids > 0 ) {
927                         /* Walk the kids tree; order is irrelevant since bdb_idl_insert
928                          * will insert in sorted order.
929                          */
930                         avl_apply( cx->ei->bei_kids, apply_func,
931                                 cx->tmp, -1, AVL_POSTORDER );
932                 }
933                 bdb_cache_entryinfo_unlock( cx->ei );
934         }
935
936 saveit:
937         if ( cx->bdb->bi_idl_cache_max_size ) {
938                 cx->key.data = &cx->id;
939                 bdb_idl_cache_put( cx->bdb, cx->db, &cx->key, cx->tmp, cx->rc );
940         }
941         ;
942 gotit:
943         if ( !BDB_IDL_IS_ZERO( cx->tmp )) {
944                 if ( cx->prefix == DN_SUBTREE_PREFIX ) {
945                         if (cx->ei->bei_state & CACHE_ENTRY_NO_GRANDKIDS) {
946                                 bdb_idl_union( cx->ids, cx->tmp );
947                         } else {
948                                 ID *save, idcurs;
949                                 EntryInfo *ei = cx->ei;
950                                 int nokids = 1;
951                                 save = cx->op->o_tmpalloc( BDB_IDL_SIZEOF( cx->tmp ),
952                                         cx->op->o_tmpmemctx );
953                                 BDB_IDL_CPY( save, cx->tmp );
954                                 bdb_idl_union( cx->ids, cx->tmp );
955
956                                 idcurs = 0;
957                                 for ( cx->id = bdb_idl_first( save, &idcurs );
958                                         cx->id != NOID;
959                                         cx->id = bdb_idl_next( save, &idcurs )) {
960                                         BDB_ID2DISK( cx->id, &cx->nid );
961                                         cx->ei = NULL;
962                                         hdb_dn2idl_internal( cx );
963                                         if ( !BDB_IDL_IS_ZERO( cx->tmp ))
964                                                 nokids = 0;
965                                 }
966                                 cx->op->o_tmpfree( save, cx->op->o_tmpmemctx );
967                                 if ( nokids ) ei->bei_state |= CACHE_ENTRY_NO_GRANDKIDS;
968                         }
969                         /* Make sure caller knows it had kids! */
970                         cx->tmp[0]=1;
971
972                         cx->rc = 0;
973                 } else {
974                         BDB_IDL_CPY( cx->ids, cx->tmp );
975                 }
976         }
977         return cx->rc;
978 }
979
980 int
981 hdb_dn2idl(
982         Operation       *op,
983         Entry           *e,
984         ID *ids,
985         ID *stack )
986 {
987         struct bdb_info *bdb = (struct bdb_info *)op->o_bd->be_private;
988         struct dn2id_cookie cx;
989
990         Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2idl(\"%s\")\n",
991                 e->e_nname.bv_val, 0, 0 );
992
993 #ifndef BDB_MULTIPLE_SUFFIXES
994         if ( op->ors_scope != LDAP_SCOPE_ONELEVEL && 
995                 BEI(e)->bei_parent->bei_id == 0 )
996         {
997                 BDB_IDL_ALL( bdb, ids );
998                 return 0;
999         }
1000 #endif
1001
1002         cx.id = e->e_id;
1003         BDB_ID2DISK( cx.id, &cx.nid );
1004         cx.ei = e->e_id ? BEI(e) : &bdb->bi_cache.c_dntree;
1005         cx.bdb = bdb;
1006         cx.db = cx.bdb->bi_dn2id->bdi_db;
1007         cx.prefix = op->ors_scope == LDAP_SCOPE_ONELEVEL
1008                 ? DN_ONE_PREFIX : DN_SUBTREE_PREFIX;
1009         cx.ids = ids;
1010         cx.buf = stack;
1011         cx.op = op;
1012
1013         BDB_IDL_ZERO( ids );
1014         if ( cx.prefix == DN_SUBTREE_PREFIX ) {
1015                 bdb_idl_insert( ids, cx.id );
1016         }
1017
1018         DBTzero(&cx.key);
1019         cx.key.ulen = sizeof(ID);
1020         cx.key.size = sizeof(ID);
1021         cx.key.flags = DB_DBT_USERMEM;
1022
1023         DBTzero(&cx.data);
1024
1025         return hdb_dn2idl_internal(&cx);
1026 }
1027 #endif  /* BDB_HIER */