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 ) {
179 slap_syntax_validate_func *validate =
180 ad->ad_type->sat_syntax->ssyn_validate;
184 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
185 "str2entry: no validator for syntax %s\n",
186 ad->ad_type->sat_syntax->ssyn_oid ));
188 Debug( LDAP_DEBUG_ANY,
189 "str2entry: no validator for syntax %s\n",
190 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
193 free( value.bv_val );
199 * validate value per syntax
201 rc = validate( ad->ad_type->sat_syntax, &value );
205 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
206 "str2entry: invalid value for syntax %s\n",
207 ad->ad_type->sat_syntax->ssyn_oid ));
209 Debug( LDAP_DEBUG_ANY,
210 "str2entry: invalid value for syntax %s\n",
211 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
214 free( value.bv_val );
220 rc = attr_merge( e, ad, vals );
224 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
225 "str2entry: NULL (attr_merge)\n" ));
227 Debug( LDAP_DEBUG_ANY,
228 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
231 free( value.bv_val );
237 free( value.bv_val );
240 /* check to make sure there was a dn: line */
241 if ( e->e_dn == NULL ) {
243 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
244 "str2entry: entry %ld has no dn.\n",
247 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
248 (long) e->e_id, 0, 0 );
254 /* generate normalized dn */
255 e->e_ndn = ch_strdup( e->e_dn );
256 (void) dn_normalize( e->e_ndn );
259 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
260 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
262 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
263 e->e_dn, (unsigned long) e, 0 );
269 #define GRABSIZE BUFSIZ
271 #define MAKE_SPACE( n ) { \
272 while ( ecur + (n) > ebuf + emaxsize ) { \
274 offset = (int) (ecur - ebuf); \
275 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
276 emaxsize + GRABSIZE ); \
277 emaxsize += GRABSIZE; \
278 ecur = ebuf + offset; \
292 * In string format, an entry looks like this:
294 * [<attr>: <value>\n]*
300 if ( e->e_dn != NULL ) {
302 tmplen = strlen( e->e_dn );
303 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
304 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
307 /* put the attributes */
308 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
309 /* put "<type>:[:] <value>" line for each value */
310 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
312 tmplen = a->a_desc->ad_cname.bv_len;
313 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
314 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
315 a->a_desc->ad_cname.bv_val,
316 bv->bv_val, bv->bv_len );
323 return( (char *) ebuf );
327 entry_free( Entry *e )
329 /* free an entry structure */
332 /* e_private must be freed by the caller */
333 assert( e->e_private == NULL );
337 if ( e->e_dn != NULL ) {
341 if ( e->e_ndn != NULL ) {
346 /* free attributes */
347 attrs_free( e->e_attrs );
354 * These routines are used only by Backend.
356 * the Entry has three entry points (ways to find things):
358 * by entry e.g., if you already have an entry from the cache
359 * and want to delete it. (really by entry ptr)
360 * by dn e.g., when looking for the base object of a search
361 * by id e.g., for search candidates
363 * these correspond to three different avl trees that are maintained.
367 entry_cmp( Entry *e1, Entry *e2 )
369 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
373 entry_dn_cmp( Entry *e1, Entry *e2 )
375 /* compare their normalized UPPERCASED dn's */
376 return( strcmp( e1->e_ndn, e2->e_ndn ) );
380 entry_id_cmp( Entry *e1, Entry *e2 )
382 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
387 /* This is like a ber_len */
389 entry_lenlen(ber_len_t len)
403 entry_putlen(unsigned char **buf, ber_len_t len)
405 ber_len_t lenlen = entry_lenlen(len);
408 **buf = (unsigned char) len;
411 **buf = 0x80 | (lenlen - 1);
412 for (i=lenlen-1; i>0; i--) {
413 (*buf)[i] = (unsigned char) len;
421 entry_getlen(unsigned char **buf)
438 /* Flatten an Entry into a buffer. The buffer is filled with just the
439 * strings/bervals of all the entry components. Each field is preceded
440 * by its length, encoded the way ber_put_len works. Every field is NUL
441 * terminated. The entire buffer size is precomputed so that a single
442 * malloc can be performed. The entry size is also recorded,
443 * to aid in entry_decode.
445 int entry_encode(Entry *e, struct berval *bv)
447 int siz = sizeof(Entry);
448 int len, dnlen, ndnlen;
454 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
455 "entry_encode: id: 0x%08lx \"%s\"\n",
456 (long) e->e_id, e->e_dn ));
458 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
459 (long) e->e_id, e->e_dn, 0 );
461 dnlen = strlen(e->e_dn);
462 ndnlen = strlen(e->e_ndn);
463 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
464 len += entry_lenlen(dnlen);
465 len += entry_lenlen(ndnlen);
466 for (a=e->e_attrs; a; a=a->a_next) {
467 /* For AttributeDesc, we only store the attr name */
468 siz += sizeof(Attribute);
469 len += a->a_desc->ad_cname.bv_len+1;
470 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
471 for (i=0; a->a_vals[i]; i++) {
472 siz += sizeof(struct berval *);
473 siz += sizeof(struct berval);
474 len += a->a_vals[i]->bv_len + 1;
475 len += entry_lenlen(a->a_vals[i]->bv_len);
477 len += entry_lenlen(i);
478 siz += sizeof(struct berval *); /* NULL pointer at end */
480 len += 1; /* NUL byte at end */
481 len += entry_lenlen(siz);
483 bv->bv_val = ch_malloc(len);
484 ptr = (unsigned char *)bv->bv_val;
485 entry_putlen(&ptr, siz);
486 entry_putlen(&ptr, dnlen);
487 memcpy(ptr, e->e_dn, dnlen);
490 entry_putlen(&ptr, ndnlen);
491 memcpy(ptr, e->e_ndn, ndnlen);
495 for (a=e->e_attrs; a; a=a->a_next) {
496 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
497 memcpy(ptr, a->a_desc->ad_cname.bv_val,
498 a->a_desc->ad_cname.bv_len);
499 ptr += a->a_desc->ad_cname.bv_len;
502 for (i=0; a->a_vals[i]; i++);
503 entry_putlen(&ptr, i);
504 for (i=0; a->a_vals[i]; i++) {
505 entry_putlen(&ptr, a->a_vals[i]->bv_len);
506 memcpy(ptr, a->a_vals[i]->bv_val,
507 a->a_vals[i]->bv_len);
508 ptr += a->a_vals[i]->bv_len;
517 /* Retrieve an Entry that was stored using entry_encode above.
518 * We malloc a single block with the size stored above for the Entry
519 * and all if its Attributes. We also must lookup the stored
520 * attribute names to get AttributeDescriptions. To detect if the
521 * attributes of an Entry are later modified, we note that e->e_attr
522 * is always a constant offset from (e).
524 * Note: everything is stored in a single contiguous block, so
525 * you can not free individual attributes or names from this
526 * structure. Attempting to do so will likely corrupt memory.
528 int entry_decode(struct berval *bv, Entry **e)
535 AttributeDescription *ad;
536 unsigned char *ptr = (unsigned char *)bv->bv_val;
537 struct berval **bptr;
540 i = entry_getlen(&ptr);
542 i = entry_getlen(&ptr);
545 i = entry_getlen(&ptr);
549 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
550 "entry_decode: \"%s\"\n", x->e_dn ));
552 Debug( LDAP_DEBUG_TRACE,
553 "entry_decode: \"%s\"\n",
556 x->e_private = bv->bv_val;
558 /* A valid entry must have at least one attr, so this
559 * pointer can never be NULL
561 x->e_attrs = (Attribute *)(x+1);
562 bptr = (struct berval **)x->e_attrs;
565 while (i = entry_getlen(&ptr)) {
567 a->a_next = (Attribute *)bptr;
569 a = (Attribute *)bptr;
571 rc = slap_str2ad( ptr, &ad, &text );
573 if( rc != LDAP_SUCCESS ) {
575 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
576 "entry_decode: str2ad(%s): %s\n", ptr, text ));
578 Debug( LDAP_DEBUG_TRACE,
579 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
581 rc = slap_str2undef_ad( ptr, &ad, &text );
583 if( rc != LDAP_SUCCESS ) {
585 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
586 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
588 Debug( LDAP_DEBUG_ANY,
589 "<= entry_decode: str2undef_ad(%s): %s\n",
597 bptr = (struct berval **)(a+1);
599 j = entry_getlen(&ptr);
601 vptr = (struct berval *)(bptr + j + 1);
604 i = entry_getlen(&ptr);
607 vptr->bv_val = (char *)ptr;
613 bptr = (struct berval **)vptr;
618 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
619 "entry_decode: %s\n", x->e_dn ));
621 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",