]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Move experimental Back-BDB2 to Attic
[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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
43         AttributeDescription *ad;
44         const char *text;
45 #else
46         int             nvals = 0;
47         int             maxvals = 0;
48         char    ptype[64];      /* fixed size buffer */
49 #endif
50         char    *next;
51
52         /*
53          * LDIF is used as the string format.
54          * An entry looks like this:
55          *
56          *      dn: <dn>\n
57          *      [<attr>:[:] <value>\n]
58          *      [<tab><continuedvalue>\n]*
59          *      ...
60          *
61          * If a double colon is used after a type, it means the
62          * following value is encoded as a base 64 string.  This
63          * happens if the value contains a non-printing character
64          * or newline.
65          */
66
67         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
68                 s ? s : "NULL", 0, 0 );
69
70         /* initialize reader/writer lock */
71         e = (Entry *) ch_malloc( sizeof(Entry) );
72
73         if( e == NULL ) {
74                 Debug( LDAP_DEBUG_TRACE,
75                     "<= str2entry NULL (entry allocation failed)\n",
76                     0, 0, 0 );
77                 return( NULL );
78         }
79
80         /* initialize entry */
81         e->e_id = NOID;
82         e->e_dn = NULL;
83         e->e_ndn = NULL;
84         e->e_attrs = NULL;
85         e->e_private = NULL;
86
87         /* dn + attributes */
88         vals[0] = &value;
89         vals[1] = NULL;
90 #ifndef SLAPD_SCHEMA_NOT_COMPAT
91         ptype[0] = '\0';
92 #endif
93
94         next = s;
95         while ( (s = ldif_getline( &next )) != NULL ) {
96                 if ( *s == '\n' || *s == '\0' ) {
97                         break;
98                 }
99
100                 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
101                         Debug( LDAP_DEBUG_TRACE,
102                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
103                         continue;
104                 }
105
106 #ifndef SLAPD_SCHEMA_NOT_COMPAT
107                 if ( strcasecmp( type, ptype ) != 0 ) {
108                         strncpy( ptype, type, sizeof(ptype) - 1 );
109                         nvals = 0;
110                         maxvals = 0;
111                         a = NULL;
112                 }
113
114 #endif
115                 if ( strcasecmp( type, "dn" ) == 0 ) {
116                         free( type );
117
118                         if ( e->e_dn != NULL ) {
119                                 Debug( LDAP_DEBUG_ANY,
120  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
121                                     e->e_id, e->e_dn,
122                                         value.bv_val != NULL ? value.bv_val : "" );
123                                 if( value.bv_val != NULL ) free( value.bv_val );
124                                 continue;
125                         }
126
127                         e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
128                         continue;
129                 }
130
131 #ifdef SLAPD_SCHEMA_NOT_COMPAT
132                 ad = NULL;
133                 rc = slap_str2ad( type, &ad, &text );
134
135                 if( rc != LDAP_SUCCESS ) {
136                         Debug( LDAP_DEBUG_TRACE,
137                                 "<= str2entry NULL (str2ad=%s)\n", text, 0, 0 );
138                         entry_free( e );
139                         free( value.bv_val );
140                         free( type );
141                         return( NULL );
142                 }
143
144                 rc = attr_merge( e, ad, vals );
145
146                 ad_free( ad, 1 );
147 #else
148                 rc = attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a );
149 #endif
150
151                 if( rc != 0 ) {
152                         Debug( LDAP_DEBUG_TRACE,
153                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
154                         entry_free( e );
155                         free( value.bv_val );
156                         free( type );
157                         return( NULL );
158                 }
159
160                 free( type );
161                 free( value.bv_val );
162 #ifndef SLAPD_SCHEMA_NOT_COMPAT
163                 nvals++;
164 #endif
165         }
166
167         /* check to make sure there was a dn: line */
168         if ( e->e_dn == NULL ) {
169                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
170                     e->e_id, 0, 0 );
171                 entry_free( e );
172                 return( NULL );
173         }
174
175         /* generate normalized dn */
176         e->e_ndn = ch_strdup( e->e_dn );
177         (void) dn_normalize( e->e_ndn );
178
179         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
180                 e->e_dn, e->e_id, (unsigned long)e );
181
182         return( e );
183 }
184
185
186 #define GRABSIZE        BUFSIZ
187
188 #define MAKE_SPACE( n ) { \
189                 while ( ecur + (n) > ebuf + emaxsize ) { \
190                         ptrdiff_t       offset; \
191                         offset = (int) (ecur - ebuf); \
192                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
193                             emaxsize + GRABSIZE ); \
194                         emaxsize += GRABSIZE; \
195                         ecur = ebuf + offset; \
196                 } \
197         }
198
199 char *
200 entry2str(
201     Entry       *e,
202     int         *len )
203 {
204         Attribute       *a;
205         struct berval   *bv;
206         int             i, tmplen;
207
208         /*
209          * In string format, an entry looks like this:
210          *      dn: <dn>\n
211          *      [<attr>: <value>\n]*
212          */
213
214         ecur = ebuf;
215
216         /* put the dn */
217         if ( e->e_dn != NULL ) {
218                 /* put "dn: <dn>" */
219                 tmplen = strlen( e->e_dn );
220                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
221                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
222         }
223
224         /* put the attributes */
225         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
226                 /* put "<type>:[:] <value>" line for each value */
227                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
228                         bv = a->a_vals[i];
229 #ifdef SLAPD_SCHEMA_NOT_COMPAT
230                         tmplen = a->a_desc->ad_cname->bv_len;
231 #else
232                         tmplen = strlen( a->a_type );
233 #endif
234                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
235                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
236 #ifdef SLAPD_SCHEMA_NOT_COMPAT
237                                 a->a_desc->ad_cname->bv_val,
238 #else
239                                 a->a_type,
240 #endif
241                             bv->bv_val, bv->bv_len );
242                 }
243         }
244         MAKE_SPACE( 1 );
245         *ecur = '\0';
246         *len = ecur - ebuf;
247
248         return( (char *) ebuf );
249 }
250
251 void
252 entry_free( Entry *e )
253 {
254         Attribute       *a, *next;
255
256         if ( e->e_dn != NULL ) {
257                 free( e->e_dn );
258                 e->e_dn = NULL;
259         }
260         if ( e->e_ndn != NULL ) {
261                 free( e->e_ndn );
262                 e->e_ndn = NULL;
263         }
264         for ( a = e->e_attrs; a != NULL; a = next ) {
265                 next = a->a_next;
266                 attr_free( a );
267         }
268         e->e_attrs = NULL;
269         e->e_private = NULL;
270         free( e );
271 }
272
273 /*
274  * These routines are used only by Backend.
275  *
276  * the Entry has three entry points (ways to find things):
277  *
278  *      by entry        e.g., if you already have an entry from the cache
279  *                      and want to delete it. (really by entry ptr)
280  *      by dn           e.g., when looking for the base object of a search
281  *      by id           e.g., for search candidates
282  *
283  * these correspond to three different avl trees that are maintained.
284  */
285
286 int
287 entry_cmp( Entry *e1, Entry *e2 )
288 {
289         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
290 }
291
292 int
293 entry_dn_cmp( Entry *e1, Entry *e2 )
294 {
295         /* compare their normalized UPPERCASED dn's */
296         return( strcmp( e1->e_ndn, e2->e_ndn ) );
297 }
298
299 int
300 entry_id_cmp( Entry *e1, Entry *e2 )
301 {
302         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
303 }
304