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