]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
Happy New Year
[openldap] / servers / slapd / back-mdb / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2018 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 #include <ac/errno.h>
22
23 #include "back-mdb.h"
24
25 typedef struct Ecount {
26         ber_len_t len;
27         int nattrs;
28         int nvals;
29         int offset;
30 } Ecount;
31
32 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
33         Ecount *eh);
34 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
35         Ecount *ec);
36 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
37
38 #define ADD_FLAGS       (MDB_NOOVERWRITE|MDB_APPEND)
39
40 static int mdb_id2entry_put(
41         Operation *op,
42         MDB_txn *txn,
43         MDB_cursor *mc,
44         Entry *e,
45         int flag )
46 {
47         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
48         Ecount ec;
49         MDB_val key, data;
50         int rc;
51
52         /* We only store rdns, and they go in the dn2id database. */
53
54         key.mv_data = &e->e_id;
55         key.mv_size = sizeof(ID);
56
57         rc = mdb_entry_partsize( mdb, txn, e, &ec );
58         if (rc)
59                 return LDAP_OTHER;
60
61         flag |= MDB_RESERVE;
62
63         if (e->e_id < mdb->mi_nextid)
64                 flag &= ~MDB_APPEND;
65
66 again:
67         data.mv_size = ec.len;
68         if ( mc )
69                 rc = mdb_cursor_put( mc, &key, &data, flag );
70         else
71                 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
72         if (rc == MDB_SUCCESS) {
73                 rc = mdb_entry_encode( op, e, &data, &ec );
74                 if( rc != LDAP_SUCCESS )
75                         return rc;
76         }
77         if (rc) {
78                 /* Was there a hole from slapadd? */
79                 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
80                         flag &= ~ADD_FLAGS;
81                         goto again;
82                 }
83                 Debug( LDAP_DEBUG_ANY,
84                         "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
85                         mdb_strerror(rc), rc,
86                         e->e_nname.bv_val );
87                 if ( rc != MDB_KEYEXIST )
88                         rc = LDAP_OTHER;
89         }
90         return rc;
91 }
92
93 /*
94  * This routine adds (or updates) an entry on disk.
95  * The cache should be already be updated.
96  */
97
98
99 int mdb_id2entry_add(
100         Operation *op,
101         MDB_txn *txn,
102         MDB_cursor *mc,
103         Entry *e )
104 {
105         return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
106 }
107
108 int mdb_id2entry_update(
109         Operation *op,
110         MDB_txn *txn,
111         MDB_cursor *mc,
112         Entry *e )
113 {
114         return mdb_id2entry_put(op, txn, mc, e, 0);
115 }
116
117 int mdb_id2edata(
118         Operation *op,
119         MDB_cursor *mc,
120         ID id,
121         MDB_val *data )
122 {
123         MDB_val key;
124         int rc;
125
126         key.mv_data = &id;
127         key.mv_size = sizeof(ID);
128
129         /* fetch it */
130         rc = mdb_cursor_get( mc, &key, data, MDB_SET );
131         /* stubs from missing parents - DB is actually invalid */
132         if ( rc == MDB_SUCCESS && !data->mv_size )
133                 rc = MDB_NOTFOUND;
134         return rc;
135 }
136
137 int mdb_id2entry(
138         Operation *op,
139         MDB_cursor *mc,
140         ID id,
141         Entry **e )
142 {
143         MDB_val key, data;
144         int rc = 0;
145
146         *e = NULL;
147
148         key.mv_data = &id;
149         key.mv_size = sizeof(ID);
150
151         /* fetch it */
152         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
153         if ( rc == MDB_NOTFOUND ) {
154                 /* Looking for root entry on an empty-dn suffix? */
155                 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
156                         struct berval gluebv = BER_BVC("glue");
157                         Entry *r = mdb_entry_alloc(op, 2, 4);
158                         Attribute *a = r->e_attrs;
159                         struct berval *bptr;
160
161                         r->e_id = 0;
162                         r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
163                         bptr = a->a_vals;
164                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
165                         a->a_desc = slap_schema.si_ad_objectClass;
166                         a->a_nvals = a->a_vals;
167                         a->a_numvals = 1;
168                         *bptr++ = gluebv;
169                         BER_BVZERO(bptr);
170                         bptr++;
171                         a->a_next = a+1;
172                         a = a->a_next;
173                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
174                         a->a_desc = slap_schema.si_ad_structuralObjectClass;
175                         a->a_vals = bptr;
176                         a->a_nvals = a->a_vals;
177                         a->a_numvals = 1;
178                         *bptr++ = gluebv;
179                         BER_BVZERO(bptr);
180                         a->a_next = NULL;
181                         *e = r;
182                         return MDB_SUCCESS;
183                 }
184         }
185         /* stubs from missing parents - DB is actually invalid */
186         if ( rc == MDB_SUCCESS && !data.mv_size )
187                 rc = MDB_NOTFOUND;
188         if ( rc ) return rc;
189
190         rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, e );
191         if ( rc ) return rc;
192
193         (*e)->e_id = id;
194         (*e)->e_name.bv_val = NULL;
195         (*e)->e_nname.bv_val = NULL;
196
197         return rc;
198 }
199
200 int mdb_id2entry_delete(
201         BackendDB *be,
202         MDB_txn *tid,
203         Entry *e )
204 {
205         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
206         MDB_dbi dbi = mdb->mi_id2entry;
207         MDB_val key;
208         int rc;
209
210         key.mv_data = &e->e_id;
211         key.mv_size = sizeof(ID);
212
213         /* delete from database */
214         rc = mdb_del( tid, dbi, &key, NULL );
215
216         return rc;
217 }
218
219 static Entry * mdb_entry_alloc(
220         Operation *op,
221         int nattrs,
222         int nvals )
223 {
224         Entry *e = op->o_tmpalloc( sizeof(Entry) +
225                 nattrs * sizeof(Attribute) +
226                 nvals * sizeof(struct berval), op->o_tmpmemctx );
227         BER_BVZERO(&e->e_bv);
228         e->e_private = e;
229         if (nattrs) {
230                 e->e_attrs = (Attribute *)(e+1);
231                 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
232         } else {
233                 e->e_attrs = NULL;
234         }
235
236         return e;
237 }
238
239 int mdb_entry_return(
240         Operation *op,
241         Entry *e
242 )
243 {
244         if ( !e )
245                 return 0;
246         if ( e->e_private ) {
247                 if ( op->o_hdr ) {
248                         op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
249                         op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
250                         op->o_tmpfree( e, op->o_tmpmemctx );
251                 } else {
252                         ch_free( e->e_nname.bv_val );
253                         ch_free( e->e_name.bv_val );
254                         ch_free( e );
255                 }
256         } else {
257                 entry_free( e );
258         }
259         return 0;
260 }
261
262 int mdb_entry_release(
263         Operation *op,
264         Entry *e,
265         int rw )
266 {
267         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
268         struct mdb_op_info *moi = NULL;
269  
270         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
271                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
272  
273         int release = 1;
274         if ( slapMode & SLAP_SERVER_MODE ) {
275                 OpExtra *oex;
276                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
277                         release = 0;
278                         if ( oex->oe_key == mdb ) {
279                                 mdb_entry_return( op, e );
280                                 moi = (mdb_op_info *)oex;
281                                 /* If it was setup by entry_get we should probably free it */
282                                 if ( moi->moi_flag & MOI_FREEIT ) {
283                                         moi->moi_ref--;
284                                         if ( moi->moi_ref < 1 ) {
285                                                 mdb_txn_reset( moi->moi_txn );
286                                                 moi->moi_ref = 0;
287                                                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
288                                                 op->o_tmpfree( moi, op->o_tmpmemctx );
289                                         }
290                                 }
291                                 break;
292                         }
293                 }
294         }
295
296         if (release)
297                 mdb_entry_return( op, e );
298  
299         return 0;
300 }
301
302 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
303  */
304 int mdb_entry_get(
305         Operation *op,
306         struct berval *ndn,
307         ObjectClass *oc,
308         AttributeDescription *at,
309         int rw,
310         Entry **ent )
311 {
312         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
313         struct mdb_op_info *moi = NULL;
314         MDB_txn *txn = NULL;
315         Entry *e = NULL;
316         int     rc;
317         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
318
319         Debug( LDAP_DEBUG_ARGS,
320                 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
321         Debug( LDAP_DEBUG_ARGS,
322                 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
323                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
324
325         rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
326         if ( rc )
327                 return LDAP_OTHER;
328         txn = moi->moi_txn;
329
330         /* can we find entry */
331         rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
332         switch( rc ) {
333         case MDB_NOTFOUND:
334         case 0:
335                 break;
336         default:
337                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
338         }
339         if (e == NULL) {
340                 Debug( LDAP_DEBUG_ACL,
341                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
342                                 ndn->bv_val, 0, 0 ); 
343                 rc = LDAP_NO_SUCH_OBJECT;
344                 goto return_results;
345         }
346         
347         Debug( LDAP_DEBUG_ACL,
348                 "=> mdb_entry_get: found entry: \"%s\"\n",
349                 ndn->bv_val, 0, 0 ); 
350
351         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
352                 Debug( LDAP_DEBUG_ACL,
353                         "<= mdb_entry_get: failed to find objectClass %s\n",
354                         oc->soc_cname.bv_val, 0, 0 ); 
355                 rc = LDAP_NO_SUCH_ATTRIBUTE;
356                 goto return_results;
357         }
358
359         /* NOTE: attr_find() or attrs_find()? */
360         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
361                 Debug( LDAP_DEBUG_ACL,
362                         "<= mdb_entry_get: failed to find attribute %s\n",
363                         at->ad_cname.bv_val, 0, 0 ); 
364                 rc = LDAP_NO_SUCH_ATTRIBUTE;
365                 goto return_results;
366         }
367
368 return_results:
369         if( rc != LDAP_SUCCESS ) {
370                 /* free entry */
371                 mdb_entry_release( op, e, rw );
372         } else {
373                 *ent = e;
374         }
375
376         Debug( LDAP_DEBUG_TRACE,
377                 "mdb_entry_get: rc=%d\n",
378                 rc, 0, 0 ); 
379         return(rc);
380 }
381
382 static void
383 mdb_reader_free( void *key, void *data )
384 {
385         MDB_txn *txn = data;
386
387         if ( txn ) mdb_txn_abort( txn );
388 }
389
390 /* free up any keys used by the main thread */
391 void
392 mdb_reader_flush( MDB_env *env )
393 {
394         void *data;
395         void *ctx = ldap_pvt_thread_pool_context();
396
397         if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
398                 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
399                 mdb_reader_free( env, data );
400         }
401 }
402
403 extern MDB_txn *mdb_tool_txn;
404
405 int
406 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
407 {
408         int rc, renew = 0;
409         void *data;
410         void *ctx;
411         mdb_op_info *moi = NULL;
412         OpExtra *oex;
413
414         assert( op != NULL );
415
416         if ( !mdb || !moip ) return -1;
417
418         /* If no op was provided, try to find the ctx anyway... */
419         if ( op ) {
420                 ctx = op->o_threadctx;
421         } else {
422                 ctx = ldap_pvt_thread_pool_context();
423         }
424
425         if ( op ) {
426                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
427                         if ( oex->oe_key == mdb ) break;
428                 }
429                 moi = (mdb_op_info *)oex;
430         }
431
432         if ( !moi ) {
433                 moi = *moip;
434
435                 if ( !moi ) {
436                         if ( op ) {
437                                 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
438                         } else {
439                                 moi = ch_malloc(sizeof(mdb_op_info));
440                         }
441                         moi->moi_flag = MOI_FREEIT;
442                         *moip = moi;
443                 }
444                 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
445                 moi->moi_oe.oe_key = mdb;
446                 moi->moi_ref = 0;
447                 moi->moi_txn = NULL;
448         }
449
450         if ( !rdonly ) {
451                 /* This op started as a reader, but now wants to write. */
452                 if ( moi->moi_flag & MOI_READER ) {
453                         moi = *moip;
454                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
455                 } else {
456                 /* This op is continuing an existing write txn */
457                         *moip = moi;
458                 }
459                 moi->moi_ref++;
460                 if ( !moi->moi_txn ) {
461                         if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
462                                 moi->moi_txn = mdb_tool_txn;
463                         } else {
464                                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
465                                 if (rc) {
466                                         Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
467                                                 mdb_strerror(rc), rc, 0 );
468                                 }
469                                 return rc;
470                         }
471                 }
472                 return 0;
473         }
474
475         /* OK, this is a reader */
476         if ( !moi->moi_txn ) {
477                 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
478                         moi->moi_txn = mdb_tool_txn;
479                         goto ok;
480                 }
481                 if ( !ctx ) {
482                         /* Shouldn't happen unless we're single-threaded */
483                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
484                         if (rc) {
485                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
486                                         mdb_strerror(rc), rc, 0 );
487                         }
488                         return rc;
489                 }
490                 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
491                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
492                         if (rc) {
493                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
494                                         mdb_strerror(rc), rc, 0 );
495                                 return rc;
496                         }
497                         data = moi->moi_txn;
498                         if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
499                                 data, mdb_reader_free, NULL, NULL ) ) ) {
500                                 mdb_txn_abort( moi->moi_txn );
501                                 moi->moi_txn = NULL;
502                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
503                                         rc, 0, 0 );
504                                 return rc;
505                         }
506                 } else {
507                         moi->moi_txn = data;
508                         renew = 1;
509                 }
510                 moi->moi_flag |= MOI_READER;
511         }
512 ok:
513         if ( moi->moi_ref < 1 ) {
514                 moi->moi_ref = 0;
515         }
516         if ( renew ) {
517                 rc = mdb_txn_renew( moi->moi_txn );
518                 assert(!rc);
519         }
520         moi->moi_ref++;
521         if ( *moip != moi )
522                 *moip = moi;
523
524         return 0;
525 }
526
527 /* Count up the sizes of the components of an entry */
528 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
529         Ecount *eh)
530 {
531         ber_len_t len;
532         int i, nat = 0, nval = 0, nnval = 0;
533         Attribute *a;
534
535         len = 4*sizeof(int);    /* nattrs, nvals, ocflags, offset */
536         for (a=e->e_attrs; a; a=a->a_next) {
537                 /* For AttributeDesc, we only store the attr index */
538                 nat++;
539                 if (a->a_desc->ad_index >= MDB_MAXADS) {
540                         Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
541                                 0, 0, 0 );
542                         return LDAP_OTHER;
543                 }
544                 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
545                         int rc = mdb_ad_get(mdb, txn, a->a_desc);
546                         if (rc)
547                                 return rc;
548                 }
549                 len += 2*sizeof(int);   /* AD index, numvals */
550                 nval += a->a_numvals + 1;       /* empty berval at end */
551                 for (i=0; i<a->a_numvals; i++) {
552                         len += a->a_vals[i].bv_len + 1 + sizeof(int);   /* len */
553                 }
554                 if (a->a_nvals != a->a_vals) {
555                         nval += a->a_numvals + 1;
556                         nnval++;
557                         for (i=0; i<a->a_numvals; i++) {
558                                 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
559                         }
560                 }
561         }
562         /* padding */
563         len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
564         eh->len = len;
565         eh->nattrs = nat;
566         eh->nvals = nval;
567         eh->offset = nat + nval - nnval;
568         return 0;
569 }
570
571 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
572
573 /* Flatten an Entry into a buffer. The buffer starts with the count of the
574  * number of attributes in the entry, the total number of values in the
575  * entry, and the e_ocflags. It then contains a list of integers for each
576  * attribute. For each attribute the first integer gives the index of the
577  * matching AttributeDescription, followed by the number of values in the
578  * attribute. If the high bit of the attr index is set, the attribute's
579  * values are already sorted.
580  * If the high bit of numvals is set, the attribute also has normalized
581  * values present. (Note - a_numvals is an unsigned int, so this means
582  * it's possible to receive an attribute that we can't encode due to size
583  * overflow. In practice, this should not be an issue.) Then the length
584  * of each value is listed. If there are normalized values, their lengths
585  * come next. This continues for each attribute. After all of the lengths
586  * for the last attribute, the actual values are copied, with a NUL
587  * terminator after each value. The buffer is padded to the sizeof(ID).
588  * The entire buffer size is precomputed so that a single malloc can be
589  * performed.
590  */
591 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
592 {
593         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
594         ber_len_t i;
595         Attribute *a;
596         unsigned char *ptr;
597         unsigned int *lp, l;
598
599         Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
600                 (long) e->e_id, e->e_dn, 0 );
601
602         /* make sure e->e_ocflags is set */
603         if (is_entry_referral(e))
604                 ;       /* empty */
605
606         lp = (unsigned int *)data->mv_data;
607         *lp++ = eh->nattrs;
608         *lp++ = eh->nvals;
609         *lp++ = (unsigned int)e->e_ocflags;
610         *lp++ = eh->offset;
611         ptr = (unsigned char *)(lp + eh->offset);
612
613         for (a=e->e_attrs; a; a=a->a_next) {
614                 if (!a->a_desc->ad_index)
615                         return LDAP_UNDEFINED_TYPE;
616                 l = mdb->mi_adxs[a->a_desc->ad_index];
617                 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
618                         l |= HIGH_BIT;
619                 *lp++ = l;
620                 l = a->a_numvals;
621                 if (a->a_nvals != a->a_vals)
622                         l |= HIGH_BIT;
623                 *lp++ = l;
624                 if (a->a_vals) {
625                         for (i=0; a->a_vals[i].bv_val; i++);
626                         assert( i == a->a_numvals );
627                         for (i=0; i<a->a_numvals; i++) {
628                                 *lp++ = a->a_vals[i].bv_len;
629                                 memcpy(ptr, a->a_vals[i].bv_val,
630                                         a->a_vals[i].bv_len);
631                                 ptr += a->a_vals[i].bv_len;
632                                 *ptr++ = '\0';
633                         }
634                         if (a->a_nvals != a->a_vals) {
635                                 for (i=0; i<a->a_numvals; i++) {
636                                         *lp++ = a->a_nvals[i].bv_len;
637                                         memcpy(ptr, a->a_nvals[i].bv_val,
638                                                 a->a_nvals[i].bv_len);
639                                         ptr += a->a_nvals[i].bv_len;
640                                         *ptr++ = '\0';
641                                 }
642                         }
643                 }
644         }
645
646         Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
647                 (long) e->e_id, e->e_dn, 0 );
648
649         return 0;
650 }
651
652 /* Retrieve an Entry that was stored using entry_encode above.
653  *
654  * Note: everything is stored in a single contiguous block, so
655  * you can not free individual attributes or names from this
656  * structure. Attempting to do so will likely corrupt memory.
657  */
658
659 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, Entry **e)
660 {
661         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
662         int i, j, nattrs, nvals;
663         int rc;
664         Attribute *a;
665         Entry *x;
666         const char *text;
667         unsigned int *lp = (unsigned int *)data->mv_data;
668         unsigned char *ptr;
669         BerVarray bptr;
670
671         Debug( LDAP_DEBUG_TRACE,
672                 "=> mdb_entry_decode:\n",
673                 0, 0, 0 );
674
675         nattrs = *lp++;
676         nvals = *lp++;
677         x = mdb_entry_alloc(op, nattrs, nvals);
678         x->e_ocflags = *lp++;
679         if (!nvals) {
680                 goto done;
681         }
682         a = x->e_attrs;
683         bptr = a->a_vals;
684         i = *lp++;
685         ptr = (unsigned char *)(lp + i);
686
687         for (;nattrs>0; nattrs--) {
688                 int have_nval = 0;
689                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
690                 i = *lp++;
691                 if (i & HIGH_BIT) {
692                         i ^= HIGH_BIT;
693                         a->a_flags |= SLAP_ATTR_SORTED_VALS;
694                 }
695                 if (i > mdb->mi_numads) {
696                         rc = mdb_ad_read(mdb, txn);
697                         if (rc)
698                                 return rc;
699                         if (i > mdb->mi_numads) {
700                                 Debug( LDAP_DEBUG_ANY,
701                                         "mdb_entry_decode: attribute index %d not recognized\n",
702                                         i, 0, 0 );
703                                 return LDAP_OTHER;
704                         }
705                 }
706                 a->a_desc = mdb->mi_ads[i];
707                 a->a_numvals = *lp++;
708                 if (a->a_numvals & HIGH_BIT) {
709                         a->a_numvals ^= HIGH_BIT;
710                         have_nval = 1;
711                 }
712                 a->a_vals = bptr;
713                 for (i=0; i<a->a_numvals; i++) {
714                         bptr->bv_len = *lp++;;
715                         bptr->bv_val = (char *)ptr;
716                         ptr += bptr->bv_len+1;
717                         bptr++;
718                 }
719                 bptr->bv_val = NULL;
720                 bptr->bv_len = 0;
721                 bptr++;
722
723                 if (have_nval) {
724                         a->a_nvals = bptr;
725                         for (i=0; i<a->a_numvals; i++) {
726                                 bptr->bv_len = *lp++;
727                                 bptr->bv_val = (char *)ptr;
728                                 ptr += bptr->bv_len+1;
729                                 bptr++;
730                         }
731                         bptr->bv_val = NULL;
732                         bptr->bv_len = 0;
733                         bptr++;
734                 } else {
735                         a->a_nvals = a->a_vals;
736                 }
737                 /* FIXME: This is redundant once a sorted entry is saved into the DB */
738                 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
739                         && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
740                         rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
741                         if ( rc == LDAP_SUCCESS ) {
742                                 a->a_flags |= SLAP_ATTR_SORTED_VALS;
743                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
744                                 /* should never happen */
745                                 Debug( LDAP_DEBUG_ANY,
746                                         "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
747                                         a->a_desc->ad_cname.bv_val, j, 0 );
748                                 return rc;
749                         }
750                 }
751                 a->a_next = a+1;
752                 a = a->a_next;
753         }
754         a[-1].a_next = NULL;
755 done:
756
757         Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",
758                 0, 0, 0 );
759         *e = x;
760         return 0;
761 }