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