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