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) );
380 #define SLAPD_SLEEPY 1
383 /* a DER encoded entry looks like:
385 * entry :== SEQUENCE {
386 * dn DistinguishedName,
387 * ndn NormalizedDistinguishedName,
388 * attrs SEQUENCE OF SEQUENCE {
389 * type AttributeType,
390 * values SET OF AttributeValue
394 * Encoding/Decoding of DER should be much faster than LDIF
397 int entry_decode( struct berval *bv, Entry **entry )
406 ber = ber_init( bv );
409 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
410 "entry_decode: ber_init failed\n" ));
412 Debug( LDAP_DEBUG_ANY,
413 "<= entry_decode: ber_init failed\n",
416 return LDAP_LOCAL_ERROR;
419 /* initialize reader/writer lock */
420 e = (Entry *) ch_malloc( sizeof(Entry) );
424 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
425 "entry_decode: entry allocation failed.\n" ));
427 Debug( LDAP_DEBUG_ANY,
428 "<= entry_decode: entry allocation failed\n",
431 return LDAP_LOCAL_ERROR;
434 /* initialize entry */
441 tag = ber_scanf( ber, "{aa" /*"}"*/, &e->e_dn, &e->e_ndn );
442 if( tag == LBER_ERROR ) {
444 return LDAP_PROTOCOL_ERROR;
448 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
449 "entry_decode: \"%s\"\n", e->e_dn ));
451 Debug( LDAP_DEBUG_TRACE,
452 "entry_decode: \"%s\"\n",
456 for ( tag = ber_first_element( ber, &len, &last );
458 tag = ber_next_element( ber, &len, last ) )
461 struct berval **vals;
462 AttributeDescription *ad;
465 tag = ber_scanf( ber, "{O{V}}", &type, &vals );
467 if ( tag == LBER_ERROR ) {
469 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
470 "entry_decode: decoding error (%s)\n", e->e_dn ));
472 Debug( LDAP_DEBUG_ANY, "entry_decode: decoding error\n", 0, 0, 0 );
475 return LDAP_PROTOCOL_ERROR;
478 if ( vals == NULL ) {
480 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
481 "entry_decode: no values for type %s\n", type ));
483 Debug( LDAP_DEBUG_ANY, "entry_decode: no values for type %s\n",
488 return LDAP_PROTOCOL_ERROR;
492 rc = slap_bv2ad( type, &ad, &text );
494 if( rc != LDAP_SUCCESS ) {
496 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
497 "entry_decode: str2ad(%s): %s\n", type, text ));
499 Debug( LDAP_DEBUG_TRACE,
500 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
502 rc = slap_bv2undef_ad( type, &ad, &text );
504 if( rc != LDAP_SUCCESS ) {
506 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
507 "entry_decode: str2undef_ad(%s): %s\n", type, text));
509 Debug( LDAP_DEBUG_ANY,
510 "<= entry_decode: str2undef_ad(%s): %s\n",
514 ber_bvecfree( vals );
520 rc = attr_merge( e, ad, vals );
525 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
526 "entry_decode: attr_merge failed\n"));
528 Debug( LDAP_DEBUG_ANY,
529 "<= entry_decode: attr_merge failed\n", 0, 0, 0 );
532 ber_bvecfree( vals );
534 return LDAP_LOCAL_ERROR;
538 ber_bvecfree( vals );
541 rc = ber_scanf( ber, /*"{"*/ "}" );
544 return LDAP_PROTOCOL_ERROR;
548 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
549 "entry_decode: %s\n", e->e_dn ));
551 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
568 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
569 "entry_encode: id: 0x%08lx \"%s\"\n",
572 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
573 e->e_id, e->e_dn, 0 );
575 ber = ber_alloc_t( LBER_USE_DER );
580 rc = ber_printf( ber, "{ss{" /*"}}"*/, e->e_dn, e->e_ndn );
585 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
586 rc = ber_printf( ber, "{O{V}}",
594 rc = ber_printf( ber, /*"{{"*/ "}}" );
599 rc = ber_flatten( ber, bv );
605 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
606 "entry_encode: id=0x%08lx failed (%d)\n", e->e_id, rc ));
608 Debug( LDAP_DEBUG_ANY, "=> entry_encode(0x%08lx): failed (%d)\n",