]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
c2fd04ed5926fbc60966b5c9b097a9aee3b25bd2
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 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 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/errno.h>
33 #include <ac/socket.h>
34 #include <ac/string.h>
35
36 #include "slap.h"
37 #include "ldif.h"
38
39 static char             *ebuf;  /* buf returned by entry2str             */
40 static char             *ecur;  /* pointer to end of currently used ebuf */
41 static int              emaxsize;/* max size of ebuf                     */
42
43 /*
44  * Empty root entry
45  */
46 const Entry slap_entry_root = {
47         NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
48 };
49
50 /*
51  * these mutexes must be used when calling the entry2str()
52  * routine since it returns a pointer to static data.
53  */
54 ldap_pvt_thread_mutex_t entry2str_mutex;
55
56 static const struct berval dn_bv = BER_BVC("dn");
57
58 /*
59  * Entry free list
60  *
61  * Allocate in chunks, minimum of 1000 at a time.
62  */
63 #define CHUNK_SIZE      1000
64 typedef struct slap_list {
65         struct slap_list *next;
66 } slap_list;
67 static slap_list *entry_chunks;
68 static Entry *entry_list;
69 static ldap_pvt_thread_mutex_t entry_mutex;
70
71 int entry_destroy(void)
72 {
73         slap_list *e;
74         if ( ebuf ) free( ebuf );
75         ebuf = NULL;
76         ecur = NULL;
77         emaxsize = 0;
78
79         for ( e=entry_chunks; e; e=entry_chunks ) {
80                 entry_chunks = e->next;
81                 free( e );
82         }
83
84         ldap_pvt_thread_mutex_destroy( &entry_mutex );
85         ldap_pvt_thread_mutex_destroy( &entry2str_mutex );
86         return attr_destroy();
87 }
88
89 int
90 entry_init(void)
91 {
92         ldap_pvt_thread_mutex_init( &entry2str_mutex );
93         ldap_pvt_thread_mutex_init( &entry_mutex );
94         return attr_init();
95 }
96
97 Entry *
98 str2entry( char *s )
99 {
100         return str2entry2( s, 1 );
101 }
102
103 Entry *
104 str2entry2( char *s, int checkvals )
105 {
106         int rc;
107         Entry           *e;
108         struct berval   *type, *vals, *nvals;
109         char    *freeval;
110         AttributeDescription *ad, *ad_prev;
111         const char *text;
112         char    *next;
113         int             attr_cnt;
114         int             i, lines;
115         Attribute       ahead, *atail;
116
117         /*
118          * LDIF is used as the string format.
119          * An entry looks like this:
120          *
121          *      dn: <dn>\n
122          *      [<attr>:[:] <value>\n]
123          *      [<tab><continuedvalue>\n]*
124          *      ...
125          *
126          * If a double colon is used after a type, it means the
127          * following value is encoded as a base 64 string.  This
128          * happens if the value contains a non-printing character
129          * or newline.
130          */
131
132         Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
133                 s ? s : "NULL", 0, 0 );
134
135         e = entry_alloc();
136
137         if( e == NULL ) {
138                 Debug( LDAP_DEBUG_ANY,
139                         "<= str2entry NULL (entry allocation failed)\n",
140                         0, 0, 0 );
141                 return( NULL );
142         }
143
144         /* initialize entry */
145         e->e_id = NOID;
146
147         /* dn + attributes */
148         atail = &ahead;
149         ahead.a_next = NULL;
150         ad = NULL;
151         ad_prev = NULL;
152         attr_cnt = 0;
153         next = s;
154
155         lines = ldif_countlines( s );
156         type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
157         vals = type+lines+1;
158         nvals = vals+lines+1;
159         freeval = (char *)(nvals+lines+1);
160         i = -1;
161
162         /* parse into individual values, record DN */
163         while ( (s = ldif_getline( &next )) != NULL ) {
164                 int freev;
165                 if ( *s == '\n' || *s == '\0' ) {
166                         break;
167                 }
168                 i++;
169                 if (i >= lines) {
170                         Debug( LDAP_DEBUG_TRACE,
171                                 "<= str2entry ran past end of entry\n", 0, 0, 0 );
172                         goto fail;
173                 }
174
175                 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
176                 freeval[i] = freev;
177                 if ( rc ) {
178                         Debug( LDAP_DEBUG_TRACE,
179                                 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
180                         continue;
181                 }
182
183                 if ( type[i].bv_len == dn_bv.bv_len &&
184                         strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
185
186                         if ( e->e_dn != NULL ) {
187                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
188                                         "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
189                                         (long) e->e_id, e->e_dn, vals[i].bv_val );
190                                 goto fail;
191                         }
192
193                         rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
194                         if( rc != LDAP_SUCCESS ) {
195                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
196                                         "entry %ld has invalid DN \"%s\"\n",
197                                         (long) e->e_id, vals[i].bv_val, 0 );
198                                 goto fail;
199                         }
200                         if ( freeval[i] ) free( vals[i].bv_val );
201                         vals[i].bv_val = NULL;
202                         i--;
203                         continue;
204                 }
205         }
206         lines = i+1;
207
208         /* check to make sure there was a dn: line */
209         if ( BER_BVISNULL( &e->e_name )) {
210                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
211                         (long) e->e_id, 0, 0 );
212                 goto fail;
213         }
214
215 #define bvcasematch(bv1, bv2)   ( ((bv1)->bv_len == (bv2)->bv_len) && (strncasecmp((bv1)->bv_val, (bv2)->bv_val, (bv1)->bv_len) == 0) )
216
217         /* Make sure all attributes with multiple values are contiguous */
218         if ( checkvals ) {
219                 int j, k;
220                 struct berval bv;
221                 int fv;
222
223                 for (i=0; i<lines; i++) {
224                         for ( j=i+1; j<lines; j++ ) {
225                                 if ( bvcasematch( type+i, type+j )) {
226                                         /* out of order, move intervening attributes down */
227                                         if ( j != i+1 ) {
228                                                 bv = vals[j];
229                                                 fv = freeval[j];
230                                                 for ( k=j; k>i; k-- ) {
231                                                         type[k] = type[k-1];
232                                                         vals[k] = vals[k-1];
233                                                         freeval[k] = freeval[k-1];
234                                                 }
235                                                 k++;
236                                                 type[k] = type[i];
237                                                 vals[k] = bv;
238                                                 freeval[k] = fv;
239                                         }
240                                         i++;
241                                 }
242                         }
243                 }
244         }
245
246         for ( i=0; i<=lines; i++ ) {
247                 ad_prev = ad;
248                 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
249                         ad = NULL;
250                         rc = slap_bv2ad( type+i, &ad, &text );
251
252                         if( rc != LDAP_SUCCESS ) {
253                                 Debug( slapMode & SLAP_TOOL_MODE
254                                         ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
255                                         "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
256                                 if( slapMode & SLAP_TOOL_MODE ) {
257                                         goto fail;
258                                 }
259
260                                 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
261                                 if( rc != LDAP_SUCCESS ) {
262                                         Debug( LDAP_DEBUG_ANY,
263                                                 "<= str2entry: slap_str2undef_ad(%s): %s\n",
264                                                         type[i].bv_val, text, 0 );
265                                         goto fail;
266                                 }
267                         }
268
269                         /* require ';binary' when appropriate (ITS#5071) */
270                         if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
271                                 Debug( LDAP_DEBUG_ANY,
272                                         "str2entry: attributeType %s #%d: "
273                                         "needs ';binary' transfer as per syntax %s\n", 
274                                         ad->ad_cname.bv_val, 0,
275                                         ad->ad_type->sat_syntax->ssyn_oid );
276                                 goto fail;
277                         }
278                 }
279
280                 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
281                         int j, k;
282                         atail->a_next = attr_alloc( NULL );
283                         atail = atail->a_next;
284                         atail->a_flags = 0;
285                         atail->a_desc = ad_prev;
286                         atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
287                         if( ad_prev->ad_type->sat_equality &&
288                                 ad_prev->ad_type->sat_equality->smr_normalize )
289                                 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
290                         else
291                                 atail->a_nvals = NULL;
292                         k = i - attr_cnt;
293                         for ( j=0; j<attr_cnt; j++ ) {
294                                 if ( freeval[k] )
295                                         atail->a_vals[j] = vals[k];
296                                 else
297                                         ber_dupbv( atail->a_vals+j, &vals[k] );
298                                 vals[k].bv_val = NULL;
299                                 if ( atail->a_nvals ) {
300                                         atail->a_nvals[j] = nvals[k];
301                                         nvals[k].bv_val = NULL;
302                                 }
303                                 k++;
304                         }
305                         BER_BVZERO( &atail->a_vals[j] );
306                         if ( atail->a_nvals ) {
307                                 BER_BVZERO( &atail->a_nvals[j] );
308                         } else {
309                                 atail->a_nvals = atail->a_vals;
310                         }
311                         attr_cnt = 0;
312                         if ( i == lines ) break;
313                 }
314
315                 if( slapMode & SLAP_TOOL_MODE ) {
316                         struct berval pval;
317                         slap_syntax_validate_func *validate =
318                                 ad->ad_type->sat_syntax->ssyn_validate;
319                         slap_syntax_transform_func *pretty =
320                                 ad->ad_type->sat_syntax->ssyn_pretty;
321
322                         if ( pretty ) {
323                                 rc = ordered_value_pretty( ad,
324                                         &vals[i], &pval, NULL );
325
326                         } else if ( validate ) {
327                                 /*
328                                  * validate value per syntax
329                                  */
330                                 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
331
332                         } else {
333                                 Debug( LDAP_DEBUG_ANY,
334                                         "str2entry: attributeType %s #%d: "
335                                         "no validator for syntax %s\n", 
336                                         ad->ad_cname.bv_val, attr_cnt,
337                                         ad->ad_type->sat_syntax->ssyn_oid );
338                                 goto fail;
339                         }
340
341                         if( rc != 0 ) {
342                                 Debug( LDAP_DEBUG_ANY,
343                                         "str2entry: invalid value "
344                                         "for attributeType %s #%d (syntax %s)\n",
345                                         ad->ad_cname.bv_val, attr_cnt,
346                                         ad->ad_type->sat_syntax->ssyn_oid );
347                                 goto fail;
348                         }
349
350                         if( pretty ) {
351                                 if ( freeval[i] ) free( vals[i].bv_val );
352                                 vals[i] = pval;
353                                 freeval[i] = 1;
354                         }
355                 }
356
357                 if ( ad->ad_type->sat_equality &&
358                         ad->ad_type->sat_equality->smr_normalize )
359                 {
360                         rc = ordered_value_normalize(
361                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
362                                 ad,
363                                 ad->ad_type->sat_equality,
364                                 &vals[i], &nvals[i], NULL );
365
366                         if ( rc ) {
367                                 Debug( LDAP_DEBUG_ANY,
368                                         "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
369                                 goto fail;
370                         }
371                 }
372
373                 attr_cnt++;
374         }
375
376         free( type );
377         atail->a_next = NULL;
378         e->e_attrs = ahead.a_next;
379
380         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
381                 e->e_dn, (unsigned long) e, 0 );
382         return( e );
383
384 fail:
385         for ( i=0; i<lines; i++ ) {
386                 if ( freeval[i] ) free( vals[i].bv_val );
387                 free( nvals[i].bv_val );
388         }
389         free( type );
390         entry_free( e );
391         return NULL;
392 }
393
394
395 #define GRABSIZE        BUFSIZ
396
397 #define MAKE_SPACE( n ) { \
398                 while ( ecur + (n) > ebuf + emaxsize ) { \
399                         ptrdiff_t       offset; \
400                         offset = (int) (ecur - ebuf); \
401                         ebuf = ch_realloc( ebuf, \
402                                 emaxsize + GRABSIZE ); \
403                         emaxsize += GRABSIZE; \
404                         ecur = ebuf + offset; \
405                 } \
406         }
407
408 char *
409 entry2str(
410         Entry   *e,
411         int             *len )
412 {
413         Attribute       *a;
414         struct berval   *bv;
415         int             i;
416         ber_len_t tmplen;
417
418         assert( e != NULL );
419
420         /*
421          * In string format, an entry looks like this:
422          *      dn: <dn>\n
423          *      [<attr>: <value>\n]*
424          */
425
426         ecur = ebuf;
427
428         /* put the dn */
429         if ( e->e_dn != NULL ) {
430                 /* put "dn: <dn>" */
431                 tmplen = e->e_name.bv_len;
432                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
433                 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
434         }
435
436         /* put the attributes */
437         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
438                 /* put "<type>:[:] <value>" line for each value */
439                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
440                         bv = &a->a_vals[i];
441                         tmplen = a->a_desc->ad_cname.bv_len;
442                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
443                         ldif_sput( &ecur, LDIF_PUT_VALUE,
444                                 a->a_desc->ad_cname.bv_val,
445                                 bv->bv_val, bv->bv_len );
446                 }
447         }
448         MAKE_SPACE( 1 );
449         *ecur = '\0';
450         *len = ecur - ebuf;
451
452         return( ebuf );
453 }
454
455 void
456 entry_clean( Entry *e )
457 {
458         /* free an entry structure */
459         assert( e != NULL );
460
461         /* e_private must be freed by the caller */
462         assert( e->e_private == NULL );
463
464         e->e_id = 0;
465
466         /* free DNs */
467         if ( !BER_BVISNULL( &e->e_name ) ) {
468                 free( e->e_name.bv_val );
469                 BER_BVZERO( &e->e_name );
470         }
471         if ( !BER_BVISNULL( &e->e_nname ) ) {
472                 free( e->e_nname.bv_val );
473                 BER_BVZERO( &e->e_nname );
474         }
475
476         if ( !BER_BVISNULL( &e->e_bv ) ) {
477                 free( e->e_bv.bv_val );
478                 BER_BVZERO( &e->e_bv );
479         }
480
481         /* free attributes */
482         if ( e->e_attrs ) {
483                 attrs_free( e->e_attrs );
484                 e->e_attrs = NULL;
485         }
486
487         e->e_ocflags = 0;
488 }
489
490 void
491 entry_free( Entry *e )
492 {
493         entry_clean( e );
494
495         ldap_pvt_thread_mutex_lock( &entry_mutex );
496         e->e_private = entry_list;
497         entry_list = e;
498         ldap_pvt_thread_mutex_unlock( &entry_mutex );
499 }
500
501 /* These parameters work well on AMD64 */
502 #if 0
503 #define STRIDE 8
504 #define STRIPE 5
505 #else
506 #define STRIDE 1
507 #define STRIPE 1
508 #endif
509 #define STRIDE_FACTOR (STRIDE*STRIPE)
510
511 int
512 entry_prealloc( int num )
513 {
514         Entry *e, **prev, *tmp;
515         slap_list *s;
516         int i, j;
517
518         if (!num) return 0;
519
520 #if STRIDE_FACTOR > 1
521         /* Round up to our stride factor */
522         num += STRIDE_FACTOR-1;
523         num /= STRIDE_FACTOR;
524         num *= STRIDE_FACTOR;
525 #endif
526
527         s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
528         s->next = entry_chunks;
529         entry_chunks = s;
530
531         prev = &tmp;
532         for (i=0; i<STRIPE; i++) {
533                 e = (Entry *)(s+1);
534                 e += i;
535                 for (j=i; j<num; j+= STRIDE) {
536                         *prev = e;
537                         prev = (Entry **)&e->e_private;
538                         e += STRIDE;
539                 }
540         }
541         *prev = entry_list;
542         entry_list = (Entry *)(s+1);
543
544         return 0;
545 }
546
547 Entry *
548 entry_alloc( void )
549 {
550         Entry *e;
551
552         ldap_pvt_thread_mutex_lock( &entry_mutex );
553         if ( !entry_list )
554                 entry_prealloc( CHUNK_SIZE );
555         e = entry_list;
556         entry_list = e->e_private;
557         e->e_private = NULL;
558         ldap_pvt_thread_mutex_unlock( &entry_mutex );
559
560         return e;
561 }
562
563
564 /*
565  * These routines are used only by Backend.
566  *
567  * the Entry has three entry points (ways to find things):
568  *
569  *      by entry        e.g., if you already have an entry from the cache
570  *                      and want to delete it. (really by entry ptr)
571  *      by dn           e.g., when looking for the base object of a search
572  *      by id           e.g., for search candidates
573  *
574  * these correspond to three different avl trees that are maintained.
575  */
576
577 int
578 entry_cmp( Entry *e1, Entry *e2 )
579 {
580         return SLAP_PTRCMP( e1, e2 );
581 }
582
583 int
584 entry_dn_cmp( const void *v_e1, const void *v_e2 )
585 {
586         /* compare their normalized UPPERCASED dn's */
587         const Entry *e1 = v_e1, *e2 = v_e2;
588
589         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
590 }
591
592 int
593 entry_id_cmp( const void *v_e1, const void *v_e2 )
594 {
595         const Entry *e1 = v_e1, *e2 = v_e2;
596         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
597 }
598
599 /* This is like a ber_len */
600 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
601         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
602
603 static void
604 entry_putlen(unsigned char **buf, ber_len_t len)
605 {
606         ber_len_t lenlen = entry_lenlen(len);
607
608         if (lenlen == 1) {
609                 **buf = (unsigned char) len;
610         } else {
611                 int i;
612                 **buf = 0x80 | ((unsigned char) lenlen - 1);
613                 for (i=lenlen-1; i>0; i--) {
614                         (*buf)[i] = (unsigned char) len;
615                         len >>= 8;
616                 }
617         }
618         *buf += lenlen;
619 }
620
621 static ber_len_t
622 entry_getlen(unsigned char **buf)
623 {
624         ber_len_t len;
625         int i;
626
627         len = *(*buf)++;
628         if (len <= 0x7f)
629                 return len;
630         i = len & 0x7f;
631         len = 0;
632         for (;i > 0; i--) {
633                 len <<= 8;
634                 len |= *(*buf)++;
635         }
636         return len;
637 }
638
639 /* Count up the sizes of the components of an entry */
640 void entry_partsize(Entry *e, ber_len_t *plen,
641         int *pnattrs, int *pnvals, int norm)
642 {
643         ber_len_t len, dnlen, ndnlen;
644         int i, nat = 0, nval = 0;
645         Attribute *a;
646
647         dnlen = e->e_name.bv_len;
648         len = dnlen + 1;        /* trailing NUL byte */
649         len += entry_lenlen(dnlen);
650         if (norm) {
651                 ndnlen = e->e_nname.bv_len;
652                 len += ndnlen + 1;
653                 len += entry_lenlen(ndnlen);
654         }
655         for (a=e->e_attrs; a; a=a->a_next) {
656                 /* For AttributeDesc, we only store the attr name */
657                 nat++;
658                 len += a->a_desc->ad_cname.bv_len+1;
659                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
660                 for (i=0; a->a_vals[i].bv_val; i++) {
661                         nval++;
662                         len += a->a_vals[i].bv_len + 1;
663                         len += entry_lenlen(a->a_vals[i].bv_len);
664                 }
665                 len += entry_lenlen(i);
666                 nval++; /* empty berval at end */
667                 if (norm && a->a_nvals != a->a_vals) {
668                         for (i=0; a->a_nvals[i].bv_val; i++) {
669                                 nval++;
670                                 len += a->a_nvals[i].bv_len + 1;
671                                 len += entry_lenlen(a->a_nvals[i].bv_len);
672                         }
673                         len += entry_lenlen(i); /* i nvals */
674                         nval++;
675                 } else {
676                         len += entry_lenlen(0); /* 0 nvals */
677                 }
678         }
679         len += entry_lenlen(nat);
680         len += entry_lenlen(nval);
681         *plen = len;
682         *pnattrs = nat;
683         *pnvals = nval;
684 }
685
686 /* Add up the size of the entry for a flattened buffer */
687 ber_len_t entry_flatsize(Entry *e, int norm)
688 {
689         ber_len_t len;
690         int nattrs, nvals;
691
692         entry_partsize(e, &len, &nattrs, &nvals, norm);
693         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
694                 (nvals * sizeof(struct berval));
695         return len;
696 }
697
698 /* Flatten an Entry into a buffer. The buffer is filled with just the
699  * strings/bervals of all the entry components. Each field is preceded
700  * by its length, encoded the way ber_put_len works. Every field is NUL
701  * terminated.  The entire buffer size is precomputed so that a single
702  * malloc can be performed. The entry size is also recorded,
703  * to aid in entry_decode.
704  */
705 int entry_encode(Entry *e, struct berval *bv)
706 {
707         ber_len_t len, dnlen, ndnlen;
708         int i, nattrs, nvals;
709         Attribute *a;
710         unsigned char *ptr;
711
712         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
713                 (long) e->e_id, e->e_dn, 0 );
714         dnlen = e->e_name.bv_len;
715         ndnlen = e->e_nname.bv_len;
716
717         entry_partsize( e, &len, &nattrs, &nvals, 1 );
718
719         bv->bv_len = len;
720         bv->bv_val = ch_malloc(len);
721         ptr = (unsigned char *)bv->bv_val;
722         entry_putlen(&ptr, nattrs);
723         entry_putlen(&ptr, nvals);
724         entry_putlen(&ptr, dnlen);
725         AC_MEMCPY(ptr, e->e_dn, dnlen);
726         ptr += dnlen;
727         *ptr++ = '\0';
728         entry_putlen(&ptr, ndnlen);
729         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
730         ptr += ndnlen;
731         *ptr++ = '\0';
732
733         for (a=e->e_attrs; a; a=a->a_next) {
734                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
735                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
736                         a->a_desc->ad_cname.bv_len);
737                 ptr += a->a_desc->ad_cname.bv_len;
738                 *ptr++ = '\0';
739                 if (a->a_vals) {
740                         for (i=0; a->a_vals[i].bv_val; i++);
741                         entry_putlen(&ptr, i);
742                         for (i=0; a->a_vals[i].bv_val; i++) {
743                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
744                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
745                                         a->a_vals[i].bv_len);
746                                 ptr += a->a_vals[i].bv_len;
747                                 *ptr++ = '\0';
748                         }
749                         if (a->a_nvals != a->a_vals) {
750                                 entry_putlen(&ptr, i);
751                                 for (i=0; a->a_nvals[i].bv_val; i++) {
752                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
753                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
754                                         a->a_nvals[i].bv_len);
755                                         ptr += a->a_nvals[i].bv_len;
756                                         *ptr++ = '\0';
757                                 }
758                         } else {
759                                 entry_putlen(&ptr, 0);
760                         }
761                 }
762         }
763         return 0;
764 }
765
766 /* Retrieve an Entry that was stored using entry_encode above.
767  * First entry_header must be called to decode the size of the entry.
768  * Then a single block of memory must be malloc'd to accomodate the
769  * bervals and the bulk data. Next the bulk data is retrieved from
770  * the DB and parsed by entry_decode.
771  *
772  * Note: everything is stored in a single contiguous block, so
773  * you can not free individual attributes or names from this
774  * structure. Attempting to do so will likely corrupt memory.
775  */
776 int entry_header(EntryHeader *eh)
777 {
778         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
779
780         eh->nattrs = entry_getlen(&ptr);
781         if ( !eh->nattrs ) {
782                 Debug( LDAP_DEBUG_ANY,
783                         "entry_header: attribute count was zero\n", 0, 0, 0);
784                 return LDAP_OTHER;
785         }
786         eh->nvals = entry_getlen(&ptr);
787         if ( !eh->nvals ) {
788                 Debug( LDAP_DEBUG_ANY,
789                         "entry_header: value count was zero\n", 0, 0, 0);
790                 return LDAP_OTHER;
791         }
792         eh->data = (char *)ptr;
793         return LDAP_SUCCESS;
794 }
795
796 #ifdef SLAP_ZONE_ALLOC
797 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
798 #else
799 int entry_decode(EntryHeader *eh, Entry **e)
800 #endif
801 {
802         int i, j, count, nattrs, nvals;
803         int rc;
804         Attribute *a;
805         Entry *x;
806         const char *text;
807         AttributeDescription *ad;
808         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
809         BerVarray bptr;
810
811         nattrs = eh->nattrs;
812         nvals = eh->nvals;
813         x = entry_alloc();
814         x->e_attrs = attrs_alloc( nattrs );
815         ptr = (unsigned char *)eh->data;
816         i = entry_getlen(&ptr);
817         x->e_name.bv_val = (char *) ptr;
818         x->e_name.bv_len = i;
819         ptr += i+1;
820         i = entry_getlen(&ptr);
821         x->e_nname.bv_val = (char *) ptr;
822         x->e_nname.bv_len = i;
823         ptr += i+1;
824         Debug( LDAP_DEBUG_TRACE,
825                 "entry_decode: \"%s\"\n",
826                 x->e_dn, 0, 0 );
827         x->e_bv = eh->bv;
828
829         a = x->e_attrs;
830         bptr = (BerVarray)eh->bv.bv_val;
831
832         while ((i = entry_getlen(&ptr))) {
833                 struct berval bv;
834                 bv.bv_len = i;
835                 bv.bv_val = (char *) ptr;
836                 ad = NULL;
837                 rc = slap_bv2ad( &bv, &ad, &text );
838
839                 if( rc != LDAP_SUCCESS ) {
840                         Debug( LDAP_DEBUG_TRACE,
841                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
842                         rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
843
844                         if( rc != LDAP_SUCCESS ) {
845                                 Debug( LDAP_DEBUG_ANY,
846                                         "<= entry_decode: slap_str2undef_ad(%s): %s\n",
847                                                 ptr, text, 0 );
848                                 return rc;
849                         }
850                 }
851                 ptr += i + 1;
852                 a->a_desc = ad;
853                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
854                 count = j = entry_getlen(&ptr);
855                 a->a_vals = bptr;
856
857                 while (j) {
858                         i = entry_getlen(&ptr);
859                         bptr->bv_len = i;
860                         bptr->bv_val = (char *)ptr;
861                         ptr += i+1;
862                         bptr++;
863                         j--;
864                 }
865                 bptr->bv_val = NULL;
866                 bptr->bv_len = 0;
867                 bptr++;
868
869                 j = entry_getlen(&ptr);
870                 if (j) {
871                         a->a_nvals = bptr;
872                         while (j) {
873                                 i = entry_getlen(&ptr);
874                                 bptr->bv_len = i;
875                                 bptr->bv_val = (char *)ptr;
876                                 ptr += i+1;
877                                 bptr++;
878                                 j--;
879                         }
880                         bptr->bv_val = NULL;
881                         bptr->bv_len = 0;
882                         bptr++;
883                 } else {
884                         a->a_nvals = a->a_vals;
885                 }
886                 a = a->a_next;
887                 nattrs--;
888                 if ( !nattrs )
889                         break;
890         }
891
892         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
893                 x->e_dn, 0, 0 );
894         *e = x;
895         return 0;
896 }
897
898 Entry *entry_dup( Entry *e )
899 {
900         Entry *ret;
901
902         ret = entry_alloc();
903
904         ret->e_id = e->e_id;
905         ber_dupbv( &ret->e_name, &e->e_name );
906         ber_dupbv( &ret->e_nname, &e->e_nname );
907         ret->e_attrs = attrs_dup( e->e_attrs );
908         ret->e_ocflags = e->e_ocflags;
909
910         return ret;
911 }
912
913 #if 1
914 /* Duplicates an entry using a single malloc. Saves CPU time, increases
915  * heap usage because a single large malloc is harder to satisfy than
916  * lots of small ones, and the freed space isn't as easily reusable.
917  *
918  * Probably not worth using this function.
919  */
920 Entry *entry_dup_bv( Entry *e )
921 {
922         ber_len_t len;
923         int nattrs, nvals;
924         Entry *ret;
925         struct berval *bvl;
926         char *ptr;
927         Attribute *src, *dst;
928
929         ret = entry_alloc();
930
931         entry_partsize(e, &len, &nattrs, &nvals, 1);
932         ret->e_id = e->e_id;
933         ret->e_attrs = attrs_alloc( nattrs );
934         ret->e_ocflags = e->e_ocflags;
935         ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
936         ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
937
938         bvl = (struct berval *)ret->e_bv.bv_val;
939         ptr = (char *)(bvl + nvals);
940
941         ret->e_name.bv_len = e->e_name.bv_len;
942         ret->e_name.bv_val = ptr;
943         AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
944         ptr += e->e_name.bv_len;
945         *ptr++ = '\0';
946
947         ret->e_nname.bv_len = e->e_nname.bv_len;
948         ret->e_nname.bv_val = ptr;
949         AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
950         ptr += e->e_name.bv_len;
951         *ptr++ = '\0';
952
953         dst = ret->e_attrs;
954         for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
955                 int i;
956                 dst->a_desc = src->a_desc;
957                 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
958                 dst->a_vals = bvl;
959                 for ( i=0; src->a_vals[i].bv_val; i++ ) {
960                         bvl->bv_len = src->a_vals[i].bv_len;
961                         bvl->bv_val = ptr;
962                         AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
963                         ptr += bvl->bv_len;
964                         *ptr++ = '\0';
965                         bvl++;
966                 }
967                 BER_BVZERO(bvl);
968                 bvl++;
969                 if ( src->a_vals != src->a_nvals ) {
970                         dst->a_nvals = bvl;
971                         for ( i=0; src->a_nvals[i].bv_val; i++ ) {
972                                 bvl->bv_len = src->a_nvals[i].bv_len;
973                                 bvl->bv_val = ptr;
974                                 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );
975                                 ptr += bvl->bv_len;
976                                 *ptr++ = '\0';
977                                 bvl++;
978                         }
979                         BER_BVZERO(bvl);
980                         bvl++;
981                 }
982         }
983         return ret;
984 }
985 #endif