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