]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
9441de24e6317c7b18a2425ceab42870e89bac20
[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-2005 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 static const struct berval dn_bv = BER_BVC("dn");
51
52 int entry_destroy(void)
53 {
54         if ( ebuf ) free( ebuf );
55         ebuf = NULL;
56         ecur = NULL;
57         emaxsize = 0;
58         return 0;
59 }
60
61
62 Entry *
63 str2entry( char *s )
64 {
65         return str2entry2( s, 1 );
66 }
67
68 Entry *
69 str2entry2( char *s, int checkvals )
70 {
71         int rc;
72         Entry           *e;
73         struct berval   *type, *vals, *nvals;
74         char    *freeval;
75         AttributeDescription *ad, *ad_prev;
76         const char *text;
77         char    *next;
78         int             attr_cnt;
79         int             i, lines;
80         Attribute       ahead, *atail;
81
82         /*
83          * LDIF is used as the string format.
84          * An entry looks like this:
85          *
86          *      dn: <dn>\n
87          *      [<attr>:[:] <value>\n]
88          *      [<tab><continuedvalue>\n]*
89          *      ...
90          *
91          * If a double colon is used after a type, it means the
92          * following value is encoded as a base 64 string.  This
93          * happens if the value contains a non-printing character
94          * or newline.
95          */
96
97         Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
98                 s ? s : "NULL", 0, 0 );
99
100         /* initialize reader/writer lock */
101         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
102
103         if( e == NULL ) {
104                 Debug( LDAP_DEBUG_ANY,
105                         "<= str2entry NULL (entry allocation failed)\n",
106                         0, 0, 0 );
107                 return( NULL );
108         }
109
110         /* initialize entry */
111         e->e_id = NOID;
112
113         /* dn + attributes */
114         atail = &ahead;
115         ahead.a_next = NULL;
116         ad = NULL;
117         ad_prev = NULL;
118         attr_cnt = 0;
119         next = s;
120
121         lines = ldif_countlines( s );
122         type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
123         vals = type+lines+1;
124         nvals = vals+lines+1;
125         freeval = (char *)(nvals+lines+1);
126         i = -1;
127
128         /* parse into individual values, record DN */
129         while ( (s = ldif_getline( &next )) != NULL ) {
130                 int freev;
131                 if ( *s == '\n' || *s == '\0' ) {
132                         break;
133                 }
134                 i++;
135
136                 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
137                 freeval[i] = freev;
138                 if ( rc ) {
139                         Debug( LDAP_DEBUG_TRACE,
140                                 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
141                         continue;
142                 }
143
144                 if ( type[i].bv_len == dn_bv.bv_len &&
145                         strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
146
147                         if ( e->e_dn != NULL ) {
148                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
149                                         "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
150                                         (long) e->e_id, e->e_dn, vals[i].bv_val );
151                                 goto fail;
152                         }
153
154                         rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
155                         if( rc != LDAP_SUCCESS ) {
156                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
157                                         "entry %ld has invalid DN \"%s\"\n",
158                                         (long) e->e_id, vals[i].bv_val, 0 );
159                                 goto fail;
160                         }
161                         if ( freeval[i] ) free( vals[i].bv_val );
162                         vals[i].bv_val = NULL;
163                         i--;
164                         continue;
165                 }
166         }
167         lines = i+1;
168
169         /* check to make sure there was a dn: line */
170         if ( BER_BVISNULL( &e->e_name )) {
171                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
172                         (long) e->e_id, 0, 0 );
173                 goto fail;
174         }
175
176         /* Make sure all attributes with multiple values are contiguous */
177         if ( checkvals ) {
178                 int j, k;
179                 struct berval bv;
180                 int fv;
181
182                 for (i=0; i<lines; i++) {
183                         for ( j=i+1; j<lines; j++ ) {
184                                 if ( bvmatch( type+i, type+j )) {
185                                         /* out of order, move intervening attributes down */
186                                         if ( j != i+1 ) {
187                                                 bv = vals[j];
188                                                 fv = freeval[j];
189                                                 for ( k=j; k>i; k-- ) {
190                                                         type[k] = type[k-1];
191                                                         vals[k] = vals[k-1];
192                                                         freeval[k] = freeval[k-1];
193                                                 }
194                                                 k++;
195                                                 type[k] = type[i];
196                                                 vals[k] = bv;
197                                                 freeval[k] = fv;
198                                         }
199                                         i++;
200                                 }
201                         }
202                 }
203         }
204
205         for ( i=0; i<=lines; i++ ) {
206                 ad_prev = ad;
207                 if ( !ad || ( i<lines && !bvmatch( type+i, &ad->ad_cname ))) {
208                         ad = NULL;
209                         rc = slap_bv2ad( type+i, &ad, &text );
210
211                         if( rc != LDAP_SUCCESS ) {
212                                 Debug( slapMode & SLAP_TOOL_MODE
213                                         ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
214                                         "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
215                                 if( slapMode & SLAP_TOOL_MODE ) {
216                                         goto fail;
217                                 }
218
219                                 rc = slap_bv2undef_ad( type+i, &ad, &text );
220                                 if( rc != LDAP_SUCCESS ) {
221                                         Debug( LDAP_DEBUG_ANY,
222                                                 "<= str2entry: str2undef_ad(%s): %s\n",
223                                                         type[i].bv_val, text, 0 );
224                                         goto fail;
225                                 }
226                         }
227                 }
228
229                 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
230                         int j, k;
231                         atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
232                         atail = atail->a_next;
233                         atail->a_flags = 0;
234                         atail->a_desc = ad_prev;
235                         atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
236                         if( ad_prev->ad_type->sat_equality &&
237                                 ad_prev->ad_type->sat_equality->smr_normalize )
238                                 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
239                         else
240                                 atail->a_nvals = NULL;
241                         k = i - attr_cnt;
242                         for ( j=0; j<attr_cnt; j++ ) {
243                                 if ( freeval[k] )
244                                         atail->a_vals[j] = vals[k];
245                                 else
246                                         ber_dupbv( atail->a_vals+j, &vals[k] );
247                                 vals[k].bv_val = NULL;
248                                 if ( atail->a_nvals ) {
249                                         atail->a_nvals[j] = nvals[k];
250                                         nvals[k].bv_val = NULL;
251                                 }
252                                 k++;
253                         }
254                         BER_BVZERO( &atail->a_vals[j] );
255                         if ( atail->a_nvals ) {
256                                 BER_BVZERO( &atail->a_nvals[j] );
257                         } else {
258                                 atail->a_nvals = atail->a_vals;
259                         }
260                         attr_cnt = 0;
261                         if ( i == lines ) break;
262                 }
263
264                 if( slapMode & SLAP_TOOL_MODE ) {
265                         struct berval pval;
266                         slap_syntax_validate_func *validate =
267                                 ad->ad_type->sat_syntax->ssyn_validate;
268                         slap_syntax_transform_func *pretty =
269                                 ad->ad_type->sat_syntax->ssyn_pretty;
270
271                         if( pretty ) {
272                                 rc = pretty( ad->ad_type->sat_syntax,
273                                         &vals[i], &pval, NULL );
274
275                         } else if( validate ) {
276                                 /*
277                                  * validate value per syntax
278                                  */
279                                 rc = validate( ad->ad_type->sat_syntax, &vals[i] );
280
281                         } else {
282                                 Debug( LDAP_DEBUG_ANY,
283                                         "str2entry: attributeType %s #%d: "
284                                         "no validator for syntax %s\n", 
285                                         ad->ad_cname.bv_val, attr_cnt,
286                                         ad->ad_type->sat_syntax->ssyn_oid );
287                                 goto fail;
288                         }
289
290                         if( rc != 0 ) {
291                                 Debug( LDAP_DEBUG_ANY,
292                                         "str2entry: invalid value "
293                                         "for attributeType %s #%d (syntax %s)\n",
294                                         ad->ad_cname.bv_val, attr_cnt,
295                                         ad->ad_type->sat_syntax->ssyn_oid );
296                                 goto fail;
297                         }
298
299                         if( pretty ) {
300                                 if ( freeval[i] ) free( vals[i].bv_val );
301                                 vals[i] = pval;
302                                 freeval[i] = 1;
303                         }
304                 }
305
306                 if( ad->ad_type->sat_equality &&
307                         ad->ad_type->sat_equality->smr_normalize )
308                 {
309                         rc = ad->ad_type->sat_equality->smr_normalize(
310                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
311                                 ad->ad_type->sat_syntax,
312                                 ad->ad_type->sat_equality,
313                                 &vals[i], &nvals[i], NULL );
314
315                         if( rc ) {
316                                 Debug( LDAP_DEBUG_ANY,
317                                         "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
318                                 goto fail;
319                         }
320                 }
321
322                 attr_cnt++;
323         }
324
325         free( type );
326         atail->a_next = NULL;
327         e->e_attrs = ahead.a_next;
328
329         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
330                 e->e_dn, (unsigned long) e, 0 );
331         return( e );
332
333 fail:
334         for ( i=0; i<lines; i++ ) {
335                 if ( freeval[i] ) free( vals[i].bv_val );
336                 free( nvals[i].bv_val );
337         }
338         free( type );
339         entry_free( e );
340         return NULL;
341 }
342
343
344 #define GRABSIZE        BUFSIZ
345
346 #define MAKE_SPACE( n ) { \
347                 while ( ecur + (n) > ebuf + emaxsize ) { \
348                         ptrdiff_t       offset; \
349                         offset = (int) (ecur - ebuf); \
350                         ebuf = ch_realloc( ebuf, \
351                                 emaxsize + GRABSIZE ); \
352                         emaxsize += GRABSIZE; \
353                         ecur = ebuf + offset; \
354                 } \
355         }
356
357 char *
358 entry2str(
359         Entry   *e,
360         int             *len )
361 {
362         Attribute       *a;
363         struct berval   *bv;
364         int             i;
365         ber_len_t tmplen;
366
367         assert( e != NULL );
368
369         /*
370          * In string format, an entry looks like this:
371          *      dn: <dn>\n
372          *      [<attr>: <value>\n]*
373          */
374
375         ecur = ebuf;
376
377         /* put the dn */
378         if ( e->e_dn != NULL ) {
379                 /* put "dn: <dn>" */
380                 tmplen = e->e_name.bv_len;
381                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
382                 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
383         }
384
385         /* put the attributes */
386         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
387                 /* put "<type>:[:] <value>" line for each value */
388                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
389                         bv = &a->a_vals[i];
390                         tmplen = a->a_desc->ad_cname.bv_len;
391                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
392                         ldif_sput( &ecur, LDIF_PUT_VALUE,
393                                 a->a_desc->ad_cname.bv_val,
394                                 bv->bv_val, bv->bv_len );
395                 }
396         }
397         MAKE_SPACE( 1 );
398         *ecur = '\0';
399         *len = ecur - ebuf;
400
401         return( ebuf );
402 }
403
404 void
405 entry_clean( Entry *e )
406 {
407         /* free an entry structure */
408         assert( e != NULL );
409
410         /* e_private must be freed by the caller */
411         assert( e->e_private == NULL );
412         e->e_private = NULL;
413
414         /* free DNs */
415         if ( !BER_BVISNULL( &e->e_name ) ) {
416                 free( e->e_name.bv_val );
417                 BER_BVZERO( &e->e_name );
418         }
419         if ( !BER_BVISNULL( &e->e_nname ) ) {
420                 free( e->e_nname.bv_val );
421                 BER_BVZERO( &e->e_nname );
422         }
423
424         if ( !BER_BVISNULL( &e->e_bv ) ) {
425                 free( e->e_bv.bv_val );
426                 BER_BVZERO( &e->e_bv );
427         }
428
429         /* free attributes */
430         attrs_free( e->e_attrs );
431         e->e_attrs = NULL;
432 }
433
434 void
435 entry_free( Entry *e )
436 {
437         entry_clean( e );
438
439         free( e );
440 }
441
442 /*
443  * These routines are used only by Backend.
444  *
445  * the Entry has three entry points (ways to find things):
446  *
447  *      by entry        e.g., if you already have an entry from the cache
448  *                      and want to delete it. (really by entry ptr)
449  *      by dn           e.g., when looking for the base object of a search
450  *      by id           e.g., for search candidates
451  *
452  * these correspond to three different avl trees that are maintained.
453  */
454
455 int
456 entry_cmp( Entry *e1, Entry *e2 )
457 {
458         return SLAP_PTRCMP( e1, e2 );
459 }
460
461 int
462 entry_dn_cmp( const void *v_e1, const void *v_e2 )
463 {
464         /* compare their normalized UPPERCASED dn's */
465         const Entry *e1 = v_e1, *e2 = v_e2;
466
467         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
468 }
469
470 int
471 entry_id_cmp( const void *v_e1, const void *v_e2 )
472 {
473         const Entry *e1 = v_e1, *e2 = v_e2;
474         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
475 }
476
477 /* This is like a ber_len */
478 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
479         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
480
481 static void
482 entry_putlen(unsigned char **buf, ber_len_t len)
483 {
484         ber_len_t lenlen = entry_lenlen(len);
485
486         if (lenlen == 1) {
487                 **buf = (unsigned char) len;
488         } else {
489                 int i;
490                 **buf = 0x80 | ((unsigned char) lenlen - 1);
491                 for (i=lenlen-1; i>0; i--) {
492                         (*buf)[i] = (unsigned char) len;
493                         len >>= 8;
494                 }
495         }
496         *buf += lenlen;
497 }
498
499 static ber_len_t
500 entry_getlen(unsigned char **buf)
501 {
502         ber_len_t len;
503         int i;
504
505         len = *(*buf)++;
506         if (len <= 0x7f)
507                 return len;
508         i = len & 0x7f;
509         len = 0;
510         for (;i > 0; i--) {
511                 len <<= 8;
512                 len |= *(*buf)++;
513         }
514         return len;
515 }
516
517 /* Count up the sizes of the components of an entry */
518 void entry_partsize(Entry *e, ber_len_t *plen,
519         int *pnattrs, int *pnvals, int norm)
520 {
521         ber_len_t len, dnlen, ndnlen;
522         int i, nat = 0, nval = 0;
523         Attribute *a;
524
525         dnlen = e->e_name.bv_len;
526         len = dnlen + 1;        /* trailing NUL byte */
527         len += entry_lenlen(dnlen);
528         if (norm) {
529                 ndnlen = e->e_nname.bv_len;
530                 len += ndnlen + 1;
531                 len += entry_lenlen(ndnlen);
532         }
533         for (a=e->e_attrs; a; a=a->a_next) {
534                 /* For AttributeDesc, we only store the attr name */
535                 nat++;
536                 len += a->a_desc->ad_cname.bv_len+1;
537                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
538                 for (i=0; a->a_vals[i].bv_val; i++) {
539                         nval++;
540                         len += a->a_vals[i].bv_len + 1;
541                         len += entry_lenlen(a->a_vals[i].bv_len);
542                 }
543                 len += entry_lenlen(i);
544                 nval++; /* empty berval at end */
545                 if (norm && a->a_nvals != a->a_vals) {
546                         for (i=0; a->a_nvals[i].bv_val; i++) {
547                                 nval++;
548                                 len += a->a_nvals[i].bv_len + 1;
549                                 len += entry_lenlen(a->a_nvals[i].bv_len);
550                         }
551                         len += entry_lenlen(i); /* i nvals */
552                         nval++;
553                 } else {
554                         len += entry_lenlen(0); /* 0 nvals */
555                 }
556         }
557         len += entry_lenlen(nat);
558         len += entry_lenlen(nval);
559         *plen = len;
560         *pnattrs = nat;
561         *pnvals = nval;
562 }
563
564 /* Add up the size of the entry for a flattened buffer */
565 ber_len_t entry_flatsize(Entry *e, int norm)
566 {
567         ber_len_t len;
568         int nattrs, nvals;
569
570         entry_partsize(e, &len, &nattrs, &nvals, norm);
571         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
572                 (nvals * sizeof(struct berval));
573         return len;
574 }
575
576 /* Flatten an Entry into a buffer. The buffer is filled with just the
577  * strings/bervals of all the entry components. Each field is preceded
578  * by its length, encoded the way ber_put_len works. Every field is NUL
579  * terminated.  The entire buffer size is precomputed so that a single
580  * malloc can be performed. The entry size is also recorded,
581  * to aid in entry_decode.
582  */
583 int entry_encode(Entry *e, struct berval *bv)
584 {
585         ber_len_t len, dnlen, ndnlen;
586         int i, nattrs, nvals;
587         Attribute *a;
588         unsigned char *ptr;
589
590         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
591                 (long) e->e_id, e->e_dn, 0 );
592         dnlen = e->e_name.bv_len;
593         ndnlen = e->e_nname.bv_len;
594
595         entry_partsize( e, &len, &nattrs, &nvals, 1 );
596
597         bv->bv_len = len;
598         bv->bv_val = ch_malloc(len);
599         ptr = (unsigned char *)bv->bv_val;
600         entry_putlen(&ptr, nattrs);
601         entry_putlen(&ptr, nvals);
602         entry_putlen(&ptr, dnlen);
603         AC_MEMCPY(ptr, e->e_dn, dnlen);
604         ptr += dnlen;
605         *ptr++ = '\0';
606         entry_putlen(&ptr, ndnlen);
607         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
608         ptr += ndnlen;
609         *ptr++ = '\0';
610
611         for (a=e->e_attrs; a; a=a->a_next) {
612                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
613                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
614                         a->a_desc->ad_cname.bv_len);
615                 ptr += a->a_desc->ad_cname.bv_len;
616                 *ptr++ = '\0';
617                 if (a->a_vals) {
618                         for (i=0; a->a_vals[i].bv_val; i++);
619                                 entry_putlen(&ptr, i);
620                                 for (i=0; a->a_vals[i].bv_val; i++) {
621                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
622                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
623                                         a->a_vals[i].bv_len);
624                                 ptr += a->a_vals[i].bv_len;
625                                 *ptr++ = '\0';
626                         }
627                         if (a->a_nvals != a->a_vals) {
628                                 entry_putlen(&ptr, i);
629                                 for (i=0; a->a_nvals[i].bv_val; i++) {
630                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
631                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
632                                         a->a_nvals[i].bv_len);
633                                         ptr += a->a_nvals[i].bv_len;
634                                         *ptr++ = '\0';
635                                 }
636                         } else {
637                                 entry_putlen(&ptr, 0);
638                         }
639                 }
640         }
641         return 0;
642 }
643
644 /* Retrieve an Entry that was stored using entry_encode above.
645  * We malloc a single block with the size stored above for the Entry
646  * and all of its Attributes. We also must lookup the stored
647  * attribute names to get AttributeDescriptions. To detect if the
648  * attributes of an Entry are later modified, we note that e->e_attr
649  * is always a constant offset from (e).
650  *
651  * Note: everything is stored in a single contiguous block, so
652  * you can not free individual attributes or names from this
653  * structure. Attempting to do so will likely corrupt memory.
654  */
655 #ifdef SLAP_ZONE_ALLOC
656 int entry_decode(struct berval *bv, Entry **e, void *ctx)
657 #else
658 int entry_decode(struct berval *bv, Entry **e)
659 #endif
660 {
661         int i, j, count, nattrs, nvals;
662         int rc;
663         Attribute *a;
664         Entry *x;
665         const char *text;
666         AttributeDescription *ad;
667         unsigned char *ptr = (unsigned char *)bv->bv_val;
668         BerVarray bptr;
669
670         nattrs = entry_getlen(&ptr);
671         if (!nattrs) {
672                 Debug( LDAP_DEBUG_ANY,
673                         "entry_decode: attribute count was zero\n", 0, 0, 0);
674                 return LDAP_OTHER;
675         }
676         nvals = entry_getlen(&ptr);
677         if (!nvals) {
678                 Debug( LDAP_DEBUG_ANY,
679                         "entry_decode: value count was zero\n", 0, 0, 0);
680                 return LDAP_OTHER;
681         }
682         i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
683                 (nvals * sizeof(struct berval));
684 #ifdef SLAP_ZONE_ALLOC
685         x = slap_zn_calloc(1, i + bv->bv_len, ctx);
686         AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
687         bv->bv_val = (char*)x + i;
688         ptr = (unsigned char *)bv->bv_val;
689         /* pointer is reset, now advance past nattrs and nvals again */
690         entry_getlen(&ptr);
691         entry_getlen(&ptr);
692 #else
693         x = ch_calloc(1, i);
694 #endif
695         i = entry_getlen(&ptr);
696         x->e_name.bv_val = (char *) ptr;
697         x->e_name.bv_len = i;
698         ptr += i+1;
699         i = entry_getlen(&ptr);
700         x->e_nname.bv_val = (char *) ptr;
701         x->e_nname.bv_len = i;
702         ptr += i+1;
703         Debug( LDAP_DEBUG_TRACE,
704                 "entry_decode: \"%s\"\n",
705                 x->e_dn, 0, 0 );
706         x->e_bv = *bv;
707
708         /* A valid entry must have at least one attr, so this
709          * pointer can never be NULL
710          */
711         x->e_attrs = (Attribute *)(x+1);
712         bptr = (BerVarray)x->e_attrs;
713         a = NULL;
714
715         while ((i = entry_getlen(&ptr))) {
716                 struct berval bv;
717                 bv.bv_len = i;
718                 bv.bv_val = (char *) ptr;
719                 if (a) {
720                         a->a_next = (Attribute *)bptr;
721                 }
722                 a = (Attribute *)bptr;
723                 ad = NULL;
724                 rc = slap_bv2ad( &bv, &ad, &text );
725
726                 if( rc != LDAP_SUCCESS ) {
727                         Debug( LDAP_DEBUG_TRACE,
728                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
729                         rc = slap_bv2undef_ad( &bv, &ad, &text );
730
731                         if( rc != LDAP_SUCCESS ) {
732                                 Debug( LDAP_DEBUG_ANY,
733                                         "<= entry_decode: str2undef_ad(%s): %s\n",
734                                                 ptr, text, 0 );
735                                 return rc;
736                         }
737                 }
738                 ptr += i + 1;
739                 a->a_desc = ad;
740                 bptr = (BerVarray)(a+1);
741                 a->a_vals = bptr;
742                 a->a_flags = 0;
743 #ifdef LDAP_COMP_MATCH
744                 a->a_comp_data = NULL;
745 #endif
746                 count = j = entry_getlen(&ptr);
747
748                 while (j) {
749                         i = entry_getlen(&ptr);
750                         bptr->bv_len = i;
751                         bptr->bv_val = (char *)ptr;
752                         ptr += i+1;
753                         bptr++;
754                         j--;
755                 }
756                 bptr->bv_val = NULL;
757                 bptr->bv_len = 0;
758                 bptr++;
759
760                 j = entry_getlen(&ptr);
761                 if (j) {
762                         a->a_nvals = bptr;
763                         while (j) {
764                                 i = entry_getlen(&ptr);
765                                 bptr->bv_len = i;
766                                 bptr->bv_val = (char *)ptr;
767                                 ptr += i+1;
768                                 bptr++;
769                                 j--;
770                         }
771                         bptr->bv_val = NULL;
772                         bptr->bv_len = 0;
773                         bptr++;
774                 } else {
775                         a->a_nvals = a->a_vals;
776                 }
777                 nattrs--;
778                 if ( !nattrs )
779                         break;
780         }
781
782         if (a) a->a_next = NULL;
783         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
784                 x->e_dn, 0, 0 );
785         *e = x;
786         return 0;
787 }
788
789 Entry *entry_dup( Entry *e )
790 {
791         Entry *ret;
792
793         ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
794
795         ret->e_id = e->e_id;
796         ber_dupbv( &ret->e_name, &e->e_name );
797         ber_dupbv( &ret->e_nname, &e->e_nname );
798         ret->e_attrs = attrs_dup( e->e_attrs );
799         ret->e_ocflags = e->e_ocflags;
800         ret->e_bv.bv_val = NULL;
801         ret->e_bv.bv_len = 0;
802         ret->e_private = NULL;
803
804         return ret;
805 }
806