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 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;
179 /* Only save the RDN onto disk */
180 dnRdn( &e->e_name, &rdn );
181 if ( rdn.bv_len != e->e_name.bv_len ) {
182 e->e_name.bv_val[rdn.bv_len] = '\0';
183 tmp = e->e_name.bv_len;
184 e->e_name.bv_len = rdn.bv_len;
188 entry_as_string = entry2str(e, &entry_length);
190 /* Restore full DN */
191 if ( rdn.bv_len != e->e_name.bv_len ) {
192 e->e_name.bv_val[e->e_name.bv_len] = ',';
193 e->e_name.bv_len = rdn.bv_len;
196 if(entry_as_string == NULL) {
197 rs = LDAP_UNWILLING_TO_PERFORM;
201 spew_res = spew_file(openres, entry_as_string);
204 rs = LDAP_UNWILLING_TO_PERFORM;
212 static Entry * get_entry_for_fd(int fd,
214 struct berval *pndn) {
215 char * entry = (char *) slurp_file(fd);
216 Entry * ldentry = NULL;
218 /* error reading file */
223 ldentry = str2entry(entry);
226 rdn = ldentry->e_name;
227 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
228 ch_free( rdn.bv_val );
229 rdn = ldentry->e_nname;
230 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
231 ch_free( rdn.bv_val );
245 static Entry * get_entry(Operation *op, struct berval *base_path) {
246 struct berval path, pdn, pndn;
249 dnParent(&op->o_req_dn, &pdn);
250 dnParent(&op->o_req_ndn, &pndn);
251 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
252 fd = open(path.bv_val, O_RDONLY);
253 /* error opening file (mebbe should log error) */
255 perror("failed to open file");
258 if(path.bv_val != NULL)
259 SLAP_FREE(path.bv_val);
260 return get_entry_for_fd(fd, &pdn, &pndn);
263 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
265 res->bv_len = name->bv_len + base->bv_len + 1;
266 res->bv_val = ch_malloc( res->bv_len + 1 );
267 strcpy(res->bv_val, base->bv_val);
268 ptr = res->bv_val + base->bv_len;
269 *ptr++ = LDAP_DIRSEP[0];
270 strcpy(ptr, name->bv_val);
273 typedef struct bvlist {
278 typedef struct enumCookie {
285 static void r_enum_tree(enumCookie *ck, struct berval *path,
286 struct berval *pdn, struct berval *pndn) {
290 if(ck->entries == NULL) {
291 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
292 ck->elen = ENTRY_BUFF_INCREMENT;
295 fd = open( path->bv_val, O_RDONLY );
297 Debug( LDAP_DEBUG_TRACE,
298 "=> ldif_enum_tree: failed to open %s\n",
299 path->bv_val, 0, 0 );
302 e = get_entry_for_fd(fd, pdn, pndn);
304 Debug( LDAP_DEBUG_ANY,
305 "=> ldif_enum_tree: failed to read entry for %s\n",
306 path->bv_val, 0, 0 );
310 if ( ck->scope == LDAP_SCOPE_BASE || ck->scope == LDAP_SCOPE_SUBTREE ) {
311 if(! (ck->eind < ck->elen)) { /* grow entries if necessary */
312 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
316 ck->entries[ck->eind] = e;
323 if ( ck->scope != LDAP_SCOPE_BASE ) {
325 bvlist *list = NULL, *ptr;
327 path->bv_len -= STRLENOF( LDIF );
328 path->bv_val[path->bv_len] = '\0';
330 dir_of_path = opendir(path->bv_val);
331 if(dir_of_path == NULL) {/* can't open directory */
332 Debug( LDAP_DEBUG_TRACE,
333 "=> ldif_enum_tree: failed to opendir %s\n",
334 path->bv_val, 0, 0 );
343 dir = readdir(dir_of_path);
344 if(dir == NULL) break; /* end of the directory */
345 fname.bv_len = strlen( dir->d_name );
346 if ( fname.bv_len <= STRLENOF( LDIF ))
348 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
350 fname.bv_val = dir->d_name;
352 bvl = ch_malloc( sizeof(bvlist) );
353 ber_dupbv( &bvl->bv, &fname );
355 for (ptr = list, prev = (bvlist *)&list; ptr;
356 prev = ptr, ptr = ptr->next) {
357 if ( strcmp( bvl->bv.bv_val, ptr->bv.bv_val ) < 0 )
364 closedir(dir_of_path);
366 if (ck->scope == LDAP_SCOPE_ONELEVEL)
367 ck->scope = LDAP_SCOPE_BASE;
368 else if ( ck->scope == LDAP_SCOPE_SUBORDINATE)
369 ck->scope = LDAP_SCOPE_SUBTREE;
371 for ( ptr = list; ptr; ptr=ptr->next ) {
373 fullpath( path, &ptr->bv, &fpath );
374 r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
379 if ( fd ) entry_free( e );
383 static Entry ** enum_tree(
390 struct ldif_info *ni = (struct ldif_info *) be->be_private;
394 struct berval pdn, pndn;
397 dnParent( dn, &pdn );
398 dnParent( ndn, &pndn );
399 dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
400 r_enum_tree(&ck, &path, &pdn, &pndn);
405 /* Get the parent path plus the LDIF suffix */
406 static void get_parent_path(struct berval * dnpath, struct berval *res) {
407 int dnpathlen = dnpath->bv_len;
410 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
411 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
414 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
415 strncpy(res->bv_val, dnpath->bv_val, i);
416 strcpy(res->bv_val+i, LDIF);
417 res->bv_val[i] = '\0';
420 static int apply_modify_to_entry(Entry * entry,
421 Modifications * modlist,
425 char textbuf[SLAP_TEXT_BUFLEN];
426 size_t textlen = sizeof textbuf;
429 Modification *mods = NULL;
430 Attribute *save_attrs;
432 if (!acl_check_modlist(op, entry, modlist)) {
433 return LDAP_INSUFFICIENT_ACCESS;
436 /* save_attrs = entry->e_attrs; Why?
437 entry->e_attrs = attrs_dup(entry->e_attrs); */
439 for (; modlist != NULL; modlist = modlist->sml_next) {
440 mods = &modlist->sml_mod;
442 switch (mods->sm_op) {
444 rc = modify_add_values(entry, mods,
445 get_permissiveModify(op),
446 &rs->sr_text, textbuf,
450 case LDAP_MOD_DELETE:
451 rc = modify_delete_values(entry, mods,
452 get_permissiveModify(op),
453 &rs->sr_text, textbuf,
458 case LDAP_MOD_REPLACE:
459 rc = modify_replace_values(entry, mods,
460 get_permissiveModify(op),
461 &rs->sr_text, textbuf,
465 case LDAP_MOD_INCREMENT:
467 case SLAP_MOD_SOFTADD:
468 mods->sm_op = LDAP_MOD_ADD;
469 rc = modify_add_values(entry, mods,
470 get_permissiveModify(op),
471 &rs->sr_text, textbuf,
473 mods->sm_op = SLAP_MOD_SOFTADD;
474 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
481 if(rc != LDAP_SUCCESS) break;
484 if(rc == LDAP_SUCCESS) {
485 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
486 entry->e_ocflags = 0;
488 /* check that the entry still obeys the schema */
489 rc = entry_schema_check(op->o_bd, entry,
490 save_attrs, &rs->sr_text,
497 ldif_back_bind( Operation *op, SlapReply *rs )
499 struct ldif_info *ni = NULL;
500 Attribute * a = NULL;
501 AttributeDescription *password = slap_schema.si_ad_userPassword;
503 Entry * entry = NULL;
505 ni = (struct ldif_info *) op->o_bd->be_private;
506 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
507 entry = (Entry *) get_entry(op, &ni->li_base_path);
509 /* no object is found for them */
511 if(be_isroot_pw(op)) {
512 return_val = LDAP_SUCCESS;
515 else if(be_root_dn(op->o_bd)) {
516 return_val = LDAP_INVALID_CREDENTIALS;
517 rs->sr_err = LDAP_INVALID_CREDENTIALS;
521 rs->sr_err = LDAP_NO_SUCH_OBJECT;
527 /* they don't have userpassword */
528 if((a = attr_find(entry->e_attrs, password)) == NULL) {
529 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
534 /* authentication actually failed */
535 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
536 &rs->sr_text) != 0) {
537 rs->sr_err = LDAP_INVALID_CREDENTIALS;
542 /* let the front-end send success */
547 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
549 send_ldap_result( op, rs );
555 static int ldif_back_search(Operation *op, SlapReply *rs)
557 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
560 Entry ** entries = NULL;
562 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
563 entries = (Entry **) enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, op->ors_scope);
565 if(entries != NULL) {
566 for(i=0;i<numentries;i++) {
567 if(test_filter(op, entries[i], op->ors_filter) == LDAP_COMPARE_TRUE) {
568 rs->sr_entry = entries[i];
569 rs->sr_attrs = op->ors_attrs;
570 rs->sr_flags = REP_ENTRY_MODIFIABLE;
571 send_search_entry(op, rs);
573 entry_free(entries[i]);
576 rs->sr_err = LDAP_SUCCESS;
577 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
578 send_ldap_result(op, rs);
581 rs->sr_err = LDAP_BUSY;
582 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
583 send_ldap_result(op, rs);
589 static int ldif_back_add(Operation *op, SlapReply *rs) {
590 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
591 Entry * e = op->ora_e;
592 struct berval dn = e->e_nname;
593 struct berval leaf_path = BER_BVNULL;
596 char textbuf[SLAP_TEXT_BUFLEN];
597 size_t textlen = sizeof textbuf;
599 rs->sr_err = entry_schema_check(op->o_bd, e,
600 NULL, &rs->sr_text, textbuf, textlen);
601 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
603 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
605 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
607 if(leaf_path.bv_val != NULL) {
608 struct berval base = BER_BVNULL;
609 /* build path to container and ldif of container */
610 get_parent_path(&leaf_path, &base);
612 statres = stat(base.bv_val, &stats); /* check if container exists */
613 if(statres == -1 && errno == ENOENT) { /* container missing */
614 base.bv_val[base.bv_len] = '.';
615 statres = stat(base.bv_val, &stats); /* check for leaf node */
616 base.bv_val[base.bv_len] = '\0';
617 if(statres == -1 && errno == ENOENT) {
618 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
620 else if(statres != -1) { /* create parent */
621 int mkdirres = mkdir(base.bv_val, 0750);
623 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
627 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
628 }/* container was possibly created, move on to add the entry */
629 if(rs->sr_err == LDAP_SUCCESS) {
630 statres = stat(leaf_path.bv_val, &stats);
631 if(statres == -1 && errno == ENOENT) {
632 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
633 rs->sr_err = (int) spew_entry(e, &leaf_path);
634 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
636 else /* it already exists */
637 rs->sr_err = LDAP_ALREADY_EXISTS;
639 SLAP_FREE(base.bv_val);
640 SLAP_FREE(leaf_path.bv_val);
643 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
646 send_ldap_result(op, rs);
650 static int ldif_back_modify(Operation *op, SlapReply *rs) {
651 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
652 Modifications * modlst = op->orm_modlist;
653 struct berval path = BER_BVNULL;
654 Entry * entry = NULL;
657 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
658 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
660 entry = (Entry *) get_entry(op, &ni->li_base_path);
663 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
664 if(rs->sr_err == LDAP_SUCCESS) {
665 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
666 spew_res = spew_entry(entry, &path);
667 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
669 perror("could not output entry");
670 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
675 rs->sr_err = LDAP_NO_SUCH_OBJECT;
680 if(path.bv_val != NULL)
681 SLAP_FREE(path.bv_val);
683 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
684 send_ldap_result(op, rs);
688 static int ldif_back_delete(Operation *op, SlapReply *rs) {
689 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
690 struct berval path = BER_BVNULL;
693 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
694 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
696 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
697 res = rmdir(path.bv_val);
698 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
699 if ( res && errno != ENOENT ) {
700 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
702 res = unlink(path.bv_val);
707 rs->sr_err = LDAP_NO_SUCH_OBJECT;
709 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
712 rs->sr_err = LDAP_SUCCESS;
714 SLAP_FREE(path.bv_val);
715 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
716 send_ldap_result(op, rs);
721 static int move_entry(Entry * entry, struct berval * ndn,
722 struct berval * newndn, struct berval * rootdn,
723 struct berval * base_path) {
727 struct berval newpath;
729 dn2path(ndn, rootdn, base_path, &path);
730 dn2path(newndn, rootdn, base_path, &newpath);
732 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
733 /* some object doesn't exist */
734 res = LDAP_NO_SUCH_OBJECT;
736 else { /* do the modrdn */
737 exists_res = open(newpath.bv_val, O_RDONLY);
738 if(exists_res == -1 && errno == ENOENT) {
739 res = spew_entry(entry, &newpath);
741 /* if this fails we should log something bad */
742 res = unlink(path.bv_val);
747 res = LDAP_NO_SUCH_OBJECT;
749 res = LDAP_UNWILLING_TO_PERFORM;
750 unlink(newpath.bv_val); /* in case file was created */
753 else if(exists_res) {
754 int close_res = close(exists_res);
755 res = LDAP_ALREADY_EXISTS;
756 if(close_res == -1) {
757 /* log heinous error */
761 res = LDAP_UNWILLING_TO_PERFORM;
765 if(newpath.bv_val != NULL)
766 SLAP_FREE(newpath.bv_val);
767 if(path.bv_val != NULL)
768 SLAP_FREE(path.bv_val);
772 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
773 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
774 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
775 struct berval * new_parent_dn = NULL;
776 struct berval p_dn, bv = {0, NULL};
777 Entry * entry = NULL;
778 LDAPRDN new_rdn = NULL;
779 LDAPRDN old_rdn = NULL;
780 Modifications * mods = NULL;
783 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
784 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
785 entry = (Entry *) get_entry(op, &ni->li_base_path);
787 /* build the mods to the entry */
789 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
790 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
791 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
793 else if(op->oq_modrdn.rs_deleteoldrdn &&
794 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
795 LDAP_DN_FORMAT_LDAP)) {
796 rs->sr_err = LDAP_OTHER;
798 else { /* got both rdns successfully, ready to build mods */
799 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
801 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
803 else { /* built mods successfully */
805 /* build new dn, and new ndn for the entry */
806 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
807 p_dn = *op->oq_modrdn.rs_newSup;
809 p_dn = slap_empty_bv;
810 dnParent(&entry->e_name, &p_dn);
811 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
812 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
813 ber_dupbv( &new_ndn, &bv );
814 entry->e_name = new_dn;
815 entry->e_nname = new_ndn;
817 /* perform the modifications */
818 res = apply_modify_to_entry(entry, mods, op, rs);
819 if(res == LDAP_SUCCESS) {
820 rs->sr_err = move_entry(entry, &op->o_req_ndn,
822 &op->o_bd->be_nsuffix[0],
830 else /* entry was null */
831 rs->sr_err = LDAP_NO_SUCH_OBJECT;
836 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
837 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
838 send_ldap_result(op, rs);
842 static int ldif_back_compare(Operation *op, SlapReply *rs) {
843 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
847 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
849 e = (Entry *) get_entry(op, &ni->li_base_path);
851 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
853 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
854 rs->sr_err = LDAP_COMPARE_FALSE;
856 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
857 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
858 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
859 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
860 op->o_tmpmemctx ) == 0) {
861 rs->sr_err = LDAP_COMPARE_TRUE;
867 rs->sr_err = LDAP_NO_SUCH_OBJECT;
872 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
873 send_ldap_result(op, rs);
877 static int ldif_tool_entry_open(BackendDB * be, int mode) {
878 struct ldif_info *ni = (struct ldif_info *) be->be_private;
879 ni->tool_entries = NULL;
880 ni->tool_numentries = 0;
881 ni->tool_current = 0;
882 ni->tool_put_entry_flag = 0;
886 static int ldif_tool_entry_close(BackendDB * be) {
887 struct ldif_info *ni = (struct ldif_info *) be->be_private;
889 SLAP_FREE(ni->tool_entries);
893 static ID ldif_tool_entry_first(BackendDB *be) {
894 struct ldif_info *ni = (struct ldif_info *) be->be_private;
895 ID id = 1; /* first entry in the array of entries shifted by one */
897 ni->tool_current = 1;
898 if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
899 ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
900 be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
901 ni->tool_put_entry_flag = 0;
906 static ID ldif_tool_entry_next(BackendDB *be) {
907 struct ldif_info *ni = (struct ldif_info *) be->be_private;
908 ni->tool_current += 1;
909 if(ni->tool_put_entry_flag) {
910 ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
911 be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
912 ni->tool_put_entry_flag = 0;
914 if(ni->tool_current > ni->tool_numentries)
917 return ni->tool_current;
920 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
921 struct ldif_info *ni = (struct ldif_info *) be->be_private;
924 if(id > ni->tool_numentries || id < 1)
927 e = ni->tool_entries[id - 1];
928 ni->tool_entries[id - 1] = NULL;
933 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
934 struct ldif_info *ni = (struct ldif_info *) be->be_private;
935 Attribute *save_attrs;
936 struct berval dn = e->e_nname;
937 struct berval leaf_path = BER_BVNULL;
940 char textbuf[SLAP_TEXT_BUFLEN];
941 size_t textlen = sizeof textbuf;
942 int res = LDAP_SUCCESS;
944 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
946 if(leaf_path.bv_val != NULL) {
947 struct berval base = BER_BVNULL;
948 /* build path to container, and path to ldif of container */
949 get_parent_path(&leaf_path, &base);
951 statres = stat(base.bv_val, &stats); /* check if container exists */
952 if(statres == -1 && errno == ENOENT) { /* container missing */
953 base.bv_val[base.bv_len] = '.';
954 statres = stat(base.bv_val, &stats); /* check for leaf node */
955 base.bv_val[base.bv_len] = '\0';
956 if(statres == -1 && errno == ENOENT) {
957 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
959 else if(statres != -1) { /* create parent */
960 int mkdirres = mkdir(base.bv_val, 0750);
962 res = LDAP_UNWILLING_TO_PERFORM;
966 res = LDAP_UNWILLING_TO_PERFORM;
967 }/* container was possibly created, move on to add the entry */
968 if(res == LDAP_SUCCESS) {
969 statres = stat(leaf_path.bv_val, &stats);
970 if(statres == -1 && errno == ENOENT) {
971 res = (int) spew_entry(e, &leaf_path);
973 else /* it already exists */
974 res = LDAP_ALREADY_EXISTS;
976 SLAP_FREE(base.bv_val);
977 SLAP_FREE(leaf_path.bv_val);
980 if(res == LDAP_SUCCESS) {
981 ni->tool_put_entry_flag = 1;
989 ldif_back_db_init( BackendDB *be )
991 struct ldif_info *ni;
993 ni = ch_calloc( 1, sizeof(struct ldif_info) );
995 be->be_cf_table = be->bd_info->bi_cf_table;
996 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1001 ldif_back_db_destroy(
1005 struct ldif_info *ni = be->be_private;
1006 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1007 free( be->be_private );
1016 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1017 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1018 fprintf(stderr, "missing base path for back-ldif\n");
1025 ldif_back_initialize(
1031 bi->bi_cf_table = ldifcfg;
1038 bi->bi_db_init = ldif_back_db_init;
1039 bi->bi_db_config = config_generic_wrapper;
1040 bi->bi_db_open = ldif_back_db_open;
1041 bi->bi_db_close = 0;
1042 bi->bi_db_destroy = ldif_back_db_destroy;
1044 bi->bi_op_bind = ldif_back_bind;
1045 bi->bi_op_unbind = 0;
1046 bi->bi_op_search = ldif_back_search;
1047 bi->bi_op_compare = ldif_back_compare;
1048 bi->bi_op_modify = ldif_back_modify;
1049 bi->bi_op_modrdn = ldif_back_modrdn;
1050 bi->bi_op_add = ldif_back_add;
1051 bi->bi_op_delete = ldif_back_delete;
1052 bi->bi_op_abandon = 0;
1054 bi->bi_extended = 0;
1056 bi->bi_chk_referrals = 0;
1058 bi->bi_connection_init = 0;
1059 bi->bi_connection_destroy = 0;
1061 bi->bi_tool_entry_open = ldif_tool_entry_open;
1062 bi->bi_tool_entry_close = ldif_tool_entry_close;
1063 bi->bi_tool_entry_first = ldif_tool_entry_first;
1064 bi->bi_tool_entry_next = ldif_tool_entry_next;
1065 bi->bi_tool_entry_get = ldif_tool_entry_get;
1066 bi->bi_tool_entry_put = ldif_tool_entry_put;
1067 bi->bi_tool_entry_reindex = 0;
1068 bi->bi_tool_sync = 0;
1070 bi->bi_tool_dn2id_get = 0;
1071 bi->bi_tool_id2entry_get = 0;
1072 bi->bi_tool_entry_modify = 0;
1074 rc = config_register_schema( ldifcfg, ldifocs );
1075 if ( rc ) return rc;
1076 ldifcfg[0].ad = slap_schema.si_ad_objectClass;