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