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