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