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,
119 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
120 e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
122 Debug( LDAP_DEBUG_ANY,
123 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
125 value.bv_val != NULL ? value.bv_val : "" );
127 if( value.bv_val != NULL ) free( value.bv_val );
131 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
136 rc = slap_str2ad( type, &ad, &text );
138 if( rc != LDAP_SUCCESS ) {
140 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
141 "str2entry: str2ad(%s): %s\n", type, text ));
143 Debug( slapMode & SLAP_TOOL_MODE
144 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
145 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
147 if( slapMode & SLAP_TOOL_MODE ) {
149 free( value.bv_val );
154 rc = slap_str2undef_ad( type, &ad, &text );
156 if( rc != LDAP_SUCCESS ) {
158 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
159 "str2entry: str2undef_ad(%s): %s\n", type, text ));
161 Debug( LDAP_DEBUG_ANY,
162 "<= str2entry: str2undef_ad(%s): %s\n",
166 free( value.bv_val );
172 if( slapMode & SLAP_TOOL_MODE ) {
173 slap_syntax_validate_func *validate =
174 ad->ad_type->sat_syntax->ssyn_validate;
178 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
179 "str2entry: no validator for syntax %s\n",
180 ad->ad_type->sat_syntax->ssyn_oid ));
182 Debug( LDAP_DEBUG_ANY,
183 "str2entry: no validator for syntax %s\n",
184 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
187 free( value.bv_val );
193 * validate value per syntax
195 rc = validate( ad->ad_type->sat_syntax, &value );
199 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
200 "str2entry: invalid value for syntax %s\n",
201 ad->ad_type->sat_syntax->ssyn_oid ));
203 Debug( LDAP_DEBUG_ANY,
204 "str2entry: invalid value for syntax %s\n",
205 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
208 free( value.bv_val );
214 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", e->e_id ));
242 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
249 /* generate normalized dn */
250 e->e_ndn = ch_strdup( e->e_dn );
251 (void) dn_normalize( e->e_ndn );
254 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
255 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
257 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
258 e->e_dn, (unsigned long) e, 0 );
264 #define GRABSIZE BUFSIZ
266 #define MAKE_SPACE( n ) { \
267 while ( ecur + (n) > ebuf + emaxsize ) { \
269 offset = (int) (ecur - ebuf); \
270 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
271 emaxsize + GRABSIZE ); \
272 emaxsize += GRABSIZE; \
273 ecur = ebuf + offset; \
287 * In string format, an entry looks like this:
289 * [<attr>: <value>\n]*
295 if ( e->e_dn != NULL ) {
297 tmplen = strlen( e->e_dn );
298 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
299 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
302 /* put the attributes */
303 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
304 /* put "<type>:[:] <value>" line for each value */
305 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
307 tmplen = a->a_desc->ad_cname->bv_len;
308 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
309 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
310 a->a_desc->ad_cname->bv_val,
311 bv->bv_val, bv->bv_len );
318 return( (char *) ebuf );
322 entry_free( Entry *e )
324 /* free an entry structure */
327 /* e_private must be freed by the caller */
328 assert( e->e_private == NULL );
332 if ( e->e_dn != NULL ) {
336 if ( e->e_ndn != NULL ) {
341 /* free attributes */
342 attrs_free( e->e_attrs );
349 * These routines are used only by Backend.
351 * the Entry has three entry points (ways to find things):
353 * by entry e.g., if you already have an entry from the cache
354 * and want to delete it. (really by entry ptr)
355 * by dn e.g., when looking for the base object of a search
356 * by id e.g., for search candidates
358 * these correspond to three different avl trees that are maintained.
362 entry_cmp( Entry *e1, Entry *e2 )
364 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
368 entry_dn_cmp( Entry *e1, Entry *e2 )
370 /* compare their normalized UPPERCASED dn's */
371 return( strcmp( e1->e_ndn, e2->e_ndn ) );
375 entry_id_cmp( Entry *e1, Entry *e2 )
377 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
382 /* a LBER encoded entry looks like:
384 * entry :== SEQUENCE {
385 * dn DistinguishedName,
386 * ndn NormalizedDistinguishedName,
387 * attrs SEQUENCE OF SEQUENCE {
388 * type AttributeType,
389 * values SET OF AttributeValue
393 * Encoding/Decoding of LBER should be much faster than LDIF
396 int entry_decode( struct berval *bv, Entry **entry )
405 assert( bv != NULL );
406 assert( entry != NULL );
408 ber = ber_init( bv );
410 assert( 0 ); /* XXYYZ: Temporary assert */
413 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
414 "entry_decode: ber_init failed\n" ));
416 Debug( LDAP_DEBUG_ANY,
417 "<= entry_decode: ber_init failed\n",
420 return LDAP_LOCAL_ERROR;
423 /* initialize reader/writer lock */
424 e = (Entry *) ch_malloc( sizeof(Entry) );
428 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
429 "entry_decode: entry allocation failed.\n" ));
431 Debug( LDAP_DEBUG_ANY,
432 "<= entry_decode: entry allocation failed\n",
435 return LDAP_LOCAL_ERROR;
438 /* initialize entry */
445 tag = ber_scanf( ber, "{aa" /*"}"*/, &e->e_dn, &e->e_ndn );
446 if( tag == LBER_ERROR ) {
448 return LDAP_PROTOCOL_ERROR;
452 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
453 "entry_decode: \"%s\"\n", e->e_dn ));
455 Debug( LDAP_DEBUG_TRACE,
456 "entry_decode: \"%s\"\n",
460 for ( tag = ber_first_element( ber, &len, &last );
462 tag = ber_next_element( ber, &len, last ) )
465 struct berval **vals;
466 AttributeDescription *ad;
469 tag = ber_scanf( ber, "{O{V}}", &type, &vals );
471 if ( tag == LBER_ERROR ) {
473 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
474 "entry_decode: decoding error (%s)\n", e->e_dn ));
476 Debug( LDAP_DEBUG_ANY, "entry_decode: decoding error\n", 0, 0, 0 );
479 return LDAP_PROTOCOL_ERROR;
482 if ( vals == NULL ) {
484 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
485 "entry_decode: no values for type %s\n", type ));
487 Debug( LDAP_DEBUG_ANY, "entry_decode: no values for type %s\n",
492 return LDAP_PROTOCOL_ERROR;
496 rc = slap_bv2ad( type, &ad, &text );
498 if( rc != LDAP_SUCCESS ) {
500 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
501 "entry_decode: str2ad(%s): %s\n", type, text ));
503 Debug( LDAP_DEBUG_TRACE,
504 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
506 rc = slap_bv2undef_ad( type, &ad, &text );
508 if( rc != LDAP_SUCCESS ) {
510 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
511 "entry_decode: str2undef_ad(%s): %s\n", type, text));
513 Debug( LDAP_DEBUG_ANY,
514 "<= entry_decode: str2undef_ad(%s): %s\n",
518 ber_bvecfree( vals );
524 rc = attr_merge( e, ad, vals );
529 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
530 "entry_decode: attr_merge failed\n"));
532 Debug( LDAP_DEBUG_ANY,
533 "<= entry_decode: attr_merge failed\n", 0, 0, 0 );
536 ber_bvecfree( vals );
538 return LDAP_LOCAL_ERROR;
542 ber_bvecfree( vals );
545 rc = ber_scanf( ber, /*"{"*/ "}" );
548 return LDAP_PROTOCOL_ERROR;
552 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
553 "entry_decode: %s\n", e->e_dn ));
555 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
572 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
573 "entry_encode: id: 0x%08lx \"%s\"\n",
576 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
577 e->e_id, e->e_dn, 0 );
579 ber = ber_alloc_t( LBER_USE_DER );
584 rc = ber_printf( ber, "{ss{" /*"}}"*/, e->e_dn, e->e_ndn );
589 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
590 rc = ber_printf( ber, "{O{V}}",
598 rc = ber_printf( ber, /*"{{"*/ "}}" );
603 rc = ber_flatten( ber, bv );
609 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
610 "entry_encode: id=0x%08lx failed (%d)\n", e->e_id, rc ));
612 Debug( LDAP_DEBUG_ANY, "=> entry_encode(0x%08lx): failed (%d)\n",