]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Change 'unsigned long len' to ber_len_t in get_filter()
[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         ber_len_t       vlen;
28         int             nvals, maxvals;
29         struct berval   bval;
30         struct berval   *vals[2];
31         char            ptype[64];
32
33         /*
34          * In string format, an entry looks like this:
35          *
36          *      <id>\n
37          *      dn: <dn>\n
38          *      [<attr>:[:] <value>\n]
39          *      [<tab><continuedvalue>\n]*
40          *      ...
41          *
42          * If a double colon is used after a type, it means the
43          * following value is encoded as a base 64 string.  This
44          * happens if the value contains a non-printing character
45          * or newline.
46          */
47
48         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
49                 s ? s : "NULL", 0, 0 );
50
51         /* check to see if there's an id included */
52         next = s;
53         if ( isdigit( (unsigned char) *s ) ) {
54                 id = atoi( s );
55                 if ( (s = ldif_getline( &next )) == NULL ) {
56                         Debug( LDAP_DEBUG_TRACE,
57                             "<= str2entry NULL (missing newline after id)\n",
58                             0, 0, 0 );
59                         return( NULL );
60                 }
61         }
62
63         /* initialize reader/writer lock */
64         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
65
66         if( e == NULL ) {
67                 Debug( LDAP_DEBUG_TRACE,
68                     "<= str2entry NULL (entry allocation failed)\n",
69                     0, 0, 0 );
70                 return( NULL );
71         }
72
73         e->e_id = id;
74         e->e_private = NULL;
75
76         /* dn + attributes */
77         e->e_attrs = NULL;
78         vals[0] = &bval;
79         vals[1] = NULL;
80         ptype[0] = '\0';
81         while ( (s = ldif_getline( &next )) != NULL ) {
82                 if ( *s == '\n' || *s == '\0' ) {
83                         break;
84                 }
85
86                 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
87                         Debug( LDAP_DEBUG_TRACE,
88                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
89                         continue;
90                 }
91
92                 if ( strcasecmp( type, ptype ) != 0 ) {
93                         strncpy( ptype, type, sizeof(ptype) - 1 );
94                         nvals = 0;
95                         maxvals = 0;
96                         a = NULL;
97                 }
98
99                 if ( strcasecmp( type, "dn" ) == 0 ) {
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, value );
104                                 continue;
105                         }
106                         e->e_dn = ch_strdup( value );
107
108                         if ( e->e_ndn != NULL ) {
109                                 Debug( LDAP_DEBUG_ANY,
110  "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
111                                     e->e_id, e->e_ndn, value );
112                                 free( e->e_ndn );
113                         }
114                         e->e_ndn = ch_strdup( value );
115                         (void) dn_normalize_case( e->e_ndn );
116                         continue;
117                 }
118
119                 bval.bv_val = value;
120                 bval.bv_len = vlen;
121                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
122                     != 0 ) {
123                         Debug( LDAP_DEBUG_TRACE,
124                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
125                         entry_free( e );
126                         return( NULL );
127                 }
128                 nvals++;
129         }
130
131         /* check to make sure there was a dn: line */
132         if ( e->e_dn == NULL ) {
133                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
134                     e->e_id, 0, 0 );
135                 entry_free( e );
136                 return( NULL );
137         }
138
139         if ( e->e_ndn == NULL ) {
140                 Debug( LDAP_DEBUG_ANY,
141                         "str2entry: entry %ld (\"%s\") has no normalized dn\n",
142                     e->e_id, e->e_dn, 0 );
143                 entry_free( e );
144                 return( NULL );
145         }
146
147         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
148                 e->e_dn, e->e_id, (unsigned long)e );
149
150         return( e );
151 }
152
153 #define GRABSIZE        BUFSIZ
154
155 #define MAKE_SPACE( n ) { \
156                 while ( ecur + (n) > ebuf + emaxsize ) { \
157                         int     offset; \
158                         offset = (int) (ecur - ebuf); \
159                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
160                             emaxsize + GRABSIZE ); \
161                         emaxsize += GRABSIZE; \
162                         ecur = ebuf + offset; \
163                 } \
164 }
165
166 char *
167 entry2str(
168     Entry       *e,
169     int         *len,
170     int         printid
171 )
172 {
173         Attribute       *a;
174         struct berval   *bv;
175         int             i, tmplen;
176
177         /*
178          * In string format, an entry looks like this:
179          *      <id>\n
180          *      dn: <dn>\n
181          *      [<attr>: <value>\n]*
182          */
183
184         ecur = ebuf;
185
186         if ( printid ) {
187                 /* id + newline */
188                 MAKE_SPACE( 10 );
189                 sprintf( (char *) ecur, "%ld\n", e->e_id );
190                 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
191         }
192
193         /* put the dn */
194         if ( e->e_dn != NULL ) {
195                 /* put "dn: <dn>" */
196                 tmplen = strlen( e->e_dn );
197                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
198                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
199         }
200
201         /* put the attributes */
202         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
203                 /* put "<type>:[:] <value>" line for each value */
204                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
205                         bv = a->a_vals[i];
206                         tmplen = strlen( a->a_type );
207                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
208                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, a->a_type,
209                             bv->bv_val, bv->bv_len );
210                 }
211         }
212         MAKE_SPACE( 1 );
213         *ecur = '\0';
214         *len = ecur - ebuf;
215
216         return( (char *) ebuf );
217 }
218
219 void
220 entry_free( Entry *e )
221 {
222         Attribute       *a, *next;
223
224         if ( e->e_dn != NULL ) {
225                 free( e->e_dn );
226                 e->e_dn = NULL;
227         }
228         if ( e->e_ndn != NULL ) {
229                 free( e->e_ndn );
230                 e->e_ndn = NULL;
231         }
232         for ( a = e->e_attrs; a != NULL; a = next ) {
233                 next = a->a_next;
234                 attr_free( a );
235         }
236         e->e_attrs = NULL;
237         e->e_private = NULL;
238         free( e );
239 }
240
241 /*
242  * These routines are used only by Backend.
243  *
244  * the Entry has three entry points (ways to find things):
245  *
246  *      by entry        e.g., if you already have an entry from the cache
247  *                      and want to delete it. (really by entry ptr)
248  *      by dn           e.g., when looking for the base object of a search
249  *      by id           e.g., for search candidates
250  *
251  * these correspond to three different avl trees that are maintained.
252  */
253
254 int
255 entry_cmp( Entry *e1, Entry *e2 )
256 {
257         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
258 }
259
260 int
261 entry_dn_cmp( Entry *e1, Entry *e2 )
262 {
263         /* compare their normalized UPPERCASED dn's */
264         return( strcmp( e1->e_ndn, e2->e_ndn ) );
265 }
266
267 int
268 entry_id_cmp( Entry *e1, Entry *e2 )
269 {
270         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
271 }
272