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