1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was originally developed by Eric Stokes for inclusion
18 * in OpenLDAP Software.
23 #include <ac/string.h>
24 #include <sys/types.h>
26 #include <ac/dirent.h>
29 #include <ac/unistd.h>
35 struct berval li_base_path;
37 Entry ** tool_entries;
38 int tool_put_entry_flag;
40 ldap_pvt_thread_mutex_t li_mutex;
45 #define ENTRY_BUFF_INCREMENT 500
47 static ObjectClass *ldif_oc;
49 static ConfigDriver ldif_cf;
51 static ConfigTable ldifcfg[] = {
52 { "", "", 0, 0, 0, ARG_MAGIC,
53 ldif_cf, NULL, NULL, NULL },
54 { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
55 (void *)offsetof(struct ldif_info, li_base_path),
56 "( OLcfgAt:1.1 NAME 'dbDirectory' "
57 "DESC 'Directory for database content' "
58 "EQUALITY caseIgnoreMatch "
59 "SYNTAX OMsDirectoryString )", NULL, NULL },
60 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
61 NULL, NULL, NULL, NULL }
64 static ConfigOCs ldifocs[] = {
67 "DESC 'LDIF backend configuration' "
69 "MAY ( dbDirectory ) )",
75 ldif_cf( ConfigArgs *c )
77 if ( c->op == SLAP_CONFIG_EMIT ) {
78 value_add_one( &c->rvalue_vals, &ldif_oc->soc_cname );
85 dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
88 char *ptr, *sep, *end;
90 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
91 res->bv_val = ch_malloc( res->bv_len + 1 );
92 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
93 *ptr++ = LDAP_DIRSEP[0];
94 ptr = lutil_strcopy( ptr, rootdn->bv_val );
95 end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
96 while ( end > dn->bv_val ) {
97 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
98 *ptr++ = LDAP_DIRSEP[0];
99 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
105 static char * slurp_file(int fd) {
106 int entry_buf_size = 40 * ENTRY_BUFF_INCREMENT;
107 int read_chars_total = 0;
109 int entry_size = 40 * ENTRY_BUFF_INCREMENT;
110 char * entry = (char *) malloc(sizeof(char) * 40 * ENTRY_BUFF_INCREMENT);
111 char * entry_pos = entry;
114 if(entry_size - read_chars_total == 0) {
115 entry = (char *) realloc(entry, sizeof(char) * 2 * entry_size);
116 entry_size = 2 * entry_size;
118 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
119 if(read_chars == -1) {
123 entry_pos += read_chars;
124 if(read_chars == 0) {
125 if(entry_size - read_chars_total > 0)
126 entry[read_chars_total] = '\0';
128 entry = (char *) realloc(entry, sizeof(char) * entry_size + 1);
129 entry_size = entry_size + 1;
130 entry[read_chars_total] = '\0';
135 read_chars_total += read_chars;
141 static int spew_file(int fd, char * spew) {
144 int len = strlen(spew);
145 char * spewptr = spew;
147 while(written < len) {
148 writeres = write(fd, spewptr, len - written);
150 perror("could not spew write");
161 static int spew_entry(Entry * e, struct berval * path) {
166 char * entry_as_string;
168 openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
171 rs = LDAP_NO_SUCH_OBJECT;
173 rs = LDAP_UNWILLING_TO_PERFORM;
176 entry_as_string = entry2str(e, &entry_length);
177 if(entry_as_string == NULL) {
178 rs = LDAP_UNWILLING_TO_PERFORM;
182 spew_res = spew_file(openres, entry_as_string);
185 rs = LDAP_UNWILLING_TO_PERFORM;
193 static Entry * get_entry_for_fd(int fd) {
194 char * entry = (char *) slurp_file(fd);
195 Entry * ldentry = NULL;
197 /* error reading file */
202 ldentry = str2entry(entry);
215 static Entry * get_entry(struct berval * dn, struct berval * rootdn, struct berval * base_path) {
219 dn2path(dn, rootdn, base_path, &path);
220 fd = open(path.bv_val, O_RDONLY);
221 /* error opening file (mebbe should log error) */
223 perror("failed to open file");
226 if(path.bv_val != NULL)
227 SLAP_FREE(path.bv_val);
228 return get_entry_for_fd(fd);
231 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
233 res->bv_len = name->bv_len + base->bv_len + 1;
234 res->bv_val = ch_malloc( res->bv_len + 1 );
235 strcpy(res->bv_val, base->bv_val);
236 ptr = res->bv_val + base->bv_len;
237 *ptr++ = LDAP_DIRSEP[0];
238 strcpy(ptr, name->bv_val);
241 typedef struct bvlist {
246 static Entry ** r_enum_tree(Entry ** entries, int *elen, int *eind,
247 struct berval * path, int scope) {
249 if(entries == NULL) {
250 entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
251 *elen = ENTRY_BUFF_INCREMENT;
254 if ( scope == LDAP_SCOPE_BASE || scope == LDAP_SCOPE_SUBTREE ) {
257 if(! (*eind < *elen)) { /* grow entries if necessary */
258 entries = (Entry **) SLAP_REALLOC(entries, sizeof(Entry *) * (*elen) * 2);
261 fd = open( path->bv_val, O_RDONLY );
263 Debug( LDAP_DEBUG_TRACE,
264 "=> ldif_enum_tree: failed to open %s\n",
265 path->bv_val, 0, 0 );
269 e = get_entry_for_fd(fd);
275 Debug( LDAP_DEBUG_ANY,
276 "=> ldif_enum_tree: failed to read entry for %s\n",
277 path->bv_val, 0, 0 );
281 if ( scope != LDAP_SCOPE_BASE ) {
283 bvlist *list = NULL, *ptr;
285 path->bv_len -= STRLENOF( LDIF );
286 path->bv_val[path->bv_len] = '\0';
288 dir_of_path = opendir(path->bv_val);
289 if(dir_of_path == NULL) {/* can't open directory */
290 Debug( LDAP_DEBUG_TRACE,
291 "=> ldif_enum_tree: failed to opendir %s\n",
292 path->bv_val, 0, 0 );
301 dir = readdir(dir_of_path);
302 if(dir == NULL) break; /* end of the directory */
303 fname.bv_len = strlen( dir->d_name );
304 if ( fname.bv_len <= STRLENOF( LDIF ))
306 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
308 fname.bv_val = dir->d_name;
310 bvl = ch_malloc( sizeof(bvlist) );
311 ber_dupbv( &bvl->bv, &fname );
313 for (ptr = list, prev = (bvlist *)&list; ptr;
314 prev = ptr, ptr = ptr->next) {
315 if ( strcmp( bvl->bv.bv_val, ptr->bv.bv_val ) < 0 )
322 closedir(dir_of_path);
324 for ( ptr = list; ptr; ptr=ptr->next ) {
326 fullpath( path, &ptr->bv, &fpath );
327 entries = r_enum_tree(entries, elen, eind, &fpath,
328 scope == LDAP_SCOPE_ONELEVEL ? LDAP_SCOPE_BASE : scope);
336 static Entry ** enum_tree(BackendDB *be, struct berval * ndn, int * length, int scope) {
337 struct ldif_info *ni = (struct ldif_info *) be->be_private;
341 dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
342 return r_enum_tree(NULL, &index, length, &path, scope);
345 /* Get the parent path plus the LDIF suffix */
346 static void get_parent_path(struct berval * dnpath, struct berval *res) {
347 int dnpathlen = dnpath->bv_len;
350 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
351 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
354 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
355 strncpy(res->bv_val, dnpath->bv_val, i);
356 strcpy(res->bv_val+i, LDIF);
357 res->bv_val[i] = '\0';
360 static int apply_modify_to_entry(Entry * entry,
361 Modifications * modlist,
365 char textbuf[SLAP_TEXT_BUFLEN];
366 size_t textlen = sizeof textbuf;
369 Modification *mods = NULL;
370 Attribute *save_attrs;
372 if (!acl_check_modlist(op, entry, modlist)) {
373 return LDAP_INSUFFICIENT_ACCESS;
376 /* save_attrs = entry->e_attrs; Why?
377 entry->e_attrs = attrs_dup(entry->e_attrs); */
379 for (; modlist != NULL; modlist = modlist->sml_next) {
380 mods = &modlist->sml_mod;
382 switch (mods->sm_op) {
384 rc = modify_add_values(entry, mods,
385 get_permissiveModify(op),
386 &rs->sr_text, textbuf,
390 case LDAP_MOD_DELETE:
391 rc = modify_delete_values(entry, mods,
392 get_permissiveModify(op),
393 &rs->sr_text, textbuf,
398 case LDAP_MOD_REPLACE:
399 rc = modify_replace_values(entry, mods,
400 get_permissiveModify(op),
401 &rs->sr_text, textbuf,
405 case LDAP_MOD_INCREMENT:
407 case SLAP_MOD_SOFTADD:
408 mods->sm_op = LDAP_MOD_ADD;
409 rc = modify_add_values(entry, mods,
410 get_permissiveModify(op),
411 &rs->sr_text, textbuf,
413 mods->sm_op = SLAP_MOD_SOFTADD;
414 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
421 if(rc != LDAP_SUCCESS) break;
424 if(rc == LDAP_SUCCESS) {
425 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
426 entry->e_ocflags = 0;
428 /* check that the entry still obeys the schema */
429 rc = entry_schema_check(op->o_bd, entry,
430 save_attrs, &rs->sr_text,
437 ldif_back_bind( Operation *op, SlapReply *rs )
439 struct ldif_info *ni = NULL;
440 Attribute * a = NULL;
441 AttributeDescription *password = slap_schema.si_ad_userPassword;
443 Entry * entry = NULL;
445 ni = (struct ldif_info *) op->o_bd->be_private;
446 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
447 entry = (Entry *) get_entry(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path);
449 /* no object is found for them */
451 if(be_isroot_pw(op)) {
452 return_val = LDAP_SUCCESS;
455 else if(be_root_dn(op->o_bd)) {
456 return_val = LDAP_INVALID_CREDENTIALS;
457 rs->sr_err = LDAP_INVALID_CREDENTIALS;
461 rs->sr_err = LDAP_NO_SUCH_OBJECT;
467 /* they don't have userpassword */
468 if((a = attr_find(entry->e_attrs, password)) == NULL) {
469 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
474 /* authentication actually failed */
475 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
476 &rs->sr_text) != 0) {
477 rs->sr_err = LDAP_INVALID_CREDENTIALS;
482 /* let the front-end send success */
487 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
489 send_ldap_result( op, rs );
495 static int ldif_back_search(Operation *op, SlapReply *rs)
497 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
500 Entry ** entries = NULL;
502 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
503 entries = (Entry **) enum_tree(op->o_bd, &op->o_req_ndn, &numentries, op->ors_scope);
505 if(entries != NULL) {
506 for(i=0;i<numentries;i++) {
507 if(test_filter(op, entries[i], op->ors_filter) == LDAP_COMPARE_TRUE) {
508 rs->sr_entry = entries[i];
509 rs->sr_attrs = op->ors_attrs;
510 rs->sr_flags = REP_ENTRY_MODIFIABLE;
511 send_search_entry(op, rs);
513 entry_free(entries[i]);
516 rs->sr_err = LDAP_SUCCESS;
517 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
518 send_ldap_result(op, rs);
521 rs->sr_err = LDAP_BUSY;
522 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
523 send_ldap_result(op, rs);
529 static int ldif_back_add(Operation *op, SlapReply *rs) {
530 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
531 Entry * e = op->ora_e;
532 struct berval dn = e->e_nname;
533 struct berval leaf_path = BER_BVNULL;
536 char textbuf[SLAP_TEXT_BUFLEN];
537 size_t textlen = sizeof textbuf;
539 rs->sr_err = entry_schema_check(op->o_bd, e,
540 NULL, &rs->sr_text, textbuf, textlen);
541 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
543 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
545 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
547 if(leaf_path.bv_val != NULL) {
548 struct berval base = BER_BVNULL;
549 /* build path to container and ldif of container */
550 get_parent_path(&leaf_path, &base);
552 statres = stat(base.bv_val, &stats); /* check if container exists */
553 if(statres == -1 && errno == ENOENT) { /* container missing */
554 base.bv_val[base.bv_len] = '.';
555 statres = stat(base.bv_val, &stats); /* check for leaf node */
556 base.bv_val[base.bv_len] = '\0';
557 if(statres == -1 && errno == ENOENT) {
558 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
560 else if(statres != -1) { /* create parent */
561 int mkdirres = mkdir(base.bv_val, 0750);
563 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
567 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
568 }/* container was possibly created, move on to add the entry */
569 if(rs->sr_err == LDAP_SUCCESS) {
570 statres = stat(leaf_path.bv_val, &stats);
571 if(statres == -1 && errno == ENOENT) {
572 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
573 rs->sr_err = (int) spew_entry(e, &leaf_path);
574 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
576 else /* it already exists */
577 rs->sr_err = LDAP_ALREADY_EXISTS;
579 SLAP_FREE(base.bv_val);
580 SLAP_FREE(leaf_path.bv_val);
583 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
586 send_ldap_result(op, rs);
590 static int ldif_back_modify(Operation *op, SlapReply *rs) {
591 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
592 Modifications * modlst = op->orm_modlist;
593 struct berval path = BER_BVNULL;
594 Entry * entry = NULL;
597 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
598 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
600 entry = (Entry *) get_entry(&op->o_req_ndn, &op->o_bd->be_nsuffix[0],
604 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
605 if(rs->sr_err == LDAP_SUCCESS) {
606 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
607 spew_res = spew_entry(entry, &path);
608 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
610 perror("could not output entry");
611 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
616 rs->sr_err = LDAP_NO_SUCH_OBJECT;
621 if(path.bv_val != NULL)
622 SLAP_FREE(path.bv_val);
624 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
625 send_ldap_result(op, rs);
629 static int ldif_back_delete(Operation *op, SlapReply *rs) {
630 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
631 struct berval path = BER_BVNULL;
634 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
635 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
636 res = unlink(path.bv_val);
640 rs->sr_err = LDAP_NO_SUCH_OBJECT;
642 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
645 rs->sr_err = LDAP_SUCCESS;
647 SLAP_FREE(path.bv_val);
648 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
649 send_ldap_result(op, rs);
653 static int is_leaf_node(struct berval * path) {
655 int path_len = path->bv_len;
658 path->bv_val[path->bv_len - STRLENOF(LDIF)] = '\0';
659 nonleafnode = opendir(path->bv_val);
660 path->bv_val[path->bv_len - STRLENOF(LDIF)] = '.';
661 if(nonleafnode == NULL) {
665 closedir(nonleafnode);
671 static int move_entry(Entry * entry, struct berval * ndn,
672 struct berval * newndn, struct berval * rootdn,
673 struct berval * base_path) {
677 struct berval newpath;
679 dn2path(ndn, rootdn, base_path, &path);
680 dn2path(newndn, rootdn, base_path, &newpath);
682 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
683 /* some object doesn't exist */
684 res = LDAP_NO_SUCH_OBJECT;
686 else if(! is_leaf_node(&path)) { /* entry is not a leaf node */
687 res = LDAP_NOT_ALLOWED_ON_NONLEAF;
689 else { /* do the modrdn */
690 exists_res = open(newpath.bv_val, O_RDONLY);
691 if(exists_res == -1 && errno == ENOENT) {
692 res = spew_entry(entry, &newpath);
694 /* if this fails we should log something bad */
695 res = unlink(path.bv_val);
700 res = LDAP_NO_SUCH_OBJECT;
702 res = LDAP_UNWILLING_TO_PERFORM;
703 unlink(newpath.bv_val); /* in case file was created */
706 else if(exists_res) {
707 int close_res = close(exists_res);
708 res = LDAP_ALREADY_EXISTS;
709 if(close_res == -1) {
710 /* log heinous error */
714 res = LDAP_UNWILLING_TO_PERFORM;
718 if(newpath.bv_val != NULL)
719 SLAP_FREE(newpath.bv_val);
720 if(path.bv_val != NULL)
721 SLAP_FREE(path.bv_val);
725 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
726 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
727 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
728 struct berval * new_parent_dn = NULL;
729 struct berval p_dn, bv = {0, NULL};
730 Entry * entry = NULL;
731 LDAPRDN new_rdn = NULL;
732 LDAPRDN old_rdn = NULL;
733 Modifications * mods = NULL;
736 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
737 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
738 entry = (Entry *) get_entry(&op->o_req_ndn, &op->o_bd->be_nsuffix[0],
741 /* build the mods to the entry */
743 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
744 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
745 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
747 else if(op->oq_modrdn.rs_deleteoldrdn &&
748 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
749 LDAP_DN_FORMAT_LDAP)) {
750 rs->sr_err = LDAP_OTHER;
752 else { /* got both rdns successfully, ready to build mods */
753 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
755 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
757 else { /* built mods successfully */
759 /* build new dn, and new ndn for the entry */
760 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
761 p_dn = *op->oq_modrdn.rs_newSup;
763 p_dn = slap_empty_bv;
764 dnParent(&entry->e_name, &p_dn);
765 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
766 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
767 ber_dupbv( &new_ndn, &bv );
768 entry->e_name = new_dn;
769 entry->e_nname = new_ndn;
771 /* perform the modifications */
772 res = apply_modify_to_entry(entry, mods, op, rs);
773 if(res == LDAP_SUCCESS) {
774 rs->sr_err = move_entry(entry, &op->o_req_ndn,
776 &op->o_bd->be_nsuffix[0],
784 else /* entry was null */
785 rs->sr_err = LDAP_NO_SUCH_OBJECT;
790 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
791 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
792 send_ldap_result(op, rs);
796 static int ldif_back_compare(Operation *op, SlapReply *rs) {
797 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
801 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
803 e = (Entry *) get_entry(&op->o_req_ndn, &op->o_bd->be_nsuffix[0],
806 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
808 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
809 rs->sr_err = LDAP_COMPARE_FALSE;
811 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
812 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
813 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
814 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
815 op->o_tmpmemctx ) == 0) {
816 rs->sr_err = LDAP_COMPARE_TRUE;
822 rs->sr_err = LDAP_NO_SUCH_OBJECT;
827 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
828 send_ldap_result(op, rs);
832 static int ldif_tool_entry_open(BackendDB * be, int mode) {
833 struct ldif_info *ni = (struct ldif_info *) be->be_private;
834 ni->tool_entries = NULL;
835 ni->tool_numentries = 0;
836 ni->tool_current = 0;
837 ni->tool_put_entry_flag = 0;
841 static int ldif_tool_entry_close(BackendDB * be) {
842 struct ldif_info *ni = (struct ldif_info *) be->be_private;
844 SLAP_FREE(ni->tool_entries);
848 static ID ldif_tool_entry_first(BackendDB *be) {
849 struct ldif_info *ni = (struct ldif_info *) be->be_private;
850 ID id = 1; /* first entry in the array of entries shifted by one */
852 ni->tool_current = 1;
853 if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
854 ni->tool_entries = (Entry **) enum_tree(be, &be->be_nsuffix[0],
855 &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
856 ni->tool_put_entry_flag = 0;
861 static ID ldif_tool_entry_next(BackendDB *be) {
862 struct ldif_info *ni = (struct ldif_info *) be->be_private;
863 ni->tool_current += 1;
864 if(ni->tool_put_entry_flag) {
865 ni->tool_entries = (Entry **) enum_tree(be, &be->be_nsuffix[0],
866 &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
867 ni->tool_put_entry_flag = 0;
869 if(ni->tool_current > ni->tool_numentries)
872 return ni->tool_current;
875 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
876 struct ldif_info *ni = (struct ldif_info *) be->be_private;
879 if(id > ni->tool_numentries || id < 1)
882 e = ni->tool_entries[id - 1];
883 ni->tool_entries[id - 1] = NULL;
888 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
889 struct ldif_info *ni = (struct ldif_info *) be->be_private;
890 Attribute *save_attrs;
891 struct berval dn = e->e_nname;
892 struct berval leaf_path = BER_BVNULL;
895 char textbuf[SLAP_TEXT_BUFLEN];
896 size_t textlen = sizeof textbuf;
897 int res = LDAP_SUCCESS;
899 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
901 if(leaf_path.bv_val != NULL) {
902 struct berval base = BER_BVNULL;
903 /* build path to container, and path to ldif of container */
904 get_parent_path(&leaf_path, &base);
906 statres = stat(base.bv_val, &stats); /* check if container exists */
907 if(statres == -1 && errno == ENOENT) { /* container missing */
908 base.bv_val[base.bv_len] = '.';
909 statres = stat(base.bv_val, &stats); /* check for leaf node */
910 base.bv_val[base.bv_len] = '\0';
911 if(statres == -1 && errno == ENOENT) {
912 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
914 else if(statres != -1) { /* create parent */
915 int mkdirres = mkdir(base.bv_val, 0750);
917 res = LDAP_UNWILLING_TO_PERFORM;
921 res = LDAP_UNWILLING_TO_PERFORM;
922 }/* container was possibly created, move on to add the entry */
923 if(res == LDAP_SUCCESS) {
924 statres = stat(leaf_path.bv_val, &stats);
925 if(statres == -1 && errno == ENOENT) {
926 res = (int) spew_entry(e, &leaf_path);
928 else /* it already exists */
929 res = LDAP_ALREADY_EXISTS;
931 SLAP_FREE(base.bv_val);
932 SLAP_FREE(leaf_path.bv_val);
935 if(res == LDAP_SUCCESS) {
936 ni->tool_put_entry_flag = 1;
944 ldif_back_db_init( BackendDB *be )
946 struct ldif_info *ni;
948 ni = ch_calloc( 1, sizeof(struct ldif_info) );
950 be->be_cf_table = be->bd_info->bi_cf_table;
951 ldap_pvt_thread_mutex_init(&ni->li_mutex);
956 ldif_back_db_destroy(
960 struct ldif_info *ni = be->be_private;
961 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
962 free( be->be_private );
971 struct ldif_info *ni = (struct ldif_info *) be->be_private;
972 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
973 fprintf(stderr, "missing base path for back-ldif\n");
980 ldif_back_initialize(
986 bi->bi_cf_table = ldifcfg;
993 bi->bi_db_init = ldif_back_db_init;
994 bi->bi_db_config = config_generic_wrapper;
995 bi->bi_db_open = ldif_back_db_open;
997 bi->bi_db_destroy = ldif_back_db_destroy;
999 bi->bi_op_bind = ldif_back_bind;
1000 bi->bi_op_unbind = 0;
1001 bi->bi_op_search = ldif_back_search;
1002 bi->bi_op_compare = ldif_back_compare;
1003 bi->bi_op_modify = ldif_back_modify;
1004 bi->bi_op_modrdn = ldif_back_modrdn;
1005 bi->bi_op_add = ldif_back_add;
1006 bi->bi_op_delete = ldif_back_delete;
1007 bi->bi_op_abandon = 0;
1009 bi->bi_extended = 0;
1011 bi->bi_chk_referrals = 0;
1013 bi->bi_connection_init = 0;
1014 bi->bi_connection_destroy = 0;
1016 bi->bi_tool_entry_open = ldif_tool_entry_open;
1017 bi->bi_tool_entry_close = ldif_tool_entry_close;
1018 bi->bi_tool_entry_first = ldif_tool_entry_first;
1019 bi->bi_tool_entry_next = ldif_tool_entry_next;
1020 bi->bi_tool_entry_get = ldif_tool_entry_get;
1021 bi->bi_tool_entry_put = ldif_tool_entry_put;
1022 bi->bi_tool_entry_reindex = 0;
1023 bi->bi_tool_sync = 0;
1025 bi->bi_tool_dn2id_get = 0;
1026 bi->bi_tool_id2entry_get = 0;
1027 bi->bi_tool_entry_modify = 0;
1029 rc = init_config_attrs( ldifcfg );
1030 if ( rc ) return rc;
1031 ldifcfg[0].ad = slap_schema.si_ad_objectClass;
1032 rc = init_config_ocs( ldifocs );