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