]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Memory context tweaks for Bind
[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 syntax %s\n",
222                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
223 #else
224                                 Debug( LDAP_DEBUG_ANY,
225                                         "str2entry: invalid value for syntax %s\n",
226                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 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( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
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         int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
434         if (rc) return rc;
435         return( strcmp( e1->e_ndn, e2->e_ndn ) );
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 = ptr;
646         x->e_name.bv_len = i;
647         ptr += i+1;
648         i = entry_getlen(&ptr);
649         x->e_nname.bv_val = 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 = 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 }