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
61 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
62 s ? s : "NULL", 0, 0 );
64 /* initialize reader/writer lock */
65 e = (Entry *) ch_malloc( sizeof(Entry) );
68 Debug( LDAP_DEBUG_TRACE,
69 "<= str2entry NULL (entry allocation failed)\n",
74 /* initialize entry */
86 while ( (s = ldif_getline( &next )) != NULL ) {
87 if ( *s == '\n' || *s == '\0' ) {
91 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
92 Debug( LDAP_DEBUG_TRACE,
93 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
97 if ( strcasecmp( type, "dn" ) == 0 ) {
100 if ( e->e_dn != NULL ) {
101 Debug( LDAP_DEBUG_ANY,
102 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
104 value.bv_val != NULL ? value.bv_val : "" );
105 if( value.bv_val != NULL ) free( value.bv_val );
109 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
114 rc = slap_str2ad( type, &ad, &text );
116 if( rc != LDAP_SUCCESS ) {
117 Debug( LDAP_DEBUG_TRACE,
118 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
120 if( slapMode & SLAP_TOOL_MODE ) {
122 free( value.bv_val );
127 rc = slap_str2undef_ad( type, &ad, &text );
129 if( rc != LDAP_SUCCESS ) {
130 Debug( LDAP_DEBUG_TRACE,
131 "<= str2entry: str2undef_ad(%s): %s\n",
134 free( value.bv_val );
140 if( slapMode & SLAP_TOOL_MODE ) {
141 slap_syntax_validate_func *validate =
142 ad->ad_type->sat_syntax->ssyn_validate;
145 Debug( LDAP_DEBUG_ANY,
146 "str2entry: no validator for syntax %s\n",
147 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
149 free( value.bv_val );
155 * validate value per syntax
157 rc = validate( ad->ad_type->sat_syntax, &value );
160 Debug( LDAP_DEBUG_TRACE,
161 "str2entry: invalid value for syntax %s\n",
162 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
164 free( value.bv_val );
170 rc = attr_merge( e, ad, vals );
175 Debug( LDAP_DEBUG_TRACE,
176 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
178 free( value.bv_val );
184 free( value.bv_val );
187 /* check to make sure there was a dn: line */
188 if ( e->e_dn == NULL ) {
189 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
195 /* generate normalized dn */
196 e->e_ndn = ch_strdup( e->e_dn );
197 (void) dn_normalize( e->e_ndn );
199 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
200 e->e_dn, (unsigned long) e, 0 );
206 #define GRABSIZE BUFSIZ
208 #define MAKE_SPACE( n ) { \
209 while ( ecur + (n) > ebuf + emaxsize ) { \
211 offset = (int) (ecur - ebuf); \
212 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
213 emaxsize + GRABSIZE ); \
214 emaxsize += GRABSIZE; \
215 ecur = ebuf + offset; \
229 * In string format, an entry looks like this:
231 * [<attr>: <value>\n]*
237 if ( e->e_dn != NULL ) {
239 tmplen = strlen( e->e_dn );
240 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
241 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
244 /* put the attributes */
245 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
246 /* put "<type>:[:] <value>" line for each value */
247 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
249 tmplen = a->a_desc->ad_cname->bv_len;
250 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
251 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
252 a->a_desc->ad_cname->bv_val,
253 bv->bv_val, bv->bv_len );
260 return( (char *) ebuf );
264 entry_free( Entry *e )
268 if ( e->e_dn != NULL ) {
272 if ( e->e_ndn != NULL ) {
276 for ( a = e->e_attrs; a != NULL; a = next ) {
286 * These routines are used only by Backend.
288 * the Entry has three entry points (ways to find things):
290 * by entry e.g., if you already have an entry from the cache
291 * and want to delete it. (really by entry ptr)
292 * by dn e.g., when looking for the base object of a search
293 * by id e.g., for search candidates
295 * these correspond to three different avl trees that are maintained.
299 entry_cmp( Entry *e1, Entry *e2 )
301 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
305 entry_dn_cmp( Entry *e1, Entry *e2 )
307 /* compare their normalized UPPERCASED dn's */
308 return( strcmp( e1->e_ndn, e2->e_ndn ) );
312 entry_id_cmp( Entry *e1, Entry *e2 )
314 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
317 #define SLAPD_SLEEPY 1
320 /* a DER encoded entry looks like:
322 * entry :== SEQUENCE {
323 * dn DistinguishedName,
324 * ndn NormalizedDistinguishedName,
325 * attrs SEQUENCE OF SEQUENCE {
326 * type AttributeType,
327 * values SET OF AttributeValue
331 * Encoding/Decoding of DER should be much faster than LDIF
334 int entry_decode( struct berval *bv, Entry **entry )
343 ber = ber_init( bv );
345 Debug( LDAP_DEBUG_TRACE,
346 "<= entry_decode: ber_init failed\n",
348 return LDAP_LOCAL_ERROR;
351 /* initialize reader/writer lock */
352 e = (Entry *) ch_malloc( sizeof(Entry) );
355 Debug( LDAP_DEBUG_ANY,
356 "<= entry_decode: entry allocation failed\n",
358 return LDAP_LOCAL_ERROR;
361 /* initialize entry */
368 tag = ber_scanf( ber, "{aa" /*"}"*/, &e->e_dn, &e->e_ndn );
369 if( tag == LBER_ERROR ) {
371 return LDAP_PROTOCOL_ERROR;
374 Debug( LDAP_DEBUG_TRACE,
375 "entry_decode: \"%s\"\n",
379 for ( tag = ber_first_element( ber, &len, &last );
381 tag = ber_next_element( ber, &len, last ) )
384 struct berval **vals;
385 AttributeDescription *ad;
388 tag = ber_scanf( ber, "{O{V}}", &type, &vals );
390 if ( tag == LBER_ERROR ) {
391 Debug( LDAP_DEBUG_ANY, "entry_decode: decoding error\n", 0, 0, 0 );
393 return LDAP_PROTOCOL_ERROR;
396 if ( vals == NULL ) {
397 Debug( LDAP_DEBUG_ANY, "entry_decode: no values for type %s\n",
401 return LDAP_PROTOCOL_ERROR;
405 rc = slap_bv2ad( type, &ad, &text );
407 if( rc != LDAP_SUCCESS ) {
408 Debug( LDAP_DEBUG_TRACE,
409 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
411 rc = slap_bv2undef_ad( type, &ad, &text );
413 if( rc != LDAP_SUCCESS ) {
414 Debug( LDAP_DEBUG_TRACE,
415 "<= entry_decode: str2undef_ad(%s): %s\n",
418 ber_bvecfree( vals );
424 rc = attr_merge( e, ad, vals );
428 Debug( LDAP_DEBUG_TRACE,
429 "<= entry_decode: attr_merge failed\n", 0, 0, 0 );
431 ber_bvecfree( vals );
433 return LDAP_LOCAL_ERROR;
437 ber_bvecfree( vals );
440 rc = ber_scanf( ber, /*"{"*/ "}" );
443 return LDAP_PROTOCOL_ERROR;
446 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
461 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
462 e->e_id, e->e_dn, 0 );
464 ber = ber_alloc_t( LBER_USE_DER );
469 rc = ber_printf( ber, "{ss{" /*"}}"*/, e->e_dn, e->e_ndn );
474 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
475 rc = ber_printf( ber, "{O{V}}",
483 rc = ber_printf( ber, /*"{{"*/ "}}" );
488 rc = ber_flatten( ber, bv );
493 Debug( LDAP_DEBUG_ANY, "=> entry_encode(0x%08lx): failed (%d)\n",