]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Fixup bdb_entry_release now that entry_decode uses two memory blocks
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16
17 #include "slap.h"
18 #include "ldif.h"
19
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                     */
23
24 /*
25  * Empty root entry
26  */
27 const Entry slap_entry_root = { NOID, "", "", NULL, NULL };
28
29 int entry_destroy(void)
30 {
31         free( ebuf );
32         ebuf = NULL;
33         ecur = NULL;
34         emaxsize = 0;
35         return 0;
36 }
37
38
39 Entry *
40 str2entry( char *s )
41 {
42         int rc;
43         Entry           *e;
44         char            *type;
45         struct berval value;
46         struct berval   *vals[2];
47         AttributeDescription *ad;
48         const char *text;
49         char    *next;
50
51         /*
52          * LDIF is used as the string format.
53          * An entry looks like this:
54          *
55          *      dn: <dn>\n
56          *      [<attr>:[:] <value>\n]
57          *      [<tab><continuedvalue>\n]*
58          *      ...
59          *
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
63          * or newline.
64          */
65
66 #ifdef NEW_LOGGING
67         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
68                    "str2entry: \"%s\"\n", s ? s : "NULL" ));
69 #else
70         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
71                 s ? s : "NULL", 0, 0 );
72 #endif
73
74         /* initialize reader/writer lock */
75         e = (Entry *) ch_malloc( sizeof(Entry) );
76
77         if( e == NULL ) {
78 #ifdef NEW_LOGGING
79                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
80                            "str2entry: entry allocation failed.\n" ));
81 #else
82                 Debug( LDAP_DEBUG_ANY,
83                     "<= str2entry NULL (entry allocation failed)\n",
84                     0, 0, 0 );
85 #endif
86                 return( NULL );
87         }
88
89         /* initialize entry */
90         e->e_id = NOID;
91         e->e_dn = NULL;
92         e->e_ndn = NULL;
93         e->e_attrs = NULL;
94         e->e_private = NULL;
95
96         /* dn + attributes */
97         vals[0] = &value;
98         vals[1] = NULL;
99
100         next = s;
101         while ( (s = ldif_getline( &next )) != NULL ) {
102                 if ( *s == '\n' || *s == '\0' ) {
103                         break;
104                 }
105
106                 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
107 #ifdef NEW_LOGGING
108                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
109                                    "str2entry:  NULL (parse_line)\n" ));
110 #else
111                         Debug( LDAP_DEBUG_TRACE,
112                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
113 #endif
114                         continue;
115                 }
116
117                 if ( strcasecmp( type, "dn" ) == 0 ) {
118                         free( type );
119
120                         if ( e->e_dn != NULL ) {
121 #ifdef NEW_LOGGING
122                                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
123                                         "entry %ld has multiple dns \"%s\" and \"%s\" "
124                                         "(second ignored)\n",
125                                         (long) e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
126 #else
127                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
128                                         "entry %ld has multiple dns \"%s\" and \"%s\" "
129                                         "(second ignored)\n",
130                                     (long) e->e_id, e->e_dn,
131                                         value.bv_val != NULL ? value.bv_val : "" );
132 #endif
133                                 if( value.bv_val != NULL ) free( value.bv_val );
134                                 continue;
135                         }
136
137                         e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
138                         continue;
139                 }
140
141                 ad = NULL;
142                 rc = slap_str2ad( type, &ad, &text );
143
144                 if( rc != LDAP_SUCCESS ) {
145 #ifdef NEW_LOGGING
146                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
147                                    "str2entry:  str2ad(%s):      %s\n", type, text ));
148 #else
149                         Debug( slapMode & SLAP_TOOL_MODE
150                                 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
151                                 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
152 #endif
153                         if( slapMode & SLAP_TOOL_MODE ) {
154                                 entry_free( e );
155                                 free( value.bv_val );
156                                 free( type );
157                                 return NULL;
158                         }
159
160                         rc = slap_str2undef_ad( type, &ad, &text );
161
162                         if( rc != LDAP_SUCCESS ) {
163 #ifdef NEW_LOGGING
164                                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
165                                            "str2entry:  str2undef_ad(%s):  %s\n", type, text ));
166 #else
167                                 Debug( LDAP_DEBUG_ANY,
168                                         "<= str2entry: str2undef_ad(%s): %s\n",
169                                                 type, text, 0 );
170 #endif
171                                 entry_free( e );
172                                 free( value.bv_val );
173                                 free( type );
174                                 return NULL;
175                         }
176                 }
177
178                 if( slapMode & SLAP_TOOL_MODE ) {
179                         slap_syntax_validate_func *validate =
180                                 ad->ad_type->sat_syntax->ssyn_validate;
181
182                         if( !validate ) {
183 #ifdef NEW_LOGGING
184                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
185                                            "str2entry: no validator for syntax %s\n", 
186                                            ad->ad_type->sat_syntax->ssyn_oid ));
187 #else
188                                 Debug( LDAP_DEBUG_ANY,
189                                         "str2entry: no validator for syntax %s\n",
190                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
191 #endif
192                                 entry_free( e );
193                                 free( value.bv_val );
194                                 free( type );
195                                 return NULL;
196                         }
197
198                         /*
199                          * validate value per syntax
200                          */
201                         rc = validate( ad->ad_type->sat_syntax, &value );
202
203                         if( rc != 0 ) {
204 #ifdef NEW_LOGGING
205                                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
206                                            "str2entry:  invalid value for syntax %s\n",
207                                            ad->ad_type->sat_syntax->ssyn_oid ));
208 #else
209                                 Debug( LDAP_DEBUG_ANY,
210                                         "str2entry: invalid value for syntax %s\n",
211                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
212 #endif
213                                 entry_free( e );
214                                 free( value.bv_val );
215                                 free( type );
216                                 return NULL;
217                         }
218                 }
219
220                 rc = attr_merge( e, ad, vals );
221
222                 if( rc != 0 ) {
223 #ifdef NEW_LOGGING
224                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
225                                    "str2entry:  NULL (attr_merge)\n" ));
226 #else
227                         Debug( LDAP_DEBUG_ANY,
228                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
229 #endif
230                         entry_free( e );
231                         free( value.bv_val );
232                         free( type );
233                         return( NULL );
234                 }
235
236                 free( type );
237                 free( value.bv_val );
238         }
239
240         /* check to make sure there was a dn: line */
241         if ( e->e_dn == NULL ) {
242 #ifdef NEW_LOGGING
243                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
244                            "str2entry:  entry %ld has no dn.\n",
245                                 (long) e->e_id ));
246 #else
247                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
248                     (long) e->e_id, 0, 0 );
249 #endif
250                 entry_free( e );
251                 return( NULL );
252         }
253
254         /* generate normalized dn */
255         e->e_ndn = ch_strdup( e->e_dn );
256         (void) dn_normalize( e->e_ndn );
257
258 #ifdef NEW_LOGGING
259         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
260                    "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
261 #else
262         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
263                 e->e_dn, (unsigned long) e, 0 );
264 #endif
265         return( e );
266 }
267
268
269 #define GRABSIZE        BUFSIZ
270
271 #define MAKE_SPACE( n ) { \
272                 while ( ecur + (n) > ebuf + emaxsize ) { \
273                         ptrdiff_t       offset; \
274                         offset = (int) (ecur - ebuf); \
275                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
276                             emaxsize + GRABSIZE ); \
277                         emaxsize += GRABSIZE; \
278                         ecur = ebuf + offset; \
279                 } \
280         }
281
282 char *
283 entry2str(
284     Entry       *e,
285     int         *len )
286 {
287         Attribute       *a;
288         struct berval   *bv;
289         int             i, tmplen;
290
291         /*
292          * In string format, an entry looks like this:
293          *      dn: <dn>\n
294          *      [<attr>: <value>\n]*
295          */
296
297         ecur = ebuf;
298
299         /* put the dn */
300         if ( e->e_dn != NULL ) {
301                 /* put "dn: <dn>" */
302                 tmplen = strlen( e->e_dn );
303                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
304                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
305         }
306
307         /* put the attributes */
308         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
309                 /* put "<type>:[:] <value>" line for each value */
310                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
311                         bv = a->a_vals[i];
312                         tmplen = a->a_desc->ad_cname.bv_len;
313                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
314                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
315                                 a->a_desc->ad_cname.bv_val,
316                             bv->bv_val, bv->bv_len );
317                 }
318         }
319         MAKE_SPACE( 1 );
320         *ecur = '\0';
321         *len = ecur - ebuf;
322
323         return( (char *) ebuf );
324 }
325
326 void
327 entry_free( Entry *e )
328 {
329         /* free an entry structure */
330         assert( e != NULL );
331
332         /* e_private must be freed by the caller */
333         assert( e->e_private == NULL );
334         e->e_private = NULL;
335
336         /* free DNs */
337         if ( e->e_dn != NULL ) {
338                 free( e->e_dn );
339                 e->e_dn = NULL;
340         }
341         if ( e->e_ndn != NULL ) {
342                 free( e->e_ndn );
343                 e->e_ndn = NULL;
344         }
345
346         /* free attributes */
347         attrs_free( e->e_attrs );
348         e->e_attrs = NULL;
349
350         free( e );
351 }
352
353 /*
354  * These routines are used only by Backend.
355  *
356  * the Entry has three entry points (ways to find things):
357  *
358  *      by entry        e.g., if you already have an entry from the cache
359  *                      and want to delete it. (really by entry ptr)
360  *      by dn           e.g., when looking for the base object of a search
361  *      by id           e.g., for search candidates
362  *
363  * these correspond to three different avl trees that are maintained.
364  */
365
366 int
367 entry_cmp( Entry *e1, Entry *e2 )
368 {
369         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
370 }
371
372 int
373 entry_dn_cmp( Entry *e1, Entry *e2 )
374 {
375         /* compare their normalized UPPERCASED dn's */
376         return( strcmp( e1->e_ndn, e2->e_ndn ) );
377 }
378
379 int
380 entry_id_cmp( Entry *e1, Entry *e2 )
381 {
382         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
383 }
384
385 #ifdef SLAPD_BDB
386
387 /* This is like a ber_len */
388 static ber_len_t
389 entry_lenlen(ber_len_t len)
390 {
391         if (len <= 0x7f)
392                 return 1;
393         if (len <= 0xff)
394                 return 2;
395         if (len <= 0xffff)
396                 return 3;
397         if (len <= 0xffffff)
398                 return 4;
399         return 5;
400 }
401
402 static void
403 entry_putlen(unsigned char **buf, ber_len_t len)
404 {
405         ber_len_t lenlen = entry_lenlen(len);
406
407         if (lenlen == 1) {
408                 **buf = (unsigned char) len;
409         } else {
410                 int i;
411                 **buf = 0x80 | (lenlen - 1);
412                 for (i=lenlen-1; i>0; i--) {
413                         (*buf)[i] = (unsigned char) len;
414                         len >>= 8;
415                 }
416         }
417         *buf += lenlen;
418 }
419
420 static ber_len_t
421 entry_getlen(unsigned char **buf)
422 {
423         ber_len_t len;
424         int i;
425
426         len = *(*buf)++;
427         if (len <= 0x7f)
428                 return len;
429         i = len & 0x7f;
430         len = 0;
431         for (;i > 0; i--) {
432                 len <<= 8;
433                 len |= *(*buf)++;
434         }
435         return len;
436 }
437
438 /* Flatten an Entry into a buffer. The buffer is filled with just the
439  * strings/bervals of all the entry components. Each field is preceded
440  * by its length, encoded the way ber_put_len works. Every field is NUL
441  * terminated.  The entire buffer size is precomputed so that a single
442  * malloc can be performed. The entry size is also recorded,
443  * to aid in entry_decode.
444  */
445 int entry_encode(Entry *e, struct berval *bv)
446 {
447         int siz = sizeof(Entry);
448         int len, dnlen, ndnlen;
449         int i;
450         Attribute *a;
451         unsigned char *ptr;
452
453 #ifdef NEW_LOGGING
454         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
455                 "entry_encode: id: 0x%08lx  \"%s\"\n",
456                 (long) e->e_id, e->e_dn ));
457 #else
458         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
459                 (long) e->e_id, e->e_dn, 0 );
460 #endif
461         dnlen = strlen(e->e_dn);
462         ndnlen = strlen(e->e_ndn);
463         len = dnlen + ndnlen + 2;       /* two trailing NUL bytes */
464         len += entry_lenlen(dnlen);
465         len += entry_lenlen(ndnlen);
466         for (a=e->e_attrs; a; a=a->a_next) {
467                 /* For AttributeDesc, we only store the attr name */
468                 siz += sizeof(Attribute);
469                 len += a->a_desc->ad_cname.bv_len+1;
470                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
471                 for (i=0; a->a_vals[i]; i++) {
472                         siz += sizeof(struct berval *);
473                         siz += sizeof(struct berval);
474                         len += a->a_vals[i]->bv_len + 1;
475                         len += entry_lenlen(a->a_vals[i]->bv_len);
476                 }
477                 len += entry_lenlen(i);
478                 siz += sizeof(struct berval *); /* NULL pointer at end */
479         }
480         len += 1;       /* NUL byte at end */
481         len += entry_lenlen(siz);
482         bv->bv_len = len;
483         bv->bv_val = ch_malloc(len);
484         ptr = (unsigned char *)bv->bv_val;
485         entry_putlen(&ptr, siz);
486         entry_putlen(&ptr, dnlen);
487         memcpy(ptr, e->e_dn, dnlen);
488         ptr += dnlen;
489         *ptr++ = '\0';
490         entry_putlen(&ptr, ndnlen);
491         memcpy(ptr, e->e_ndn, ndnlen);
492         ptr += ndnlen;
493         *ptr++ = '\0';
494
495         for (a=e->e_attrs; a; a=a->a_next) {
496                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
497                 memcpy(ptr, a->a_desc->ad_cname.bv_val,
498                         a->a_desc->ad_cname.bv_len);
499                 ptr += a->a_desc->ad_cname.bv_len;
500                 *ptr++ = '\0';
501                 if (a->a_vals) {
502                     for (i=0; a->a_vals[i]; i++);
503                     entry_putlen(&ptr, i);
504                     for (i=0; a->a_vals[i]; i++) {
505                         entry_putlen(&ptr, a->a_vals[i]->bv_len);
506                         memcpy(ptr, a->a_vals[i]->bv_val,
507                                 a->a_vals[i]->bv_len);
508                         ptr += a->a_vals[i]->bv_len;
509                         *ptr++ = '\0';
510                     }
511                 }
512         }
513         *ptr = '\0';
514         return 0;
515 }
516
517 /* Retrieve an Entry that was stored using entry_encode above.
518  * We malloc a single block with the size stored above for the Entry
519  * and all if its Attributes. We also must lookup the stored
520  * attribute names to get AttributeDescriptions. To detect if the
521  * attributes of an Entry are later modified, we note that e->e_attr
522  * is always a constant offset from (e).
523  *
524  * Note: everything is stored in a single contiguous block, so
525  * you can not free individual attributes or names from this
526  * structure. Attempting to do so will likely corrupt memory.
527  */
528 int entry_decode(struct berval *bv, Entry **e)
529 {
530         int i, j;
531         int rc;
532         Attribute *a;
533         Entry *x;
534         const char *text;
535         AttributeDescription *ad;
536         unsigned char *ptr = (unsigned char *)bv->bv_val;
537         struct berval **bptr;
538         struct berval *vptr;
539
540         i = entry_getlen(&ptr);
541         x = ch_malloc(i);
542         i = entry_getlen(&ptr);
543         x->e_dn = ptr;
544         ptr += i+1;
545         i = entry_getlen(&ptr);
546         x->e_ndn = ptr;
547         ptr += i+1;
548 #ifdef NEW_LOGGING
549         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
550                    "entry_decode: \"%s\"\n", x->e_dn ));
551 #else
552         Debug( LDAP_DEBUG_TRACE,
553             "entry_decode: \"%s\"\n",
554             x->e_dn, 0, 0 );
555 #endif
556         x->e_private = bv->bv_val;
557
558         /* A valid entry must have at least one attr, so this
559          * pointer can never be NULL
560          */
561         x->e_attrs = (Attribute *)(x+1);
562         bptr = (struct berval **)x->e_attrs;
563         a = NULL;
564
565         while (i = entry_getlen(&ptr)) {
566                 if (a) {
567                         a->a_next = (Attribute *)bptr;
568                 }
569                 a = (Attribute *)bptr;
570                 ad = NULL;
571                 rc = slap_str2ad( ptr, &ad, &text );
572
573                 if( rc != LDAP_SUCCESS ) {
574 #ifdef NEW_LOGGING
575                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
576                                    "entry_decode: str2ad(%s): %s\n", ptr, text ));
577 #else
578                         Debug( LDAP_DEBUG_TRACE,
579                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
580 #endif
581                         rc = slap_str2undef_ad( ptr, &ad, &text );
582
583                         if( rc != LDAP_SUCCESS ) {
584 #ifdef NEW_LOGGING
585                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
586                                            "entry_decode:  str2undef_ad(%s): %s\n", ptr, text));
587 #else
588                                 Debug( LDAP_DEBUG_ANY,
589                                         "<= entry_decode: str2undef_ad(%s): %s\n",
590                                                 ptr, text, 0 );
591 #endif
592                                 return rc;
593                         }
594                 }
595                 ptr += i + 1;
596                 a->a_desc = ad;
597                 bptr = (struct berval **)(a+1);
598                 a->a_vals = bptr;
599                 j = entry_getlen(&ptr);
600                 a->a_vals[j] = NULL;
601                 vptr = (struct berval *)(bptr + j + 1);
602
603                 while (j) {
604                         i = entry_getlen(&ptr);
605                         *bptr = vptr;
606                         vptr->bv_len = i;
607                         vptr->bv_val = (char *)ptr;
608                         ptr += i+1;
609                         bptr++;
610                         vptr++;
611                         j--;
612                 }
613                 bptr = (struct berval **)vptr;
614         }
615         if (a)
616                 a->a_next = NULL;
617 #ifdef NEW_LOGGING
618         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
619                    "entry_decode:  %s\n", x->e_dn ));
620 #else
621         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
622                 x->e_dn, 0, 0 );
623 #endif
624         *e = x;
625         return 0;
626 }
627 #endif