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 );
222 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
223 "str2entry: NULL (attr_merge)\n" ));
225 Debug( LDAP_DEBUG_ANY,
226 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
229 free( value.bv_val );
235 free( value.bv_val );
238 /* check to make sure there was a dn: line */
239 if ( e->e_dn == NULL ) {
241 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
242 "str2entry: entry %ld has no dn.\n",
245 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
246 (long) e->e_id, 0, 0 );
252 /* generate normalized dn */
253 e->e_ndn = ch_strdup( e->e_dn );
254 (void) dn_normalize( e->e_ndn );
257 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
258 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
260 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
261 e->e_dn, (unsigned long) e, 0 );
267 #define GRABSIZE BUFSIZ
269 #define MAKE_SPACE( n ) { \
270 while ( ecur + (n) > ebuf + emaxsize ) { \
272 offset = (int) (ecur - ebuf); \
273 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
274 emaxsize + GRABSIZE ); \
275 emaxsize += GRABSIZE; \
276 ecur = ebuf + offset; \
290 * In string format, an entry looks like this:
292 * [<attr>: <value>\n]*
298 if ( e->e_dn != NULL ) {
300 tmplen = strlen( e->e_dn );
301 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
302 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
305 /* put the attributes */
306 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
307 /* put "<type>:[:] <value>" line for each value */
308 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
310 tmplen = a->a_desc->ad_cname->bv_len;
311 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
312 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
313 a->a_desc->ad_cname->bv_val,
314 bv->bv_val, bv->bv_len );
321 return( (char *) ebuf );
325 entry_free( Entry *e )
327 /* free an entry structure */
330 /* e_private must be freed by the caller */
331 assert( e->e_private == NULL );
335 if ( e->e_dn != NULL ) {
339 if ( e->e_ndn != NULL ) {
344 /* free attributes */
345 attrs_free( e->e_attrs );
352 * These routines are used only by Backend.
354 * the Entry has three entry points (ways to find things):
356 * by entry e.g., if you already have an entry from the cache
357 * and want to delete it. (really by entry ptr)
358 * by dn e.g., when looking for the base object of a search
359 * by id e.g., for search candidates
361 * these correspond to three different avl trees that are maintained.
365 entry_cmp( Entry *e1, Entry *e2 )
367 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
371 entry_dn_cmp( Entry *e1, Entry *e2 )
373 /* compare their normalized UPPERCASED dn's */
374 return( strcmp( e1->e_ndn, e2->e_ndn ) );
378 entry_id_cmp( Entry *e1, Entry *e2 )
380 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
385 /* a LBER encoded entry looks like:
387 * entry :== SEQUENCE {
388 * dn DistinguishedName,
389 * ndn NormalizedDistinguishedName,
390 * attrs SEQUENCE OF SEQUENCE {
391 * type AttributeType,
392 * values SET OF AttributeValue
396 * Encoding/Decoding of LBER should be much faster than LDIF
399 int entry_decode( struct berval *bv, Entry **entry )
408 assert( bv != NULL );
409 assert( entry != NULL );
411 ber = ber_init( bv );
413 assert( 0 ); /* XXYYZ: Temporary assert */
416 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
417 "entry_decode: ber_init failed\n" ));
419 Debug( LDAP_DEBUG_ANY,
420 "<= entry_decode: ber_init failed\n",
423 return LDAP_LOCAL_ERROR;
426 /* initialize reader/writer lock */
427 e = (Entry *) ch_malloc( sizeof(Entry) );
431 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
432 "entry_decode: entry allocation failed.\n" ));
434 Debug( LDAP_DEBUG_ANY,
435 "<= entry_decode: entry allocation failed\n",
438 return LDAP_LOCAL_ERROR;
441 /* initialize entry */
448 tag = ber_scanf( ber, "{aa" /*"}"*/, &e->e_dn, &e->e_ndn );
449 if( tag == LBER_ERROR ) {
451 return LDAP_PROTOCOL_ERROR;
455 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
456 "entry_decode: \"%s\"\n", e->e_dn ));
458 Debug( LDAP_DEBUG_TRACE,
459 "entry_decode: \"%s\"\n",
463 for ( tag = ber_first_element( ber, &len, &last );
465 tag = ber_next_element( ber, &len, last ) )
468 struct berval **vals;
469 AttributeDescription *ad;
472 tag = ber_scanf( ber, "{O{V}}", &type, &vals );
474 if ( tag == LBER_ERROR ) {
476 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
477 "entry_decode: decoding error (%s)\n", e->e_dn ));
479 Debug( LDAP_DEBUG_ANY, "entry_decode: decoding error\n", 0, 0, 0 );
482 return LDAP_PROTOCOL_ERROR;
485 if ( vals == NULL ) {
487 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
488 "entry_decode: no values for type %s\n", type ));
490 Debug( LDAP_DEBUG_ANY, "entry_decode: no values for type %s\n",
495 return LDAP_PROTOCOL_ERROR;
499 rc = slap_bv2ad( type, &ad, &text );
501 if( rc != LDAP_SUCCESS ) {
503 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
504 "entry_decode: str2ad(%s): %s\n", type, text ));
506 Debug( LDAP_DEBUG_TRACE,
507 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
509 rc = slap_bv2undef_ad( type, &ad, &text );
511 if( rc != LDAP_SUCCESS ) {
513 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
514 "entry_decode: str2undef_ad(%s): %s\n", type, text));
516 Debug( LDAP_DEBUG_ANY,
517 "<= entry_decode: str2undef_ad(%s): %s\n",
521 ber_bvecfree( vals );
527 rc = attr_merge( e, ad, vals );
532 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
533 "entry_decode: attr_merge failed\n"));
535 Debug( LDAP_DEBUG_ANY,
536 "<= entry_decode: attr_merge failed\n", 0, 0, 0 );
539 ber_bvecfree( vals );
541 return LDAP_LOCAL_ERROR;
545 ber_bvecfree( vals );
548 rc = ber_scanf( ber, /*"{"*/ "}" );
551 return LDAP_PROTOCOL_ERROR;
555 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
556 "entry_decode: %s\n", e->e_dn ));
558 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
575 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
576 "entry_encode: id: 0x%08lx \"%s\"\n",
577 (long) e->e_id, e->e_dn ));
579 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
580 (long) e->e_id, e->e_dn, 0 );
582 ber = ber_alloc_t( LBER_USE_DER );
587 rc = ber_printf( ber, "{ss{" /*"}}"*/, e->e_dn, e->e_ndn );
592 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
593 rc = ber_printf( ber, "{O{V}}",
601 rc = ber_printf( ber, /*"{{"*/ "}}" );
606 rc = ber_flatten( ber, bv );
612 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
613 "entry_encode: id=0x%08lx failed (%d)\n",
614 (long) e->e_id, rc ));
616 Debug( LDAP_DEBUG_ANY, "=> entry_encode(0x%08lx): failed (%d)\n",
617 (long) e->e_id, rc, 0 );