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);
395 int len, dnlen, ndnlen;
399 struct berval **bvl, *bz;
400 char *ptr, *base, *data;
402 *bv = ch_malloc(sizeof(struct berval));
404 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
405 "entry_encode: id: 0x%08lx \"%s\"\n",
406 (long) e->e_id, e->e_dn ));
408 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
409 (long) e->e_id, e->e_dn, 0 );
411 dnlen = strlen(e->e_dn);
412 ndnlen = strlen(e->e_ndn);
413 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
414 for (a=e->e_attrs; a; a=a->a_next) {
415 /* For AttributeDesc, we only store the attr name */
416 siz += sizeof(Attribute);
417 len += a->a_desc->ad_cname.bv_len+1;
418 for (i=0; a->a_vals[i]; i++) {
419 siz += sizeof(struct berval *);
420 siz += sizeof(struct berval);
421 len += a->a_vals[i]->bv_len + 1;
423 siz += sizeof(struct berval *); /* NULL pointer at end */
425 (*bv)->bv_len = siz + len;
426 (*bv)->bv_val = ch_malloc(siz+len);
427 base = (*bv)->bv_val;
430 data = (char *)(f+1);
432 f->e_dn = (char *)(ptr-base);
433 memcpy(ptr, e->e_dn, dnlen);
436 f->e_ndn = (char *)(ptr-base);
437 memcpy(ptr, e->e_ndn, ndnlen);
440 f->e_attrs = e->e_attrs ? (Attribute *)sizeof(Entry) : NULL;
442 for (a=e->e_attrs; a; a=a->a_next) {
443 b = (Attribute *)data;
444 data = (char *)(b+1);
445 b->a_desc = (AttributeDescription *)(ptr-base);
446 memcpy(ptr, a->a_desc->ad_cname.bv_val,
447 a->a_desc->ad_cname.bv_len);
448 ptr += a->a_desc->ad_cname.bv_len;
451 bvl = (struct berval **)data;
452 b->a_vals = (struct berval **)(data-base);
453 for (i=0; a->a_vals[i]; i++);
454 data = (char *)(bvl+i+1);
455 bz = (struct berval *)data;
456 for (j=0; j<i; j++) {
457 bz->bv_len = a->a_vals[j]->bv_len;
458 if (a->a_vals[j]->bv_val) {
459 bz->bv_val = (char *)(ptr-base);
460 memcpy(ptr, a->a_vals[j]->bv_val, bz->bv_len);
466 bvl[j] = (struct berval *)(data-base);
476 b->a_next = (Attribute *)(data-base);
483 /* Retrieve an Entry that was stored using entry_encode above.
484 * All we have to do is add the buffer address to all of the
485 * stored offsets. We also must lookup the stored attribute names
486 * to get AttributeDescriptions. To detect if the attributes of
487 * an Entry are later modified, we also store the address of the
488 * end of this block in e_private.
490 * Note: everything is stored in a single contiguous block, so
491 * you can not free individual attributes or names from this
492 * structure. Attempting to do so will likely corrupt memory.
494 int entry_decode(struct berval *bv, Entry **e)
499 Entry *x = (Entry *)bv->bv_val;
502 AttributeDescription *ad;
504 base = (long)bv->bv_val;
508 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
509 "entry_decode: \"%s\"\n", x->e_dn ));
511 Debug( LDAP_DEBUG_TRACE,
512 "entry_decode: \"%s\"\n",
515 x->e_private = bv->bv_val + bv->bv_len;
517 x->e_attrs = (Attribute *)((long)x->e_attrs+base);
518 for (a=x->e_attrs; a; a=a->a_next) {
520 a->a_next = (Attribute *)((long)a->a_next+base);
522 type = (char *)a->a_desc+base;
523 i = slap_str2ad( type, &ad, &text );
525 if( i != LDAP_SUCCESS ) {
527 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
528 "entry_decode: str2ad(%s): %s\n", type, text ));
530 Debug( LDAP_DEBUG_TRACE,
531 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
533 i = slap_str2undef_ad( type, &ad, &text );
535 if( i != LDAP_SUCCESS ) {
537 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
538 "entry_decode: str2undef_ad(%s): %s\n", type, text));
540 Debug( LDAP_DEBUG_ANY,
541 "<= entry_decode: str2undef_ad(%s): %s\n",
549 a->a_vals = (struct berval **)((long)a->a_vals+base);
550 for (i=0; a->a_vals[i]; i++) {
551 a->a_vals[i] = (struct berval *)
552 ((long)a->a_vals[i]+base);
553 if (a->a_vals[i]->bv_val)
554 a->a_vals[i]->bv_val += base;
559 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
560 "entry_decode: %s\n", x->e_dn ));
562 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",