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