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