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