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;
52 #define ENTRY_BUFF_INCREMENT 500
54 static ObjectClass *ldif_oc;
56 static ConfigDriver ldif_cf;
58 static ConfigTable ldifcfg[] = {
59 { "", "", 0, 0, 0, ARG_MAGIC,
60 ldif_cf, NULL, NULL, NULL },
61 { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
62 (void *)offsetof(struct ldif_info, li_base_path),
63 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
64 "DESC 'Directory for database content' "
65 "EQUALITY caseIgnoreMatch "
66 "SYNTAX OMsDirectoryString )", NULL, NULL },
67 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
68 NULL, NULL, NULL, NULL }
71 static ConfigOCs ldifocs[] = {
73 "NAME 'olcLdifConfig' "
74 "DESC 'LDIF backend configuration' "
75 "SUP olcDatabaseConfig "
76 "MUST ( olcDbDirectory ) )", Cft_Database,
82 ldif_cf( ConfigArgs *c )
84 if ( c->op == SLAP_CONFIG_EMIT ) {
85 value_add_one( &c->rvalue_vals, &ldif_oc->soc_cname );
92 dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
95 char *ptr, *sep, *end;
97 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
98 res->bv_val = ch_malloc( res->bv_len + 1 );
99 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
100 *ptr++ = LDAP_DIRSEP[0];
101 ptr = lutil_strcopy( ptr, rootdn->bv_val );
102 end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
103 while ( end > dn->bv_val ) {
104 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
105 *ptr++ = LDAP_DIRSEP[0];
106 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
112 while( ptr=strchr(ptr, IX_DNL) ) {
114 ptr = strchr(ptr, IX_DNR);
123 static char * slurp_file(int fd) {
124 int read_chars_total = 0;
132 entry_size = st.st_size;
133 entry = ch_malloc( entry_size+1 );
137 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
138 if(read_chars == -1) {
142 if(read_chars == 0) {
143 entry[read_chars_total] = '\0';
147 read_chars_total += read_chars;
148 entry_pos += read_chars;
154 static int spew_file(int fd, char * spew) {
157 int len = strlen(spew);
158 char * spewptr = spew;
160 while(written < len) {
161 writeres = write(fd, spewptr, len - written);
163 perror("could not spew write");
174 static int spew_entry(Entry * e, struct berval * path) {
179 char * entry_as_string;
181 openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
184 rs = LDAP_NO_SUCH_OBJECT;
186 rs = LDAP_UNWILLING_TO_PERFORM;
192 /* Only save the RDN onto disk */
193 dnRdn( &e->e_name, &rdn );
194 if ( rdn.bv_len != e->e_name.bv_len ) {
195 e->e_name.bv_val[rdn.bv_len] = '\0';
196 tmp = e->e_name.bv_len;
197 e->e_name.bv_len = rdn.bv_len;
201 entry_as_string = entry2str(e, &entry_length);
203 /* Restore full DN */
204 if ( rdn.bv_len != e->e_name.bv_len ) {
205 e->e_name.bv_val[e->e_name.bv_len] = ',';
206 e->e_name.bv_len = rdn.bv_len;
209 if(entry_as_string == NULL) {
210 rs = LDAP_UNWILLING_TO_PERFORM;
214 spew_res = spew_file(openres, entry_as_string);
217 rs = LDAP_UNWILLING_TO_PERFORM;
225 static Entry * get_entry_for_fd(int fd,
229 char * entry = (char *) slurp_file(fd);
230 Entry * ldentry = NULL;
232 /* error reading file */
237 ldentry = str2entry(entry);
240 rdn = ldentry->e_name;
241 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
242 ch_free( rdn.bv_val );
243 rdn = ldentry->e_nname;
244 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
245 ch_free( rdn.bv_val );
259 static Entry * get_entry(Operation *op, struct berval *base_path) {
260 struct berval path, pdn, pndn;
263 dnParent(&op->o_req_dn, &pdn);
264 dnParent(&op->o_req_ndn, &pndn);
265 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
266 fd = open(path.bv_val, O_RDONLY);
267 /* error opening file (mebbe should log error) */
269 perror("failed to open file");
272 if(path.bv_val != NULL)
273 SLAP_FREE(path.bv_val);
276 return get_entry_for_fd(fd, &pdn, &pndn);
282 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
284 res->bv_len = name->bv_len + base->bv_len + 1;
285 res->bv_val = ch_malloc( res->bv_len + 1 );
286 strcpy(res->bv_val, base->bv_val);
287 ptr = res->bv_val + base->bv_len;
288 *ptr++ = LDAP_DIRSEP[0];
289 strcpy(ptr, name->bv_val);
292 typedef struct bvlist {
300 typedef struct enumCookie {
307 static int r_enum_tree(enumCookie *ck, struct berval *path,
308 struct berval *pdn, struct berval *pndn)
311 int fd, rc = LDAP_SUCCESS;
313 fd = open( path->bv_val, O_RDONLY );
315 Debug( LDAP_DEBUG_TRACE,
316 "=> ldif_enum_tree: failed to open %s\n",
317 path->bv_val, 0, 0 );
318 return LDAP_NO_SUCH_OBJECT;
321 if(ck->entries == NULL) {
322 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
323 ck->elen = ENTRY_BUFF_INCREMENT;
326 e = get_entry_for_fd(fd, pdn, pndn);
328 Debug( LDAP_DEBUG_ANY,
329 "=> ldif_enum_tree: failed to read entry for %s\n",
330 path->bv_val, 0, 0 );
334 if ( ck->scope == LDAP_SCOPE_BASE || ck->scope == LDAP_SCOPE_SUBTREE ) {
335 if(! (ck->eind < ck->elen)) { /* grow entries if necessary */
336 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
340 ck->entries[ck->eind] = e;
347 if ( ck->scope != LDAP_SCOPE_BASE ) {
349 bvlist *list = NULL, *ptr;
351 path->bv_len -= STRLENOF( LDIF );
352 path->bv_val[path->bv_len] = '\0';
354 dir_of_path = opendir(path->bv_val);
355 if(dir_of_path == NULL) { /* can't open directory */
356 if ( errno != ENOENT ) {
357 /* it shouldn't be treated as an error
358 * only if the directory doesn't exist */
360 Debug( LDAP_DEBUG_TRACE,
361 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
362 path->bv_val, errno, 0 );
368 struct berval fname, itmp;
372 dir = readdir(dir_of_path);
373 if(dir == NULL) break; /* end of the directory */
374 fname.bv_len = strlen( dir->d_name );
375 if ( fname.bv_len <= STRLENOF( LDIF ))
377 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
379 fname.bv_val = dir->d_name;
381 bvl = ch_malloc( sizeof(bvlist) );
382 ber_dupbv( &bvl->bv, &fname );
383 BER_BVZERO( &bvl->num );
384 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
388 ptr = strchr( itmp.bv_val, IX_FSR );
390 itmp.bv_len = ptr - itmp.bv_val;
391 ber_dupbv( &bvl->num, &itmp );
392 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
393 itmp.bv_val[0] = '\0';
394 bvl->off = itmp.bv_val - bvl->bv.bv_val;
398 for (ptr = list, prev = (bvlist *)&list; ptr;
399 prev = ptr, ptr = ptr->next) {
400 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
401 if ( !cmp && bvl->num.bv_val )
402 cmp = bvl->inum - ptr->inum;
410 closedir(dir_of_path);
412 if (ck->scope == LDAP_SCOPE_ONELEVEL)
413 ck->scope = LDAP_SCOPE_BASE;
414 else if ( ck->scope == LDAP_SCOPE_SUBORDINATE)
415 ck->scope = LDAP_SCOPE_SUBTREE;
417 while ( ( ptr = list ) ) {
422 if ( ptr->num.bv_val )
423 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
425 fullpath( path, &ptr->bv, &fpath );
426 r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
428 if ( ptr->num.bv_val )
429 free( ptr->num.bv_val );
430 free(ptr->bv.bv_val);
435 if ( fd ) entry_free( e );
448 struct ldif_info *ni = (struct ldif_info *) be->be_private;
452 struct berval pdn, pndn;
458 dnParent( dn, &pdn );
459 dnParent( ndn, &pndn );
460 dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
461 rc = r_enum_tree(&ck, &path, &pdn, &pndn);
467 /* Get the parent path plus the LDIF suffix */
468 static void get_parent_path(struct berval * dnpath, struct berval *res) {
469 int dnpathlen = dnpath->bv_len;
472 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
473 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
476 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
477 strncpy(res->bv_val, dnpath->bv_val, i);
478 strcpy(res->bv_val+i, LDIF);
479 res->bv_val[i] = '\0';
482 static int apply_modify_to_entry(Entry * entry,
483 Modifications * modlist,
487 char textbuf[SLAP_TEXT_BUFLEN];
488 int rc = LDAP_UNWILLING_TO_PERFORM;
489 Modification *mods = NULL;
491 if (!acl_check_modlist(op, entry, modlist)) {
492 return LDAP_INSUFFICIENT_ACCESS;
495 for (; modlist != NULL; modlist = modlist->sml_next) {
496 mods = &modlist->sml_mod;
498 switch (mods->sm_op) {
500 rc = modify_add_values(entry, mods,
501 get_permissiveModify(op),
502 &rs->sr_text, textbuf,
506 case LDAP_MOD_DELETE:
507 rc = modify_delete_values(entry, mods,
508 get_permissiveModify(op),
509 &rs->sr_text, textbuf,
514 case LDAP_MOD_REPLACE:
515 rc = modify_replace_values(entry, mods,
516 get_permissiveModify(op),
517 &rs->sr_text, textbuf,
521 case LDAP_MOD_INCREMENT:
523 case SLAP_MOD_SOFTADD:
524 mods->sm_op = LDAP_MOD_ADD;
525 rc = modify_add_values(entry, mods,
526 get_permissiveModify(op),
527 &rs->sr_text, textbuf,
529 mods->sm_op = SLAP_MOD_SOFTADD;
530 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
537 if(rc != LDAP_SUCCESS) break;
540 if(rc == LDAP_SUCCESS) {
541 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
542 entry->e_ocflags = 0;
544 /* check that the entry still obeys the schema */
545 rc = entry_schema_check(op->o_bd, entry, NULL,
546 &rs->sr_text, textbuf, sizeof( textbuf ) );
552 ldif_back_referrals( Operation *op, SlapReply *rs )
554 struct ldif_info *ni = NULL;
556 int rc = LDAP_SUCCESS;
559 if ( op->o_tag == LDAP_REQ_SEARCH ) {
560 /* let search take care of itself */
565 if ( get_manageDSAit( op ) ) {
566 /* let op take care of DSA management */
570 ni = (struct ldif_info *)op->o_bd->be_private;
571 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
572 entry = (Entry *)get_entry( op, &ni->li_base_path );
574 /* no object is found for them */
575 if ( entry == NULL ) {
576 struct berval odn = op->o_req_dn;
577 struct berval ondn = op->o_req_ndn;
579 struct berval pndn = op->o_req_ndn;
581 for ( ; entry == NULL; ) {
582 dnParent( &pndn, &pndn );
584 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
589 op->o_req_ndn = pndn;
591 entry = (Entry *)get_entry( op, &ni->li_base_path );
594 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
597 op->o_req_ndn = ondn;
600 rs->sr_matched = NULL;
601 if ( entry != NULL ) {
602 Debug( LDAP_DEBUG_TRACE,
603 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
604 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
606 if ( is_entry_referral( entry ) ) {
608 rs->sr_ref = get_entry_referrals( op, entry );
610 rs->sr_matched = ber_strdup_x(
611 entry->e_name.bv_val, op->o_tmpmemctx );
617 } else if ( default_referral != NULL ) {
619 rs->sr_ref = referral_rewrite( default_referral,
620 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
623 if ( rs->sr_ref != NULL ) {
625 rc = rs->sr_err = LDAP_REFERRAL;
626 send_ldap_result( op, rs );
627 ber_bvarray_free( rs->sr_ref );
630 } else if ( rc != LDAP_SUCCESS ) {
632 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
633 send_ldap_result( op, rs );
636 if ( rs->sr_matched ) {
637 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
638 rs->sr_matched = NULL;
644 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
646 if ( is_entry_referral( entry ) ) {
647 /* entry is a referral */
648 BerVarray refs = get_entry_referrals( op, entry );
649 rs->sr_ref = referral_rewrite(
650 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
652 Debug( LDAP_DEBUG_TRACE,
653 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
654 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
656 rs->sr_matched = entry->e_name.bv_val;
657 if ( rs->sr_ref != NULL ) {
658 rc = rs->sr_err = LDAP_REFERRAL;
659 send_ldap_result( op, rs );
660 ber_bvarray_free( rs->sr_ref );
664 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
668 rs->sr_matched = NULL;
669 ber_bvarray_free( refs );
678 ldif_back_bind( Operation *op, SlapReply *rs )
680 struct ldif_info *ni = NULL;
681 Attribute * a = NULL;
682 AttributeDescription *password = slap_schema.si_ad_userPassword;
684 Entry * entry = NULL;
686 ni = (struct ldif_info *) op->o_bd->be_private;
687 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
688 entry = (Entry *) get_entry(op, &ni->li_base_path);
690 /* no object is found for them */
692 if(be_isroot_pw(op)) {
693 rs->sr_err = return_val = LDAP_SUCCESS;
695 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
700 /* they don't have userpassword */
701 if((a = attr_find(entry->e_attrs, password)) == NULL) {
702 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
707 /* authentication actually failed */
708 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
709 &rs->sr_text) != 0) {
710 rs->sr_err = LDAP_INVALID_CREDENTIALS;
715 /* let the front-end send success */
720 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
722 send_ldap_result( op, rs );
728 static int ldif_back_search(Operation *op, SlapReply *rs)
730 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
733 Entry ** entries = NULL;
735 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
736 rs->sr_err = enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, &entries, op->ors_scope);
737 if ( rs->sr_err == LDAP_SUCCESS ) {
738 for ( i = 0; i < numentries; i++ ) {
742 * if it's a referral, add it to the list of referrals. only do
743 * this for non-base searches, and don't check the filter
744 * explicitly here since it's only a candidate anyway.
746 if ( !get_manageDSAit( op )
747 && op->oq_search.rs_scope != LDAP_SCOPE_BASE
748 && is_entry_referral( entries[i] ) )
750 BerVarray erefs = get_entry_referrals( op, entries[i] );
751 rs->sr_ref = referral_rewrite( erefs,
752 &entries[i]->e_name, NULL,
753 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
754 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
756 send_search_reference( op, rs );
758 ber_bvarray_free( rs->sr_ref );
759 ber_bvarray_free( erefs );
762 } else if ( test_filter( op, entries[i], op->ors_filter ) == LDAP_COMPARE_TRUE )
764 rs->sr_entry = entries[i];
765 rs->sr_attrs = op->ors_attrs;
766 rs->sr_flags = REP_ENTRY_MODIFIABLE;
767 send_search_entry(op, rs);
769 entry_free(entries[i]);
773 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
774 send_ldap_result(op, rs);
779 static int ldif_back_add(Operation *op, SlapReply *rs) {
780 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
781 Entry * e = op->ora_e;
782 struct berval dn = e->e_nname;
783 struct berval leaf_path = BER_BVNULL;
786 char textbuf[SLAP_TEXT_BUFLEN];
788 rs->sr_err = entry_schema_check(op->o_bd, e,
789 NULL, &rs->sr_text, textbuf, sizeof( textbuf ) );
790 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
792 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
794 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
796 if(leaf_path.bv_val != NULL) {
797 struct berval base = BER_BVNULL;
798 /* build path to container and ldif of container */
799 get_parent_path(&leaf_path, &base);
801 statres = stat(base.bv_val, &stats); /* check if container exists */
802 if(statres == -1 && errno == ENOENT) { /* container missing */
803 base.bv_val[base.bv_len] = '.';
804 statres = stat(base.bv_val, &stats); /* check for leaf node */
805 base.bv_val[base.bv_len] = '\0';
806 if(statres == -1 && errno == ENOENT) {
807 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
809 else if(statres != -1) { /* create parent */
810 int mkdirres = mkdir(base.bv_val, 0750);
812 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
816 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
817 }/* container was possibly created, move on to add the entry */
818 if(rs->sr_err == LDAP_SUCCESS) {
819 statres = stat(leaf_path.bv_val, &stats);
820 if(statres == -1 && errno == ENOENT) {
821 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
822 rs->sr_err = (int) spew_entry(e, &leaf_path);
823 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
825 else /* it already exists */
826 rs->sr_err = LDAP_ALREADY_EXISTS;
828 SLAP_FREE(base.bv_val);
829 SLAP_FREE(leaf_path.bv_val);
832 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
835 send_ldap_result(op, rs);
839 static int ldif_back_modify(Operation *op, SlapReply *rs) {
840 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
841 Modifications * modlst = op->orm_modlist;
842 struct berval path = BER_BVNULL;
843 Entry * entry = NULL;
846 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
847 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
849 entry = (Entry *) get_entry(op, &ni->li_base_path);
852 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
853 if(rs->sr_err == LDAP_SUCCESS) {
854 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
855 spew_res = spew_entry(entry, &path);
856 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
858 perror("could not output entry");
859 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
864 rs->sr_err = LDAP_NO_SUCH_OBJECT;
869 if(path.bv_val != NULL)
870 SLAP_FREE(path.bv_val);
872 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
873 send_ldap_result(op, rs);
877 static int ldif_back_delete(Operation *op, SlapReply *rs) {
878 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
879 struct berval path = BER_BVNULL;
882 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
883 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
885 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
886 res = rmdir(path.bv_val);
887 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
888 if ( res && errno != ENOENT ) {
889 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
891 res = unlink(path.bv_val);
896 rs->sr_err = LDAP_NO_SUCH_OBJECT;
898 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
901 rs->sr_err = LDAP_SUCCESS;
903 SLAP_FREE(path.bv_val);
904 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
905 send_ldap_result(op, rs);
910 static int move_entry(Entry * entry, struct berval * ndn,
911 struct berval * newndn, struct berval * rootdn,
912 struct berval * base_path) {
916 struct berval newpath;
918 dn2path(ndn, rootdn, base_path, &path);
919 dn2path(newndn, rootdn, base_path, &newpath);
921 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
922 /* some object doesn't exist */
923 res = LDAP_NO_SUCH_OBJECT;
925 else { /* do the modrdn */
926 exists_res = open(newpath.bv_val, O_RDONLY);
927 if(exists_res == -1 && errno == ENOENT) {
928 res = spew_entry(entry, &newpath);
930 /* if this fails we should log something bad */
931 res = unlink(path.bv_val);
936 res = LDAP_NO_SUCH_OBJECT;
938 res = LDAP_UNWILLING_TO_PERFORM;
939 unlink(newpath.bv_val); /* in case file was created */
942 else if(exists_res) {
943 int close_res = close(exists_res);
944 res = LDAP_ALREADY_EXISTS;
945 if(close_res == -1) {
946 /* log heinous error */
950 res = LDAP_UNWILLING_TO_PERFORM;
954 if(newpath.bv_val != NULL)
955 SLAP_FREE(newpath.bv_val);
956 if(path.bv_val != NULL)
957 SLAP_FREE(path.bv_val);
961 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
962 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
963 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
964 struct berval p_dn, bv = BER_BVNULL;
965 Entry * entry = NULL;
966 LDAPRDN new_rdn = NULL;
967 LDAPRDN old_rdn = NULL;
968 Modifications * mods = NULL;
971 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
972 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
973 entry = (Entry *) get_entry(op, &ni->li_base_path);
975 /* build the mods to the entry */
977 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
978 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
979 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
981 else if(op->oq_modrdn.rs_deleteoldrdn &&
982 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
983 LDAP_DN_FORMAT_LDAP)) {
984 rs->sr_err = LDAP_OTHER;
986 else { /* got both rdns successfully, ready to build mods */
987 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
989 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
991 else { /* built mods successfully */
993 /* build new dn, and new ndn for the entry */
994 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
995 p_dn = *op->oq_modrdn.rs_newSup;
997 p_dn = slap_empty_bv;
998 dnParent(&entry->e_name, &p_dn);
999 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
1000 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
1001 ber_dupbv( &new_ndn, &bv );
1002 entry->e_name = new_dn;
1003 entry->e_nname = new_ndn;
1005 /* perform the modifications */
1006 res = apply_modify_to_entry(entry, mods, op, rs);
1007 if(res == LDAP_SUCCESS) {
1008 rs->sr_err = move_entry(entry, &op->o_req_ndn,
1010 &op->o_bd->be_nsuffix[0],
1018 else /* entry was null */
1019 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1024 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1025 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1026 send_ldap_result(op, rs);
1030 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1031 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1035 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1037 e = (Entry *) get_entry(op, &ni->li_base_path);
1039 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1041 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1042 rs->sr_err = LDAP_COMPARE_FALSE;
1044 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1045 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1046 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1047 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1048 op->o_tmpmemctx ) == 0) {
1049 rs->sr_err = LDAP_COMPARE_TRUE;
1055 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1060 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1061 send_ldap_result(op, rs);
1065 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1066 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1067 ni->tool_entries = NULL;
1068 ni->tool_numentries = 0;
1069 ni->tool_current = 0;
1070 ni->tool_put_entry_flag = 0;
1074 static int ldif_tool_entry_close(BackendDB * be) {
1075 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1077 SLAP_FREE(ni->tool_entries);
1082 ldif_tool_entry_first(BackendDB *be)
1084 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1085 ID id = 1; /* first entry in the array of entries shifted by one */
1087 ni->tool_current = 1;
1088 if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
1089 (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
1090 &ni->tool_numentries, &ni->tool_entries,
1091 LDAP_SCOPE_SUBTREE);
1092 ni->tool_put_entry_flag = 0;
1097 static ID ldif_tool_entry_next(BackendDB *be)
1099 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1100 ni->tool_current += 1;
1101 if(ni->tool_put_entry_flag) {
1102 (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
1103 &ni->tool_numentries, &ni->tool_entries,
1104 LDAP_SCOPE_SUBTREE);
1105 ni->tool_put_entry_flag = 0;
1107 if(ni->tool_current > ni->tool_numentries)
1110 return ni->tool_current;
1113 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1114 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1117 if(id > ni->tool_numentries || id < 1)
1120 e = ni->tool_entries[id - 1];
1121 ni->tool_entries[id - 1] = NULL;
1126 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1127 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1128 struct berval dn = e->e_nname;
1129 struct berval leaf_path = BER_BVNULL;
1132 int res = LDAP_SUCCESS;
1134 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1136 if(leaf_path.bv_val != NULL) {
1137 struct berval base = BER_BVNULL;
1138 /* build path to container, and path to ldif of container */
1139 get_parent_path(&leaf_path, &base);
1141 statres = stat(base.bv_val, &stats); /* check if container exists */
1142 if(statres == -1 && errno == ENOENT) { /* container missing */
1143 base.bv_val[base.bv_len] = '.';
1144 statres = stat(base.bv_val, &stats); /* check for leaf node */
1145 base.bv_val[base.bv_len] = '\0';
1146 if(statres == -1 && errno == ENOENT) {
1147 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1149 else if(statres != -1) { /* create parent */
1150 int mkdirres = mkdir(base.bv_val, 0750);
1151 if(mkdirres == -1) {
1152 res = LDAP_UNWILLING_TO_PERFORM;
1156 res = LDAP_UNWILLING_TO_PERFORM;
1157 }/* container was possibly created, move on to add the entry */
1158 if(res == LDAP_SUCCESS) {
1159 statres = stat(leaf_path.bv_val, &stats);
1160 if(statres == -1 && errno == ENOENT) {
1161 res = (int) spew_entry(e, &leaf_path);
1163 else /* it already exists */
1164 res = LDAP_ALREADY_EXISTS;
1166 SLAP_FREE(base.bv_val);
1167 SLAP_FREE(leaf_path.bv_val);
1170 if(res == LDAP_SUCCESS) {
1171 ni->tool_put_entry_flag = 1;
1179 ldif_back_db_init( BackendDB *be )
1181 struct ldif_info *ni;
1183 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1184 be->be_private = ni;
1185 be->be_cf_table = be->bd_info->bi_cf_table;
1186 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1191 ldif_back_db_destroy(
1195 struct ldif_info *ni = be->be_private;
1196 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1197 free( be->be_private );
1206 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1207 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1208 fprintf(stderr, "missing base path for back-ldif\n");
1215 ldif_back_initialize(
1219 static char *controls[] = {
1220 LDAP_CONTROL_MANAGEDSAIT,
1226 SLAP_BFLAG_REFERRALS;
1228 bi->bi_controls = controls;
1230 bi->bi_cf_table = ldifcfg;
1237 bi->bi_db_init = ldif_back_db_init;
1238 bi->bi_db_config = config_generic_wrapper;
1239 bi->bi_db_open = ldif_back_db_open;
1240 bi->bi_db_close = 0;
1241 bi->bi_db_destroy = ldif_back_db_destroy;
1243 bi->bi_op_bind = ldif_back_bind;
1244 bi->bi_op_unbind = 0;
1245 bi->bi_op_search = ldif_back_search;
1246 bi->bi_op_compare = ldif_back_compare;
1247 bi->bi_op_modify = ldif_back_modify;
1248 bi->bi_op_modrdn = ldif_back_modrdn;
1249 bi->bi_op_add = ldif_back_add;
1250 bi->bi_op_delete = ldif_back_delete;
1251 bi->bi_op_abandon = 0;
1253 bi->bi_extended = 0;
1255 bi->bi_chk_referrals = ldif_back_referrals;
1257 bi->bi_connection_init = 0;
1258 bi->bi_connection_destroy = 0;
1260 bi->bi_tool_entry_open = ldif_tool_entry_open;
1261 bi->bi_tool_entry_close = ldif_tool_entry_close;
1262 bi->bi_tool_entry_first = ldif_tool_entry_first;
1263 bi->bi_tool_entry_next = ldif_tool_entry_next;
1264 bi->bi_tool_entry_get = ldif_tool_entry_get;
1265 bi->bi_tool_entry_put = ldif_tool_entry_put;
1266 bi->bi_tool_entry_reindex = 0;
1267 bi->bi_tool_sync = 0;
1269 bi->bi_tool_dn2id_get = 0;
1270 bi->bi_tool_id2entry_get = 0;
1271 bi->bi_tool_entry_modify = 0;
1273 rc = config_register_schema( ldifcfg, ldifocs );
1274 if ( rc ) return rc;
1275 ldifcfg[0].ad = slap_schema.si_ad_objectClass;