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