]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
complete this round of constification
[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                         free( type );
103
104                         if ( e->e_dn != NULL ) {
105                                 Debug( LDAP_DEBUG_ANY,
106  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
107                                     e->e_id, e->e_dn,
108                                         value != NULL ? value : NULL );
109                                 if( value != NULL ) free( value );
110                                 continue;
111                         }
112                         e->e_dn = value != NULL ? value : ch_strdup( "" );
113
114                         if ( e->e_ndn != NULL ) {
115                                 Debug( LDAP_DEBUG_ANY,
116  "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
117                                     e->e_id, e->e_ndn,
118                                         value != NULL ? value : NULL );
119                                 free( e->e_ndn );
120                         }
121                         e->e_ndn = ch_strdup( e->e_dn );
122                         (void) dn_normalize_case( e->e_ndn );
123                         continue;
124                 }
125
126                 bval.bv_val = value;
127                 bval.bv_len = vlen;
128                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
129                     != 0 ) {
130                         Debug( LDAP_DEBUG_TRACE,
131                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
132                         entry_free( e );
133                         free( value );
134                         free( type );
135                         return( NULL );
136                 }
137
138                 free( value );
139                 free( type );
140                 nvals++;
141         }
142
143         /* check to make sure there was a dn: line */
144         if ( e->e_dn == NULL ) {
145                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
146                     e->e_id, 0, 0 );
147                 entry_free( e );
148                 return( NULL );
149         }
150
151         if ( e->e_ndn == NULL ) {
152                 Debug( LDAP_DEBUG_ANY,
153                         "str2entry: entry %ld (\"%s\") has no normalized dn\n",
154                     e->e_id, e->e_dn, 0 );
155                 entry_free( e );
156                 return( NULL );
157         }
158
159         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
160                 e->e_dn, e->e_id, (unsigned long)e );
161
162         return( e );
163 }
164
165 #define GRABSIZE        BUFSIZ
166
167 #define MAKE_SPACE( n ) { \
168                 while ( ecur + (n) > ebuf + emaxsize ) { \
169                         int     offset; \
170                         offset = (int) (ecur - ebuf); \
171                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
172                             emaxsize + GRABSIZE ); \
173                         emaxsize += GRABSIZE; \
174                         ecur = ebuf + offset; \
175                 } \
176 }
177
178 char *
179 entry2str(
180     Entry       *e,
181     int         *len )
182 {
183         Attribute       *a;
184         struct berval   *bv;
185         int             i, tmplen;
186
187         /*
188          * In string format, an entry looks like this:
189          *      <id>\n
190          *      dn: <dn>\n
191          *      [<attr>: <value>\n]*
192          */
193
194         ecur = ebuf;
195
196         /* put the dn */
197         if ( e->e_dn != NULL ) {
198                 /* put "dn: <dn>" */
199                 tmplen = strlen( e->e_dn );
200                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
201                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
202         }
203
204         /* put the attributes */
205         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
206                 /* put "<type>:[:] <value>" line for each value */
207                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
208                         bv = a->a_vals[i];
209                         tmplen = strlen( a->a_type );
210                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
211                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, a->a_type,
212                             bv->bv_val, bv->bv_len );
213                 }
214         }
215         MAKE_SPACE( 1 );
216         *ecur = '\0';
217         *len = ecur - ebuf;
218
219         return( (char *) ebuf );
220 }
221
222 void
223 entry_free( Entry *e )
224 {
225         Attribute       *a, *next;
226
227         if ( e->e_dn != NULL ) {
228                 free( e->e_dn );
229                 e->e_dn = NULL;
230         }
231         if ( e->e_ndn != NULL ) {
232                 free( e->e_ndn );
233                 e->e_ndn = NULL;
234         }
235         for ( a = e->e_attrs; a != NULL; a = next ) {
236                 next = a->a_next;
237                 attr_free( a );
238         }
239         e->e_attrs = NULL;
240         e->e_private = NULL;
241         free( e );
242 }
243
244 /*
245  * These routines are used only by Backend.
246  *
247  * the Entry has three entry points (ways to find things):
248  *
249  *      by entry        e.g., if you already have an entry from the cache
250  *                      and want to delete it. (really by entry ptr)
251  *      by dn           e.g., when looking for the base object of a search
252  *      by id           e.g., for search candidates
253  *
254  * these correspond to three different avl trees that are maintained.
255  */
256
257 int
258 entry_cmp( Entry *e1, Entry *e2 )
259 {
260         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
261 }
262
263 int
264 entry_dn_cmp( Entry *e1, Entry *e2 )
265 {
266         /* compare their normalized UPPERCASED dn's */
267         return( strcmp( e1->e_ndn, e2->e_ndn ) );
268 }
269
270 int
271 entry_id_cmp( Entry *e1, Entry *e2 )
272 {
273         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
274 }
275