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