]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
don't continue if fopen failed
[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 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 struct ldif_info {
35         struct berval li_base_path;
36         ID tool_current;
37         Entry ** tool_entries;
38         int tool_put_entry_flag;
39         int tool_numentries;
40         ldap_pvt_thread_mutex_t  li_mutex;
41 };
42
43 #define LDIF    ".ldif"
44
45 #define IX_DNL  '{'
46 #define IX_DNR  '}'
47 #ifndef IX_FSL
48 #define IX_FSL  IX_DNL
49 #define IX_FSR  IX_DNR
50 #endif
51
52 #define ENTRY_BUFF_INCREMENT 500
53
54 static ObjectClass *ldif_oc;
55
56 static ConfigDriver ldif_cf;
57
58 static ConfigTable ldifcfg[] = {
59         { "", "", 0, 0, 0, ARG_MAGIC,
60                 ldif_cf, NULL, NULL, NULL },
61         { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
62                 (void *)offsetof(struct ldif_info, li_base_path),
63                 "( OLcfgAt:1.1 NAME 'dbDirectory' "
64                         "DESC 'Directory for database content' "
65                         "EQUALITY caseIgnoreMatch "
66                         "SYNTAX OMsDirectoryString )", NULL, NULL },
67         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
68                 NULL, NULL, NULL, NULL }
69 };
70
71 static ConfigOCs ldifocs[] = {
72         { "( OLcfgOc:2.1 "
73                 "NAME 'ldifConfig' "
74                 "DESC 'LDIF backend configuration' "
75                 "SUP olcDatabaseConfig "
76                 "MUST ( dbDirectory ) )", Cft_Database,
77                 &ldif_oc },
78         { NULL, 0, NULL }
79 };
80
81 static int
82 ldif_cf( ConfigArgs *c )
83 {
84         if ( c->op == SLAP_CONFIG_EMIT ) {
85                 value_add_one( &c->rvalue_vals, &ldif_oc->soc_cname );
86                 return 0;
87         }
88         return 1;
89 }
90
91 static void
92 dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
93         struct berval *res)
94 {
95         char *ptr, *sep, *end;
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, rootdn->bv_val );
102         end = dn->bv_val + dn->bv_len - rootdn->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         ptr = res->bv_val;
112         while( ptr=strchr(ptr, IX_DNL) ) {
113                 *ptr++ = IX_FSL;
114                 ptr = strchr(ptr, IX_DNR);
115                 if ( ptr )
116                         *ptr++ = IX_FSR;
117                 else
118                         break;
119         }
120 #endif
121 }
122
123 static char * slurp_file(int fd) {
124         int read_chars_total = 0;
125         int read_chars = 0;
126         int entry_size;
127         char * entry;
128         char * entry_pos;
129         struct stat st;
130
131         fstat(fd, &st);
132         entry_size = st.st_size;
133         entry = ch_malloc( entry_size+1 );
134         entry_pos = entry;
135         
136         while(1) {
137                 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
138                 if(read_chars == -1) {
139                         SLAP_FREE(entry);
140                         return NULL;
141                 }
142                 if(read_chars == 0) {
143                         entry[read_chars_total] = '\0';
144                         break;
145                 }
146                 else {
147                         read_chars_total += read_chars;
148                         entry_pos += read_chars;
149                 }
150         }
151         return entry;
152 }
153
154 static int spew_file(int fd, char * spew) {
155         int written = 0;
156         int writeres;
157         int len = strlen(spew);
158         char * spewptr = spew;
159         
160         while(written < len) {
161                 writeres = write(fd, spewptr, len - written);
162                 if(writeres == -1) {
163                         perror("could not spew write");
164                         return -1;
165                 }
166                 else {
167                         spewptr += writeres;
168                         written += writeres;
169                 }
170         }
171         return writeres;
172 }
173
174 static int spew_entry(Entry * e, struct berval * path) {
175         int rs;
176         int openres;
177         int spew_res;
178         int entry_length;
179         char * entry_as_string;
180
181         openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
182         if(openres == -1) {
183                 if(errno == ENOENT)
184                         rs = LDAP_NO_SUCH_OBJECT;
185                 else
186                         rs = LDAP_UNWILLING_TO_PERFORM;
187         }
188         else {
189                 struct berval rdn;
190                 int tmp;
191
192                 /* Only save the RDN onto disk */
193                 dnRdn( &e->e_name, &rdn );
194                 if ( rdn.bv_len != e->e_name.bv_len ) {
195                         e->e_name.bv_val[rdn.bv_len] = '\0';
196                         tmp = e->e_name.bv_len;
197                         e->e_name.bv_len = rdn.bv_len;
198                         rdn.bv_len = tmp;
199                 }
200
201                 entry_as_string = entry2str(e, &entry_length);
202
203                 /* Restore full DN */
204                 if ( rdn.bv_len != e->e_name.bv_len ) {
205                         e->e_name.bv_val[e->e_name.bv_len] = ',';
206                         e->e_name.bv_len = rdn.bv_len;
207                 }
208
209                 if(entry_as_string == NULL) {
210                         rs = LDAP_UNWILLING_TO_PERFORM;
211                         close(openres);
212                 }
213                 else {
214                         spew_res = spew_file(openres, entry_as_string);
215                         close(openres);
216                         if(spew_res == -1)
217                                 rs = LDAP_UNWILLING_TO_PERFORM;
218                         else
219                                 rs = LDAP_SUCCESS;
220                 }
221         }
222         return rs;
223 }
224
225 static Entry * get_entry_for_fd(int fd,
226         struct berval *pdn,
227         struct berval *pndn)
228 {
229         char * entry = (char *) slurp_file(fd);
230         Entry * ldentry = NULL;
231         
232         /* error reading file */
233         if(entry == NULL) {
234                 goto return_value;
235         }
236
237         ldentry = str2entry(entry);
238         if ( ldentry ) {
239                 struct berval rdn;
240                 rdn = ldentry->e_name;
241                 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
242                 ch_free( rdn.bv_val );
243                 rdn = ldentry->e_nname;
244                 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
245                 ch_free( rdn.bv_val );
246         }
247
248  return_value:
249         if(fd != -1) {
250                 if(close(fd) != 0) {
251                         /* log error */
252                 }
253         }
254         if(entry != NULL)
255                 SLAP_FREE(entry);
256         return ldentry;
257 }
258
259 static Entry * get_entry(Operation *op, struct berval *base_path) {
260         struct berval path, pdn, pndn;
261         int fd;
262
263         dnParent(&op->o_req_dn, &pdn);
264         dnParent(&op->o_req_ndn, &pndn);
265         dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
266         fd = open(path.bv_val, O_RDONLY);
267         /* error opening file (mebbe should log error) */
268         if(fd == -1) {
269                 perror("failed to open file");
270         }
271
272         if(path.bv_val != NULL)
273                 SLAP_FREE(path.bv_val);
274
275         if ( fd != -1 ) {
276                 return get_entry_for_fd(fd, &pdn, &pndn);
277         }
278
279         return NULL;
280 }
281
282 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
283         char *ptr;
284         res->bv_len = name->bv_len + base->bv_len + 1;
285         res->bv_val = ch_malloc( res->bv_len + 1 );
286         strcpy(res->bv_val, base->bv_val);
287         ptr = res->bv_val + base->bv_len;
288         *ptr++ = LDAP_DIRSEP[0];
289         strcpy(ptr, name->bv_val);
290 }
291
292 typedef struct bvlist {
293         struct bvlist *next;
294         struct berval bv;
295         struct berval num;
296         unsigned int inum;
297         int off;
298 } bvlist;
299
300 typedef struct enumCookie {
301         Entry **entries;
302         int elen;
303         int eind;
304         int scope;
305 } enumCookie;
306
307 static void r_enum_tree(enumCookie *ck, struct berval *path,
308         struct berval *pdn, struct berval *pndn) {
309         Entry *e;
310         int fd;
311
312         if(ck->entries == NULL) {
313                 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
314                 ck->elen = ENTRY_BUFF_INCREMENT;
315         }
316
317         fd = open( path->bv_val, O_RDONLY );
318         if ( fd < 0 ) {
319                 Debug( LDAP_DEBUG_TRACE,
320                         "=> ldif_enum_tree: failed to open %s\n",
321                         path->bv_val, 0, 0 );
322                 return;
323         }
324         e = get_entry_for_fd(fd, pdn, pndn);
325         if ( !e ) {
326                 Debug( LDAP_DEBUG_ANY,
327                         "=> ldif_enum_tree: failed to read entry for %s\n",
328                         path->bv_val, 0, 0 );
329                 return;
330         }
331
332         if ( ck->scope == LDAP_SCOPE_BASE || ck->scope == LDAP_SCOPE_SUBTREE ) {
333                 if(! (ck->eind < ck->elen)) { /* grow entries if necessary */   
334                         ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
335                         ck->elen *= 2;
336                 }
337
338                 ck->entries[ck->eind] = e;
339                 ck->eind++;
340                 fd = 0;
341         } else {
342                 fd = 1;
343         }
344
345         if ( ck->scope != LDAP_SCOPE_BASE ) {
346                 DIR * dir_of_path;
347                 bvlist *list = NULL, *ptr;
348
349                 path->bv_len -= STRLENOF( LDIF );
350                 path->bv_val[path->bv_len] = '\0';
351
352                 dir_of_path = opendir(path->bv_val);
353                 if(dir_of_path == NULL) {/* can't open directory */
354                         Debug( LDAP_DEBUG_TRACE,
355                                 "=> ldif_enum_tree: failed to opendir %s\n",
356                                 path->bv_val, 0, 0 );
357                         goto leave;
358                 }
359         
360                 while(1) {
361                         struct berval fname, itmp;
362                         struct dirent * dir;
363                         bvlist *bvl, *prev;
364
365                         dir = readdir(dir_of_path);
366                         if(dir == NULL) break; /* end of the directory */
367                         fname.bv_len = strlen( dir->d_name );
368                         if ( fname.bv_len <= STRLENOF( LDIF ))
369                                 continue;
370                         if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
371                                 continue;
372                         fname.bv_val = dir->d_name;
373
374                         bvl = ch_malloc( sizeof(bvlist) );
375                         ber_dupbv( &bvl->bv, &fname );
376                         BER_BVZERO( &bvl->num );
377                         itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
378                         if ( itmp.bv_val ) {
379                                 char *ptr;
380                                 itmp.bv_val++;
381                                 ptr = strchr( itmp.bv_val, IX_FSR );
382                                 if ( ptr ) {
383                                         itmp.bv_len = ptr - itmp.bv_val;
384                                         ber_dupbv( &bvl->num, &itmp );
385                                         bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
386                                         itmp.bv_val[0] = '\0';
387                                         bvl->off = itmp.bv_val - bvl->bv.bv_val;
388                                 }
389                         }
390
391                         for (ptr = list, prev = (bvlist *)&list; ptr;
392                                 prev = ptr, ptr = ptr->next) {
393                                 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
394                                 if ( !cmp && bvl->num.bv_val )
395                                         cmp = bvl->inum - ptr->inum;
396                                 if ( cmp < 0 )
397                                         break;
398                         }
399                         prev->next = bvl;
400                         bvl->next = ptr;
401                                 
402                 }
403                 closedir(dir_of_path);
404
405                 if (ck->scope == LDAP_SCOPE_ONELEVEL)
406                         ck->scope = LDAP_SCOPE_BASE;
407                 else if ( ck->scope == LDAP_SCOPE_SUBORDINATE)
408                         ck->scope = LDAP_SCOPE_SUBTREE;
409
410                 while ( ptr=list ) {
411                         struct berval fpath;
412
413                         list = ptr->next;
414
415                         if ( ptr->num.bv_val )
416                                 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
417                                         ptr->num.bv_len );
418                         fullpath( path, &ptr->bv, &fpath );
419                         r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
420                         free(fpath.bv_val);
421                         if ( ptr->num.bv_val )
422                                 free( ptr->num.bv_val );
423                         free(ptr->bv.bv_val);
424                         free(ptr);
425                 }
426         }
427 leave:
428         if ( fd ) entry_free( e );
429         return;
430 }
431
432 static Entry ** enum_tree(
433         BackendDB *be,
434         struct berval *dn,
435         struct berval *ndn,
436         int * length,
437         int scope )
438 {
439         struct ldif_info *ni = (struct ldif_info *) be->be_private;
440         struct berval path;
441         int index = 0;
442         enumCookie ck = {0};
443         struct berval pdn, pndn;
444
445         ck.scope = scope;
446         dnParent( dn, &pdn );
447         dnParent( ndn, &pndn );
448         dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
449         r_enum_tree(&ck, &path, &pdn, &pndn);
450         *length = ck.eind;
451         return ck.entries;
452 }
453
454 /* Get the parent path plus the LDIF suffix */
455 static void get_parent_path(struct berval * dnpath, struct berval *res) {
456         int dnpathlen = dnpath->bv_len;
457         int i;
458         
459         for(i = dnpathlen;i>0;i--) /* find the first path seperator */
460                 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
461                         break;
462         res->bv_len = i;
463         res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
464         strncpy(res->bv_val, dnpath->bv_val, i);
465         strcpy(res->bv_val+i, LDIF);
466         res->bv_val[i] = '\0';
467 }
468
469 static int apply_modify_to_entry(Entry * entry,
470                                 Modifications * modlist,
471                                 Operation * op,
472                                 SlapReply * rs)
473 {
474         char textbuf[SLAP_TEXT_BUFLEN];
475         size_t textlen = sizeof textbuf;
476         int rc;
477         int tempdebug;
478         Modification *mods = NULL;
479         Attribute *save_attrs;
480
481         if (!acl_check_modlist(op, entry, modlist)) {
482                 return LDAP_INSUFFICIENT_ACCESS;
483         }
484
485         /*  save_attrs = entry->e_attrs; Why?
486                         entry->e_attrs = attrs_dup(entry->e_attrs); */
487
488         for (; modlist != NULL; modlist = modlist->sml_next) {
489                 mods = &modlist->sml_mod;
490
491                 switch (mods->sm_op) {
492                 case LDAP_MOD_ADD:
493                         rc = modify_add_values(entry, mods,
494                                    get_permissiveModify(op),
495                                    &rs->sr_text, textbuf,
496                                    textlen);
497                         break;
498                                 
499                 case LDAP_MOD_DELETE:
500                         rc = modify_delete_values(entry, mods,
501                                 get_permissiveModify(op),
502                                 &rs->sr_text, textbuf,
503                                 textlen);
504
505                         break;
506                                 
507                 case LDAP_MOD_REPLACE:
508                         rc = modify_replace_values(entry, mods,
509                                  get_permissiveModify(op),
510                                  &rs->sr_text, textbuf,
511                                  textlen);
512
513                         break;
514                 case LDAP_MOD_INCREMENT:
515                         break;
516                 case SLAP_MOD_SOFTADD:
517                         mods->sm_op = LDAP_MOD_ADD;
518                         rc = modify_add_values(entry, mods,
519                                    get_permissiveModify(op),
520                                    &rs->sr_text, textbuf,
521                                    textlen);
522                         mods->sm_op = SLAP_MOD_SOFTADD;
523                         if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
524                                 rc = LDAP_SUCCESS;
525                         }
526                         break;
527                 default:
528                         break;
529                 }
530                 if(rc != LDAP_SUCCESS) break;
531         }
532         
533         if(rc == LDAP_SUCCESS) {
534                 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
535                         entry->e_ocflags = 0;
536                 }
537                 /* check that the entry still obeys the schema */
538                 rc = entry_schema_check(op->o_bd, entry,
539                                   save_attrs, &rs->sr_text,
540                                   textbuf, textlen);
541         }
542         return rc;
543 }
544
545 static int
546 ldif_back_bind( Operation *op, SlapReply *rs )
547 {
548         struct ldif_info *ni = NULL;
549         Attribute * a = NULL;
550         AttributeDescription *password = slap_schema.si_ad_userPassword;
551         int return_val = 0;
552         Entry * entry = NULL;
553
554         ni = (struct ldif_info *) op->o_bd->be_private;
555         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
556         entry = (Entry *) get_entry(op, &ni->li_base_path);
557
558         /* no object is found for them */
559         if(entry == NULL) {
560                 if(be_isroot_pw(op)) {
561                         return_val = LDAP_SUCCESS;
562                         goto return_result;
563                 }
564                 else if(be_root_dn(op->o_bd)) {
565                         return_val = LDAP_INVALID_CREDENTIALS;
566                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
567                         goto return_result;
568                 }
569                 else {
570                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
571                         return_val = 1;
572                         goto return_result;
573                 }
574         }
575
576         /* they don't have userpassword */
577         if((a = attr_find(entry->e_attrs, password)) == NULL) {
578                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
579                 return_val = 1;
580                 goto return_result;
581         }
582
583         /* authentication actually failed */
584         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
585                              &rs->sr_text) != 0) {
586                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
587                 return_val = 1;
588                 goto return_result;
589         }
590
591         /* let the front-end send success */
592         return_val = 0;
593         goto return_result;
594
595  return_result:
596         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
597         if(return_val != 0)
598                 send_ldap_result( op, rs );
599         if(entry != NULL)
600                 entry_free(entry);
601         return return_val;
602 }
603
604 static int ldif_back_search(Operation *op, SlapReply *rs)
605 {
606         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
607         int numentries = 0;
608         int i = 0;
609         Entry ** entries = NULL;
610
611         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
612         entries = (Entry **) enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, op->ors_scope);
613
614         if(entries != NULL) {
615                 for(i=0;i<numentries;i++) {
616                         if(test_filter(op, entries[i], op->ors_filter) == LDAP_COMPARE_TRUE) {
617                                 rs->sr_entry = entries[i];
618                                 rs->sr_attrs = op->ors_attrs;
619                                 rs->sr_flags = REP_ENTRY_MODIFIABLE;
620                                 send_search_entry(op, rs);
621                         }
622                         entry_free(entries[i]);
623                 }
624                 SLAP_FREE(entries);
625                 rs->sr_err = LDAP_SUCCESS;
626                 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
627                 send_ldap_result(op, rs);
628         }
629         else {
630                 rs->sr_err = LDAP_BUSY;
631                 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
632                 send_ldap_result(op, rs);
633         }
634
635         return 0;
636 }
637
638 static int ldif_back_add(Operation *op, SlapReply *rs) {
639         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
640         Entry * e = op->ora_e;
641         struct berval dn = e->e_nname;
642         struct berval leaf_path = BER_BVNULL;
643         struct stat stats;
644         int statres;
645         char textbuf[SLAP_TEXT_BUFLEN];
646         size_t textlen = sizeof textbuf;
647
648         rs->sr_err = entry_schema_check(op->o_bd, e,
649                                   NULL, &rs->sr_text, textbuf, textlen);
650         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
651                                 
652         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
653
654         dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
655
656         if(leaf_path.bv_val != NULL) {
657                 struct berval base = BER_BVNULL;
658                 /* build path to container and ldif of container */
659                 get_parent_path(&leaf_path, &base);
660
661                 statres = stat(base.bv_val, &stats); /* check if container exists */
662                 if(statres == -1 && errno == ENOENT) { /* container missing */
663                         base.bv_val[base.bv_len] = '.';
664                         statres = stat(base.bv_val, &stats); /* check for leaf node */
665                         base.bv_val[base.bv_len] = '\0';
666                         if(statres == -1 && errno == ENOENT) {
667                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
668                         }
669                         else if(statres != -1) { /* create parent */
670                                 int mkdirres = mkdir(base.bv_val, 0750);
671                                 if(mkdirres == -1) {
672                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
673                                 }
674                         }
675                         else
676                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
677                 }/* container was possibly created, move on to add the entry */
678                 if(rs->sr_err == LDAP_SUCCESS) {
679                         statres = stat(leaf_path.bv_val, &stats);
680                         if(statres == -1 && errno == ENOENT) {
681                                 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
682                                 rs->sr_err = (int) spew_entry(e, &leaf_path);
683                                 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
684                         }
685                         else /* it already exists */
686                                 rs->sr_err = LDAP_ALREADY_EXISTS;
687                 }
688                 SLAP_FREE(base.bv_val);
689                 SLAP_FREE(leaf_path.bv_val);
690         }
691
692         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
693
694 send_res:
695         send_ldap_result(op, rs);
696         return 0;
697 }
698
699 static int ldif_back_modify(Operation *op, SlapReply *rs) {
700         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
701         Modifications * modlst = op->orm_modlist;
702         struct berval path = BER_BVNULL;
703         Entry * entry = NULL;
704         int spew_res;
705
706         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
707         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
708                 &path);
709         entry = (Entry *) get_entry(op, &ni->li_base_path);
710
711         if(entry != NULL) {
712                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
713                 if(rs->sr_err == LDAP_SUCCESS) {
714                         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
715                         spew_res = spew_entry(entry, &path);
716                         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
717                         if(spew_res == -1) {
718                                 perror("could not output entry");
719                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
720                         }
721                 }
722         }
723         else {
724                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
725         }
726         
727         if(entry != NULL)
728                 entry_free(entry);
729         if(path.bv_val != NULL)
730                 SLAP_FREE(path.bv_val);
731         rs->sr_text = NULL;
732         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
733         send_ldap_result(op, rs);
734         return 0;
735 }
736
737 static int ldif_back_delete(Operation *op, SlapReply *rs) {
738         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
739         struct berval path = BER_BVNULL;
740         int res = 0;
741
742         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
743         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
744
745         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
746         res = rmdir(path.bv_val);
747         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
748         if ( res && errno != ENOENT ) {
749                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
750         } else {
751                 res = unlink(path.bv_val);
752         }
753
754         if(res == -1) {
755                 if(errno == ENOENT)
756                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
757                 else
758                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
759         }
760         else
761                 rs->sr_err = LDAP_SUCCESS;
762
763         SLAP_FREE(path.bv_val);
764         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
765         send_ldap_result(op, rs);
766         return 0;
767 }
768
769
770 static int move_entry(Entry * entry, struct berval * ndn,
771                            struct berval * newndn, struct berval * rootdn,
772                            struct berval * base_path) {
773         int res;
774         int exists_res;
775         struct berval path;
776         struct berval newpath;
777
778         dn2path(ndn, rootdn, base_path, &path);
779         dn2path(newndn, rootdn, base_path, &newpath);
780
781         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
782                 /* some object doesn't exist */
783                 res = LDAP_NO_SUCH_OBJECT;
784         }
785         else { /* do the modrdn */
786                 exists_res = open(newpath.bv_val, O_RDONLY);
787                 if(exists_res == -1 && errno == ENOENT) {
788                         res = spew_entry(entry, &newpath);
789                         if(res != -1) {
790                                 /* if this fails we should log something bad */
791                                 res = unlink(path.bv_val);
792                                 res = LDAP_SUCCESS;
793                         }
794                         else {
795                                 if(errno == ENOENT)
796                                         res = LDAP_NO_SUCH_OBJECT;
797                                 else
798                                         res = LDAP_UNWILLING_TO_PERFORM;
799                                 unlink(newpath.bv_val); /* in case file was created */
800                         }
801                 }
802                 else if(exists_res) {
803                         int close_res = close(exists_res);
804                         res = LDAP_ALREADY_EXISTS;
805                         if(close_res == -1) {
806                         /* log heinous error */
807                         }
808                 }
809                 else {
810                         res = LDAP_UNWILLING_TO_PERFORM;
811                 }
812         }
813
814         if(newpath.bv_val != NULL)
815                 SLAP_FREE(newpath.bv_val);
816         if(path.bv_val != NULL)
817                 SLAP_FREE(path.bv_val);
818         return res;
819 }
820
821 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
822         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
823         struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
824         struct berval * new_parent_dn = NULL;
825         struct berval p_dn, bv = {0, NULL};
826         Entry * entry = NULL;
827         LDAPRDN new_rdn = NULL;
828         LDAPRDN old_rdn = NULL;
829         Modifications * mods = NULL;
830         int res;
831
832         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
833         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
834         entry = (Entry *) get_entry(op, &ni->li_base_path);
835
836         /* build the mods to the entry */
837         if(entry != NULL) {
838                 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
839                         (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
840                         rs->sr_err = LDAP_INVALID_DN_SYNTAX;
841                 }
842                 else if(op->oq_modrdn.rs_deleteoldrdn &&
843                         ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
844                         LDAP_DN_FORMAT_LDAP)) {
845                         rs->sr_err = LDAP_OTHER;
846                 }
847                 else { /* got both rdns successfully, ready to build mods */
848                         if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
849                                 != LDAP_SUCCESS) {
850                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
851                         }
852                         else { /* built mods successfully */
853
854                                 /* build new dn, and new ndn for the entry */
855                                 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
856                                         p_dn = *op->oq_modrdn.rs_newSup;
857                                 else
858                                         p_dn = slap_empty_bv;
859                                 dnParent(&entry->e_name, &p_dn);
860                                 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL); 
861                                 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
862                                 ber_dupbv( &new_ndn, &bv );
863                                 entry->e_name = new_dn;
864                                 entry->e_nname = new_ndn;
865
866                                 /* perform the modifications */
867                                 res = apply_modify_to_entry(entry, mods, op, rs);
868                                 if(res == LDAP_SUCCESS) {
869                                         rs->sr_err = move_entry(entry, &op->o_req_ndn,
870                                                                 &new_ndn,
871                                                                 &op->o_bd->be_nsuffix[0],
872                                                                 &ni->li_base_path);
873                                 }
874                                 else
875                                         rs->sr_err = res;
876                         }
877                 }
878         }
879         else /* entry was null */
880                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
881
882         if(entry != NULL)
883                 entry_free(entry);
884         rs->sr_text = "";
885         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
886         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
887         send_ldap_result(op, rs);
888         return 0;
889 }
890
891 static int ldif_back_compare(Operation *op, SlapReply *rs) {
892         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
893         Entry * e = NULL;
894         Attribute       *a;
895
896         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
897
898         e = (Entry *) get_entry(op, &ni->li_base_path);
899         if(e != NULL) {
900                 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
901                         a != NULL;
902                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
903                         rs->sr_err = LDAP_COMPARE_FALSE;
904                 
905                         if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
906                                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
907                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
908                                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
909                                                 op->o_tmpmemctx ) == 0) {
910                                 rs->sr_err = LDAP_COMPARE_TRUE;
911                                 break;
912                         }
913                 }
914         }
915         else {
916                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
917         }
918
919         if(e != NULL)
920                 entry_free(e);
921         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
922         send_ldap_result(op, rs);
923         return 0;
924 }
925
926 static int ldif_tool_entry_open(BackendDB * be, int mode) {
927         struct ldif_info *ni = (struct ldif_info *) be->be_private;
928         ni->tool_entries = NULL;
929         ni->tool_numentries = 0;
930         ni->tool_current = 0;
931         ni->tool_put_entry_flag = 0;
932         return 0;
933 }                                       
934
935 static int ldif_tool_entry_close(BackendDB * be) {
936         struct ldif_info *ni = (struct ldif_info *) be->be_private;
937
938         SLAP_FREE(ni->tool_entries);
939         return 0;
940 }
941
942 static ID ldif_tool_entry_first(BackendDB *be) {
943         struct ldif_info *ni = (struct ldif_info *) be->be_private;
944         ID id = 1; /* first entry in the array of entries shifted by one */
945
946         ni->tool_current = 1;
947         if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
948                 ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
949                         be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
950                 ni->tool_put_entry_flag = 0;
951         }
952         return id;
953 }
954
955 static ID ldif_tool_entry_next(BackendDB *be) {
956         struct ldif_info *ni = (struct ldif_info *) be->be_private;
957         ni->tool_current += 1;
958         if(ni->tool_put_entry_flag) {
959                 ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
960                         be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
961                 ni->tool_put_entry_flag = 0;
962         }
963         if(ni->tool_current > ni->tool_numentries)
964                 return NOID;
965         else
966                 return ni->tool_current;
967 }
968
969 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
970         struct ldif_info *ni = (struct ldif_info *) be->be_private;
971         Entry * e;
972
973         if(id > ni->tool_numentries || id < 1)
974                 return NULL;
975         else {
976                 e = ni->tool_entries[id - 1];
977                 ni->tool_entries[id - 1] = NULL;
978                 return e;
979         }
980 }
981
982 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
983         struct ldif_info *ni = (struct ldif_info *) be->be_private;
984         Attribute *save_attrs;
985         struct berval dn = e->e_nname;
986         struct berval leaf_path = BER_BVNULL;
987         struct stat stats;
988         int statres;
989         char textbuf[SLAP_TEXT_BUFLEN];
990         size_t textlen = sizeof textbuf;
991         int res = LDAP_SUCCESS;
992
993         dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
994
995         if(leaf_path.bv_val != NULL) {
996                 struct berval base = BER_BVNULL;
997                 /* build path to container, and path to ldif of container */
998                 get_parent_path(&leaf_path, &base);
999
1000                 statres = stat(base.bv_val, &stats); /* check if container exists */
1001                 if(statres == -1 && errno == ENOENT) { /* container missing */
1002                         base.bv_val[base.bv_len] = '.';
1003                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1004                         base.bv_val[base.bv_len] = '\0';
1005                         if(statres == -1 && errno == ENOENT) {
1006                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1007                         }
1008                         else if(statres != -1) { /* create parent */
1009                                 int mkdirres = mkdir(base.bv_val, 0750);
1010                                 if(mkdirres == -1) {
1011                                         res = LDAP_UNWILLING_TO_PERFORM;
1012                                 }
1013                         }
1014                         else
1015                                 res = LDAP_UNWILLING_TO_PERFORM;
1016                 }/* container was possibly created, move on to add the entry */
1017                 if(res == LDAP_SUCCESS) {
1018                         statres = stat(leaf_path.bv_val, &stats);
1019                         if(statres == -1 && errno == ENOENT) {
1020                                 res = (int) spew_entry(e, &leaf_path);
1021                         }
1022                         else /* it already exists */
1023                                 res = LDAP_ALREADY_EXISTS;
1024                 }
1025                 SLAP_FREE(base.bv_val);
1026                 SLAP_FREE(leaf_path.bv_val);
1027         }
1028
1029         if(res == LDAP_SUCCESS) {
1030                 ni->tool_put_entry_flag = 1;
1031                 return 1;
1032         }
1033         else
1034                 return NOID;
1035 }
1036
1037 static int
1038 ldif_back_db_init( BackendDB *be )
1039 {
1040         struct ldif_info *ni;
1041
1042         ni = ch_calloc( 1, sizeof(struct ldif_info) );
1043         be->be_private = ni;
1044         be->be_cf_table = be->bd_info->bi_cf_table;
1045         ldap_pvt_thread_mutex_init(&ni->li_mutex);
1046         return 0;
1047 }
1048
1049 static int
1050 ldif_back_db_destroy(
1051                            Backend      *be
1052                            )
1053 {
1054         struct ldif_info *ni = be->be_private;
1055         ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1056         free( be->be_private );
1057         return 0;
1058 }
1059
1060 static int
1061 ldif_back_db_open(
1062                         Backend *be
1063                         )
1064 {
1065         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1066         if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1067                 fprintf(stderr, "missing base path for back-ldif\n");
1068                 return 1;
1069         }
1070         return 0;
1071 }
1072
1073 int
1074 ldif_back_initialize(
1075                            BackendInfo  *bi
1076                            )
1077 {
1078         int rc;
1079
1080         bi->bi_cf_table = ldifcfg;
1081
1082         bi->bi_open = 0;
1083         bi->bi_close = 0;
1084         bi->bi_config = 0;
1085         bi->bi_destroy = 0;
1086
1087         bi->bi_db_init = ldif_back_db_init;
1088         bi->bi_db_config = config_generic_wrapper;
1089         bi->bi_db_open = ldif_back_db_open;
1090         bi->bi_db_close = 0;
1091         bi->bi_db_destroy = ldif_back_db_destroy;
1092
1093         bi->bi_op_bind = ldif_back_bind;
1094         bi->bi_op_unbind = 0;
1095         bi->bi_op_search = ldif_back_search;
1096         bi->bi_op_compare = ldif_back_compare;
1097         bi->bi_op_modify = ldif_back_modify;
1098         bi->bi_op_modrdn = ldif_back_modrdn;
1099         bi->bi_op_add = ldif_back_add;
1100         bi->bi_op_delete = ldif_back_delete;
1101         bi->bi_op_abandon = 0;
1102
1103         bi->bi_extended = 0;
1104
1105         bi->bi_chk_referrals = 0;
1106
1107         bi->bi_connection_init = 0;
1108         bi->bi_connection_destroy = 0;
1109
1110         bi->bi_tool_entry_open = ldif_tool_entry_open;
1111         bi->bi_tool_entry_close = ldif_tool_entry_close;
1112         bi->bi_tool_entry_first = ldif_tool_entry_first;
1113         bi->bi_tool_entry_next = ldif_tool_entry_next;
1114         bi->bi_tool_entry_get = ldif_tool_entry_get;
1115         bi->bi_tool_entry_put = ldif_tool_entry_put;
1116         bi->bi_tool_entry_reindex = 0;
1117         bi->bi_tool_sync = 0;
1118         
1119         bi->bi_tool_dn2id_get = 0;
1120         bi->bi_tool_id2entry_get = 0;
1121         bi->bi_tool_entry_modify = 0;
1122
1123         rc = config_register_schema( ldifcfg, ldifocs );
1124         if ( rc ) return rc;
1125         ldifcfg[0].ad = slap_schema.si_ad_objectClass;
1126         return 0;
1127 }