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' "
68 "SUP olcDatabaseConfig "
69 "MUST ( dbDirectory ) )", Cft_Database,
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 read_chars_total = 0;
114 entry_size = st.st_size;
115 entry = ch_malloc( entry_size+1 );
119 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
120 if(read_chars == -1) {
124 if(read_chars == 0) {
125 entry[read_chars_total] = '\0';
129 read_chars_total += read_chars;
130 entry_pos += read_chars;
136 static int spew_file(int fd, char * spew) {
139 int len = strlen(spew);
140 char * spewptr = spew;
142 while(written < len) {
143 writeres = write(fd, spewptr, len - written);
145 perror("could not spew write");
156 static int spew_entry(Entry * e, struct berval * path) {
161 char * entry_as_string;
163 openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
166 rs = LDAP_NO_SUCH_OBJECT;
168 rs = LDAP_UNWILLING_TO_PERFORM;
174 /* Only save the RDN onto disk */
175 dnRdn( &e->e_name, &rdn );
176 if ( rdn.bv_len != e->e_name.bv_len ) {
177 e->e_name.bv_val[rdn.bv_len] = '\0';
178 tmp = e->e_name.bv_len;
179 e->e_name.bv_len = rdn.bv_len;
183 entry_as_string = entry2str(e, &entry_length);
185 /* Restore full DN */
186 if ( rdn.bv_len != e->e_name.bv_len ) {
187 e->e_name.bv_val[e->e_name.bv_len] = ',';
188 e->e_name.bv_len = rdn.bv_len;
191 if(entry_as_string == NULL) {
192 rs = LDAP_UNWILLING_TO_PERFORM;
196 spew_res = spew_file(openres, entry_as_string);
199 rs = LDAP_UNWILLING_TO_PERFORM;
207 static Entry * get_entry_for_fd(int fd,
209 struct berval *pndn) {
210 char * entry = (char *) slurp_file(fd);
211 Entry * ldentry = NULL;
213 /* error reading file */
218 ldentry = str2entry(entry);
221 rdn = ldentry->e_name;
222 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
223 ch_free( rdn.bv_val );
224 rdn = ldentry->e_nname;
225 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
226 ch_free( rdn.bv_val );
240 static Entry * get_entry(Operation *op, struct berval *base_path) {
241 struct berval path, pdn, pndn;
244 dnParent(&op->o_req_dn, &pdn);
245 dnParent(&op->o_req_ndn, &pndn);
246 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
247 fd = open(path.bv_val, O_RDONLY);
248 /* error opening file (mebbe should log error) */
250 perror("failed to open file");
253 if(path.bv_val != NULL)
254 SLAP_FREE(path.bv_val);
255 return get_entry_for_fd(fd, &pdn, &pndn);
258 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
260 res->bv_len = name->bv_len + base->bv_len + 1;
261 res->bv_val = ch_malloc( res->bv_len + 1 );
262 strcpy(res->bv_val, base->bv_val);
263 ptr = res->bv_val + base->bv_len;
264 *ptr++ = LDAP_DIRSEP[0];
265 strcpy(ptr, name->bv_val);
268 typedef struct bvlist {
276 typedef struct enumCookie {
283 static void r_enum_tree(enumCookie *ck, struct berval *path,
284 struct berval *pdn, struct berval *pndn) {
288 if(ck->entries == NULL) {
289 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
290 ck->elen = ENTRY_BUFF_INCREMENT;
293 fd = open( path->bv_val, O_RDONLY );
295 Debug( LDAP_DEBUG_TRACE,
296 "=> ldif_enum_tree: failed to open %s\n",
297 path->bv_val, 0, 0 );
300 e = get_entry_for_fd(fd, pdn, pndn);
302 Debug( LDAP_DEBUG_ANY,
303 "=> ldif_enum_tree: failed to read entry for %s\n",
304 path->bv_val, 0, 0 );
308 if ( ck->scope == LDAP_SCOPE_BASE || ck->scope == LDAP_SCOPE_SUBTREE ) {
309 if(! (ck->eind < ck->elen)) { /* grow entries if necessary */
310 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
314 ck->entries[ck->eind] = e;
321 if ( ck->scope != LDAP_SCOPE_BASE ) {
323 bvlist *list = NULL, *ptr;
325 path->bv_len -= STRLENOF( LDIF );
326 path->bv_val[path->bv_len] = '\0';
328 dir_of_path = opendir(path->bv_val);
329 if(dir_of_path == NULL) {/* can't open directory */
330 Debug( LDAP_DEBUG_TRACE,
331 "=> ldif_enum_tree: failed to opendir %s\n",
332 path->bv_val, 0, 0 );
337 struct berval fname, itmp;
341 dir = readdir(dir_of_path);
342 if(dir == NULL) break; /* end of the directory */
343 fname.bv_len = strlen( dir->d_name );
344 if ( fname.bv_len <= STRLENOF( LDIF ))
346 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
348 fname.bv_val = dir->d_name;
350 bvl = ch_malloc( sizeof(bvlist) );
351 ber_dupbv( &bvl->bv, &fname );
352 BER_BVZERO( &bvl->num );
353 itmp.bv_val = strchr( bvl->bv.bv_val, '{' );
357 ptr = strchr( itmp.bv_val, '}' );
359 itmp.bv_len = ptr - itmp.bv_val;
360 ber_dupbv( &bvl->num, &itmp );
361 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
362 itmp.bv_val[0] = '\0';
363 bvl->off = itmp.bv_val - bvl->bv.bv_val;
367 for (ptr = list, prev = (bvlist *)&list; ptr;
368 prev = ptr, ptr = ptr->next) {
369 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
370 if ( !cmp && bvl->num.bv_val )
371 cmp = bvl->inum - ptr->inum;
379 closedir(dir_of_path);
381 if (ck->scope == LDAP_SCOPE_ONELEVEL)
382 ck->scope = LDAP_SCOPE_BASE;
383 else if ( ck->scope == LDAP_SCOPE_SUBORDINATE)
384 ck->scope = LDAP_SCOPE_SUBTREE;
391 if ( ptr->num.bv_val )
392 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
394 fullpath( path, &ptr->bv, &fpath );
395 r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
397 if ( ptr->num.bv_val )
398 free( ptr->num.bv_val );
399 free(ptr->bv.bv_val);
404 if ( fd ) entry_free( e );
408 static Entry ** enum_tree(
415 struct ldif_info *ni = (struct ldif_info *) be->be_private;
419 struct berval pdn, pndn;
422 dnParent( dn, &pdn );
423 dnParent( ndn, &pndn );
424 dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
425 r_enum_tree(&ck, &path, &pdn, &pndn);
430 /* Get the parent path plus the LDIF suffix */
431 static void get_parent_path(struct berval * dnpath, struct berval *res) {
432 int dnpathlen = dnpath->bv_len;
435 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
436 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
439 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
440 strncpy(res->bv_val, dnpath->bv_val, i);
441 strcpy(res->bv_val+i, LDIF);
442 res->bv_val[i] = '\0';
445 static int apply_modify_to_entry(Entry * entry,
446 Modifications * modlist,
450 char textbuf[SLAP_TEXT_BUFLEN];
451 size_t textlen = sizeof textbuf;
454 Modification *mods = NULL;
455 Attribute *save_attrs;
457 if (!acl_check_modlist(op, entry, modlist)) {
458 return LDAP_INSUFFICIENT_ACCESS;
461 /* save_attrs = entry->e_attrs; Why?
462 entry->e_attrs = attrs_dup(entry->e_attrs); */
464 for (; modlist != NULL; modlist = modlist->sml_next) {
465 mods = &modlist->sml_mod;
467 switch (mods->sm_op) {
469 rc = modify_add_values(entry, mods,
470 get_permissiveModify(op),
471 &rs->sr_text, textbuf,
475 case LDAP_MOD_DELETE:
476 rc = modify_delete_values(entry, mods,
477 get_permissiveModify(op),
478 &rs->sr_text, textbuf,
483 case LDAP_MOD_REPLACE:
484 rc = modify_replace_values(entry, mods,
485 get_permissiveModify(op),
486 &rs->sr_text, textbuf,
490 case LDAP_MOD_INCREMENT:
492 case SLAP_MOD_SOFTADD:
493 mods->sm_op = LDAP_MOD_ADD;
494 rc = modify_add_values(entry, mods,
495 get_permissiveModify(op),
496 &rs->sr_text, textbuf,
498 mods->sm_op = SLAP_MOD_SOFTADD;
499 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
506 if(rc != LDAP_SUCCESS) break;
509 if(rc == LDAP_SUCCESS) {
510 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
511 entry->e_ocflags = 0;
513 /* check that the entry still obeys the schema */
514 rc = entry_schema_check(op->o_bd, entry,
515 save_attrs, &rs->sr_text,
522 ldif_back_bind( Operation *op, SlapReply *rs )
524 struct ldif_info *ni = NULL;
525 Attribute * a = NULL;
526 AttributeDescription *password = slap_schema.si_ad_userPassword;
528 Entry * entry = NULL;
530 ni = (struct ldif_info *) op->o_bd->be_private;
531 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
532 entry = (Entry *) get_entry(op, &ni->li_base_path);
534 /* no object is found for them */
536 if(be_isroot_pw(op)) {
537 return_val = LDAP_SUCCESS;
540 else if(be_root_dn(op->o_bd)) {
541 return_val = LDAP_INVALID_CREDENTIALS;
542 rs->sr_err = LDAP_INVALID_CREDENTIALS;
546 rs->sr_err = LDAP_NO_SUCH_OBJECT;
552 /* they don't have userpassword */
553 if((a = attr_find(entry->e_attrs, password)) == NULL) {
554 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
559 /* authentication actually failed */
560 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
561 &rs->sr_text) != 0) {
562 rs->sr_err = LDAP_INVALID_CREDENTIALS;
567 /* let the front-end send success */
572 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
574 send_ldap_result( op, rs );
580 static int ldif_back_search(Operation *op, SlapReply *rs)
582 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
585 Entry ** entries = NULL;
587 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
588 entries = (Entry **) enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, op->ors_scope);
590 if(entries != NULL) {
591 for(i=0;i<numentries;i++) {
592 if(test_filter(op, entries[i], op->ors_filter) == LDAP_COMPARE_TRUE) {
593 rs->sr_entry = entries[i];
594 rs->sr_attrs = op->ors_attrs;
595 rs->sr_flags = REP_ENTRY_MODIFIABLE;
596 send_search_entry(op, rs);
598 entry_free(entries[i]);
601 rs->sr_err = LDAP_SUCCESS;
602 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
603 send_ldap_result(op, rs);
606 rs->sr_err = LDAP_BUSY;
607 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
608 send_ldap_result(op, rs);
614 static int ldif_back_add(Operation *op, SlapReply *rs) {
615 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
616 Entry * e = op->ora_e;
617 struct berval dn = e->e_nname;
618 struct berval leaf_path = BER_BVNULL;
621 char textbuf[SLAP_TEXT_BUFLEN];
622 size_t textlen = sizeof textbuf;
624 rs->sr_err = entry_schema_check(op->o_bd, e,
625 NULL, &rs->sr_text, textbuf, textlen);
626 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
628 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
630 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
632 if(leaf_path.bv_val != NULL) {
633 struct berval base = BER_BVNULL;
634 /* build path to container and ldif of container */
635 get_parent_path(&leaf_path, &base);
637 statres = stat(base.bv_val, &stats); /* check if container exists */
638 if(statres == -1 && errno == ENOENT) { /* container missing */
639 base.bv_val[base.bv_len] = '.';
640 statres = stat(base.bv_val, &stats); /* check for leaf node */
641 base.bv_val[base.bv_len] = '\0';
642 if(statres == -1 && errno == ENOENT) {
643 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
645 else if(statres != -1) { /* create parent */
646 int mkdirres = mkdir(base.bv_val, 0750);
648 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
652 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
653 }/* container was possibly created, move on to add the entry */
654 if(rs->sr_err == LDAP_SUCCESS) {
655 statres = stat(leaf_path.bv_val, &stats);
656 if(statres == -1 && errno == ENOENT) {
657 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
658 rs->sr_err = (int) spew_entry(e, &leaf_path);
659 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
661 else /* it already exists */
662 rs->sr_err = LDAP_ALREADY_EXISTS;
664 SLAP_FREE(base.bv_val);
665 SLAP_FREE(leaf_path.bv_val);
668 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
671 send_ldap_result(op, rs);
675 static int ldif_back_modify(Operation *op, SlapReply *rs) {
676 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
677 Modifications * modlst = op->orm_modlist;
678 struct berval path = BER_BVNULL;
679 Entry * entry = NULL;
682 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
683 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
685 entry = (Entry *) get_entry(op, &ni->li_base_path);
688 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
689 if(rs->sr_err == LDAP_SUCCESS) {
690 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
691 spew_res = spew_entry(entry, &path);
692 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
694 perror("could not output entry");
695 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
700 rs->sr_err = LDAP_NO_SUCH_OBJECT;
705 if(path.bv_val != NULL)
706 SLAP_FREE(path.bv_val);
708 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
709 send_ldap_result(op, rs);
713 static int ldif_back_delete(Operation *op, SlapReply *rs) {
714 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
715 struct berval path = BER_BVNULL;
718 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
719 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
721 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
722 res = rmdir(path.bv_val);
723 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
724 if ( res && errno != ENOENT ) {
725 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
727 res = unlink(path.bv_val);
732 rs->sr_err = LDAP_NO_SUCH_OBJECT;
734 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
737 rs->sr_err = LDAP_SUCCESS;
739 SLAP_FREE(path.bv_val);
740 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
741 send_ldap_result(op, rs);
746 static int move_entry(Entry * entry, struct berval * ndn,
747 struct berval * newndn, struct berval * rootdn,
748 struct berval * base_path) {
752 struct berval newpath;
754 dn2path(ndn, rootdn, base_path, &path);
755 dn2path(newndn, rootdn, base_path, &newpath);
757 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
758 /* some object doesn't exist */
759 res = LDAP_NO_SUCH_OBJECT;
761 else { /* do the modrdn */
762 exists_res = open(newpath.bv_val, O_RDONLY);
763 if(exists_res == -1 && errno == ENOENT) {
764 res = spew_entry(entry, &newpath);
766 /* if this fails we should log something bad */
767 res = unlink(path.bv_val);
772 res = LDAP_NO_SUCH_OBJECT;
774 res = LDAP_UNWILLING_TO_PERFORM;
775 unlink(newpath.bv_val); /* in case file was created */
778 else if(exists_res) {
779 int close_res = close(exists_res);
780 res = LDAP_ALREADY_EXISTS;
781 if(close_res == -1) {
782 /* log heinous error */
786 res = LDAP_UNWILLING_TO_PERFORM;
790 if(newpath.bv_val != NULL)
791 SLAP_FREE(newpath.bv_val);
792 if(path.bv_val != NULL)
793 SLAP_FREE(path.bv_val);
797 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
798 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
799 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
800 struct berval * new_parent_dn = NULL;
801 struct berval p_dn, bv = {0, NULL};
802 Entry * entry = NULL;
803 LDAPRDN new_rdn = NULL;
804 LDAPRDN old_rdn = NULL;
805 Modifications * mods = NULL;
808 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
809 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
810 entry = (Entry *) get_entry(op, &ni->li_base_path);
812 /* build the mods to the entry */
814 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
815 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
816 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
818 else if(op->oq_modrdn.rs_deleteoldrdn &&
819 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
820 LDAP_DN_FORMAT_LDAP)) {
821 rs->sr_err = LDAP_OTHER;
823 else { /* got both rdns successfully, ready to build mods */
824 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
826 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
828 else { /* built mods successfully */
830 /* build new dn, and new ndn for the entry */
831 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
832 p_dn = *op->oq_modrdn.rs_newSup;
834 p_dn = slap_empty_bv;
835 dnParent(&entry->e_name, &p_dn);
836 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
837 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
838 ber_dupbv( &new_ndn, &bv );
839 entry->e_name = new_dn;
840 entry->e_nname = new_ndn;
842 /* perform the modifications */
843 res = apply_modify_to_entry(entry, mods, op, rs);
844 if(res == LDAP_SUCCESS) {
845 rs->sr_err = move_entry(entry, &op->o_req_ndn,
847 &op->o_bd->be_nsuffix[0],
855 else /* entry was null */
856 rs->sr_err = LDAP_NO_SUCH_OBJECT;
861 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
862 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
863 send_ldap_result(op, rs);
867 static int ldif_back_compare(Operation *op, SlapReply *rs) {
868 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
872 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
874 e = (Entry *) get_entry(op, &ni->li_base_path);
876 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
878 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
879 rs->sr_err = LDAP_COMPARE_FALSE;
881 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
882 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
883 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
884 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
885 op->o_tmpmemctx ) == 0) {
886 rs->sr_err = LDAP_COMPARE_TRUE;
892 rs->sr_err = LDAP_NO_SUCH_OBJECT;
897 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
898 send_ldap_result(op, rs);
902 static int ldif_tool_entry_open(BackendDB * be, int mode) {
903 struct ldif_info *ni = (struct ldif_info *) be->be_private;
904 ni->tool_entries = NULL;
905 ni->tool_numentries = 0;
906 ni->tool_current = 0;
907 ni->tool_put_entry_flag = 0;
911 static int ldif_tool_entry_close(BackendDB * be) {
912 struct ldif_info *ni = (struct ldif_info *) be->be_private;
914 SLAP_FREE(ni->tool_entries);
918 static ID ldif_tool_entry_first(BackendDB *be) {
919 struct ldif_info *ni = (struct ldif_info *) be->be_private;
920 ID id = 1; /* first entry in the array of entries shifted by one */
922 ni->tool_current = 1;
923 if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
924 ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
925 be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
926 ni->tool_put_entry_flag = 0;
931 static ID ldif_tool_entry_next(BackendDB *be) {
932 struct ldif_info *ni = (struct ldif_info *) be->be_private;
933 ni->tool_current += 1;
934 if(ni->tool_put_entry_flag) {
935 ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
936 be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
937 ni->tool_put_entry_flag = 0;
939 if(ni->tool_current > ni->tool_numentries)
942 return ni->tool_current;
945 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
946 struct ldif_info *ni = (struct ldif_info *) be->be_private;
949 if(id > ni->tool_numentries || id < 1)
952 e = ni->tool_entries[id - 1];
953 ni->tool_entries[id - 1] = NULL;
958 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
959 struct ldif_info *ni = (struct ldif_info *) be->be_private;
960 Attribute *save_attrs;
961 struct berval dn = e->e_nname;
962 struct berval leaf_path = BER_BVNULL;
965 char textbuf[SLAP_TEXT_BUFLEN];
966 size_t textlen = sizeof textbuf;
967 int res = LDAP_SUCCESS;
969 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
971 if(leaf_path.bv_val != NULL) {
972 struct berval base = BER_BVNULL;
973 /* build path to container, and path to ldif of container */
974 get_parent_path(&leaf_path, &base);
976 statres = stat(base.bv_val, &stats); /* check if container exists */
977 if(statres == -1 && errno == ENOENT) { /* container missing */
978 base.bv_val[base.bv_len] = '.';
979 statres = stat(base.bv_val, &stats); /* check for leaf node */
980 base.bv_val[base.bv_len] = '\0';
981 if(statres == -1 && errno == ENOENT) {
982 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
984 else if(statres != -1) { /* create parent */
985 int mkdirres = mkdir(base.bv_val, 0750);
987 res = LDAP_UNWILLING_TO_PERFORM;
991 res = LDAP_UNWILLING_TO_PERFORM;
992 }/* container was possibly created, move on to add the entry */
993 if(res == LDAP_SUCCESS) {
994 statres = stat(leaf_path.bv_val, &stats);
995 if(statres == -1 && errno == ENOENT) {
996 res = (int) spew_entry(e, &leaf_path);
998 else /* it already exists */
999 res = LDAP_ALREADY_EXISTS;
1001 SLAP_FREE(base.bv_val);
1002 SLAP_FREE(leaf_path.bv_val);
1005 if(res == LDAP_SUCCESS) {
1006 ni->tool_put_entry_flag = 1;
1014 ldif_back_db_init( BackendDB *be )
1016 struct ldif_info *ni;
1018 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1019 be->be_private = ni;
1020 be->be_cf_table = be->bd_info->bi_cf_table;
1021 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1026 ldif_back_db_destroy(
1030 struct ldif_info *ni = be->be_private;
1031 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1032 free( be->be_private );
1041 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1042 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1043 fprintf(stderr, "missing base path for back-ldif\n");
1050 ldif_back_initialize(
1056 bi->bi_cf_table = ldifcfg;
1063 bi->bi_db_init = ldif_back_db_init;
1064 bi->bi_db_config = config_generic_wrapper;
1065 bi->bi_db_open = ldif_back_db_open;
1066 bi->bi_db_close = 0;
1067 bi->bi_db_destroy = ldif_back_db_destroy;
1069 bi->bi_op_bind = ldif_back_bind;
1070 bi->bi_op_unbind = 0;
1071 bi->bi_op_search = ldif_back_search;
1072 bi->bi_op_compare = ldif_back_compare;
1073 bi->bi_op_modify = ldif_back_modify;
1074 bi->bi_op_modrdn = ldif_back_modrdn;
1075 bi->bi_op_add = ldif_back_add;
1076 bi->bi_op_delete = ldif_back_delete;
1077 bi->bi_op_abandon = 0;
1079 bi->bi_extended = 0;
1081 bi->bi_chk_referrals = 0;
1083 bi->bi_connection_init = 0;
1084 bi->bi_connection_destroy = 0;
1086 bi->bi_tool_entry_open = ldif_tool_entry_open;
1087 bi->bi_tool_entry_close = ldif_tool_entry_close;
1088 bi->bi_tool_entry_first = ldif_tool_entry_first;
1089 bi->bi_tool_entry_next = ldif_tool_entry_next;
1090 bi->bi_tool_entry_get = ldif_tool_entry_get;
1091 bi->bi_tool_entry_put = ldif_tool_entry_put;
1092 bi->bi_tool_entry_reindex = 0;
1093 bi->bi_tool_sync = 0;
1095 bi->bi_tool_dn2id_get = 0;
1096 bi->bi_tool_id2entry_get = 0;
1097 bi->bi_tool_entry_modify = 0;
1099 rc = config_register_schema( ldifcfg, ldifocs );
1100 if ( rc ) return rc;
1101 ldifcfg[0].ad = slap_schema.si_ad_objectClass;