]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
s/SUBSTRINGS/SUBSTR/
[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
33 Entry *
34 str2entry( char *s )
35 {
36         int rc;
37         Entry           *e;
38         Attribute       **a = NULL;
39         char            *type;
40         struct berval value;
41         struct berval   *vals[2];
42         AttributeDescription *ad;
43         const char *text;
44         char    *next;
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         /* initialize reader/writer lock */
65         e = (Entry *) ch_malloc( sizeof(Entry) );
66
67         if( e == NULL ) {
68                 Debug( LDAP_DEBUG_TRACE,
69                     "<= str2entry NULL (entry allocation failed)\n",
70                     0, 0, 0 );
71                 return( NULL );
72         }
73
74         /* initialize entry */
75         e->e_id = NOID;
76         e->e_dn = NULL;
77         e->e_ndn = NULL;
78         e->e_attrs = NULL;
79         e->e_private = NULL;
80
81         /* dn + attributes */
82         vals[0] = &value;
83         vals[1] = NULL;
84
85         next = s;
86         while ( (s = ldif_getline( &next )) != NULL ) {
87                 if ( *s == '\n' || *s == '\0' ) {
88                         break;
89                 }
90
91                 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
92                         Debug( LDAP_DEBUG_TRACE,
93                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
94                         continue;
95                 }
96
97                 if ( strcasecmp( type, "dn" ) == 0 ) {
98                         free( type );
99
100                         if ( e->e_dn != NULL ) {
101                                 Debug( LDAP_DEBUG_ANY,
102  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
103                                     e->e_id, e->e_dn,
104                                         value.bv_val != NULL ? value.bv_val : "" );
105                                 if( value.bv_val != NULL ) free( value.bv_val );
106                                 continue;
107                         }
108
109                         e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
110                         continue;
111                 }
112
113                 ad = NULL;
114                 rc = slap_str2ad( type, &ad, &text );
115
116                 if( rc != LDAP_SUCCESS ) {
117                         Debug( LDAP_DEBUG_TRACE,
118                                 "<= str2entry NULL (str2ad=%s)\n", text, 0, 0 );
119                         entry_free( e );
120                         free( value.bv_val );
121                         free( type );
122                         return( NULL );
123                 }
124
125                 rc = attr_merge( e, ad, vals );
126
127                 ad_free( ad, 1 );
128
129                 if( rc != 0 ) {
130                         Debug( LDAP_DEBUG_TRACE,
131                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
132                         entry_free( e );
133                         free( value.bv_val );
134                         free( type );
135                         return( NULL );
136                 }
137
138                 free( type );
139                 free( value.bv_val );
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
161 #define GRABSIZE        BUFSIZ
162
163 #define MAKE_SPACE( n ) { \
164                 while ( ecur + (n) > ebuf + emaxsize ) { \
165                         ptrdiff_t       offset; \
166                         offset = (int) (ecur - ebuf); \
167                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
168                             emaxsize + GRABSIZE ); \
169                         emaxsize += GRABSIZE; \
170                         ecur = ebuf + offset; \
171                 } \
172         }
173
174 char *
175 entry2str(
176     Entry       *e,
177     int         *len )
178 {
179         Attribute       *a;
180         struct berval   *bv;
181         int             i, tmplen;
182
183         /*
184          * In string format, an entry looks like this:
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                         tmplen = a->a_desc->ad_cname->bv_len;
205                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
206                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
207                                 a->a_desc->ad_cname->bv_val,
208                             bv->bv_val, bv->bv_len );
209                 }
210         }
211         MAKE_SPACE( 1 );
212         *ecur = '\0';
213         *len = ecur - ebuf;
214
215         return( (char *) ebuf );
216 }
217
218 void
219 entry_free( Entry *e )
220 {
221         Attribute       *a, *next;
222
223         if ( e->e_dn != NULL ) {
224                 free( e->e_dn );
225                 e->e_dn = NULL;
226         }
227         if ( e->e_ndn != NULL ) {
228                 free( e->e_ndn );
229                 e->e_ndn = NULL;
230         }
231         for ( a = e->e_attrs; a != NULL; a = next ) {
232                 next = a->a_next;
233                 attr_free( a );
234         }
235         e->e_attrs = NULL;
236         e->e_private = NULL;
237         free( e );
238 }
239
240 /*
241  * These routines are used only by Backend.
242  *
243  * the Entry has three entry points (ways to find things):
244  *
245  *      by entry        e.g., if you already have an entry from the cache
246  *                      and want to delete it. (really by entry ptr)
247  *      by dn           e.g., when looking for the base object of a search
248  *      by id           e.g., for search candidates
249  *
250  * these correspond to three different avl trees that are maintained.
251  */
252
253 int
254 entry_cmp( Entry *e1, Entry *e2 )
255 {
256         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
257 }
258
259 int
260 entry_dn_cmp( Entry *e1, Entry *e2 )
261 {
262         /* compare their normalized UPPERCASED dn's */
263         return( strcmp( e1->e_ndn, e2->e_ndn ) );
264 }
265
266 int
267 entry_id_cmp( Entry *e1, Entry *e2 )
268 {
269         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
270 }
271