1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2000 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 = { NOID, "", "", NULL, NULL };
29 int entry_destroy(void)
46 struct berval *vals[2];
47 AttributeDescription *ad;
52 * LDIF is used as the string format.
53 * An entry looks like this:
56 * [<attr>:[:] <value>\n]
57 * [<tab><continuedvalue>\n]*
60 * If a double colon is used after a type, it means the
61 * following value is encoded as a base 64 string. This
62 * happens if the value contains a non-printing character
67 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
68 "str2entry: \"%s\"\n", s ? s : "NULL" ));
70 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
71 s ? s : "NULL", 0, 0 );
74 /* initialize reader/writer lock */
75 e = (Entry *) ch_malloc( sizeof(Entry) );
79 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
80 "str2entry: entry allocation failed.\n" ));
82 Debug( LDAP_DEBUG_ANY,
83 "<= str2entry NULL (entry allocation failed)\n",
89 /* initialize entry */
101 while ( (s = ldif_getline( &next )) != NULL ) {
102 if ( *s == '\n' || *s == '\0' ) {
106 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
108 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
109 "str2entry: NULL (parse_line)\n" ));
111 Debug( LDAP_DEBUG_TRACE,
112 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
117 if ( strcasecmp( type, "dn" ) == 0 ) {
120 if ( e->e_dn != NULL ) {
122 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
123 "entry %ld has multiple dns \"%s\" and \"%s\" "
124 "(second ignored)\n",
125 (long) e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
127 Debug( LDAP_DEBUG_ANY, "str2entry: "
128 "entry %ld has multiple dns \"%s\" and \"%s\" "
129 "(second ignored)\n",
130 (long) e->e_id, e->e_dn,
131 value.bv_val != NULL ? value.bv_val : "" );
133 if( value.bv_val != NULL ) free( value.bv_val );
137 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
142 rc = slap_str2ad( type, &ad, &text );
144 if( rc != LDAP_SUCCESS ) {
146 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
147 "str2entry: str2ad(%s): %s\n", type, text ));
149 Debug( slapMode & SLAP_TOOL_MODE
150 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
151 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
153 if( slapMode & SLAP_TOOL_MODE ) {
155 free( value.bv_val );
160 rc = slap_str2undef_ad( type, &ad, &text );
162 if( rc != LDAP_SUCCESS ) {
164 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
165 "str2entry: str2undef_ad(%s): %s\n", type, text ));
167 Debug( LDAP_DEBUG_ANY,
168 "<= str2entry: str2undef_ad(%s): %s\n",
172 free( value.bv_val );
178 if( slapMode & SLAP_TOOL_MODE ) {
180 slap_syntax_validate_func *validate =
181 ad->ad_type->sat_syntax->ssyn_validate;
182 slap_syntax_transform_func *pretty =
183 ad->ad_type->sat_syntax->ssyn_pretty;
186 rc = pretty( ad->ad_type->sat_syntax,
189 } else if( validate ) {
191 * validate value per syntax
193 rc = validate( ad->ad_type->sat_syntax, &value );
197 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
198 "str2entry: no validator for syntax %s\n",
199 ad->ad_type->sat_syntax->ssyn_oid ));
201 Debug( LDAP_DEBUG_ANY,
202 "str2entry: no validator for syntax %s\n",
203 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
206 free( value.bv_val );
213 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
214 "str2entry: invalid value for syntax %s\n",
215 ad->ad_type->sat_syntax->ssyn_oid ));
217 Debug( LDAP_DEBUG_ANY,
218 "str2entry: invalid value for syntax %s\n",
219 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
222 free( value.bv_val );
228 free( value.bv_val );
234 rc = attr_merge( e, ad, vals );
238 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
239 "str2entry: NULL (attr_merge)\n" ));
241 Debug( LDAP_DEBUG_ANY,
242 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
245 free( value.bv_val );
251 free( value.bv_val );
254 /* check to make sure there was a dn: line */
255 if ( e->e_dn == NULL ) {
257 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
258 "str2entry: entry %ld has no dn.\n",
261 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
262 (long) e->e_id, 0, 0 );
268 /* generate normalized dn */
269 e->e_ndn = ch_strdup( e->e_dn );
270 (void) dn_normalize( e->e_ndn );
273 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
274 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
276 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
277 e->e_dn, (unsigned long) e, 0 );
283 #define GRABSIZE BUFSIZ
285 #define MAKE_SPACE( n ) { \
286 while ( ecur + (n) > ebuf + emaxsize ) { \
288 offset = (int) (ecur - ebuf); \
289 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
290 emaxsize + GRABSIZE ); \
291 emaxsize += GRABSIZE; \
292 ecur = ebuf + offset; \
306 * In string format, an entry looks like this:
308 * [<attr>: <value>\n]*
314 if ( e->e_dn != NULL ) {
316 tmplen = strlen( e->e_dn );
317 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
318 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
321 /* put the attributes */
322 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
323 /* put "<type>:[:] <value>" line for each value */
324 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
326 tmplen = a->a_desc->ad_cname.bv_len;
327 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
328 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
329 a->a_desc->ad_cname.bv_val,
330 bv->bv_val, bv->bv_len );
337 return( (char *) ebuf );
341 entry_free( Entry *e )
343 /* free an entry structure */
346 /* e_private must be freed by the caller */
347 assert( e->e_private == NULL );
351 if ( e->e_dn != NULL ) {
355 if ( e->e_ndn != NULL ) {
360 /* free attributes */
361 attrs_free( e->e_attrs );
368 * These routines are used only by Backend.
370 * the Entry has three entry points (ways to find things):
372 * by entry e.g., if you already have an entry from the cache
373 * and want to delete it. (really by entry ptr)
374 * by dn e.g., when looking for the base object of a search
375 * by id e.g., for search candidates
377 * these correspond to three different avl trees that are maintained.
381 entry_cmp( Entry *e1, Entry *e2 )
383 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
387 entry_dn_cmp( Entry *e1, Entry *e2 )
389 /* compare their normalized UPPERCASED dn's */
390 return( strcmp( e1->e_ndn, e2->e_ndn ) );
394 entry_id_cmp( Entry *e1, Entry *e2 )
396 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
401 /* This is like a ber_len */
403 entry_lenlen(ber_len_t len)
417 entry_putlen(unsigned char **buf, ber_len_t len)
419 ber_len_t lenlen = entry_lenlen(len);
422 **buf = (unsigned char) len;
425 **buf = 0x80 | (lenlen - 1);
426 for (i=lenlen-1; i>0; i--) {
427 (*buf)[i] = (unsigned char) len;
435 entry_getlen(unsigned char **buf)
452 /* Flatten an Entry into a buffer. The buffer is filled with just the
453 * strings/bervals of all the entry components. Each field is preceded
454 * by its length, encoded the way ber_put_len works. Every field is NUL
455 * terminated. The entire buffer size is precomputed so that a single
456 * malloc can be performed. The entry size is also recorded,
457 * to aid in entry_decode.
459 int entry_encode(Entry *e, struct berval *bv)
461 int siz = sizeof(Entry);
462 int len, dnlen, ndnlen;
468 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
469 "entry_encode: id: 0x%08lx \"%s\"\n",
470 (long) e->e_id, e->e_dn ));
472 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
473 (long) e->e_id, e->e_dn, 0 );
475 dnlen = strlen(e->e_dn);
476 ndnlen = strlen(e->e_ndn);
477 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
478 len += entry_lenlen(dnlen);
479 len += entry_lenlen(ndnlen);
480 for (a=e->e_attrs; a; a=a->a_next) {
481 /* For AttributeDesc, we only store the attr name */
482 siz += sizeof(Attribute);
483 len += a->a_desc->ad_cname.bv_len+1;
484 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
485 for (i=0; a->a_vals[i]; i++) {
486 siz += sizeof(struct berval *);
487 siz += sizeof(struct berval);
488 len += a->a_vals[i]->bv_len + 1;
489 len += entry_lenlen(a->a_vals[i]->bv_len);
491 len += entry_lenlen(i);
492 siz += sizeof(struct berval *); /* NULL pointer at end */
494 len += 1; /* NUL byte at end */
495 len += entry_lenlen(siz);
497 bv->bv_val = ch_malloc(len);
498 ptr = (unsigned char *)bv->bv_val;
499 entry_putlen(&ptr, siz);
500 entry_putlen(&ptr, dnlen);
501 memcpy(ptr, e->e_dn, dnlen);
504 entry_putlen(&ptr, ndnlen);
505 memcpy(ptr, e->e_ndn, ndnlen);
509 for (a=e->e_attrs; a; a=a->a_next) {
510 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
511 memcpy(ptr, a->a_desc->ad_cname.bv_val,
512 a->a_desc->ad_cname.bv_len);
513 ptr += a->a_desc->ad_cname.bv_len;
516 for (i=0; a->a_vals[i]; i++);
517 entry_putlen(&ptr, i);
518 for (i=0; a->a_vals[i]; i++) {
519 entry_putlen(&ptr, a->a_vals[i]->bv_len);
520 memcpy(ptr, a->a_vals[i]->bv_val,
521 a->a_vals[i]->bv_len);
522 ptr += a->a_vals[i]->bv_len;
531 /* Retrieve an Entry that was stored using entry_encode above.
532 * We malloc a single block with the size stored above for the Entry
533 * and all if its Attributes. We also must lookup the stored
534 * attribute names to get AttributeDescriptions. To detect if the
535 * attributes of an Entry are later modified, we note that e->e_attr
536 * is always a constant offset from (e).
538 * Note: everything is stored in a single contiguous block, so
539 * you can not free individual attributes or names from this
540 * structure. Attempting to do so will likely corrupt memory.
542 int entry_decode(struct berval *bv, Entry **e)
549 AttributeDescription *ad;
550 unsigned char *ptr = (unsigned char *)bv->bv_val;
551 struct berval **bptr;
554 i = entry_getlen(&ptr);
556 i = entry_getlen(&ptr);
559 i = entry_getlen(&ptr);
563 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
564 "entry_decode: \"%s\"\n", x->e_dn ));
566 Debug( LDAP_DEBUG_TRACE,
567 "entry_decode: \"%s\"\n",
570 x->e_private = bv->bv_val;
572 /* A valid entry must have at least one attr, so this
573 * pointer can never be NULL
575 x->e_attrs = (Attribute *)(x+1);
576 bptr = (struct berval **)x->e_attrs;
579 while (i = entry_getlen(&ptr)) {
581 a->a_next = (Attribute *)bptr;
583 a = (Attribute *)bptr;
585 rc = slap_str2ad( ptr, &ad, &text );
587 if( rc != LDAP_SUCCESS ) {
589 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
590 "entry_decode: str2ad(%s): %s\n", ptr, text ));
592 Debug( LDAP_DEBUG_TRACE,
593 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
595 rc = slap_str2undef_ad( ptr, &ad, &text );
597 if( rc != LDAP_SUCCESS ) {
599 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
600 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
602 Debug( LDAP_DEBUG_ANY,
603 "<= entry_decode: str2undef_ad(%s): %s\n",
611 bptr = (struct berval **)(a+1);
613 j = entry_getlen(&ptr);
615 vptr = (struct berval *)(bptr + j + 1);
618 i = entry_getlen(&ptr);
621 vptr->bv_val = (char *)ptr;
627 bptr = (struct berval **)vptr;
632 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
633 "entry_decode: %s\n", x->e_dn ));
635 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",