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