]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
Happy new year! (belated)
[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 )", 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
726                 struct berval   pndn = op->o_req_ndn;
727
728                 for ( ; entry == NULL; ) {
729                         dnParent( &pndn, &pndn );
730                         
731                         if ( BER_BVISEMPTY( &pndn )
732                                 || !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) )
733                         {
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_err = rc;
781                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
782                         send_ldap_result( op, rs );
783                 }
784
785                 if ( rs->sr_matched ) {
786                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
787                         rs->sr_matched = NULL;
788                 }
789
790                 return rc;
791         }
792
793         ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
794
795         if ( is_entry_referral( entry ) ) {
796                 /* entry is a referral */
797                 BerVarray refs = get_entry_referrals( op, entry );
798                 rs->sr_ref = referral_rewrite(
799                         refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
800
801                 Debug( LDAP_DEBUG_TRACE,
802                         "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
803                         (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
804
805                 rs->sr_matched = entry->e_name.bv_val;
806                 if ( rs->sr_ref != NULL ) {
807                         rc = rs->sr_err = LDAP_REFERRAL;
808                         send_ldap_result( op, rs );
809                         ber_bvarray_free( rs->sr_ref );
810                         rs->sr_ref = NULL;
811
812                 } else {
813                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
814                         rc = rs->sr_err;
815                 }
816
817                 rs->sr_matched = NULL;
818                 ber_bvarray_free( refs );
819         }
820
821         entry_free( entry );
822
823         return rc;
824 }
825
826 static int
827 ldif_back_bind( Operation *op, SlapReply *rs )
828 {
829         struct ldif_info *li = NULL;
830         Attribute * a = NULL;
831         AttributeDescription *password = slap_schema.si_ad_userPassword;
832         int return_val = 0;
833         Entry * entry = NULL;
834
835         switch ( be_rootdn_bind( op, rs ) ) {
836         case SLAP_CB_CONTINUE:
837                 break;
838
839         default:
840                 /* in case of success, front end will send result;
841                  * otherwise, be_rootdn_bind() did */
842                 return rs->sr_err;
843         }
844
845         li = (struct ldif_info *) op->o_bd->be_private;
846         ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
847         entry = get_entry(op, &li->li_base_path);
848
849         /* no object is found for them */
850         if(entry == NULL) {
851                 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
852                 goto return_result;
853         }
854
855         /* they don't have userpassword */
856         if((a = attr_find(entry->e_attrs, password)) == NULL) {
857                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
858                 return_val = 1;
859                 goto return_result;
860         }
861
862         /* authentication actually failed */
863         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
864                              &rs->sr_text) != 0) {
865                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
866                 return_val = 1;
867                 goto return_result;
868         }
869
870         /* let the front-end send success */
871         return_val = 0;
872         goto return_result;
873
874  return_result:
875         ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
876         if(return_val != 0)
877                 send_ldap_result( op, rs );
878         if(entry != NULL)
879                 entry_free(entry);
880         return return_val;
881 }
882
883 static int ldif_back_search(Operation *op, SlapReply *rs)
884 {
885         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
886         enumCookie ck = { NULL, NULL, NULL, 0, 0 };
887
888         ck.op = op;
889         ck.rs = rs;
890         ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
891         rs->sr_err = enum_tree( &ck );
892         ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
893         send_ldap_result(op, rs);
894
895         return rs->sr_err;
896 }
897
898 static int ldif_back_add(Operation *op, SlapReply *rs) {
899         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
900         Entry * e = op->ora_e;
901         struct berval dn = e->e_nname;
902         struct berval leaf_path = BER_BVNULL;
903         struct stat stats;
904         int statres;
905         char textbuf[SLAP_TEXT_BUFLEN];
906
907         Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
908
909         rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
910                 &rs->sr_text, textbuf, sizeof( textbuf ) );
911         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
912
913         rs->sr_err = slap_add_opattrs( op,
914                 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
915         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
916
917         ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
918
919         dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
920
921         if(leaf_path.bv_val != NULL) {
922                 struct berval base = BER_BVNULL;
923                 /* build path to container and ldif of container */
924                 get_parent_path(&leaf_path, &base);
925
926                 statres = stat(base.bv_val, &stats); /* check if container exists */
927                 if(statres == -1 && errno == ENOENT) { /* container missing */
928                         base.bv_val[base.bv_len] = '.';
929                         statres = stat(base.bv_val, &stats); /* check for leaf node */
930                         base.bv_val[base.bv_len] = '\0';
931                         if(statres == -1 && errno == ENOENT) {
932                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
933                                 rs->sr_text = "Parent does not exist";
934                         }
935                         else if(statres != -1) { /* create parent */
936                                 int mkdirres = mkdir(base.bv_val, 0750);
937                                 if(mkdirres == -1) {
938                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
939                                         rs->sr_text = "Could not create parent folder";
940                                         Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
941                                                 base.bv_val, STRERROR( errno ), 0 );
942                                 }
943                         }
944                         else
945                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
946                 }/* container was possibly created, move on to add the entry */
947                 if(rs->sr_err == LDAP_SUCCESS) {
948                         statres = stat(leaf_path.bv_val, &stats);
949                         if(statres == -1 && errno == ENOENT) {
950                                 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
951                         }
952                         else if ( statres == -1 ) {
953                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
954                                 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
955                                         leaf_path.bv_val, STRERROR( errno ), 0 );
956                         }
957                         else /* it already exists */
958                                 rs->sr_err = LDAP_ALREADY_EXISTS;
959                 }
960                 SLAP_FREE(base.bv_val);
961                 SLAP_FREE(leaf_path.bv_val);
962         }
963
964         ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
965
966 send_res:
967         Debug( LDAP_DEBUG_TRACE, 
968                         "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
969                                 rs->sr_text : "", 0);
970         send_ldap_result(op, rs);
971         slap_graduate_commit_csn( op );
972         return rs->sr_err;
973 }
974
975 static int ldif_back_modify(Operation *op, SlapReply *rs) {
976         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
977         Modifications * modlst = op->orm_modlist;
978         struct berval path = BER_BVNULL;
979         Entry * entry = NULL;
980         int spew_res;
981
982         slap_mods_opattrs( op, &op->orm_modlist, 1 );
983
984         ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
985         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
986                 &path);
987         entry = get_entry(op, &li->li_base_path);
988
989         if(entry != NULL) {
990                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
991                 if(rs->sr_err == LDAP_SUCCESS) {
992                         int save_errno;
993                         spew_res = spew_entry(entry, &path, 1, &save_errno);
994                         if(spew_res == -1) {
995                                 Debug( LDAP_DEBUG_ANY,
996                                         "%s ldif_back_modify: could not output entry \"%s\": %s\n",
997                                         op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
998                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
999                         }
1000                 }
1001         }
1002         else {
1003                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1004         }
1005         
1006         if(entry != NULL)
1007                 entry_free(entry);
1008         if(path.bv_val != NULL)
1009                 SLAP_FREE(path.bv_val);
1010         rs->sr_text = NULL;
1011         ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1012         send_ldap_result(op, rs);
1013         slap_graduate_commit_csn( op );
1014         return rs->sr_err;
1015 }
1016
1017 static int ldif_back_delete(Operation *op, SlapReply *rs) {
1018         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1019         struct berval path = BER_BVNULL;
1020         int res = 0;
1021
1022         if ( BER_BVISEMPTY( &op->o_csn )) {
1023                 struct berval csn;
1024                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1025
1026                 csn.bv_val = csnbuf;
1027                 csn.bv_len = sizeof( csnbuf );
1028                 slap_get_csn( op, &csn, 1 );
1029         }
1030
1031         ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
1032         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
1033
1034         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
1035         res = rmdir(path.bv_val);
1036         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
1037         rs->sr_err = LDAP_SUCCESS;
1038         if ( res ) {
1039                 switch ( errno ) {
1040                 case ENOTEMPTY:
1041                         rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
1042                         break;
1043
1044                 case ENOENT:
1045                         /* is leaf, go on */
1046                         res = 0;
1047                         break;
1048
1049                 default:
1050                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1051                         break;
1052                 }
1053         }
1054
1055         if ( !res ) {
1056                 res = unlink(path.bv_val);
1057                 if ( res == -1 ) {
1058                         switch ( errno ) {
1059                         case ENOENT:
1060                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1061                                 break;
1062
1063                         default:
1064                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1065                                 break;
1066                         }
1067                 }
1068         }
1069
1070         SLAP_FREE(path.bv_val);
1071         ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1072         send_ldap_result(op, rs);
1073         slap_graduate_commit_csn( op );
1074         return rs->sr_err;
1075 }
1076
1077
1078 static int move_entry(Entry * entry, struct berval * ndn,
1079                            struct berval * newndn, struct berval * suffixdn,
1080                            struct berval * base_path) {
1081         int res;
1082         int exists_res;
1083         struct berval path;
1084         struct berval newpath;
1085
1086         dn2path(ndn, suffixdn, base_path, &path);
1087         dn2path(newndn, suffixdn, base_path, &newpath);
1088
1089         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1090                 /* some object doesn't exist */
1091                 res = LDAP_NO_SUCH_OBJECT;
1092         }
1093         else { /* do the modrdn */
1094                 exists_res = open(newpath.bv_val, O_RDONLY);
1095                 if(exists_res == -1 && errno == ENOENT) {
1096                         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1097                         res = spew_entry(entry, &newpath, 0, NULL);
1098                         if(res != -1) {
1099                                 /* if this fails we should log something bad */
1100                                 res = unlink(path.bv_val);
1101                                 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1102                                 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1103                                 res = rename(path.bv_val, newpath.bv_val);
1104                                 res = LDAP_SUCCESS;
1105                         }
1106                         else {
1107                                 if(errno == ENOENT)
1108                                         res = LDAP_NO_SUCH_OBJECT;
1109                                 else
1110                                         res = LDAP_UNWILLING_TO_PERFORM;
1111                                 unlink(newpath.bv_val); /* in case file was created */
1112                         }
1113                         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1114                 }
1115                 else if(exists_res) {
1116                         int close_res = close(exists_res);
1117                         res = LDAP_ALREADY_EXISTS;
1118                         if(close_res == -1) {
1119                         /* log heinous error */
1120                         }
1121                 }
1122                 else {
1123                         res = LDAP_UNWILLING_TO_PERFORM;
1124                 }
1125         }
1126
1127         if(newpath.bv_val != NULL)
1128                 SLAP_FREE(newpath.bv_val);
1129         if(path.bv_val != NULL)
1130                 SLAP_FREE(path.bv_val);
1131         return res;
1132 }
1133
1134 static int
1135 ldif_back_modrdn(Operation *op, SlapReply *rs)
1136 {
1137         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1138         struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1139         struct berval p_dn;
1140         Entry * entry = NULL;
1141         int res;
1142
1143         slap_mods_opattrs( op, &op->orr_modlist, 1 );
1144
1145         ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1146         entry = get_entry( op, &li->li_base_path );
1147
1148         /* build the mods to the entry */
1149         if ( entry != NULL ) {
1150                 /* build new dn, and new ndn for the entry */
1151                 if ( op->oq_modrdn.rs_newSup != NULL ) {
1152                         struct berval   op_dn = op->o_req_dn,
1153                                         op_ndn = op->o_req_ndn;
1154                         Entry           *np;
1155
1156                         /* new superior */
1157                         p_dn = *op->oq_modrdn.rs_newSup;
1158                         op->o_req_dn = *op->oq_modrdn.rs_newSup;
1159                         op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1160                         np = get_entry( op, &li->li_base_path );
1161                         op->o_req_dn = op_dn;
1162                         op->o_req_ndn = op_ndn;
1163                         if ( np == NULL ) {
1164                                 goto no_such_object;
1165                         }
1166                         entry_free( np );
1167                 } else {
1168                         dnParent( &entry->e_name, &p_dn );
1169                 }
1170                 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
1171                 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1172                 ber_memfree_x( entry->e_name.bv_val, NULL );
1173                 ber_memfree_x( entry->e_nname.bv_val, NULL );
1174                 entry->e_name = new_dn;
1175                 entry->e_nname = new_ndn;
1176
1177                 /* perform the modifications */
1178                 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1179                 if ( res == LDAP_SUCCESS ) {
1180                         rs->sr_err = move_entry( entry, &op->o_req_ndn,
1181                                                 &new_ndn,
1182                                                 &op->o_bd->be_nsuffix[0],
1183                                                 &li->li_base_path );
1184                 } else {
1185                         rs->sr_err = res;
1186                 }
1187         } else {
1188 no_such_object:;
1189                 /* entry was null */
1190                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1191         }
1192
1193         if ( entry != NULL ) {
1194                 entry_free( entry );
1195         }
1196         rs->sr_text = "";
1197         ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1198         send_ldap_result( op, rs );
1199         slap_graduate_commit_csn( op );
1200         return rs->sr_err;
1201 }
1202
1203 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1204  */
1205 int ldif_back_entry_get(
1206         Operation *op,
1207         struct berval *ndn,
1208         ObjectClass *oc,
1209         AttributeDescription *at,
1210         int rw,
1211         Entry **ent )
1212 {
1213         struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1214         struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1215
1216         assert( ndn != NULL );
1217         assert( !BER_BVISNULL( ndn ) );
1218
1219         ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1220         op->o_req_dn = *ndn;
1221         op->o_req_ndn = *ndn;
1222         *ent = get_entry( op, &li->li_base_path );
1223         op->o_req_dn = op_dn;
1224         op->o_req_ndn = op_ndn;
1225         ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1226
1227         if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1228                 entry_free( *ent );
1229                 *ent = NULL;
1230         }
1231
1232         return ( *ent == NULL ? 1 : 0 );
1233 }
1234
1235 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1236         struct ldif_info *li = (struct ldif_info *) be->be_private;
1237         li->li_tool_current = 0;
1238         return 0;
1239 }                                       
1240
1241 static int ldif_tool_entry_close(BackendDB * be) {
1242         struct ldif_info *li = (struct ldif_info *) be->be_private;
1243
1244         SLAP_FREE(li->li_tool_cookie.entries);
1245         return 0;
1246 }
1247
1248 static ID ldif_tool_entry_next(BackendDB *be)
1249 {
1250         struct ldif_info *li = (struct ldif_info *) be->be_private;
1251         if(li->li_tool_current >= li->li_tool_cookie.eind)
1252                 return NOID;
1253         else
1254                 return ++li->li_tool_current;
1255 }
1256
1257 static ID
1258 ldif_tool_entry_first(BackendDB *be)
1259 {
1260         struct ldif_info *li = (struct ldif_info *) be->be_private;
1261
1262         if(li->li_tool_cookie.entries == NULL) {
1263                 Operation op = {0};
1264
1265                 op.o_bd = be;
1266                 op.o_req_dn = *be->be_suffix;
1267                 op.o_req_ndn = *be->be_nsuffix;
1268                 op.ors_scope = LDAP_SCOPE_SUBTREE;
1269                 li->li_tool_cookie.op = &op;
1270                 (void)enum_tree( &li->li_tool_cookie );
1271                 li->li_tool_cookie.op = NULL;
1272         }
1273         return ldif_tool_entry_next( be );
1274 }
1275
1276 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1277         struct ldif_info *li = (struct ldif_info *) be->be_private;
1278         Entry * e;
1279
1280         if(id > li->li_tool_cookie.eind || id < 1)
1281                 return NULL;
1282         else {
1283                 e = li->li_tool_cookie.entries[id - 1];
1284                 li->li_tool_cookie.entries[id - 1] = NULL;
1285                 return e;
1286         }
1287 }
1288
1289 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1290         struct ldif_info *li = (struct ldif_info *) be->be_private;
1291         struct berval dn = e->e_nname;
1292         struct berval leaf_path = BER_BVNULL;
1293         struct stat stats;
1294         int statres;
1295         int res = LDAP_SUCCESS;
1296
1297         dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1298
1299         if(leaf_path.bv_val != NULL) {
1300                 struct berval base = BER_BVNULL;
1301                 /* build path to container, and path to ldif of container */
1302                 get_parent_path(&leaf_path, &base);
1303
1304                 statres = stat(base.bv_val, &stats); /* check if container exists */
1305                 if(statres == -1 && errno == ENOENT) { /* container missing */
1306                         base.bv_val[base.bv_len] = '.';
1307                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1308                         base.bv_val[base.bv_len] = '\0';
1309                         if(statres == -1 && errno == ENOENT) {
1310                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1311                         }
1312                         else if(statres != -1) { /* create parent */
1313                                 int mkdirres = mkdir(base.bv_val, 0750);
1314                                 if(mkdirres == -1) {
1315                                         res = LDAP_UNWILLING_TO_PERFORM;
1316                                 }
1317                         }
1318                         else
1319                                 res = LDAP_UNWILLING_TO_PERFORM;
1320                 }/* container was possibly created, move on to add the entry */
1321                 if(res == LDAP_SUCCESS) {
1322                         statres = stat(leaf_path.bv_val, &stats);
1323                         if(statres == -1 && errno == ENOENT) {
1324                                 res = spew_entry(e, &leaf_path, 0, NULL);
1325                         }
1326                         else /* it already exists */
1327                                 res = LDAP_ALREADY_EXISTS;
1328                 }
1329                 SLAP_FREE(base.bv_val);
1330                 SLAP_FREE(leaf_path.bv_val);
1331         }
1332
1333         if(res == LDAP_SUCCESS) {
1334                 return 1;
1335         }
1336         else
1337                 return NOID;
1338 }
1339
1340 static int
1341 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1342 {
1343         struct ldif_info *li;
1344
1345         li = ch_calloc( 1, sizeof(struct ldif_info) );
1346         be->be_private = li;
1347         be->be_cf_ocs = ldifocs;
1348         ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1349         return 0;
1350 }
1351
1352 static int
1353 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1354 {
1355         struct ldif_info *li = be->be_private;
1356
1357         ch_free(li->li_base_path.bv_val);
1358         ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1359         free( be->be_private );
1360         return 0;
1361 }
1362
1363 static int
1364 ldif_back_db_open( Backend *be, ConfigReply *cr)
1365 {
1366         struct ldif_info *li = (struct ldif_info *) be->be_private;
1367         if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1368                 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1369                 return 1;
1370         }
1371         return 0;
1372 }
1373
1374 int
1375 ldif_back_initialize(
1376                            BackendInfo  *bi
1377                            )
1378 {
1379         static char *controls[] = {
1380                 LDAP_CONTROL_MANAGEDSAIT,
1381                 NULL
1382         };
1383         int rc;
1384
1385         bi->bi_flags |=
1386                 SLAP_BFLAG_INCREMENT |
1387                 SLAP_BFLAG_REFERRALS;
1388
1389         bi->bi_controls = controls;
1390
1391         bi->bi_open = 0;
1392         bi->bi_close = 0;
1393         bi->bi_config = 0;
1394         bi->bi_destroy = 0;
1395
1396         bi->bi_db_init = ldif_back_db_init;
1397         bi->bi_db_config = config_generic_wrapper;
1398         bi->bi_db_open = ldif_back_db_open;
1399         bi->bi_db_close = 0;
1400         bi->bi_db_destroy = ldif_back_db_destroy;
1401
1402         bi->bi_op_bind = ldif_back_bind;
1403         bi->bi_op_unbind = 0;
1404         bi->bi_op_search = ldif_back_search;
1405         bi->bi_op_compare = 0;
1406         bi->bi_op_modify = ldif_back_modify;
1407         bi->bi_op_modrdn = ldif_back_modrdn;
1408         bi->bi_op_add = ldif_back_add;
1409         bi->bi_op_delete = ldif_back_delete;
1410         bi->bi_op_abandon = 0;
1411
1412         bi->bi_extended = 0;
1413
1414         bi->bi_chk_referrals = ldif_back_referrals;
1415
1416         bi->bi_connection_init = 0;
1417         bi->bi_connection_destroy = 0;
1418
1419         bi->bi_entry_get_rw = ldif_back_entry_get;
1420
1421 #if 0   /* NOTE: uncomment to completely disable access control */
1422         bi->bi_access_allowed = slap_access_always_allowed;
1423 #endif
1424
1425         bi->bi_tool_entry_open = ldif_tool_entry_open;
1426         bi->bi_tool_entry_close = ldif_tool_entry_close;
1427         bi->bi_tool_entry_first = ldif_tool_entry_first;
1428         bi->bi_tool_entry_next = ldif_tool_entry_next;
1429         bi->bi_tool_entry_get = ldif_tool_entry_get;
1430         bi->bi_tool_entry_put = ldif_tool_entry_put;
1431         bi->bi_tool_entry_reindex = 0;
1432         bi->bi_tool_sync = 0;
1433         
1434         bi->bi_tool_dn2id_get = 0;
1435         bi->bi_tool_entry_modify = 0;
1436
1437         bi->bi_cf_ocs = ldifocs;
1438
1439         rc = config_register_schema( ldifcfg, ldifocs );
1440         if ( rc ) return rc;
1441         return 0;
1442 }