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 /* Flatten an Entry into a buffer. The buffer contents become a direct
388 * copy of the entry, with all pointers converted to offsets from the
389 * beginning of the buffer. We do this by first walking through all
390 * the fields of the Entry, adding up their sizes. Then a single chunk
391 * of memory is malloc'd and the entry is copied. We differentiate between
392 * fixed size fields and variable-length content when tallying up the
393 * entry size, so that we can stick all of the variable-length stuff
394 * into the back half of the buffer.
396 int entry_encode(Entry *e, struct berval **bv)
398 int siz = sizeof(Entry);
399 int len, dnlen, ndnlen;
403 struct berval **bvl, *bz;
404 char *ptr, *base, *data;
406 *bv = ch_malloc(sizeof(struct berval));
408 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
409 "entry_encode: id: 0x%08lx \"%s\"\n",
410 (long) e->e_id, e->e_dn ));
412 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
413 (long) e->e_id, e->e_dn, 0 );
415 dnlen = strlen(e->e_dn);
416 ndnlen = strlen(e->e_ndn);
417 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
418 for (a=e->e_attrs; a; a=a->a_next) {
419 /* For AttributeDesc, we only store the attr name */
420 siz += sizeof(Attribute);
421 len += a->a_desc->ad_cname.bv_len+1;
422 for (i=0; a->a_vals[i]; i++) {
423 siz += sizeof(struct berval *);
424 siz += sizeof(struct berval);
425 len += a->a_vals[i]->bv_len + 1;
427 siz += sizeof(struct berval *); /* NULL pointer at end */
429 (*bv)->bv_len = siz + len;
430 (*bv)->bv_val = ch_malloc(siz+len);
431 base = (*bv)->bv_val;
434 data = (char *)(f+1);
436 f->e_dn = (char *)(ptr-base);
437 memcpy(ptr, e->e_dn, dnlen);
440 f->e_ndn = (char *)(ptr-base);
441 memcpy(ptr, e->e_ndn, ndnlen);
444 f->e_attrs = e->e_attrs ? (Attribute *)sizeof(Entry) : NULL;
446 for (a=e->e_attrs; a; a=a->a_next) {
447 b = (Attribute *)data;
448 data = (char *)(b+1);
449 b->a_desc = (AttributeDescription *)(ptr-base);
450 memcpy(ptr, a->a_desc->ad_cname.bv_val,
451 a->a_desc->ad_cname.bv_len);
452 ptr += a->a_desc->ad_cname.bv_len;
455 bvl = (struct berval **)data;
456 b->a_vals = (struct berval **)(data-base);
457 for (i=0; a->a_vals[i]; i++);
458 data = (char *)(bvl+i+1);
459 bz = (struct berval *)data;
460 for (j=0; j<i; j++) {
461 bz->bv_len = a->a_vals[j]->bv_len;
462 if (a->a_vals[j]->bv_val) {
463 bz->bv_val = (char *)(ptr-base);
464 memcpy(ptr, a->a_vals[j]->bv_val, bz->bv_len);
470 bvl[j] = (struct berval *)(data-base);
480 b->a_next = (Attribute *)(data-base);
487 /* Retrieve an Entry that was stored using entry_encode above.
488 * All we have to do is add the buffer address to all of the
489 * stored offsets. We also must lookup the stored attribute names
490 * to get AttributeDescriptions. To detect if the attributes of
491 * an Entry are later modified, we also store the address of the
492 * end of this block in e_private.
494 * Note: everything is stored in a single contiguous block, so
495 * you can not free individual attributes or names from this
496 * structure. Attempting to do so will likely corrupt memory.
498 int entry_decode(struct berval *bv, Entry **e)
503 Entry *x = (Entry *)bv->bv_val;
506 AttributeDescription *ad;
508 base = (long)bv->bv_val;
512 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
513 "entry_decode: \"%s\"\n", x->e_dn ));
515 Debug( LDAP_DEBUG_TRACE,
516 "entry_decode: \"%s\"\n",
519 x->e_private = bv->bv_val + bv->bv_len;
521 x->e_attrs = (Attribute *)((long)x->e_attrs+base);
522 for (a=x->e_attrs; a; a=a->a_next) {
524 a->a_next = (Attribute *)((long)a->a_next+base);
526 type = (char *)a->a_desc+base;
527 i = slap_str2ad( type, &ad, &text );
529 if( i != LDAP_SUCCESS ) {
531 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
532 "entry_decode: str2ad(%s): %s\n", type, text ));
534 Debug( LDAP_DEBUG_TRACE,
535 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
537 i = slap_str2undef_ad( type, &ad, &text );
539 if( i != LDAP_SUCCESS ) {
541 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
542 "entry_decode: str2undef_ad(%s): %s\n", type, text));
544 Debug( LDAP_DEBUG_ANY,
545 "<= entry_decode: str2undef_ad(%s): %s\n",
553 a->a_vals = (struct berval **)((long)a->a_vals+base);
554 for (i=0; a->a_vals[i]; i++) {
555 a->a_vals[i] = (struct berval *)
556 ((long)a->a_vals[i]+base);
557 if (a->a_vals[i]->bv_val)
558 a->a_vals[i]->bv_val += base;
563 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
564 "entry_decode: %s\n", x->e_dn ));
566 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",