]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
For ITS#5408: back-ldif cannot currently handle multiple database suffixes.
[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-2008 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_rdwr_t  li_rdwr;
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 SINGLE-VALUE )", 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 * orig_dn, struct berval * suffixdn, struct berval * base_path,
86         struct berval *res)
87 {
88         char *ptr, *sep, *end;
89         int nsep = 0;
90         struct berval dn;
91
92         assert( orig_dn != NULL );
93         assert( !BER_BVISNULL( orig_dn ) );
94         assert( suffixdn != NULL );
95         assert( !BER_BVISNULL( suffixdn ) );
96         assert( dnIsSuffix( orig_dn, suffixdn ) );
97
98         dn = *orig_dn;
99
100         /* escape dirsep's
101          * use "\" + hexpair, so the escaped DN remains formally valid */
102         for ( ptr = dn.bv_val, end = &dn.bv_val[dn.bv_len]; ptr < end; ptr++ ) {
103                 if ( ptr[0] == LDAP_DIRSEP[0] ) {
104                         nsep++;
105                 }
106         }
107
108         if ( nsep ) {
109                 char    *p;
110
111                 dn.bv_len += 2*nsep;
112                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
113
114                 for ( ptr = orig_dn->bv_val, end = &orig_dn->bv_val[orig_dn->bv_len], p = dn.bv_val;
115                         ptr < end; ptr++, p++)
116                 {
117                         static const char hex[] = "0123456789ABCDEF";
118                         if ( ptr[0] == LDAP_DIRSEP[0] ) {
119                                 *p++ = '\\';
120                                 *p++ = hex[(LDAP_DIRSEP[0] & 0xF0U) >> 4];
121                                 *p = hex[LDAP_DIRSEP[0] & 0x0FU];
122                         } else {
123                                 p[0] = ptr[0];
124                         }
125                 }
126                 p[0] = '\0';
127         }
128
129         res->bv_len = dn.bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
130         res->bv_val = ch_malloc( res->bv_len + 1 );
131         ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
132         end = dn.bv_val + dn.bv_len;
133         if ( !BER_BVISEMPTY( suffixdn ) ) {
134                 *ptr++ = LDAP_DIRSEP[0];
135                 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
136                 end -= suffixdn->bv_len + 1;
137
138         } else if ( BER_BVISEMPTY( &dn ) ) {
139                 *ptr++ = LDAP_DIRSEP[0];
140         }
141         while ( end > dn.bv_val ) {
142                 for (sep = end-1; sep >= dn.bv_val && !DN_SEPARATOR( *sep ); sep--);
143                 *ptr++ = LDAP_DIRSEP[0];
144                 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
145                 end = sep;
146         }
147         strcpy(ptr, LDIF);
148 #if IX_FSL != IX_DNL
149         {
150                 struct berval bv;
151                 bv = *res;
152                 while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
153                         *ptr++ = IX_FSL;
154                         assert( ( ptr - bv.bv_val ) <= bv.bv_len );
155                         bv.bv_len -= ( ptr - bv.bv_val );
156                         bv.bv_val = ptr;
157                         ptr = ber_bvchr( &bv, IX_DNR );
158                         if ( !ptr )
159                                 break;
160                         *ptr++ = IX_FSR;
161                         assert( ( ptr - bv.bv_val ) <= bv.bv_len );
162                         bv.bv_len -= ( ptr - bv.bv_val );
163                         bv.bv_val = ptr;
164                 }
165         }
166 #endif
167         if ( dn.bv_val != orig_dn->bv_val ) {
168                 ch_free( dn.bv_val );
169         }
170
171         assert( strlen( res->bv_val ) == res->bv_len );
172 }
173
174 static char * slurp_file(int fd) {
175         int read_chars_total = 0;
176         int read_chars = 0;
177         int entry_size;
178         char * entry;
179         char * entry_pos;
180         struct stat st;
181
182         fstat(fd, &st);
183         entry_size = st.st_size;
184         entry = ch_malloc( entry_size+1 );
185         entry_pos = entry;
186         
187         while(1) {
188                 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
189                 if(read_chars == -1) {
190                         SLAP_FREE(entry);
191                         return NULL;
192                 }
193                 if(read_chars == 0) {
194                         entry[read_chars_total] = '\0';
195                         break;
196                 }
197                 else {
198                         read_chars_total += read_chars;
199                         entry_pos += read_chars;
200                 }
201         }
202         return entry;
203 }
204
205 /*
206  * return nonnegative for success or -1 for error
207  * do not return numbers less than -1
208  */
209 static int spew_file(int fd, char * spew, int len) {
210         int writeres = 0;
211         
212         while(len > 0) {
213                 writeres = write(fd, spew, len);
214                 if(writeres == -1) {
215                         return -1;
216                 }
217                 else {
218                         spew += writeres;
219                         len -= writeres;
220                 }
221         }
222         return writeres;
223 }
224
225 static int
226 spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
227 {
228         int rs, save_errno = 0;
229         int openres;
230         int res, spew_res;
231         int entry_length;
232         char * entry_as_string;
233         char *tmpfname = NULL;
234
235         tmpfname = ch_malloc( path->bv_len + STRLENOF( "XXXXXX" ) + 1 );
236         AC_MEMCPY( tmpfname, path->bv_val, path->bv_len );
237         AC_MEMCPY( &tmpfname[ path->bv_len ], "XXXXXX", STRLENOF( "XXXXXX" ) + 1 );
238
239         openres = mkstemp( tmpfname );
240         if ( openres == -1 ) {
241                 save_errno = errno;
242                 rs = LDAP_UNWILLING_TO_PERFORM;
243                 Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
244                         tmpfname, STRERROR( save_errno ), 0 );
245
246         } else {
247                 struct berval rdn;
248                 int tmp;
249
250                 /* Only save the RDN onto disk */
251                 dnRdn( &e->e_name, &rdn );
252                 if ( rdn.bv_len != e->e_name.bv_len ) {
253                         e->e_name.bv_val[rdn.bv_len] = '\0';
254                         tmp = e->e_name.bv_len;
255                         e->e_name.bv_len = rdn.bv_len;
256                         rdn.bv_len = tmp;
257                 }
258
259                 spew_res = -2;
260                 if ( dolock ) {
261                         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
262                 }
263
264                 entry_as_string = entry2str(e, &entry_length);
265                 if ( entry_as_string != NULL ) {
266                         spew_res = spew_file( openres,
267                                 entry_as_string, entry_length );
268                         if ( spew_res == -1 ) {
269                                 save_errno = errno;
270                         }
271                 }
272
273                 if ( dolock ) {
274                         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
275                 }
276
277                 /* Restore full DN */
278                 if ( rdn.bv_len != e->e_name.bv_len ) {
279                         e->e_name.bv_val[e->e_name.bv_len] = ',';
280                         e->e_name.bv_len = rdn.bv_len;
281                 }
282
283                 res = close( openres );
284                 rs = LDAP_UNWILLING_TO_PERFORM;
285
286                 if ( spew_res > -2 ) {
287                         if ( res == -1 || spew_res == -1 ) {
288                                 if ( save_errno == 0 ) {
289                                         save_errno = errno;
290                                 }
291                                 Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
292                                         tmpfname, STRERROR( save_errno ), 0 );
293
294                         } else {
295                                 res = rename( tmpfname, path->bv_val );
296                                 if ( res == 0 ) {
297                                         rs = LDAP_SUCCESS;
298
299                                 } else {
300                                         save_errno = errno;
301                                         switch ( save_errno ) {
302                                         case ENOENT:
303                                                 rs = LDAP_NO_SUCH_OBJECT;
304                                                 break;
305
306                                         default:
307                                                 break;
308                                         }
309                                 }
310                         }
311                 }
312
313                 if ( rs != LDAP_SUCCESS ) {
314                         unlink( tmpfname );
315                 }
316         }
317
318         ch_free( tmpfname );
319
320         if ( rs != LDAP_SUCCESS && save_errnop != NULL ) {
321                 *save_errnop = save_errno;
322         }
323
324         return rs;
325 }
326
327 static Entry * get_entry_for_fd(int fd,
328         struct berval *pdn,
329         struct berval *pndn)
330 {
331         char * entry = (char *) slurp_file(fd);
332         Entry * ldentry = NULL;
333         
334         /* error reading file */
335         if(entry == NULL) {
336                 goto return_value;
337         }
338
339         ldentry = str2entry(entry);
340         if ( ldentry ) {
341                 struct berval rdn;
342                 rdn = ldentry->e_name;
343                 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
344                 ch_free( rdn.bv_val );
345                 rdn = ldentry->e_nname;
346                 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
347                 ch_free( rdn.bv_val );
348         }
349
350  return_value:
351         if(fd != -1) {
352                 if(close(fd) != 0) {
353                         /* log error */
354                 }
355         }
356         if(entry != NULL)
357                 SLAP_FREE(entry);
358         return ldentry;
359 }
360
361 static Entry * get_entry(Operation *op, struct berval *base_path) {
362         struct berval path, pdn, pndn;
363         int fd;
364
365         dnParent(&op->o_req_dn, &pdn);
366         dnParent(&op->o_req_ndn, &pndn);
367         dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
368         fd = open(path.bv_val, O_RDONLY);
369         /* error opening file (mebbe should log error) */
370         if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
371                 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
372                         path.bv_val, STRERROR(errno), 0 );
373         }
374
375         if(path.bv_val != NULL)
376                 SLAP_FREE(path.bv_val);
377
378         if ( fd != -1 ) {
379                 return get_entry_for_fd(fd, &pdn, &pndn);
380         }
381
382         return NULL;
383 }
384
385 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
386         char *ptr;
387         res->bv_len = name->bv_len + base->bv_len + 1;
388         res->bv_val = ch_malloc( res->bv_len + 1 );
389         strcpy(res->bv_val, base->bv_val);
390         ptr = res->bv_val + base->bv_len;
391         *ptr++ = LDAP_DIRSEP[0];
392         strcpy(ptr, name->bv_val);
393 }
394
395 typedef struct bvlist {
396         struct bvlist *next;
397         struct berval bv;
398         struct berval num;
399         int inum;
400         int off;
401 } bvlist;
402
403
404 static int r_enum_tree(enumCookie *ck, struct berval *path, int base,
405         struct berval *pdn, struct berval *pndn)
406 {
407         Entry *e = NULL;
408         int fd = 0, rc = LDAP_SUCCESS;
409
410         if ( !base ) {
411                 fd = open( path->bv_val, O_RDONLY );
412                 if ( fd < 0 ) {
413                         Debug( LDAP_DEBUG_TRACE,
414                                 "=> ldif_enum_tree: failed to open %s: %s\n",
415                                 path->bv_val, STRERROR(errno), 0 );
416                         return LDAP_NO_SUCH_OBJECT;
417                 }
418
419                 e = get_entry_for_fd(fd, pdn, pndn);
420                 if ( !e ) {
421                         Debug( LDAP_DEBUG_ANY,
422                                 "=> ldif_enum_tree: failed to read entry for %s\n",
423                                 path->bv_val, 0, 0 );
424                         return LDAP_BUSY;
425                 }
426
427                 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
428                         ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
429                         /* Send right away? */
430                         if ( ck->rs ) {
431                                 /*
432                                  * if it's a referral, add it to the list of referrals. only do
433                                  * this for non-base searches, and don't check the filter
434                                  * explicitly here since it's only a candidate anyway.
435                                  */
436                                 if ( !get_manageDSAit( ck->op )
437                                                 && ck->op->ors_scope != LDAP_SCOPE_BASE
438                                                 && is_entry_referral( e ) )
439                                 {
440                                         BerVarray erefs = get_entry_referrals( ck->op, e );
441                                         ck->rs->sr_ref = referral_rewrite( erefs,
442                                                         &e->e_name, NULL,
443                                                         ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
444                                                                 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
445         
446                                         ck->rs->sr_entry = e;
447                                         rc = send_search_reference( ck->op, ck->rs );
448                                         ber_bvarray_free( ck->rs->sr_ref );
449                                         ber_bvarray_free( erefs );
450                                         ck->rs->sr_ref = NULL;
451                                         ck->rs->sr_entry = NULL;
452         
453                                 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
454                                 {
455                                         ck->rs->sr_entry = e;
456                                         ck->rs->sr_attrs = ck->op->ors_attrs;
457                                         ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
458                                         rc = send_search_entry(ck->op, ck->rs);
459                                         ck->rs->sr_entry = NULL;
460                                 }
461                                 fd = 1;
462                                 if ( rc )
463                                         goto done;
464                         } else {
465                         /* Queueing up for tool mode */
466                                 if(ck->entries == NULL) {
467                                         ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
468                                         ck->elen = ENTRY_BUFF_INCREMENT;
469                                 }
470                                 if(ck->eind >= ck->elen) { /* grow entries if necessary */      
471                                         ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
472                                         ck->elen *= 2;
473                                 }
474         
475                                 ck->entries[ck->eind++] = e;
476                                 fd = 0;
477                         }
478                 } else {
479                         fd = 1;
480                 }
481         }
482
483         if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
484                 DIR * dir_of_path;
485                 bvlist *list = NULL, *ptr;
486
487                 path->bv_len -= STRLENOF( LDIF );
488                 path->bv_val[path->bv_len] = '\0';
489
490                 dir_of_path = opendir(path->bv_val);
491                 if(dir_of_path == NULL) { /* can't open directory */
492                         if ( errno != ENOENT ) {
493                                 /* it shouldn't be treated as an error
494                                  * only if the directory doesn't exist */
495                                 rc = LDAP_BUSY;
496                                 Debug( LDAP_DEBUG_ANY,
497                                         "=> ldif_enum_tree: failed to opendir %s (%d)\n",
498                                         path->bv_val, errno, 0 );
499                         }
500                         goto done;
501                 }
502         
503                 while(1) {
504                         struct berval fname, itmp;
505                         struct dirent * dir;
506                         bvlist *bvl, **prev;
507
508                         dir = readdir(dir_of_path);
509                         if(dir == NULL) break; /* end of the directory */
510                         fname.bv_len = strlen( dir->d_name );
511                         if ( fname.bv_len <= STRLENOF( LDIF ))
512                                 continue;
513                         if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
514                                 continue;
515                         fname.bv_val = dir->d_name;
516
517                         bvl = ch_malloc( sizeof(bvlist) );
518                         ber_dupbv( &bvl->bv, &fname );
519                         BER_BVZERO( &bvl->num );
520                         itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
521                         if ( itmp.bv_val ) {
522                                 char *ptr;
523                                 itmp.bv_val++;
524                                 itmp.bv_len = bvl->bv.bv_len
525                                         - ( itmp.bv_val - bvl->bv.bv_val );
526                                 ptr = ber_bvchr( &itmp, IX_FSR );
527                                 if ( ptr ) {
528                                         itmp.bv_len = ptr - itmp.bv_val;
529                                         ber_dupbv( &bvl->num, &itmp );
530                                         bvl->inum = strtol( itmp.bv_val, NULL, 0 );
531                                         itmp.bv_val[0] = '\0';
532                                         bvl->off = itmp.bv_val - bvl->bv.bv_val;
533                                 }
534                         }
535
536                         for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
537                                 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
538                                 if ( !cmp && bvl->num.bv_val )
539                                         cmp = bvl->inum - ptr->inum;
540                                 if ( cmp < 0 )
541                                         break;
542                         }
543                         *prev = bvl;
544                         bvl->next = ptr;
545                                 
546                 }
547                 closedir(dir_of_path);
548
549                 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
550                         ck->op->ors_scope = LDAP_SCOPE_BASE;
551                 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
552                         ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
553
554                 while ( ( ptr = list ) ) {
555                         struct berval fpath;
556
557                         list = ptr->next;
558
559                         if ( rc == LDAP_SUCCESS ) {
560                                 if ( ptr->num.bv_val )
561                                         AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
562                                                 ptr->num.bv_len );
563                                 fullpath( path, &ptr->bv, &fpath );
564                                 rc = r_enum_tree(ck, &fpath, 0,
565                                         e != NULL ? &e->e_name : pdn,
566                                         e != NULL ? &e->e_nname : pndn );
567                                 free(fpath.bv_val);
568                         }
569                         if ( ptr->num.bv_val )
570                                 free( ptr->num.bv_val );
571                         free(ptr->bv.bv_val);
572                         free(ptr);
573                 }
574         }
575 done:
576         if ( fd ) entry_free( e );
577         return rc;
578 }
579
580 static int
581 enum_tree(
582         enumCookie *ck
583 )
584 {
585         struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
586         struct berval path;
587         struct berval pdn, pndn;
588         int rc;
589
590         dnParent( &ck->op->o_req_dn, &pdn );
591         dnParent( &ck->op->o_req_ndn, &pndn );
592         dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
593         rc = r_enum_tree(ck, &path, BER_BVISEMPTY( &ck->op->o_req_ndn ) ? 1 : 0, &pdn, &pndn);
594         ch_free( path.bv_val );
595         return rc;
596 }
597
598 /* Get the parent path plus the LDIF suffix */
599 static void get_parent_path(struct berval * dnpath, struct berval *res) {
600         int dnpathlen = dnpath->bv_len;
601         int i;
602         
603         for(i = dnpathlen;i>0;i--) /* find the first path seperator */
604                 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
605                         break;
606         res->bv_len = i;
607         res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
608         strncpy(res->bv_val, dnpath->bv_val, i);
609         strcpy(res->bv_val+i, LDIF);
610         res->bv_val[i] = '\0';
611 }
612
613 static int apply_modify_to_entry(Entry * entry,
614                                 Modifications * modlist,
615                                 Operation * op,
616                                 SlapReply * rs)
617 {
618         char textbuf[SLAP_TEXT_BUFLEN];
619         int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
620         int is_oc = 0;
621         Modification *mods = NULL;
622
623         if (!acl_check_modlist(op, entry, modlist)) {
624                 return LDAP_INSUFFICIENT_ACCESS;
625         }
626
627         for (; modlist != NULL; modlist = modlist->sml_next) {
628                 mods = &modlist->sml_mod;
629
630                 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
631                         is_oc = 1;
632                 }
633                 switch (mods->sm_op) {
634                 case LDAP_MOD_ADD:
635                         rc = modify_add_values(entry, mods,
636                                    get_permissiveModify(op),
637                                    &rs->sr_text, textbuf,
638                                    sizeof( textbuf ) );
639                         break;
640                                 
641                 case LDAP_MOD_DELETE:
642                         rc = modify_delete_values(entry, mods,
643                                 get_permissiveModify(op),
644                                 &rs->sr_text, textbuf,
645                                 sizeof( textbuf ) );
646                         break;
647                                 
648                 case LDAP_MOD_REPLACE:
649                         rc = modify_replace_values(entry, mods,
650                                  get_permissiveModify(op),
651                                  &rs->sr_text, textbuf,
652                                  sizeof( textbuf ) );
653                         break;
654
655                 case LDAP_MOD_INCREMENT:
656                         rc = modify_increment_values( entry,
657                                 mods, get_permissiveModify(op),
658                                 &rs->sr_text, textbuf,
659                                 sizeof( textbuf ) );
660                         break;
661
662                         break;
663
664                 case SLAP_MOD_SOFTADD:
665                         mods->sm_op = LDAP_MOD_ADD;
666                         rc = modify_add_values(entry, mods,
667                                    get_permissiveModify(op),
668                                    &rs->sr_text, textbuf,
669                                    sizeof( textbuf ) );
670                         mods->sm_op = SLAP_MOD_SOFTADD;
671                         if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
672                                 rc = LDAP_SUCCESS;
673                         }
674                         break;
675                 default:
676                         break;
677                 }
678                 if(rc != LDAP_SUCCESS) break;
679         }
680         
681         if(rc == LDAP_SUCCESS) {
682                 if ( is_oc ) {
683                         entry->e_ocflags = 0;
684                 }
685                 /* check that the entry still obeys the schema */
686                 rc = entry_schema_check( op, entry, NULL, 0, 0,
687                           &rs->sr_text, textbuf, sizeof( textbuf ) );
688         }
689
690         return rc;
691 }
692
693 int
694 ldif_back_referrals( Operation *op, SlapReply *rs )
695 {
696         struct ldif_info        *li = NULL;
697         Entry                   *entry;
698         int                     rc = LDAP_SUCCESS;
699
700 #if 0
701         if ( op->o_tag == LDAP_REQ_SEARCH ) {
702                 /* let search take care of itself */
703                 return rc;
704         }
705 #endif
706
707         if ( get_manageDSAit( op ) ) {
708                 /* let op take care of DSA management */
709                 return rc;
710         }
711
712         if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
713                 /* the empty DN cannot be a referral */
714                 return rc;
715         }
716
717         li = (struct ldif_info *)op->o_bd->be_private;
718         ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
719         entry = get_entry( op, &li->li_base_path );
720
721         /* no object is found for them */
722         if ( entry == NULL ) {
723                 struct berval   odn = op->o_req_dn;
724                 struct berval   ondn = op->o_req_ndn;
725                 struct berval   pndn = ondn;
726                 ber_len_t               min_dnlen = op->o_bd->be_nsuffix[0].bv_len;
727
728                 if ( min_dnlen == 0 )
729                         min_dnlen = 1;     /* catch empty DN */
730
731                 for ( ; entry == NULL; ) {
732                         dnParent( &pndn, &pndn );
733                         if ( pndn.bv_len < min_dnlen ) {
734                                 break;
735                         }
736
737                         op->o_req_dn = pndn;
738                         op->o_req_ndn = pndn;
739
740                         entry = get_entry( op, &li->li_base_path );
741                 }
742
743                 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
744
745                 op->o_req_dn = odn;
746                 op->o_req_ndn = ondn;
747
748                 rc = LDAP_SUCCESS;
749                 rs->sr_matched = NULL;
750                 if ( entry != NULL ) {
751                         Debug( LDAP_DEBUG_TRACE,
752                                 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
753                                 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
754
755                         if ( is_entry_referral( entry ) ) {
756                                 rc = LDAP_OTHER;
757                                 rs->sr_ref = get_entry_referrals( op, entry );
758                                 if ( rs->sr_ref ) {
759                                         rs->sr_matched = ber_strdup_x(
760                                         entry->e_name.bv_val, op->o_tmpmemctx );
761                                 }
762                         }
763
764                         entry_free(entry);
765
766                 } else if ( default_referral != NULL ) {
767                         rc = LDAP_OTHER;
768                         rs->sr_ref = referral_rewrite( default_referral,
769                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
770                 }
771
772                 if ( rs->sr_ref != NULL ) {
773                         /* send referrals */
774                         rc = rs->sr_err = LDAP_REFERRAL;
775                         send_ldap_result( op, rs );
776                         ber_bvarray_free( rs->sr_ref );
777                         rs->sr_ref = NULL;
778
779                 } else if ( rc != LDAP_SUCCESS ) {
780                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
781                 }
782
783                 if ( rs->sr_matched ) {
784                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
785                         rs->sr_matched = NULL;
786                 }
787
788                 return rc;
789         }
790
791         ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
792
793         if ( is_entry_referral( entry ) ) {
794                 /* entry is a referral */
795                 BerVarray refs = get_entry_referrals( op, entry );
796                 rs->sr_ref = referral_rewrite(
797                         refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
798
799                 Debug( LDAP_DEBUG_TRACE,
800                         "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
801                         (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
802
803                 rs->sr_matched = entry->e_name.bv_val;
804                 if ( rs->sr_ref != NULL ) {
805                         rc = rs->sr_err = LDAP_REFERRAL;
806                         send_ldap_result( op, rs );
807                         ber_bvarray_free( rs->sr_ref );
808                         rs->sr_ref = NULL;
809
810                 } else {
811                         rc = LDAP_OTHER;
812                         rs->sr_text = "bad referral object";
813                 }
814
815                 rs->sr_matched = NULL;
816                 ber_bvarray_free( refs );
817         }
818
819         entry_free( entry );
820
821         return rc;
822 }
823
824 static int
825 ldif_back_bind( Operation *op, SlapReply *rs )
826 {
827         struct ldif_info *li = NULL;
828         Attribute * a = NULL;
829         AttributeDescription *password = slap_schema.si_ad_userPassword;
830         int return_val = 0;
831         Entry * entry = NULL;
832
833         switch ( be_rootdn_bind( op, rs ) ) {
834         case SLAP_CB_CONTINUE:
835                 break;
836
837         default:
838                 /* in case of success, front end will send result;
839                  * otherwise, be_rootdn_bind() did */
840                 return rs->sr_err;
841         }
842
843         li = (struct ldif_info *) op->o_bd->be_private;
844         ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
845         entry = get_entry(op, &li->li_base_path);
846
847         /* no object is found for them */
848         if(entry == NULL) {
849                 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
850                 goto return_result;
851         }
852
853         /* they don't have userpassword */
854         if((a = attr_find(entry->e_attrs, password)) == NULL) {
855                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
856                 return_val = 1;
857                 goto return_result;
858         }
859
860         /* authentication actually failed */
861         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
862                              &rs->sr_text) != 0) {
863                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
864                 return_val = 1;
865                 goto return_result;
866         }
867
868         /* let the front-end send success */
869         return_val = 0;
870         goto return_result;
871
872  return_result:
873         ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
874         if(return_val != 0)
875                 send_ldap_result( op, rs );
876         if(entry != NULL)
877                 entry_free(entry);
878         return return_val;
879 }
880
881 static int ldif_back_search(Operation *op, SlapReply *rs)
882 {
883         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
884         enumCookie ck = { NULL, NULL, NULL, 0, 0 };
885
886         ck.op = op;
887         ck.rs = rs;
888         ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
889         rs->sr_err = enum_tree( &ck );
890         ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
891         send_ldap_result(op, rs);
892
893         return rs->sr_err;
894 }
895
896 static int ldif_back_add(Operation *op, SlapReply *rs) {
897         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
898         Entry * e = op->ora_e;
899         struct berval dn = e->e_nname;
900         struct berval leaf_path = BER_BVNULL;
901         struct stat stats;
902         int statres;
903         char textbuf[SLAP_TEXT_BUFLEN];
904
905         Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
906
907         rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
908                 &rs->sr_text, textbuf, sizeof( textbuf ) );
909         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
910
911         rs->sr_err = slap_add_opattrs( op,
912                 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
913         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
914
915         ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
916
917         dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
918
919         if(leaf_path.bv_val != NULL) {
920                 struct berval base = BER_BVNULL;
921                 /* build path to container and ldif of container */
922                 get_parent_path(&leaf_path, &base);
923
924                 statres = stat(base.bv_val, &stats); /* check if container exists */
925                 if(statres == -1 && errno == ENOENT) { /* container missing */
926                         base.bv_val[base.bv_len] = '.';
927                         statres = stat(base.bv_val, &stats); /* check for leaf node */
928                         base.bv_val[base.bv_len] = '\0';
929                         if(statres == -1 && errno == ENOENT) {
930                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
931                                 rs->sr_text = "Parent does not exist";
932                         }
933                         else if(statres != -1) { /* create parent */
934                                 int mkdirres = mkdir(base.bv_val, 0750);
935                                 if(mkdirres == -1) {
936                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
937                                         rs->sr_text = "Could not create parent folder";
938                                         Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
939                                                 base.bv_val, STRERROR( errno ), 0 );
940                                 }
941                         }
942                         else
943                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
944                 }/* container was possibly created, move on to add the entry */
945                 if(rs->sr_err == LDAP_SUCCESS) {
946                         statres = stat(leaf_path.bv_val, &stats);
947                         if(statres == -1 && errno == ENOENT) {
948                                 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
949                         }
950                         else if ( statres == -1 ) {
951                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
952                                 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
953                                         leaf_path.bv_val, STRERROR( errno ), 0 );
954                         }
955                         else /* it already exists */
956                                 rs->sr_err = LDAP_ALREADY_EXISTS;
957                 }
958                 SLAP_FREE(base.bv_val);
959                 SLAP_FREE(leaf_path.bv_val);
960         }
961
962         ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
963
964 send_res:
965         Debug( LDAP_DEBUG_TRACE, 
966                         "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
967                                 rs->sr_text : "", 0);
968         send_ldap_result(op, rs);
969         slap_graduate_commit_csn( op );
970         return rs->sr_err;
971 }
972
973 static int ldif_back_modify(Operation *op, SlapReply *rs) {
974         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
975         Modifications * modlst = op->orm_modlist;
976         struct berval path = BER_BVNULL;
977         Entry * entry = NULL;
978         int spew_res;
979
980         slap_mods_opattrs( op, &op->orm_modlist, 1 );
981
982         ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
983         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
984                 &path);
985         entry = get_entry(op, &li->li_base_path);
986
987         if(entry != NULL) {
988                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
989                 if(rs->sr_err == LDAP_SUCCESS) {
990                         int save_errno;
991                         spew_res = spew_entry(entry, &path, 1, &save_errno);
992                         if(spew_res == -1) {
993                                 Debug( LDAP_DEBUG_ANY,
994                                         "%s ldif_back_modify: could not output entry \"%s\": %s\n",
995                                         op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
996                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
997                         }
998                 }
999         }
1000         else {
1001                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1002         }
1003         
1004         if(entry != NULL)
1005                 entry_free(entry);
1006         if(path.bv_val != NULL)
1007                 SLAP_FREE(path.bv_val);
1008         rs->sr_text = NULL;
1009         ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1010         send_ldap_result(op, rs);
1011         slap_graduate_commit_csn( op );
1012         return rs->sr_err;
1013 }
1014
1015 static int ldif_back_delete(Operation *op, SlapReply *rs) {
1016         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1017         struct berval path = BER_BVNULL;
1018         int res = 0;
1019
1020         if ( BER_BVISEMPTY( &op->o_csn )) {
1021                 struct berval csn;
1022                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1023
1024                 csn.bv_val = csnbuf;
1025                 csn.bv_len = sizeof( csnbuf );
1026                 slap_get_csn( op, &csn, 1 );
1027         }
1028
1029         ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
1030         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
1031
1032         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
1033         res = rmdir(path.bv_val);
1034         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
1035         rs->sr_err = LDAP_SUCCESS;
1036         if ( res ) {
1037                 switch ( errno ) {
1038                 case ENOTEMPTY:
1039                         rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
1040                         break;
1041
1042                 case ENOENT:
1043                         /* is leaf, go on */
1044                         res = 0;
1045                         break;
1046
1047                 default:
1048                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1049                         break;
1050                 }
1051         }
1052
1053         if ( !res ) {
1054                 res = unlink(path.bv_val);
1055                 if ( res == -1 ) {
1056                         switch ( errno ) {
1057                         case ENOENT:
1058                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1059                                 break;
1060
1061                         default:
1062                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1063                                 break;
1064                         }
1065                 }
1066         }
1067
1068         SLAP_FREE(path.bv_val);
1069         ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1070         send_ldap_result(op, rs);
1071         slap_graduate_commit_csn( op );
1072         return rs->sr_err;
1073 }
1074
1075
1076 static int move_entry(Entry * entry, struct berval * ndn,
1077                            struct berval * newndn, struct berval * suffixdn,
1078                            struct berval * base_path) {
1079         int res;
1080         int exists_res;
1081         struct berval path;
1082         struct berval newpath;
1083
1084         dn2path(ndn, suffixdn, base_path, &path);
1085         dn2path(newndn, suffixdn, base_path, &newpath);
1086
1087         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1088                 /* some object doesn't exist */
1089                 res = LDAP_NO_SUCH_OBJECT;
1090         }
1091         else { /* do the modrdn */
1092                 exists_res = open(newpath.bv_val, O_RDONLY);
1093                 if(exists_res == -1 && errno == ENOENT) {
1094                         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1095                         res = spew_entry(entry, &newpath, 0, NULL);
1096                         if(res != -1) {
1097                                 /* if this fails we should log something bad */
1098                                 res = unlink(path.bv_val);
1099                                 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1100                                 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1101                                 res = rename(path.bv_val, newpath.bv_val);
1102                                 res = LDAP_SUCCESS;
1103                         }
1104                         else {
1105                                 if(errno == ENOENT)
1106                                         res = LDAP_NO_SUCH_OBJECT;
1107                                 else
1108                                         res = LDAP_UNWILLING_TO_PERFORM;
1109                                 unlink(newpath.bv_val); /* in case file was created */
1110                         }
1111                         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1112                 }
1113                 else if(exists_res) {
1114                         int close_res = close(exists_res);
1115                         res = LDAP_ALREADY_EXISTS;
1116                         if(close_res == -1) {
1117                         /* log heinous error */
1118                         }
1119                 }
1120                 else {
1121                         res = LDAP_UNWILLING_TO_PERFORM;
1122                 }
1123         }
1124
1125         if(newpath.bv_val != NULL)
1126                 SLAP_FREE(newpath.bv_val);
1127         if(path.bv_val != NULL)
1128                 SLAP_FREE(path.bv_val);
1129         return res;
1130 }
1131
1132 static int
1133 ldif_back_modrdn(Operation *op, SlapReply *rs)
1134 {
1135         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1136         struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1137         struct berval p_dn;
1138         Entry * entry = NULL;
1139         int res;
1140
1141         slap_mods_opattrs( op, &op->orr_modlist, 1 );
1142
1143         ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1144         entry = get_entry( op, &li->li_base_path );
1145
1146         /* build the mods to the entry */
1147         if ( entry != NULL ) {
1148                 /* build new dn, and new ndn for the entry */
1149                 if ( op->oq_modrdn.rs_newSup != NULL ) {
1150                         struct berval   op_dn = op->o_req_dn,
1151                                         op_ndn = op->o_req_ndn;
1152                         Entry           *np;
1153
1154                         /* new superior */
1155                         p_dn = *op->oq_modrdn.rs_newSup;
1156                         op->o_req_dn = *op->oq_modrdn.rs_newSup;
1157                         op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1158                         np = get_entry( op, &li->li_base_path );
1159                         op->o_req_dn = op_dn;
1160                         op->o_req_ndn = op_ndn;
1161                         if ( np == NULL ) {
1162                                 goto no_such_object;
1163                         }
1164                         entry_free( np );
1165                 } else {
1166                         dnParent( &entry->e_name, &p_dn );
1167                 }
1168                 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
1169                 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1170                 ber_memfree_x( entry->e_name.bv_val, NULL );
1171                 ber_memfree_x( entry->e_nname.bv_val, NULL );
1172                 entry->e_name = new_dn;
1173                 entry->e_nname = new_ndn;
1174
1175                 /* perform the modifications */
1176                 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1177                 if ( res == LDAP_SUCCESS ) {
1178                         rs->sr_err = move_entry( entry, &op->o_req_ndn,
1179                                                 &new_ndn,
1180                                                 &op->o_bd->be_nsuffix[0],
1181                                                 &li->li_base_path );
1182                 } else {
1183                         rs->sr_err = res;
1184                 }
1185         } else {
1186 no_such_object:;
1187                 /* entry was null */
1188                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1189         }
1190
1191         if ( entry != NULL ) {
1192                 entry_free( entry );
1193         }
1194         rs->sr_text = "";
1195         ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1196         send_ldap_result( op, rs );
1197         slap_graduate_commit_csn( op );
1198         return rs->sr_err;
1199 }
1200
1201 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1202  */
1203 int ldif_back_entry_get(
1204         Operation *op,
1205         struct berval *ndn,
1206         ObjectClass *oc,
1207         AttributeDescription *at,
1208         int rw,
1209         Entry **ent )
1210 {
1211         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1212         struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1213         int rc = LDAP_NO_SUCH_OBJECT;
1214
1215         assert( ndn != NULL );
1216         assert( !BER_BVISNULL( ndn ) );
1217
1218         ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1219         op->o_req_dn = *ndn;
1220         op->o_req_ndn = *ndn;
1221         *ent = get_entry( op, &li->li_base_path );
1222         op->o_req_dn = op_dn;
1223         op->o_req_ndn = op_ndn;
1224         ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1225
1226         if ( *ent ) {
1227                 rc = LDAP_SUCCESS;
1228                 if ( oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1229                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1230                         entry_free( *ent );
1231                         *ent = NULL;
1232                 }
1233         }
1234
1235         return rc;
1236 }
1237
1238 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1239         struct ldif_info *li = (struct ldif_info *) be->be_private;
1240         li->li_tool_current = 0;
1241         return 0;
1242 }                                       
1243
1244 static int ldif_tool_entry_close(BackendDB * be) {
1245         struct ldif_info *li = (struct ldif_info *) be->be_private;
1246
1247         SLAP_FREE(li->li_tool_cookie.entries);
1248         return 0;
1249 }
1250
1251 static ID ldif_tool_entry_next(BackendDB *be)
1252 {
1253         struct ldif_info *li = (struct ldif_info *) be->be_private;
1254         if(li->li_tool_current >= li->li_tool_cookie.eind)
1255                 return NOID;
1256         else
1257                 return ++li->li_tool_current;
1258 }
1259
1260 static ID
1261 ldif_tool_entry_first(BackendDB *be)
1262 {
1263         struct ldif_info *li = (struct ldif_info *) be->be_private;
1264
1265         if(li->li_tool_cookie.entries == NULL) {
1266                 Operation op = {0};
1267
1268                 op.o_bd = be;
1269                 op.o_req_dn = *be->be_suffix;
1270                 op.o_req_ndn = *be->be_nsuffix;
1271                 op.ors_scope = LDAP_SCOPE_SUBTREE;
1272                 li->li_tool_cookie.op = &op;
1273                 (void)enum_tree( &li->li_tool_cookie );
1274                 li->li_tool_cookie.op = NULL;
1275         }
1276         return ldif_tool_entry_next( be );
1277 }
1278
1279 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1280         struct ldif_info *li = (struct ldif_info *) be->be_private;
1281         Entry * e;
1282
1283         if(id > li->li_tool_cookie.eind || id < 1)
1284                 return NULL;
1285         else {
1286                 e = li->li_tool_cookie.entries[id - 1];
1287                 li->li_tool_cookie.entries[id - 1] = NULL;
1288                 return e;
1289         }
1290 }
1291
1292 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1293         struct ldif_info *li = (struct ldif_info *) be->be_private;
1294         struct berval dn = e->e_nname;
1295         struct berval leaf_path = BER_BVNULL;
1296         struct stat stats;
1297         int statres;
1298         int res = LDAP_SUCCESS;
1299
1300         dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1301
1302         if(leaf_path.bv_val != NULL) {
1303                 struct berval base = BER_BVNULL;
1304                 /* build path to container, and path to ldif of container */
1305                 get_parent_path(&leaf_path, &base);
1306
1307                 statres = stat(base.bv_val, &stats); /* check if container exists */
1308                 if(statres == -1 && errno == ENOENT) { /* container missing */
1309                         base.bv_val[base.bv_len] = '.';
1310                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1311                         base.bv_val[base.bv_len] = '\0';
1312                         if(statres == -1 && errno == ENOENT) {
1313                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1314                         }
1315                         else if(statres != -1) { /* create parent */
1316                                 int mkdirres = mkdir(base.bv_val, 0750);
1317                                 if(mkdirres == -1) {
1318                                         res = LDAP_UNWILLING_TO_PERFORM;
1319                                 }
1320                         }
1321                         else
1322                                 res = LDAP_UNWILLING_TO_PERFORM;
1323                 }/* container was possibly created, move on to add the entry */
1324                 if(res == LDAP_SUCCESS) {
1325                         statres = stat(leaf_path.bv_val, &stats);
1326                         if(statres == -1 && errno == ENOENT) {
1327                                 res = spew_entry(e, &leaf_path, 0, NULL);
1328                         }
1329                         else /* it already exists */
1330                                 res = LDAP_ALREADY_EXISTS;
1331                 }
1332                 SLAP_FREE(base.bv_val);
1333                 SLAP_FREE(leaf_path.bv_val);
1334         }
1335
1336         if(res == LDAP_SUCCESS) {
1337                 return 1;
1338         }
1339         else
1340                 return NOID;
1341 }
1342
1343 static int
1344 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1345 {
1346         struct ldif_info *li;
1347
1348         li = ch_calloc( 1, sizeof(struct ldif_info) );
1349         be->be_private = li;
1350         be->be_cf_ocs = ldifocs;
1351         ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1352         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
1353         return 0;
1354 }
1355
1356 static int
1357 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1358 {
1359         struct ldif_info *li = be->be_private;
1360
1361         ch_free(li->li_base_path.bv_val);
1362         ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1363         free( be->be_private );
1364         return 0;
1365 }
1366
1367 static int
1368 ldif_back_db_open( Backend *be, ConfigReply *cr)
1369 {
1370         struct ldif_info *li = (struct ldif_info *) be->be_private;
1371         if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1372                 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1373                 return 1;
1374         }
1375         return 0;
1376 }
1377
1378 int
1379 ldif_back_initialize(
1380                            BackendInfo  *bi
1381                            )
1382 {
1383         static char *controls[] = {
1384                 LDAP_CONTROL_MANAGEDSAIT,
1385                 NULL
1386         };
1387         int rc;
1388
1389         bi->bi_flags |=
1390                 SLAP_BFLAG_INCREMENT |
1391                 SLAP_BFLAG_REFERRALS;
1392
1393         bi->bi_controls = controls;
1394
1395         bi->bi_open = 0;
1396         bi->bi_close = 0;
1397         bi->bi_config = 0;
1398         bi->bi_destroy = 0;
1399
1400         bi->bi_db_init = ldif_back_db_init;
1401         bi->bi_db_config = config_generic_wrapper;
1402         bi->bi_db_open = ldif_back_db_open;
1403         bi->bi_db_close = 0;
1404         bi->bi_db_destroy = ldif_back_db_destroy;
1405
1406         bi->bi_op_bind = ldif_back_bind;
1407         bi->bi_op_unbind = 0;
1408         bi->bi_op_search = ldif_back_search;
1409         bi->bi_op_compare = 0;
1410         bi->bi_op_modify = ldif_back_modify;
1411         bi->bi_op_modrdn = ldif_back_modrdn;
1412         bi->bi_op_add = ldif_back_add;
1413         bi->bi_op_delete = ldif_back_delete;
1414         bi->bi_op_abandon = 0;
1415
1416         bi->bi_extended = 0;
1417
1418         bi->bi_chk_referrals = ldif_back_referrals;
1419
1420         bi->bi_connection_init = 0;
1421         bi->bi_connection_destroy = 0;
1422
1423         bi->bi_entry_get_rw = ldif_back_entry_get;
1424
1425 #if 0   /* NOTE: uncomment to completely disable access control */
1426         bi->bi_access_allowed = slap_access_always_allowed;
1427 #endif
1428
1429         bi->bi_tool_entry_open = ldif_tool_entry_open;
1430         bi->bi_tool_entry_close = ldif_tool_entry_close;
1431         bi->bi_tool_entry_first = ldif_tool_entry_first;
1432         bi->bi_tool_entry_next = ldif_tool_entry_next;
1433         bi->bi_tool_entry_get = ldif_tool_entry_get;
1434         bi->bi_tool_entry_put = ldif_tool_entry_put;
1435         bi->bi_tool_entry_reindex = 0;
1436         bi->bi_tool_sync = 0;
1437         
1438         bi->bi_tool_dn2id_get = 0;
1439         bi->bi_tool_entry_modify = 0;
1440
1441         bi->bi_cf_ocs = ldifocs;
1442
1443         rc = config_register_schema( ldifcfg, ldifocs );
1444         if ( rc ) return rc;
1445         return 0;
1446 }