]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Apply dn_parent memory leak fix to bdb2
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/ctype.h>
12 #include <ac/errno.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15
16 #include "slap.h"
17
18 static unsigned char    *ebuf;  /* buf returned by entry2str             */
19 static unsigned char    *ecur;  /* pointer to end of currently used ebuf */
20 static int              emaxsize;/* max size of ebuf                     */
21
22 int entry_destroy(void)
23 {
24         free( ebuf );
25         ebuf = NULL;
26         ecur = NULL;
27         emaxsize = 0;
28         return 0;
29 }
30
31 Entry *
32 str2entry( char *s )
33 {
34         Entry           *e;
35         Attribute       **a;
36         char            *type;
37         char            *value;
38         char            *next;
39         ber_len_t       vlen;
40         int             nvals, maxvals;
41         struct berval   bval;
42         struct berval   *vals[2];
43         char            ptype[64];
44
45         /*
46          * LDIF is used as the string format.
47          * An entry looks like this:
48          *
49          *      dn: <dn>\n
50          *      [<attr>:[:] <value>\n]
51          *      [<tab><continuedvalue>\n]*
52          *      ...
53          *
54          * If a double colon is used after a type, it means the
55          * following value is encoded as a base 64 string.  This
56          * happens if the value contains a non-printing character
57          * or newline.
58          */
59
60         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
61                 s ? s : "NULL", 0, 0 );
62
63         next = s;
64
65         /* initialize reader/writer lock */
66         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
67
68         if( e == NULL ) {
69                 Debug( LDAP_DEBUG_TRACE,
70                     "<= str2entry NULL (entry allocation failed)\n",
71                     0, 0, 0 );
72                 return( NULL );
73         }
74
75         e->e_id = NOID;
76         e->e_private = NULL;
77
78         /* dn + attributes */
79         e->e_attrs = NULL;
80         vals[0] = &bval;
81         vals[1] = NULL;
82         ptype[0] = '\0';
83         while ( (s = ldif_getline( &next )) != NULL ) {
84                 if ( *s == '\n' || *s == '\0' ) {
85                         break;
86                 }
87
88                 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
89                         Debug( LDAP_DEBUG_TRACE,
90                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
91                         continue;
92                 }
93
94                 if ( strcasecmp( type, ptype ) != 0 ) {
95                         strncpy( ptype, type, sizeof(ptype) - 1 );
96                         nvals = 0;
97                         maxvals = 0;
98                         a = NULL;
99                 }
100
101                 if ( strcasecmp( type, "dn" ) == 0 ) {
102                         if ( e->e_dn != NULL ) {
103                                 Debug( LDAP_DEBUG_ANY,
104  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
105                                     e->e_id, e->e_dn,
106                                         value != NULL ? value : NULL );
107                                 continue;
108                         }
109                         e->e_dn = ch_strdup( value != NULL ? value : "" );
110
111                         if ( e->e_ndn != NULL ) {
112                                 Debug( LDAP_DEBUG_ANY,
113  "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
114                                     e->e_id, e->e_ndn,
115                                         value != NULL ? value : NULL );
116                                 free( e->e_ndn );
117                         }
118                         e->e_ndn = ch_strdup( e->e_dn );
119                         (void) dn_normalize_case( e->e_ndn );
120                         continue;
121                 }
122
123                 bval.bv_val = value;
124                 bval.bv_len = vlen;
125                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
126                     != 0 ) {
127                         Debug( LDAP_DEBUG_TRACE,
128                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
129                         entry_free( e );
130                         return( NULL );
131                 }
132                 nvals++;
133         }
134
135         /* check to make sure there was a dn: line */
136         if ( e->e_dn == NULL ) {
137                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
138                     e->e_id, 0, 0 );
139                 entry_free( e );
140                 return( NULL );
141         }
142
143         if ( e->e_ndn == NULL ) {
144                 Debug( LDAP_DEBUG_ANY,
145                         "str2entry: entry %ld (\"%s\") has no normalized dn\n",
146                     e->e_id, e->e_dn, 0 );
147                 entry_free( e );
148                 return( NULL );
149         }
150
151         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
152                 e->e_dn, e->e_id, (unsigned long)e );
153
154         return( e );
155 }
156
157 #define GRABSIZE        BUFSIZ
158
159 #define MAKE_SPACE( n ) { \
160                 while ( ecur + (n) > ebuf + emaxsize ) { \
161                         int     offset; \
162                         offset = (int) (ecur - ebuf); \
163                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
164                             emaxsize + GRABSIZE ); \
165                         emaxsize += GRABSIZE; \
166                         ecur = ebuf + offset; \
167                 } \
168 }
169
170 char *
171 entry2str(
172     Entry       *e,
173     int         *len )
174 {
175         Attribute       *a;
176         struct berval   *bv;
177         int             i, tmplen;
178
179         /*
180          * In string format, an entry looks like this:
181          *      <id>\n
182          *      dn: <dn>\n
183          *      [<attr>: <value>\n]*
184          */
185
186         ecur = ebuf;
187
188         /* put the dn */
189         if ( e->e_dn != NULL ) {
190                 /* put "dn: <dn>" */
191                 tmplen = strlen( e->e_dn );
192                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
193                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
194         }
195
196         /* put the attributes */
197         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
198                 /* put "<type>:[:] <value>" line for each value */
199                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
200                         bv = a->a_vals[i];
201                         tmplen = strlen( a->a_type );
202                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
203                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, a->a_type,
204                             bv->bv_val, bv->bv_len );
205                 }
206         }
207         MAKE_SPACE( 1 );
208         *ecur = '\0';
209         *len = ecur - ebuf;
210
211         return( (char *) ebuf );
212 }
213
214 void
215 entry_free( Entry *e )
216 {
217         Attribute       *a, *next;
218
219         if ( e->e_dn != NULL ) {
220                 free( e->e_dn );
221                 e->e_dn = NULL;
222         }
223         if ( e->e_ndn != NULL ) {
224                 free( e->e_ndn );
225                 e->e_ndn = NULL;
226         }
227         for ( a = e->e_attrs; a != NULL; a = next ) {
228                 next = a->a_next;
229                 attr_free( a );
230         }
231         e->e_attrs = NULL;
232         e->e_private = NULL;
233         free( e );
234 }
235
236 /*
237  * These routines are used only by Backend.
238  *
239  * the Entry has three entry points (ways to find things):
240  *
241  *      by entry        e.g., if you already have an entry from the cache
242  *                      and want to delete it. (really by entry ptr)
243  *      by dn           e.g., when looking for the base object of a search
244  *      by id           e.g., for search candidates
245  *
246  * these correspond to three different avl trees that are maintained.
247  */
248
249 int
250 entry_cmp( Entry *e1, Entry *e2 )
251 {
252         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
253 }
254
255 int
256 entry_dn_cmp( Entry *e1, Entry *e2 )
257 {
258         /* compare their normalized UPPERCASED dn's */
259         return( strcmp( e1->e_ndn, e2->e_ndn ) );
260 }
261
262 int
263 entry_id_cmp( Entry *e1, Entry *e2 )
264 {
265         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
266 }
267