]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
ITS#3226: Clear attribute flags after schema_check failed
[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_free( 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         free( e );
446 }
447
448 /*
449  * These routines are used only by Backend.
450  *
451  * the Entry has three entry points (ways to find things):
452  *
453  *      by entry        e.g., if you already have an entry from the cache
454  *                      and want to delete it. (really by entry ptr)
455  *      by dn           e.g., when looking for the base object of a search
456  *      by id           e.g., for search candidates
457  *
458  * these correspond to three different avl trees that are maintained.
459  */
460
461 int
462 entry_cmp( Entry *e1, Entry *e2 )
463 {
464         return SLAP_PTRCMP( e1, e2 );
465 }
466
467 int
468 entry_dn_cmp( const void *v_e1, const void *v_e2 )
469 {
470         /* compare their normalized UPPERCASED dn's */
471         const Entry *e1 = v_e1, *e2 = v_e2;
472
473         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
474 }
475
476 int
477 entry_id_cmp( const void *v_e1, const void *v_e2 )
478 {
479         const Entry *e1 = v_e1, *e2 = v_e2;
480         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
481 }
482
483 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
484         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
485 #if 0
486 /* This is like a ber_len */
487 static ber_len_t
488 entry_lenlen(ber_len_t len)
489 {
490         if (len <= 0x7f)
491                 return 1;
492         if (len <= 0xff)
493                 return 2;
494         if (len <= 0xffff)
495                 return 3;
496         if (len <= 0xffffff)
497                 return 4;
498         return 5;
499 }
500 #endif
501
502 static void
503 entry_putlen(unsigned char **buf, ber_len_t len)
504 {
505         ber_len_t lenlen = entry_lenlen(len);
506
507         if (lenlen == 1) {
508                 **buf = (unsigned char) len;
509         } else {
510                 int i;
511                 **buf = 0x80 | ((unsigned char) lenlen - 1);
512                 for (i=lenlen-1; i>0; i--) {
513                         (*buf)[i] = (unsigned char) len;
514                         len >>= 8;
515                 }
516         }
517         *buf += lenlen;
518 }
519
520 static ber_len_t
521 entry_getlen(unsigned char **buf)
522 {
523         ber_len_t len;
524         int i;
525
526         len = *(*buf)++;
527         if (len <= 0x7f)
528                 return len;
529         i = len & 0x7f;
530         len = 0;
531         for (;i > 0; i--) {
532                 len <<= 8;
533                 len |= *(*buf)++;
534         }
535         return len;
536 }
537
538 /* Add up the size of the entry for a flattened buffer */
539 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
540 {
541         ber_len_t siz = sizeof(Entry);
542         ber_len_t len, dnlen, ndnlen;
543         int i;
544         Attribute *a;
545
546         dnlen = e->e_name.bv_len;
547         len = dnlen + 1;        /* trailing NUL byte */
548         len += entry_lenlen(dnlen);
549         if (norm) {
550                 ndnlen = e->e_nname.bv_len;
551                 len += ndnlen + 1;
552                 len += entry_lenlen(ndnlen);
553         }
554         for (a=e->e_attrs; a; a=a->a_next) {
555                 /* For AttributeDesc, we only store the attr name */
556                 siz += sizeof(Attribute);
557                 len += a->a_desc->ad_cname.bv_len+1;
558                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
559                 for (i=0; a->a_vals[i].bv_val; i++) {
560                         siz += sizeof(struct berval);
561                         len += a->a_vals[i].bv_len + 1;
562                         len += entry_lenlen(a->a_vals[i].bv_len);
563                 }
564                 len += entry_lenlen(i);
565                 siz += sizeof(struct berval);   /* empty berval at end */
566                 if (norm && a->a_nvals != a->a_vals) {
567                         for (i=0; a->a_nvals[i].bv_val; i++) {
568                                 siz += sizeof(struct berval);
569                                 len += a->a_nvals[i].bv_len + 1;
570                                 len += entry_lenlen(a->a_nvals[i].bv_len);
571                         }
572                         len += entry_lenlen(i); /* i nvals */
573                         siz += sizeof(struct berval);
574                 } else {
575                         len += entry_lenlen(0); /* 0 nvals */
576                 }
577         }
578         len += 1;       /* NUL byte at end */
579         len += entry_lenlen(siz);
580         *psiz = siz;
581         *plen = len;
582 }
583
584 /* Flatten an Entry into a buffer. The buffer is filled with just the
585  * strings/bervals of all the entry components. Each field is preceded
586  * by its length, encoded the way ber_put_len works. Every field is NUL
587  * terminated.  The entire buffer size is precomputed so that a single
588  * malloc can be performed. The entry size is also recorded,
589  * to aid in entry_decode.
590  */
591 int entry_encode(Entry *e, struct berval *bv)
592 {
593         ber_len_t siz = sizeof(Entry);
594         ber_len_t len, dnlen, ndnlen;
595         int i;
596         Attribute *a;
597         unsigned char *ptr;
598
599 #ifdef NEW_LOGGING
600         LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx  \"%s\"\n",
601                 (long) e->e_id, e->e_dn, 0 );
602 #else
603         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
604                 (long) e->e_id, e->e_dn, 0 );
605 #endif
606         dnlen = e->e_name.bv_len;
607         ndnlen = e->e_nname.bv_len;
608
609         entry_flatsize( e, &siz, &len, 1 );
610
611         bv->bv_len = len;
612         bv->bv_val = ch_malloc(len);
613         ptr = (unsigned char *)bv->bv_val;
614         entry_putlen(&ptr, siz);
615         entry_putlen(&ptr, dnlen);
616         AC_MEMCPY(ptr, e->e_dn, dnlen);
617         ptr += dnlen;
618         *ptr++ = '\0';
619         entry_putlen(&ptr, ndnlen);
620         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
621         ptr += ndnlen;
622         *ptr++ = '\0';
623
624         for (a=e->e_attrs; a; a=a->a_next) {
625                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
626                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
627                         a->a_desc->ad_cname.bv_len);
628                 ptr += a->a_desc->ad_cname.bv_len;
629                 *ptr++ = '\0';
630                 if (a->a_vals) {
631                     for (i=0; a->a_vals[i].bv_val; i++);
632                     entry_putlen(&ptr, i);
633                     for (i=0; a->a_vals[i].bv_val; i++) {
634                         entry_putlen(&ptr, a->a_vals[i].bv_len);
635                         AC_MEMCPY(ptr, a->a_vals[i].bv_val,
636                                 a->a_vals[i].bv_len);
637                         ptr += a->a_vals[i].bv_len;
638                         *ptr++ = '\0';
639                     }
640                     if (a->a_nvals != a->a_vals) {
641                         entry_putlen(&ptr, i);
642                         for (i=0; a->a_nvals[i].bv_val; i++) {
643                             entry_putlen(&ptr, a->a_nvals[i].bv_len);
644                             AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
645                                 a->a_nvals[i].bv_len);
646                             ptr += a->a_nvals[i].bv_len;
647                             *ptr++ = '\0';
648                         }
649                     } else {
650                         entry_putlen(&ptr, 0);
651                     }
652                 }
653         }
654         *ptr = '\0';
655         return 0;
656 }
657
658 /* Retrieve an Entry that was stored using entry_encode above.
659  * We malloc a single block with the size stored above for the Entry
660  * and all of its Attributes. We also must lookup the stored
661  * attribute names to get AttributeDescriptions. To detect if the
662  * attributes of an Entry are later modified, we note that e->e_attr
663  * is always a constant offset from (e).
664  *
665  * Note: everything is stored in a single contiguous block, so
666  * you can not free individual attributes or names from this
667  * structure. Attempting to do so will likely corrupt memory.
668  */
669 int entry_decode(struct berval *bv, Entry **e)
670 {
671         int i, j, count;
672         int rc;
673         Attribute *a;
674         Entry *x;
675         const char *text;
676         AttributeDescription *ad;
677         unsigned char *ptr = (unsigned char *)bv->bv_val;
678         BerVarray bptr;
679
680         i = entry_getlen(&ptr);
681         if (!i) {
682 #ifdef NEW_LOGGING
683                 LDAP_LOG( OPERATION, INFO, 
684                         "entry_decode: entry length was zero\n", 0, 0, 0);
685 #else
686                 Debug( LDAP_DEBUG_ANY,
687                         "entry_decode: entry length was zero\n", 0, 0, 0);
688 #endif
689                 return LDAP_OTHER;
690         }
691         x = ch_calloc(1, i);
692         i = entry_getlen(&ptr);
693         x->e_name.bv_val = (char *) ptr;
694         x->e_name.bv_len = i;
695         ptr += i+1;
696         i = entry_getlen(&ptr);
697         x->e_nname.bv_val = (char *) ptr;
698         x->e_nname.bv_len = i;
699         ptr += i+1;
700 #ifdef NEW_LOGGING
701         LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
702 #else
703         Debug( LDAP_DEBUG_TRACE,
704             "entry_decode: \"%s\"\n",
705             x->e_dn, 0, 0 );
706 #endif
707         x->e_bv = *bv;
708
709         /* A valid entry must have at least one attr, so this
710          * pointer can never be NULL
711          */
712         x->e_attrs = (Attribute *)(x+1);
713         bptr = (BerVarray)x->e_attrs;
714         a = NULL;
715
716         while ((i = entry_getlen(&ptr))) {
717                 struct berval bv;
718                 bv.bv_len = i;
719                 bv.bv_val = (char *) ptr;
720                 if (a) {
721                         a->a_next = (Attribute *)bptr;
722                 }
723                 a = (Attribute *)bptr;
724                 ad = NULL;
725                 rc = slap_bv2ad( &bv, &ad, &text );
726
727                 if( rc != LDAP_SUCCESS ) {
728 #ifdef NEW_LOGGING
729                         LDAP_LOG( OPERATION, INFO, 
730                                 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
731 #else
732                         Debug( LDAP_DEBUG_TRACE,
733                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
734 #endif
735                         rc = slap_bv2undef_ad( &bv, &ad, &text );
736
737                         if( rc != LDAP_SUCCESS ) {
738 #ifdef NEW_LOGGING
739                                 LDAP_LOG( OPERATION, INFO, 
740                                         "entry_decode:  str2undef_ad(%s): %s\n", ptr, text, 0 );
741 #else
742                                 Debug( LDAP_DEBUG_ANY,
743                                         "<= entry_decode: str2undef_ad(%s): %s\n",
744                                                 ptr, text, 0 );
745 #endif
746                                 return rc;
747                         }
748                 }
749                 ptr += i + 1;
750                 a->a_desc = ad;
751                 bptr = (BerVarray)(a+1);
752                 a->a_vals = bptr;
753                 a->a_flags = 0;
754                 count = j = entry_getlen(&ptr);
755
756                 while (j) {
757                         i = entry_getlen(&ptr);
758                         bptr->bv_len = i;
759                         bptr->bv_val = (char *)ptr;
760                         ptr += i+1;
761                         bptr++;
762                         j--;
763                 }
764                 bptr->bv_val = NULL;
765                 bptr->bv_len = 0;
766                 bptr++;
767
768                 j = entry_getlen(&ptr);
769                 if (j) {
770                         a->a_nvals = bptr;
771                         while (j) {
772                                 i = entry_getlen(&ptr);
773                                 bptr->bv_len = i;
774                                 bptr->bv_val = (char *)ptr;
775                                 ptr += i+1;
776                                 bptr++;
777                                 j--;
778                         }
779                         bptr->bv_val = NULL;
780                         bptr->bv_len = 0;
781                         bptr++;
782                 } else {
783                         a->a_nvals = a->a_vals;
784                 }
785         }
786
787         if (a) a->a_next = NULL;
788 #ifdef NEW_LOGGING
789         LDAP_LOG( OPERATION, DETAIL1, "entry_decode:  %s\n", x->e_dn, 0, 0 );
790 #else
791         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
792                 x->e_dn, 0, 0 );
793 #endif
794         *e = x;
795         return 0;
796 }
797
798 Entry *entry_dup( Entry *e )
799 {
800         Entry *ret;
801
802         ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
803
804         ret->e_id = e->e_id;
805         ber_dupbv( &ret->e_name, &e->e_name );
806         ber_dupbv( &ret->e_nname, &e->e_nname );
807         ret->e_attrs = attrs_dup( e->e_attrs );
808         ret->e_ocflags = e->e_ocflags;
809         ret->e_bv.bv_val = NULL;
810         ret->e_bv.bv_len = 0;
811         ret->e_private = NULL;
812
813         return ret;
814 }
815