1 /* entry.c - routines for dealing with entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
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.
33 #include <ac/socket.h>
34 #include <ac/string.h>
39 static unsigned char *ebuf; /* buf returned by entry2str */
40 static unsigned char *ecur; /* pointer to end of currently used ebuf */
41 static int emaxsize;/* max size of ebuf */
46 const Entry slap_entry_root = {
47 NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
50 int entry_destroy(void)
52 if ( ebuf ) free( ebuf );
66 struct berval vals[2];
67 struct berval nvals[2], *nvalsp;
68 AttributeDescription *ad, *ad_prev;
74 * LDIF is used as the string format.
75 * An entry looks like this:
78 * [<attr>:[:] <value>\n]
79 * [<tab><continuedvalue>\n]*
82 * If a double colon is used after a type, it means the
83 * following value is encoded as a base 64 string. This
84 * happens if the value contains a non-printing character
88 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
89 s ? s : "NULL", 0, 0 );
91 /* initialize reader/writer lock */
92 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
95 Debug( LDAP_DEBUG_ANY,
96 "<= str2entry NULL (entry allocation failed)\n",
101 /* initialize entry */
104 /* dn + attributes */
106 vals[1].bv_val = NULL;
112 while ( (s = ldif_getline( &next )) != NULL ) {
113 if ( *s == '\n' || *s == '\0' ) {
117 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
118 Debug( LDAP_DEBUG_TRACE,
119 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
123 if ( strcasecmp( type, "dn" ) == 0 ) {
126 if ( e->e_dn != NULL ) {
127 Debug( LDAP_DEBUG_ANY, "str2entry: "
128 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
129 (long) e->e_id, e->e_dn, vals[0].bv_val );
130 free( vals[0].bv_val );
135 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname, NULL );
136 if( rc != LDAP_SUCCESS ) {
137 Debug( LDAP_DEBUG_ANY, "str2entry: "
138 "entry %ld has invalid DN \"%s\"\n",
139 (long) e->e_id, vals[0].bv_val, 0 );
141 free( vals[0].bv_val );
144 free( vals[0].bv_val );
150 rc = slap_str2ad( type, &ad, &text );
152 if( rc != LDAP_SUCCESS ) {
153 Debug( slapMode & SLAP_TOOL_MODE
154 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
155 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
156 if( slapMode & SLAP_TOOL_MODE ) {
158 free( vals[0].bv_val );
163 rc = slap_str2undef_ad( type, &ad, &text );
164 if( rc != LDAP_SUCCESS ) {
165 Debug( LDAP_DEBUG_ANY,
166 "<= str2entry: str2undef_ad(%s): %s\n",
169 free( vals[0].bv_val );
175 if ( ad != ad_prev ) {
179 if( slapMode & SLAP_TOOL_MODE ) {
181 slap_syntax_validate_func *validate =
182 ad->ad_type->sat_syntax->ssyn_validate;
183 slap_syntax_transform_func *pretty =
184 ad->ad_type->sat_syntax->ssyn_pretty;
187 rc = pretty( ad->ad_type->sat_syntax,
188 &vals[0], &pval, NULL );
190 } else if( validate ) {
192 * validate value per syntax
194 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
197 Debug( LDAP_DEBUG_ANY,
198 "str2entry: attributeType %s #%d: "
199 "no validator for syntax %s\n",
200 ad->ad_cname.bv_val, attr_cnt,
201 ad->ad_type->sat_syntax->ssyn_oid );
203 free( vals[0].bv_val );
209 Debug( LDAP_DEBUG_ANY,
210 "str2entry: invalid value "
211 "for attributeType %s #%d (syntax %s)\n",
212 ad->ad_cname.bv_val, attr_cnt,
213 ad->ad_type->sat_syntax->ssyn_oid );
215 free( vals[0].bv_val );
221 free( vals[0].bv_val );
227 nvals[0].bv_val = NULL;
229 if( ad->ad_type->sat_equality &&
230 ad->ad_type->sat_equality->smr_normalize )
232 rc = ad->ad_type->sat_equality->smr_normalize(
233 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
234 ad->ad_type->sat_syntax,
235 ad->ad_type->sat_equality,
236 &vals[0], &nvals[0], NULL );
239 Debug( LDAP_DEBUG_ANY,
240 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
243 free( vals[0].bv_val );
249 nvals[1].bv_val = NULL;
254 rc = attr_merge( e, ad, vals, nvalsp );
256 Debug( LDAP_DEBUG_ANY,
257 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
259 free( vals[0].bv_val );
265 free( vals[0].bv_val );
266 free( nvals[0].bv_val );
271 /* check to make sure there was a dn: line */
272 if ( e->e_dn == NULL ) {
273 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
274 (long) e->e_id, 0, 0 );
279 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
280 e->e_dn, (unsigned long) e, 0 );
285 #define GRABSIZE BUFSIZ
287 #define MAKE_SPACE( n ) { \
288 while ( ecur + (n) > ebuf + emaxsize ) { \
290 offset = (int) (ecur - ebuf); \
291 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
292 emaxsize + GRABSIZE ); \
293 emaxsize += GRABSIZE; \
294 ecur = ebuf + offset; \
311 * In string format, an entry looks like this:
313 * [<attr>: <value>\n]*
319 if ( e->e_dn != NULL ) {
321 tmplen = e->e_name.bv_len;
322 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
323 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
326 /* put the attributes */
327 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
328 /* put "<type>:[:] <value>" line for each value */
329 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
331 tmplen = a->a_desc->ad_cname.bv_len;
332 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
333 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
334 a->a_desc->ad_cname.bv_val,
335 bv->bv_val, bv->bv_len );
342 return( (char *) ebuf );
346 entry_clean( Entry *e )
348 /* free an entry structure */
351 /* e_private must be freed by the caller */
352 assert( e->e_private == NULL );
356 if ( !BER_BVISNULL( &e->e_name ) ) {
357 free( e->e_name.bv_val );
358 BER_BVZERO( &e->e_name );
360 if ( !BER_BVISNULL( &e->e_nname ) ) {
361 free( e->e_nname.bv_val );
362 BER_BVZERO( &e->e_nname );
365 if ( !BER_BVISNULL( &e->e_bv ) ) {
366 free( e->e_bv.bv_val );
367 BER_BVZERO( &e->e_bv );
370 /* free attributes */
371 attrs_free( e->e_attrs );
376 entry_free( Entry *e )
384 * These routines are used only by Backend.
386 * the Entry has three entry points (ways to find things):
388 * by entry e.g., if you already have an entry from the cache
389 * and want to delete it. (really by entry ptr)
390 * by dn e.g., when looking for the base object of a search
391 * by id e.g., for search candidates
393 * these correspond to three different avl trees that are maintained.
397 entry_cmp( Entry *e1, Entry *e2 )
399 return SLAP_PTRCMP( e1, e2 );
403 entry_dn_cmp( const void *v_e1, const void *v_e2 )
405 /* compare their normalized UPPERCASED dn's */
406 const Entry *e1 = v_e1, *e2 = v_e2;
408 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
412 entry_id_cmp( const void *v_e1, const void *v_e2 )
414 const Entry *e1 = v_e1, *e2 = v_e2;
415 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
418 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
419 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
421 /* This is like a ber_len */
423 entry_lenlen(ber_len_t len)
438 entry_putlen(unsigned char **buf, ber_len_t len)
440 ber_len_t lenlen = entry_lenlen(len);
443 **buf = (unsigned char) len;
446 **buf = 0x80 | ((unsigned char) lenlen - 1);
447 for (i=lenlen-1; i>0; i--) {
448 (*buf)[i] = (unsigned char) len;
456 entry_getlen(unsigned char **buf)
473 /* Add up the size of the entry for a flattened buffer */
474 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
476 ber_len_t siz = sizeof(Entry);
477 ber_len_t len, dnlen, ndnlen;
481 dnlen = e->e_name.bv_len;
482 len = dnlen + 1; /* trailing NUL byte */
483 len += entry_lenlen(dnlen);
485 ndnlen = e->e_nname.bv_len;
487 len += entry_lenlen(ndnlen);
489 for (a=e->e_attrs; a; a=a->a_next) {
490 /* For AttributeDesc, we only store the attr name */
491 siz += sizeof(Attribute);
492 len += a->a_desc->ad_cname.bv_len+1;
493 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
494 for (i=0; a->a_vals[i].bv_val; i++) {
495 siz += sizeof(struct berval);
496 len += a->a_vals[i].bv_len + 1;
497 len += entry_lenlen(a->a_vals[i].bv_len);
499 len += entry_lenlen(i);
500 siz += sizeof(struct berval); /* empty berval at end */
501 if (norm && a->a_nvals != a->a_vals) {
502 for (i=0; a->a_nvals[i].bv_val; i++) {
503 siz += sizeof(struct berval);
504 len += a->a_nvals[i].bv_len + 1;
505 len += entry_lenlen(a->a_nvals[i].bv_len);
507 len += entry_lenlen(i); /* i nvals */
508 siz += sizeof(struct berval);
510 len += entry_lenlen(0); /* 0 nvals */
513 len += 1; /* NUL byte at end */
514 len += entry_lenlen(siz);
519 /* Flatten an Entry into a buffer. The buffer is filled with just the
520 * strings/bervals of all the entry components. Each field is preceded
521 * by its length, encoded the way ber_put_len works. Every field is NUL
522 * terminated. The entire buffer size is precomputed so that a single
523 * malloc can be performed. The entry size is also recorded,
524 * to aid in entry_decode.
526 int entry_encode(Entry *e, struct berval *bv)
528 ber_len_t siz = sizeof(Entry);
529 ber_len_t len, dnlen, ndnlen;
534 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
535 (long) e->e_id, e->e_dn, 0 );
536 dnlen = e->e_name.bv_len;
537 ndnlen = e->e_nname.bv_len;
539 entry_flatsize( e, &siz, &len, 1 );
542 bv->bv_val = ch_malloc(len);
543 ptr = (unsigned char *)bv->bv_val;
544 entry_putlen(&ptr, siz);
545 entry_putlen(&ptr, dnlen);
546 AC_MEMCPY(ptr, e->e_dn, dnlen);
549 entry_putlen(&ptr, ndnlen);
550 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
554 for (a=e->e_attrs; a; a=a->a_next) {
555 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
556 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
557 a->a_desc->ad_cname.bv_len);
558 ptr += a->a_desc->ad_cname.bv_len;
561 for (i=0; a->a_vals[i].bv_val; i++);
562 entry_putlen(&ptr, i);
563 for (i=0; a->a_vals[i].bv_val; i++) {
564 entry_putlen(&ptr, a->a_vals[i].bv_len);
565 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
566 a->a_vals[i].bv_len);
567 ptr += a->a_vals[i].bv_len;
570 if (a->a_nvals != a->a_vals) {
571 entry_putlen(&ptr, i);
572 for (i=0; a->a_nvals[i].bv_val; i++) {
573 entry_putlen(&ptr, a->a_nvals[i].bv_len);
574 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
575 a->a_nvals[i].bv_len);
576 ptr += a->a_nvals[i].bv_len;
580 entry_putlen(&ptr, 0);
588 /* Retrieve an Entry that was stored using entry_encode above.
589 * We malloc a single block with the size stored above for the Entry
590 * and all of its Attributes. We also must lookup the stored
591 * attribute names to get AttributeDescriptions. To detect if the
592 * attributes of an Entry are later modified, we note that e->e_attr
593 * is always a constant offset from (e).
595 * Note: everything is stored in a single contiguous block, so
596 * you can not free individual attributes or names from this
597 * structure. Attempting to do so will likely corrupt memory.
599 int entry_decode(struct berval *bv, Entry **e)
606 AttributeDescription *ad;
607 unsigned char *ptr = (unsigned char *)bv->bv_val;
610 i = entry_getlen(&ptr);
612 Debug( LDAP_DEBUG_ANY,
613 "entry_decode: entry length was zero\n", 0, 0, 0);
617 i = entry_getlen(&ptr);
618 x->e_name.bv_val = (char *) ptr;
619 x->e_name.bv_len = i;
621 i = entry_getlen(&ptr);
622 x->e_nname.bv_val = (char *) ptr;
623 x->e_nname.bv_len = i;
625 Debug( LDAP_DEBUG_TRACE,
626 "entry_decode: \"%s\"\n",
630 /* A valid entry must have at least one attr, so this
631 * pointer can never be NULL
633 x->e_attrs = (Attribute *)(x+1);
634 bptr = (BerVarray)x->e_attrs;
637 while ((i = entry_getlen(&ptr))) {
640 bv.bv_val = (char *) ptr;
642 a->a_next = (Attribute *)bptr;
644 a = (Attribute *)bptr;
646 rc = slap_bv2ad( &bv, &ad, &text );
648 if( rc != LDAP_SUCCESS ) {
649 Debug( LDAP_DEBUG_TRACE,
650 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
651 rc = slap_bv2undef_ad( &bv, &ad, &text );
653 if( rc != LDAP_SUCCESS ) {
654 Debug( LDAP_DEBUG_ANY,
655 "<= entry_decode: str2undef_ad(%s): %s\n",
662 bptr = (BerVarray)(a+1);
665 count = j = entry_getlen(&ptr);
668 i = entry_getlen(&ptr);
670 bptr->bv_val = (char *)ptr;
679 j = entry_getlen(&ptr);
683 i = entry_getlen(&ptr);
685 bptr->bv_val = (char *)ptr;
694 a->a_nvals = a->a_vals;
698 if (a) a->a_next = NULL;
699 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
705 Entry *entry_dup( Entry *e )
709 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
712 ber_dupbv( &ret->e_name, &e->e_name );
713 ber_dupbv( &ret->e_nname, &e->e_nname );
714 ret->e_attrs = attrs_dup( e->e_attrs );
715 ret->e_ocflags = e->e_ocflags;
716 ret->e_bv.bv_val = NULL;
717 ret->e_bv.bv_len = 0;
718 ret->e_private = NULL;