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