]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
822145f2f518744a535271cc87b28078fd745a73
[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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
124                 /* not yet implemented */
125 #else
126                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
127                     != 0 ) {
128                         Debug( LDAP_DEBUG_TRACE,
129                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
130                         entry_free( e );
131                         free( value );
132                         free( type );
133                         return( NULL );
134                 }
135 #endif
136
137                 free( value );
138                 free( type );
139                 nvals++;
140         }
141
142         /* check to make sure there was a dn: line */
143         if ( e->e_dn == NULL ) {
144                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
145                     e->e_id, 0, 0 );
146                 entry_free( e );
147                 return( NULL );
148         }
149
150         /* generate normalized dn */
151         e->e_ndn = ch_strdup( e->e_dn );
152         (void) dn_normalize( e->e_ndn );
153
154         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
155                 e->e_dn, e->e_id, (unsigned long)e );
156
157         return( e );
158 }
159
160 #define GRABSIZE        BUFSIZ
161
162 #define MAKE_SPACE( n ) { \
163                 while ( ecur + (n) > ebuf + emaxsize ) { \
164                         ptrdiff_t       offset; \
165                         offset = (int) (ecur - ebuf); \
166                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
167                             emaxsize + GRABSIZE ); \
168                         emaxsize += GRABSIZE; \
169                         ecur = ebuf + offset; \
170                 } \
171 }
172
173 char *
174 entry2str(
175     Entry       *e,
176     int         *len )
177 {
178         Attribute       *a;
179         struct berval   *bv;
180         int             i, tmplen;
181
182         /*
183          * In string format, an entry looks like this:
184          *      <id>\n
185          *      dn: <dn>\n
186          *      [<attr>: <value>\n]*
187          */
188
189         ecur = ebuf;
190
191         /* put the dn */
192         if ( e->e_dn != NULL ) {
193                 /* put "dn: <dn>" */
194                 tmplen = strlen( e->e_dn );
195                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
196                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
197         }
198
199         /* put the attributes */
200         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
201                 /* put "<type>:[:] <value>" line for each value */
202                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
203                         bv = a->a_vals[i];
204 #ifdef SLAPD_SCHEMA_NOT_COMPAT
205                         tmplen = a->a_desc.ad_cname->bv_len;
206 #else
207                         tmplen = strlen( a->a_type );
208 #endif
209                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
210                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
211 #ifdef SLAPD_SCHEMA_NOT_COMPAT
212                                 a->a_desc.ad_cname->bv_val,
213 #else
214                                 a->a_type,
215 #endif
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