]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
d5ee966b570ad152344a50176022a9083e5173df
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include "slap.h"
9
10 void    entry_free();
11 char    *entry2str();
12
13 static unsigned char    *ebuf;  /* buf returned by entry2str             */
14 static unsigned char    *ecur;  /* pointer to end of currently used ebuf */
15 static int              emaxsize;/* max size of ebuf                     */
16
17 Entry *
18 str2entry( char *s )
19 {
20         int             i;
21         Entry           *e;
22         Attribute       **a;
23         char            *type;
24         char            *value;
25         char            *next;
26         int             vlen, nvals, maxvals;
27         struct berval   bval;
28         struct berval   *vals[2];
29         char            ptype[64];
30
31         /*
32          * In string format, an entry looks like this:
33          *
34          *      <id>\n
35          *      dn: <dn>\n
36          *      [<attr>:[:] <value>\n]
37          *      [<tab><continuedvalue>\n]*
38          *      ...
39          *
40          * If a double colon is used after a type, it means the
41          * following value is encoded as a base 64 string.  This
42          * happens if the value contains a non-printing character
43          * or newline.
44          */
45
46         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n", s, 0, 0 );
47
48         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
49
50         /* check to see if there's an id included */
51         next = s;
52         if ( isdigit( *s ) ) {
53                 e->e_id = atoi( s );
54                 if ( (s = str_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         /* dn + attributes */
63         e->e_attrs = NULL;
64         vals[0] = &bval;
65         vals[1] = NULL;
66         ptype[0] = '\0';
67         while ( (s = str_getline( &next )) != NULL ) {
68                 if ( *s == '\n' || *s == '\0' ) {
69                         break;
70                 }
71
72                 if ( str_parse_line( s, &type, &value, &vlen ) != 0 ) {
73                         Debug( LDAP_DEBUG_TRACE,
74                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
75                         continue;
76                 }
77
78                 if ( strcasecmp( type, ptype ) != 0 ) {
79                         strncpy( ptype, type, sizeof(ptype) - 1 );
80                         nvals = 0;
81                         maxvals = 0;
82                         a = NULL;
83                 }
84                 if ( strcasecmp( type, "dn" ) == 0 ) {
85                         if ( e->e_dn != NULL ) {
86                                 Debug( LDAP_DEBUG_ANY,
87     "str2entry: entry %d has multiple dns \"%s\" and \"%s\" (second ignored)\n",
88                                     e->e_id, e->e_dn, value );
89                                 continue;
90                         }
91                         e->e_dn = strdup( value );
92                         continue;
93                 }
94
95                 bval.bv_val = value;
96                 bval.bv_len = vlen;
97                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
98                     != 0 ) {
99                         Debug( LDAP_DEBUG_TRACE,
100                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
101                         return( NULL );
102                 }
103                 nvals++;
104         }
105
106         /* check to make sure there was a dn: line */
107         if ( e->e_dn == NULL ) {
108                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %d has no dn\n",
109                     e->e_id, 0, 0 );
110                 entry_free( e );
111                 return( NULL );
112         }
113
114         Debug( LDAP_DEBUG_TRACE, "<= str2entry 0x%x\n", e, 0, 0 );
115         return( e );
116 }
117
118 #define GRABSIZE        BUFSIZ
119
120 #define MAKE_SPACE( n ) { \
121                 while ( ecur + (n) > ebuf + emaxsize ) { \
122                         int     offset; \
123                         offset = (int) (ecur - ebuf); \
124                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
125                             emaxsize + GRABSIZE ); \
126                         emaxsize += GRABSIZE; \
127                         ecur = ebuf + offset; \
128                 } \
129 }
130
131 char *
132 entry2str(
133     Entry       *e,
134     int         *len,
135     int         printid
136 )
137 {
138         Attribute       *a;
139         struct berval   *bv;
140         int             i, tmplen;
141
142         /*
143          * In string format, an entry looks like this:
144          *      <id>\n
145          *      dn: <dn>\n
146          *      [<attr>: <value>\n]*
147          */
148
149         ecur = ebuf;
150
151         if ( printid ) {
152                 /* id + newline */
153                 MAKE_SPACE( 10 );
154                 sprintf( (char *) ecur, "%ld\n", e->e_id );
155                 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
156         }
157
158         /* put the dn */
159         if ( e->e_dn != NULL ) {
160                 /* put "dn: <dn>" */
161                 tmplen = strlen( e->e_dn );
162                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
163                 put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
164         }
165
166         /* put the attributes */
167         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
168                 /* put "<type>:[:] <value>" line for each value */
169                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
170                         bv = a->a_vals[i];
171                         tmplen = strlen( a->a_type );
172                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
173                         put_type_and_value( (char **) &ecur, a->a_type,
174                             bv->bv_val, bv->bv_len );
175                 }
176         }
177         MAKE_SPACE( 1 );
178         *ecur = '\0';
179         *len = ecur - ebuf;
180
181         return( (char *) ebuf );
182 }
183
184 void
185 entry_free( Entry *e )
186 {
187         int             i;
188         Attribute       *a, *next;
189
190         if ( e->e_dn != NULL ) {
191                 free( e->e_dn );
192         }
193         for ( a = e->e_attrs; a != NULL; a = next ) {
194                 next = a->a_next;
195                 attr_free( a );
196         }
197         free( e );
198 }