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