]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
Plug memleak in enum_tree
[openldap] / servers / slapd / back-ldif / ldif.c
1 /* ldif.c - the ldif backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Eric Stokes for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22 #include <stdio.h>
23 #include <ac/string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <ac/dirent.h>
27 #include <fcntl.h>
28 #include <ac/errno.h>
29 #include <ac/unistd.h>
30 #include "slap.h"
31 #include "lutil.h"
32 #include "config.h"
33
34 typedef struct enumCookie {
35         Operation *op;
36         SlapReply *rs;
37         Entry **entries;
38         int elen;
39         int eind;
40 } enumCookie;
41
42 struct ldif_info {
43         struct berval li_base_path;
44         enumCookie li_tool_cookie;
45         ID li_tool_current;
46         ldap_pvt_thread_mutex_t  li_mutex;
47 };
48
49 #ifdef _WIN32
50 #define mkdir(a,b)      mkdir(a)
51 #endif
52
53 #define LDIF    ".ldif"
54
55 #define IX_DNL  '{'
56 #define IX_DNR  '}'
57 #ifndef IX_FSL
58 #define IX_FSL  IX_DNL
59 #define IX_FSR  IX_DNR
60 #endif
61
62 #define ENTRY_BUFF_INCREMENT 500
63
64 static ConfigTable ldifcfg[] = {
65         { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
66                 (void *)offsetof(struct ldif_info, li_base_path),
67                 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
68                         "DESC 'Directory for database content' "
69                         "EQUALITY caseIgnoreMatch "
70                         "SYNTAX OMsDirectoryString )", NULL, NULL },
71         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
72                 NULL, NULL, NULL, NULL }
73 };
74
75 static ConfigOCs ldifocs[] = {
76         { "( OLcfgDbOc:2.1 "
77                 "NAME 'olcLdifConfig' "
78                 "DESC 'LDIF backend configuration' "
79                 "SUP olcDatabaseConfig "
80                 "MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
81         { NULL, 0, NULL }
82 };
83
84 static void
85 dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
86         struct berval *res)
87 {
88         char *ptr, *sep, *end;
89
90         res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
91         res->bv_val = ch_malloc( res->bv_len + 1 );
92         ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
93         *ptr++ = LDAP_DIRSEP[0];
94         ptr = lutil_strcopy( ptr, rootdn->bv_val );
95         end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
96         while ( end > dn->bv_val ) {
97                 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
98                 *ptr++ = LDAP_DIRSEP[0];
99                 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
100                 end = sep;
101         }
102         strcpy(ptr, LDIF);
103 #if IX_FSL != IX_DNL
104         ptr = res->bv_val;
105         while( ptr=strchr(ptr, IX_DNL) ) {
106                 *ptr++ = IX_FSL;
107                 ptr = strchr(ptr, IX_DNR);
108                 if ( ptr )
109                         *ptr++ = IX_FSR;
110                 else
111                         break;
112         }
113 #endif
114 }
115
116 static char * slurp_file(int fd) {
117         int read_chars_total = 0;
118         int read_chars = 0;
119         int entry_size;
120         char * entry;
121         char * entry_pos;
122         struct stat st;
123
124         fstat(fd, &st);
125         entry_size = st.st_size;
126         entry = ch_malloc( entry_size+1 );
127         entry_pos = entry;
128         
129         while(1) {
130                 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
131                 if(read_chars == -1) {
132                         SLAP_FREE(entry);
133                         return NULL;
134                 }
135                 if(read_chars == 0) {
136                         entry[read_chars_total] = '\0';
137                         break;
138                 }
139                 else {
140                         read_chars_total += read_chars;
141                         entry_pos += read_chars;
142                 }
143         }
144         return entry;
145 }
146
147 static int spew_file(int fd, char * spew, int len) {
148         int writeres = 0;
149         
150         while(len > 0) {
151                 writeres = write(fd, spew, len);
152                 if(writeres == -1) {
153                         perror("could not spew write");
154                         return -1;
155                 }
156                 else {
157                         spew += writeres;
158                         len -= writeres;
159                 }
160         }
161         return writeres;
162 }
163
164 static int spew_entry(Entry * e, struct berval * path) {
165         int rs;
166         int openres;
167         int spew_res;
168         int entry_length;
169         char * entry_as_string;
170
171         openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
172         if(openres == -1) {
173                 if(errno == ENOENT)
174                         rs = LDAP_NO_SUCH_OBJECT;
175                 else
176                         rs = LDAP_UNWILLING_TO_PERFORM;
177         }
178         else {
179                 struct berval rdn;
180                 int tmp;
181
182                 /* Only save the RDN onto disk */
183                 dnRdn( &e->e_name, &rdn );
184                 if ( rdn.bv_len != e->e_name.bv_len ) {
185                         e->e_name.bv_val[rdn.bv_len] = '\0';
186                         tmp = e->e_name.bv_len;
187                         e->e_name.bv_len = rdn.bv_len;
188                         rdn.bv_len = tmp;
189                 }
190
191                 entry_as_string = entry2str(e, &entry_length);
192
193                 /* Restore full DN */
194                 if ( rdn.bv_len != e->e_name.bv_len ) {
195                         e->e_name.bv_val[e->e_name.bv_len] = ',';
196                         e->e_name.bv_len = rdn.bv_len;
197                 }
198
199                 if(entry_as_string == NULL) {
200                         rs = LDAP_UNWILLING_TO_PERFORM;
201                         close(openres);
202                 }
203                 else {
204                         spew_res = spew_file(openres, entry_as_string, entry_length);
205                         close(openres);
206                         if(spew_res == -1)
207                                 rs = LDAP_UNWILLING_TO_PERFORM;
208                         else
209                                 rs = LDAP_SUCCESS;
210                 }
211         }
212         return rs;
213 }
214
215 static Entry * get_entry_for_fd(int fd,
216         struct berval *pdn,
217         struct berval *pndn)
218 {
219         char * entry = (char *) slurp_file(fd);
220         Entry * ldentry = NULL;
221         
222         /* error reading file */
223         if(entry == NULL) {
224                 goto return_value;
225         }
226
227         ldentry = str2entry(entry);
228         if ( ldentry ) {
229                 struct berval rdn;
230                 rdn = ldentry->e_name;
231                 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
232                 ch_free( rdn.bv_val );
233                 rdn = ldentry->e_nname;
234                 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
235                 ch_free( rdn.bv_val );
236         }
237
238  return_value:
239         if(fd != -1) {
240                 if(close(fd) != 0) {
241                         /* log error */
242                 }
243         }
244         if(entry != NULL)
245                 SLAP_FREE(entry);
246         return ldentry;
247 }
248
249 static Entry * get_entry(Operation *op, struct berval *base_path) {
250         struct berval path, pdn, pndn;
251         int fd;
252
253         dnParent(&op->o_req_dn, &pdn);
254         dnParent(&op->o_req_ndn, &pndn);
255         dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
256         fd = open(path.bv_val, O_RDONLY);
257         /* error opening file (mebbe should log error) */
258         if(fd == -1) {
259                 perror("failed to open file");
260         }
261
262         if(path.bv_val != NULL)
263                 SLAP_FREE(path.bv_val);
264
265         if ( fd != -1 ) {
266                 return get_entry_for_fd(fd, &pdn, &pndn);
267         }
268
269         return NULL;
270 }
271
272 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
273         char *ptr;
274         res->bv_len = name->bv_len + base->bv_len + 1;
275         res->bv_val = ch_malloc( res->bv_len + 1 );
276         strcpy(res->bv_val, base->bv_val);
277         ptr = res->bv_val + base->bv_len;
278         *ptr++ = LDAP_DIRSEP[0];
279         strcpy(ptr, name->bv_val);
280 }
281
282 typedef struct bvlist {
283         struct bvlist *next;
284         struct berval bv;
285         struct berval num;
286         unsigned int inum;
287         int off;
288 } bvlist;
289
290
291 static int r_enum_tree(enumCookie *ck, struct berval *path,
292         struct berval *pdn, struct berval *pndn)
293 {
294         Entry *e;
295         int fd, rc = LDAP_SUCCESS;
296
297         fd = open( path->bv_val, O_RDONLY );
298         if ( fd < 0 ) {
299                 Debug( LDAP_DEBUG_TRACE,
300                         "=> ldif_enum_tree: failed to open %s\n",
301                         path->bv_val, 0, 0 );
302                 return LDAP_NO_SUCH_OBJECT;
303         }
304
305         e = get_entry_for_fd(fd, pdn, pndn);
306         if ( !e ) {
307                 Debug( LDAP_DEBUG_ANY,
308                         "=> ldif_enum_tree: failed to read entry for %s\n",
309                         path->bv_val, 0, 0 );
310                 return LDAP_BUSY;
311         }
312
313         if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
314                 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
315                 /* Send right away? */
316                 if ( ck->rs ) {
317                         /*
318                          * if it's a referral, add it to the list of referrals. only do
319                          * this for non-base searches, and don't check the filter
320                          * explicitly here since it's only a candidate anyway.
321                          */
322                         if ( !get_manageDSAit( ck->op )
323                                         && ck->op->ors_scope != LDAP_SCOPE_BASE
324                                         && is_entry_referral( e ) )
325                         {
326                                 BerVarray erefs = get_entry_referrals( ck->op, e );
327                                 ck->rs->sr_ref = referral_rewrite( erefs,
328                                                 &e->e_name, NULL,
329                                                 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
330                                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
331
332                                 ck->rs->sr_entry = e;
333                                 rc = send_search_reference( ck->op, ck->rs );
334                                 ber_bvarray_free( ck->rs->sr_ref );
335                                 ber_bvarray_free( erefs );
336                                 ck->rs->sr_ref = NULL;
337                                 ck->rs->sr_entry = NULL;
338
339                         } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
340                         {
341                                 ck->rs->sr_entry = e;
342                                 ck->rs->sr_attrs = ck->op->ors_attrs;
343                                 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
344                                 rc = send_search_entry(ck->op, ck->rs);
345                                 ck->rs->sr_entry = NULL;
346                         }
347                         fd = 1;
348                         if ( rc )
349                                 goto leave;
350                 } else {
351                 /* Queueing up for tool mode */
352                         if(ck->entries == NULL) {
353                                 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
354                                 ck->elen = ENTRY_BUFF_INCREMENT;
355                         }
356                         if(ck->eind >= ck->elen) { /* grow entries if necessary */      
357                                 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
358                                 ck->elen *= 2;
359                         }
360
361                         ck->entries[ck->eind++] = e;
362                         fd = 0;
363                 }
364         } else {
365                 fd = 1;
366         }
367
368         if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
369                 DIR * dir_of_path;
370                 bvlist *list = NULL, *ptr;
371
372                 path->bv_len -= STRLENOF( LDIF );
373                 path->bv_val[path->bv_len] = '\0';
374
375                 dir_of_path = opendir(path->bv_val);
376                 if(dir_of_path == NULL) { /* can't open directory */
377                         if ( errno != ENOENT ) {
378                                 /* it shouldn't be treated as an error
379                                  * only if the directory doesn't exist */
380                                 rc = LDAP_BUSY;
381                                 Debug( LDAP_DEBUG_TRACE,
382                                         "=> ldif_enum_tree: failed to opendir %s (%d)\n",
383                                         path->bv_val, errno, 0 );
384                         }
385                         goto leave;
386                 }
387         
388                 while(1) {
389                         struct berval fname, itmp;
390                         struct dirent * dir;
391                         bvlist *bvl, **prev;
392
393                         dir = readdir(dir_of_path);
394                         if(dir == NULL) break; /* end of the directory */
395                         fname.bv_len = strlen( dir->d_name );
396                         if ( fname.bv_len <= STRLENOF( LDIF ))
397                                 continue;
398                         if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
399                                 continue;
400                         fname.bv_val = dir->d_name;
401
402                         bvl = ch_malloc( sizeof(bvlist) );
403                         ber_dupbv( &bvl->bv, &fname );
404                         BER_BVZERO( &bvl->num );
405                         itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
406                         if ( itmp.bv_val ) {
407                                 char *ptr;
408                                 itmp.bv_val++;
409                                 ptr = strchr( itmp.bv_val, IX_FSR );
410                                 if ( ptr ) {
411                                         itmp.bv_len = ptr - itmp.bv_val;
412                                         ber_dupbv( &bvl->num, &itmp );
413                                         bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
414                                         itmp.bv_val[0] = '\0';
415                                         bvl->off = itmp.bv_val - bvl->bv.bv_val;
416                                 }
417                         }
418
419                         for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
420                                 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
421                                 if ( !cmp && bvl->num.bv_val )
422                                         cmp = bvl->inum - ptr->inum;
423                                 if ( cmp < 0 )
424                                         break;
425                         }
426                         *prev = bvl;
427                         bvl->next = ptr;
428                                 
429                 }
430                 closedir(dir_of_path);
431
432                 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
433                         ck->op->ors_scope = LDAP_SCOPE_BASE;
434                 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
435                         ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
436
437                 while ( ( ptr = list ) ) {
438                         struct berval fpath;
439
440                         list = ptr->next;
441
442                         if ( rc == LDAP_SUCCESS ) {
443                                 if ( ptr->num.bv_val )
444                                         AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
445                                                 ptr->num.bv_len );
446                                 fullpath( path, &ptr->bv, &fpath );
447                                 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
448                                 free(fpath.bv_val);
449                         }
450                         if ( ptr->num.bv_val )
451                                 free( ptr->num.bv_val );
452                         free(ptr->bv.bv_val);
453                         free(ptr);
454                 }
455         }
456 leave:
457         if ( fd ) entry_free( e );
458         return rc;
459 }
460
461 static int
462 enum_tree(
463         enumCookie *ck
464 )
465 {
466         struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
467         struct berval path;
468         struct berval pdn, pndn;
469         int rc;
470
471         dnParent( &ck->op->o_req_dn, &pdn );
472         dnParent( &ck->op->o_req_ndn, &pndn );
473         dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
474         rc = r_enum_tree(ck, &path, &pdn, &pndn);
475         ch_free( path.bv_val );
476         return rc;
477 }
478
479 /* Get the parent path plus the LDIF suffix */
480 static void get_parent_path(struct berval * dnpath, struct berval *res) {
481         int dnpathlen = dnpath->bv_len;
482         int i;
483         
484         for(i = dnpathlen;i>0;i--) /* find the first path seperator */
485                 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
486                         break;
487         res->bv_len = i;
488         res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
489         strncpy(res->bv_val, dnpath->bv_val, i);
490         strcpy(res->bv_val+i, LDIF);
491         res->bv_val[i] = '\0';
492 }
493
494 static int apply_modify_to_entry(Entry * entry,
495                                 Modifications * modlist,
496                                 Operation * op,
497                                 SlapReply * rs)
498 {
499         char textbuf[SLAP_TEXT_BUFLEN];
500         int rc = LDAP_UNWILLING_TO_PERFORM;
501         Modification *mods = NULL;
502
503         if (!acl_check_modlist(op, entry, modlist)) {
504                 return LDAP_INSUFFICIENT_ACCESS;
505         }
506
507         for (; modlist != NULL; modlist = modlist->sml_next) {
508                 mods = &modlist->sml_mod;
509
510                 switch (mods->sm_op) {
511                 case LDAP_MOD_ADD:
512                         rc = modify_add_values(entry, mods,
513                                    get_permissiveModify(op),
514                                    &rs->sr_text, textbuf,
515                                    sizeof( textbuf ) );
516                         break;
517                                 
518                 case LDAP_MOD_DELETE:
519                         rc = modify_delete_values(entry, mods,
520                                 get_permissiveModify(op),
521                                 &rs->sr_text, textbuf,
522                                 sizeof( textbuf ) );
523
524                         break;
525                                 
526                 case LDAP_MOD_REPLACE:
527                         rc = modify_replace_values(entry, mods,
528                                  get_permissiveModify(op),
529                                  &rs->sr_text, textbuf,
530                                  sizeof( textbuf ) );
531
532                         break;
533                 case LDAP_MOD_INCREMENT:
534                         break;
535                 case SLAP_MOD_SOFTADD:
536                         mods->sm_op = LDAP_MOD_ADD;
537                         rc = modify_add_values(entry, mods,
538                                    get_permissiveModify(op),
539                                    &rs->sr_text, textbuf,
540                                    sizeof( textbuf ) );
541                         mods->sm_op = SLAP_MOD_SOFTADD;
542                         if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
543                                 rc = LDAP_SUCCESS;
544                         }
545                         break;
546                 default:
547                         break;
548                 }
549                 if(rc != LDAP_SUCCESS) break;
550         }
551         
552         if(rc == LDAP_SUCCESS) {
553                 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
554                         entry->e_ocflags = 0;
555                 }
556                 /* check that the entry still obeys the schema */
557                 rc = entry_schema_check(op->o_bd, entry, NULL, 0,
558                           &rs->sr_text, textbuf, sizeof( textbuf ) );
559         }
560         return rc;
561 }
562
563 int
564 ldif_back_referrals( Operation *op, SlapReply *rs )
565 {
566         struct ldif_info        *ni = NULL;
567         Entry                   *entry;
568         int                     rc = LDAP_SUCCESS;
569
570 #if 0
571         if ( op->o_tag == LDAP_REQ_SEARCH ) {
572                 /* let search take care of itself */
573                 return rc;
574         }
575 #endif
576
577         if ( get_manageDSAit( op ) ) {
578                 /* let op take care of DSA management */
579                 return rc;
580         }
581
582         ni = (struct ldif_info *)op->o_bd->be_private;
583         ldap_pvt_thread_mutex_lock( &ni->li_mutex );
584         entry = (Entry *)get_entry( op, &ni->li_base_path );
585
586         /* no object is found for them */
587         if ( entry == NULL ) {
588                 struct berval   odn = op->o_req_dn;
589                 struct berval   ondn = op->o_req_ndn;
590
591                 struct berval   pndn = op->o_req_ndn;
592
593                 for ( ; entry == NULL; ) {
594                         dnParent( &pndn, &pndn );
595                         
596                         if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
597                                 break;
598                         }
599
600                         op->o_req_dn = pndn;
601                         op->o_req_ndn = pndn;
602
603                         entry = (Entry *)get_entry( op, &ni->li_base_path );
604                 }
605
606                 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
607
608                 op->o_req_dn = odn;
609                 op->o_req_ndn = ondn;
610
611                 rc = LDAP_SUCCESS;
612                 rs->sr_matched = NULL;
613                 if ( entry != NULL ) {
614                         Debug( LDAP_DEBUG_TRACE,
615                                 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
616                                 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
617
618                         if ( is_entry_referral( entry ) ) {
619                                 rc = LDAP_OTHER;
620                                 rs->sr_ref = get_entry_referrals( op, entry );
621                                 if ( rs->sr_ref ) {
622                                         rs->sr_matched = ber_strdup_x(
623                                         entry->e_name.bv_val, op->o_tmpmemctx );
624                                 }
625                         }
626
627                         entry_free(entry);
628
629                 } else if ( default_referral != NULL ) {
630                         rc = LDAP_OTHER;
631                         rs->sr_ref = referral_rewrite( default_referral,
632                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
633                 }
634
635                 if ( rs->sr_ref != NULL ) {
636                         /* send referrals */
637                         rc = rs->sr_err = LDAP_REFERRAL;
638                         send_ldap_result( op, rs );
639                         ber_bvarray_free( rs->sr_ref );
640                         rs->sr_ref = NULL;
641
642                 } else if ( rc != LDAP_SUCCESS ) {
643                         rs->sr_err = rc;
644                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
645                         send_ldap_result( op, rs );
646                 }
647
648                 if ( rs->sr_matched ) {
649                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
650                         rs->sr_matched = NULL;
651                 }
652
653                 return rc;
654         }
655
656         ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
657
658         if ( is_entry_referral( entry ) ) {
659                 /* entry is a referral */
660                 BerVarray refs = get_entry_referrals( op, entry );
661                 rs->sr_ref = referral_rewrite(
662                         refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
663
664                 Debug( LDAP_DEBUG_TRACE,
665                         "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
666                         (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
667
668                 rs->sr_matched = entry->e_name.bv_val;
669                 if ( rs->sr_ref != NULL ) {
670                         rc = rs->sr_err = LDAP_REFERRAL;
671                         send_ldap_result( op, rs );
672                         ber_bvarray_free( rs->sr_ref );
673                         rs->sr_ref = NULL;
674
675                 } else {
676                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
677                         rc = rs->sr_err;
678                 }
679
680                 rs->sr_matched = NULL;
681                 ber_bvarray_free( refs );
682
683                 entry_free( entry );
684         }
685
686         return rc;
687 }
688
689 static int
690 ldif_back_bind( Operation *op, SlapReply *rs )
691 {
692         struct ldif_info *ni = NULL;
693         Attribute * a = NULL;
694         AttributeDescription *password = slap_schema.si_ad_userPassword;
695         int return_val = 0;
696         Entry * entry = NULL;
697
698         ni = (struct ldif_info *) op->o_bd->be_private;
699         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
700         entry = (Entry *) get_entry(op, &ni->li_base_path);
701
702         /* no object is found for them */
703         if(entry == NULL) {
704                 if(be_isroot_pw(op)) {
705                         rs->sr_err = return_val = LDAP_SUCCESS;
706                 } else {
707                         rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
708                 }
709                 goto return_result;
710         }
711
712         /* they don't have userpassword */
713         if((a = attr_find(entry->e_attrs, password)) == NULL) {
714                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
715                 return_val = 1;
716                 goto return_result;
717         }
718
719         /* authentication actually failed */
720         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
721                              &rs->sr_text) != 0) {
722                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
723                 return_val = 1;
724                 goto return_result;
725         }
726
727         /* let the front-end send success */
728         return_val = 0;
729         goto return_result;
730
731  return_result:
732         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
733         if(return_val != 0)
734                 send_ldap_result( op, rs );
735         if(entry != NULL)
736                 entry_free(entry);
737         return return_val;
738 }
739
740 static int ldif_back_search(Operation *op, SlapReply *rs)
741 {
742         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
743         enumCookie ck = {0};
744
745         ck.op = op;
746         ck.rs = rs;
747         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
748         rs->sr_err = enum_tree( &ck );
749         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
750         send_ldap_result(op, rs);
751
752         return rs->sr_err;
753 }
754
755 static int ldif_back_add(Operation *op, SlapReply *rs) {
756         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
757         Entry * e = op->ora_e;
758         struct berval dn = e->e_nname;
759         struct berval leaf_path = BER_BVNULL;
760         struct stat stats;
761         int statres;
762         char textbuf[SLAP_TEXT_BUFLEN];
763
764         rs->sr_err = entry_schema_check(op->o_bd, e, NULL, 0,
765                 &rs->sr_text, textbuf, sizeof( textbuf ) );
766         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
767                                 
768         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
769
770         dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
771
772         if(leaf_path.bv_val != NULL) {
773                 struct berval base = BER_BVNULL;
774                 /* build path to container and ldif of container */
775                 get_parent_path(&leaf_path, &base);
776
777                 statres = stat(base.bv_val, &stats); /* check if container exists */
778                 if(statres == -1 && errno == ENOENT) { /* container missing */
779                         base.bv_val[base.bv_len] = '.';
780                         statres = stat(base.bv_val, &stats); /* check for leaf node */
781                         base.bv_val[base.bv_len] = '\0';
782                         if(statres == -1 && errno == ENOENT) {
783                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
784                         }
785                         else if(statres != -1) { /* create parent */
786                                 int mkdirres = mkdir(base.bv_val, 0750);
787                                 if(mkdirres == -1) {
788                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
789                                 }
790                         }
791                         else
792                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
793                 }/* container was possibly created, move on to add the entry */
794                 if(rs->sr_err == LDAP_SUCCESS) {
795                         statres = stat(leaf_path.bv_val, &stats);
796                         if(statres == -1 && errno == ENOENT) {
797                                 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
798                                 rs->sr_err = (int) spew_entry(e, &leaf_path);
799                                 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
800                         }
801                         else /* it already exists */
802                                 rs->sr_err = LDAP_ALREADY_EXISTS;
803                 }
804                 SLAP_FREE(base.bv_val);
805                 SLAP_FREE(leaf_path.bv_val);
806         }
807
808         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
809
810 send_res:
811         send_ldap_result(op, rs);
812         return 0;
813 }
814
815 static int ldif_back_modify(Operation *op, SlapReply *rs) {
816         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
817         Modifications * modlst = op->orm_modlist;
818         struct berval path = BER_BVNULL;
819         Entry * entry = NULL;
820         int spew_res;
821
822         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
823         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
824                 &path);
825         entry = (Entry *) get_entry(op, &ni->li_base_path);
826
827         if(entry != NULL) {
828                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
829                 if(rs->sr_err == LDAP_SUCCESS) {
830                         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
831                         spew_res = spew_entry(entry, &path);
832                         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
833                         if(spew_res == -1) {
834                                 perror("could not output entry");
835                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
836                         }
837                 }
838         }
839         else {
840                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
841         }
842         
843         if(entry != NULL)
844                 entry_free(entry);
845         if(path.bv_val != NULL)
846                 SLAP_FREE(path.bv_val);
847         rs->sr_text = NULL;
848         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
849         send_ldap_result(op, rs);
850         return 0;
851 }
852
853 static int ldif_back_delete(Operation *op, SlapReply *rs) {
854         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
855         struct berval path = BER_BVNULL;
856         int res = 0;
857
858         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
859         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
860
861         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
862         res = rmdir(path.bv_val);
863         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
864         if ( res && errno != ENOENT ) {
865                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
866         } else {
867                 res = unlink(path.bv_val);
868         }
869
870         if(res == -1) {
871                 if(errno == ENOENT)
872                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
873                 else
874                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
875         }
876         else
877                 rs->sr_err = LDAP_SUCCESS;
878
879         SLAP_FREE(path.bv_val);
880         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
881         send_ldap_result(op, rs);
882         return 0;
883 }
884
885
886 static int move_entry(Entry * entry, struct berval * ndn,
887                            struct berval * newndn, struct berval * rootdn,
888                            struct berval * base_path) {
889         int res;
890         int exists_res;
891         struct berval path;
892         struct berval newpath;
893
894         dn2path(ndn, rootdn, base_path, &path);
895         dn2path(newndn, rootdn, base_path, &newpath);
896
897         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
898                 /* some object doesn't exist */
899                 res = LDAP_NO_SUCH_OBJECT;
900         }
901         else { /* do the modrdn */
902                 exists_res = open(newpath.bv_val, O_RDONLY);
903                 if(exists_res == -1 && errno == ENOENT) {
904                         res = spew_entry(entry, &newpath);
905                         if(res != -1) {
906                                 /* if this fails we should log something bad */
907                                 res = unlink(path.bv_val);
908                                 res = LDAP_SUCCESS;
909                         }
910                         else {
911                                 if(errno == ENOENT)
912                                         res = LDAP_NO_SUCH_OBJECT;
913                                 else
914                                         res = LDAP_UNWILLING_TO_PERFORM;
915                                 unlink(newpath.bv_val); /* in case file was created */
916                         }
917                 }
918                 else if(exists_res) {
919                         int close_res = close(exists_res);
920                         res = LDAP_ALREADY_EXISTS;
921                         if(close_res == -1) {
922                         /* log heinous error */
923                         }
924                 }
925                 else {
926                         res = LDAP_UNWILLING_TO_PERFORM;
927                 }
928         }
929
930         if(newpath.bv_val != NULL)
931                 SLAP_FREE(newpath.bv_val);
932         if(path.bv_val != NULL)
933                 SLAP_FREE(path.bv_val);
934         return res;
935 }
936
937 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
938         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
939         struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
940         struct berval p_dn, bv = BER_BVNULL;
941         Entry * entry = NULL;
942         LDAPRDN new_rdn = NULL;
943         LDAPRDN old_rdn = NULL;
944         Modifications * mods = NULL;
945         int res;
946
947         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
948         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
949         entry = (Entry *) get_entry(op, &ni->li_base_path);
950
951         /* build the mods to the entry */
952         if(entry != NULL) {
953                 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
954                         (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
955                         rs->sr_err = LDAP_INVALID_DN_SYNTAX;
956                 }
957                 else if(op->oq_modrdn.rs_deleteoldrdn &&
958                         ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
959                         LDAP_DN_FORMAT_LDAP)) {
960                         rs->sr_err = LDAP_OTHER;
961                 }
962                 else { /* got both rdns successfully, ready to build mods */
963                         if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
964                                 != LDAP_SUCCESS) {
965                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
966                         }
967                         else { /* built mods successfully */
968
969                                 /* build new dn, and new ndn for the entry */
970                                 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
971                                         p_dn = *op->oq_modrdn.rs_newSup;
972                                 else
973                                         p_dn = slap_empty_bv;
974                                 dnParent(&entry->e_name, &p_dn);
975                                 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL); 
976                                 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
977                                 ber_dupbv( &new_ndn, &bv );
978                                 entry->e_name = new_dn;
979                                 entry->e_nname = new_ndn;
980
981                                 /* perform the modifications */
982                                 res = apply_modify_to_entry(entry, mods, op, rs);
983                                 if(res == LDAP_SUCCESS) {
984                                         rs->sr_err = move_entry(entry, &op->o_req_ndn,
985                                                                 &new_ndn,
986                                                                 &op->o_bd->be_nsuffix[0],
987                                                                 &ni->li_base_path);
988                                 }
989                                 else
990                                         rs->sr_err = res;
991                         }
992                 }
993         }
994         else /* entry was null */
995                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
996
997         if(entry != NULL)
998                 entry_free(entry);
999         rs->sr_text = "";
1000         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1001         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1002         send_ldap_result(op, rs);
1003         return 0;
1004 }
1005
1006 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1007         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1008         Entry * e = NULL;
1009         Attribute       *a;
1010
1011         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1012
1013         e = (Entry *) get_entry(op, &ni->li_base_path);
1014         if(e != NULL) {
1015                 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1016                         a != NULL;
1017                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1018                         rs->sr_err = LDAP_COMPARE_FALSE;
1019                 
1020                         if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1021                                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1022                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1023                                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1024                                                 op->o_tmpmemctx ) == 0) {
1025                                 rs->sr_err = LDAP_COMPARE_TRUE;
1026                                 break;
1027                         }
1028                 }
1029         }
1030         else {
1031                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1032         }
1033
1034         if(e != NULL)
1035                 entry_free(e);
1036         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1037         send_ldap_result(op, rs);
1038         return 0;
1039 }
1040
1041 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1042         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1043         ni->li_tool_current = 0;
1044         return 0;
1045 }                                       
1046
1047 static int ldif_tool_entry_close(BackendDB * be) {
1048         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1049
1050         SLAP_FREE(ni->li_tool_cookie.entries);
1051         return 0;
1052 }
1053
1054 static ID
1055 ldif_tool_entry_first(BackendDB *be)
1056 {
1057         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1058         ID id = 1; /* first entry in the array of entries shifted by one */
1059
1060         ni->li_tool_current = 1;
1061         if(ni->li_tool_cookie.entries == NULL) {
1062                 Operation op = {0};
1063
1064                 op.o_bd = be;
1065                 op.o_req_dn = *be->be_suffix;
1066                 op.o_req_ndn = *be->be_nsuffix;
1067                 op.ors_scope = LDAP_SCOPE_SUBTREE;
1068                 ni->li_tool_cookie.op = &op;
1069                 (void)enum_tree( &ni->li_tool_cookie );
1070                 ni->li_tool_cookie.op = NULL;
1071         }
1072         return id;
1073 }
1074
1075 static ID ldif_tool_entry_next(BackendDB *be)
1076 {
1077         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1078         ni->li_tool_current += 1;
1079         if(ni->li_tool_current > ni->li_tool_cookie.eind)
1080                 return NOID;
1081         else
1082                 return ni->li_tool_current;
1083 }
1084
1085 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1086         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1087         Entry * e;
1088
1089         if(id > ni->li_tool_cookie.eind || id < 1)
1090                 return NULL;
1091         else {
1092                 e = ni->li_tool_cookie.entries[id - 1];
1093                 ni->li_tool_cookie.entries[id - 1] = NULL;
1094                 return e;
1095         }
1096 }
1097
1098 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1099         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1100         struct berval dn = e->e_nname;
1101         struct berval leaf_path = BER_BVNULL;
1102         struct stat stats;
1103         int statres;
1104         int res = LDAP_SUCCESS;
1105
1106         dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1107
1108         if(leaf_path.bv_val != NULL) {
1109                 struct berval base = BER_BVNULL;
1110                 /* build path to container, and path to ldif of container */
1111                 get_parent_path(&leaf_path, &base);
1112
1113                 statres = stat(base.bv_val, &stats); /* check if container exists */
1114                 if(statres == -1 && errno == ENOENT) { /* container missing */
1115                         base.bv_val[base.bv_len] = '.';
1116                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1117                         base.bv_val[base.bv_len] = '\0';
1118                         if(statres == -1 && errno == ENOENT) {
1119                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1120                         }
1121                         else if(statres != -1) { /* create parent */
1122                                 int mkdirres = mkdir(base.bv_val, 0750);
1123                                 if(mkdirres == -1) {
1124                                         res = LDAP_UNWILLING_TO_PERFORM;
1125                                 }
1126                         }
1127                         else
1128                                 res = LDAP_UNWILLING_TO_PERFORM;
1129                 }/* container was possibly created, move on to add the entry */
1130                 if(res == LDAP_SUCCESS) {
1131                         statres = stat(leaf_path.bv_val, &stats);
1132                         if(statres == -1 && errno == ENOENT) {
1133                                 res = (int) spew_entry(e, &leaf_path);
1134                         }
1135                         else /* it already exists */
1136                                 res = LDAP_ALREADY_EXISTS;
1137                 }
1138                 SLAP_FREE(base.bv_val);
1139                 SLAP_FREE(leaf_path.bv_val);
1140         }
1141
1142         if(res == LDAP_SUCCESS) {
1143                 return 1;
1144         }
1145         else
1146                 return NOID;
1147 }
1148
1149 static int
1150 ldif_back_db_init( BackendDB *be )
1151 {
1152         struct ldif_info *ni;
1153
1154         ni = ch_calloc( 1, sizeof(struct ldif_info) );
1155         be->be_private = ni;
1156         be->be_cf_ocs = ldifocs;
1157         ldap_pvt_thread_mutex_init(&ni->li_mutex);
1158         return 0;
1159 }
1160
1161 static int
1162 ldif_back_db_destroy(
1163                            Backend      *be
1164                            )
1165 {
1166         struct ldif_info *ni = be->be_private;
1167         ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1168         free( be->be_private );
1169         return 0;
1170 }
1171
1172 static int
1173 ldif_back_db_open(
1174                         Backend *be
1175                         )
1176 {
1177         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1178         if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1179                 fprintf(stderr, "missing base path for back-ldif\n");
1180                 return 1;
1181         }
1182         return 0;
1183 }
1184
1185 int
1186 ldif_back_initialize(
1187                            BackendInfo  *bi
1188                            )
1189 {
1190         static char *controls[] = {
1191                 LDAP_CONTROL_MANAGEDSAIT,
1192                 NULL
1193         };
1194         int rc;
1195
1196         bi->bi_flags |=
1197                 SLAP_BFLAG_REFERRALS;
1198
1199         bi->bi_controls = controls;
1200
1201         bi->bi_open = 0;
1202         bi->bi_close = 0;
1203         bi->bi_config = 0;
1204         bi->bi_destroy = 0;
1205
1206         bi->bi_db_init = ldif_back_db_init;
1207         bi->bi_db_config = config_generic_wrapper;
1208         bi->bi_db_open = ldif_back_db_open;
1209         bi->bi_db_close = 0;
1210         bi->bi_db_destroy = ldif_back_db_destroy;
1211
1212         bi->bi_op_bind = ldif_back_bind;
1213         bi->bi_op_unbind = 0;
1214         bi->bi_op_search = ldif_back_search;
1215         bi->bi_op_compare = ldif_back_compare;
1216         bi->bi_op_modify = ldif_back_modify;
1217         bi->bi_op_modrdn = ldif_back_modrdn;
1218         bi->bi_op_add = ldif_back_add;
1219         bi->bi_op_delete = ldif_back_delete;
1220         bi->bi_op_abandon = 0;
1221
1222         bi->bi_extended = 0;
1223
1224         bi->bi_chk_referrals = ldif_back_referrals;
1225
1226         bi->bi_connection_init = 0;
1227         bi->bi_connection_destroy = 0;
1228
1229         bi->bi_tool_entry_open = ldif_tool_entry_open;
1230         bi->bi_tool_entry_close = ldif_tool_entry_close;
1231         bi->bi_tool_entry_first = ldif_tool_entry_first;
1232         bi->bi_tool_entry_next = ldif_tool_entry_next;
1233         bi->bi_tool_entry_get = ldif_tool_entry_get;
1234         bi->bi_tool_entry_put = ldif_tool_entry_put;
1235         bi->bi_tool_entry_reindex = 0;
1236         bi->bi_tool_sync = 0;
1237         
1238         bi->bi_tool_dn2id_get = 0;
1239         bi->bi_tool_id2entry_get = 0;
1240         bi->bi_tool_entry_modify = 0;
1241
1242         rc = config_register_schema( ldifcfg, ldifocs );
1243         if ( rc ) return rc;
1244         return 0;
1245 }