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
89 LDAP_LOG( OPERATION, DETAIL1, "str2entry: \"%s\"\n",
90 s ? s : "NULL", 0, 0 );
92 Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
93 s ? s : "NULL", 0, 0 );
96 /* initialize reader/writer lock */
97 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
101 LDAP_LOG( OPERATION, ERR, "str2entry: entry allocation failed.\n", 0, 0, 0 );
103 Debug( LDAP_DEBUG_ANY,
104 "<= str2entry NULL (entry allocation failed)\n",
110 /* initialize entry */
113 /* dn + attributes */
115 vals[1].bv_val = NULL;
121 while ( (s = ldif_getline( &next )) != NULL ) {
122 if ( *s == '\n' || *s == '\0' ) {
126 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
128 LDAP_LOG( OPERATION, DETAIL1, "str2entry: NULL (parse_line)\n",0, 0, 0 );
130 Debug( LDAP_DEBUG_TRACE,
131 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
136 if ( strcasecmp( type, "dn" ) == 0 ) {
139 if ( e->e_dn != NULL ) {
141 LDAP_LOG( OPERATION, DETAIL1, "str2entry: "
142 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
143 (long) e->e_id, e->e_dn, vals[0].bv_val );
145 Debug( LDAP_DEBUG_ANY, "str2entry: "
146 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
147 (long) e->e_id, e->e_dn, vals[0].bv_val );
149 free( vals[0].bv_val );
154 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname, NULL );
155 if( rc != LDAP_SUCCESS ) {
157 LDAP_LOG( OPERATION, DETAIL1,
158 "str2entry: entry %ld has invalid DN \"%s\"\n",
159 (long) e->e_id, vals[0].bv_val, 0 );
161 Debug( LDAP_DEBUG_ANY, "str2entry: "
162 "entry %ld has invalid DN \"%s\"\n",
163 (long) e->e_id, vals[0].bv_val, 0 );
166 free( vals[0].bv_val );
169 free( vals[0].bv_val );
175 rc = slap_str2ad( type, &ad, &text );
177 if( rc != LDAP_SUCCESS ) {
179 LDAP_LOG( OPERATION, DETAIL1,
180 "str2entry: str2ad(%s): %s\n", type, text, 0 );
182 Debug( slapMode & SLAP_TOOL_MODE
183 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
184 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
186 if( slapMode & SLAP_TOOL_MODE ) {
188 free( vals[0].bv_val );
193 rc = slap_str2undef_ad( type, &ad, &text );
194 if( rc != LDAP_SUCCESS ) {
196 LDAP_LOG( OPERATION, DETAIL1,
197 "str2entry: str2undef_ad(%s): %s\n", type, text, 0 );
199 Debug( LDAP_DEBUG_ANY,
200 "<= str2entry: str2undef_ad(%s): %s\n",
204 free( vals[0].bv_val );
210 if ( ad != ad_prev ) {
214 if( slapMode & SLAP_TOOL_MODE ) {
216 slap_syntax_validate_func *validate =
217 ad->ad_type->sat_syntax->ssyn_validate;
218 slap_syntax_transform_func *pretty =
219 ad->ad_type->sat_syntax->ssyn_pretty;
222 rc = pretty( ad->ad_type->sat_syntax,
223 &vals[0], &pval, NULL );
225 } else if( validate ) {
227 * validate value per syntax
229 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
233 LDAP_LOG( OPERATION, INFO,
234 "str2entry: attributeType %s #%d: "
235 "no validator for syntax %s\n",
236 ad->ad_cname.bv_val, attr_cnt,
237 ad->ad_type->sat_syntax->ssyn_oid );
239 Debug( LDAP_DEBUG_ANY,
240 "str2entry: attributeType %s #%d: "
241 "no validator for syntax %s\n",
242 ad->ad_cname.bv_val, attr_cnt,
243 ad->ad_type->sat_syntax->ssyn_oid );
246 free( vals[0].bv_val );
253 LDAP_LOG( OPERATION, ERR,
254 "str2entry: invalid value "
255 "for attributeType %s #%d (syntax %s)\n",
256 ad->ad_cname.bv_val, attr_cnt,
257 ad->ad_type->sat_syntax->ssyn_oid );
259 Debug( LDAP_DEBUG_ANY,
260 "str2entry: invalid value "
261 "for attributeType %s #%d (syntax %s)\n",
262 ad->ad_cname.bv_val, attr_cnt,
263 ad->ad_type->sat_syntax->ssyn_oid );
266 free( vals[0].bv_val );
272 free( vals[0].bv_val );
278 nvals[0].bv_val = NULL;
280 if( ad->ad_type->sat_equality &&
281 ad->ad_type->sat_equality->smr_normalize )
283 rc = ad->ad_type->sat_equality->smr_normalize(
284 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
285 ad->ad_type->sat_syntax,
286 ad->ad_type->sat_equality,
287 &vals[0], &nvals[0], NULL );
291 LDAP_LOG( OPERATION, DETAIL1,
292 "str2entry: NULL (smr_normalize %d)\n" , rc, 0, 0 );
294 Debug( LDAP_DEBUG_ANY,
295 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
299 free( vals[0].bv_val );
305 nvals[1].bv_val = NULL;
310 rc = attr_merge( e, ad, vals, nvalsp );
313 LDAP_LOG( OPERATION, DETAIL1,
314 "str2entry: NULL (attr_merge)\n" , 0, 0, 0 );
316 Debug( LDAP_DEBUG_ANY,
317 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
320 free( vals[0].bv_val );
326 free( vals[0].bv_val );
327 free( nvals[0].bv_val );
332 /* check to make sure there was a dn: line */
333 if ( e->e_dn == NULL ) {
335 LDAP_LOG( OPERATION, INFO,
336 "str2entry: entry %ld has no dn.\n", (long) e->e_id, 0, 0 );
338 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
339 (long) e->e_id, 0, 0 );
346 LDAP_LOG( OPERATION, DETAIL2,
347 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e, 0 );
349 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
350 e->e_dn, (unsigned long) e, 0 );
356 #define GRABSIZE BUFSIZ
358 #define MAKE_SPACE( n ) { \
359 while ( ecur + (n) > ebuf + emaxsize ) { \
361 offset = (int) (ecur - ebuf); \
362 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
363 emaxsize + GRABSIZE ); \
364 emaxsize += GRABSIZE; \
365 ecur = ebuf + offset; \
382 * In string format, an entry looks like this:
384 * [<attr>: <value>\n]*
390 if ( e->e_dn != NULL ) {
392 tmplen = e->e_name.bv_len;
393 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
394 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
397 /* put the attributes */
398 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
399 /* put "<type>:[:] <value>" line for each value */
400 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
402 tmplen = a->a_desc->ad_cname.bv_len;
403 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
404 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
405 a->a_desc->ad_cname.bv_val,
406 bv->bv_val, bv->bv_len );
413 return( (char *) ebuf );
417 entry_free( Entry *e )
419 /* free an entry structure */
422 /* e_private must be freed by the caller */
423 assert( e->e_private == NULL );
427 if ( e->e_dn != NULL ) {
431 if ( e->e_ndn != NULL ) {
436 if ( e->e_bv.bv_val != NULL ) {
437 free( e->e_bv.bv_val );
438 e->e_bv.bv_val = NULL;
441 /* free attributes */
442 attrs_free( e->e_attrs );
449 * These routines are used only by Backend.
451 * the Entry has three entry points (ways to find things):
453 * by entry e.g., if you already have an entry from the cache
454 * and want to delete it. (really by entry ptr)
455 * by dn e.g., when looking for the base object of a search
456 * by id e.g., for search candidates
458 * these correspond to three different avl trees that are maintained.
462 entry_cmp( Entry *e1, Entry *e2 )
464 return SLAP_PTRCMP( e1, e2 );
468 entry_dn_cmp( const void *v_e1, const void *v_e2 )
470 /* compare their normalized UPPERCASED dn's */
471 const Entry *e1 = v_e1, *e2 = v_e2;
473 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
477 entry_id_cmp( const void *v_e1, const void *v_e2 )
479 const Entry *e1 = v_e1, *e2 = v_e2;
480 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
483 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
484 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
486 /* This is like a ber_len */
488 entry_lenlen(ber_len_t len)
503 entry_putlen(unsigned char **buf, ber_len_t len)
505 ber_len_t lenlen = entry_lenlen(len);
508 **buf = (unsigned char) len;
511 **buf = 0x80 | ((unsigned char) lenlen - 1);
512 for (i=lenlen-1; i>0; i--) {
513 (*buf)[i] = (unsigned char) len;
521 entry_getlen(unsigned char **buf)
538 /* Add up the size of the entry for a flattened buffer */
539 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
541 ber_len_t siz = sizeof(Entry);
542 ber_len_t len, dnlen, ndnlen;
546 dnlen = e->e_name.bv_len;
547 len = dnlen + 1; /* trailing NUL byte */
548 len += entry_lenlen(dnlen);
550 ndnlen = e->e_nname.bv_len;
552 len += entry_lenlen(ndnlen);
554 for (a=e->e_attrs; a; a=a->a_next) {
555 /* For AttributeDesc, we only store the attr name */
556 siz += sizeof(Attribute);
557 len += a->a_desc->ad_cname.bv_len+1;
558 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
559 for (i=0; a->a_vals[i].bv_val; i++) {
560 siz += sizeof(struct berval);
561 len += a->a_vals[i].bv_len + 1;
562 len += entry_lenlen(a->a_vals[i].bv_len);
564 len += entry_lenlen(i);
565 siz += sizeof(struct berval); /* empty berval at end */
566 if (norm && a->a_nvals != a->a_vals) {
567 for (i=0; a->a_nvals[i].bv_val; i++) {
568 siz += sizeof(struct berval);
569 len += a->a_nvals[i].bv_len + 1;
570 len += entry_lenlen(a->a_nvals[i].bv_len);
572 len += entry_lenlen(i); /* i nvals */
573 siz += sizeof(struct berval);
575 len += entry_lenlen(0); /* 0 nvals */
578 len += 1; /* NUL byte at end */
579 len += entry_lenlen(siz);
584 /* Flatten an Entry into a buffer. The buffer is filled with just the
585 * strings/bervals of all the entry components. Each field is preceded
586 * by its length, encoded the way ber_put_len works. Every field is NUL
587 * terminated. The entire buffer size is precomputed so that a single
588 * malloc can be performed. The entry size is also recorded,
589 * to aid in entry_decode.
591 int entry_encode(Entry *e, struct berval *bv)
593 ber_len_t siz = sizeof(Entry);
594 ber_len_t len, dnlen, ndnlen;
600 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
601 (long) e->e_id, e->e_dn, 0 );
603 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
604 (long) e->e_id, e->e_dn, 0 );
606 dnlen = e->e_name.bv_len;
607 ndnlen = e->e_nname.bv_len;
609 entry_flatsize( e, &siz, &len, 1 );
612 bv->bv_val = ch_malloc(len);
613 ptr = (unsigned char *)bv->bv_val;
614 entry_putlen(&ptr, siz);
615 entry_putlen(&ptr, dnlen);
616 AC_MEMCPY(ptr, e->e_dn, dnlen);
619 entry_putlen(&ptr, ndnlen);
620 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
624 for (a=e->e_attrs; a; a=a->a_next) {
625 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
626 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
627 a->a_desc->ad_cname.bv_len);
628 ptr += a->a_desc->ad_cname.bv_len;
631 for (i=0; a->a_vals[i].bv_val; i++);
632 entry_putlen(&ptr, i);
633 for (i=0; a->a_vals[i].bv_val; i++) {
634 entry_putlen(&ptr, a->a_vals[i].bv_len);
635 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
636 a->a_vals[i].bv_len);
637 ptr += a->a_vals[i].bv_len;
640 if (a->a_nvals != a->a_vals) {
641 entry_putlen(&ptr, i);
642 for (i=0; a->a_nvals[i].bv_val; i++) {
643 entry_putlen(&ptr, a->a_nvals[i].bv_len);
644 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
645 a->a_nvals[i].bv_len);
646 ptr += a->a_nvals[i].bv_len;
650 entry_putlen(&ptr, 0);
658 /* Retrieve an Entry that was stored using entry_encode above.
659 * We malloc a single block with the size stored above for the Entry
660 * and all of its Attributes. We also must lookup the stored
661 * attribute names to get AttributeDescriptions. To detect if the
662 * attributes of an Entry are later modified, we note that e->e_attr
663 * is always a constant offset from (e).
665 * Note: everything is stored in a single contiguous block, so
666 * you can not free individual attributes or names from this
667 * structure. Attempting to do so will likely corrupt memory.
669 int entry_decode(struct berval *bv, Entry **e)
676 AttributeDescription *ad;
677 unsigned char *ptr = (unsigned char *)bv->bv_val;
680 i = entry_getlen(&ptr);
683 LDAP_LOG( OPERATION, INFO,
684 "entry_decode: entry length was zero\n", 0, 0, 0);
686 Debug( LDAP_DEBUG_ANY,
687 "entry_decode: entry length was zero\n", 0, 0, 0);
692 i = entry_getlen(&ptr);
693 x->e_name.bv_val = (char *) ptr;
694 x->e_name.bv_len = i;
696 i = entry_getlen(&ptr);
697 x->e_nname.bv_val = (char *) ptr;
698 x->e_nname.bv_len = i;
701 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
703 Debug( LDAP_DEBUG_TRACE,
704 "entry_decode: \"%s\"\n",
709 /* A valid entry must have at least one attr, so this
710 * pointer can never be NULL
712 x->e_attrs = (Attribute *)(x+1);
713 bptr = (BerVarray)x->e_attrs;
716 while ((i = entry_getlen(&ptr))) {
719 bv.bv_val = (char *) ptr;
721 a->a_next = (Attribute *)bptr;
723 a = (Attribute *)bptr;
725 rc = slap_bv2ad( &bv, &ad, &text );
727 if( rc != LDAP_SUCCESS ) {
729 LDAP_LOG( OPERATION, INFO,
730 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
732 Debug( LDAP_DEBUG_TRACE,
733 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
735 rc = slap_bv2undef_ad( &bv, &ad, &text );
737 if( rc != LDAP_SUCCESS ) {
739 LDAP_LOG( OPERATION, INFO,
740 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
742 Debug( LDAP_DEBUG_ANY,
743 "<= entry_decode: str2undef_ad(%s): %s\n",
751 bptr = (BerVarray)(a+1);
754 count = j = entry_getlen(&ptr);
757 i = entry_getlen(&ptr);
759 bptr->bv_val = (char *)ptr;
768 j = entry_getlen(&ptr);
772 i = entry_getlen(&ptr);
774 bptr->bv_val = (char *)ptr;
783 a->a_nvals = a->a_vals;
787 if (a) a->a_next = NULL;
789 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
791 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
798 Entry *entry_dup( Entry *e )
802 ret = (Entry *)ch_calloc( 1, sizeof(*ret) );
805 ber_dupbv( &ret->e_name, &e->e_name );
806 ber_dupbv( &ret->e_nname, &e->e_nname );
807 ret->e_attrs = attrs_dup( e->e_attrs );
808 ret->e_ocflags = e->e_ocflags;
809 ret->e_bv.bv_val = NULL;
810 ret->e_bv.bv_len = 0;
811 ret->e_private = NULL;