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