]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
temporary variable erroneously inside #ifdef (ITS#1503)
[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                         struct berval *pval;
180                         slap_syntax_validate_func *validate =
181                                 ad->ad_type->sat_syntax->ssyn_validate;
182                         slap_syntax_transform_func *pretty =
183                                 ad->ad_type->sat_syntax->ssyn_pretty;
184
185                         if( pretty ) {
186                                 rc = pretty( ad->ad_type->sat_syntax,
187                                         &value, &pval );
188
189                         } else if( validate ) {
190                                 /*
191                                  * validate value per syntax
192                                  */
193                                 rc = validate( ad->ad_type->sat_syntax, &value );
194
195                         } else {
196 #ifdef NEW_LOGGING
197                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
198                                            "str2entry: no validator for syntax %s\n", 
199                                            ad->ad_type->sat_syntax->ssyn_oid ));
200 #else
201                                 Debug( LDAP_DEBUG_ANY,
202                                         "str2entry: no validator for syntax %s\n",
203                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
204 #endif
205                                 entry_free( e );
206                                 free( value.bv_val );
207                                 free( type );
208                                 return NULL;
209                         }
210
211                         if( rc != 0 ) {
212 #ifdef NEW_LOGGING
213                                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
214                                            "str2entry:  invalid value for syntax %s\n",
215                                            ad->ad_type->sat_syntax->ssyn_oid ));
216 #else
217                                 Debug( LDAP_DEBUG_ANY,
218                                         "str2entry: invalid value for syntax %s\n",
219                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
220 #endif
221                                 entry_free( e );
222                                 free( value.bv_val );
223                                 free( type );
224                                 return NULL;
225                         }
226
227                         if( pretty ) {
228                                 free( value.bv_val );
229                                 value = *pval;
230                                 free( pval );
231                         }
232                 }
233
234                 rc = attr_merge( e, ad, vals );
235
236                 if( rc != 0 ) {
237 #ifdef NEW_LOGGING
238                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
239                                    "str2entry:  NULL (attr_merge)\n" ));
240 #else
241                         Debug( LDAP_DEBUG_ANY,
242                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
243 #endif
244                         entry_free( e );
245                         free( value.bv_val );
246                         free( type );
247                         return( NULL );
248                 }
249
250                 free( type );
251                 free( value.bv_val );
252         }
253
254         /* check to make sure there was a dn: line */
255         if ( e->e_dn == NULL ) {
256 #ifdef NEW_LOGGING
257                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
258                            "str2entry:  entry %ld has no dn.\n",
259                                 (long) e->e_id ));
260 #else
261                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
262                     (long) e->e_id, 0, 0 );
263 #endif
264                 entry_free( e );
265                 return( NULL );
266         }
267
268         /* generate normalized dn */
269         e->e_ndn = e->e_dn;
270         e->e_dn = dn_pretty( e->e_dn );
271
272         if( e->e_dn == NULL ) {
273 #ifdef NEW_LOGGING
274                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
275                            "str2entry:  entry %ld has invalid dn: %s\n",
276                                 (long) e->e_id, e->e_ndn ));
277 #else
278                 Debug( LDAP_DEBUG_ANY,
279                         "str2entry: entry %ld has invalid dn: %s\n",
280                     (long) e->e_id, e->e_ndn, 0 );
281 #endif
282                 entry_free( e );
283                 return( NULL );
284         }
285
286         (void) dn_normalize( e->e_ndn );
287
288 #ifdef NEW_LOGGING
289         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
290                 "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
291 #else
292         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
293                 e->e_dn, (unsigned long) e, 0 );
294 #endif
295         return( e );
296 }
297
298
299 #define GRABSIZE        BUFSIZ
300
301 #define MAKE_SPACE( n ) { \
302                 while ( ecur + (n) > ebuf + emaxsize ) { \
303                         ptrdiff_t       offset; \
304                         offset = (int) (ecur - ebuf); \
305                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
306                             emaxsize + GRABSIZE ); \
307                         emaxsize += GRABSIZE; \
308                         ecur = ebuf + offset; \
309                 } \
310         }
311
312 char *
313 entry2str(
314     Entry       *e,
315     int         *len )
316 {
317         Attribute       *a;
318         struct berval   *bv;
319         int             i, tmplen;
320
321         /*
322          * In string format, an entry looks like this:
323          *      dn: <dn>\n
324          *      [<attr>: <value>\n]*
325          */
326
327         ecur = ebuf;
328
329         /* put the dn */
330         if ( e->e_dn != NULL ) {
331                 /* put "dn: <dn>" */
332                 tmplen = strlen( e->e_dn );
333                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
334                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
335         }
336
337         /* put the attributes */
338         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
339                 /* put "<type>:[:] <value>" line for each value */
340                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
341                         bv = a->a_vals[i];
342                         tmplen = a->a_desc->ad_cname.bv_len;
343                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
344                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
345                                 a->a_desc->ad_cname.bv_val,
346                             bv->bv_val, bv->bv_len );
347                 }
348         }
349         MAKE_SPACE( 1 );
350         *ecur = '\0';
351         *len = ecur - ebuf;
352
353         return( (char *) ebuf );
354 }
355
356 void
357 entry_free( Entry *e )
358 {
359         /* free an entry structure */
360         assert( e != NULL );
361
362         /* e_private must be freed by the caller */
363         assert( e->e_private == NULL );
364         e->e_private = NULL;
365
366         /* free DNs */
367         if ( e->e_dn != NULL ) {
368                 free( e->e_dn );
369                 e->e_dn = NULL;
370         }
371         if ( e->e_ndn != NULL ) {
372                 free( e->e_ndn );
373                 e->e_ndn = NULL;
374         }
375
376         /* free attributes */
377         attrs_free( e->e_attrs );
378         e->e_attrs = NULL;
379
380         free( e );
381 }
382
383 /*
384  * These routines are used only by Backend.
385  *
386  * the Entry has three entry points (ways to find things):
387  *
388  *      by entry        e.g., if you already have an entry from the cache
389  *                      and want to delete it. (really by entry ptr)
390  *      by dn           e.g., when looking for the base object of a search
391  *      by id           e.g., for search candidates
392  *
393  * these correspond to three different avl trees that are maintained.
394  */
395
396 int
397 entry_cmp( Entry *e1, Entry *e2 )
398 {
399         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
400 }
401
402 int
403 entry_dn_cmp( Entry *e1, Entry *e2 )
404 {
405         /* compare their normalized UPPERCASED dn's */
406         return( strcmp( e1->e_ndn, e2->e_ndn ) );
407 }
408
409 int
410 entry_id_cmp( Entry *e1, Entry *e2 )
411 {
412         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
413 }
414
415 #ifdef SLAPD_BDB
416
417 /* This is like a ber_len */
418 static ber_len_t
419 entry_lenlen(ber_len_t len)
420 {
421         if (len <= 0x7f)
422                 return 1;
423         if (len <= 0xff)
424                 return 2;
425         if (len <= 0xffff)
426                 return 3;
427         if (len <= 0xffffff)
428                 return 4;
429         return 5;
430 }
431
432 static void
433 entry_putlen(unsigned char **buf, ber_len_t len)
434 {
435         ber_len_t lenlen = entry_lenlen(len);
436
437         if (lenlen == 1) {
438                 **buf = (unsigned char) len;
439         } else {
440                 int i;
441                 **buf = 0x80 | ((unsigned char) lenlen - 1);
442                 for (i=lenlen-1; i>0; i--) {
443                         (*buf)[i] = (unsigned char) len;
444                         len >>= 8;
445                 }
446         }
447         *buf += lenlen;
448 }
449
450 static ber_len_t
451 entry_getlen(unsigned char **buf)
452 {
453         ber_len_t len;
454         int i;
455
456         len = *(*buf)++;
457         if (len <= 0x7f)
458                 return len;
459         i = len & 0x7f;
460         len = 0;
461         for (;i > 0; i--) {
462                 len <<= 8;
463                 len |= *(*buf)++;
464         }
465         return len;
466 }
467
468 /* Flatten an Entry into a buffer. The buffer is filled with just the
469  * strings/bervals of all the entry components. Each field is preceded
470  * by its length, encoded the way ber_put_len works. Every field is NUL
471  * terminated.  The entire buffer size is precomputed so that a single
472  * malloc can be performed. The entry size is also recorded,
473  * to aid in entry_decode.
474  */
475 int entry_encode(Entry *e, struct berval *bv)
476 {
477         int siz = sizeof(Entry);
478         int len, dnlen, ndnlen;
479         int i;
480         Attribute *a;
481         unsigned char *ptr;
482
483 #ifdef NEW_LOGGING
484         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
485                 "entry_encode: id: 0x%08lx  \"%s\"\n",
486                 (long) e->e_id, e->e_dn ));
487 #else
488         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
489                 (long) e->e_id, e->e_dn, 0 );
490 #endif
491         dnlen = strlen(e->e_dn);
492         ndnlen = strlen(e->e_ndn);
493         len = dnlen + ndnlen + 2;       /* two trailing NUL bytes */
494         len += entry_lenlen(dnlen);
495         len += entry_lenlen(ndnlen);
496         for (a=e->e_attrs; a; a=a->a_next) {
497                 /* For AttributeDesc, we only store the attr name */
498                 siz += sizeof(Attribute);
499                 len += a->a_desc->ad_cname.bv_len+1;
500                 len += entry_lenlen(a->a_desc->ad_cname.bv_len);
501                 for (i=0; a->a_vals[i]; i++) {
502                         siz += sizeof(struct berval *);
503                         siz += sizeof(struct berval);
504                         len += a->a_vals[i]->bv_len + 1;
505                         len += entry_lenlen(a->a_vals[i]->bv_len);
506                 }
507                 len += entry_lenlen(i);
508                 siz += sizeof(struct berval *); /* NULL pointer at end */
509         }
510         len += 1;       /* NUL byte at end */
511         len += entry_lenlen(siz);
512         bv->bv_len = len;
513         bv->bv_val = ch_malloc(len);
514         ptr = (unsigned char *)bv->bv_val;
515         entry_putlen(&ptr, siz);
516         entry_putlen(&ptr, dnlen);
517         memcpy(ptr, e->e_dn, dnlen);
518         ptr += dnlen;
519         *ptr++ = '\0';
520         entry_putlen(&ptr, ndnlen);
521         memcpy(ptr, e->e_ndn, ndnlen);
522         ptr += ndnlen;
523         *ptr++ = '\0';
524
525         for (a=e->e_attrs; a; a=a->a_next) {
526                 entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
527                 memcpy(ptr, a->a_desc->ad_cname.bv_val,
528                         a->a_desc->ad_cname.bv_len);
529                 ptr += a->a_desc->ad_cname.bv_len;
530                 *ptr++ = '\0';
531                 if (a->a_vals) {
532                     for (i=0; a->a_vals[i]; i++);
533                     entry_putlen(&ptr, i);
534                     for (i=0; a->a_vals[i]; i++) {
535                         entry_putlen(&ptr, a->a_vals[i]->bv_len);
536                         memcpy(ptr, a->a_vals[i]->bv_val,
537                                 a->a_vals[i]->bv_len);
538                         ptr += a->a_vals[i]->bv_len;
539                         *ptr++ = '\0';
540                     }
541                 }
542         }
543         *ptr = '\0';
544         return 0;
545 }
546
547 /* Retrieve an Entry that was stored using entry_encode above.
548  * We malloc a single block with the size stored above for the Entry
549  * and all if its Attributes. We also must lookup the stored
550  * attribute names to get AttributeDescriptions. To detect if the
551  * attributes of an Entry are later modified, we note that e->e_attr
552  * is always a constant offset from (e).
553  *
554  * Note: everything is stored in a single contiguous block, so
555  * you can not free individual attributes or names from this
556  * structure. Attempting to do so will likely corrupt memory.
557  */
558 int entry_decode(struct berval *bv, Entry **e)
559 {
560         int i, j;
561         int rc;
562         Attribute *a;
563         Entry *x;
564         const char *text;
565         AttributeDescription *ad;
566         unsigned char *ptr = (unsigned char *)bv->bv_val;
567         struct berval **bptr;
568         struct berval *vptr;
569
570         i = entry_getlen(&ptr);
571         x = ch_malloc(i);
572         i = entry_getlen(&ptr);
573         x->e_dn = ptr;
574         ptr += i+1;
575         i = entry_getlen(&ptr);
576         x->e_ndn = ptr;
577         ptr += i+1;
578 #ifdef NEW_LOGGING
579         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
580                    "entry_decode: \"%s\"\n", x->e_dn ));
581 #else
582         Debug( LDAP_DEBUG_TRACE,
583             "entry_decode: \"%s\"\n",
584             x->e_dn, 0, 0 );
585 #endif
586         x->e_private = bv->bv_val;
587
588         /* A valid entry must have at least one attr, so this
589          * pointer can never be NULL
590          */
591         x->e_attrs = (Attribute *)(x+1);
592         bptr = (struct berval **)x->e_attrs;
593         a = NULL;
594
595         while (i = entry_getlen(&ptr)) {
596                 if (a) {
597                         a->a_next = (Attribute *)bptr;
598                 }
599                 a = (Attribute *)bptr;
600                 ad = NULL;
601                 rc = slap_str2ad( ptr, &ad, &text );
602
603                 if( rc != LDAP_SUCCESS ) {
604 #ifdef NEW_LOGGING
605                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
606                                    "entry_decode: str2ad(%s): %s\n", ptr, text ));
607 #else
608                         Debug( LDAP_DEBUG_TRACE,
609                                 "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
610 #endif
611                         rc = slap_str2undef_ad( ptr, &ad, &text );
612
613                         if( rc != LDAP_SUCCESS ) {
614 #ifdef NEW_LOGGING
615                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
616                                            "entry_decode:  str2undef_ad(%s): %s\n", ptr, text));
617 #else
618                                 Debug( LDAP_DEBUG_ANY,
619                                         "<= entry_decode: str2undef_ad(%s): %s\n",
620                                                 ptr, text, 0 );
621 #endif
622                                 return rc;
623                         }
624                 }
625                 ptr += i + 1;
626                 a->a_desc = ad;
627                 bptr = (struct berval **)(a+1);
628                 a->a_vals = bptr;
629                 j = entry_getlen(&ptr);
630                 a->a_vals[j] = NULL;
631                 vptr = (struct berval *)(bptr + j + 1);
632
633                 while (j) {
634                         i = entry_getlen(&ptr);
635                         *bptr = vptr;
636                         vptr->bv_len = i;
637                         vptr->bv_val = (char *)ptr;
638                         ptr += i+1;
639                         bptr++;
640                         vptr++;
641                         j--;
642                 }
643                 bptr = (struct berval **)vptr;
644         }
645         if (a)
646                 a->a_next = NULL;
647 #ifdef NEW_LOGGING
648         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
649                    "entry_decode:  %s\n", x->e_dn ));
650 #else
651         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
652                 x->e_dn, 0, 0 );
653 #endif
654         *e = x;
655         return 0;
656 }
657 #endif