]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
bc0f2de4da36bf19bcd5b554d2119f6933bb01ad
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/ctype.h>
8 #include <ac/errno.h>
9 #include <ac/socket.h>
10 #include <ac/string.h>
11
12 #include "slap.h"
13
14 static unsigned char    *ebuf;  /* buf returned by entry2str             */
15 static unsigned char    *ecur;  /* pointer to end of currently used ebuf */
16 static int              emaxsize;/* max size of ebuf                     */
17
18 Entry *
19 str2entry( char *s )
20 {
21         int                     id = 0;
22         Entry           *e;
23         Attribute       **a;
24         char            *type;
25         char            *value;
26         char            *next;
27         int             vlen, nvals, maxvals;
28         struct berval   bval;
29         struct berval   *vals[2];
30         char            ptype[64];
31
32         /*
33          * In string format, an entry looks like this:
34          *
35          *      <id>\n
36          *      dn: <dn>\n
37          *      [<attr>:[:] <value>\n]
38          *      [<tab><continuedvalue>\n]*
39          *      ...
40          *
41          * If a double colon is used after a type, it means the
42          * following value is encoded as a base 64 string.  This
43          * happens if the value contains a non-printing character
44          * or newline.
45          */
46
47         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
48                 s ? s : "NULL", 0, 0 );
49
50         /* check to see if there's an id included */
51         next = s;
52         if ( isdigit( (unsigned char) *s ) ) {
53                 id = atoi( s );
54                 if ( (s = str_getline( &next )) == NULL ) {
55                         Debug( LDAP_DEBUG_TRACE,
56                             "<= str2entry NULL (missing newline after id)\n",
57                             0, 0, 0 );
58                         return( NULL );
59                 }
60         }
61
62         /* initialize reader/writer lock */
63         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
64
65         if( e == NULL ) {
66                 Debug( LDAP_DEBUG_TRACE,
67                     "<= str2entry NULL (entry allocation failed)\n",
68                     0, 0, 0 );
69                 return( NULL );
70         }
71         e->e_id = id;
72
73         entry_rdwr_init(e);
74
75         /* dn + attributes */
76         e->e_attrs = NULL;
77         vals[0] = &bval;
78         vals[1] = NULL;
79         ptype[0] = '\0';
80         while ( (s = str_getline( &next )) != NULL ) {
81                 if ( *s == '\n' || *s == '\0' ) {
82                         break;
83                 }
84
85                 if ( str_parse_line( s, &type, &value, &vlen ) != 0 ) {
86                         Debug( LDAP_DEBUG_TRACE,
87                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
88                         continue;
89                 }
90
91                 if ( strcasecmp( type, ptype ) != 0 ) {
92                         strncpy( ptype, type, sizeof(ptype) - 1 );
93                         nvals = 0;
94                         maxvals = 0;
95                         a = NULL;
96                 }
97
98                 if ( strcasecmp( type, "dn" ) == 0 ) {
99                         if ( e->e_dn != NULL ) {
100                                 Debug( LDAP_DEBUG_ANY,
101  "str2entry: entry %lu has multiple dns \"%s\" and \"%s\" (second ignored)\n",
102                                     e->e_id, e->e_dn, value );
103                                 continue;
104                         }
105                         e->e_dn = ch_strdup( value );
106
107                         if ( e->e_ndn != NULL ) {
108                                 Debug( LDAP_DEBUG_ANY,
109  "str2entry: entry %lu already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
110                                     e->e_id, e->e_ndn, value );
111                                 free( e->e_ndn );
112                         }
113                         e->e_ndn = dn_normalize_case( ch_strdup( value ) );
114                         continue;
115                 }
116
117                 bval.bv_val = value;
118                 bval.bv_len = vlen;
119                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
120                     != 0 ) {
121                         Debug( LDAP_DEBUG_TRACE,
122                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
123                         entry_free( e );
124                         return( NULL );
125                 }
126                 nvals++;
127         }
128
129         /* check to make sure there was a dn: line */
130         if ( e->e_dn == NULL ) {
131                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %lu has no dn\n",
132                     e->e_id, 0, 0 );
133                 entry_free( e );
134                 return( NULL );
135         }
136
137         if ( e->e_ndn == NULL ) {
138                 Debug( LDAP_DEBUG_ANY,
139                         "str2entry: entry %lu (\"%s\") has no normalized dn\n",
140                     e->e_id, e->e_dn, 0 );
141                 entry_free( e );
142                 return( NULL );
143         }
144
145         Debug(LDAP_DEBUG_TRACE, "<= str2entry 0x%lx\n", (unsigned long)e, 0,0);
146
147         return( e );
148 }
149
150 #define GRABSIZE        BUFSIZ
151
152 #define MAKE_SPACE( n ) { \
153                 while ( ecur + (n) > ebuf + emaxsize ) { \
154                         ptrdiff_t       offset; \
155                         offset = (int) (ecur - ebuf); \
156                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
157                             emaxsize + GRABSIZE ); \
158                         emaxsize += GRABSIZE; \
159                         ecur = ebuf + offset; \
160                 } \
161 }
162
163 char *
164 entry2str(
165     Entry       *e,
166     int         *len,
167     int         printid
168 )
169 {
170         Attribute       *a;
171         struct berval   *bv;
172         int             i, tmplen;
173
174         /*
175          * In string format, an entry looks like this:
176          *      <id>\n
177          *      dn: <dn>\n
178          *      [<attr>: <value>\n]*
179          */
180
181         ecur = ebuf;
182
183         if ( printid ) {
184                 /* id + newline */
185                 MAKE_SPACE( 10 );
186                 sprintf( (char *) ecur, "%ld\n", e->e_id );
187                 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
188         }
189
190         /* put the dn */
191         if ( e->e_dn != NULL ) {
192                 /* put "dn: <dn>" */
193                 tmplen = strlen( e->e_dn );
194                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
195                 put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
196         }
197
198         /* put the attributes */
199         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
200                 /* put "<type>:[:] <value>" line for each value */
201                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
202                         bv = a->a_vals[i];
203                         tmplen = strlen( a->a_type );
204                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
205                         put_type_and_value( (char **) &ecur, a->a_type,
206                             bv->bv_val, bv->bv_len );
207                 }
208         }
209         MAKE_SPACE( 1 );
210         *ecur = '\0';
211         *len = ecur - ebuf;
212
213         return( (char *) ebuf );
214 }
215
216 void
217 entry_free( Entry *e )
218 {
219         int             i;
220         Attribute       *a, *next;
221
222         /* check that no reader/writer locks exist */
223
224         if ( ldap_pvt_thread_rdwr_wtrylock( &e->e_rdwr ) == 
225                 LDAP_PVT_THREAD_EBUSY )
226         {
227                 Debug( LDAP_DEBUG_ANY, "entry_free(%ld): active (%d, %d)\n",
228                         e->e_id,
229                         ldap_pvt_thread_rdwr_readers( &e->e_rdwr ),
230                         ldap_pvt_thread_rdwr_writers( &e->e_rdwr ));
231
232 #ifdef LDAP_DEBUG
233                 assert(!ldap_pvt_thread_rdwr_active( &e->e_rdwr ));
234 #endif
235         }
236
237         if ( e->e_dn != NULL ) {
238                 free( e->e_dn );
239         }
240         if ( e->e_ndn != NULL ) {
241                 free( e->e_ndn );
242         }
243         for ( a = e->e_attrs; a != NULL; a = next ) {
244                 next = a->a_next;
245                 attr_free( a );
246         }
247         free( e );
248 }
249
250 int
251 entry_rdwr_lock(Entry *e, int rw)
252 {
253         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
254                 rw ? "w" : "r", e->e_id, 0);
255         if (rw)
256                 return ldap_pvt_thread_rdwr_wlock(&e->e_rdwr);
257         else
258                 return ldap_pvt_thread_rdwr_rlock(&e->e_rdwr);
259 }
260
261 int
262 entry_rdwr_rlock(Entry *e)
263 {
264         return entry_rdwr_lock( e, 0 );
265 }
266
267 int
268 entry_rdwr_wlock(Entry *e)
269 {
270         return entry_rdwr_lock( e, 1 );
271 }
272
273 int
274 entry_rdwr_trylock(Entry *e, int rw)
275 {
276         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
277                 rw ? "w" : "r", e->e_id, 0);
278         if (rw)
279                 return ldap_pvt_thread_rdwr_wtrylock(&e->e_rdwr);
280         else
281                 return ldap_pvt_thread_rdwr_rtrylock(&e->e_rdwr);
282 }
283
284 int
285 entry_rdwr_unlock(Entry *e, int rw)
286 {
287         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
288                 rw ? "w" : "r", e->e_id, 0);
289         if (rw)
290                 return ldap_pvt_thread_rdwr_wunlock(&e->e_rdwr);
291         else
292                 return ldap_pvt_thread_rdwr_runlock(&e->e_rdwr);
293 }
294
295 int
296 entry_rdwr_runlock(Entry *e)
297 {
298         return entry_rdwr_unlock( e, 0 );
299 }
300
301 int
302 entry_rdwr_wunlock(Entry *e)
303 {
304         return entry_rdwr_unlock( e, 1 );
305 }
306
307 int
308 entry_rdwr_init(Entry *e)
309 {
310         return ldap_pvt_thread_rdwr_init( &e->e_rdwr );
311 }