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