]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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_calloc( 1, 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         e->e_id = NOID;
77         e->e_private = NULL;
78
79         /* dn + attributes */
80         e->e_attrs = NULL;
81         vals[0] = &bval;
82         vals[1] = NULL;
83         ptype[0] = '\0';
84         while ( (s = ldif_getline( &next )) != NULL ) {
85                 if ( *s == '\n' || *s == '\0' ) {
86                         break;
87                 }
88
89                 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
90                         Debug( LDAP_DEBUG_TRACE,
91                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
92                         continue;
93                 }
94
95                 if ( strcasecmp( type, ptype ) != 0 ) {
96                         strncpy( ptype, type, sizeof(ptype) - 1 );
97                         nvals = 0;
98                         maxvals = 0;
99                         a = NULL;
100                 }
101
102                 if ( strcasecmp( type, "dn" ) == 0 ) {
103                         free( type );
104
105                         if ( e->e_dn != NULL ) {
106                                 Debug( LDAP_DEBUG_ANY,
107  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
108                                     e->e_id, e->e_dn,
109                                         value != NULL ? value : NULL );
110                                 if( value != NULL ) free( value );
111                                 continue;
112                         }
113                         e->e_dn = value != NULL ? value : ch_strdup( "" );
114
115                         if ( e->e_ndn != NULL ) {
116                                 Debug( LDAP_DEBUG_ANY,
117  "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
118                                     e->e_id, e->e_ndn,
119                                         value != NULL ? value : NULL );
120                                 free( e->e_ndn );
121                         }
122                         e->e_ndn = ch_strdup( e->e_dn );
123                         (void) dn_normalize_case( e->e_ndn );
124                         continue;
125                 }
126
127                 bval.bv_val = value;
128                 bval.bv_len = vlen;
129                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
130                     != 0 ) {
131                         Debug( LDAP_DEBUG_TRACE,
132                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
133                         entry_free( e );
134                         free( value );
135                         free( type );
136                         return( NULL );
137                 }
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         if ( e->e_ndn == NULL ) {
153                 Debug( LDAP_DEBUG_ANY,
154                         "str2entry: entry %ld (\"%s\") has no normalized dn\n",
155                     e->e_id, e->e_dn, 0 );
156                 entry_free( e );
157                 return( NULL );
158         }
159
160         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
161                 e->e_dn, e->e_id, (unsigned long)e );
162
163         return( e );
164 }
165
166 #define GRABSIZE        BUFSIZ
167
168 #define MAKE_SPACE( n ) { \
169                 while ( ecur + (n) > ebuf + emaxsize ) { \
170                         int     offset; \
171                         offset = (int) (ecur - ebuf); \
172                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
173                             emaxsize + GRABSIZE ); \
174                         emaxsize += GRABSIZE; \
175                         ecur = ebuf + offset; \
176                 } \
177 }
178
179 char *
180 entry2str(
181     Entry       *e,
182     int         *len )
183 {
184         Attribute       *a;
185         struct berval   *bv;
186         int             i, tmplen;
187
188         /*
189          * In string format, an entry looks like this:
190          *      <id>\n
191          *      dn: <dn>\n
192          *      [<attr>: <value>\n]*
193          */
194
195         ecur = ebuf;
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