]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
ITS#5322 don't try to free a NULL locker
[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 /*
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         for ( i=0; i<=lines; i++ ) {
245                 ad_prev = ad;
246                 if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
247                         ad = NULL;
248                         rc = slap_bv2ad( type+i, &ad, &text );
249
250                         if( rc != LDAP_SUCCESS ) {
251                                 Debug( slapMode & SLAP_TOOL_MODE
252                                         ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
253                                         "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
254                                 if( slapMode & SLAP_TOOL_MODE ) {
255                                         goto fail;
256                                 }
257
258                                 rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
259                                 if( rc != LDAP_SUCCESS ) {
260                                         Debug( LDAP_DEBUG_ANY,
261                                                 "<= str2entry: slap_str2undef_ad(%s): %s\n",
262                                                         type[i].bv_val, text, 0 );
263                                         goto fail;
264                                 }
265                         }
266
267                         /* require ';binary' when appropriate (ITS#5071) */
268                         if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
269                                 Debug( LDAP_DEBUG_ANY,
270                                         "str2entry: attributeType %s #%d: "
271                                         "needs ';binary' transfer as per syntax %s\n", 
272                                         ad->ad_cname.bv_val, 0,
273                                         ad->ad_type->sat_syntax->ssyn_oid );
274                                 goto fail;
275                         }
276                 }
277
278                 if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
279                         int j, k;
280                         /* FIXME: we only need this when migrating from an unsorted DB */
281                         if ( atail != &ahead && atail->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
282                                 rc = slap_sort_vals( (Modifications *)atail, &text, &j, NULL );
283                                 if ( rc == LDAP_SUCCESS ) {
284                                         atail->a_flags |= SLAP_ATTR_SORTED_VALS;
285                                 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
286                                         Debug( LDAP_DEBUG_ANY,
287                                                 "str2entry: attributeType %s value #%d provided more than once\n",
288                                                 atail->a_desc->ad_cname.bv_val, j, 0 );
289                                         goto fail;
290                                 }
291                         }
292                         atail->a_next = attr_alloc( NULL );
293                         atail = atail->a_next;
294                         atail->a_flags = 0;
295                         atail->a_numvals = attr_cnt;
296                         atail->a_desc = ad_prev;
297                         atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
298                         if( ad_prev->ad_type->sat_equality &&
299                                 ad_prev->ad_type->sat_equality->smr_normalize )
300                                 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
301                         else
302                                 atail->a_nvals = NULL;
303                         k = i - attr_cnt;
304                         for ( j=0; j<attr_cnt; j++ ) {
305                                 if ( freeval[k] )
306                                         atail->a_vals[j] = vals[k];
307                                 else
308                                         ber_dupbv( atail->a_vals+j, &vals[k] );
309                                 vals[k].bv_val = NULL;
310                                 if ( atail->a_nvals ) {
311                                         atail->a_nvals[j] = nvals[k];
312                                         nvals[k].bv_val = NULL;
313                                 }
314                                 k++;
315                         }
316                         BER_BVZERO( &atail->a_vals[j] );
317                         if ( atail->a_nvals ) {
318                                 BER_BVZERO( &atail->a_nvals[j] );
319                         } else {
320                                 atail->a_nvals = atail->a_vals;
321                         }
322                         attr_cnt = 0;
323                         if ( i == lines ) break;
324                 }
325
326                 if ( BER_BVISNULL( &vals[i] ) ) {
327                         Debug( LDAP_DEBUG_ANY,
328                                 "str2entry: attributeType %s #%d: "
329                                 "no value\n", 
330                                 ad->ad_cname.bv_val, attr_cnt, 0 );
331                         goto fail;
332                 }
333
334                 if( slapMode & SLAP_TOOL_MODE ) {
335                         struct berval pval;
336                         slap_syntax_validate_func *validate =
337                                 ad->ad_type->sat_syntax->ssyn_validate;
338                         slap_syntax_transform_func *pretty =
339                                 ad->ad_type->sat_syntax->ssyn_pretty;
340
341                         if ( pretty ) {
342                                 rc = ordered_value_pretty( ad,
343                                         &vals[i], &pval, NULL );
344
345                         } else if ( validate ) {
346                                 /*
347                                  * validate value per syntax
348                                  */
349                                 rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
350
351                         } else {
352                                 Debug( LDAP_DEBUG_ANY,
353                                         "str2entry: attributeType %s #%d: "
354                                         "no validator for syntax %s\n", 
355                                         ad->ad_cname.bv_val, attr_cnt,
356                                         ad->ad_type->sat_syntax->ssyn_oid );
357                                 goto fail;
358                         }
359
360                         if( rc != 0 ) {
361                                 Debug( LDAP_DEBUG_ANY,
362                                         "str2entry: invalid value "
363                                         "for attributeType %s #%d (syntax %s)\n",
364                                         ad->ad_cname.bv_val, attr_cnt,
365                                         ad->ad_type->sat_syntax->ssyn_oid );
366                                 goto fail;
367                         }
368
369                         if( pretty ) {
370                                 if ( freeval[i] ) free( vals[i].bv_val );
371                                 vals[i] = pval;
372                                 freeval[i] = 1;
373                         }
374                 }
375
376                 if ( ad->ad_type->sat_equality &&
377                         ad->ad_type->sat_equality->smr_normalize )
378                 {
379                         rc = ordered_value_normalize(
380                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
381                                 ad,
382                                 ad->ad_type->sat_equality,
383                                 &vals[i], &nvals[i], NULL );
384
385                         if ( rc ) {
386                                 Debug( LDAP_DEBUG_ANY,
387                                         "<= str2entry NULL (smr_normalize %s %d)\n", ad->ad_cname.bv_val, rc, 0 );
388                                 goto fail;
389                         }
390                 }
391
392                 attr_cnt++;
393         }
394
395         free( type );
396         atail->a_next = NULL;
397         e->e_attrs = ahead.a_next;
398
399         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
400                 e->e_dn, (unsigned long) e, 0 );
401         return( e );
402
403 fail:
404         for ( i=0; i<lines; i++ ) {
405                 if ( freeval[i] ) free( vals[i].bv_val );
406                 free( nvals[i].bv_val );
407         }
408         free( type );
409         entry_free( e );
410         return NULL;
411 }
412
413
414 #define GRABSIZE        BUFSIZ
415
416 #define MAKE_SPACE( n ) { \
417                 while ( ecur + (n) > ebuf + emaxsize ) { \
418                         ptrdiff_t       offset; \
419                         offset = (int) (ecur - ebuf); \
420                         ebuf = ch_realloc( ebuf, \
421                                 emaxsize + GRABSIZE ); \
422                         emaxsize += GRABSIZE; \
423                         ecur = ebuf + offset; \
424                 } \
425         }
426
427 char *
428 entry2str(
429         Entry   *e,
430         int             *len )
431 {
432         Attribute       *a;
433         struct berval   *bv;
434         int             i;
435         ber_len_t tmplen;
436
437         assert( e != NULL );
438
439         /*
440          * In string format, an entry looks like this:
441          *      dn: <dn>\n
442          *      [<attr>: <value>\n]*
443          */
444
445         ecur = ebuf;
446
447         /* put the dn */
448         if ( e->e_dn != NULL ) {
449                 /* put "dn: <dn>" */
450                 tmplen = e->e_name.bv_len;
451                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
452                 ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
453         }
454
455         /* put the attributes */
456         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
457                 /* put "<type>:[:] <value>" line for each value */
458                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
459                         bv = &a->a_vals[i];
460                         tmplen = a->a_desc->ad_cname.bv_len;
461                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
462                         ldif_sput( &ecur, LDIF_PUT_VALUE,
463                                 a->a_desc->ad_cname.bv_val,
464                                 bv->bv_val, bv->bv_len );
465                 }
466         }
467         MAKE_SPACE( 1 );
468         *ecur = '\0';
469         *len = ecur - ebuf;
470
471         return( ebuf );
472 }
473
474 void
475 entry_clean( Entry *e )
476 {
477         /* free an entry structure */
478         assert( e != NULL );
479
480         /* e_private must be freed by the caller */
481         assert( e->e_private == NULL );
482
483         e->e_id = 0;
484
485         /* free DNs */
486         if ( !BER_BVISNULL( &e->e_name ) ) {
487                 free( e->e_name.bv_val );
488                 BER_BVZERO( &e->e_name );
489         }
490         if ( !BER_BVISNULL( &e->e_nname ) ) {
491                 free( e->e_nname.bv_val );
492                 BER_BVZERO( &e->e_nname );
493         }
494
495         if ( !BER_BVISNULL( &e->e_bv ) ) {
496                 free( e->e_bv.bv_val );
497                 BER_BVZERO( &e->e_bv );
498         }
499
500         /* free attributes */
501         if ( e->e_attrs ) {
502                 attrs_free( e->e_attrs );
503                 e->e_attrs = NULL;
504         }
505
506         e->e_ocflags = 0;
507 }
508
509 void
510 entry_free( Entry *e )
511 {
512         entry_clean( e );
513
514         ldap_pvt_thread_mutex_lock( &entry_mutex );
515         e->e_private = entry_list;
516         entry_list = e;
517         ldap_pvt_thread_mutex_unlock( &entry_mutex );
518 }
519
520 /* These parameters work well on AMD64 */
521 #if 0
522 #define STRIDE 8
523 #define STRIPE 5
524 #else
525 #define STRIDE 1
526 #define STRIPE 1
527 #endif
528 #define STRIDE_FACTOR (STRIDE*STRIPE)
529
530 int
531 entry_prealloc( int num )
532 {
533         Entry *e, **prev, *tmp;
534         slap_list *s;
535         int i, j;
536
537         if (!num) return 0;
538
539 #if STRIDE_FACTOR > 1
540         /* Round up to our stride factor */
541         num += STRIDE_FACTOR-1;
542         num /= STRIDE_FACTOR;
543         num *= STRIDE_FACTOR;
544 #endif
545
546         s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
547         s->next = entry_chunks;
548         entry_chunks = s;
549
550         prev = &tmp;
551         for (i=0; i<STRIPE; i++) {
552                 e = (Entry *)(s+1);
553                 e += i;
554                 for (j=i; j<num; j+= STRIDE) {
555                         *prev = e;
556                         prev = (Entry **)&e->e_private;
557                         e += STRIDE;
558                 }
559         }
560         *prev = entry_list;
561         entry_list = (Entry *)(s+1);
562
563         return 0;
564 }
565
566 Entry *
567 entry_alloc( void )
568 {
569         Entry *e;
570
571         ldap_pvt_thread_mutex_lock( &entry_mutex );
572         if ( !entry_list )
573                 entry_prealloc( CHUNK_SIZE );
574         e = entry_list;
575         entry_list = e->e_private;
576         e->e_private = NULL;
577         ldap_pvt_thread_mutex_unlock( &entry_mutex );
578
579         return e;
580 }
581
582
583 /*
584  * These routines are used only by Backend.
585  *
586  * the Entry has three entry points (ways to find things):
587  *
588  *      by entry        e.g., if you already have an entry from the cache
589  *                      and want to delete it. (really by entry ptr)
590  *      by dn           e.g., when looking for the base object of a search
591  *      by id           e.g., for search candidates
592  *
593  * these correspond to three different avl trees that are maintained.
594  */
595
596 int
597 entry_cmp( Entry *e1, Entry *e2 )
598 {
599         return SLAP_PTRCMP( e1, e2 );
600 }
601
602 int
603 entry_dn_cmp( const void *v_e1, const void *v_e2 )
604 {
605         /* compare their normalized UPPERCASED dn's */
606         const Entry *e1 = v_e1, *e2 = v_e2;
607
608         return ber_bvcmp( &e1->e_nname, &e2->e_nname );
609 }
610
611 int
612 entry_id_cmp( const void *v_e1, const void *v_e2 )
613 {
614         const Entry *e1 = v_e1, *e2 = v_e2;
615         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
616 }
617
618 /* This is like a ber_len */
619 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
620         ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
621
622 static void
623 entry_putlen(unsigned char **buf, ber_len_t len)
624 {
625         ber_len_t lenlen = entry_lenlen(len);
626
627         if (lenlen == 1) {
628                 **buf = (unsigned char) len;
629         } else {
630                 int i;
631                 **buf = 0x80 | ((unsigned char) lenlen - 1);
632                 for (i=lenlen-1; i>0; i--) {
633                         (*buf)[i] = (unsigned char) len;
634                         len >>= 8;
635                 }
636         }
637         *buf += lenlen;
638 }
639
640 static ber_len_t
641 entry_getlen(unsigned char **buf)
642 {
643         ber_len_t len;
644         int i;
645
646         len = *(*buf)++;
647         if (len <= 0x7f)
648                 return len;
649         i = len & 0x7f;
650         len = 0;
651         for (;i > 0; i--) {
652                 len <<= 8;
653                 len |= *(*buf)++;
654         }
655         return len;
656 }
657
658 /* Count up the sizes of the components of an entry */
659 void entry_partsize(Entry *e, ber_len_t *plen,
660         int *pnattrs, int *pnvals, int norm)
661 {
662         ber_len_t len, dnlen, ndnlen;
663         int i, nat = 0, nval = 0;
664         Attribute *a;
665
666         dnlen = e->e_name.bv_len;
667         len = dnlen + 1;        /* trailing NUL byte */
668         len += entry_lenlen(dnlen);
669         if (norm) {
670                 ndnlen = e->e_nname.bv_len;
671                 len += ndnlen + 1;
672                 len += entry_lenlen(ndnlen);
673         }
674         for (a=e->e_attrs; a; a=a->a_next) {
675                 /* For AttributeDesc, we only store the attr name */
676                 nat++;
677                 len += a->a_desc->ad_cname.bv_len+1;
678                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
679                 for (i=0; a->a_vals[i].bv_val; i++) {
680                         nval++;
681                         len += a->a_vals[i].bv_len + 1;
682                         len += entry_lenlen(a->a_vals[i].bv_len);
683                 }
684                 len += entry_lenlen(i);
685                 nval++; /* empty berval at end */
686                 if (norm && a->a_nvals != a->a_vals) {
687                         for (i=0; a->a_nvals[i].bv_val; i++) {
688                                 nval++;
689                                 len += a->a_nvals[i].bv_len + 1;
690                                 len += entry_lenlen(a->a_nvals[i].bv_len);
691                         }
692                         len += entry_lenlen(i); /* i nvals */
693                         nval++;
694                 } else {
695                         len += entry_lenlen(0); /* 0 nvals */
696                 }
697         }
698         len += entry_lenlen(nat);
699         len += entry_lenlen(nval);
700         *plen = len;
701         *pnattrs = nat;
702         *pnvals = nval;
703 }
704
705 /* Add up the size of the entry for a flattened buffer */
706 ber_len_t entry_flatsize(Entry *e, int norm)
707 {
708         ber_len_t len;
709         int nattrs, nvals;
710
711         entry_partsize(e, &len, &nattrs, &nvals, norm);
712         len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
713                 (nvals * sizeof(struct berval));
714         return len;
715 }
716
717 /* Flatten an Entry into a buffer. The buffer is filled with just the
718  * strings/bervals of all the entry components. Each field is preceded
719  * by its length, encoded the way ber_put_len works. Every field is NUL
720  * terminated.  The entire buffer size is precomputed so that a single
721  * malloc can be performed. The entry size is also recorded,
722  * to aid in entry_decode.
723  */
724 int entry_encode(Entry *e, struct berval *bv)
725 {
726         ber_len_t len, dnlen, ndnlen;
727         int i, nattrs, nvals;
728         Attribute *a;
729         unsigned char *ptr;
730
731         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
732                 (long) e->e_id, e->e_dn, 0 );
733         dnlen = e->e_name.bv_len;
734         ndnlen = e->e_nname.bv_len;
735
736         entry_partsize( e, &len, &nattrs, &nvals, 1 );
737
738         bv->bv_len = len;
739         bv->bv_val = ch_malloc(len);
740         ptr = (unsigned char *)bv->bv_val;
741         entry_putlen(&ptr, nattrs);
742         entry_putlen(&ptr, nvals);
743         entry_putlen(&ptr, dnlen);
744         AC_MEMCPY(ptr, e->e_dn, dnlen);
745         ptr += dnlen;
746         *ptr++ = '\0';
747         entry_putlen(&ptr, ndnlen);
748         AC_MEMCPY(ptr, e->e_ndn, ndnlen);
749         ptr += ndnlen;
750         *ptr++ = '\0';
751
752         for (a=e->e_attrs; a; a=a->a_next) {
753                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
754                 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
755                         a->a_desc->ad_cname.bv_len);
756                 ptr += a->a_desc->ad_cname.bv_len;
757                 *ptr++ = '\0';
758                 if (a->a_vals) {
759                         for (i=0; a->a_vals[i].bv_val; i++);
760                         assert( i == a->a_numvals );
761                         entry_putlen(&ptr, i);
762                         for (i=0; a->a_vals[i].bv_val; i++) {
763                                 entry_putlen(&ptr, a->a_vals[i].bv_len);
764                                 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
765                                         a->a_vals[i].bv_len);
766                                 ptr += a->a_vals[i].bv_len;
767                                 *ptr++ = '\0';
768                         }
769                         if (a->a_nvals != a->a_vals) {
770                                 entry_putlen(&ptr, i);
771                                 for (i=0; a->a_nvals[i].bv_val; i++) {
772                                         entry_putlen(&ptr, a->a_nvals[i].bv_len);
773                                         AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
774                                         a->a_nvals[i].bv_len);
775                                         ptr += a->a_nvals[i].bv_len;
776                                         *ptr++ = '\0';
777                                 }
778                         } else {
779                                 entry_putlen(&ptr, 0);
780                         }
781                 }
782         }
783         return 0;
784 }
785
786 /* Retrieve an Entry that was stored using entry_encode above.
787  * First entry_header must be called to decode the size of the entry.
788  * Then a single block of memory must be malloc'd to accomodate the
789  * bervals and the bulk data. Next the bulk data is retrieved from
790  * the DB and parsed by entry_decode.
791  *
792  * Note: everything is stored in a single contiguous block, so
793  * you can not free individual attributes or names from this
794  * structure. Attempting to do so will likely corrupt memory.
795  */
796 int entry_header(EntryHeader *eh)
797 {
798         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
799
800         eh->nattrs = entry_getlen(&ptr);
801         if ( !eh->nattrs ) {
802                 Debug( LDAP_DEBUG_ANY,
803                         "entry_header: attribute count was zero\n", 0, 0, 0);
804                 return LDAP_OTHER;
805         }
806         eh->nvals = entry_getlen(&ptr);
807         if ( !eh->nvals ) {
808                 Debug( LDAP_DEBUG_ANY,
809                         "entry_header: value count was zero\n", 0, 0, 0);
810                 return LDAP_OTHER;
811         }
812         eh->data = (char *)ptr;
813         return LDAP_SUCCESS;
814 }
815
816 #ifdef SLAP_ZONE_ALLOC
817 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
818 #else
819 int entry_decode(EntryHeader *eh, Entry **e)
820 #endif
821 {
822         int i, j, nattrs, nvals;
823         int rc;
824         Attribute *a;
825         Entry *x;
826         const char *text;
827         AttributeDescription *ad;
828         unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
829         BerVarray bptr;
830
831         nattrs = eh->nattrs;
832         nvals = eh->nvals;
833         x = entry_alloc();
834         x->e_attrs = attrs_alloc( nattrs );
835         ptr = (unsigned char *)eh->data;
836         i = entry_getlen(&ptr);
837         x->e_name.bv_val = (char *) ptr;
838         x->e_name.bv_len = i;
839         ptr += i+1;
840         i = entry_getlen(&ptr);
841         x->e_nname.bv_val = (char *) ptr;
842         x->e_nname.bv_len = i;
843         ptr += i+1;
844         Debug( LDAP_DEBUG_TRACE,
845                 "entry_decode: \"%s\"\n",
846                 x->e_dn, 0, 0 );
847         x->e_bv = eh->bv;
848
849         a = x->e_attrs;
850         bptr = (BerVarray)eh->bv.bv_val;
851
852         while ((i = entry_getlen(&ptr))) {
853                 struct berval bv;
854                 bv.bv_len = i;
855                 bv.bv_val = (char *) ptr;
856                 ad = NULL;
857                 rc = slap_bv2ad( &bv, &ad, &text );
858
859                 if( rc != LDAP_SUCCESS ) {
860                         Debug( LDAP_DEBUG_TRACE,
861                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
862                         rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
863
864                         if( rc != LDAP_SUCCESS ) {
865                                 Debug( LDAP_DEBUG_ANY,
866                                         "<= entry_decode: slap_str2undef_ad(%s): %s\n",
867                                                 ptr, text, 0 );
868                                 return rc;
869                         }
870                 }
871                 ptr += i + 1;
872                 a->a_desc = ad;
873                 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
874                 j = entry_getlen(&ptr);
875                 a->a_numvals = j;
876                 a->a_vals = bptr;
877
878                 while (j) {
879                         i = entry_getlen(&ptr);
880                         bptr->bv_len = i;
881                         bptr->bv_val = (char *)ptr;
882                         ptr += i+1;
883                         bptr++;
884                         j--;
885                 }
886                 bptr->bv_val = NULL;
887                 bptr->bv_len = 0;
888                 bptr++;
889
890                 j = entry_getlen(&ptr);
891                 if (j) {
892                         a->a_nvals = bptr;
893                         while (j) {
894                                 i = entry_getlen(&ptr);
895                                 bptr->bv_len = i;
896                                 bptr->bv_val = (char *)ptr;
897                                 ptr += i+1;
898                                 bptr++;
899                                 j--;
900                         }
901                         bptr->bv_val = NULL;
902                         bptr->bv_len = 0;
903                         bptr++;
904                 } else {
905                         a->a_nvals = a->a_vals;
906                 }
907                 /* FIXME: This is redundant once a sorted entry is saved into the DB */
908                 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
909                         rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
910                         if ( rc == LDAP_SUCCESS ) {
911                                 a->a_flags |= SLAP_ATTR_SORTED_VALS;
912                         } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
913                                 /* should never happen */
914                                 Debug( LDAP_DEBUG_ANY,
915                                         "entry_decode: attributeType %s value #%d provided more than once\n",
916                                         a->a_desc->ad_cname.bv_val, j, 0 );
917                                 return rc;
918                         }
919                 }
920                 a = a->a_next;
921                 nattrs--;
922                 if ( !nattrs )
923                         break;
924         }
925
926         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
927                 x->e_dn, 0, 0 );
928         *e = x;
929         return 0;
930 }
931
932 Entry *entry_dup( Entry *e )
933 {
934         Entry *ret;
935
936         ret = entry_alloc();
937
938         ret->e_id = e->e_id;
939         ber_dupbv( &ret->e_name, &e->e_name );
940         ber_dupbv( &ret->e_nname, &e->e_nname );
941         ret->e_attrs = attrs_dup( e->e_attrs );
942         ret->e_ocflags = e->e_ocflags;
943
944         return ret;
945 }
946
947 #if 1
948 /* Duplicates an entry using a single malloc. Saves CPU time, increases
949  * heap usage because a single large malloc is harder to satisfy than
950  * lots of small ones, and the freed space isn't as easily reusable.
951  *
952  * Probably not worth using this function.
953  */
954 Entry *entry_dup_bv( Entry *e )
955 {
956         ber_len_t len;
957         int nattrs, nvals;
958         Entry *ret;
959         struct berval *bvl;
960         char *ptr;
961         Attribute *src, *dst;
962
963         ret = entry_alloc();
964
965         entry_partsize(e, &len, &nattrs, &nvals, 1);
966         ret->e_id = e->e_id;
967         ret->e_attrs = attrs_alloc( nattrs );
968         ret->e_ocflags = e->e_ocflags;
969         ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
970         ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
971
972         bvl = (struct berval *)ret->e_bv.bv_val;
973         ptr = (char *)(bvl + nvals);
974
975         ret->e_name.bv_len = e->e_name.bv_len;
976         ret->e_name.bv_val = ptr;
977         AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
978         ptr += e->e_name.bv_len;
979         *ptr++ = '\0';
980
981         ret->e_nname.bv_len = e->e_nname.bv_len;
982         ret->e_nname.bv_val = ptr;
983         AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
984         ptr += e->e_name.bv_len;
985         *ptr++ = '\0';
986
987         dst = ret->e_attrs;
988         for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
989                 int i;
990                 dst->a_desc = src->a_desc;
991                 dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
992                 dst->a_vals = bvl;
993                 dst->a_numvals = src->a_numvals;
994                 for ( i=0; src->a_vals[i].bv_val; i++ ) {
995                         bvl->bv_len = src->a_vals[i].bv_len;
996                         bvl->bv_val = ptr;
997                         AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
998                         ptr += bvl->bv_len;
999                         *ptr++ = '\0';
1000                         bvl++;
1001                 }
1002                 BER_BVZERO(bvl);
1003                 bvl++;
1004                 if ( src->a_vals != src->a_nvals ) {
1005                         dst->a_nvals = bvl;
1006                         for ( i=0; src->a_nvals[i].bv_val; i++ ) {
1007                                 bvl->bv_len = src->a_nvals[i].bv_len;
1008                                 bvl->bv_val = ptr;
1009                                 AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );
1010                                 ptr += bvl->bv_len;
1011                                 *ptr++ = '\0';
1012                                 bvl++;
1013                         }
1014                         BER_BVZERO(bvl);
1015                         bvl++;
1016                 }
1017         }
1018         return ret;
1019 }
1020 #endif