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