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