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