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