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