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