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