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