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 */
24 int entry_destroy(void)
42 struct berval *vals[2];
43 AttributeDescription *ad;
48 * LDIF is used as the string format.
49 * An entry looks like this:
52 * [<attr>:[:] <value>\n]
53 * [<tab><continuedvalue>\n]*
56 * If a double colon is used after a type, it means the
57 * following value is encoded as a base 64 string. This
58 * happens if the value contains a non-printing character
63 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
64 "str2entry: \"%s\"\n", s ? s : "NULL" ));
66 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
67 s ? s : "NULL", 0, 0 );
70 /* initialize reader/writer lock */
71 e = (Entry *) ch_malloc( sizeof(Entry) );
75 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
76 "str2entry: entry allocation failed.\n" ));
78 Debug( LDAP_DEBUG_ANY,
79 "<= str2entry NULL (entry allocation failed)\n",
85 /* initialize entry */
97 while ( (s = ldif_getline( &next )) != NULL ) {
98 if ( *s == '\n' || *s == '\0' ) {
102 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
104 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
105 "str2entry: NULL (parse_line)\n" ));
107 Debug( LDAP_DEBUG_TRACE,
108 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
113 if ( strcasecmp( type, "dn" ) == 0 ) {
116 if ( e->e_dn != NULL ) {
118 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
119 "entry %ld has multiple dns \"%s\" and \"%s\" "
120 "(second ignored)\n",
121 (long) e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
123 Debug( LDAP_DEBUG_ANY, "str2entry: "
124 "entry %ld has multiple dns \"%s\" and \"%s\" "
125 "(second ignored)\n",
126 (long) e->e_id, e->e_dn,
127 value.bv_val != NULL ? value.bv_val : "" );
129 if( value.bv_val != NULL ) free( value.bv_val );
133 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
138 rc = slap_str2ad( type, &ad, &text );
140 if( rc != LDAP_SUCCESS ) {
142 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
143 "str2entry: str2ad(%s): %s\n", type, text ));
145 Debug( slapMode & SLAP_TOOL_MODE
146 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
147 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
149 if( slapMode & SLAP_TOOL_MODE ) {
151 free( value.bv_val );
156 rc = slap_str2undef_ad( type, &ad, &text );
158 if( rc != LDAP_SUCCESS ) {
160 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
161 "str2entry: str2undef_ad(%s): %s\n", type, text ));
163 Debug( LDAP_DEBUG_ANY,
164 "<= str2entry: str2undef_ad(%s): %s\n",
168 free( value.bv_val );
174 if( slapMode & SLAP_TOOL_MODE ) {
175 slap_syntax_validate_func *validate =
176 ad->ad_type->sat_syntax->ssyn_validate;
180 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
181 "str2entry: no validator for syntax %s\n",
182 ad->ad_type->sat_syntax->ssyn_oid ));
184 Debug( LDAP_DEBUG_ANY,
185 "str2entry: no validator for syntax %s\n",
186 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
189 free( value.bv_val );
195 * validate value per syntax
197 rc = validate( ad->ad_type->sat_syntax, &value );
201 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
202 "str2entry: invalid value for syntax %s\n",
203 ad->ad_type->sat_syntax->ssyn_oid ));
205 Debug( LDAP_DEBUG_ANY,
206 "str2entry: invalid value for syntax %s\n",
207 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
210 free( value.bv_val );
216 rc = attr_merge( e, ad, vals );
220 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
221 "str2entry: NULL (attr_merge)\n" ));
223 Debug( LDAP_DEBUG_ANY,
224 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
227 free( value.bv_val );
233 free( value.bv_val );
236 /* check to make sure there was a dn: line */
237 if ( e->e_dn == NULL ) {
239 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
240 "str2entry: entry %ld has no dn.\n",
243 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
244 (long) e->e_id, 0, 0 );
250 /* generate normalized dn */
251 e->e_ndn = ch_strdup( e->e_dn );
252 (void) dn_normalize( e->e_ndn );
255 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
256 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
258 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
259 e->e_dn, (unsigned long) e, 0 );
265 #define GRABSIZE BUFSIZ
267 #define MAKE_SPACE( n ) { \
268 while ( ecur + (n) > ebuf + emaxsize ) { \
270 offset = (int) (ecur - ebuf); \
271 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
272 emaxsize + GRABSIZE ); \
273 emaxsize += GRABSIZE; \
274 ecur = ebuf + offset; \
288 * In string format, an entry looks like this:
290 * [<attr>: <value>\n]*
296 if ( e->e_dn != NULL ) {
298 tmplen = strlen( e->e_dn );
299 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
300 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
303 /* put the attributes */
304 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
305 /* put "<type>:[:] <value>" line for each value */
306 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
308 tmplen = a->a_desc->ad_cname.bv_len;
309 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
310 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
311 a->a_desc->ad_cname.bv_val,
312 bv->bv_val, bv->bv_len );
319 return( (char *) ebuf );
323 entry_free( Entry *e )
325 /* free an entry structure */
328 /* e_private must be freed by the caller */
329 assert( e->e_private == NULL );
333 if ( e->e_dn != NULL ) {
337 if ( e->e_ndn != NULL ) {
342 /* free attributes */
343 attrs_free( e->e_attrs );
350 * These routines are used only by Backend.
352 * the Entry has three entry points (ways to find things):
354 * by entry e.g., if you already have an entry from the cache
355 * and want to delete it. (really by entry ptr)
356 * by dn e.g., when looking for the base object of a search
357 * by id e.g., for search candidates
359 * these correspond to three different avl trees that are maintained.
363 entry_cmp( Entry *e1, Entry *e2 )
365 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
369 entry_dn_cmp( Entry *e1, Entry *e2 )
371 /* compare their normalized UPPERCASED dn's */
372 return( strcmp( e1->e_ndn, e2->e_ndn ) );
376 entry_id_cmp( Entry *e1, Entry *e2 )
378 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
383 /* Flatten an Entry into a buffer. The buffer contents become a direct
384 * copy of the entry, with all pointers converted to offsets from the
385 * beginning of the buffer. We do this by first walking through all
386 * the fields of the Entry, adding up their sizes. Then a single chunk
387 * of memory is malloc'd and the entry is copied. We differentiate between
388 * fixed size fields and variable-length content when tallying up the
389 * entry size, so that we can stick all of the variable-length stuff
390 * into the back half of the buffer.
392 int entry_encode(Entry *e, struct berval **bv)
394 int siz = sizeof(Entry);
399 struct berval **bvl, *bz;
400 char *ptr, *base, *data;
402 *bv = ch_malloc(sizeof(struct berval));
403 /* Compress any white space in the DN */
404 dn_validate(e->e_dn);
406 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
407 "entry_encode: id: 0x%08lx \"%s\"\n",
408 (long) e->e_id, e->e_dn ));
410 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
411 (long) e->e_id, e->e_dn, 0 );
413 dnlen = strlen(e->e_dn);
414 /* The dn and ndn are always the same length */
415 len = dnlen + dnlen + 2; /* two trailing NUL bytes */
416 for (a=e->e_attrs; a; a=a->a_next) {
417 /* For AttributeDesc, we only store the attr name */
418 siz += sizeof(Attribute);
419 len += a->a_desc->ad_cname.bv_len+1;
420 for (i=0; a->a_vals[i]; i++) {
421 siz += sizeof(struct berval *);
422 siz += sizeof(struct berval);
423 len += a->a_vals[i]->bv_len + 1;
425 siz += sizeof(struct berval *); /* NULL pointer at end */
427 (*bv)->bv_len = siz + len;
428 (*bv)->bv_val = ch_malloc(siz+len);
429 base = (*bv)->bv_val;
432 data = (char *)(f+1);
434 f->e_dn = (char *)(ptr-base);
435 memcpy(ptr, e->e_dn, dnlen);
438 f->e_ndn = (char *)(ptr-base);
439 memcpy(ptr, e->e_ndn, dnlen);
442 f->e_attrs = e->e_attrs ? (Attribute *)sizeof(Entry) : NULL;
444 for (a=e->e_attrs; a; a=a->a_next) {
445 b = (Attribute *)data;
446 data = (char *)(b+1);
447 b->a_desc = (AttributeDescription *)(ptr-base);
448 memcpy(ptr, a->a_desc->ad_cname.bv_val,
449 a->a_desc->ad_cname.bv_len);
450 ptr += a->a_desc->ad_cname.bv_len;
453 bvl = (struct berval **)data;
454 b->a_vals = (struct berval **)(data-base);
455 for (i=0; a->a_vals[i]; i++);
456 data = (char *)(bvl+i+1);
457 bz = (struct berval *)data;
458 for (j=0; j<i; j++) {
459 bz->bv_len = a->a_vals[j]->bv_len;
460 if (a->a_vals[j]->bv_val) {
461 bz->bv_val = (char *)(ptr-base);
462 memcpy(ptr, a->a_vals[j]->bv_val, bz->bv_len);
468 bvl[j] = (struct berval *)(data-base);
478 b->a_next = (Attribute *)(data-base);
485 /* Retrieve an Entry that was stored using entry_encode above.
486 * All we have to do is add the buffer address to all of the
487 * stored offsets. We also must lookup the stored attribute names
488 * to get AttributeDescriptions. To detect if the attributes of
489 * an Entry are later modified, we also store the address of the
490 * end of this block in e_private.
492 * Note: everything is stored in a single contiguous block, so
493 * you can not free individual attributes or names from this
494 * structure. Attempting to do so will likely corrupt memory.
496 int entry_decode(struct berval *bv, Entry **e)
501 Entry *x = (Entry *)bv->bv_val;
504 AttributeDescription *ad;
506 base = (long)bv->bv_val;
510 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
511 "entry_decode: \"%s\"\n", x->e_dn ));
513 Debug( LDAP_DEBUG_TRACE,
514 "entry_decode: \"%s\"\n",
517 x->e_private = bv->bv_val + bv->bv_len;
519 x->e_attrs = (Attribute *)((long)x->e_attrs+base);
520 for (a=x->e_attrs; a; a=a->a_next) {
522 a->a_next = (Attribute *)((long)a->a_next+base);
524 type = (char *)a->a_desc+base;
525 i = slap_str2ad( type, &ad, &text );
527 if( i != LDAP_SUCCESS ) {
529 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
530 "entry_decode: str2ad(%s): %s\n", type, text ));
532 Debug( LDAP_DEBUG_TRACE,
533 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
535 i = slap_str2undef_ad( type, &ad, &text );
537 if( i != LDAP_SUCCESS ) {
539 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
540 "entry_decode: str2undef_ad(%s): %s\n", type, text));
542 Debug( LDAP_DEBUG_ANY,
543 "<= entry_decode: str2undef_ad(%s): %s\n",
551 a->a_vals = (struct berval **)((long)a->a_vals+base);
552 for (i=0; a->a_vals[i]; i++) {
553 a->a_vals[i] = (struct berval *)
554 ((long)a->a_vals[i]+base);
555 if (a->a_vals[i]->bv_val)
556 a->a_vals[i]->bv_val += base;
561 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
562 "entry_decode: %s\n", x->e_dn ));
564 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",