1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2008 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_rdwr_t li_rdwr;
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 * orig_dn, struct berval * suffixdn, struct berval * base_path,
88 char *ptr, *sep, *end;
92 assert( orig_dn != NULL );
93 assert( !BER_BVISNULL( orig_dn ) );
94 assert( suffixdn != NULL );
95 assert( !BER_BVISNULL( suffixdn ) );
96 assert( dnIsSuffix( orig_dn, suffixdn ) );
100 for ( ptr = dn.bv_val, end = &dn.bv_val[dn.bv_len]; ptr < end; ptr++) {
101 if ( ptr[0] == LDAP_DIRSEP[0] ) {
110 dn.bv_val = ch_malloc( dn.bv_len + 1 );
112 for ( ptr = orig_dn->bv_val, end = &orig_dn->bv_val[orig_dn->bv_len], p = dn.bv_val;
113 ptr < end; ptr++, p++)
115 static const char hex[] = "0123456789ABCDEF";
116 if ( ptr[0] == LDAP_DIRSEP[0] ) {
117 *p++ = '\\'; /* FIXME: fs-escape */
118 *p++ = hex[(LDAP_DIRSEP[0] & 0xF0U) >> 4];
119 *p = hex[LDAP_DIRSEP[0] & 0x0FU];
127 res->bv_len = dn.bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
128 res->bv_val = ch_malloc( res->bv_len + 1 );
129 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
130 *ptr++ = LDAP_DIRSEP[0];
131 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
132 end = dn.bv_val + dn.bv_len - suffixdn->bv_len - 1;
133 while ( end > dn.bv_val ) {
134 for (sep = end-1; sep >= dn.bv_val && !DN_SEPARATOR( *sep ); sep--);
135 *ptr++ = LDAP_DIRSEP[0];
136 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
142 while( ptr=strchr(ptr, IX_DNL) ) {
144 ptr = strchr(ptr, IX_DNR);
151 if ( dn.bv_val != orig_dn->bv_val ) {
152 ch_free( dn.bv_val );
156 static char * slurp_file(int fd) {
157 int read_chars_total = 0;
165 entry_size = st.st_size;
166 entry = ch_malloc( entry_size+1 );
170 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
171 if(read_chars == -1) {
175 if(read_chars == 0) {
176 entry[read_chars_total] = '\0';
180 read_chars_total += read_chars;
181 entry_pos += read_chars;
187 static int spew_file(int fd, char * spew, int len) {
191 writeres = write(fd, spew, len);
193 Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
194 STRERROR( errno ), 0, 0 );
205 static int spew_entry(Entry * e, struct berval * path) {
210 char * entry_as_string;
212 openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
215 rs = LDAP_NO_SUCH_OBJECT;
217 rs = LDAP_UNWILLING_TO_PERFORM;
218 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
219 path->bv_val, STRERROR( errno ), 0 );
225 /* Only save the RDN onto disk */
226 dnRdn( &e->e_name, &rdn );
227 if ( rdn.bv_len != e->e_name.bv_len ) {
228 e->e_name.bv_val[rdn.bv_len] = '\0';
229 tmp = e->e_name.bv_len;
230 e->e_name.bv_len = rdn.bv_len;
234 entry_as_string = entry2str(e, &entry_length);
236 /* Restore full DN */
237 if ( rdn.bv_len != e->e_name.bv_len ) {
238 e->e_name.bv_val[e->e_name.bv_len] = ',';
239 e->e_name.bv_len = rdn.bv_len;
242 if(entry_as_string == NULL) {
243 rs = LDAP_UNWILLING_TO_PERFORM;
247 spew_res = spew_file(openres, entry_as_string, entry_length);
250 rs = LDAP_UNWILLING_TO_PERFORM;
258 static Entry * get_entry_for_fd(int fd,
262 char * entry = (char *) slurp_file(fd);
263 Entry * ldentry = NULL;
265 /* error reading file */
270 ldentry = str2entry(entry);
273 rdn = ldentry->e_name;
274 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
275 ch_free( rdn.bv_val );
276 rdn = ldentry->e_nname;
277 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
278 ch_free( rdn.bv_val );
292 static Entry * get_entry(Operation *op, struct berval *base_path) {
293 struct berval path, pdn, pndn;
296 dnParent(&op->o_req_dn, &pdn);
297 dnParent(&op->o_req_ndn, &pndn);
298 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
299 fd = open(path.bv_val, O_RDONLY);
300 /* error opening file (mebbe should log error) */
301 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
302 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
303 path.bv_val, STRERROR(errno), 0 );
306 if(path.bv_val != NULL)
307 SLAP_FREE(path.bv_val);
310 return get_entry_for_fd(fd, &pdn, &pndn);
316 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
318 res->bv_len = name->bv_len + base->bv_len + 1;
319 res->bv_val = ch_malloc( res->bv_len + 1 );
320 strcpy(res->bv_val, base->bv_val);
321 ptr = res->bv_val + base->bv_len;
322 *ptr++ = LDAP_DIRSEP[0];
323 strcpy(ptr, name->bv_val);
326 typedef struct bvlist {
335 static int r_enum_tree(enumCookie *ck, struct berval *path,
336 struct berval *pdn, struct berval *pndn)
339 int fd, rc = LDAP_SUCCESS;
341 fd = open( path->bv_val, O_RDONLY );
343 Debug( LDAP_DEBUG_ANY,
344 "=> ldif_enum_tree: failed to open %s: %s\n",
345 path->bv_val, STRERROR(errno), 0 );
346 return LDAP_NO_SUCH_OBJECT;
349 e = get_entry_for_fd(fd, pdn, pndn);
351 Debug( LDAP_DEBUG_ANY,
352 "=> ldif_enum_tree: failed to read entry for %s\n",
353 path->bv_val, 0, 0 );
357 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
358 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
359 /* Send right away? */
362 * if it's a referral, add it to the list of referrals. only do
363 * this for non-base searches, and don't check the filter
364 * explicitly here since it's only a candidate anyway.
366 if ( !get_manageDSAit( ck->op )
367 && ck->op->ors_scope != LDAP_SCOPE_BASE
368 && is_entry_referral( e ) )
370 BerVarray erefs = get_entry_referrals( ck->op, e );
371 ck->rs->sr_ref = referral_rewrite( erefs,
373 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
374 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
376 ck->rs->sr_entry = e;
377 rc = send_search_reference( ck->op, ck->rs );
378 ber_bvarray_free( ck->rs->sr_ref );
379 ber_bvarray_free( erefs );
380 ck->rs->sr_ref = NULL;
381 ck->rs->sr_entry = NULL;
383 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
385 ck->rs->sr_entry = e;
386 ck->rs->sr_attrs = ck->op->ors_attrs;
387 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
388 rc = send_search_entry(ck->op, ck->rs);
389 ck->rs->sr_entry = NULL;
395 /* Queueing up for tool mode */
396 if(ck->entries == NULL) {
397 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
398 ck->elen = ENTRY_BUFF_INCREMENT;
400 if(ck->eind >= ck->elen) { /* grow entries if necessary */
401 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
405 ck->entries[ck->eind++] = e;
412 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
414 bvlist *list = NULL, *ptr;
416 path->bv_len -= STRLENOF( LDIF );
417 path->bv_val[path->bv_len] = '\0';
419 dir_of_path = opendir(path->bv_val);
420 if(dir_of_path == NULL) { /* can't open directory */
421 if ( errno != ENOENT ) {
422 /* it shouldn't be treated as an error
423 * only if the directory doesn't exist */
425 Debug( LDAP_DEBUG_ANY,
426 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
427 path->bv_val, errno, 0 );
433 struct berval fname, itmp;
437 dir = readdir(dir_of_path);
438 if(dir == NULL) break; /* end of the directory */
439 fname.bv_len = strlen( dir->d_name );
440 if ( fname.bv_len <= STRLENOF( LDIF ))
442 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
444 fname.bv_val = dir->d_name;
446 bvl = ch_malloc( sizeof(bvlist) );
447 ber_dupbv( &bvl->bv, &fname );
448 BER_BVZERO( &bvl->num );
449 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
453 ptr = strchr( itmp.bv_val, IX_FSR );
455 itmp.bv_len = ptr - itmp.bv_val;
456 ber_dupbv( &bvl->num, &itmp );
457 bvl->inum = strtol( itmp.bv_val, NULL, 0 );
458 itmp.bv_val[0] = '\0';
459 bvl->off = itmp.bv_val - bvl->bv.bv_val;
463 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
464 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
465 if ( !cmp && bvl->num.bv_val )
466 cmp = bvl->inum - ptr->inum;
474 closedir(dir_of_path);
476 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
477 ck->op->ors_scope = LDAP_SCOPE_BASE;
478 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
479 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
481 while ( ( ptr = list ) ) {
486 if ( rc == LDAP_SUCCESS ) {
487 if ( ptr->num.bv_val )
488 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
490 fullpath( path, &ptr->bv, &fpath );
491 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
494 if ( ptr->num.bv_val )
495 free( ptr->num.bv_val );
496 free(ptr->bv.bv_val);
501 if ( fd ) entry_free( e );
510 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
512 struct berval pdn, pndn;
515 dnParent( &ck->op->o_req_dn, &pdn );
516 dnParent( &ck->op->o_req_ndn, &pndn );
517 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
518 rc = r_enum_tree(ck, &path, &pdn, &pndn);
519 ch_free( path.bv_val );
523 /* Get the parent path plus the LDIF suffix */
524 static void get_parent_path(struct berval * dnpath, struct berval *res) {
525 int dnpathlen = dnpath->bv_len;
528 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
529 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
532 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
533 strncpy(res->bv_val, dnpath->bv_val, i);
534 strcpy(res->bv_val+i, LDIF);
535 res->bv_val[i] = '\0';
538 static int apply_modify_to_entry(Entry * entry,
539 Modifications * modlist,
543 char textbuf[SLAP_TEXT_BUFLEN];
544 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
546 Modification *mods = NULL;
548 if (!acl_check_modlist(op, entry, modlist)) {
549 return LDAP_INSUFFICIENT_ACCESS;
552 for (; modlist != NULL; modlist = modlist->sml_next) {
553 mods = &modlist->sml_mod;
555 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
558 switch (mods->sm_op) {
560 rc = modify_add_values(entry, mods,
561 get_permissiveModify(op),
562 &rs->sr_text, textbuf,
566 case LDAP_MOD_DELETE:
567 rc = modify_delete_values(entry, mods,
568 get_permissiveModify(op),
569 &rs->sr_text, textbuf,
573 case LDAP_MOD_REPLACE:
574 rc = modify_replace_values(entry, mods,
575 get_permissiveModify(op),
576 &rs->sr_text, textbuf,
580 case LDAP_MOD_INCREMENT:
581 rc = modify_increment_values( entry,
582 mods, get_permissiveModify(op),
583 &rs->sr_text, textbuf,
589 case SLAP_MOD_SOFTADD:
590 mods->sm_op = LDAP_MOD_ADD;
591 rc = modify_add_values(entry, mods,
592 get_permissiveModify(op),
593 &rs->sr_text, textbuf,
595 mods->sm_op = SLAP_MOD_SOFTADD;
596 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
603 if(rc != LDAP_SUCCESS) break;
606 if(rc == LDAP_SUCCESS) {
608 entry->e_ocflags = 0;
610 /* check that the entry still obeys the schema */
611 rc = entry_schema_check( op, entry, NULL, 0,
612 &rs->sr_text, textbuf, sizeof( textbuf ) );
619 ldif_back_referrals( Operation *op, SlapReply *rs )
621 struct ldif_info *ni = NULL;
623 int rc = LDAP_SUCCESS;
626 if ( op->o_tag == LDAP_REQ_SEARCH ) {
627 /* let search take care of itself */
632 if ( get_manageDSAit( op ) ) {
633 /* let op take care of DSA management */
637 ni = (struct ldif_info *)op->o_bd->be_private;
638 ldap_pvt_thread_rdwr_rlock( &ni->li_rdwr );
639 entry = (Entry *)get_entry( op, &ni->li_base_path );
641 /* no object is found for them */
642 if ( entry == NULL ) {
643 struct berval odn = op->o_req_dn;
644 struct berval ondn = op->o_req_ndn;
646 struct berval pndn = op->o_req_ndn;
648 for ( ; entry == NULL; ) {
649 dnParent( &pndn, &pndn );
651 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
656 op->o_req_ndn = pndn;
658 entry = (Entry *)get_entry( op, &ni->li_base_path );
661 ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr );
664 op->o_req_ndn = ondn;
667 rs->sr_matched = NULL;
668 if ( entry != NULL ) {
669 Debug( LDAP_DEBUG_TRACE,
670 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
671 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
673 if ( is_entry_referral( entry ) ) {
675 rs->sr_ref = get_entry_referrals( op, entry );
677 rs->sr_matched = ber_strdup_x(
678 entry->e_name.bv_val, op->o_tmpmemctx );
684 } else if ( default_referral != NULL ) {
686 rs->sr_ref = referral_rewrite( default_referral,
687 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
690 if ( rs->sr_ref != NULL ) {
692 rc = rs->sr_err = LDAP_REFERRAL;
693 send_ldap_result( op, rs );
694 ber_bvarray_free( rs->sr_ref );
697 } else if ( rc != LDAP_SUCCESS ) {
699 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
700 send_ldap_result( op, rs );
703 if ( rs->sr_matched ) {
704 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
705 rs->sr_matched = NULL;
711 ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr );
713 if ( is_entry_referral( entry ) ) {
714 /* entry is a referral */
715 BerVarray refs = get_entry_referrals( op, entry );
716 rs->sr_ref = referral_rewrite(
717 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
719 Debug( LDAP_DEBUG_TRACE,
720 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
721 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
723 rs->sr_matched = entry->e_name.bv_val;
724 if ( rs->sr_ref != NULL ) {
725 rc = rs->sr_err = LDAP_REFERRAL;
726 send_ldap_result( op, rs );
727 ber_bvarray_free( rs->sr_ref );
731 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
735 rs->sr_matched = NULL;
736 ber_bvarray_free( refs );
745 ldif_back_bind( Operation *op, SlapReply *rs )
747 struct ldif_info *ni = NULL;
748 Attribute * a = NULL;
749 AttributeDescription *password = slap_schema.si_ad_userPassword;
751 Entry * entry = NULL;
753 ni = (struct ldif_info *) op->o_bd->be_private;
754 ldap_pvt_thread_rdwr_rlock(&ni->li_rdwr);
755 entry = (Entry *) get_entry(op, &ni->li_base_path);
757 /* no object is found for them */
759 if(be_isroot_pw(op)) {
760 rs->sr_err = return_val = LDAP_SUCCESS;
762 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
767 /* they don't have userpassword */
768 if((a = attr_find(entry->e_attrs, password)) == NULL) {
769 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
774 /* authentication actually failed */
775 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
776 &rs->sr_text) != 0) {
777 rs->sr_err = LDAP_INVALID_CREDENTIALS;
782 /* let the front-end send success */
787 ldap_pvt_thread_rdwr_runlock(&ni->li_rdwr);
789 send_ldap_result( op, rs );
795 static int ldif_back_search(Operation *op, SlapReply *rs)
797 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
798 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
802 ldap_pvt_thread_rdwr_rlock(&ni->li_rdwr);
803 rs->sr_err = enum_tree( &ck );
804 ldap_pvt_thread_rdwr_runlock(&ni->li_rdwr);
805 send_ldap_result(op, rs);
810 static int ldif_back_add(Operation *op, SlapReply *rs) {
811 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
812 Entry * e = op->ora_e;
813 struct berval dn = e->e_nname;
814 struct berval leaf_path = BER_BVNULL;
817 char textbuf[SLAP_TEXT_BUFLEN];
819 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
820 slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
822 rs->sr_err = entry_schema_check(op, e, NULL, 0,
823 &rs->sr_text, textbuf, sizeof( textbuf ) );
824 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
826 ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr);
828 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
830 if(leaf_path.bv_val != NULL) {
831 struct berval base = BER_BVNULL;
832 /* build path to container and ldif of container */
833 get_parent_path(&leaf_path, &base);
835 statres = stat(base.bv_val, &stats); /* check if container exists */
836 if(statres == -1 && errno == ENOENT) { /* container missing */
837 base.bv_val[base.bv_len] = '.';
838 statres = stat(base.bv_val, &stats); /* check for leaf node */
839 base.bv_val[base.bv_len] = '\0';
840 if(statres == -1 && errno == ENOENT) {
841 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
842 rs->sr_text = "Parent does not exist";
844 else if(statres != -1) { /* create parent */
845 int mkdirres = mkdir(base.bv_val, 0750);
847 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
848 rs->sr_text = "Could not create parent folder";
849 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
850 base.bv_val, STRERROR( errno ), 0 );
854 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
855 }/* container was possibly created, move on to add the entry */
856 if(rs->sr_err == LDAP_SUCCESS) {
857 statres = stat(leaf_path.bv_val, &stats);
858 if(statres == -1 && errno == ENOENT) {
859 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
860 rs->sr_err = (int) spew_entry(e, &leaf_path);
861 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
863 else if ( statres == -1 ) {
864 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
865 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
866 leaf_path.bv_val, STRERROR( errno ), 0 );
868 else /* it already exists */
869 rs->sr_err = LDAP_ALREADY_EXISTS;
871 SLAP_FREE(base.bv_val);
872 SLAP_FREE(leaf_path.bv_val);
875 ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr);
878 Debug( LDAP_DEBUG_TRACE,
879 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
880 rs->sr_text : "", 0);
881 send_ldap_result(op, rs);
882 slap_graduate_commit_csn( op );
886 static int ldif_back_modify(Operation *op, SlapReply *rs) {
887 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
888 Modifications * modlst = op->orm_modlist;
889 struct berval path = BER_BVNULL;
890 Entry * entry = NULL;
893 slap_mods_opattrs( op, &op->orm_modlist, 1 );
895 ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr);
896 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
898 entry = (Entry *) get_entry(op, &ni->li_base_path);
901 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
902 if(rs->sr_err == LDAP_SUCCESS) {
904 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
905 spew_res = spew_entry(entry, &path);
907 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
909 Debug( LDAP_DEBUG_ANY,
910 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
911 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
912 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
917 rs->sr_err = LDAP_NO_SUCH_OBJECT;
922 if(path.bv_val != NULL)
923 SLAP_FREE(path.bv_val);
925 ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr);
926 send_ldap_result(op, rs);
927 slap_graduate_commit_csn( op );
931 static int ldif_back_delete(Operation *op, SlapReply *rs) {
932 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
933 struct berval path = BER_BVNULL;
936 if ( BER_BVISEMPTY( &op->o_csn )) {
938 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
941 csn.bv_len = sizeof( csnbuf );
942 slap_get_csn( op, &csn, 1 );
945 ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr);
946 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
948 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
949 res = rmdir(path.bv_val);
950 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
951 if ( res && errno != ENOENT ) {
952 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
954 res = unlink(path.bv_val);
959 rs->sr_err = LDAP_NO_SUCH_OBJECT;
961 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
964 rs->sr_err = LDAP_SUCCESS;
966 SLAP_FREE(path.bv_val);
967 ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr);
968 send_ldap_result(op, rs);
969 slap_graduate_commit_csn( op );
974 static int move_entry(Entry * entry, struct berval * ndn,
975 struct berval * newndn, struct berval * suffixdn,
976 struct berval * base_path) {
980 struct berval newpath;
982 dn2path(ndn, suffixdn, base_path, &path);
983 dn2path(newndn, suffixdn, base_path, &newpath);
985 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
986 /* some object doesn't exist */
987 res = LDAP_NO_SUCH_OBJECT;
989 else { /* do the modrdn */
990 exists_res = open(newpath.bv_val, O_RDONLY);
991 if(exists_res == -1 && errno == ENOENT) {
992 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
993 res = spew_entry(entry, &newpath);
995 /* if this fails we should log something bad */
996 res = unlink(path.bv_val);
1001 res = LDAP_NO_SUCH_OBJECT;
1003 res = LDAP_UNWILLING_TO_PERFORM;
1004 unlink(newpath.bv_val); /* in case file was created */
1006 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1008 else if(exists_res) {
1009 int close_res = close(exists_res);
1010 res = LDAP_ALREADY_EXISTS;
1011 if(close_res == -1) {
1012 /* log heinous error */
1016 res = LDAP_UNWILLING_TO_PERFORM;
1020 if(newpath.bv_val != NULL)
1021 SLAP_FREE(newpath.bv_val);
1022 if(path.bv_val != NULL)
1023 SLAP_FREE(path.bv_val);
1027 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
1028 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1029 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1030 struct berval p_dn, bv = BER_BVNULL;
1031 Entry * entry = NULL;
1032 LDAPRDN new_rdn = NULL;
1033 LDAPRDN old_rdn = NULL;
1034 Modifications * mods = NULL;
1037 ldap_pvt_thread_rdwr_wlock( &ni->li_rdwr );
1038 entry = (Entry *) get_entry(op, &ni->li_base_path);
1040 /* build the mods to the entry */
1042 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
1043 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
1044 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
1046 else if(op->oq_modrdn.rs_deleteoldrdn &&
1047 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
1048 LDAP_DN_FORMAT_LDAP)) {
1049 rs->sr_err = LDAP_OTHER;
1051 else { /* got both rdns successfully, ready to build mods */
1052 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
1054 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1056 else { /* built mods successfully */
1058 /* build new dn, and new ndn for the entry */
1059 if(op->oq_modrdn.rs_newSup != NULL) {
1060 struct berval op_dn = op->o_req_dn,
1061 op_ndn = op->o_req_ndn;
1065 p_dn = *op->oq_modrdn.rs_newSup;
1066 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1067 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1068 np = (Entry *)get_entry( op, &ni->li_base_path );
1069 op->o_req_dn = op_dn;
1070 op->o_req_ndn = op_ndn;
1072 goto no_such_object;
1076 dnParent(&entry->e_name, &p_dn);
1078 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
1079 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
1080 ber_dupbv( &new_ndn, &bv );
1081 entry->e_name = new_dn;
1082 entry->e_nname = new_ndn;
1084 /* perform the modifications */
1085 res = apply_modify_to_entry(entry, mods, op, rs);
1086 if(res == LDAP_SUCCESS) {
1087 rs->sr_err = move_entry(entry, &op->o_req_ndn,
1089 &op->o_bd->be_nsuffix[0],
1098 /* entry was null */
1099 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1102 if ( entry != NULL ) {
1106 ldap_pvt_thread_rdwr_wunlock( &ni->li_rdwr );
1107 send_ldap_result(op, rs);
1108 slap_graduate_commit_csn( op );
1112 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1114 int ldif_back_entry_get(
1118 AttributeDescription *at,
1122 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1123 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1125 assert( ndn != NULL );
1126 assert( !BER_BVISNULL( ndn ) );
1128 ldap_pvt_thread_rdwr_rlock( &ni->li_rdwr );
1129 op->o_req_dn = *ndn;
1130 op->o_req_ndn = *ndn;
1131 *ent = (Entry *) get_entry( op, &ni->li_base_path );
1132 op->o_req_dn = op_dn;
1133 op->o_req_ndn = op_ndn;
1134 ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr );
1136 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1141 return ( *ent == NULL ? 1 : 0 );
1144 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1145 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1146 ni->li_tool_current = 0;
1150 static int ldif_tool_entry_close(BackendDB * be) {
1151 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1153 SLAP_FREE(ni->li_tool_cookie.entries);
1158 ldif_tool_entry_first(BackendDB *be)
1160 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1161 ID id = 1; /* first entry in the array of entries shifted by one */
1163 ni->li_tool_current = 1;
1164 if(ni->li_tool_cookie.entries == NULL) {
1168 op.o_req_dn = *be->be_suffix;
1169 op.o_req_ndn = *be->be_nsuffix;
1170 op.ors_scope = LDAP_SCOPE_SUBTREE;
1171 ni->li_tool_cookie.op = &op;
1172 (void)enum_tree( &ni->li_tool_cookie );
1173 ni->li_tool_cookie.op = NULL;
1178 static ID ldif_tool_entry_next(BackendDB *be)
1180 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1181 ni->li_tool_current += 1;
1182 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1185 return ni->li_tool_current;
1188 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1189 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1192 if(id > ni->li_tool_cookie.eind || id < 1)
1195 e = ni->li_tool_cookie.entries[id - 1];
1196 ni->li_tool_cookie.entries[id - 1] = NULL;
1201 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1202 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1203 struct berval dn = e->e_nname;
1204 struct berval leaf_path = BER_BVNULL;
1207 int res = LDAP_SUCCESS;
1209 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1211 if(leaf_path.bv_val != NULL) {
1212 struct berval base = BER_BVNULL;
1213 /* build path to container, and path to ldif of container */
1214 get_parent_path(&leaf_path, &base);
1216 statres = stat(base.bv_val, &stats); /* check if container exists */
1217 if(statres == -1 && errno == ENOENT) { /* container missing */
1218 base.bv_val[base.bv_len] = '.';
1219 statres = stat(base.bv_val, &stats); /* check for leaf node */
1220 base.bv_val[base.bv_len] = '\0';
1221 if(statres == -1 && errno == ENOENT) {
1222 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1224 else if(statres != -1) { /* create parent */
1225 int mkdirres = mkdir(base.bv_val, 0750);
1226 if(mkdirres == -1) {
1227 res = LDAP_UNWILLING_TO_PERFORM;
1231 res = LDAP_UNWILLING_TO_PERFORM;
1232 }/* container was possibly created, move on to add the entry */
1233 if(res == LDAP_SUCCESS) {
1234 statres = stat(leaf_path.bv_val, &stats);
1235 if(statres == -1 && errno == ENOENT) {
1236 res = (int) spew_entry(e, &leaf_path);
1238 else /* it already exists */
1239 res = LDAP_ALREADY_EXISTS;
1241 SLAP_FREE(base.bv_val);
1242 SLAP_FREE(leaf_path.bv_val);
1245 if(res == LDAP_SUCCESS) {
1253 ldif_back_db_init( BackendDB *be )
1255 struct ldif_info *ni;
1257 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1258 be->be_private = ni;
1259 be->be_cf_ocs = ldifocs;
1260 ldap_pvt_thread_rdwr_init(&ni->li_rdwr);
1265 ldif_back_db_destroy(
1269 struct ldif_info *ni = be->be_private;
1271 ch_free(ni->li_base_path.bv_val);
1272 ldap_pvt_thread_rdwr_destroy(&ni->li_rdwr);
1273 free( be->be_private );
1282 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1283 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1284 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1291 ldif_back_initialize(
1295 static char *controls[] = {
1296 LDAP_CONTROL_MANAGEDSAIT,
1302 SLAP_BFLAG_INCREMENT |
1303 SLAP_BFLAG_REFERRALS;
1305 bi->bi_controls = controls;
1312 bi->bi_db_init = ldif_back_db_init;
1313 bi->bi_db_config = config_generic_wrapper;
1314 bi->bi_db_open = ldif_back_db_open;
1315 bi->bi_db_close = 0;
1316 bi->bi_db_destroy = ldif_back_db_destroy;
1318 bi->bi_op_bind = ldif_back_bind;
1319 bi->bi_op_unbind = 0;
1320 bi->bi_op_search = ldif_back_search;
1321 bi->bi_op_compare = 0;
1322 bi->bi_op_modify = ldif_back_modify;
1323 bi->bi_op_modrdn = ldif_back_modrdn;
1324 bi->bi_op_add = ldif_back_add;
1325 bi->bi_op_delete = ldif_back_delete;
1326 bi->bi_op_abandon = 0;
1328 bi->bi_extended = 0;
1330 bi->bi_chk_referrals = ldif_back_referrals;
1332 bi->bi_connection_init = 0;
1333 bi->bi_connection_destroy = 0;
1335 bi->bi_entry_get_rw = ldif_back_entry_get;
1337 #if 0 /* NOTE: uncomment to completely disable access control */
1338 #ifdef SLAP_OVERLAY_ACCESS
1339 bi->bi_access_allowed = slap_access_always_allowed;
1340 #endif /* SLAP_OVERLAY_ACCESS */
1343 bi->bi_tool_entry_open = ldif_tool_entry_open;
1344 bi->bi_tool_entry_close = ldif_tool_entry_close;
1345 bi->bi_tool_entry_first = ldif_tool_entry_first;
1346 bi->bi_tool_entry_next = ldif_tool_entry_next;
1347 bi->bi_tool_entry_get = ldif_tool_entry_get;
1348 bi->bi_tool_entry_put = ldif_tool_entry_put;
1349 bi->bi_tool_entry_reindex = 0;
1350 bi->bi_tool_sync = 0;
1352 bi->bi_tool_dn2id_get = 0;
1353 bi->bi_tool_id2entry_get = 0;
1354 bi->bi_tool_entry_modify = 0;
1356 bi->bi_cf_ocs = ldifocs;
1358 rc = config_register_schema( ldifcfg, ldifocs );
1359 if ( rc ) return rc;