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