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