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