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