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