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 syntax %s\n",
222 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
224 Debug( LDAP_DEBUG_ANY,
225 "str2entry: invalid value for syntax %s\n",
226 ad->ad_type->sat_syntax->ssyn_oid, 0, 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( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
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;
433 int rc = e1->e_nname.bv_len - e2->e_nname.bv_len;
435 return( strcmp( e1->e_ndn, e2->e_ndn ) );
439 entry_id_cmp( const void *v_e1, const void *v_e2 )
441 const Entry *e1 = v_e1, *e2 = v_e2;
442 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
445 #define entry_lenlen(l) ((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
446 ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5
448 /* This is like a ber_len */
450 entry_lenlen(ber_len_t len)
465 entry_putlen(unsigned char **buf, ber_len_t len)
467 ber_len_t lenlen = entry_lenlen(len);
470 **buf = (unsigned char) len;
473 **buf = 0x80 | ((unsigned char) lenlen - 1);
474 for (i=lenlen-1; i>0; i--) {
475 (*buf)[i] = (unsigned char) len;
483 entry_getlen(unsigned char **buf)
500 /* Add up the size of the entry for a flattened buffer */
501 void entry_flatsize(Entry *e, ber_len_t *psiz, ber_len_t *plen, int norm)
503 ber_len_t siz = sizeof(Entry);
504 ber_len_t len, dnlen, ndnlen;
508 dnlen = e->e_name.bv_len;
509 len = dnlen + 1; /* trailing NUL byte */
510 len += entry_lenlen(dnlen);
512 ndnlen = e->e_nname.bv_len;
514 len += entry_lenlen(ndnlen);
516 for (a=e->e_attrs; a; a=a->a_next) {
517 /* For AttributeDesc, we only store the attr name */
518 siz += sizeof(Attribute);
519 len += a->a_desc->ad_cname.bv_len+1;
520 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
521 for (i=0; a->a_vals[i].bv_val; i++) {
522 siz += sizeof(struct berval);
523 len += a->a_vals[i].bv_len + 1;
524 len += entry_lenlen(a->a_vals[i].bv_len);
526 len += entry_lenlen(i);
527 siz += sizeof(struct berval); /* empty berval at end */
528 if (norm && a->a_nvals != a->a_vals) {
529 for (i=0; a->a_nvals[i].bv_val; i++) {
530 siz += sizeof(struct berval);
531 len += a->a_nvals[i].bv_len + 1;
532 len += entry_lenlen(a->a_nvals[i].bv_len);
534 len += entry_lenlen(i); /* i nvals */
535 siz += sizeof(struct berval);
537 len += entry_lenlen(0); /* 0 nvals */
540 len += 1; /* NUL byte at end */
541 len += entry_lenlen(siz);
546 /* Flatten an Entry into a buffer. The buffer is filled with just the
547 * strings/bervals of all the entry components. Each field is preceded
548 * by its length, encoded the way ber_put_len works. Every field is NUL
549 * terminated. The entire buffer size is precomputed so that a single
550 * malloc can be performed. The entry size is also recorded,
551 * to aid in entry_decode.
553 int entry_encode(Entry *e, struct berval *bv)
555 ber_len_t siz = sizeof(Entry);
556 ber_len_t len, dnlen, ndnlen;
562 LDAP_LOG( OPERATION, DETAIL1, "entry_encode: id: 0x%08lx \"%s\"\n",
563 (long) e->e_id, e->e_dn, 0 );
565 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
566 (long) e->e_id, e->e_dn, 0 );
568 dnlen = e->e_name.bv_len;
569 ndnlen = e->e_nname.bv_len;
571 entry_flatsize( e, &siz, &len, 1 );
574 bv->bv_val = ch_malloc(len);
575 ptr = (unsigned char *)bv->bv_val;
576 entry_putlen(&ptr, siz);
577 entry_putlen(&ptr, dnlen);
578 AC_MEMCPY(ptr, e->e_dn, dnlen);
581 entry_putlen(&ptr, ndnlen);
582 AC_MEMCPY(ptr, e->e_ndn, ndnlen);
586 for (a=e->e_attrs; a; a=a->a_next) {
587 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
588 AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
589 a->a_desc->ad_cname.bv_len);
590 ptr += a->a_desc->ad_cname.bv_len;
593 for (i=0; a->a_vals[i].bv_val; i++);
594 entry_putlen(&ptr, i);
595 for (i=0; a->a_vals[i].bv_val; i++) {
596 entry_putlen(&ptr, a->a_vals[i].bv_len);
597 AC_MEMCPY(ptr, a->a_vals[i].bv_val,
598 a->a_vals[i].bv_len);
599 ptr += a->a_vals[i].bv_len;
602 if (a->a_nvals != a->a_vals) {
603 entry_putlen(&ptr, i);
604 for (i=0; a->a_nvals[i].bv_val; i++) {
605 entry_putlen(&ptr, a->a_nvals[i].bv_len);
606 AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
607 a->a_nvals[i].bv_len);
608 ptr += a->a_nvals[i].bv_len;
612 entry_putlen(&ptr, 0);
620 /* Retrieve an Entry that was stored using entry_encode above.
621 * We malloc a single block with the size stored above for the Entry
622 * and all of its Attributes. We also must lookup the stored
623 * attribute names to get AttributeDescriptions. To detect if the
624 * attributes of an Entry are later modified, we note that e->e_attr
625 * is always a constant offset from (e).
627 * Note: everything is stored in a single contiguous block, so
628 * you can not free individual attributes or names from this
629 * structure. Attempting to do so will likely corrupt memory.
631 int entry_decode(struct berval *bv, Entry **e)
638 AttributeDescription *ad;
639 unsigned char *ptr = (unsigned char *)bv->bv_val;
642 i = entry_getlen(&ptr);
644 i = entry_getlen(&ptr);
645 x->e_name.bv_val = ptr;
646 x->e_name.bv_len = i;
648 i = entry_getlen(&ptr);
649 x->e_nname.bv_val = ptr;
650 x->e_nname.bv_len = i;
653 LDAP_LOG( OPERATION, DETAIL2, "entry_decode: \"%s\"\n", x->e_dn, 0, 0 );
655 Debug( LDAP_DEBUG_TRACE,
656 "entry_decode: \"%s\"\n",
661 /* A valid entry must have at least one attr, so this
662 * pointer can never be NULL
664 x->e_attrs = (Attribute *)(x+1);
665 bptr = (BerVarray)x->e_attrs;
668 while ((i = entry_getlen(&ptr))) {
673 a->a_next = (Attribute *)bptr;
675 a = (Attribute *)bptr;
677 rc = slap_bv2ad( &bv, &ad, &text );
679 if( rc != LDAP_SUCCESS ) {
681 LDAP_LOG( OPERATION, INFO,
682 "entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
684 Debug( LDAP_DEBUG_TRACE,
685 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
687 rc = slap_bv2undef_ad( &bv, &ad, &text );
689 if( rc != LDAP_SUCCESS ) {
691 LDAP_LOG( OPERATION, INFO,
692 "entry_decode: str2undef_ad(%s): %s\n", ptr, text, 0 );
694 Debug( LDAP_DEBUG_ANY,
695 "<= entry_decode: str2undef_ad(%s): %s\n",
703 bptr = (BerVarray)(a+1);
706 count = j = entry_getlen(&ptr);
709 i = entry_getlen(&ptr);
711 bptr->bv_val = (char *)ptr;
720 j = entry_getlen(&ptr);
724 i = entry_getlen(&ptr);
726 bptr->bv_val = (char *)ptr;
735 a->a_nvals = a->a_vals;
739 if (a) a->a_next = NULL;
741 LDAP_LOG( OPERATION, DETAIL1, "entry_decode: %s\n", x->e_dn, 0, 0 );
743 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",