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