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