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>
19 static unsigned char *ebuf; /* buf returned by entry2str */
20 static unsigned char *ecur; /* pointer to end of currently used ebuf */
21 static int emaxsize;/* max size of ebuf */
23 int entry_destroy(void)
41 struct berval *vals[2];
42 AttributeDescription *ad;
47 * LDIF is used as the string format.
48 * An entry looks like this:
51 * [<attr>:[:] <value>\n]
52 * [<tab><continuedvalue>\n]*
55 * If a double colon is used after a type, it means the
56 * following value is encoded as a base 64 string. This
57 * happens if the value contains a non-printing character
62 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
63 "str2entry: \"%s\"\n", s ? s : "NULL" ));
65 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
66 s ? s : "NULL", 0, 0 );
69 /* initialize reader/writer lock */
70 e = (Entry *) ch_malloc( sizeof(Entry) );
74 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
75 "str2entry: entry allocation failed.\n" ));
77 Debug( LDAP_DEBUG_ANY,
78 "<= str2entry NULL (entry allocation failed)\n",
84 /* initialize entry */
96 while ( (s = ldif_getline( &next )) != NULL ) {
97 if ( *s == '\n' || *s == '\0' ) {
101 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
103 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
104 "str2entry: NULL (parse_line)\n" ));
106 Debug( LDAP_DEBUG_TRACE,
107 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
112 if ( strcasecmp( type, "dn" ) == 0 ) {
115 if ( e->e_dn != NULL ) {
117 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
118 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
119 e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
121 Debug( LDAP_DEBUG_ANY,
122 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
124 value.bv_val != NULL ? value.bv_val : "" );
126 if( value.bv_val != NULL ) free( value.bv_val );
130 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
135 rc = slap_str2ad( type, &ad, &text );
137 if( rc != LDAP_SUCCESS ) {
139 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
140 "str2entry: str2ad(%s): %s\n", type, text ));
142 Debug( slapMode & SLAP_TOOL_MODE
143 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
144 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
146 if( slapMode & SLAP_TOOL_MODE ) {
148 free( value.bv_val );
153 rc = slap_str2undef_ad( type, &ad, &text );
155 if( rc != LDAP_SUCCESS ) {
157 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
158 "str2entry: str2undef_ad(%s): %s\n", type, text ));
160 Debug( LDAP_DEBUG_ANY,
161 "<= str2entry: str2undef_ad(%s): %s\n",
165 free( value.bv_val );
171 if( slapMode & SLAP_TOOL_MODE ) {
172 slap_syntax_validate_func *validate =
173 ad->ad_type->sat_syntax->ssyn_validate;
177 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
178 "str2entry: no validator for syntax %s\n",
179 ad->ad_type->sat_syntax->ssyn_oid ));
181 Debug( LDAP_DEBUG_ANY,
182 "str2entry: no validator for syntax %s\n",
183 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
186 free( value.bv_val );
192 * validate value per syntax
194 rc = validate( ad->ad_type->sat_syntax, &value );
198 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
199 "str2entry: invalid value for syntax %s\n",
200 ad->ad_type->sat_syntax->ssyn_oid ));
202 Debug( LDAP_DEBUG_ANY,
203 "str2entry: invalid value for syntax %s\n",
204 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
207 free( value.bv_val );
213 rc = attr_merge( e, ad, vals );
219 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
220 "str2entry: NULL (attr_merge)\n" ));
222 Debug( LDAP_DEBUG_ANY,
223 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
226 free( value.bv_val );
232 free( value.bv_val );
235 /* check to make sure there was a dn: line */
236 if ( e->e_dn == NULL ) {
238 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
239 "str2entry: entry %ld has no dn.\n", e->e_id ));
241 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
248 /* generate normalized dn */
249 e->e_ndn = ch_strdup( e->e_dn );
250 (void) dn_normalize( e->e_ndn );
253 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
254 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
256 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
257 e->e_dn, (unsigned long) e, 0 );
263 #define GRABSIZE BUFSIZ
265 #define MAKE_SPACE( n ) { \
266 while ( ecur + (n) > ebuf + emaxsize ) { \
268 offset = (int) (ecur - ebuf); \
269 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
270 emaxsize + GRABSIZE ); \
271 emaxsize += GRABSIZE; \
272 ecur = ebuf + offset; \
286 * In string format, an entry looks like this:
288 * [<attr>: <value>\n]*
294 if ( e->e_dn != NULL ) {
296 tmplen = strlen( e->e_dn );
297 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
298 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
301 /* put the attributes */
302 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
303 /* put "<type>:[:] <value>" line for each value */
304 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
306 tmplen = a->a_desc->ad_cname->bv_len;
307 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
308 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
309 a->a_desc->ad_cname->bv_val,
310 bv->bv_val, bv->bv_len );
317 return( (char *) ebuf );
321 entry_free( Entry *e )
325 if ( e->e_dn != NULL ) {
329 if ( e->e_ndn != NULL ) {
333 for ( a = e->e_attrs; a != NULL; a = next ) {
343 * These routines are used only by Backend.
345 * the Entry has three entry points (ways to find things):
347 * by entry e.g., if you already have an entry from the cache
348 * and want to delete it. (really by entry ptr)
349 * by dn e.g., when looking for the base object of a search
350 * by id e.g., for search candidates
352 * these correspond to three different avl trees that are maintained.
356 entry_cmp( Entry *e1, Entry *e2 )
358 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
362 entry_dn_cmp( Entry *e1, Entry *e2 )
364 /* compare their normalized UPPERCASED dn's */
365 return( strcmp( e1->e_ndn, e2->e_ndn ) );
369 entry_id_cmp( Entry *e1, Entry *e2 )
371 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
374 #define SLAPD_SLEEPY 1
377 /* a DER encoded entry looks like:
379 * entry :== SEQUENCE {
380 * dn DistinguishedName,
381 * ndn NormalizedDistinguishedName,
382 * attrs SEQUENCE OF SEQUENCE {
383 * type AttributeType,
384 * values SET OF AttributeValue
388 * Encoding/Decoding of DER should be much faster than LDIF
391 int entry_decode( struct berval *bv, Entry **entry )
400 ber = ber_init( bv );
403 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
404 "entry_decode: ber_init failed\n" ));
406 Debug( LDAP_DEBUG_ANY,
407 "<= entry_decode: ber_init failed\n",
410 return LDAP_LOCAL_ERROR;
413 /* initialize reader/writer lock */
414 e = (Entry *) ch_malloc( sizeof(Entry) );
418 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
419 "entry_decode: entry allocation failed.\n" ));
421 Debug( LDAP_DEBUG_ANY,
422 "<= entry_decode: entry allocation failed\n",
425 return LDAP_LOCAL_ERROR;
428 /* initialize entry */
435 tag = ber_scanf( ber, "{aa" /*"}"*/, &e->e_dn, &e->e_ndn );
436 if( tag == LBER_ERROR ) {
438 return LDAP_PROTOCOL_ERROR;
442 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
443 "entry_decode: \"%s\"\n", e->e_dn ));
445 Debug( LDAP_DEBUG_TRACE,
446 "entry_decode: \"%s\"\n",
450 for ( tag = ber_first_element( ber, &len, &last );
452 tag = ber_next_element( ber, &len, last ) )
455 struct berval **vals;
456 AttributeDescription *ad;
459 tag = ber_scanf( ber, "{O{V}}", &type, &vals );
461 if ( tag == LBER_ERROR ) {
463 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
464 "entry_decode: decoding error (%s)\n", e->e_dn ));
466 Debug( LDAP_DEBUG_ANY, "entry_decode: decoding error\n", 0, 0, 0 );
469 return LDAP_PROTOCOL_ERROR;
472 if ( vals == NULL ) {
474 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
475 "entry_decode: no values for type %s\n", type ));
477 Debug( LDAP_DEBUG_ANY, "entry_decode: no values for type %s\n",
482 return LDAP_PROTOCOL_ERROR;
486 rc = slap_bv2ad( type, &ad, &text );
488 if( rc != LDAP_SUCCESS ) {
490 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
491 "entry_decode: str2ad(%s): %s\n", type, text ));
493 Debug( LDAP_DEBUG_TRACE,
494 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
496 rc = slap_bv2undef_ad( type, &ad, &text );
498 if( rc != LDAP_SUCCESS ) {
500 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
501 "entry_decode: str2undef_ad(%s): %s\n", type, text));
503 Debug( LDAP_DEBUG_ANY,
504 "<= entry_decode: str2undef_ad(%s): %s\n",
508 ber_bvecfree( vals );
514 rc = attr_merge( e, ad, vals );
519 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
520 "entry_decode: attr_merge failed\n"));
522 Debug( LDAP_DEBUG_ANY,
523 "<= entry_decode: attr_merge failed\n", 0, 0, 0 );
526 ber_bvecfree( vals );
528 return LDAP_LOCAL_ERROR;
532 ber_bvecfree( vals );
535 rc = ber_scanf( ber, /*"{"*/ "}" );
538 return LDAP_PROTOCOL_ERROR;
542 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
543 "entry_decode: %s\n", e->e_dn ));
545 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
562 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
563 "entry_encode: id: 0x%08lx \"%s\"\n",
566 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
567 e->e_id, e->e_dn, 0 );
569 ber = ber_alloc_t( LBER_USE_DER );
574 rc = ber_printf( ber, "{ss{" /*"}}"*/, e->e_dn, e->e_ndn );
579 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
580 rc = ber_printf( ber, "{O{V}}",
588 rc = ber_printf( ber, /*"{{"*/ "}}" );
593 rc = ber_flatten( ber, bv );
599 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
600 "entry_encode: id=0x%08lx failed (%d)\n", e->e_id, rc ));
602 Debug( LDAP_DEBUG_ANY, "=> entry_encode(0x%08lx): failed (%d)\n",