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