]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Exit loop after matching command is found in openldap_ldap_init_w_conf
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 Entry *
33 str2entry( char *s )
34 {
35         Entry           *e;
36         Attribute       **a = NULL;
37         char            *type;
38         char            *value;
39         char            *next;
40         ber_len_t       vlen;
41         int             nvals = 0;
42         int             maxvals = 0;
43         struct berval   bval;
44         struct berval   *vals[2];
45         char            ptype[64];
46
47         /*
48          * LDIF is used as the string format.
49          * An entry looks like this:
50          *
51          *      dn: <dn>\n
52          *      [<attr>:[:] <value>\n]
53          *      [<tab><continuedvalue>\n]*
54          *      ...
55          *
56          * If a double colon is used after a type, it means the
57          * following value is encoded as a base 64 string.  This
58          * happens if the value contains a non-printing character
59          * or newline.
60          */
61
62         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
63                 s ? s : "NULL", 0, 0 );
64
65         next = s;
66
67         /* initialize reader/writer lock */
68         e = (Entry *) ch_malloc( sizeof(Entry) );
69
70         if( e == NULL ) {
71                 Debug( LDAP_DEBUG_TRACE,
72                     "<= str2entry NULL (entry allocation failed)\n",
73                     0, 0, 0 );
74                 return( NULL );
75         }
76
77         /* initialize entry */
78         e->e_id = NOID;
79         e->e_dn = NULL;
80         e->e_ndn = NULL;
81         e->e_attrs = NULL;
82         e->e_private = NULL;
83
84         /* dn + attributes */
85         vals[0] = &bval;
86         vals[1] = NULL;
87         ptype[0] = '\0';
88
89         while ( (s = ldif_getline( &next )) != NULL ) {
90                 if ( *s == '\n' || *s == '\0' ) {
91                         break;
92                 }
93
94                 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
95                         Debug( LDAP_DEBUG_TRACE,
96                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
97                         continue;
98                 }
99
100                 if ( strcasecmp( type, ptype ) != 0 ) {
101                         strncpy( ptype, type, sizeof(ptype) - 1 );
102                         nvals = 0;
103                         maxvals = 0;
104                         a = NULL;
105                 }
106
107                 if ( strcasecmp( type, "dn" ) == 0 ) {
108                         free( type );
109
110                         if ( e->e_dn != NULL ) {
111                                 Debug( LDAP_DEBUG_ANY,
112  "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
113                                     e->e_id, e->e_dn,
114                                         value != NULL ? value : NULL );
115                                 if( value != NULL ) free( value );
116                                 continue;
117                         }
118                         e->e_dn = value != NULL ? value : ch_strdup( "" );
119                         continue;
120                 }
121
122                 bval.bv_val = value;
123                 bval.bv_len = vlen;
124 #ifdef SLAPD_SCHEMA_NOT_COMPAT
125                 /* not yet implemented */
126 #else
127                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
128                     != 0 ) {
129                         Debug( LDAP_DEBUG_TRACE,
130                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
131                         entry_free( e );
132                         free( value );
133                         free( type );
134                         return( NULL );
135                 }
136 #endif
137
138                 free( value );
139                 free( type );
140                 nvals++;
141         }
142
143         /* check to make sure there was a dn: line */
144         if ( e->e_dn == NULL ) {
145                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
146                     e->e_id, 0, 0 );
147                 entry_free( e );
148                 return( NULL );
149         }
150
151         /* generate normalized dn */
152         e->e_ndn = ch_strdup( e->e_dn );
153         (void) dn_normalize( e->e_ndn );
154
155         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
156                 e->e_dn, e->e_id, (unsigned long)e );
157
158         return( e );
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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
205                         tmplen = a->a_desc->ad_cname->bv_len;
206 #else
207                         tmplen = strlen( a->a_type );
208 #endif
209                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
210                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
211 #ifdef SLAPD_SCHEMA_NOT_COMPAT
212                                 a->a_desc->ad_cname->bv_val,
213 #else
214                                 a->a_type,
215 #endif
216                             bv->bv_val, bv->bv_len );
217                 }
218         }
219         MAKE_SPACE( 1 );
220         *ecur = '\0';
221         *len = ecur - ebuf;
222
223         return( (char *) ebuf );
224 }
225
226 void
227 entry_free( Entry *e )
228 {
229         Attribute       *a, *next;
230
231         if ( e->e_dn != NULL ) {
232                 free( e->e_dn );
233                 e->e_dn = NULL;
234         }
235         if ( e->e_ndn != NULL ) {
236                 free( e->e_ndn );
237                 e->e_ndn = NULL;
238         }
239         for ( a = e->e_attrs; a != NULL; a = next ) {
240                 next = a->a_next;
241                 attr_free( a );
242         }
243         e->e_attrs = NULL;
244         e->e_private = NULL;
245         free( e );
246 }
247
248 /*
249  * These routines are used only by Backend.
250  *
251  * the Entry has three entry points (ways to find things):
252  *
253  *      by entry        e.g., if you already have an entry from the cache
254  *                      and want to delete it. (really by entry ptr)
255  *      by dn           e.g., when looking for the base object of a search
256  *      by id           e.g., for search candidates
257  *
258  * these correspond to three different avl trees that are maintained.
259  */
260
261 int
262 entry_cmp( Entry *e1, Entry *e2 )
263 {
264         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
265 }
266
267 int
268 entry_dn_cmp( Entry *e1, Entry *e2 )
269 {
270         /* compare their normalized UPPERCASED dn's */
271         return( strcmp( e1->e_ndn, e2->e_ndn ) );
272 }
273
274 int
275 entry_id_cmp( Entry *e1, Entry *e2 )
276 {
277         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
278 }
279