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 */
27 const Entry slap_entry_root = { NOID, { 0, "" }, { 0, "" }, NULL, NULL };
29 int entry_destroy(void)
46 struct berval *vals[2];
47 AttributeDescription *ad;
52 * LDIF is used as the string format.
53 * An entry looks like this:
56 * [<attr>:[:] <value>\n]
57 * [<tab><continuedvalue>\n]*
60 * If a double colon is used after a type, it means the
61 * following value is encoded as a base 64 string. This
62 * happens if the value contains a non-printing character
67 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
68 "str2entry: \"%s\"\n", s ? s : "NULL" ));
70 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
71 s ? s : "NULL", 0, 0 );
74 /* initialize reader/writer lock */
75 e = (Entry *) ch_malloc( sizeof(Entry) );
79 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
80 "str2entry: entry allocation failed.\n" ));
82 Debug( LDAP_DEBUG_ANY,
83 "<= str2entry NULL (entry allocation failed)\n",
89 /* initialize entry */
91 e->e_name.bv_val = NULL;
93 e->e_nname.bv_val = NULL;
94 e->e_nname.bv_len = 0;
103 while ( (s = ldif_getline( &next )) != NULL ) {
104 if ( *s == '\n' || *s == '\0' ) {
108 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
110 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
111 "str2entry: NULL (parse_line)\n" ));
113 Debug( LDAP_DEBUG_TRACE,
114 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
119 if ( strcasecmp( type, "dn" ) == 0 ) {
120 struct berval *pdn = NULL;
124 if ( e->e_dn != NULL ) {
126 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
127 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
128 (long) e->e_id, e->e_dn,
129 value.bv_val != NULL ? value.bv_val : "" ));
131 Debug( LDAP_DEBUG_ANY, "str2entry: "
132 "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
133 (long) e->e_id, e->e_dn,
134 value.bv_val != NULL ? value.bv_val : "" );
136 if( value.bv_val != NULL ) free( value.bv_val );
141 rc = dnPretty( NULL, &value, &pdn );
142 if( rc != LDAP_SUCCESS ) {
144 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
145 "entry %ld has invalid DN \"%s\"\n",
147 pdn->bv_val ? pdn->bv_val : "" ));
149 Debug( LDAP_DEBUG_ANY, "str2entry: "
150 "entry %ld has invalid DN \"%s\"\n",
152 pdn->bv_val ? pdn->bv_val : "", 0 );
154 if( value.bv_val != NULL ) free( value.bv_val );
159 e->e_name.bv_val = pdn->bv_val != NULL
160 ? pdn->bv_val : ch_strdup( "" );
161 e->e_name.bv_len = pdn->bv_len;
167 rc = slap_str2ad( type, &ad, &text );
169 if( rc != LDAP_SUCCESS ) {
171 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
172 "str2entry: str2ad(%s): %s\n", type, text ));
174 Debug( slapMode & SLAP_TOOL_MODE
175 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
176 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
178 if( slapMode & SLAP_TOOL_MODE ) {
180 free( value.bv_val );
185 rc = slap_str2undef_ad( type, &ad, &text );
186 if( rc != LDAP_SUCCESS ) {
188 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
189 "str2entry: str2undef_ad(%s): %s\n", type, text ));
191 Debug( LDAP_DEBUG_ANY,
192 "<= str2entry: str2undef_ad(%s): %s\n",
196 free( value.bv_val );
202 if( slapMode & SLAP_TOOL_MODE ) {
203 struct berval *pval = NULL;
204 slap_syntax_validate_func *validate =
205 ad->ad_type->sat_syntax->ssyn_validate;
206 slap_syntax_transform_func *pretty =
207 ad->ad_type->sat_syntax->ssyn_pretty;
210 rc = pretty( ad->ad_type->sat_syntax,
213 } else if( validate ) {
215 * validate value per syntax
217 rc = validate( ad->ad_type->sat_syntax, &value );
221 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
222 "str2entry: no validator for syntax %s\n",
223 ad->ad_type->sat_syntax->ssyn_oid ));
225 Debug( LDAP_DEBUG_ANY,
226 "str2entry: no validator for syntax %s\n",
227 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
230 free( value.bv_val );
237 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
238 "str2entry: invalid value for syntax %s\n",
239 ad->ad_type->sat_syntax->ssyn_oid ));
241 Debug( LDAP_DEBUG_ANY,
242 "str2entry: invalid value for syntax %s\n",
243 ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
246 free( value.bv_val );
252 free( value.bv_val );
258 rc = attr_merge( e, ad, vals );
261 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
262 "str2entry: NULL (attr_merge)\n" ));
264 Debug( LDAP_DEBUG_ANY,
265 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
268 free( value.bv_val );
274 free( value.bv_val );
277 /* check to make sure there was a dn: line */
278 if ( e->e_dn == NULL ) {
280 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
281 "str2entry: entry %ld has no dn.\n",
284 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
285 (long) e->e_id, 0, 0 );
291 /* generate normalized dn */
293 struct berval *ndn = NULL;
295 rc = dnNormalize( NULL, &e->e_name, &ndn );
296 if( rc != LDAP_SUCCESS ) {
298 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
299 "str2entry: entry %ld has invalid dn: %s\n",
300 (long) e->e_id, e->e_dn ));
302 Debug( LDAP_DEBUG_ANY,
303 "str2entry: entry %ld has invalid dn: %s\n",
304 (long) e->e_id, e->e_dn, 0 );
315 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
316 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
318 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
319 e->e_dn, (unsigned long) e, 0 );
325 #define GRABSIZE BUFSIZ
327 #define MAKE_SPACE( n ) { \
328 while ( ecur + (n) > ebuf + emaxsize ) { \
330 offset = (int) (ecur - ebuf); \
331 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
332 emaxsize + GRABSIZE ); \
333 emaxsize += GRABSIZE; \
334 ecur = ebuf + offset; \
349 * In string format, an entry looks like this:
351 * [<attr>: <value>\n]*
357 if ( e->e_dn != NULL ) {
359 tmplen = e->e_name.bv_len;
360 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
361 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
364 /* put the attributes */
365 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
366 /* put "<type>:[:] <value>" line for each value */
367 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
369 tmplen = a->a_desc->ad_cname.bv_len;
370 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
371 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
372 a->a_desc->ad_cname.bv_val,
373 bv->bv_val, bv->bv_len );
380 return( (char *) ebuf );
384 entry_free( Entry *e )
386 /* free an entry structure */
389 /* e_private must be freed by the caller */
390 assert( e->e_private == NULL );
394 if ( e->e_dn != NULL ) {
398 if ( e->e_ndn != NULL ) {
403 /* free attributes */
404 attrs_free( e->e_attrs );
411 * These routines are used only by Backend.
413 * the Entry has three entry points (ways to find things):
415 * by entry e.g., if you already have an entry from the cache
416 * and want to delete it. (really by entry ptr)
417 * by dn e.g., when looking for the base object of a search
418 * by id e.g., for search candidates
420 * these correspond to three different avl trees that are maintained.
424 entry_cmp( Entry *e1, Entry *e2 )
426 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
430 entry_dn_cmp( Entry *e1, Entry *e2 )
432 /* compare their normalized UPPERCASED dn's */
433 return( strcmp( e1->e_ndn, e2->e_ndn ) );
437 entry_id_cmp( Entry *e1, Entry *e2 )
439 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
444 /* This is like a ber_len */
446 entry_lenlen(ber_len_t len)
460 entry_putlen(unsigned char **buf, ber_len_t len)
462 ber_len_t lenlen = entry_lenlen(len);
465 **buf = (unsigned char) len;
468 **buf = 0x80 | ((unsigned char) lenlen - 1);
469 for (i=lenlen-1; i>0; i--) {
470 (*buf)[i] = (unsigned char) len;
478 entry_getlen(unsigned char **buf)
495 /* Flatten an Entry into a buffer. The buffer is filled with just the
496 * strings/bervals of all the entry components. Each field is preceded
497 * by its length, encoded the way ber_put_len works. Every field is NUL
498 * terminated. The entire buffer size is precomputed so that a single
499 * malloc can be performed. The entry size is also recorded,
500 * to aid in entry_decode.
502 int entry_encode(Entry *e, struct berval *bv)
504 ber_len_t siz = sizeof(Entry);
505 ber_len_t len, dnlen, ndnlen;
511 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
512 "entry_encode: id: 0x%08lx \"%s\"\n",
513 (long) e->e_id, e->e_dn ));
515 Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
516 (long) e->e_id, e->e_dn, 0 );
518 dnlen = e->e_name.bv_len;
519 ndnlen = e->e_nname.bv_len;
520 len = dnlen + ndnlen + 2; /* two trailing NUL bytes */
521 len += entry_lenlen(dnlen);
522 len += entry_lenlen(ndnlen);
523 for (a=e->e_attrs; a; a=a->a_next) {
524 /* For AttributeDesc, we only store the attr name */
525 siz += sizeof(Attribute);
526 len += a->a_desc->ad_cname.bv_len+1;
527 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
528 for (i=0; a->a_vals[i]; i++) {
529 siz += sizeof(struct berval *);
530 siz += sizeof(struct berval);
531 len += a->a_vals[i]->bv_len + 1;
532 len += entry_lenlen(a->a_vals[i]->bv_len);
534 len += entry_lenlen(i);
535 siz += sizeof(struct berval *); /* NULL pointer at end */
537 len += 1; /* NUL byte at end */
538 len += entry_lenlen(siz);
540 bv->bv_val = ch_malloc(len);
541 ptr = (unsigned char *)bv->bv_val;
542 entry_putlen(&ptr, siz);
543 entry_putlen(&ptr, dnlen);
544 memcpy(ptr, e->e_dn, dnlen);
547 entry_putlen(&ptr, ndnlen);
548 memcpy(ptr, e->e_ndn, ndnlen);
552 for (a=e->e_attrs; a; a=a->a_next) {
553 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
554 memcpy(ptr, a->a_desc->ad_cname.bv_val,
555 a->a_desc->ad_cname.bv_len);
556 ptr += a->a_desc->ad_cname.bv_len;
559 for (i=0; a->a_vals[i]; i++);
560 entry_putlen(&ptr, i);
561 for (i=0; a->a_vals[i]; i++) {
562 entry_putlen(&ptr, a->a_vals[i]->bv_len);
563 memcpy(ptr, a->a_vals[i]->bv_val,
564 a->a_vals[i]->bv_len);
565 ptr += a->a_vals[i]->bv_len;
574 /* Retrieve an Entry that was stored using entry_encode above.
575 * We malloc a single block with the size stored above for the Entry
576 * and all if its Attributes. We also must lookup the stored
577 * attribute names to get AttributeDescriptions. To detect if the
578 * attributes of an Entry are later modified, we note that e->e_attr
579 * is always a constant offset from (e).
581 * Note: everything is stored in a single contiguous block, so
582 * you can not free individual attributes or names from this
583 * structure. Attempting to do so will likely corrupt memory.
585 int entry_decode(struct berval *bv, Entry **e)
592 AttributeDescription *ad;
593 unsigned char *ptr = (unsigned char *)bv->bv_val;
594 struct berval **bptr;
597 i = entry_getlen(&ptr);
599 i = entry_getlen(&ptr);
600 x->e_name.bv_val = ptr;
601 x->e_name.bv_len = i;
603 i = entry_getlen(&ptr);
604 x->e_nname.bv_val = ptr;
605 x->e_nname.bv_len = i;
608 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
609 "entry_decode: \"%s\"\n", x->e_dn ));
611 Debug( LDAP_DEBUG_TRACE,
612 "entry_decode: \"%s\"\n",
615 x->e_private = bv->bv_val;
617 /* A valid entry must have at least one attr, so this
618 * pointer can never be NULL
620 x->e_attrs = (Attribute *)(x+1);
621 bptr = (struct berval **)x->e_attrs;
624 while (i = entry_getlen(&ptr)) {
626 a->a_next = (Attribute *)bptr;
628 a = (Attribute *)bptr;
630 rc = slap_str2ad( ptr, &ad, &text );
632 if( rc != LDAP_SUCCESS ) {
634 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
635 "entry_decode: str2ad(%s): %s\n", ptr, text ));
637 Debug( LDAP_DEBUG_TRACE,
638 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
640 rc = slap_str2undef_ad( ptr, &ad, &text );
642 if( rc != LDAP_SUCCESS ) {
644 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
645 "entry_decode: str2undef_ad(%s): %s\n", ptr, text));
647 Debug( LDAP_DEBUG_ANY,
648 "<= entry_decode: str2undef_ad(%s): %s\n",
656 bptr = (struct berval **)(a+1);
658 j = entry_getlen(&ptr);
660 vptr = (struct berval *)(bptr + j + 1);
663 i = entry_getlen(&ptr);
666 vptr->bv_val = (char *)ptr;
672 bptr = (struct berval **)vptr;
677 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
678 "entry_decode: %s\n", x->e_dn ));
680 Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",