]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Move LDBM-backend specific fields of Entry struct into a private struct.
[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( *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 %lu 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 %lu 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 = dn_normalize_case( ch_strdup( value ) );
114                         continue;
115                 }
116
117                 bval.bv_val = value;
118                 bval.bv_len = vlen;
119                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
120                     != 0 ) {
121                         Debug( LDAP_DEBUG_TRACE,
122                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
123                         entry_free( e );
124                         return( NULL );
125                 }
126                 nvals++;
127         }
128
129         /* check to make sure there was a dn: line */
130         if ( e->e_dn == NULL ) {
131                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %lu has no dn\n",
132                     e->e_id, 0, 0 );
133                 entry_free( e );
134                 return( NULL );
135         }
136
137         if ( e->e_ndn == NULL ) {
138                 Debug( LDAP_DEBUG_ANY,
139                         "str2entry: entry %lu (\"%s\") has no normalized dn\n",
140                     e->e_id, e->e_dn, 0 );
141                 entry_free( e );
142                 return( NULL );
143         }
144
145         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %lu (0x%lx)\n",
146                 e->e_dn, e->e_id, (unsigned long)e );
147
148         return( e );
149 }
150
151 #define GRABSIZE        BUFSIZ
152
153 #define MAKE_SPACE( n ) { \
154                 while ( ecur + (n) > ebuf + emaxsize ) { \
155                         int     offset; \
156                         offset = (int) (ecur - ebuf); \
157                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
158                             emaxsize + GRABSIZE ); \
159                         emaxsize += GRABSIZE; \
160                         ecur = ebuf + offset; \
161                 } \
162 }
163
164 char *
165 entry2str(
166     Entry       *e,
167     int         *len,
168     int         printid
169 )
170 {
171         Attribute       *a;
172         struct berval   *bv;
173         int             i, tmplen;
174
175         /*
176          * In string format, an entry looks like this:
177          *      <id>\n
178          *      dn: <dn>\n
179          *      [<attr>: <value>\n]*
180          */
181
182         ecur = ebuf;
183
184         if ( printid ) {
185                 /* id + newline */
186                 MAKE_SPACE( 10 );
187                 sprintf( (char *) ecur, "%ld\n", e->e_id );
188                 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
189         }
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_put_type_and_value( (char **) &ecur, "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                         tmplen = strlen( a->a_type );
205                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
206                         ldif_put_type_and_value( (char **) &ecur, a->a_type,
207                             bv->bv_val, bv->bv_len );
208                 }
209         }
210         MAKE_SPACE( 1 );
211         *ecur = '\0';
212         *len = ecur - ebuf;
213
214         return( (char *) ebuf );
215 }
216
217 void
218 entry_free( Entry *e )
219 {
220         int             i;
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