1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2006 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>
34 typedef struct enumCookie {
43 struct berval li_base_path;
44 enumCookie li_tool_cookie;
46 ldap_pvt_thread_mutex_t li_mutex;
50 #define mkdir(a,b) mkdir(a)
62 #define ENTRY_BUFF_INCREMENT 500
64 static ConfigTable ldifcfg[] = {
65 { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
66 (void *)offsetof(struct ldif_info, li_base_path),
67 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
68 "DESC 'Directory for database content' "
69 "EQUALITY caseIgnoreMatch "
70 "SYNTAX OMsDirectoryString )", NULL, NULL },
71 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
72 NULL, NULL, NULL, NULL }
75 static ConfigOCs ldifocs[] = {
77 "NAME 'olcLdifConfig' "
78 "DESC 'LDIF backend configuration' "
79 "SUP olcDatabaseConfig "
80 "MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
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 while( ptr=strchr(ptr, IX_DNL) ) {
107 ptr = strchr(ptr, IX_DNR);
116 static char * slurp_file(int fd) {
117 int read_chars_total = 0;
125 entry_size = st.st_size;
126 entry = ch_malloc( entry_size+1 );
130 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
131 if(read_chars == -1) {
135 if(read_chars == 0) {
136 entry[read_chars_total] = '\0';
140 read_chars_total += read_chars;
141 entry_pos += read_chars;
147 static int spew_file(int fd, char * spew, int len) {
151 writeres = write(fd, spew, len);
153 Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
154 strerror( errno ), 0, 0 );
165 static int spew_entry(Entry * e, struct berval * path) {
170 char * entry_as_string;
172 openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
175 rs = LDAP_NO_SUCH_OBJECT;
177 rs = LDAP_UNWILLING_TO_PERFORM;
178 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
179 path->bv_val, strerror( errno ), 0 );
185 /* Only save the RDN onto disk */
186 dnRdn( &e->e_name, &rdn );
187 if ( rdn.bv_len != e->e_name.bv_len ) {
188 e->e_name.bv_val[rdn.bv_len] = '\0';
189 tmp = e->e_name.bv_len;
190 e->e_name.bv_len = rdn.bv_len;
194 entry_as_string = entry2str(e, &entry_length);
196 /* Restore full DN */
197 if ( rdn.bv_len != e->e_name.bv_len ) {
198 e->e_name.bv_val[e->e_name.bv_len] = ',';
199 e->e_name.bv_len = rdn.bv_len;
202 if(entry_as_string == NULL) {
203 rs = LDAP_UNWILLING_TO_PERFORM;
207 spew_res = spew_file(openres, entry_as_string, entry_length);
210 rs = LDAP_UNWILLING_TO_PERFORM;
218 static Entry * get_entry_for_fd(int fd,
222 char * entry = (char *) slurp_file(fd);
223 Entry * ldentry = NULL;
225 /* error reading file */
230 ldentry = str2entry(entry);
233 rdn = ldentry->e_name;
234 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
235 ch_free( rdn.bv_val );
236 rdn = ldentry->e_nname;
237 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
238 ch_free( rdn.bv_val );
252 static Entry * get_entry(Operation *op, struct berval *base_path) {
253 struct berval path, pdn, pndn;
256 dnParent(&op->o_req_dn, &pdn);
257 dnParent(&op->o_req_ndn, &pndn);
258 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
259 fd = open(path.bv_val, O_RDONLY);
260 /* error opening file (mebbe should log error) */
262 Debug(LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
263 path.bv_val, strerror(errno), 0 );
266 if(path.bv_val != NULL)
267 SLAP_FREE(path.bv_val);
270 return get_entry_for_fd(fd, &pdn, &pndn);
276 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
278 res->bv_len = name->bv_len + base->bv_len + 1;
279 res->bv_val = ch_malloc( res->bv_len + 1 );
280 strcpy(res->bv_val, base->bv_val);
281 ptr = res->bv_val + base->bv_len;
282 *ptr++ = LDAP_DIRSEP[0];
283 strcpy(ptr, name->bv_val);
286 typedef struct bvlist {
295 static int r_enum_tree(enumCookie *ck, struct berval *path,
296 struct berval *pdn, struct berval *pndn)
299 int fd, rc = LDAP_SUCCESS;
301 fd = open( path->bv_val, O_RDONLY );
303 Debug( LDAP_DEBUG_ANY,
304 "=> ldif_enum_tree: failed to open %s: %s\n",
305 path->bv_val, strerror(errno), 0 );
306 return LDAP_NO_SUCH_OBJECT;
309 e = get_entry_for_fd(fd, pdn, pndn);
311 Debug( LDAP_DEBUG_ANY,
312 "=> ldif_enum_tree: failed to read entry for %s\n",
313 path->bv_val, 0, 0 );
317 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
318 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
319 /* Send right away? */
322 * if it's a referral, add it to the list of referrals. only do
323 * this for non-base searches, and don't check the filter
324 * explicitly here since it's only a candidate anyway.
326 if ( !get_manageDSAit( ck->op )
327 && ck->op->ors_scope != LDAP_SCOPE_BASE
328 && is_entry_referral( e ) )
330 BerVarray erefs = get_entry_referrals( ck->op, e );
331 ck->rs->sr_ref = referral_rewrite( erefs,
333 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
334 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
336 ck->rs->sr_entry = e;
337 rc = send_search_reference( ck->op, ck->rs );
338 ber_bvarray_free( ck->rs->sr_ref );
339 ber_bvarray_free( erefs );
340 ck->rs->sr_ref = NULL;
341 ck->rs->sr_entry = NULL;
343 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
345 ck->rs->sr_entry = e;
346 ck->rs->sr_attrs = ck->op->ors_attrs;
347 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
348 rc = send_search_entry(ck->op, ck->rs);
349 ck->rs->sr_entry = NULL;
355 /* Queueing up for tool mode */
356 if(ck->entries == NULL) {
357 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
358 ck->elen = ENTRY_BUFF_INCREMENT;
360 if(ck->eind >= ck->elen) { /* grow entries if necessary */
361 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
365 ck->entries[ck->eind++] = e;
372 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
374 bvlist *list = NULL, *ptr;
376 path->bv_len -= STRLENOF( LDIF );
377 path->bv_val[path->bv_len] = '\0';
379 dir_of_path = opendir(path->bv_val);
380 if(dir_of_path == NULL) { /* can't open directory */
381 if ( errno != ENOENT ) {
382 /* it shouldn't be treated as an error
383 * only if the directory doesn't exist */
385 Debug( LDAP_DEBUG_ANY,
386 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
387 path->bv_val, errno, 0 );
393 struct berval fname, itmp;
397 dir = readdir(dir_of_path);
398 if(dir == NULL) break; /* end of the directory */
399 fname.bv_len = strlen( dir->d_name );
400 if ( fname.bv_len <= STRLENOF( LDIF ))
402 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
404 fname.bv_val = dir->d_name;
406 bvl = ch_malloc( sizeof(bvlist) );
407 ber_dupbv( &bvl->bv, &fname );
408 BER_BVZERO( &bvl->num );
409 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
413 ptr = strchr( itmp.bv_val, IX_FSR );
415 itmp.bv_len = ptr - itmp.bv_val;
416 ber_dupbv( &bvl->num, &itmp );
417 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
418 itmp.bv_val[0] = '\0';
419 bvl->off = itmp.bv_val - bvl->bv.bv_val;
423 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
424 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
425 if ( !cmp && bvl->num.bv_val )
426 cmp = bvl->inum - ptr->inum;
434 closedir(dir_of_path);
436 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
437 ck->op->ors_scope = LDAP_SCOPE_BASE;
438 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
439 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
441 while ( ( ptr = list ) ) {
446 if ( rc == LDAP_SUCCESS ) {
447 if ( ptr->num.bv_val )
448 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
450 fullpath( path, &ptr->bv, &fpath );
451 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
454 if ( ptr->num.bv_val )
455 free( ptr->num.bv_val );
456 free(ptr->bv.bv_val);
461 if ( fd ) entry_free( e );
470 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
472 struct berval pdn, pndn;
475 dnParent( &ck->op->o_req_dn, &pdn );
476 dnParent( &ck->op->o_req_ndn, &pndn );
477 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
478 rc = r_enum_tree(ck, &path, &pdn, &pndn);
479 ch_free( path.bv_val );
483 /* Get the parent path plus the LDIF suffix */
484 static void get_parent_path(struct berval * dnpath, struct berval *res) {
485 int dnpathlen = dnpath->bv_len;
488 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
489 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
492 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
493 strncpy(res->bv_val, dnpath->bv_val, i);
494 strcpy(res->bv_val+i, LDIF);
495 res->bv_val[i] = '\0';
498 static int apply_modify_to_entry(Entry * entry,
499 Modifications * modlist,
503 char textbuf[SLAP_TEXT_BUFLEN];
504 int rc = LDAP_UNWILLING_TO_PERFORM;
505 Modification *mods = NULL;
507 if (!acl_check_modlist(op, entry, modlist)) {
508 return LDAP_INSUFFICIENT_ACCESS;
511 for (; modlist != NULL; modlist = modlist->sml_next) {
512 mods = &modlist->sml_mod;
514 switch (mods->sm_op) {
516 rc = modify_add_values(entry, mods,
517 get_permissiveModify(op),
518 &rs->sr_text, textbuf,
522 case LDAP_MOD_DELETE:
523 rc = modify_delete_values(entry, mods,
524 get_permissiveModify(op),
525 &rs->sr_text, textbuf,
529 case LDAP_MOD_REPLACE:
530 rc = modify_replace_values(entry, mods,
531 get_permissiveModify(op),
532 &rs->sr_text, textbuf,
536 case LDAP_MOD_INCREMENT:
537 rc = modify_increment_values( entry,
538 mods, get_permissiveModify(op),
539 &rs->sr_text, textbuf,
545 case SLAP_MOD_SOFTADD:
546 mods->sm_op = LDAP_MOD_ADD;
547 rc = modify_add_values(entry, mods,
548 get_permissiveModify(op),
549 &rs->sr_text, textbuf,
551 mods->sm_op = SLAP_MOD_SOFTADD;
552 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
559 if(rc != LDAP_SUCCESS) break;
562 if(rc == LDAP_SUCCESS) {
563 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
564 entry->e_ocflags = 0;
566 /* check that the entry still obeys the schema */
567 rc = entry_schema_check(op, entry, NULL, 0,
568 &rs->sr_text, textbuf, sizeof( textbuf ) );
574 ldif_back_referrals( Operation *op, SlapReply *rs )
576 struct ldif_info *ni = NULL;
578 int rc = LDAP_SUCCESS;
581 if ( op->o_tag == LDAP_REQ_SEARCH ) {
582 /* let search take care of itself */
587 if ( get_manageDSAit( op ) ) {
588 /* let op take care of DSA management */
592 ni = (struct ldif_info *)op->o_bd->be_private;
593 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
594 entry = (Entry *)get_entry( op, &ni->li_base_path );
596 /* no object is found for them */
597 if ( entry == NULL ) {
598 struct berval odn = op->o_req_dn;
599 struct berval ondn = op->o_req_ndn;
601 struct berval pndn = op->o_req_ndn;
603 for ( ; entry == NULL; ) {
604 dnParent( &pndn, &pndn );
606 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
611 op->o_req_ndn = pndn;
613 entry = (Entry *)get_entry( op, &ni->li_base_path );
616 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
619 op->o_req_ndn = ondn;
622 rs->sr_matched = NULL;
623 if ( entry != NULL ) {
624 Debug( LDAP_DEBUG_TRACE,
625 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
626 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
628 if ( is_entry_referral( entry ) ) {
630 rs->sr_ref = get_entry_referrals( op, entry );
632 rs->sr_matched = ber_strdup_x(
633 entry->e_name.bv_val, op->o_tmpmemctx );
639 } else if ( default_referral != NULL ) {
641 rs->sr_ref = referral_rewrite( default_referral,
642 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
645 if ( rs->sr_ref != NULL ) {
647 rc = rs->sr_err = LDAP_REFERRAL;
648 send_ldap_result( op, rs );
649 ber_bvarray_free( rs->sr_ref );
652 } else if ( rc != LDAP_SUCCESS ) {
654 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
655 send_ldap_result( op, rs );
658 if ( rs->sr_matched ) {
659 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
660 rs->sr_matched = NULL;
666 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
668 if ( is_entry_referral( entry ) ) {
669 /* entry is a referral */
670 BerVarray refs = get_entry_referrals( op, entry );
671 rs->sr_ref = referral_rewrite(
672 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
674 Debug( LDAP_DEBUG_TRACE,
675 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
676 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
678 rs->sr_matched = entry->e_name.bv_val;
679 if ( rs->sr_ref != NULL ) {
680 rc = rs->sr_err = LDAP_REFERRAL;
681 send_ldap_result( op, rs );
682 ber_bvarray_free( rs->sr_ref );
686 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
690 rs->sr_matched = NULL;
691 ber_bvarray_free( refs );
700 ldif_back_bind( Operation *op, SlapReply *rs )
702 struct ldif_info *ni = NULL;
703 Attribute * a = NULL;
704 AttributeDescription *password = slap_schema.si_ad_userPassword;
706 Entry * entry = NULL;
708 ni = (struct ldif_info *) op->o_bd->be_private;
709 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
710 entry = (Entry *) get_entry(op, &ni->li_base_path);
712 /* no object is found for them */
714 if(be_isroot_pw(op)) {
715 rs->sr_err = return_val = LDAP_SUCCESS;
717 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
722 /* they don't have userpassword */
723 if((a = attr_find(entry->e_attrs, password)) == NULL) {
724 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
729 /* authentication actually failed */
730 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
731 &rs->sr_text) != 0) {
732 rs->sr_err = LDAP_INVALID_CREDENTIALS;
737 /* let the front-end send success */
742 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
744 send_ldap_result( op, rs );
750 static int ldif_back_search(Operation *op, SlapReply *rs)
752 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
753 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
757 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
758 rs->sr_err = enum_tree( &ck );
759 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
760 send_ldap_result(op, rs);
765 static int ldif_back_add(Operation *op, SlapReply *rs) {
766 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
767 Entry * e = op->ora_e;
768 struct berval dn = e->e_nname;
769 struct berval leaf_path = BER_BVNULL;
772 char textbuf[SLAP_TEXT_BUFLEN];
774 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
775 slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
777 rs->sr_err = entry_schema_check(op, e, NULL, 0,
778 &rs->sr_text, textbuf, sizeof( textbuf ) );
779 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
781 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
783 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
785 if(leaf_path.bv_val != NULL) {
786 struct berval base = BER_BVNULL;
787 /* build path to container and ldif of container */
788 get_parent_path(&leaf_path, &base);
790 statres = stat(base.bv_val, &stats); /* check if container exists */
791 if(statres == -1 && errno == ENOENT) { /* container missing */
792 base.bv_val[base.bv_len] = '.';
793 statres = stat(base.bv_val, &stats); /* check for leaf node */
794 base.bv_val[base.bv_len] = '\0';
795 if(statres == -1 && errno == ENOENT) {
796 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
797 rs->sr_text = "Parent does not exist";
799 else if(statres != -1) { /* create parent */
800 int mkdirres = mkdir(base.bv_val, 0750);
802 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
803 rs->sr_text = "Could not create parent folder";
804 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
805 base.bv_val, strerror( errno ), 0 );
809 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
810 }/* container was possibly created, move on to add the entry */
811 if(rs->sr_err == LDAP_SUCCESS) {
812 statres = stat(leaf_path.bv_val, &stats);
813 if(statres == -1 && errno == ENOENT) {
814 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
815 rs->sr_err = (int) spew_entry(e, &leaf_path);
816 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
818 else if ( statres == -1 ) {
819 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
820 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
821 leaf_path.bv_val, strerror( errno ), 0 );
823 else /* it already exists */
824 rs->sr_err = LDAP_ALREADY_EXISTS;
826 SLAP_FREE(base.bv_val);
827 SLAP_FREE(leaf_path.bv_val);
830 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
833 Debug( LDAP_DEBUG_TRACE,
834 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text, 0);
835 send_ldap_result(op, rs);
836 slap_graduate_commit_csn( op );
840 static int ldif_back_modify(Operation *op, SlapReply *rs) {
841 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
842 Modifications * modlst = op->orm_modlist;
843 struct berval path = BER_BVNULL;
844 Entry * entry = NULL;
847 slap_mods_opattrs( op, &op->orm_modlist, 1 );
849 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
850 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
852 entry = (Entry *) get_entry(op, &ni->li_base_path);
855 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
856 if(rs->sr_err == LDAP_SUCCESS) {
858 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
859 spew_res = spew_entry(entry, &path);
861 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
863 Debug( LDAP_DEBUG_ANY,
864 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
865 op->o_log_prefix, entry->e_name.bv_val, strerror( save_errno ) );
866 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
871 rs->sr_err = LDAP_NO_SUCH_OBJECT;
876 if(path.bv_val != NULL)
877 SLAP_FREE(path.bv_val);
879 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
880 send_ldap_result(op, rs);
881 slap_graduate_commit_csn( op );
885 static int ldif_back_delete(Operation *op, SlapReply *rs) {
886 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
887 struct berval path = BER_BVNULL;
890 if ( BER_BVISEMPTY( &op->o_csn )) {
892 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
895 csn.bv_len = sizeof( csnbuf );
896 slap_get_csn( op, &csn, 1 );
899 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
900 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
902 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
903 res = rmdir(path.bv_val);
904 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
905 if ( res && errno != ENOENT ) {
906 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
908 res = unlink(path.bv_val);
913 rs->sr_err = LDAP_NO_SUCH_OBJECT;
915 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
918 rs->sr_err = LDAP_SUCCESS;
920 SLAP_FREE(path.bv_val);
921 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
922 send_ldap_result(op, rs);
923 slap_graduate_commit_csn( op );
928 static int move_entry(Entry * entry, struct berval * ndn,
929 struct berval * newndn, struct berval * rootdn,
930 struct berval * base_path) {
934 struct berval newpath;
936 dn2path(ndn, rootdn, base_path, &path);
937 dn2path(newndn, rootdn, base_path, &newpath);
939 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
940 /* some object doesn't exist */
941 res = LDAP_NO_SUCH_OBJECT;
943 else { /* do the modrdn */
944 exists_res = open(newpath.bv_val, O_RDONLY);
945 if(exists_res == -1 && errno == ENOENT) {
946 res = spew_entry(entry, &newpath);
948 /* if this fails we should log something bad */
949 res = unlink(path.bv_val);
954 res = LDAP_NO_SUCH_OBJECT;
956 res = LDAP_UNWILLING_TO_PERFORM;
957 unlink(newpath.bv_val); /* in case file was created */
960 else if(exists_res) {
961 int close_res = close(exists_res);
962 res = LDAP_ALREADY_EXISTS;
963 if(close_res == -1) {
964 /* log heinous error */
968 res = LDAP_UNWILLING_TO_PERFORM;
972 if(newpath.bv_val != NULL)
973 SLAP_FREE(newpath.bv_val);
974 if(path.bv_val != NULL)
975 SLAP_FREE(path.bv_val);
980 ldif_back_modrdn(Operation *op, SlapReply *rs)
982 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
983 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
985 Entry * entry = NULL;
988 slap_mods_opattrs( op, &op->orr_modlist, 1 );
990 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
991 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
992 entry = (Entry *) get_entry( op, &ni->li_base_path );
994 /* build the mods to the entry */
995 if ( entry != NULL ) {
996 /* build new dn, and new ndn for the entry */
997 if ( op->oq_modrdn.rs_newSup != NULL ) {
999 p_dn = *op->oq_modrdn.rs_newSup;
1001 p_dn = slap_empty_bv;
1003 dnParent( &entry->e_name, &p_dn );
1004 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1005 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1006 ber_memfree_x( entry->e_name.bv_val, NULL );
1007 ber_memfree_x( entry->e_nname.bv_val, NULL );
1008 entry->e_name = new_dn;
1009 entry->e_nname = new_ndn;
1011 /* perform the modifications */
1012 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1013 if ( res == LDAP_SUCCESS ) {
1014 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1016 &op->o_bd->be_nsuffix[0],
1017 &ni->li_base_path );
1022 /* entry was null */
1023 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1026 if ( entry != NULL ) {
1027 entry_free( entry );
1030 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1031 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1032 send_ldap_result( op, rs );
1033 slap_graduate_commit_csn( op );
1037 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1039 int ldif_back_entry_get(
1043 AttributeDescription *at,
1047 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1049 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
1051 *ent = (Entry *) get_entry( op, &ni->li_base_path );
1053 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1055 return ( *ent == NULL ? 1 : 0 );
1058 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1059 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1060 ni->li_tool_current = 0;
1064 static int ldif_tool_entry_close(BackendDB * be) {
1065 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1067 SLAP_FREE(ni->li_tool_cookie.entries);
1072 ldif_tool_entry_first(BackendDB *be)
1074 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1075 ID id = 1; /* first entry in the array of entries shifted by one */
1077 ni->li_tool_current = 1;
1078 if(ni->li_tool_cookie.entries == NULL) {
1082 op.o_req_dn = *be->be_suffix;
1083 op.o_req_ndn = *be->be_nsuffix;
1084 op.ors_scope = LDAP_SCOPE_SUBTREE;
1085 ni->li_tool_cookie.op = &op;
1086 (void)enum_tree( &ni->li_tool_cookie );
1087 ni->li_tool_cookie.op = NULL;
1092 static ID ldif_tool_entry_next(BackendDB *be)
1094 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1095 ni->li_tool_current += 1;
1096 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1099 return ni->li_tool_current;
1102 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1103 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1106 if(id > ni->li_tool_cookie.eind || id < 1)
1109 e = ni->li_tool_cookie.entries[id - 1];
1110 ni->li_tool_cookie.entries[id - 1] = NULL;
1115 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1116 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1117 struct berval dn = e->e_nname;
1118 struct berval leaf_path = BER_BVNULL;
1121 int res = LDAP_SUCCESS;
1123 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1125 if(leaf_path.bv_val != NULL) {
1126 struct berval base = BER_BVNULL;
1127 /* build path to container, and path to ldif of container */
1128 get_parent_path(&leaf_path, &base);
1130 statres = stat(base.bv_val, &stats); /* check if container exists */
1131 if(statres == -1 && errno == ENOENT) { /* container missing */
1132 base.bv_val[base.bv_len] = '.';
1133 statres = stat(base.bv_val, &stats); /* check for leaf node */
1134 base.bv_val[base.bv_len] = '\0';
1135 if(statres == -1 && errno == ENOENT) {
1136 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1138 else if(statres != -1) { /* create parent */
1139 int mkdirres = mkdir(base.bv_val, 0750);
1140 if(mkdirres == -1) {
1141 res = LDAP_UNWILLING_TO_PERFORM;
1145 res = LDAP_UNWILLING_TO_PERFORM;
1146 }/* container was possibly created, move on to add the entry */
1147 if(res == LDAP_SUCCESS) {
1148 statres = stat(leaf_path.bv_val, &stats);
1149 if(statres == -1 && errno == ENOENT) {
1150 res = (int) spew_entry(e, &leaf_path);
1152 else /* it already exists */
1153 res = LDAP_ALREADY_EXISTS;
1155 SLAP_FREE(base.bv_val);
1156 SLAP_FREE(leaf_path.bv_val);
1159 if(res == LDAP_SUCCESS) {
1167 ldif_back_db_init( BackendDB *be )
1169 struct ldif_info *ni;
1171 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1172 be->be_private = ni;
1173 be->be_cf_ocs = ldifocs;
1174 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1179 ldif_back_db_destroy(
1183 struct ldif_info *ni = be->be_private;
1185 ch_free(ni->li_base_path.bv_val);
1186 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1187 free( be->be_private );
1196 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1197 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1198 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1205 ldif_back_initialize(
1209 static char *controls[] = {
1210 LDAP_CONTROL_MANAGEDSAIT,
1216 SLAP_BFLAG_INCREMENT |
1217 SLAP_BFLAG_REFERRALS;
1219 bi->bi_controls = controls;
1226 bi->bi_db_init = ldif_back_db_init;
1227 bi->bi_db_config = config_generic_wrapper;
1228 bi->bi_db_open = ldif_back_db_open;
1229 bi->bi_db_close = 0;
1230 bi->bi_db_destroy = ldif_back_db_destroy;
1232 bi->bi_op_bind = ldif_back_bind;
1233 bi->bi_op_unbind = 0;
1234 bi->bi_op_search = ldif_back_search;
1235 bi->bi_op_compare = 0;
1236 bi->bi_op_modify = ldif_back_modify;
1237 bi->bi_op_modrdn = ldif_back_modrdn;
1238 bi->bi_op_add = ldif_back_add;
1239 bi->bi_op_delete = ldif_back_delete;
1240 bi->bi_op_abandon = 0;
1242 bi->bi_extended = 0;
1244 bi->bi_chk_referrals = ldif_back_referrals;
1246 bi->bi_connection_init = 0;
1247 bi->bi_connection_destroy = 0;
1249 bi->bi_entry_get_rw = ldif_back_entry_get;
1251 #if 0 /* NOTE: uncomment to completely disable access control */
1252 bi->bi_access_allowed = slap_access_always_allowed;
1255 bi->bi_tool_entry_open = ldif_tool_entry_open;
1256 bi->bi_tool_entry_close = ldif_tool_entry_close;
1257 bi->bi_tool_entry_first = ldif_tool_entry_first;
1258 bi->bi_tool_entry_next = ldif_tool_entry_next;
1259 bi->bi_tool_entry_get = ldif_tool_entry_get;
1260 bi->bi_tool_entry_put = ldif_tool_entry_put;
1261 bi->bi_tool_entry_reindex = 0;
1262 bi->bi_tool_sync = 0;
1264 bi->bi_tool_dn2id_get = 0;
1265 bi->bi_tool_id2entry_get = 0;
1266 bi->bi_tool_entry_modify = 0;
1268 bi->bi_cf_ocs = ldifocs;
1270 rc = config_register_schema( ldifcfg, ldifocs );
1271 if ( rc ) return rc;