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