]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Fix the 1.71 fix - only offset the length if the last character of the
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 Entry *
33 str2entry( char *s )
34 {
35         Entry           *e;
36         Attribute       **a;
37         char            *type;
38         char            *value;
39         char            *next;
40         ber_len_t       vlen;
41         int             nvals, maxvals;
42         struct berval   bval;
43         struct berval   *vals[2];
44         char            ptype[64];
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         next = s;
65
66         /* initialize reader/writer lock */
67         e = (Entry *) ch_malloc( sizeof(Entry) );
68
69         if( e == NULL ) {
70                 Debug( LDAP_DEBUG_TRACE,
71                     "<= str2entry NULL (entry allocation failed)\n",
72                     0, 0, 0 );
73                 return( NULL );
74         }
75
76         /* initialize entry */
77         e->e_id = NOID;
78         e->e_dn = NULL;
79         e->e_ndn = NULL;
80         e->e_attrs = NULL;
81         e->e_private = NULL;
82
83         /* dn + attributes */
84         vals[0] = &bval;
85         vals[1] = NULL;
86         ptype[0] = '\0';
87
88         while ( (s = ldif_getline( &next )) != NULL ) {
89                 if ( *s == '\n' || *s == '\0' ) {
90                         break;
91                 }
92
93                 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
94                         Debug( LDAP_DEBUG_TRACE,
95                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
96                         continue;
97                 }
98
99                 if ( strcasecmp( type, ptype ) != 0 ) {
100                         strncpy( ptype, type, sizeof(ptype) - 1 );
101                         nvals = 0;
102                         maxvals = 0;
103                         a = NULL;
104                 }
105
106                 if ( strcasecmp( type, "dn" ) == 0 ) {
107                         free( type );
108
109                         if ( e->e_dn != NULL ) {
110                                 Debug( LDAP_DEBUG_ANY,
111  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
112                                     e->e_id, e->e_dn,
113                                         value != NULL ? value : NULL );
114                                 if( value != NULL ) free( value );
115                                 continue;
116                         }
117                         e->e_dn = value != NULL ? value : ch_strdup( "" );
118                         continue;
119                 }
120
121                 bval.bv_val = value;
122                 bval.bv_len = vlen;
123                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
124                     != 0 ) {
125                         Debug( LDAP_DEBUG_TRACE,
126                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
127                         entry_free( e );
128                         free( value );
129                         free( type );
130                         return( NULL );
131                 }
132
133                 free( value );
134                 free( type );
135                 nvals++;
136         }
137
138         /* check to make sure there was a dn: line */
139         if ( e->e_dn == NULL ) {
140                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
141                     e->e_id, 0, 0 );
142                 entry_free( e );
143                 return( NULL );
144         }
145
146         /* generate normalized dn */
147         e->e_ndn = ch_strdup( e->e_dn );
148         (void) dn_normalize( e->e_ndn );
149
150         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
151                 e->e_dn, e->e_id, (unsigned long)e );
152
153         return( e );
154 }
155
156 #define GRABSIZE        BUFSIZ
157
158 #define MAKE_SPACE( n ) { \
159                 while ( ecur + (n) > ebuf + emaxsize ) { \
160                         ptrdiff_t       offset; \
161                         offset = (int) (ecur - ebuf); \
162                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
163                             emaxsize + GRABSIZE ); \
164                         emaxsize += GRABSIZE; \
165                         ecur = ebuf + offset; \
166                 } \
167 }
168
169 char *
170 entry2str(
171     Entry       *e,
172     int         *len )
173 {
174         Attribute       *a;
175         struct berval   *bv;
176         int             i, tmplen;
177
178         /*
179          * In string format, an entry looks like this:
180          *      <id>\n
181          *      dn: <dn>\n
182          *      [<attr>: <value>\n]*
183          */
184
185         ecur = ebuf;
186
187         /* put the dn */
188         if ( e->e_dn != NULL ) {
189                 /* put "dn: <dn>" */
190                 tmplen = strlen( e->e_dn );
191                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
192                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
193         }
194
195         /* put the attributes */
196         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
197                 /* put "<type>:[:] <value>" line for each value */
198                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
199                         bv = a->a_vals[i];
200                         tmplen = strlen( a->a_type );
201                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
202                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, a->a_type,
203                             bv->bv_val, bv->bv_len );
204                 }
205         }
206         MAKE_SPACE( 1 );
207         *ecur = '\0';
208         *len = ecur - ebuf;
209
210         return( (char *) ebuf );
211 }
212
213 void
214 entry_free( Entry *e )
215 {
216         Attribute       *a, *next;
217
218         if ( e->e_dn != NULL ) {
219                 free( e->e_dn );
220                 e->e_dn = NULL;
221         }
222         if ( e->e_ndn != NULL ) {
223                 free( e->e_ndn );
224                 e->e_ndn = NULL;
225         }
226         for ( a = e->e_attrs; a != NULL; a = next ) {
227                 next = a->a_next;
228                 attr_free( a );
229         }
230         e->e_attrs = NULL;
231         e->e_private = NULL;
232         free( e );
233 }
234
235 /*
236  * These routines are used only by Backend.
237  *
238  * the Entry has three entry points (ways to find things):
239  *
240  *      by entry        e.g., if you already have an entry from the cache
241  *                      and want to delete it. (really by entry ptr)
242  *      by dn           e.g., when looking for the base object of a search
243  *      by id           e.g., for search candidates
244  *
245  * these correspond to three different avl trees that are maintained.
246  */
247
248 int
249 entry_cmp( Entry *e1, Entry *e2 )
250 {
251         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
252 }
253
254 int
255 entry_dn_cmp( Entry *e1, Entry *e2 )
256 {
257         /* compare their normalized UPPERCASED dn's */
258         return( strcmp( e1->e_ndn, e2->e_ndn ) );
259 }
260
261 int
262 entry_id_cmp( Entry *e1, Entry *e2 )
263 {
264         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
265 }
266