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