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