1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
14 #include <ac/socket.h>
15 #include <ac/string.h>
20 static unsigned char *ebuf; /* buf returned by entry2str */
21 static unsigned char *ecur; /* pointer to end of currently used ebuf */
22 static int emaxsize;/* max size of ebuf */
27 const Entry slap_entry_root = {
28 NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
31 int entry_destroy(void)
33 if ( ebuf ) free( ebuf );
47 struct berval vals[2];
48 struct berval nvals[2], *nvalsp;
49 AttributeDescription *ad;
54 * LDIF is used as the string format.
55 * An entry looks like this:
58 * [<attr>:[:] <value>\n]
59 * [<tab><continuedvalue>\n]*
62 * If a double colon is used after a type, it means the
63 * following value is encoded as a base 64 string. This
64 * happens if the value contains a non-printing character
69 LDAP_LOG( OPERATION, DETAIL1, "str2entry: \"%s\"\n", s ? s : "NULL", 0, 0 );
71 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
72 s ? s : "NULL", 0, 0 );
75 /* initialize reader/writer lock */
76 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
80 LDAP_LOG( OPERATION, ERR, "str2entry: entry allocation failed.\n", 0, 0, 0 );
82 Debug( LDAP_DEBUG_ANY,
83 "<= str2entry NULL (entry allocation failed)\n",
89 /* initialize entry */
94 vals[1].bv_val = NULL;
97 while ( (s = ldif_getline( &next )) != NULL ) {
98 if ( *s == '\n' || *s == '\0' ) {
102 if ( ldif_parse_line( s, &type, &vals[0].bv_val, &vals[0].bv_len ) != 0 ) {
104 LDAP_LOG( OPERATION, DETAIL1, "str2entry: NULL (parse_line)\n",0, 0, 0 );
106 Debug( LDAP_DEBUG_TRACE,
107 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
112 if ( strcasecmp( type, "dn" ) == 0 ) {
115 if ( e->e_dn != NULL ) {
117 LDAP_LOG( OPERATION, DETAIL1, "str2entry: "
118 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
119 (long) e->e_id, e->e_dn, vals[0].bv_val );
121 Debug( LDAP_DEBUG_ANY, "str2entry: "
122 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
123 (long) e->e_id, e->e_dn, vals[0].bv_val );
125 free( vals[0].bv_val );
130 rc = dnPrettyNormal( NULL, &vals[0], &e->e_name, &e->e_nname, NULL );
131 if( rc != LDAP_SUCCESS ) {
133 LDAP_LOG( OPERATION, DETAIL1,
134 "str2entry: entry %ld has invalid DN \"%s\"\n",
135 (long) e->e_id, vals[0].bv_val, 0 );
137 Debug( LDAP_DEBUG_ANY, "str2entry: "
138 "entry %ld has invalid DN \"%s\"\n",
139 (long) e->e_id, vals[0].bv_val, 0 );
142 free( vals[0].bv_val );
145 free( vals[0].bv_val );
150 rc = slap_str2ad( type, &ad, &text );
152 if( rc != LDAP_SUCCESS ) {
154 LDAP_LOG( OPERATION, DETAIL1,
155 "str2entry: str2ad(%s): %s\n", type, text, 0 );
157 Debug( slapMode & SLAP_TOOL_MODE
158 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
159 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
161 if( slapMode & SLAP_TOOL_MODE ) {
163 free( vals[0].bv_val );
168 rc = slap_str2undef_ad( type, &ad, &text );
169 if( rc != LDAP_SUCCESS ) {
171 LDAP_LOG( OPERATION, DETAIL1,
172 "str2entry: str2undef_ad(%s): %s\n", type, text, 0 );
174 Debug( LDAP_DEBUG_ANY,
175 "<= str2entry: str2undef_ad(%s): %s\n",
179 free( vals[0].bv_val );
185 if( slapMode & SLAP_TOOL_MODE ) {
187 slap_syntax_validate_func *validate =
188 ad->ad_type->sat_syntax->ssyn_validate;
189 slap_syntax_transform_func *pretty =
190 ad->ad_type->sat_syntax->ssyn_pretty;
193 rc = pretty( ad->ad_type->sat_syntax,
194 &vals[0], &pval, NULL );
196 } else if( validate ) {
198 * validate value per syntax
200 rc = validate( ad->ad_type->sat_syntax, &vals[0] );
204 LDAP_LOG( OPERATION, INFO,
205 "str2entry: no validator for syntax %s\n",
206 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
208 Debug( LDAP_DEBUG_ANY,
209 "str2entry: no validator for syntax %s\n",
210 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
213 free( vals[0].bv_val );
220 LDAP_LOG( OPERATION, ERR,
221 "str2entry: invalid value for attribute %s (syntax %s)\n",
222 ad->ad_cname.bv_val, ad->ad_type->sat_syntax->ssyn_oid, 0 );
224 Debug( LDAP_DEBUG_ANY,
225 "str2entry: invalid value for attribute %s (syntax %s)\n",
226 ad->ad_cname.bv_val, ad->ad_type->sat_syntax->ssyn_oid, 0 );
229 free( vals[0].bv_val );
235 free( vals[0].bv_val );
241 nvals[0].bv_val = NULL;
243 if( ad->ad_type->sat_equality &&
244 ad->ad_type->sat_equality->smr_normalize )
246 rc = ad->ad_type->sat_equality->smr_normalize(
247 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
248 ad->ad_type->sat_syntax,
249 ad->ad_type->sat_equality,
250 &vals[0], &nvals[0], NULL );
254 LDAP_LOG( OPERATION, DETAIL1,
255 "str2entry: NULL (smr_normalize %d)\n" , rc, 0, 0 );
257 Debug( LDAP_DEBUG_ANY,
258 "<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
262 free( vals[0].bv_val );
268 nvals[1].bv_val = NULL;
273 rc = attr_merge( e, ad, vals, nvalsp );
276 LDAP_LOG( OPERATION, DETAIL1,
277 "str2entry: NULL (attr_merge)\n" , 0, 0, 0 );
279 Debug( LDAP_DEBUG_ANY,
280 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
283 free( vals[0].bv_val );
289 free( vals[0].bv_val );
290 free( nvals[0].bv_val );
293 /* check to make sure there was a dn: line */
294 if ( e->e_dn == NULL ) {
296 LDAP_LOG( OPERATION, INFO,
297 "str2entry: entry %ld has no dn.\n", (long) e->e_id, 0, 0 );
299 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
300 (long) e->e_id, 0, 0 );
307 LDAP_LOG( OPERATION, DETAIL2,
308 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e, 0 );
310 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
311 e->e_dn, (unsigned long) e, 0 );
317 #define GRABSIZE BUFSIZ
319 #define MAKE_SPACE( n ) { \
320 while ( ecur + (n) > ebuf + emaxsize ) { \
322 offset = (int) (ecur - ebuf); \
323 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
324 emaxsize + GRABSIZE ); \
325 emaxsize += GRABSIZE; \
326 ecur = ebuf + offset; \
343 * In string format, an entry looks like this:
345 * [<attr>: <value>\n]*
351 if ( e->e_dn != NULL ) {
353 tmplen = e->e_name.bv_len;
354 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
355 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
358 /* put the attributes */
359 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
360 /* put "<type>:[:] <value>" line for each value */
361 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
363 tmplen = a->a_desc->ad_cname.bv_len;
364 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
365 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
366 a->a_desc->ad_cname.bv_val,
367 bv->bv_val, bv->bv_len );
374 return( (char *) ebuf );
378 entry_free( Entry *e )
380 /* free an entry structure */
383 /* e_private must be freed by the caller */
384 assert( e->e_private == NULL );
388 if ( e->e_dn != NULL ) {
392 if ( e->e_ndn != NULL ) {
397 if ( e->e_bv.bv_val != NULL ) {
398 free( e->e_bv.bv_val );
399 e->e_bv.bv_val = NULL;
402 /* free attributes */
403 attrs_free( e->e_attrs );
410 * These routines are used only by Backend.
412 * the Entry has three entry points (ways to find things):
414 * by entry e.g., if you already have an entry from the cache
415 * and want to delete it. (really by entry ptr)
416 * by dn e.g., when looking for the base object of a search
417 * by id e.g., for search candidates
419 * these correspond to three different avl trees that are maintained.
423 entry_cmp( Entry *e1, Entry *e2 )
425 return SLAP_PTRCMP( e1, e2 );
429 entry_dn_cmp( const void *v_e1, const void *v_e2 )
431 /* compare their normalized UPPERCASED dn's */
432 const Entry *e1 = v_e1, *e2 = v_e2;
434 return ber_bvcmp( &e1->e_nname, &e2->e_nname );
438 entry_id_cmp( const void *v_e1, const void *v_e2 )
440 const Entry *e1 = v_e1, *e2 = v_e2;
441 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
444 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
445 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
447 /* This is like a ber_len */
449 entry_lenlen(ber_len_t len)
464 entry_putlen(unsigned char **buf, ber_len_t len)
466 ber_len_t lenlen = entry_lenlen(len);
469 **buf = (unsigned char) len;
472 **buf = 0x80 | ((unsigned char) lenlen - 1);
473 for (i=lenlen-1; i>0; i--) {
474 (*buf)[i] = (unsigned char) len;
482 entry_getlen(unsigned char **buf)
499 /* Add up the size of the entry for a flattened buffer */
500 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
502 ber_len_t siz = sizeof(Entry);
503 ber_len_t len, dnlen, ndnlen;
507 dnlen = e->e_name.bv_len;
508 len = dnlen + 1; /* trailing NUL byte */
509 len += entry_lenlen(dnlen);
511 ndnlen = e->e_nname.bv_len;
513 len += entry_lenlen(ndnlen);
515 for (a=e->e_attrs; a; a=a->a_next) {
516 /* For AttributeDesc, we only store the attr name */
517 siz += sizeof(Attribute);
518 len += a->a_desc->ad_cname.bv_len+1;
519 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
520 for (i=0; a->a_vals[i].bv_val; i++) {
521 siz += sizeof(struct berval);
522 len += a->a_vals[i].bv_len + 1;
523 len += entry_lenlen(a->a_vals[i].bv_len);
525 len += entry_lenlen(i);
526 siz += sizeof(struct berval); /* empty berval at end */
527 if (norm && a->a_nvals != a->a_vals) {
528 for (i=0; a->a_nvals[i].bv_val; i++) {
529 siz += sizeof(struct berval);
530 len += a->a_nvals[i].bv_len + 1;
531 len += entry_lenlen(a->a_nvals[i].bv_len);
533 len += entry_lenlen(i); /* i nvals */
534 siz += sizeof(struct berval);
536 len += entry_lenlen(0); /* 0 nvals */
539 len += 1; /* NUL byte at end */
540 len += entry_lenlen(siz);
545 /* Flatten an Entry into a buffer. The buffer is filled with just the
546 * strings/bervals of all the entry components. Each field is preceded
547 * by its length, encoded the way ber_put_len works. Every field is NUL
548 * terminated. The entire buffer size is precomputed so that a single
549 * malloc can be performed. The entry size is also recorded,
550 * to aid in entry_decode.
552 int entry_encode(Entry *e, struct berval *bv)
554 ber_len_t siz = sizeof(Entry);
555 ber_len_t len, dnlen, ndnlen;
561 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
562 (long) e->e_id, e->e_dn, 0 );
564 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
565 (long) e->e_id, e->e_dn, 0 );
567 dnlen = e->e_name.bv_len;
568 ndnlen = e->e_nname.bv_len;
570 entry_flatsize( e, &siz, &len, 1 );
573 bv->bv_val = ch_malloc(len);
574 ptr = (unsigned char *)bv->bv_val;
575 entry_putlen(&ptr, siz);
576 entry_putlen(&ptr, dnlen);
577 AC_MEMCPY(ptr, e->e_dn, dnlen);
580 entry_putlen(&ptr, ndnlen);
581 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
585 for (a=e->e_attrs; a; a=a->a_next) {
586 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
587 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
588 a->a_desc->ad_cname.bv_len);
589 ptr += a->a_desc->ad_cname.bv_len;
592 for (i=0; a->a_vals[i].bv_val; i++);
593 entry_putlen(&ptr, i);
594 for (i=0; a->a_vals[i].bv_val; i++) {
595 entry_putlen(&ptr, a->a_vals[i].bv_len);
596 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
597 a->a_vals[i].bv_len);
598 ptr += a->a_vals[i].bv_len;
601 if (a->a_nvals != a->a_vals) {
602 entry_putlen(&ptr, i);
603 for (i=0; a->a_nvals[i].bv_val; i++) {
604 entry_putlen(&ptr, a->a_nvals[i].bv_len);
605 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
606 a->a_nvals[i].bv_len);
607 ptr += a->a_nvals[i].bv_len;
611 entry_putlen(&ptr, 0);
619 /* Retrieve an Entry that was stored using entry_encode above.
620 * We malloc a single block with the size stored above for the Entry
621 * and all of its Attributes. We also must lookup the stored
622 * attribute names to get AttributeDescriptions. To detect if the
623 * attributes of an Entry are later modified, we note that e->e_attr
624 * is always a constant offset from (e).
626 * Note: everything is stored in a single contiguous block, so
627 * you can not free individual attributes or names from this
628 * structure. Attempting to do so will likely corrupt memory.
630 int entry_decode(struct berval *bv, Entry **e)
637 AttributeDescription *ad;
638 unsigned char *ptr = (unsigned char *)bv->bv_val;
641 i = entry_getlen(&ptr);
643 i = entry_getlen(&ptr);
644 x->e_name.bv_val = (char *) ptr;
645 x->e_name.bv_len = i;
647 i = entry_getlen(&ptr);
648 x->e_nname.bv_val = (char *) ptr;
649 x->e_nname.bv_len = i;
652 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
654 Debug( LDAP_DEBUG_TRACE,
655 "entry_decode: \"%s\"\n",
660 /* A valid entry must have at least one attr, so this
661 * pointer can never be NULL
663 x->e_attrs = (Attribute *)(x+1);
664 bptr = (BerVarray)x->e_attrs;
667 while ((i = entry_getlen(&ptr))) {
670 bv.bv_val = (char *) ptr;
672 a->a_next = (Attribute *)bptr;
674 a = (Attribute *)bptr;
676 rc = slap_bv2ad( &bv, &ad, &text );
678 if( rc != LDAP_SUCCESS ) {
680 LDAP_LOG( OPERATION, INFO,
681 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
683 Debug( LDAP_DEBUG_TRACE,
684 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
686 rc = slap_bv2undef_ad( &bv, &ad, &text );
688 if( rc != LDAP_SUCCESS ) {
690 LDAP_LOG( OPERATION, INFO,
691 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
693 Debug( LDAP_DEBUG_ANY,
694 "<= entry_decode: str2undef_ad(%s): %s\n",
702 bptr = (BerVarray)(a+1);
705 count = j = entry_getlen(&ptr);
708 i = entry_getlen(&ptr);
710 bptr->bv_val = (char *)ptr;
719 j = entry_getlen(&ptr);
723 i = entry_getlen(&ptr);
725 bptr->bv_val = (char *)ptr;
734 a->a_nvals = a->a_vals;
738 if (a) a->a_next = NULL;
740 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
742 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",