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