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