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