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