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;
73 * LDIF is used as the string format.
74 * An entry looks like this:
77 * [<attr>:[:] <value>\n]
78 * [<tab><continuedvalue>\n]*
81 * If a double colon is used after a type, it means the
82 * following value is encoded as a base 64 string. This
83 * happens if the value contains a non-printing character
88 LDAP_LOG( OPERATION, DETAIL1, "str2entry: \"%s\"\n",
89 s ? s : "NULL", 0, 0 );
91 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
92 s ? s : "NULL", 0, 0 );
95 /* initialize reader/writer lock */
96 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
100 LDAP_LOG( OPERATION, ERR, "str2entry: entry allocation failed.\n", 0, 0, 0 );
102 Debug( LDAP_DEBUG_ANY,
103 "<= str2entry NULL (entry allocation failed)\n",
109 /* initialize entry */
112 /* dn + attributes */
114 vals[1].bv_val = NULL;
117 while ( (s = ldif_getline( &next )) != NULL ) {
118 if ( *s == '\n' || *s == '\0' ) {
122 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
124 LDAP_LOG( OPERATION, DETAIL1, "str2entry: NULL (parse_line)\n",0, 0, 0 );
126 Debug( LDAP_DEBUG_TRACE,
127 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
132 if ( strcasecmp( type, "dn" ) == 0 ) {
135 if ( e->e_dn != NULL ) {
137 LDAP_LOG( OPERATION, DETAIL1, "str2entry: "
138 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
139 (long) e->e_id, e->e_dn, vals[0].bv_val );
141 Debug( LDAP_DEBUG_ANY, "str2entry: "
142 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
143 (long) e->e_id, e->e_dn, vals[0].bv_val );
145 free( vals[0].bv_val );
150 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname, NULL );
151 if( rc != LDAP_SUCCESS ) {
153 LDAP_LOG( OPERATION, DETAIL1,
154 "str2entry: entry %ld has invalid DN \"%s\"\n",
155 (long) e->e_id, vals[0].bv_val, 0 );
157 Debug( LDAP_DEBUG_ANY, "str2entry: "
158 "entry %ld has invalid DN \"%s\"\n",
159 (long) e->e_id, vals[0].bv_val, 0 );
162 free( vals[0].bv_val );
165 free( vals[0].bv_val );
170 rc = slap_str2ad( type, &ad, &text );
172 if( rc != LDAP_SUCCESS ) {
174 LDAP_LOG( OPERATION, DETAIL1,
175 "str2entry: str2ad(%s): %s\n", type, text, 0 );
177 Debug( slapMode & SLAP_TOOL_MODE
178 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
179 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
181 if( slapMode & SLAP_TOOL_MODE ) {
183 free( vals[0].bv_val );
188 rc = slap_str2undef_ad( type, &ad, &text );
189 if( rc != LDAP_SUCCESS ) {
191 LDAP_LOG( OPERATION, DETAIL1,
192 "str2entry: str2undef_ad(%s): %s\n", type, text, 0 );
194 Debug( LDAP_DEBUG_ANY,
195 "<= str2entry: str2undef_ad(%s): %s\n",
199 free( vals[0].bv_val );
205 if( slapMode & SLAP_TOOL_MODE ) {
207 slap_syntax_validate_func *validate =
208 ad->ad_type->sat_syntax->ssyn_validate;
209 slap_syntax_transform_func *pretty =
210 ad->ad_type->sat_syntax->ssyn_pretty;
213 rc = pretty( ad->ad_type->sat_syntax,
214 &vals[0], &pval, NULL );
216 } else if( validate ) {
218 * validate value per syntax
220 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
224 LDAP_LOG( OPERATION, INFO,
225 "str2entry: no validator for syntax %s\n",
226 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
228 Debug( LDAP_DEBUG_ANY,
229 "str2entry: no validator for syntax %s\n",
230 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
233 free( vals[0].bv_val );
240 LDAP_LOG( OPERATION, ERR,
241 "str2entry: invalid value for attribute %s (syntax %s)\n",
242 ad->ad_cname.bv_val, ad->ad_type->sat_syntax->ssyn_oid, 0 );
244 Debug( LDAP_DEBUG_ANY,
245 "str2entry: invalid value for attribute %s (syntax %s)\n",
246 ad->ad_cname.bv_val, ad->ad_type->sat_syntax->ssyn_oid, 0 );
249 free( vals[0].bv_val );
255 free( vals[0].bv_val );
261 nvals[0].bv_val = NULL;
263 if( ad->ad_type->sat_equality &&
264 ad->ad_type->sat_equality->smr_normalize )
266 rc = ad->ad_type->sat_equality->smr_normalize(
267 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
268 ad->ad_type->sat_syntax,
269 ad->ad_type->sat_equality,
270 &vals[0], &nvals[0], NULL );
274 LDAP_LOG( OPERATION, DETAIL1,
275 "str2entry: NULL (smr_normalize %d)\n" , rc, 0, 0 );
277 Debug( LDAP_DEBUG_ANY,
278 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
282 free( vals[0].bv_val );
288 nvals[1].bv_val = NULL;
293 rc = attr_merge( e, ad, vals, nvalsp );
296 LDAP_LOG( OPERATION, DETAIL1,
297 "str2entry: NULL (attr_merge)\n" , 0, 0, 0 );
299 Debug( LDAP_DEBUG_ANY,
300 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
303 free( vals[0].bv_val );
309 free( vals[0].bv_val );
310 free( nvals[0].bv_val );
313 /* check to make sure there was a dn: line */
314 if ( e->e_dn == NULL ) {
316 LDAP_LOG( OPERATION, INFO,
317 "str2entry: entry %ld has no dn.\n", (long) e->e_id, 0, 0 );
319 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
320 (long) e->e_id, 0, 0 );
327 LDAP_LOG( OPERATION, DETAIL2,
328 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e, 0 );
330 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
331 e->e_dn, (unsigned long) e, 0 );
337 #define GRABSIZE BUFSIZ
339 #define MAKE_SPACE( n ) { \
340 while ( ecur + (n) > ebuf + emaxsize ) { \
342 offset = (int) (ecur - ebuf); \
343 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
344 emaxsize + GRABSIZE ); \
345 emaxsize += GRABSIZE; \
346 ecur = ebuf + offset; \
363 * In string format, an entry looks like this:
365 * [<attr>: <value>\n]*
371 if ( e->e_dn != NULL ) {
373 tmplen = e->e_name.bv_len;
374 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
375 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
378 /* put the attributes */
379 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
380 /* put "<type>:[:] <value>" line for each value */
381 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
383 tmplen = a->a_desc->ad_cname.bv_len;
384 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
385 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
386 a->a_desc->ad_cname.bv_val,
387 bv->bv_val, bv->bv_len );
394 return( (char *) ebuf );
398 entry_free( Entry *e )
400 /* free an entry structure */
403 /* e_private must be freed by the caller */
404 assert( e->e_private == NULL );
408 if ( e->e_dn != NULL ) {
412 if ( e->e_ndn != NULL ) {
417 if ( e->e_bv.bv_val != NULL ) {
418 free( e->e_bv.bv_val );
419 e->e_bv.bv_val = NULL;
422 /* free attributes */
423 attrs_free( e->e_attrs );
430 * These routines are used only by Backend.
432 * the Entry has three entry points (ways to find things):
434 * by entry e.g., if you already have an entry from the cache
435 * and want to delete it. (really by entry ptr)
436 * by dn e.g., when looking for the base object of a search
437 * by id e.g., for search candidates
439 * these correspond to three different avl trees that are maintained.
443 entry_cmp( Entry *e1, Entry *e2 )
445 return SLAP_PTRCMP( e1, e2 );
449 entry_dn_cmp( const void *v_e1, const void *v_e2 )
451 /* compare their normalized UPPERCASED dn's */
452 const Entry *e1 = v_e1, *e2 = v_e2;
454 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
458 entry_id_cmp( const void *v_e1, const void *v_e2 )
460 const Entry *e1 = v_e1, *e2 = v_e2;
461 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
464 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
465 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
467 /* This is like a ber_len */
469 entry_lenlen(ber_len_t len)
484 entry_putlen(unsigned char **buf, ber_len_t len)
486 ber_len_t lenlen = entry_lenlen(len);
489 **buf = (unsigned char) len;
492 **buf = 0x80 | ((unsigned char) lenlen - 1);
493 for (i=lenlen-1; i>0; i--) {
494 (*buf)[i] = (unsigned char) len;
502 entry_getlen(unsigned char **buf)
519 /* Add up the size of the entry for a flattened buffer */
520 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
522 ber_len_t siz = sizeof(Entry);
523 ber_len_t len, dnlen, ndnlen;
527 dnlen = e->e_name.bv_len;
528 len = dnlen + 1; /* trailing NUL byte */
529 len += entry_lenlen(dnlen);
531 ndnlen = e->e_nname.bv_len;
533 len += entry_lenlen(ndnlen);
535 for (a=e->e_attrs; a; a=a->a_next) {
536 /* For AttributeDesc, we only store the attr name */
537 siz += sizeof(Attribute);
538 len += a->a_desc->ad_cname.bv_len+1;
539 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
540 for (i=0; a->a_vals[i].bv_val; i++) {
541 siz += sizeof(struct berval);
542 len += a->a_vals[i].bv_len + 1;
543 len += entry_lenlen(a->a_vals[i].bv_len);
545 len += entry_lenlen(i);
546 siz += sizeof(struct berval); /* empty berval at end */
547 if (norm && a->a_nvals != a->a_vals) {
548 for (i=0; a->a_nvals[i].bv_val; i++) {
549 siz += sizeof(struct berval);
550 len += a->a_nvals[i].bv_len + 1;
551 len += entry_lenlen(a->a_nvals[i].bv_len);
553 len += entry_lenlen(i); /* i nvals */
554 siz += sizeof(struct berval);
556 len += entry_lenlen(0); /* 0 nvals */
559 len += 1; /* NUL byte at end */
560 len += entry_lenlen(siz);
565 /* Flatten an Entry into a buffer. The buffer is filled with just the
566 * strings/bervals of all the entry components. Each field is preceded
567 * by its length, encoded the way ber_put_len works. Every field is NUL
568 * terminated. The entire buffer size is precomputed so that a single
569 * malloc can be performed. The entry size is also recorded,
570 * to aid in entry_decode.
572 int entry_encode(Entry *e, struct berval *bv)
574 ber_len_t siz = sizeof(Entry);
575 ber_len_t len, dnlen, ndnlen;
581 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
582 (long) e->e_id, e->e_dn, 0 );
584 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
585 (long) e->e_id, e->e_dn, 0 );
587 dnlen = e->e_name.bv_len;
588 ndnlen = e->e_nname.bv_len;
590 entry_flatsize( e, &siz, &len, 1 );
593 bv->bv_val = ch_malloc(len);
594 ptr = (unsigned char *)bv->bv_val;
595 entry_putlen(&ptr, siz);
596 entry_putlen(&ptr, dnlen);
597 AC_MEMCPY(ptr, e->e_dn, dnlen);
600 entry_putlen(&ptr, ndnlen);
601 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
605 for (a=e->e_attrs; a; a=a->a_next) {
606 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
607 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
608 a->a_desc->ad_cname.bv_len);
609 ptr += a->a_desc->ad_cname.bv_len;
612 for (i=0; a->a_vals[i].bv_val; i++);
613 entry_putlen(&ptr, i);
614 for (i=0; a->a_vals[i].bv_val; i++) {
615 entry_putlen(&ptr, a->a_vals[i].bv_len);
616 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
617 a->a_vals[i].bv_len);
618 ptr += a->a_vals[i].bv_len;
621 if (a->a_nvals != a->a_vals) {
622 entry_putlen(&ptr, i);
623 for (i=0; a->a_nvals[i].bv_val; i++) {
624 entry_putlen(&ptr, a->a_nvals[i].bv_len);
625 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
626 a->a_nvals[i].bv_len);
627 ptr += a->a_nvals[i].bv_len;
631 entry_putlen(&ptr, 0);
639 /* Retrieve an Entry that was stored using entry_encode above.
640 * We malloc a single block with the size stored above for the Entry
641 * and all of its Attributes. We also must lookup the stored
642 * attribute names to get AttributeDescriptions. To detect if the
643 * attributes of an Entry are later modified, we note that e->e_attr
644 * is always a constant offset from (e).
646 * Note: everything is stored in a single contiguous block, so
647 * you can not free individual attributes or names from this
648 * structure. Attempting to do so will likely corrupt memory.
650 int entry_decode(struct berval *bv, Entry **e)
657 AttributeDescription *ad;
658 unsigned char *ptr = (unsigned char *)bv->bv_val;
661 i = entry_getlen(&ptr);
664 LDAP_LOG( OPERATION, INFO,
665 "entry_decode: entry length was zero\n", 0, 0, 0);
667 Debug( LDAP_DEBUG_ANY,
668 "entry_decode: entry length was zero\n", 0, 0, 0);
673 i = entry_getlen(&ptr);
674 x->e_name.bv_val = (char *) ptr;
675 x->e_name.bv_len = i;
677 i = entry_getlen(&ptr);
678 x->e_nname.bv_val = (char *) ptr;
679 x->e_nname.bv_len = i;
682 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
684 Debug( LDAP_DEBUG_TRACE,
685 "entry_decode: \"%s\"\n",
690 /* A valid entry must have at least one attr, so this
691 * pointer can never be NULL
693 x->e_attrs = (Attribute *)(x+1);
694 bptr = (BerVarray)x->e_attrs;
697 while ((i = entry_getlen(&ptr))) {
700 bv.bv_val = (char *) ptr;
702 a->a_next = (Attribute *)bptr;
704 a = (Attribute *)bptr;
706 rc = slap_bv2ad( &bv, &ad, &text );
708 if( rc != LDAP_SUCCESS ) {
710 LDAP_LOG( OPERATION, INFO,
711 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
713 Debug( LDAP_DEBUG_TRACE,
714 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
716 rc = slap_bv2undef_ad( &bv, &ad, &text );
718 if( rc != LDAP_SUCCESS ) {
720 LDAP_LOG( OPERATION, INFO,
721 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
723 Debug( LDAP_DEBUG_ANY,
724 "<= entry_decode: str2undef_ad(%s): %s\n",
732 bptr = (BerVarray)(a+1);
735 count = j = entry_getlen(&ptr);
738 i = entry_getlen(&ptr);
740 bptr->bv_val = (char *)ptr;
749 j = entry_getlen(&ptr);
753 i = entry_getlen(&ptr);
755 bptr->bv_val = (char *)ptr;
764 a->a_nvals = a->a_vals;
768 if (a) a->a_next = NULL;
770 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
772 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
779 Entry *entry_dup( Entry *e )
783 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
786 ber_dupbv( &ret->e_name, &e->e_name );
787 ber_dupbv( &ret->e_nname, &e->e_nname );
788 ret->e_attrs = attrs_dup( e->e_attrs );
789 ret->e_ocflags = e->e_ocflags;
790 ret->e_bv.bv_val = NULL;
791 ret->e_bv.bv_len = 0;
792 ret->e_private = NULL;