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