]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Add missing semicolon.
[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 = ldif_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
72         e->e_id = id;
73         e->e_private = NULL;
74
75         /* dn + attributes */
76         e->e_attrs = NULL;
77         vals[0] = &bval;
78         vals[1] = NULL;
79         ptype[0] = '\0';
80         while ( (s = ldif_getline( &next )) != NULL ) {
81                 if ( *s == '\n' || *s == '\0' ) {
82                         break;
83                 }
84
85                 if ( ldif_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 %ld 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 %ld 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 = ch_strdup( value );
114                         (void) dn_normalize_case( e->e_ndn );
115                         continue;
116                 }
117
118                 bval.bv_val = value;
119                 bval.bv_len = vlen;
120                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
121                     != 0 ) {
122                         Debug( LDAP_DEBUG_TRACE,
123                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
124                         entry_free( e );
125                         return( NULL );
126                 }
127                 nvals++;
128         }
129
130         /* check to make sure there was a dn: line */
131         if ( e->e_dn == NULL ) {
132                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
133                     e->e_id, 0, 0 );
134                 entry_free( e );
135                 return( NULL );
136         }
137
138         if ( e->e_ndn == NULL ) {
139                 Debug( LDAP_DEBUG_ANY,
140                         "str2entry: entry %ld (\"%s\") has no normalized dn\n",
141                     e->e_id, e->e_dn, 0 );
142                 entry_free( e );
143                 return( NULL );
144         }
145
146         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
147                 e->e_dn, e->e_id, (unsigned long)e );
148
149         return( e );
150 }
151
152 #define GRABSIZE        BUFSIZ
153
154 #define MAKE_SPACE( n ) { \
155                 while ( ecur + (n) > ebuf + emaxsize ) { \
156                         int     offset; \
157                         offset = (int) (ecur - ebuf); \
158                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
159                             emaxsize + GRABSIZE ); \
160                         emaxsize += GRABSIZE; \
161                         ecur = ebuf + offset; \
162                 } \
163 }
164
165 char *
166 entry2str(
167     Entry       *e,
168     int         *len,
169     int         printid
170 )
171 {
172         Attribute       *a;
173         struct berval   *bv;
174         int             i, tmplen;
175
176         /*
177          * In string format, an entry looks like this:
178          *      <id>\n
179          *      dn: <dn>\n
180          *      [<attr>: <value>\n]*
181          */
182
183         ecur = ebuf;
184
185         if ( printid ) {
186                 /* id + newline */
187                 MAKE_SPACE( 10 );
188                 sprintf( (char *) ecur, "%ld\n", e->e_id );
189                 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
190         }
191
192         /* put the dn */
193         if ( e->e_dn != NULL ) {
194                 /* put "dn: <dn>" */
195                 tmplen = strlen( e->e_dn );
196                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
197                 ldif_put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
198         }
199
200         /* put the attributes */
201         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
202                 /* put "<type>:[:] <value>" line for each value */
203                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
204                         bv = a->a_vals[i];
205                         tmplen = strlen( a->a_type );
206                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
207                         ldif_put_type_and_value( (char **) &ecur, a->a_type,
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