]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
2c9ce52b6f07200e56d8e004f9e044ace37ae87c
[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 int entry_destroy(void)
25 {
26         free( ebuf );
27         ebuf = NULL;
28         ecur = NULL;
29         emaxsize = 0;
30         return 0;
31 }
32
33
34 Entry *
35 str2entry( char *s )
36 {
37         int rc;
38         Entry           *e;
39         Attribute       **a = NULL;
40         char            *type;
41         struct berval value;
42         struct berval   *vals[2];
43         AttributeDescription *ad;
44         const char *text;
45         char    *next;
46
47         /*
48          * LDIF is used as the string format.
49          * An entry looks like this:
50          *
51          *      dn: <dn>\n
52          *      [<attr>:[:] <value>\n]
53          *      [<tab><continuedvalue>\n]*
54          *      ...
55          *
56          * If a double colon is used after a type, it means the
57          * following value is encoded as a base 64 string.  This
58          * happens if the value contains a non-printing character
59          * or newline.
60          */
61
62 #ifdef NEW_LOGGING
63         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
64                    "str2entry: \"%s\"\n", s ? s : "NULL" ));
65 #else
66         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
67                 s ? s : "NULL", 0, 0 );
68 #endif
69
70         /* initialize reader/writer lock */
71         e = (Entry *) ch_malloc( sizeof(Entry) );
72
73         if( e == NULL ) {
74 #ifdef NEW_LOGGING
75                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
76                            "str2entry: entry allocation failed.\n" ));
77 #else
78                 Debug( LDAP_DEBUG_ANY,
79                     "<= str2entry NULL (entry allocation failed)\n",
80                     0, 0, 0 );
81 #endif
82                 return( NULL );
83         }
84
85         /* initialize entry */
86         e->e_id = NOID;
87         e->e_dn = NULL;
88         e->e_ndn = NULL;
89         e->e_attrs = NULL;
90         e->e_private = NULL;
91
92         /* dn + attributes */
93         vals[0] = &value;
94         vals[1] = NULL;
95
96         next = s;
97         while ( (s = ldif_getline( &next )) != NULL ) {
98                 if ( *s == '\n' || *s == '\0' ) {
99                         break;
100                 }
101
102                 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
103 #ifdef NEW_LOGGING
104                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
105                                    "str2entry:  NULL (parse_line)\n" ));
106 #else
107                         Debug( LDAP_DEBUG_TRACE,
108                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
109 #endif
110                         continue;
111                 }
112
113                 if ( strcasecmp( type, "dn" ) == 0 ) {
114                         free( type );
115
116                         if ( e->e_dn != NULL ) {
117 #ifdef NEW_LOGGING
118                                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
119                                         "entry %ld has multiple dns \"%s\" and \"%s\" "
120                                         "(second ignored)\n",
121                                         (long) e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
122 #else
123                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
124                                         "entry %ld has multiple dns \"%s\" and \"%s\" "
125                                         "(second ignored)\n",
126                                     (long) e->e_id, e->e_dn,
127                                         value.bv_val != NULL ? value.bv_val : "" );
128 #endif
129                                 if( value.bv_val != NULL ) free( value.bv_val );
130                                 continue;
131                         }
132
133                         e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
134                         continue;
135                 }
136
137                 ad = NULL;
138                 rc = slap_str2ad( type, &ad, &text );
139
140                 if( rc != LDAP_SUCCESS ) {
141 #ifdef NEW_LOGGING
142                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
143                                    "str2entry:  str2ad(%s):      %s\n", type, text ));
144 #else
145                         Debug( slapMode & SLAP_TOOL_MODE
146                                 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
147                                 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
148 #endif
149                         if( slapMode & SLAP_TOOL_MODE ) {
150                                 entry_free( e );
151                                 free( value.bv_val );
152                                 free( type );
153                                 return NULL;
154                         }
155
156                         rc = slap_str2undef_ad( type, &ad, &text );
157
158                         if( rc != LDAP_SUCCESS ) {
159 #ifdef NEW_LOGGING
160                                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
161                                            "str2entry:  str2undef_ad(%s):  %s\n", type, text ));
162 #else
163                                 Debug( LDAP_DEBUG_ANY,
164                                         "<= str2entry: str2undef_ad(%s): %s\n",
165                                                 type, text, 0 );
166 #endif
167                                 entry_free( e );
168                                 free( value.bv_val );
169                                 free( type );
170                                 return NULL;
171                         }
172                 }
173
174                 if( slapMode & SLAP_TOOL_MODE ) {
175                         slap_syntax_validate_func *validate =
176                                 ad->ad_type->sat_syntax->ssyn_validate;
177
178                         if( !validate ) {
179 #ifdef NEW_LOGGING
180                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
181                                            "str2entry: no validator for syntax %s\n", 
182                                            ad->ad_type->sat_syntax->ssyn_oid ));
183 #else
184                                 Debug( LDAP_DEBUG_ANY,
185                                         "str2entry: no validator for syntax %s\n",
186                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
187 #endif
188                                 entry_free( e );
189                                 free( value.bv_val );
190                                 free( type );
191                                 return NULL;
192                         }
193
194                         /*
195                          * validate value per syntax
196                          */
197                         rc = validate( ad->ad_type->sat_syntax, &value );
198
199                         if( rc != 0 ) {
200 #ifdef NEW_LOGGING
201                                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
202                                            "str2entry:  invalid value for syntax %s\n",
203                                            ad->ad_type->sat_syntax->ssyn_oid ));
204 #else
205                                 Debug( LDAP_DEBUG_ANY,
206                                         "str2entry: invalid value for syntax %s\n",
207                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
208 #endif
209                                 entry_free( e );
210                                 free( value.bv_val );
211                                 free( type );
212                                 return NULL;
213                         }
214                 }
215
216                 rc = attr_merge( e, ad, vals );
217
218                 ad_free( ad, 1 );
219
220                 if( rc != 0 ) {
221 #ifdef NEW_LOGGING
222                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
223                                    "str2entry:  NULL (attr_merge)\n" ));
224 #else
225                         Debug( LDAP_DEBUG_ANY,
226                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
227 #endif
228                         entry_free( e );
229                         free( value.bv_val );
230                         free( type );
231                         return( NULL );
232                 }
233
234                 free( type );
235                 free( value.bv_val );
236         }
237
238         /* check to make sure there was a dn: line */
239         if ( e->e_dn == NULL ) {
240 #ifdef NEW_LOGGING
241                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
242                            "str2entry:  entry %ld has no dn.\n",
243                                 (long) e->e_id ));
244 #else
245                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
246                     (long) e->e_id, 0, 0 );
247 #endif
248                 entry_free( e );
249                 return( NULL );
250         }
251
252         /* generate normalized dn */
253         e->e_ndn = ch_strdup( e->e_dn );
254         (void) dn_normalize( e->e_ndn );
255
256 #ifdef NEW_LOGGING
257         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
258                    "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
259 #else
260         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
261                 e->e_dn, (unsigned long) e, 0 );
262 #endif
263         return( e );
264 }
265
266
267 #define GRABSIZE        BUFSIZ
268
269 #define MAKE_SPACE( n ) { \
270                 while ( ecur + (n) > ebuf + emaxsize ) { \
271                         ptrdiff_t       offset; \
272                         offset = (int) (ecur - ebuf); \
273                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
274                             emaxsize + GRABSIZE ); \
275                         emaxsize += GRABSIZE; \
276                         ecur = ebuf + offset; \
277                 } \
278         }
279
280 char *
281 entry2str(
282     Entry       *e,
283     int         *len )
284 {
285         Attribute       *a;
286         struct berval   *bv;
287         int             i, tmplen;
288
289         /*
290          * In string format, an entry looks like this:
291          *      dn: <dn>\n
292          *      [<attr>: <value>\n]*
293          */
294
295         ecur = ebuf;
296
297         /* put the dn */
298         if ( e->e_dn != NULL ) {
299                 /* put "dn: <dn>" */
300                 tmplen = strlen( e->e_dn );
301                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
302                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
303         }
304
305         /* put the attributes */
306         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
307                 /* put "<type>:[:] <value>" line for each value */
308                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
309                         bv = a->a_vals[i];
310                         tmplen = a->a_desc->ad_cname->bv_len;
311                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
312                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
313                                 a->a_desc->ad_cname->bv_val,
314                             bv->bv_val, bv->bv_len );
315                 }
316         }
317         MAKE_SPACE( 1 );
318         *ecur = '\0';
319         *len = ecur - ebuf;
320
321         return( (char *) ebuf );
322 }
323
324 void
325 entry_free( Entry *e )
326 {
327         /* free an entry structure */
328         assert( e != NULL );
329
330         /* e_private must be freed by the caller */
331         assert( e->e_private == NULL );
332         e->e_private = NULL;
333
334         /* free DNs */
335         if ( e->e_dn != NULL ) {
336                 free( e->e_dn );
337                 e->e_dn = NULL;
338         }
339         if ( e->e_ndn != NULL ) {
340                 free( e->e_ndn );
341                 e->e_ndn = NULL;
342         }
343
344         /* free attributes */
345         attrs_free( e->e_attrs );
346         e->e_attrs = NULL;
347
348         free( e );
349 }
350
351 /*
352  * These routines are used only by Backend.
353  *
354  * the Entry has three entry points (ways to find things):
355  *
356  *      by entry        e.g., if you already have an entry from the cache
357  *                      and want to delete it. (really by entry ptr)
358  *      by dn           e.g., when looking for the base object of a search
359  *      by id           e.g., for search candidates
360  *
361  * these correspond to three different avl trees that are maintained.
362  */
363
364 int
365 entry_cmp( Entry *e1, Entry *e2 )
366 {
367         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
368 }
369
370 int
371 entry_dn_cmp( Entry *e1, Entry *e2 )
372 {
373         /* compare their normalized UPPERCASED dn's */
374         return( strcmp( e1->e_ndn, e2->e_ndn ) );
375 }
376
377 int
378 entry_id_cmp( Entry *e1, Entry *e2 )
379 {
380         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
381 }
382
383 #ifdef SLAPD_BDB
384
385 /* a LBER encoded entry looks like:
386  *
387  *       entry :== SEQUENCE {
388  *                      dn              DistinguishedName,
389  *                      ndn             NormalizedDistinguishedName,
390  *                      attrs   SEQUENCE OF SEQUENCE {
391  *                              type    AttributeType,
392  *                              values  SET OF AttributeValue
393  *                      }
394  *       }
395  *
396  * Encoding/Decoding of LBER should be much faster than LDIF
397  */
398
399 int entry_decode( struct berval *bv, Entry **entry )
400 {
401         int rc;
402         BerElement      *ber;
403         Entry           *e;
404         ber_tag_t       tag;
405         ber_len_t       len;
406         char *last;
407
408         assert( bv != NULL );
409         assert( entry != NULL );
410
411         ber = ber_init( bv );
412         if( ber == NULL ) {
413                 assert( 0 );    /* XXYYZ: Temporary assert */
414
415 #ifdef NEW_LOGGING
416                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
417                            "entry_decode: ber_init failed\n" ));
418 #else
419                 Debug( LDAP_DEBUG_ANY,
420                     "<= entry_decode: ber_init failed\n",
421                     0, 0, 0 );
422 #endif
423                 return LDAP_LOCAL_ERROR;
424         }
425
426         /* initialize reader/writer lock */
427         e = (Entry *) ch_malloc( sizeof(Entry) );
428
429         if( e == NULL ) {
430 #ifdef NEW_LOGGING
431                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
432                            "entry_decode: entry allocation failed.\n" ));
433 #else
434                 Debug( LDAP_DEBUG_ANY,
435                     "<= entry_decode: entry allocation failed\n",
436                     0, 0, 0 );
437 #endif
438                 return LDAP_LOCAL_ERROR;
439         }
440
441         /* initialize entry */
442         e->e_id = NOID;
443         e->e_dn = NULL;
444         e->e_ndn = NULL;
445         e->e_attrs = NULL;
446         e->e_private = NULL;
447
448         tag = ber_scanf( ber, "{aa" /*"}"*/, &e->e_dn, &e->e_ndn );
449         if( tag == LBER_ERROR ) {
450                 free( e );
451                 return LDAP_PROTOCOL_ERROR;
452         }
453
454 #ifdef NEW_LOGGING
455         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
456                    "entry_decode: \"%s\"\n", e->e_dn ));
457 #else
458         Debug( LDAP_DEBUG_TRACE,
459             "entry_decode: \"%s\"\n",
460             e->e_dn, 0, 0 );
461 #endif
462         /* get the attrs */
463         for ( tag = ber_first_element( ber, &len, &last );
464                 tag != LBER_DEFAULT;
465             tag = ber_next_element( ber, &len, last ) )
466         {
467                 struct berval *type;
468                 struct berval **vals;
469                 AttributeDescription *ad;
470                 const char *text;
471
472                 tag = ber_scanf( ber, "{O{V}}", &type, &vals );
473
474                 if ( tag == LBER_ERROR ) {
475 #ifdef NEW_LOGGING
476                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
477                                    "entry_decode: decoding error (%s)\n", e->e_dn ));
478 #else
479                         Debug( LDAP_DEBUG_ANY, "entry_decode: decoding error\n", 0, 0, 0 );
480 #endif
481                         entry_free( e );
482                         return LDAP_PROTOCOL_ERROR;
483                 }
484
485                 if ( vals == NULL ) {
486 #ifdef NEW_LOGGING
487                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
488                                    "entry_decode: no values for type %s\n", type ));
489 #else
490                         Debug( LDAP_DEBUG_ANY, "entry_decode: no values for type %s\n",
491                                 type, 0, 0 );
492 #endif
493                         ber_bvfree( type );
494                         entry_free( e );
495                         return LDAP_PROTOCOL_ERROR;
496                 }
497
498                 ad = NULL;
499                 rc = slap_bv2ad( type, &ad, &text );
500
501                 if( rc != LDAP_SUCCESS ) {
502 #ifdef NEW_LOGGING
503                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
504                                    "entry_decode: str2ad(%s): %s\n", type, text ));
505 #else
506                         Debug( LDAP_DEBUG_TRACE,
507                                 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
508 #endif
509                         rc = slap_bv2undef_ad( type, &ad, &text );
510
511                         if( rc != LDAP_SUCCESS ) {
512 #ifdef NEW_LOGGING
513                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
514                                            "entry_decode:  str2undef_ad(%s): %s\n", type, text));
515 #else
516                                 Debug( LDAP_DEBUG_ANY,
517                                         "<= entry_decode: str2undef_ad(%s): %s\n",
518                                                 type, text, 0 );
519 #endif
520                                 ber_bvfree( type );
521                                 ber_bvecfree( vals );
522                                 entry_free( e );
523                                 return rc;
524                         }
525                 }
526
527                 rc = attr_merge( e, ad, vals );
528                 ad_free( ad, 1 );
529
530                 if( rc != 0 ) {
531 #ifdef NEW_LOGGING
532                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
533                                    "entry_decode:  attr_merge failed\n"));
534 #else
535                         Debug( LDAP_DEBUG_ANY,
536                             "<= entry_decode: attr_merge failed\n", 0, 0, 0 );
537 #endif
538                         ber_bvfree( type );
539                         ber_bvecfree( vals );
540                         entry_free( e );
541                         return LDAP_LOCAL_ERROR;
542                 }
543
544                 free( type );
545                 ber_bvecfree( vals );
546         }
547
548         rc = ber_scanf( ber, /*"{"*/  "}" );
549         if( rc < 0 ) {
550                 entry_free( e );
551                 return LDAP_PROTOCOL_ERROR;
552         }
553
554 #ifdef NEW_LOGGING
555         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
556                    "entry_decode:  %s\n", e->e_dn ));
557 #else
558         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
559                 e->e_dn, 0, 0 );
560 #endif
561
562         *entry = e;
563         return LDAP_SUCCESS;
564 }
565
566 int entry_encode(
567         Entry *e,
568         struct berval **bv )
569 {
570         int rc = -1;
571         Attribute *a;
572         BerElement *ber;
573         
574 #ifdef NEW_LOGGING
575         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
576                 "entry_encode: id: 0x%08lx  \"%s\"\n",
577                 (long) e->e_id, e->e_dn ));
578 #else
579         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
580                 (long) e->e_id, e->e_dn, 0 );
581 #endif
582         ber = ber_alloc_t( LBER_USE_DER );
583         if( ber == NULL ) {
584                 goto done;
585         }
586
587         rc = ber_printf( ber, "{ss{" /*"}}"*/, e->e_dn, e->e_ndn );
588         if( rc < 0 ) {
589                 goto done;
590         }
591
592         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
593                 rc = ber_printf( ber, "{O{V}}",
594                         a->a_desc->ad_cname,
595                         a->a_vals );
596                 if( rc < 0 ) {
597                         goto done;
598                 }
599         }
600
601         rc = ber_printf( ber, /*"{{"*/ "}}" );
602         if( rc < 0 ) {
603                 goto done;
604         }
605
606         rc = ber_flatten( ber, bv );
607
608 done:
609         ber_free( ber, 1 );
610         if( rc ) {
611 #ifdef NEW_LOGGING
612                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
613                         "entry_encode: id=0x%08lx  failed (%d)\n",
614                         (long) e->e_id, rc ));
615 #else
616                 Debug( LDAP_DEBUG_ANY, "=> entry_encode(0x%08lx): failed (%d)\n",
617                         (long) e->e_id, rc, 0 );
618 #endif
619         }
620         return rc;
621 }
622 #endif