]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Fixed exit code processing. passwd.c never committed its password change
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 #include "ldif.h"
19
20 static unsigned char    *ebuf;  /* buf returned by entry2str             */
21 static unsigned char    *ecur;  /* pointer to end of currently used ebuf */
22 static int              emaxsize;/* max size of ebuf                     */
23
24 /*
25  * Empty root entry
26  */
27 const Entry slap_entry_root = { NOID, "", "", NULL, NULL };
28
29 int entry_destroy(void)
30 {
31         free( ebuf );
32         ebuf = NULL;
33         ecur = NULL;
34         emaxsize = 0;
35         return 0;
36 }
37
38
39 Entry *
40 str2entry( char *s )
41 {
42         int rc;
43         Entry           *e;
44         char            *type;
45         struct berval value;
46         struct berval   *vals[2];
47         AttributeDescription *ad;
48         const char *text;
49         char    *next;
50
51         /*
52          * LDIF is used as the string format.
53          * An entry looks like this:
54          *
55          *      dn: <dn>\n
56          *      [<attr>:[:] <value>\n]
57          *      [<tab><continuedvalue>\n]*
58          *      ...
59          *
60          * If a double colon is used after a type, it means the
61          * following value is encoded as a base 64 string.  This
62          * happens if the value contains a non-printing character
63          * or newline.
64          */
65
66 #ifdef NEW_LOGGING
67         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
68                    "str2entry: \"%s\"\n", s ? s : "NULL" ));
69 #else
70         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
71                 s ? s : "NULL", 0, 0 );
72 #endif
73
74         /* initialize reader/writer lock */
75         e = (Entry *) ch_malloc( sizeof(Entry) );
76
77         if( e == NULL ) {
78 #ifdef NEW_LOGGING
79                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
80                            "str2entry: entry allocation failed.\n" ));
81 #else
82                 Debug( LDAP_DEBUG_ANY,
83                     "<= str2entry NULL (entry allocation failed)\n",
84                     0, 0, 0 );
85 #endif
86                 return( NULL );
87         }
88
89         /* initialize entry */
90         e->e_id = NOID;
91         e->e_dn = NULL;
92         e->e_ndn = NULL;
93         e->e_attrs = NULL;
94         e->e_private = NULL;
95
96         /* dn + attributes */
97         vals[0] = &value;
98         vals[1] = NULL;
99
100         next = s;
101         while ( (s = ldif_getline( &next )) != NULL ) {
102                 if ( *s == '\n' || *s == '\0' ) {
103                         break;
104                 }
105
106                 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
107 #ifdef NEW_LOGGING
108                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
109                                    "str2entry:  NULL (parse_line)\n" ));
110 #else
111                         Debug( LDAP_DEBUG_TRACE,
112                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
113 #endif
114                         continue;
115                 }
116
117                 if ( strcasecmp( type, "dn" ) == 0 ) {
118                         free( type );
119
120                         if ( e->e_dn != NULL ) {
121 #ifdef NEW_LOGGING
122                                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1, "str2entry: "
123                                         "entry %ld has multiple dns \"%s\" and \"%s\" "
124                                         "(second ignored)\n",
125                                         (long) e->e_id, e->e_dn, value.bv_val != NULL ? value.bv_val : "" ));
126 #else
127                                 Debug( LDAP_DEBUG_ANY, "str2entry: "
128                                         "entry %ld has multiple dns \"%s\" and \"%s\" "
129                                         "(second ignored)\n",
130                                     (long) e->e_id, e->e_dn,
131                                         value.bv_val != NULL ? value.bv_val : "" );
132 #endif
133                                 if( value.bv_val != NULL ) free( value.bv_val );
134                                 continue;
135                         }
136
137                         e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
138                         continue;
139                 }
140
141                 ad = NULL;
142                 rc = slap_str2ad( type, &ad, &text );
143
144                 if( rc != LDAP_SUCCESS ) {
145 #ifdef NEW_LOGGING
146                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
147                                    "str2entry:  str2ad(%s):      %s\n", type, text ));
148 #else
149                         Debug( slapMode & SLAP_TOOL_MODE
150                                 ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
151                                 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
152 #endif
153                         if( slapMode & SLAP_TOOL_MODE ) {
154                                 entry_free( e );
155                                 free( value.bv_val );
156                                 free( type );
157                                 return NULL;
158                         }
159
160                         rc = slap_str2undef_ad( type, &ad, &text );
161
162                         if( rc != LDAP_SUCCESS ) {
163 #ifdef NEW_LOGGING
164                                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
165                                            "str2entry:  str2undef_ad(%s):  %s\n", type, text ));
166 #else
167                                 Debug( LDAP_DEBUG_ANY,
168                                         "<= str2entry: str2undef_ad(%s): %s\n",
169                                                 type, text, 0 );
170 #endif
171                                 entry_free( e );
172                                 free( value.bv_val );
173                                 free( type );
174                                 return NULL;
175                         }
176                 }
177
178                 if( slapMode & SLAP_TOOL_MODE ) {
179                         slap_syntax_validate_func *validate =
180                                 ad->ad_type->sat_syntax->ssyn_validate;
181
182                         if( !validate ) {
183 #ifdef NEW_LOGGING
184                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
185                                            "str2entry: no validator for syntax %s\n", 
186                                            ad->ad_type->sat_syntax->ssyn_oid ));
187 #else
188                                 Debug( LDAP_DEBUG_ANY,
189                                         "str2entry: no validator for syntax %s\n",
190                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
191 #endif
192                                 entry_free( e );
193                                 free( value.bv_val );
194                                 free( type );
195                                 return NULL;
196                         }
197
198                         /*
199                          * validate value per syntax
200                          */
201                         rc = validate( ad->ad_type->sat_syntax, &value );
202
203                         if( rc != 0 ) {
204 #ifdef NEW_LOGGING
205                                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
206                                            "str2entry:  invalid value for syntax %s\n",
207                                            ad->ad_type->sat_syntax->ssyn_oid ));
208 #else
209                                 Debug( LDAP_DEBUG_ANY,
210                                         "str2entry: invalid value for syntax %s\n",
211                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
212 #endif
213                                 entry_free( e );
214                                 free( value.bv_val );
215                                 free( type );
216                                 return NULL;
217                         }
218                 }
219
220                 rc = attr_merge( e, ad, vals );
221
222                 if( rc != 0 ) {
223 #ifdef NEW_LOGGING
224                         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
225                                    "str2entry:  NULL (attr_merge)\n" ));
226 #else
227                         Debug( LDAP_DEBUG_ANY,
228                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
229 #endif
230                         entry_free( e );
231                         free( value.bv_val );
232                         free( type );
233                         return( NULL );
234                 }
235
236                 free( type );
237                 free( value.bv_val );
238         }
239
240         /* check to make sure there was a dn: line */
241         if ( e->e_dn == NULL ) {
242 #ifdef NEW_LOGGING
243                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
244                            "str2entry:  entry %ld has no dn.\n",
245                                 (long) e->e_id ));
246 #else
247                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
248                     (long) e->e_id, 0, 0 );
249 #endif
250                 entry_free( e );
251                 return( NULL );
252         }
253
254         /* generate normalized dn */
255         e->e_ndn = ch_strdup( e->e_dn );
256         (void) dn_normalize( e->e_ndn );
257
258 #ifdef NEW_LOGGING
259         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
260                    "str2entry(%s) -> 0x%lx\n", e->e_dn, (unsigned long)e ));
261 #else
262         Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
263                 e->e_dn, (unsigned long) e, 0 );
264 #endif
265         return( e );
266 }
267
268
269 #define GRABSIZE        BUFSIZ
270
271 #define MAKE_SPACE( n ) { \
272                 while ( ecur + (n) > ebuf + emaxsize ) { \
273                         ptrdiff_t       offset; \
274                         offset = (int) (ecur - ebuf); \
275                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
276                             emaxsize + GRABSIZE ); \
277                         emaxsize += GRABSIZE; \
278                         ecur = ebuf + offset; \
279                 } \
280         }
281
282 char *
283 entry2str(
284     Entry       *e,
285     int         *len )
286 {
287         Attribute       *a;
288         struct berval   *bv;
289         int             i, tmplen;
290
291         /*
292          * In string format, an entry looks like this:
293          *      dn: <dn>\n
294          *      [<attr>: <value>\n]*
295          */
296
297         ecur = ebuf;
298
299         /* put the dn */
300         if ( e->e_dn != NULL ) {
301                 /* put "dn: <dn>" */
302                 tmplen = strlen( e->e_dn );
303                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
304                 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
305         }
306
307         /* put the attributes */
308         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
309                 /* put "<type>:[:] <value>" line for each value */
310                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
311                         bv = a->a_vals[i];
312                         tmplen = a->a_desc->ad_cname.bv_len;
313                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
314                         ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
315                                 a->a_desc->ad_cname.bv_val,
316                             bv->bv_val, bv->bv_len );
317                 }
318         }
319         MAKE_SPACE( 1 );
320         *ecur = '\0';
321         *len = ecur - ebuf;
322
323         return( (char *) ebuf );
324 }
325
326 void
327 entry_free( Entry *e )
328 {
329         /* free an entry structure */
330         assert( e != NULL );
331
332         /* e_private must be freed by the caller */
333         assert( e->e_private == NULL );
334         e->e_private = NULL;
335
336         /* free DNs */
337         if ( e->e_dn != NULL ) {
338                 free( e->e_dn );
339                 e->e_dn = NULL;
340         }
341         if ( e->e_ndn != NULL ) {
342                 free( e->e_ndn );
343                 e->e_ndn = NULL;
344         }
345
346         /* free attributes */
347         attrs_free( e->e_attrs );
348         e->e_attrs = NULL;
349
350         free( e );
351 }
352
353 /*
354  * These routines are used only by Backend.
355  *
356  * the Entry has three entry points (ways to find things):
357  *
358  *      by entry        e.g., if you already have an entry from the cache
359  *                      and want to delete it. (really by entry ptr)
360  *      by dn           e.g., when looking for the base object of a search
361  *      by id           e.g., for search candidates
362  *
363  * these correspond to three different avl trees that are maintained.
364  */
365
366 int
367 entry_cmp( Entry *e1, Entry *e2 )
368 {
369         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
370 }
371
372 int
373 entry_dn_cmp( Entry *e1, Entry *e2 )
374 {
375         /* compare their normalized UPPERCASED dn's */
376         return( strcmp( e1->e_ndn, e2->e_ndn ) );
377 }
378
379 int
380 entry_id_cmp( Entry *e1, Entry *e2 )
381 {
382         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
383 }
384
385 #ifdef SLAPD_BDB
386
387 /* Flatten an Entry into a buffer. The buffer contents become a direct
388  * copy of the entry, with all pointers converted to offsets from the
389  * beginning of the buffer. We do this by first walking through all
390  * the fields of the Entry, adding up their sizes. Then a single chunk
391  * of memory is malloc'd and the entry is copied. We differentiate between
392  * fixed size fields and variable-length content when tallying up the
393  * entry size, so that we can stick all of the variable-length stuff
394  * into the back half of the buffer.
395  */
396 int entry_encode(Entry *e, struct berval **bv)
397 {
398         int siz = sizeof(Entry);
399         int len, dnlen, ndnlen;
400         int i, j;
401         Entry *f;
402         Attribute *a, *b;
403         struct berval **bvl, *bz;
404         char *ptr, *base, *data;
405
406         *bv = ch_malloc(sizeof(struct berval));
407 #ifdef NEW_LOGGING
408         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
409                 "entry_encode: id: 0x%08lx  \"%s\"\n",
410                 (long) e->e_id, e->e_dn ));
411 #else
412         Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
413                 (long) e->e_id, e->e_dn, 0 );
414 #endif
415         dnlen = strlen(e->e_dn);
416         ndnlen = strlen(e->e_ndn);
417         len = dnlen + ndnlen + 2;       /* two trailing NUL bytes */
418         for (a=e->e_attrs; a; a=a->a_next) {
419                 /* For AttributeDesc, we only store the attr name */
420                 siz += sizeof(Attribute);
421                 len += a->a_desc->ad_cname.bv_len+1;
422                 for (i=0; a->a_vals[i]; i++) {
423                         siz += sizeof(struct berval *);
424                         siz += sizeof(struct berval);
425                         len += a->a_vals[i]->bv_len + 1;
426                 }
427                 siz += sizeof(struct berval *); /* NULL pointer at end */
428         }
429         (*bv)->bv_len = siz + len;
430         (*bv)->bv_val = ch_malloc(siz+len);
431         base = (*bv)->bv_val;
432         ptr = base + siz;
433         f = (Entry *)base;
434         data = (char *)(f+1);
435         f->e_id = e->e_id;
436         f->e_dn = (char *)(ptr-base);
437         memcpy(ptr, e->e_dn, dnlen);
438         ptr += dnlen;
439         *ptr++ = '\0';
440         f->e_ndn = (char *)(ptr-base);
441         memcpy(ptr, e->e_ndn, ndnlen);
442         ptr += ndnlen;
443         *ptr++ = '\0';
444         f->e_attrs = e->e_attrs ? (Attribute *)sizeof(Entry) : NULL;
445         f->e_private = NULL;
446         for (a=e->e_attrs; a; a=a->a_next) {
447                 b = (Attribute *)data;
448                 data = (char *)(b+1);
449                 b->a_desc = (AttributeDescription *)(ptr-base);
450                 memcpy(ptr, a->a_desc->ad_cname.bv_val,
451                         a->a_desc->ad_cname.bv_len);
452                 ptr += a->a_desc->ad_cname.bv_len;
453                 *ptr++ = '\0';
454                 if (a->a_vals) {
455                     bvl = (struct berval **)data;
456                     b->a_vals = (struct berval **)(data-base);
457                     for (i=0; a->a_vals[i]; i++);
458                     data = (char *)(bvl+i+1);
459                     bz = (struct berval *)data;
460                     for (j=0; j<i; j++) {
461                             bz->bv_len = a->a_vals[j]->bv_len;
462                             if (a->a_vals[j]->bv_val) {
463                                 bz->bv_val = (char *)(ptr-base);
464                                 memcpy(ptr, a->a_vals[j]->bv_val, bz->bv_len);
465                             } else {
466                                 bz->bv_val = NULL;
467                             }
468                             ptr += bz->bv_len;
469                             *ptr++ = '\0';
470                             bvl[j] = (struct berval *)(data-base);
471                             bz++;
472                             data = (char *)bz;
473                     }
474                     bvl[j] = NULL;
475                 } else {
476                     b->a_vals = NULL;
477                 }
478
479                 if (a->a_next)
480                     b->a_next = (Attribute *)(data-base);
481                 else
482                     b->a_next = NULL;
483         }
484         return 0;
485 }
486
487 /* Retrieve an Entry that was stored using entry_encode above.
488  * All we have to do is add the buffer address to all of the
489  * stored offsets. We also must lookup the stored attribute names
490  * to get AttributeDescriptions. To detect if the attributes of
491  * an Entry are later modified, we also store the address of the
492  * end of this block in e_private.
493  *
494  * Note: everything is stored in a single contiguous block, so
495  * you can not free individual attributes or names from this
496  * structure. Attempting to do so will likely corrupt memory.
497  */
498 int entry_decode(struct berval *bv, Entry **e)
499 {
500         int i;
501         long base;
502         Attribute *a;
503         Entry *x = (Entry *)bv->bv_val;
504         char *type;
505         const char *text;
506         AttributeDescription *ad;
507
508         base = (long)bv->bv_val;
509         x->e_dn += base;
510         x->e_ndn += base;
511 #ifdef NEW_LOGGING
512         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL2,
513                    "entry_decode: \"%s\"\n", x->e_dn ));
514 #else
515         Debug( LDAP_DEBUG_TRACE,
516             "entry_decode: \"%s\"\n",
517             x->e_dn, 0, 0 );
518 #endif
519         x->e_private = bv->bv_val + bv->bv_len;
520         if (x->e_attrs)
521                 x->e_attrs = (Attribute *)((long)x->e_attrs+base);
522         for (a=x->e_attrs; a; a=a->a_next) {
523                 if (a->a_next)
524                         a->a_next = (Attribute *)((long)a->a_next+base);
525                 ad = NULL;
526                 type = (char *)a->a_desc+base;
527                 i = slap_str2ad( type, &ad, &text );
528
529                 if( i != LDAP_SUCCESS ) {
530 #ifdef NEW_LOGGING
531                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
532                                    "entry_decode: str2ad(%s): %s\n", type, text ));
533 #else
534                         Debug( LDAP_DEBUG_TRACE,
535                                 "<= entry_decode: str2ad(%s): %s\n", type, text, 0 );
536 #endif
537                         i = slap_str2undef_ad( type, &ad, &text );
538
539                         if( i != LDAP_SUCCESS ) {
540 #ifdef NEW_LOGGING
541                                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
542                                            "entry_decode:  str2undef_ad(%s): %s\n", type, text));
543 #else
544                                 Debug( LDAP_DEBUG_ANY,
545                                         "<= entry_decode: str2undef_ad(%s): %s\n",
546                                                 type, text, 0 );
547 #endif
548                                 return i;
549                         }
550                 }
551                 a->a_desc = ad;
552                 if (a->a_vals) {
553                         a->a_vals = (struct berval **)((long)a->a_vals+base);
554                         for (i=0; a->a_vals[i]; i++) {
555                                 a->a_vals[i] = (struct berval *)
556                                         ((long)a->a_vals[i]+base);
557                                 if (a->a_vals[i]->bv_val)
558                                     a->a_vals[i]->bv_val += base;
559                         }
560                 }
561         }
562 #ifdef NEW_LOGGING
563         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
564                    "entry_decode:  %s\n", x->e_dn ));
565 #else
566         Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
567                 x->e_dn, 0, 0 );
568 #endif
569         *e = x;
570         return 0;
571 }
572 #endif