]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/id2entry.c
ITS#7920 fix for slapacl
[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-2014 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         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
144         MDB_val key, data;
145         int rc = 0;
146
147         *e = NULL;
148
149         key.mv_data = &id;
150         key.mv_size = sizeof(ID);
151
152         /* fetch it */
153         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
154         if ( rc == MDB_NOTFOUND ) {
155                 /* Looking for root entry on an empty-dn suffix? */
156                 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
157                         struct berval gluebv = BER_BVC("glue");
158                         Entry *r = mdb_entry_alloc(op, 2, 4);
159                         Attribute *a = r->e_attrs;
160                         struct berval *bptr;
161
162                         r->e_id = 0;
163                         r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
164                         bptr = a->a_vals;
165                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
166                         a->a_desc = slap_schema.si_ad_objectClass;
167                         a->a_nvals = a->a_vals;
168                         a->a_numvals = 1;
169                         *bptr++ = gluebv;
170                         BER_BVZERO(bptr);
171                         bptr++;
172                         a->a_next = a+1;
173                         a = a->a_next;
174                         a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
175                         a->a_desc = slap_schema.si_ad_structuralObjectClass;
176                         a->a_vals = bptr;
177                         a->a_nvals = a->a_vals;
178                         a->a_numvals = 1;
179                         *bptr++ = gluebv;
180                         BER_BVZERO(bptr);
181                         a->a_next = NULL;
182                         *e = r;
183                         return MDB_SUCCESS;
184                 }
185         }
186         /* stubs from missing parents - DB is actually invalid */
187         if ( rc == MDB_SUCCESS && !data.mv_size )
188                 rc = MDB_NOTFOUND;
189         if ( rc ) return rc;
190
191         rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, e );
192         if ( rc ) return rc;
193
194         (*e)->e_id = id;
195         (*e)->e_name.bv_val = NULL;
196         (*e)->e_nname.bv_val = NULL;
197
198         return rc;
199 }
200
201 int mdb_id2entry_delete(
202         BackendDB *be,
203         MDB_txn *tid,
204         Entry *e )
205 {
206         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
207         MDB_dbi dbi = mdb->mi_id2entry;
208         MDB_val key;
209         int rc;
210
211         key.mv_data = &e->e_id;
212         key.mv_size = sizeof(ID);
213
214         /* delete from database */
215         rc = mdb_del( tid, dbi, &key, NULL );
216
217         return rc;
218 }
219
220 static Entry * mdb_entry_alloc(
221         Operation *op,
222         int nattrs,
223         int nvals )
224 {
225         Entry *e = op->o_tmpalloc( sizeof(Entry) +
226                 nattrs * sizeof(Attribute) +
227                 nvals * sizeof(struct berval), op->o_tmpmemctx );
228         BER_BVZERO(&e->e_bv);
229         e->e_private = e;
230         if (nattrs) {
231                 e->e_attrs = (Attribute *)(e+1);
232                 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
233         } else {
234                 e->e_attrs = NULL;
235         }
236
237         return e;
238 }
239
240 int mdb_entry_return(
241         Operation *op,
242         Entry *e
243 )
244 {
245         if ( !e )
246                 return 0;
247         if ( e->e_private ) {
248                 if ( op->o_hdr ) {
249                         op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
250                         op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
251                         op->o_tmpfree( e, op->o_tmpmemctx );
252                 } else {
253                         ch_free( e->e_nname.bv_val );
254                         ch_free( e->e_name.bv_val );
255                         ch_free( e );
256                 }
257         } else {
258                 entry_free( e );
259         }
260         return 0;
261 }
262
263 int mdb_entry_release(
264         Operation *op,
265         Entry *e,
266         int rw )
267 {
268         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
269         struct mdb_op_info *moi = NULL;
270         int rc;
271  
272         /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
273                         SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
274  
275         if ( slapMode & SLAP_SERVER_MODE ) {
276                 OpExtra *oex;
277                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
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         } else {
295                 mdb_entry_return( op, e );
296         }
297  
298         return 0;
299 }
300
301 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
302  */
303 int mdb_entry_get(
304         Operation *op,
305         struct berval *ndn,
306         ObjectClass *oc,
307         AttributeDescription *at,
308         int rw,
309         Entry **ent )
310 {
311         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
312         struct mdb_op_info *moi = NULL;
313         MDB_txn *txn = NULL;
314         Entry *e = NULL;
315         int     rc;
316         const char *at_name = at ? at->ad_cname.bv_val : "(null)";
317
318         Debug( LDAP_DEBUG_ARGS,
319                 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
320         Debug( LDAP_DEBUG_ARGS,
321                 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
322                 oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
323
324         rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
325         if ( rc )
326                 return LDAP_OTHER;
327         txn = moi->moi_txn;
328
329         /* can we find entry */
330         rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
331         switch( rc ) {
332         case MDB_NOTFOUND:
333         case 0:
334                 break;
335         default:
336                 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
337         }
338         if (e == NULL) {
339                 Debug( LDAP_DEBUG_ACL,
340                         "=> mdb_entry_get: cannot find entry: \"%s\"\n",
341                                 ndn->bv_val, 0, 0 ); 
342                 rc = LDAP_NO_SUCH_OBJECT;
343                 goto return_results;
344         }
345         
346         Debug( LDAP_DEBUG_ACL,
347                 "=> mdb_entry_get: found entry: \"%s\"\n",
348                 ndn->bv_val, 0, 0 ); 
349
350         if ( oc && !is_entry_objectclass( e, oc, 0 )) {
351                 Debug( LDAP_DEBUG_ACL,
352                         "<= mdb_entry_get: failed to find objectClass %s\n",
353                         oc->soc_cname.bv_val, 0, 0 ); 
354                 rc = LDAP_NO_SUCH_ATTRIBUTE;
355                 goto return_results;
356         }
357
358         /* NOTE: attr_find() or attrs_find()? */
359         if ( at && attr_find( e->e_attrs, at ) == NULL ) {
360                 Debug( LDAP_DEBUG_ACL,
361                         "<= mdb_entry_get: failed to find attribute %s\n",
362                         at->ad_cname.bv_val, 0, 0 ); 
363                 rc = LDAP_NO_SUCH_ATTRIBUTE;
364                 goto return_results;
365         }
366
367 return_results:
368         if( rc != LDAP_SUCCESS ) {
369                 /* free entry */
370                 mdb_entry_release( op, e, rw );
371         } else {
372                 *ent = e;
373         }
374
375         Debug( LDAP_DEBUG_TRACE,
376                 "mdb_entry_get: rc=%d\n",
377                 rc, 0, 0 ); 
378         return(rc);
379 }
380
381 static void
382 mdb_reader_free( void *key, void *data )
383 {
384         MDB_txn *txn = data;
385
386         if ( txn ) mdb_txn_abort( txn );
387 }
388
389 /* free up any keys used by the main thread */
390 void
391 mdb_reader_flush( MDB_env *env )
392 {
393         void *data;
394         void *ctx = ldap_pvt_thread_pool_context();
395
396         if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
397                 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
398                 mdb_reader_free( env, data );
399         }
400 }
401
402 extern MDB_txn *mdb_tool_txn;
403
404 int
405 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
406 {
407         int rc, renew = 0;
408         void *data;
409         void *ctx;
410         mdb_op_info *moi = NULL;
411         OpExtra *oex;
412
413         assert( op != NULL );
414
415         if ( !mdb || !moip ) return -1;
416
417         /* If no op was provided, try to find the ctx anyway... */
418         if ( op ) {
419                 ctx = op->o_threadctx;
420         } else {
421                 ctx = ldap_pvt_thread_pool_context();
422         }
423
424         if ( op ) {
425                 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
426                         if ( oex->oe_key == mdb ) break;
427                 }
428                 moi = (mdb_op_info *)oex;
429         }
430
431         if ( !moi ) {
432                 moi = *moip;
433
434                 if ( !moi ) {
435                         if ( op ) {
436                                 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
437                         } else {
438                                 moi = ch_malloc(sizeof(mdb_op_info));
439                         }
440                         moi->moi_flag = MOI_FREEIT;
441                         *moip = moi;
442                 }
443                 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
444                 moi->moi_oe.oe_key = mdb;
445                 moi->moi_ref = 0;
446                 moi->moi_txn = NULL;
447         }
448
449         if ( !rdonly ) {
450                 /* This op started as a reader, but now wants to write. */
451                 if ( moi->moi_flag & MOI_READER ) {
452                         moi = *moip;
453                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
454                 } else {
455                 /* This op is continuing an existing write txn */
456                         *moip = moi;
457                 }
458                 moi->moi_ref++;
459                 if ( !moi->moi_txn ) {
460                         if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
461                                 moi->moi_txn = mdb_tool_txn;
462                         } else {
463                                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
464                                 if (rc) {
465                                         Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
466                                                 mdb_strerror(rc), rc, 0 );
467                                 }
468                                 return rc;
469                         }
470                 }
471                 return 0;
472         }
473
474         /* OK, this is a reader */
475         if ( !moi->moi_txn ) {
476                 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
477                         moi->moi_txn = mdb_tool_txn;
478                         goto ok;
479                 }
480                 if ( !ctx ) {
481                         /* Shouldn't happen unless we're single-threaded */
482                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
483                         if (rc) {
484                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
485                                         mdb_strerror(rc), rc, 0 );
486                         }
487                         return rc;
488                 }
489                 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
490                         rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
491                         if (rc) {
492                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
493                                         mdb_strerror(rc), rc, 0 );
494                                 return rc;
495                         }
496                         data = moi->moi_txn;
497                         if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
498                                 data, mdb_reader_free, NULL, NULL ) ) ) {
499                                 mdb_txn_abort( moi->moi_txn );
500                                 moi->moi_txn = NULL;
501                                 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
502                                         rc, 0, 0 );
503                                 return rc;
504                         }
505                 } else {
506                         moi->moi_txn = data;
507                         renew = 1;
508                 }
509                 moi->moi_flag |= MOI_READER;
510         }
511 ok:
512         if ( moi->moi_ref < 1 ) {
513                 moi->moi_ref = 0;
514         }
515         if ( renew ) {
516                 rc = mdb_txn_renew( moi->moi_txn );
517                 assert(!rc);
518         }
519         moi->moi_ref++;
520         if ( *moip != moi )
521                 *moip = moi;
522
523         return 0;
524 }
525
526 /* Count up the sizes of the components of an entry */
527 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
528         Ecount *eh)
529 {
530         ber_len_t len;
531         int i, nat = 0, nval = 0, nnval = 0;
532         Attribute *a;
533
534         len = 4*sizeof(int);    /* nattrs, nvals, ocflags, offset */
535         for (a=e->e_attrs; a; a=a->a_next) {
536                 /* For AttributeDesc, we only store the attr index */
537                 nat++;
538                 if (a->a_desc->ad_index >= MDB_MAXADS) {
539                         Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
540                                 0, 0, 0 );
541                         return LDAP_OTHER;
542                 }
543                 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
544                         int rc = mdb_ad_get(mdb, txn, a->a_desc);
545                         if (rc)
546                                 return rc;
547                 }
548                 len += 2*sizeof(int);   /* AD index, numvals */
549                 nval += a->a_numvals + 1;       /* empty berval at end */
550                 for (i=0; i<a->a_numvals; i++) {
551                         len += a->a_vals[i].bv_len + 1 + sizeof(int);   /* len */
552                 }
553                 if (a->a_nvals != a->a_vals) {
554                         nval += a->a_numvals + 1;
555                         nnval++;
556                         for (i=0; i<a->a_numvals; i++) {
557                                 len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
558                         }
559                 }
560         }
561         /* padding */
562         len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
563         eh->len = len;
564         eh->nattrs = nat;
565         eh->nvals = nval;
566         eh->offset = nat + nval - nnval;
567         return 0;
568 }
569
570 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
571
572 /* Flatten an Entry into a buffer. The buffer starts with the count of the
573  * number of attributes in the entry, the total number of values in the
574  * entry, and the e_ocflags. It then contains a list of integers for each
575  * attribute. For each attribute the first integer gives the index of the
576  * matching AttributeDescription, followed by the number of values in the
577  * attribute. If the high bit of the attr index is set, the attribute's
578  * values are already sorted.
579  * If the high bit of numvals is set, the attribute also has normalized
580  * values present. (Note - a_numvals is an unsigned int, so this means
581  * it's possible to receive an attribute that we can't encode due to size
582  * overflow. In practice, this should not be an issue.) Then the length
583  * of each value is listed. If there are normalized values, their lengths
584  * come next. This continues for each attribute. After all of the lengths
585  * for the last attribute, the actual values are copied, with a NUL
586  * terminator after each value. The buffer is padded to the sizeof(ID).
587  * The entire buffer size is precomputed so that a single malloc can be
588  * performed.
589  */
590 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
591 {
592         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
593         ber_len_t len, i;
594         int rc;
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         AttributeDescription *ad;
668         unsigned int *lp = (unsigned int *)data->mv_data;
669         unsigned char *ptr;
670         BerVarray bptr;
671
672         Debug( LDAP_DEBUG_TRACE,
673                 "=> mdb_entry_decode:\n",
674                 0, 0, 0 );
675
676         nattrs = *lp++;
677         nvals = *lp++;
678         x = mdb_entry_alloc(op, nattrs, nvals);
679         x->e_ocflags = *lp++;
680         if (!nvals) {
681                 goto done;
682         }
683         a = x->e_attrs;
684         bptr = a->a_vals;
685         i = *lp++;
686         ptr = (unsigned char *)(lp + i);
687
688         for (;nattrs>0; nattrs--) {
689                 int have_nval = 0;
690                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
691                 i = *lp++;
692                 if (i & HIGH_BIT) {
693                         i ^= HIGH_BIT;
694                         a->a_flags |= SLAP_ATTR_SORTED_VALS;
695                 }
696                 if (i > mdb->mi_numads) {
697                         rc = mdb_ad_read(mdb, txn);
698                         if (rc)
699                                 return rc;
700                         if (i > mdb->mi_numads) {
701                                 Debug( LDAP_DEBUG_ANY,
702                                         "mdb_entry_decode: attribute index %d not recognized\n",
703                                         i, 0, 0 );
704                                 return LDAP_OTHER;
705                         }
706                 }
707                 a->a_desc = mdb->mi_ads[i];
708                 a->a_numvals = *lp++;
709                 if (a->a_numvals & HIGH_BIT) {
710                         a->a_numvals ^= HIGH_BIT;
711                         have_nval = 1;
712                 }
713                 a->a_vals = bptr;
714                 for (i=0; i<a->a_numvals; i++) {
715                         bptr->bv_len = *lp++;;
716                         bptr->bv_val = (char *)ptr;
717                         ptr += bptr->bv_len+1;
718                         bptr++;
719                 }
720                 bptr->bv_val = NULL;
721                 bptr->bv_len = 0;
722                 bptr++;
723
724                 if (have_nval) {
725                         a->a_nvals = bptr;
726                         for (i=0; i<a->a_numvals; i++) {
727                                 bptr->bv_len = *lp++;
728                                 bptr->bv_val = (char *)ptr;
729                                 ptr += bptr->bv_len+1;
730                                 bptr++;
731                         }
732                         bptr->bv_val = NULL;
733                         bptr->bv_len = 0;
734                         bptr++;
735                 } else {
736                         a->a_nvals = a->a_vals;
737                 }
738                 /* FIXME: This is redundant once a sorted entry is saved into the DB */
739                 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
740                         && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
741                         rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
742                         if ( rc == LDAP_SUCCESS ) {
743                                 a->a_flags |= SLAP_ATTR_SORTED_VALS;
744                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
745                                 /* should never happen */
746                                 Debug( LDAP_DEBUG_ANY,
747                                         "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
748                                         a->a_desc->ad_cname.bv_val, j, 0 );
749                                 return rc;
750                         }
751                 }
752                 a->a_next = a+1;
753                 a = a->a_next;
754         }
755         a[-1].a_next = NULL;
756 done:
757
758         Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",
759                 0, 0, 0 );
760         *e = x;
761         return 0;
762 }