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-2021 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
entry_destroy(void)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
entry_init(void)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 *
str2entry(char * s)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 *
str2entry2(char * s,int checkvals)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" );
136
137 e = entry_alloc();
138
139 if( e == NULL ) {
140 Debug( LDAP_DEBUG_ANY,
141 "<= str2entry NULL (entry allocation failed)\n" );
142 return( NULL );
143 }
144
145 /* initialize entry */
146 e->e_id = NOID;
147
148 /* dn + attributes */
149 atail = &ahead;
150 ahead.a_next = NULL;
151 ad = NULL;
152 ad_prev = NULL;
153 attr_cnt = 0;
154 next = s;
155
156 lines = ldif_countlines( s );
157 type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
158 vals = type+lines+1;
159 nvals = vals+lines+1;
160 freeval = (char *)(nvals+lines+1);
161 i = -1;
162
163 /* parse into individual values, record DN */
164 while ( (s = ldif_getline( &next )) != NULL ) {
165 int freev;
166 if ( *s == '\n' || *s == '\0' ) {
167 break;
168 }
169 i++;
170 if (i >= lines) {
171 Debug( LDAP_DEBUG_TRACE,
172 "<= str2entry ran past end of entry\n" );
173 goto fail;
174 }
175
176 rc = ldif_parse_line2( s, type+i, vals+i, &freev );
177 freeval[i] = freev;
178 if ( rc ) {
179 Debug( LDAP_DEBUG_TRACE,
180 "<= str2entry NULL (parse_line)\n" );
181 continue;
182 }
183
184 if ( bvcasematch( &type[i], &dn_bv ) ) {
185 if ( e->e_dn != NULL ) {
186 Debug( LDAP_DEBUG_ANY, "str2entry: "
187 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
188 (long) e->e_id, e->e_dn, vals[i].bv_val );
189 goto fail;
190 }
191
192 rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
193 if( rc != LDAP_SUCCESS ) {
194 Debug( LDAP_DEBUG_ANY, "str2entry: "
195 "entry %ld has invalid DN \"%s\"\n",
196 (long) e->e_id, vals[i].bv_val );
197 goto fail;
198 }
199 if ( freeval[i] ) free( vals[i].bv_val );
200 vals[i].bv_val = NULL;
201 i--;
202 continue;
203 }
204 }
205 lines = i+1;
206
207 /* check to make sure there was a dn: line */
208 if ( BER_BVISNULL( &e->e_name )) {
209 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
210 (long) e->e_id );
211 goto fail;
212 }
213
214 /* Make sure all attributes with multiple values are contiguous */
215 if ( checkvals ) {
216 int j, k;
217 struct berval bv;
218 int fv;
219
220 for (i=0; i<lines; i++) {
221 for ( j=i+1; j<lines; j++ ) {
222 if ( bvcasematch( type+i, type+j )) {
223 /* out of order, move intervening attributes down */
224 if ( j != i+1 ) {
225 bv = vals[j];
226 fv = freeval[j];
227 for ( k=j; k>i; k-- ) {
228 type[k] = type[k-1];
229 vals[k] = vals[k-1];
230 freeval[k] = freeval[k-1];
231 }
232 k++;
233 type[k] = type[i];
234 vals[k] = bv;
235 freeval[k] = fv;
236 }
237 i++;
238 }
239 }
240 }
241 }
242
243 if ( lines > 0 ) {
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 int wtool = ( slapMode & (SLAP_TOOL_MODE|SLAP_TOOL_READONLY|SLAP_TOOL_NO_SCHEMA_CHECK) ) == SLAP_TOOL_MODE;
252 Debug( wtool ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
253 "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text );
254 if( wtool ) {
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 );
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 atail->a_next = attr_alloc( NULL );
281 atail = atail->a_next;
282 atail->a_flags = 0;
283 atail->a_numvals = attr_cnt;
284 atail->a_desc = ad_prev;
285 atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
286 if( ad_prev->ad_type->sat_equality &&
287 ad_prev->ad_type->sat_equality->smr_normalize )
288 atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
289 else
290 atail->a_nvals = NULL;
291 k = i - attr_cnt;
292 for ( j=0; j<attr_cnt; j++ ) {
293 if ( freeval[k] )
294 atail->a_vals[j] = vals[k];
295 else
296 ber_dupbv( atail->a_vals+j, &vals[k] );
297 vals[k].bv_val = NULL;
298 if ( atail->a_nvals ) {
299 atail->a_nvals[j] = nvals[k];
300 nvals[k].bv_val = NULL;
301 }
302 k++;
303 }
304 BER_BVZERO( &atail->a_vals[j] );
305 if ( atail->a_nvals ) {
306 BER_BVZERO( &atail->a_nvals[j] );
307 } else {
308 atail->a_nvals = atail->a_vals;
309 }
310 attr_cnt = 0;
311 /* FIXME: we only need this when migrating from an unsorted DB */
312 if ( atail->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
313 rc = slap_sort_vals( (Modifications *)atail, &text, &j, NULL );
314 if ( rc == LDAP_SUCCESS ) {
315 atail->a_flags |= SLAP_ATTR_SORTED_VALS;
316 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
317 Debug( LDAP_DEBUG_ANY,
318 "str2entry: attributeType %s value #%d provided more than once\n",
319 atail->a_desc->ad_cname.bv_val, j );
320 goto fail;
321 }
322 }
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 );
331 goto fail;
332 }
333
334 if ( ad->ad_type->sat_equality &&
335 ad->ad_type->sat_equality->smr_normalize )
336 {
337 rc = ordered_value_normalize(
338 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
339 ad,
340 ad->ad_type->sat_equality,
341 &vals[i], &nvals[i], NULL );
342
343 if ( rc ) {
344 Debug( LDAP_DEBUG_ANY,
345 "<= str2entry NULL (smr_normalize %s %d)\n", ad->ad_cname.bv_val, rc );
346 goto fail;
347 }
348 }
349
350 attr_cnt++;
351 }
352 }
353
354 free( type );
355 atail->a_next = NULL;
356 e->e_attrs = ahead.a_next;
357
358 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
359 e->e_dn, (unsigned long) e );
360 return( e );
361
362 fail:
363 for ( i=0; i<lines; i++ ) {
364 if ( freeval[i] ) free( vals[i].bv_val );
365 free( nvals[i].bv_val );
366 }
367 free( type );
368 entry_free( e );
369 return NULL;
370 }
371
372
373 #define GRABSIZE BUFSIZ
374
375 #define MAKE_SPACE( n ) { \
376 while ( ecur + (n) > ebuf + emaxsize ) { \
377 ptrdiff_t offset; \
378 offset = (int) (ecur - ebuf); \
379 ebuf = ch_realloc( ebuf, \
380 emaxsize + GRABSIZE ); \
381 emaxsize += GRABSIZE; \
382 ecur = ebuf + offset; \
383 } \
384 }
385
386 /* NOTE: only preserved for binary compatibility */
387 char *
entry2str(Entry * e,int * len)388 entry2str(
389 Entry *e,
390 int *len )
391 {
392 return entry2str_wrap( e, len, LDIF_LINE_WIDTH );
393 }
394
395 char *
entry2str_wrap(Entry * e,int * len,ber_len_t wrap)396 entry2str_wrap(
397 Entry *e,
398 int *len,
399 ber_len_t wrap )
400 {
401 Attribute *a;
402 struct berval *bv;
403 int i;
404 ber_len_t tmplen;
405
406 assert( e != NULL );
407
408 /*
409 * In string format, an entry looks like this:
410 * dn: <dn>\n
411 * [<attr>: <value>\n]*
412 */
413
414 ecur = ebuf;
415
416 /* put the dn */
417 if ( e->e_dn != NULL ) {
418 /* put "dn: <dn>" */
419 tmplen = e->e_name.bv_len;
420 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
421 ldif_sput_wrap( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen, wrap );
422 }
423
424 /* put the attributes */
425 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
426 /* put "<type>:[:] <value>" line for each value */
427 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
428 bv = &a->a_vals[i];
429 tmplen = a->a_desc->ad_cname.bv_len;
430 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
431 ldif_sput_wrap( &ecur, LDIF_PUT_VALUE,
432 a->a_desc->ad_cname.bv_val,
433 bv->bv_val, bv->bv_len, wrap );
434 }
435 }
436 MAKE_SPACE( 1 );
437 *ecur = '\0';
438 *len = ecur - ebuf;
439
440 return( ebuf );
441 }
442
443 void
entry_clean(Entry * e)444 entry_clean( Entry *e )
445 {
446 /* free an entry structure */
447 assert( e != NULL );
448
449 /* e_private must be freed by the caller */
450 assert( e->e_private == NULL );
451
452 e->e_id = 0;
453
454 /* free DNs */
455 if ( !BER_BVISNULL( &e->e_name ) ) {
456 free( e->e_name.bv_val );
457 BER_BVZERO( &e->e_name );
458 }
459 if ( !BER_BVISNULL( &e->e_nname ) ) {
460 free( e->e_nname.bv_val );
461 BER_BVZERO( &e->e_nname );
462 }
463
464 if ( !BER_BVISNULL( &e->e_bv ) ) {
465 free( e->e_bv.bv_val );
466 BER_BVZERO( &e->e_bv );
467 }
468
469 /* free attributes */
470 if ( e->e_attrs ) {
471 attrs_free( e->e_attrs );
472 e->e_attrs = NULL;
473 }
474
475 e->e_ocflags = 0;
476 }
477
478 void
entry_free(Entry * e)479 entry_free( Entry *e )
480 {
481 entry_clean( e );
482
483 ldap_pvt_thread_mutex_lock( &entry_mutex );
484 e->e_private = entry_list;
485 entry_list = e;
486 ldap_pvt_thread_mutex_unlock( &entry_mutex );
487 }
488
489 /* These parameters work well on AMD64 */
490 #if 0
491 #define STRIDE 8
492 #define STRIPE 5
493 #else
494 #define STRIDE 1
495 #define STRIPE 1
496 #endif
497 #define STRIDE_FACTOR (STRIDE*STRIPE)
498
499 int
entry_prealloc(int num)500 entry_prealloc( int num )
501 {
502 Entry *e, **prev, *tmp;
503 slap_list *s;
504 int i, j;
505
506 if (!num) return 0;
507
508 #if STRIDE_FACTOR > 1
509 /* Round up to our stride factor */
510 num += STRIDE_FACTOR-1;
511 num /= STRIDE_FACTOR;
512 num *= STRIDE_FACTOR;
513 #endif
514
515 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
516 s->next = entry_chunks;
517 entry_chunks = s;
518
519 prev = &tmp;
520 for (i=0; i<STRIPE; i++) {
521 e = (Entry *)(s+1);
522 e += i;
523 for (j=i; j<num; j+= STRIDE) {
524 *prev = e;
525 prev = (Entry **)&e->e_private;
526 e += STRIDE;
527 }
528 }
529 *prev = entry_list;
530 entry_list = (Entry *)(s+1);
531
532 return 0;
533 }
534
535 Entry *
entry_alloc(void)536 entry_alloc( void )
537 {
538 Entry *e;
539
540 ldap_pvt_thread_mutex_lock( &entry_mutex );
541 if ( !entry_list )
542 entry_prealloc( CHUNK_SIZE );
543 e = entry_list;
544 entry_list = e->e_private;
545 e->e_private = NULL;
546 ldap_pvt_thread_mutex_unlock( &entry_mutex );
547
548 return e;
549 }
550
551
552 /*
553 * These routines are used only by Backend.
554 *
555 * the Entry has three entry points (ways to find things):
556 *
557 * by entry e.g., if you already have an entry from the cache
558 * and want to delete it. (really by entry ptr)
559 * by dn e.g., when looking for the base object of a search
560 * by id e.g., for search candidates
561 *
562 * these correspond to three different avl trees that are maintained.
563 */
564
565 int
entry_cmp(Entry * e1,Entry * e2)566 entry_cmp( Entry *e1, Entry *e2 )
567 {
568 return SLAP_PTRCMP( e1, e2 );
569 }
570
571 int
entry_dn_cmp(const void * v_e1,const void * v_e2)572 entry_dn_cmp( const void *v_e1, const void *v_e2 )
573 {
574 /* compare their normalized UPPERCASED dn's */
575 const Entry *e1 = v_e1, *e2 = v_e2;
576
577 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
578 }
579
580 int
entry_id_cmp(const void * v_e1,const void * v_e2)581 entry_id_cmp( const void *v_e1, const void *v_e2 )
582 {
583 const Entry *e1 = v_e1, *e2 = v_e2;
584 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
585 }
586
587 /* This is like a ber_len */
588 #define entry_lenlen(l) (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
589 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
590
591 static void
entry_putlen(unsigned char ** buf,ber_len_t len)592 entry_putlen(unsigned char **buf, ber_len_t len)
593 {
594 ber_len_t lenlen = entry_lenlen(len);
595
596 if (lenlen == 1) {
597 **buf = (unsigned char) len;
598 } else {
599 int i;
600 **buf = 0x80 | ((unsigned char) lenlen - 1);
601 for (i=lenlen-1; i>0; i--) {
602 (*buf)[i] = (unsigned char) len;
603 len >>= 8;
604 }
605 }
606 *buf += lenlen;
607 }
608
609 static ber_len_t
entry_getlen(unsigned char ** buf)610 entry_getlen(unsigned char **buf)
611 {
612 ber_len_t len;
613 int i;
614
615 len = *(*buf)++;
616 if (len <= 0x7f)
617 return len;
618 i = len & 0x7f;
619 len = 0;
620 for (;i > 0; i--) {
621 len <<= 8;
622 len |= *(*buf)++;
623 }
624 return len;
625 }
626
627 /* Count up the sizes of the components of an entry */
entry_partsize(Entry * e,ber_len_t * plen,int * pnattrs,int * pnvals,int norm)628 void entry_partsize(Entry *e, ber_len_t *plen,
629 int *pnattrs, int *pnvals, int norm)
630 {
631 ber_len_t len, dnlen, ndnlen;
632 int i, nat = 0, nval = 0;
633 Attribute *a;
634
635 dnlen = e->e_name.bv_len;
636 len = dnlen + 1; /* trailing NUL byte */
637 len += entry_lenlen(dnlen);
638 if (norm) {
639 ndnlen = e->e_nname.bv_len;
640 len += ndnlen + 1;
641 len += entry_lenlen(ndnlen);
642 }
643 for (a=e->e_attrs; a; a=a->a_next) {
644 /* For AttributeDesc, we only store the attr name */
645 nat++;
646 len += a->a_desc->ad_cname.bv_len+1;
647 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
648 for (i=0; a->a_vals[i].bv_val; i++) {
649 nval++;
650 len += a->a_vals[i].bv_len + 1;
651 len += entry_lenlen(a->a_vals[i].bv_len);
652 }
653 len += entry_lenlen(i);
654 nval++; /* empty berval at end */
655 if (norm && a->a_nvals != a->a_vals) {
656 for (i=0; a->a_nvals[i].bv_val; i++) {
657 nval++;
658 len += a->a_nvals[i].bv_len + 1;
659 len += entry_lenlen(a->a_nvals[i].bv_len);
660 }
661 len += entry_lenlen(i); /* i nvals */
662 nval++;
663 } else {
664 len += entry_lenlen(0); /* 0 nvals */
665 }
666 }
667 len += entry_lenlen(nat);
668 len += entry_lenlen(nval);
669 *plen = len;
670 *pnattrs = nat;
671 *pnvals = nval;
672 }
673
674 /* Add up the size of the entry for a flattened buffer */
entry_flatsize(Entry * e,int norm)675 ber_len_t entry_flatsize(Entry *e, int norm)
676 {
677 ber_len_t len;
678 int nattrs, nvals;
679
680 entry_partsize(e, &len, &nattrs, &nvals, norm);
681 len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
682 (nvals * sizeof(struct berval));
683 return len;
684 }
685
686 /* Flatten an Entry into a buffer. The buffer is filled with just the
687 * strings/bervals of all the entry components. Each field is preceded
688 * by its length, encoded the way ber_put_len works. Every field is NUL
689 * terminated. The entire buffer size is precomputed so that a single
690 * malloc can be performed. The entry size is also recorded,
691 * to aid in entry_decode.
692 */
entry_encode(Entry * e,struct berval * bv)693 int entry_encode(Entry *e, struct berval *bv)
694 {
695 ber_len_t len, dnlen, ndnlen, i;
696 int nattrs, nvals;
697 Attribute *a;
698 unsigned char *ptr;
699
700 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
701 (long) e->e_id, e->e_dn );
702
703 dnlen = e->e_name.bv_len;
704 ndnlen = e->e_nname.bv_len;
705
706 entry_partsize( e, &len, &nattrs, &nvals, 1 );
707
708 bv->bv_len = len;
709 bv->bv_val = ch_malloc(len);
710 ptr = (unsigned char *)bv->bv_val;
711 entry_putlen(&ptr, nattrs);
712 entry_putlen(&ptr, nvals);
713 entry_putlen(&ptr, dnlen);
714 AC_MEMCPY(ptr, e->e_dn, dnlen);
715 ptr += dnlen;
716 *ptr++ = '\0';
717 entry_putlen(&ptr, ndnlen);
718 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
719 ptr += ndnlen;
720 *ptr++ = '\0';
721
722 for (a=e->e_attrs; a; a=a->a_next) {
723 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
724 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
725 a->a_desc->ad_cname.bv_len);
726 ptr += a->a_desc->ad_cname.bv_len;
727 *ptr++ = '\0';
728 if (a->a_vals) {
729 for (i=0; a->a_vals[i].bv_val; i++);
730 assert( i == a->a_numvals );
731 entry_putlen(&ptr, i);
732 for (i=0; a->a_vals[i].bv_val; i++) {
733 entry_putlen(&ptr, a->a_vals[i].bv_len);
734 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
735 a->a_vals[i].bv_len);
736 ptr += a->a_vals[i].bv_len;
737 *ptr++ = '\0';
738 }
739 if (a->a_nvals != a->a_vals) {
740 entry_putlen(&ptr, i);
741 for (i=0; a->a_nvals[i].bv_val; i++) {
742 entry_putlen(&ptr, a->a_nvals[i].bv_len);
743 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
744 a->a_nvals[i].bv_len);
745 ptr += a->a_nvals[i].bv_len;
746 *ptr++ = '\0';
747 }
748 } else {
749 entry_putlen(&ptr, 0);
750 }
751 }
752 }
753
754 Debug( LDAP_DEBUG_TRACE, "<= entry_encode(0x%08lx): %s\n",
755 (long) e->e_id, e->e_dn );
756
757 return 0;
758 }
759
760 /* Retrieve an Entry that was stored using entry_encode above.
761 * First entry_header must be called to decode the size of the entry.
762 * Then a single block of memory must be malloc'd to accommodate the
763 * bervals and the bulk data. Next the bulk data is retrieved from
764 * the DB and parsed by entry_decode.
765 *
766 * Note: everything is stored in a single contiguous block, so
767 * you can not free individual attributes or names from this
768 * structure. Attempting to do so will likely corrupt memory.
769 */
entry_header(EntryHeader * eh)770 int entry_header(EntryHeader *eh)
771 {
772 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
773
774 /* Some overlays can create empty entries
775 * so don't check for zeros here.
776 */
777 eh->nattrs = entry_getlen(&ptr);
778 eh->nvals = entry_getlen(&ptr);
779 eh->data = (char *)ptr;
780 return LDAP_SUCCESS;
781 }
782
783 int
entry_decode_dn(EntryHeader * eh,struct berval * dn,struct berval * ndn)784 entry_decode_dn( EntryHeader *eh, struct berval *dn, struct berval *ndn )
785 {
786 int i;
787 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
788
789 assert( dn != NULL || ndn != NULL );
790
791 ptr = (unsigned char *)eh->data;
792 i = entry_getlen(&ptr);
793 if ( dn != NULL ) {
794 dn->bv_val = (char *) ptr;
795 dn->bv_len = i;
796 }
797
798 if ( ndn != NULL ) {
799 ptr += i + 1;
800 i = entry_getlen(&ptr);
801 ndn->bv_val = (char *) ptr;
802 ndn->bv_len = i;
803 }
804
805 Debug( LDAP_DEBUG_TRACE,
806 "entry_decode_dn: \"%s\"\n",
807 dn ? dn->bv_val : ndn->bv_val );
808
809 return 0;
810 }
811
812 #ifdef SLAP_ZONE_ALLOC
entry_decode(EntryHeader * eh,Entry ** e,void * ctx)813 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
814 #else
815 int entry_decode(EntryHeader *eh, Entry **e)
816 #endif
817 {
818 int i, j, nattrs, nvals;
819 int rc;
820 Attribute *a;
821 Entry *x;
822 const char *text;
823 AttributeDescription *ad;
824 unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
825 BerVarray bptr;
826
827 nattrs = eh->nattrs;
828 nvals = eh->nvals;
829 x = entry_alloc();
830 x->e_attrs = attrs_alloc( nattrs );
831 ptr = (unsigned char *)eh->data;
832 i = entry_getlen(&ptr);
833 x->e_name.bv_val = (char *) ptr;
834 x->e_name.bv_len = i;
835 ptr += i+1;
836 i = entry_getlen(&ptr);
837 x->e_nname.bv_val = (char *) ptr;
838 x->e_nname.bv_len = i;
839 ptr += i+1;
840 Debug( LDAP_DEBUG_TRACE,
841 "entry_decode: \"%s\"\n",
842 x->e_dn );
843 x->e_bv = eh->bv;
844
845 a = x->e_attrs;
846 bptr = (BerVarray)eh->bv.bv_val;
847
848 while (((char *)ptr - eh->bv.bv_val < eh->bv.bv_len) &&
849 (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 );
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 );
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 );
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 );
925 *e = x;
926 return 0;
927 }
928
929 Entry *
entry_dup2(Entry * dest,Entry * source)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 *
entry_dup(Entry * e)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 */
entry_dup_bv(Entry * e)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
1026