]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
ITS#8616 don't check for existing value when deleting values
[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-2018 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 /*
51  * these mutexes must be used when calling the entry2str()
52  * routine since it returns a pointer to static data.
53  */
54 ldap_pvt_thread_mutex_t entry2str_mutex;
55
56 static const struct berval dn_bv = BER_BVC("dn");
57
58 /*
59  * Entry free list
60  *
61  * Allocate in chunks, minimum of 1000 at a time.
62  */
63 #define CHUNK_SIZE      1000
64 typedef struct slap_list {
65         struct slap_list *next;
66 } slap_list;
67 static slap_list *entry_chunks;
68 static Entry *entry_list;
69 static ldap_pvt_thread_mutex_t entry_mutex;
70
71 int entry_destroy(void)
72 {
73         slap_list *e;
74         if ( ebuf ) free( ebuf );
75         ebuf = NULL;
76         ecur = NULL;
77         emaxsize = 0;
78
79         for ( e=entry_chunks; e; e=entry_chunks ) {
80                 entry_chunks = e->next;
81                 free( e );
82         }
83
84         ldap_pvt_thread_mutex_destroy( &entry_mutex );
85         ldap_pvt_thread_mutex_destroy( &entry2str_mutex );
86         return attr_destroy();
87 }
88
89 int
90 entry_init(void)
91 {
92         ldap_pvt_thread_mutex_init( &entry2str_mutex );
93         ldap_pvt_thread_mutex_init( &entry_mutex );
94         return attr_init();
95 }
96
97 Entry *
98 str2entry( char *s )
99 {
100         return str2entry2( s, 1 );
101 }
102
103 #define bvcasematch(bv1, bv2)   (ber_bvstrcasecmp(bv1, bv2) == 0)
104
105 Entry *
106 str2entry2( char *s, int checkvals )
107 {
108         int rc;
109         Entry           *e;
110         struct berval   *type, *vals, *nvals;
111         char    *freeval;
112         AttributeDescription *ad, *ad_prev;
113         const char *text;
114         char    *next;
115         int             attr_cnt;
116         int             i, lines;
117         Attribute       ahead, *atail;
118
119         /*
120          * LDIF is used as the string format.
121          * An entry looks like this:
122          *
123          *      dn: <dn>\n
124          *      [<attr>:[:] <value>\n]
125          *      [<tab><continuedvalue>\n]*
126          *      ...
127          *
128          * If a double colon is used after a type, it means the
129          * following value is encoded as a base 64 string.  This
130          * happens if the value contains a non-printing character
131          * or newline.
132          */
133
134         Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
135                 s ? s : "NULL", 0, 0 );
136
137         e = entry_alloc();
138
139         if( e == NULL ) {
140                 Debug( LDAP_DEBUG_ANY,
141                         "<= str2entry NULL (entry allocation failed)\n",
142                         0, 0, 0 );
143                 return( NULL );
144         }
145
146         /* initialize entry */
147         e->e_id = NOID;
148
149         /* dn + attributes */
150         atail = &ahead;
151         ahead.a_next = NULL;
152         ad = NULL;
153         ad_prev = NULL;
154         attr_cnt = 0;
155         next = s;
156
157         lines = ldif_countlines( s );
158         type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
159         vals = type+lines+1;
160         nvals = vals+lines+1;
161         freeval = (char *)(nvals+lines+1);
162         i = -1;
163
164         /* parse into individual values, record DN */
165         while ( (s = ldif_getline( &next )) != NULL ) {
166                 int freev;
167                 if ( *s == '\n' || *s == '\0' ) {
168                         break;
169                 }
170                 i++;
171                 if (i >= lines) {
172                         Debug( LDAP_DEBUG_TRACE,
173                                 "<= str2entry ran past end of entry\n", 0, 0, 0 );
174                         goto fail;
175                 }
176
177                 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
178                 freeval[i] = freev;
179                 if ( rc ) {
180                         Debug( LDAP_DEBUG_TRACE,
181                                 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
182                         continue;
183                 }
184
185                 if ( bvcasematch( &type[i], &dn_bv ) ) {
186                         if ( e->e_dn != NULL ) {
187                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
188                                         "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
189                                         (long) e->e_id, e->e_dn, vals[i].bv_val );
190                                 goto fail;
191                         }
192
193                         rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
194                         if( rc != LDAP_SUCCESS ) {
195                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
196                                         "entry %ld has invalid DN \"%s\"\n",
197                                         (long) e->e_id, vals[i].bv_val, 0 );
198                                 goto fail;
199                         }
200                         if ( freeval[i] ) free( vals[i].bv_val );
201                         vals[i].bv_val = NULL;
202                         i--;
203                         continue;
204                 }
205         }
206         lines = i+1;
207
208         /* check to make sure there was a dn: line */
209         if ( BER_BVISNULL( &e->e_name )) {
210                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
211                         (long) e->e_id, 0, 0 );
212                 goto fail;
213         }
214
215         /* Make sure all attributes with multiple values are contiguous */
216         if ( checkvals ) {
217                 int j, k;
218                 struct berval bv;
219                 int fv;
220
221                 for (i=0; i<lines; i++) {
222                         for ( j=i+1; j<lines; j++ ) {
223                                 if ( bvcasematch( type+i, type+j )) {
224                                         /* out of order, move intervening attributes down */
225                                         if ( j != i+1 ) {
226                                                 bv = vals[j];
227                                                 fv = freeval[j];
228                                                 for ( k=j; k>i; k-- ) {
229                                                         type[k] = type[k-1];
230                                                         vals[k] = vals[k-1];
231                                                         freeval[k] = freeval[k-1];
232                                                 }
233                                                 k++;
234                                                 type[k] = type[i];
235                                                 vals[k] = bv;
236                                                 freeval[k] = fv;
237                                         }
238                                         i++;
239                                 }
240                         }
241                 }
242         }
243
244         if ( lines > 0 ) {
245                 for ( i=0; i<=lines; i++ ) {
246                         ad_prev = ad;
247                         if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
248                                 ad = NULL;
249                                 rc = slap_bv2ad( type+i, &ad, &text );
250         
251                                 if( rc != LDAP_SUCCESS ) {
252                                         int wtool = ( slapMode & (SLAP_TOOL_MODE|SLAP_TOOL_READONLY) ) == SLAP_TOOL_MODE;
253                                         Debug( wtool ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
254                                                 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
255                                         if( wtool ) {
256                                                 goto fail;
257                                         }
258         
259                                         rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
260                                         if( rc != LDAP_SUCCESS ) {
261                                                 Debug( LDAP_DEBUG_ANY,
262                                                         "<= str2entry: slap_str2undef_ad(%s): %s\n",
263                                                                 type[i].bv_val, text, 0 );
264                                                 goto fail;
265                                         }
266                                 }
267         
268                                 /* require ';binary' when appropriate (ITS#5071) */
269                                 if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
270                                         Debug( LDAP_DEBUG_ANY,
271                                                 "str2entry: attributeType %s #%d: "
272                                                 "needs ';binary' transfer as per syntax %s\n", 
273                                                 ad->ad_cname.bv_val, 0,
274                                                 ad->ad_type->sat_syntax->ssyn_oid );
275                                         goto fail;
276                                 }
277                         }
278         
279                         if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
280                                 int j, k;
281                                 atail->a_next = attr_alloc( NULL );
282                                 atail = atail->a_next;
283                                 atail->a_flags = 0;
284                                 atail->a_numvals = attr_cnt;
285                                 atail->a_desc = ad_prev;
286                                 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
287                                 if( ad_prev->ad_type->sat_equality &&
288                                         ad_prev->ad_type->sat_equality->smr_normalize )
289                                         atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
290                                 else
291                                         atail->a_nvals = NULL;
292                                 k = i - attr_cnt;
293                                 for ( j=0; j<attr_cnt; j++ ) {
294                                         if ( freeval[k] )
295                                                 atail->a_vals[j] = vals[k];
296                                         else
297                                                 ber_dupbv( atail->a_vals+j, &vals[k] );
298                                         vals[k].bv_val = NULL;
299                                         if ( atail->a_nvals ) {
300                                                 atail->a_nvals[j] = nvals[k];
301                                                 nvals[k].bv_val = NULL;
302                                         }
303                                         k++;
304                                 }
305                                 BER_BVZERO( &atail->a_vals[j] );
306                                 if ( atail->a_nvals ) {
307                                         BER_BVZERO( &atail->a_nvals[j] );
308                                 } else {
309                                         atail->a_nvals = atail->a_vals;
310                                 }
311                                 attr_cnt = 0;
312                                 /* FIXME: we only need this when migrating from an unsorted DB */
313                                 if ( atail->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
314                                         rc = slap_sort_vals( (Modifications *)atail, &text, &j, NULL );
315                                         if ( rc == LDAP_SUCCESS ) {
316                                                 atail->a_flags |= SLAP_ATTR_SORTED_VALS;
317                                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
318                                                 Debug( LDAP_DEBUG_ANY,
319                                                         "str2entry: attributeType %s value #%d provided more than once\n",
320                                                         atail->a_desc->ad_cname.bv_val, j, 0 );
321                                                 goto fail;
322                                         }
323                                 }
324                                 if ( i == lines ) break;
325                         }
326         
327                         if ( BER_BVISNULL( &vals[i] ) ) {
328                                 Debug( LDAP_DEBUG_ANY,
329                                         "str2entry: attributeType %s #%d: "
330                                         "no value\n", 
331                                         ad->ad_cname.bv_val, attr_cnt, 0 );
332                                 goto fail;
333                         }
334         
335                         if ( ad->ad_type->sat_equality &&
336                                 ad->ad_type->sat_equality->smr_normalize )
337                         {
338                                 rc = ordered_value_normalize(
339                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
340                                         ad,
341                                         ad->ad_type->sat_equality,
342                                         &vals[i], &nvals[i], NULL );
343         
344                                 if ( rc ) {
345                                         Debug( LDAP_DEBUG_ANY,
346                                                 "<= str2entry NULL (smr_normalize %s %d)\n", ad->ad_cname.bv_val, rc, 0 );
347                                         goto fail;
348                                 }
349                         }
350         
351                         attr_cnt++;
352                 }
353         }
354
355         free( type );
356         atail->a_next = NULL;
357         e->e_attrs = ahead.a_next;
358
359         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
360                 e->e_dn, (unsigned long) e, 0 );
361         return( e );
362
363 fail:
364         for ( i=0; i<lines; i++ ) {
365                 if ( freeval[i] ) free( vals[i].bv_val );
366                 free( nvals[i].bv_val );
367         }
368         free( type );
369         entry_free( e );
370         return NULL;
371 }
372
373
374 #define GRABSIZE        BUFSIZ
375
376 #define MAKE_SPACE( n ) { \
377                 while ( ecur + (n) > ebuf + emaxsize ) { \
378                         ptrdiff_t       offset; \
379                         offset = (int) (ecur - ebuf); \
380                         ebuf = ch_realloc( ebuf, \
381                                 emaxsize + GRABSIZE ); \
382                         emaxsize += GRABSIZE; \
383                         ecur = ebuf + offset; \
384                 } \
385         }
386
387 /* NOTE: only preserved for binary compatibility */
388 char *
389 entry2str(
390         Entry   *e,
391         int             *len )
392 {
393         return entry2str_wrap( e, len, LDIF_LINE_WIDTH );
394 }
395
396 char *
397 entry2str_wrap(
398         Entry           *e,
399         int                     *len,
400         ber_len_t       wrap )
401 {
402         Attribute       *a;
403         struct berval   *bv;
404         int             i;
405         ber_len_t tmplen;
406
407         assert( e != NULL );
408
409         /*
410          * In string format, an entry looks like this:
411          *      dn: <dn>\n
412          *      [<attr>: <value>\n]*
413          */
414
415         ecur = ebuf;
416
417         /* put the dn */
418         if ( e->e_dn != NULL ) {
419                 /* put "dn: <dn>" */
420                 tmplen = e->e_name.bv_len;
421                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
422                 ldif_sput_wrap( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen, wrap );
423         }
424
425         /* put the attributes */
426         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
427                 /* put "<type>:[:] <value>" line for each value */
428                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
429                         bv = &a->a_vals[i];
430                         tmplen = a->a_desc->ad_cname.bv_len;
431                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
432                         ldif_sput_wrap( &ecur, LDIF_PUT_VALUE,
433                                 a->a_desc->ad_cname.bv_val,
434                                 bv->bv_val, bv->bv_len, wrap );
435                 }
436         }
437         MAKE_SPACE( 1 );
438         *ecur = '\0';
439         *len = ecur - ebuf;
440
441         return( ebuf );
442 }
443
444 void
445 entry_clean( Entry *e )
446 {
447         /* free an entry structure */
448         assert( e != NULL );
449
450         /* e_private must be freed by the caller */
451         assert( e->e_private == NULL );
452
453         e->e_id = 0;
454
455         /* free DNs */
456         if ( !BER_BVISNULL( &e->e_name ) ) {
457                 free( e->e_name.bv_val );
458                 BER_BVZERO( &e->e_name );
459         }
460         if ( !BER_BVISNULL( &e->e_nname ) ) {
461                 free( e->e_nname.bv_val );
462                 BER_BVZERO( &e->e_nname );
463         }
464
465         if ( !BER_BVISNULL( &e->e_bv ) ) {
466                 free( e->e_bv.bv_val );
467                 BER_BVZERO( &e->e_bv );
468         }
469
470         /* free attributes */
471         if ( e->e_attrs ) {
472                 attrs_free( e->e_attrs );
473                 e->e_attrs = NULL;
474         }
475
476         e->e_ocflags = 0;
477 }
478
479 void
480 entry_free( Entry *e )
481 {
482         entry_clean( e );
483
484         ldap_pvt_thread_mutex_lock( &entry_mutex );
485         e->e_private = entry_list;
486         entry_list = e;
487         ldap_pvt_thread_mutex_unlock( &entry_mutex );
488 }
489
490 /* These parameters work well on AMD64 */
491 #if 0
492 #define STRIDE 8
493 #define STRIPE 5
494 #else
495 #define STRIDE 1
496 #define STRIPE 1
497 #endif
498 #define STRIDE_FACTOR (STRIDE*STRIPE)
499
500 int
501 entry_prealloc( int num )
502 {
503         Entry *e, **prev, *tmp;
504         slap_list *s;
505         int i, j;
506
507         if (!num) return 0;
508
509 #if STRIDE_FACTOR > 1
510         /* Round up to our stride factor */
511         num += STRIDE_FACTOR-1;
512         num /= STRIDE_FACTOR;
513         num *= STRIDE_FACTOR;
514 #endif
515
516         s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
517         s->next = entry_chunks;
518         entry_chunks = s;
519
520         prev = &tmp;
521         for (i=0; i<STRIPE; i++) {
522                 e = (Entry *)(s+1);
523                 e += i;
524                 for (j=i; j<num; j+= STRIDE) {
525                         *prev = e;
526                         prev = (Entry **)&e->e_private;
527                         e += STRIDE;
528                 }
529         }
530         *prev = entry_list;
531         entry_list = (Entry *)(s+1);
532
533         return 0;
534 }
535
536 Entry *
537 entry_alloc( void )
538 {
539         Entry *e;
540
541         ldap_pvt_thread_mutex_lock( &entry_mutex );
542         if ( !entry_list )
543                 entry_prealloc( CHUNK_SIZE );
544         e = entry_list;
545         entry_list = e->e_private;
546         e->e_private = NULL;
547         ldap_pvt_thread_mutex_unlock( &entry_mutex );
548
549         return e;
550 }
551
552
553 /*
554  * These routines are used only by Backend.
555  *
556  * the Entry has three entry points (ways to find things):
557  *
558  *      by entry        e.g., if you already have an entry from the cache
559  *                      and want to delete it. (really by entry ptr)
560  *      by dn           e.g., when looking for the base object of a search
561  *      by id           e.g., for search candidates
562  *
563  * these correspond to three different avl trees that are maintained.
564  */
565
566 int
567 entry_cmp( Entry *e1, Entry *e2 )
568 {
569         return SLAP_PTRCMP( e1, e2 );
570 }
571
572 int
573 entry_dn_cmp( const void *v_e1, const void *v_e2 )
574 {
575         /* compare their normalized UPPERCASED dn's */
576         const Entry *e1 = v_e1, *e2 = v_e2;
577
578         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
579 }
580
581 int
582 entry_id_cmp( const void *v_e1, const void *v_e2 )
583 {
584         const Entry *e1 = v_e1, *e2 = v_e2;
585         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
586 }
587
588 /* This is like a ber_len */
589 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
590         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
591
592 static void
593 entry_putlen(unsigned char **buf, ber_len_t len)
594 {
595         ber_len_t lenlen = entry_lenlen(len);
596
597         if (lenlen == 1) {
598                 **buf = (unsigned char) len;
599         } else {
600                 int i;
601                 **buf = 0x80 | ((unsigned char) lenlen - 1);
602                 for (i=lenlen-1; i>0; i--) {
603                         (*buf)[i] = (unsigned char) len;
604                         len >>= 8;
605                 }
606         }
607         *buf += lenlen;
608 }
609
610 static ber_len_t
611 entry_getlen(unsigned char **buf)
612 {
613         ber_len_t len;
614         int i;
615
616         len = *(*buf)++;
617         if (len <= 0x7f)
618                 return len;
619         i = len & 0x7f;
620         len = 0;
621         for (;i > 0; i--) {
622                 len <<= 8;
623                 len |= *(*buf)++;
624         }
625         return len;
626 }
627
628 /* Count up the sizes of the components of an entry */
629 void entry_partsize(Entry *e, ber_len_t *plen,
630         int *pnattrs, int *pnvals, int norm)
631 {
632         ber_len_t len, dnlen, ndnlen;
633         int i, nat = 0, nval = 0;
634         Attribute *a;
635
636         dnlen = e->e_name.bv_len;
637         len = dnlen + 1;        /* trailing NUL byte */
638         len += entry_lenlen(dnlen);
639         if (norm) {
640                 ndnlen = e->e_nname.bv_len;
641                 len += ndnlen + 1;
642                 len += entry_lenlen(ndnlen);
643         }
644         for (a=e->e_attrs; a; a=a->a_next) {
645                 /* For AttributeDesc, we only store the attr name */
646                 nat++;
647                 len += a->a_desc->ad_cname.bv_len+1;
648                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
649                 for (i=0; a->a_vals[i].bv_val; i++) {
650                         nval++;
651                         len += a->a_vals[i].bv_len + 1;
652                         len += entry_lenlen(a->a_vals[i].bv_len);
653                 }
654                 len += entry_lenlen(i);
655                 nval++; /* empty berval at end */
656                 if (norm && a->a_nvals != a->a_vals) {
657                         for (i=0; a->a_nvals[i].bv_val; i++) {
658                                 nval++;
659                                 len += a->a_nvals[i].bv_len + 1;
660                                 len += entry_lenlen(a->a_nvals[i].bv_len);
661                         }
662                         len += entry_lenlen(i); /* i nvals */
663                         nval++;
664                 } else {
665                         len += entry_lenlen(0); /* 0 nvals */
666                 }
667         }
668         len += entry_lenlen(nat);
669         len += entry_lenlen(nval);
670         *plen = len;
671         *pnattrs = nat;
672         *pnvals = nval;
673 }
674
675 /* Add up the size of the entry for a flattened buffer */
676 ber_len_t entry_flatsize(Entry *e, int norm)
677 {
678         ber_len_t len;
679         int nattrs, nvals;
680
681         entry_partsize(e, &len, &nattrs, &nvals, norm);
682         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
683                 (nvals * sizeof(struct berval));
684         return len;
685 }
686
687 /* Flatten an Entry into a buffer. The buffer is filled with just the
688  * strings/bervals of all the entry components. Each field is preceded
689  * by its length, encoded the way ber_put_len works. Every field is NUL
690  * terminated.  The entire buffer size is precomputed so that a single
691  * malloc can be performed. The entry size is also recorded,
692  * to aid in entry_decode.
693  */
694 int entry_encode(Entry *e, struct berval *bv)
695 {
696         ber_len_t len, dnlen, ndnlen, i;
697         int nattrs, nvals;
698         Attribute *a;
699         unsigned char *ptr;
700
701         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
702                 (long) e->e_id, e->e_dn, 0 );
703
704         dnlen = e->e_name.bv_len;
705         ndnlen = e->e_nname.bv_len;
706
707         entry_partsize( e, &len, &nattrs, &nvals, 1 );
708
709         bv->bv_len = len;
710         bv->bv_val = ch_malloc(len);
711         ptr = (unsigned char *)bv->bv_val;
712         entry_putlen(&ptr, nattrs);
713         entry_putlen(&ptr, nvals);
714         entry_putlen(&ptr, dnlen);
715         AC_MEMCPY(ptr, e->e_dn, dnlen);
716         ptr += dnlen;
717         *ptr++ = '\0';
718         entry_putlen(&ptr, ndnlen);
719         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
720         ptr += ndnlen;
721         *ptr++ = '\0';
722
723         for (a=e->e_attrs; a; a=a->a_next) {
724                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
725                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
726                         a->a_desc->ad_cname.bv_len);
727                 ptr += a->a_desc->ad_cname.bv_len;
728                 *ptr++ = '\0';
729                 if (a->a_vals) {
730                         for (i=0; a->a_vals[i].bv_val; i++);
731                         assert( i == a->a_numvals );
732                         entry_putlen(&ptr, i);
733                         for (i=0; a->a_vals[i].bv_val; i++) {
734                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
735                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
736                                         a->a_vals[i].bv_len);
737                                 ptr += a->a_vals[i].bv_len;
738                                 *ptr++ = '\0';
739                         }
740                         if (a->a_nvals != a->a_vals) {
741                                 entry_putlen(&ptr, i);
742                                 for (i=0; a->a_nvals[i].bv_val; i++) {
743                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
744                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
745                                         a->a_nvals[i].bv_len);
746                                         ptr += a->a_nvals[i].bv_len;
747                                         *ptr++ = '\0';
748                                 }
749                         } else {
750                                 entry_putlen(&ptr, 0);
751                         }
752                 }
753         }
754
755         Debug( LDAP_DEBUG_TRACE, "<= entry_encode(0x%08lx): %s\n",
756                 (long) e->e_id, e->e_dn, 0 );
757
758         return 0;
759 }
760
761 /* Retrieve an Entry that was stored using entry_encode above.
762  * First entry_header must be called to decode the size of the entry.
763  * Then a single block of memory must be malloc'd to accomodate the
764  * bervals and the bulk data. Next the bulk data is retrieved from
765  * the DB and parsed by entry_decode.
766  *
767  * Note: everything is stored in a single contiguous block, so
768  * you can not free individual attributes or names from this
769  * structure. Attempting to do so will likely corrupt memory.
770  */
771 int entry_header(EntryHeader *eh)
772 {
773         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
774
775         /* Some overlays can create empty entries
776          * so don't check for zeros here.
777          */
778         eh->nattrs = entry_getlen(&ptr);
779         eh->nvals = entry_getlen(&ptr);
780         eh->data = (char *)ptr;
781         return LDAP_SUCCESS;
782 }
783
784 int
785 entry_decode_dn( EntryHeader *eh, struct berval *dn, struct berval *ndn )
786 {
787         int i;
788         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
789
790         assert( dn != NULL || ndn != NULL );
791
792         ptr = (unsigned char *)eh->data;
793         i = entry_getlen(&ptr);
794         if ( dn != NULL ) {
795                 dn->bv_val = (char *) ptr;
796                 dn->bv_len = i;
797         }
798
799         if ( ndn != NULL ) {
800                 ptr += i + 1;
801                 i = entry_getlen(&ptr);
802                 ndn->bv_val = (char *) ptr;
803                 ndn->bv_len = i;
804         }
805
806         Debug( LDAP_DEBUG_TRACE,
807                 "entry_decode_dn: \"%s\"\n",
808                 dn ? dn->bv_val : ndn->bv_val, 0, 0 );
809
810         return 0;
811 }
812
813 #ifdef SLAP_ZONE_ALLOC
814 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
815 #else
816 int entry_decode(EntryHeader *eh, Entry **e)
817 #endif
818 {
819         int i, j, nattrs, nvals;
820         int rc;
821         Attribute *a;
822         Entry *x;
823         const char *text;
824         AttributeDescription *ad;
825         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
826         BerVarray bptr;
827
828         nattrs = eh->nattrs;
829         nvals = eh->nvals;
830         x = entry_alloc();
831         x->e_attrs = attrs_alloc( nattrs );
832         ptr = (unsigned char *)eh->data;
833         i = entry_getlen(&ptr);
834         x->e_name.bv_val = (char *) ptr;
835         x->e_name.bv_len = i;
836         ptr += i+1;
837         i = entry_getlen(&ptr);
838         x->e_nname.bv_val = (char *) ptr;
839         x->e_nname.bv_len = i;
840         ptr += i+1;
841         Debug( LDAP_DEBUG_TRACE,
842                 "entry_decode: \"%s\"\n",
843                 x->e_dn, 0, 0 );
844         x->e_bv = eh->bv;
845
846         a = x->e_attrs;
847         bptr = (BerVarray)eh->bv.bv_val;
848
849         while ((i = entry_getlen(&ptr))) {
850                 struct berval bv;
851                 bv.bv_len = i;
852                 bv.bv_val = (char *) ptr;
853                 ad = NULL;
854                 rc = slap_bv2ad( &bv, &ad, &text );
855
856                 if( rc != LDAP_SUCCESS ) {
857                         Debug( LDAP_DEBUG_TRACE,
858                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
859                         rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
860
861                         if( rc != LDAP_SUCCESS ) {
862                                 Debug( LDAP_DEBUG_ANY,
863                                         "<= entry_decode: slap_str2undef_ad(%s): %s\n",
864                                                 ptr, text, 0 );
865                                 return rc;
866                         }
867                 }
868                 ptr += i + 1;
869                 a->a_desc = ad;
870                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
871                 j = entry_getlen(&ptr);
872                 a->a_numvals = j;
873                 a->a_vals = bptr;
874
875                 while (j) {
876                         i = entry_getlen(&ptr);
877                         bptr->bv_len = i;
878                         bptr->bv_val = (char *)ptr;
879                         ptr += i+1;
880                         bptr++;
881                         j--;
882                 }
883                 bptr->bv_val = NULL;
884                 bptr->bv_len = 0;
885                 bptr++;
886
887                 j = entry_getlen(&ptr);
888                 if (j) {
889                         a->a_nvals = bptr;
890                         while (j) {
891                                 i = entry_getlen(&ptr);
892                                 bptr->bv_len = i;
893                                 bptr->bv_val = (char *)ptr;
894                                 ptr += i+1;
895                                 bptr++;
896                                 j--;
897                         }
898                         bptr->bv_val = NULL;
899                         bptr->bv_len = 0;
900                         bptr++;
901                 } else {
902                         a->a_nvals = a->a_vals;
903                 }
904                 /* FIXME: This is redundant once a sorted entry is saved into the DB */
905                 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
906                         rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
907                         if ( rc == LDAP_SUCCESS ) {
908                                 a->a_flags |= SLAP_ATTR_SORTED_VALS;
909                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
910                                 /* should never happen */
911                                 Debug( LDAP_DEBUG_ANY,
912                                         "entry_decode: attributeType %s value #%d provided more than once\n",
913                                         a->a_desc->ad_cname.bv_val, j, 0 );
914                                 return rc;
915                         }
916                 }
917                 a = a->a_next;
918                 nattrs--;
919                 if ( !nattrs )
920                         break;
921         }
922
923         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
924                 x->e_dn, 0, 0 );
925         *e = x;
926         return 0;
927 }
928
929 Entry *
930 entry_dup2( Entry *dest, Entry *source )
931 {
932         assert( dest != NULL );
933         assert( source != NULL );
934
935         assert( dest->e_private == NULL );
936
937         dest->e_id = source->e_id;
938         ber_dupbv( &dest->e_name, &source->e_name );
939         ber_dupbv( &dest->e_nname, &source->e_nname );
940         dest->e_attrs = attrs_dup( source->e_attrs );
941         dest->e_ocflags = source->e_ocflags;
942
943         return dest;
944 }
945
946 Entry *
947 entry_dup( Entry *e )
948 {
949         return entry_dup2( entry_alloc(), e );
950 }
951
952 #if 1
953 /* Duplicates an entry using a single malloc. Saves CPU time, increases
954  * heap usage because a single large malloc is harder to satisfy than
955  * lots of small ones, and the freed space isn't as easily reusable.
956  *
957  * Probably not worth using this function.
958  */
959 Entry *entry_dup_bv( Entry *e )
960 {
961         ber_len_t len;
962         int nattrs, nvals;
963         Entry *ret;
964         struct berval *bvl;
965         char *ptr;
966         Attribute *src, *dst;
967
968         ret = entry_alloc();
969
970         entry_partsize(e, &len, &nattrs, &nvals, 1);
971         ret->e_id = e->e_id;
972         ret->e_attrs = attrs_alloc( nattrs );
973         ret->e_ocflags = e->e_ocflags;
974         ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
975         ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
976
977         bvl = (struct berval *)ret->e_bv.bv_val;
978         ptr = (char *)(bvl + nvals);
979
980         ret->e_name.bv_len = e->e_name.bv_len;
981         ret->e_name.bv_val = ptr;
982         AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
983         ptr += e->e_name.bv_len;
984         *ptr++ = '\0';
985
986         ret->e_nname.bv_len = e->e_nname.bv_len;
987         ret->e_nname.bv_val = ptr;
988         AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
989         ptr += e->e_name.bv_len;
990         *ptr++ = '\0';
991
992         dst = ret->e_attrs;
993         for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
994                 int i;
995                 dst->a_desc = src->a_desc;
996                 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
997                 dst->a_vals = bvl;
998                 dst->a_numvals = src->a_numvals;
999                 for ( i=0; src->a_vals[i].bv_val; i++ ) {
1000                         bvl->bv_len = src->a_vals[i].bv_len;
1001                         bvl->bv_val = ptr;
1002                         AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
1003                         ptr += bvl->bv_len;
1004                         *ptr++ = '\0';
1005                         bvl++;
1006                 }
1007                 BER_BVZERO(bvl);
1008                 bvl++;
1009                 if ( src->a_vals != src->a_nvals ) {
1010                         dst->a_nvals = bvl;
1011                         for ( i=0; src->a_nvals[i].bv_val; i++ ) {
1012                                 bvl->bv_len = src->a_nvals[i].bv_len;
1013                                 bvl->bv_val = ptr;
1014                                 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );
1015                                 ptr += bvl->bv_len;
1016                                 *ptr++ = '\0';
1017                                 bvl++;
1018                         }
1019                         BER_BVZERO(bvl);
1020                         bvl++;
1021                 }
1022         }
1023         return ret;
1024 }
1025 #endif