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 * suffixdn, struct berval * base_path,
88 char *ptr, *sep, *end;
91 assert( !BER_BVISNULL( dn ) );
92 assert( suffixdn != NULL );
93 assert( !BER_BVISNULL( suffixdn ) );
94 assert( dnIsSuffix( dn, suffixdn ) );
96 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
97 res->bv_val = ch_malloc( res->bv_len + 1 );
98 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
99 *ptr++ = LDAP_DIRSEP[0];
100 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
101 end = dn->bv_val + dn->bv_len - suffixdn->bv_len - 1;
102 while ( end > dn->bv_val ) {
103 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
104 *ptr++ = LDAP_DIRSEP[0];
105 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
111 while( ptr=strchr(ptr, IX_DNL) ) {
113 ptr = strchr(ptr, IX_DNR);
122 static char * slurp_file(int fd) {
123 int read_chars_total = 0;
131 entry_size = st.st_size;
132 entry = ch_malloc( entry_size+1 );
136 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
137 if(read_chars == -1) {
141 if(read_chars == 0) {
142 entry[read_chars_total] = '\0';
146 read_chars_total += read_chars;
147 entry_pos += read_chars;
153 static int spew_file(int fd, char * spew, int len) {
157 writeres = write(fd, spew, len);
159 Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
160 strerror( errno ), 0, 0 );
171 static int spew_entry(Entry * e, struct berval * path) {
176 char * entry_as_string;
178 openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
181 rs = LDAP_NO_SUCH_OBJECT;
183 rs = LDAP_UNWILLING_TO_PERFORM;
184 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
185 path->bv_val, strerror( errno ), 0 );
191 /* Only save the RDN onto disk */
192 dnRdn( &e->e_name, &rdn );
193 if ( rdn.bv_len != e->e_name.bv_len ) {
194 e->e_name.bv_val[rdn.bv_len] = '\0';
195 tmp = e->e_name.bv_len;
196 e->e_name.bv_len = rdn.bv_len;
200 entry_as_string = entry2str(e, &entry_length);
202 /* Restore full DN */
203 if ( rdn.bv_len != e->e_name.bv_len ) {
204 e->e_name.bv_val[e->e_name.bv_len] = ',';
205 e->e_name.bv_len = rdn.bv_len;
208 if(entry_as_string == NULL) {
209 rs = LDAP_UNWILLING_TO_PERFORM;
213 spew_res = spew_file(openres, entry_as_string, entry_length);
216 rs = LDAP_UNWILLING_TO_PERFORM;
224 static Entry * get_entry_for_fd(int fd,
228 char * entry = (char *) slurp_file(fd);
229 Entry * ldentry = NULL;
231 /* error reading file */
236 ldentry = str2entry(entry);
239 rdn = ldentry->e_name;
240 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
241 ch_free( rdn.bv_val );
242 rdn = ldentry->e_nname;
243 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
244 ch_free( rdn.bv_val );
258 static Entry * get_entry(Operation *op, struct berval *base_path) {
259 struct berval path, pdn, pndn;
262 dnParent(&op->o_req_dn, &pdn);
263 dnParent(&op->o_req_ndn, &pndn);
264 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
265 fd = open(path.bv_val, O_RDONLY);
266 /* error opening file (mebbe should log error) */
268 Debug(LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
269 path.bv_val, strerror(errno), 0 );
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 {
301 static int r_enum_tree(enumCookie *ck, struct berval *path,
302 struct berval *pdn, struct berval *pndn)
305 int fd, rc = LDAP_SUCCESS;
307 fd = open( path->bv_val, O_RDONLY );
309 Debug( LDAP_DEBUG_ANY,
310 "=> ldif_enum_tree: failed to open %s: %s\n",
311 path->bv_val, strerror(errno), 0 );
312 return LDAP_NO_SUCH_OBJECT;
315 e = get_entry_for_fd(fd, pdn, pndn);
317 Debug( LDAP_DEBUG_ANY,
318 "=> ldif_enum_tree: failed to read entry for %s\n",
319 path->bv_val, 0, 0 );
323 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
324 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
325 /* Send right away? */
328 * if it's a referral, add it to the list of referrals. only do
329 * this for non-base searches, and don't check the filter
330 * explicitly here since it's only a candidate anyway.
332 if ( !get_manageDSAit( ck->op )
333 && ck->op->ors_scope != LDAP_SCOPE_BASE
334 && is_entry_referral( e ) )
336 BerVarray erefs = get_entry_referrals( ck->op, e );
337 ck->rs->sr_ref = referral_rewrite( erefs,
339 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
340 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
342 ck->rs->sr_entry = e;
343 rc = send_search_reference( ck->op, ck->rs );
344 ber_bvarray_free( ck->rs->sr_ref );
345 ber_bvarray_free( erefs );
346 ck->rs->sr_ref = NULL;
347 ck->rs->sr_entry = NULL;
349 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
351 ck->rs->sr_entry = e;
352 ck->rs->sr_attrs = ck->op->ors_attrs;
353 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
354 rc = send_search_entry(ck->op, ck->rs);
355 ck->rs->sr_entry = NULL;
361 /* Queueing up for tool mode */
362 if(ck->entries == NULL) {
363 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
364 ck->elen = ENTRY_BUFF_INCREMENT;
366 if(ck->eind >= ck->elen) { /* grow entries if necessary */
367 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
371 ck->entries[ck->eind++] = e;
378 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
380 bvlist *list = NULL, *ptr;
382 path->bv_len -= STRLENOF( LDIF );
383 path->bv_val[path->bv_len] = '\0';
385 dir_of_path = opendir(path->bv_val);
386 if(dir_of_path == NULL) { /* can't open directory */
387 if ( errno != ENOENT ) {
388 /* it shouldn't be treated as an error
389 * only if the directory doesn't exist */
391 Debug( LDAP_DEBUG_ANY,
392 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
393 path->bv_val, errno, 0 );
399 struct berval fname, itmp;
403 dir = readdir(dir_of_path);
404 if(dir == NULL) break; /* end of the directory */
405 fname.bv_len = strlen( dir->d_name );
406 if ( fname.bv_len <= STRLENOF( LDIF ))
408 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
410 fname.bv_val = dir->d_name;
412 bvl = ch_malloc( sizeof(bvlist) );
413 ber_dupbv( &bvl->bv, &fname );
414 BER_BVZERO( &bvl->num );
415 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
419 ptr = strchr( itmp.bv_val, IX_FSR );
421 itmp.bv_len = ptr - itmp.bv_val;
422 ber_dupbv( &bvl->num, &itmp );
423 /* FIXME: handle error? */
424 assert( itmp.bv_val[ 0 ] != '-' );
425 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
426 itmp.bv_val[0] = '\0';
427 bvl->off = itmp.bv_val - bvl->bv.bv_val;
431 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
432 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
433 if ( !cmp && bvl->num.bv_val )
434 cmp = bvl->inum - ptr->inum;
442 closedir(dir_of_path);
444 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
445 ck->op->ors_scope = LDAP_SCOPE_BASE;
446 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
447 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
449 while ( ( ptr = list ) ) {
454 if ( rc == LDAP_SUCCESS ) {
455 if ( ptr->num.bv_val )
456 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
458 fullpath( path, &ptr->bv, &fpath );
459 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
462 if ( ptr->num.bv_val )
463 free( ptr->num.bv_val );
464 free(ptr->bv.bv_val);
469 if ( fd ) entry_free( e );
478 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
480 struct berval pdn, pndn;
483 dnParent( &ck->op->o_req_dn, &pdn );
484 dnParent( &ck->op->o_req_ndn, &pndn );
485 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
486 rc = r_enum_tree(ck, &path, &pdn, &pndn);
487 ch_free( path.bv_val );
491 /* Get the parent path plus the LDIF suffix */
492 static void get_parent_path(struct berval * dnpath, struct berval *res) {
493 int dnpathlen = dnpath->bv_len;
496 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
497 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
500 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
501 strncpy(res->bv_val, dnpath->bv_val, i);
502 strcpy(res->bv_val+i, LDIF);
503 res->bv_val[i] = '\0';
506 static int apply_modify_to_entry(Entry * entry,
507 Modifications * modlist,
511 char textbuf[SLAP_TEXT_BUFLEN];
512 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
514 Modification *mods = NULL;
516 if (!acl_check_modlist(op, entry, modlist)) {
517 return LDAP_INSUFFICIENT_ACCESS;
520 for (; modlist != NULL; modlist = modlist->sml_next) {
521 mods = &modlist->sml_mod;
523 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
526 switch (mods->sm_op) {
528 rc = modify_add_values(entry, mods,
529 get_permissiveModify(op),
530 &rs->sr_text, textbuf,
534 case LDAP_MOD_DELETE:
535 rc = modify_delete_values(entry, mods,
536 get_permissiveModify(op),
537 &rs->sr_text, textbuf,
541 case LDAP_MOD_REPLACE:
542 rc = modify_replace_values(entry, mods,
543 get_permissiveModify(op),
544 &rs->sr_text, textbuf,
548 case LDAP_MOD_INCREMENT:
549 rc = modify_increment_values( entry,
550 mods, get_permissiveModify(op),
551 &rs->sr_text, textbuf,
557 case SLAP_MOD_SOFTADD:
558 mods->sm_op = LDAP_MOD_ADD;
559 rc = modify_add_values(entry, mods,
560 get_permissiveModify(op),
561 &rs->sr_text, textbuf,
563 mods->sm_op = SLAP_MOD_SOFTADD;
564 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
571 if(rc != LDAP_SUCCESS) break;
574 if(rc == LDAP_SUCCESS) {
576 entry->e_ocflags = 0;
578 /* check that the entry still obeys the schema */
579 rc = entry_schema_check( op, entry, NULL, 0,
580 &rs->sr_text, textbuf, sizeof( textbuf ) );
587 ldif_back_referrals( Operation *op, SlapReply *rs )
589 struct ldif_info *ni = NULL;
591 int rc = LDAP_SUCCESS;
594 if ( op->o_tag == LDAP_REQ_SEARCH ) {
595 /* let search take care of itself */
600 if ( get_manageDSAit( op ) ) {
601 /* let op take care of DSA management */
605 ni = (struct ldif_info *)op->o_bd->be_private;
606 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
607 entry = (Entry *)get_entry( op, &ni->li_base_path );
609 /* no object is found for them */
610 if ( entry == NULL ) {
611 struct berval odn = op->o_req_dn;
612 struct berval ondn = op->o_req_ndn;
614 struct berval pndn = op->o_req_ndn;
616 for ( ; entry == NULL; ) {
617 dnParent( &pndn, &pndn );
619 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
624 op->o_req_ndn = pndn;
626 entry = (Entry *)get_entry( op, &ni->li_base_path );
629 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
632 op->o_req_ndn = ondn;
635 rs->sr_matched = NULL;
636 if ( entry != NULL ) {
637 Debug( LDAP_DEBUG_TRACE,
638 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
639 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
641 if ( is_entry_referral( entry ) ) {
643 rs->sr_ref = get_entry_referrals( op, entry );
645 rs->sr_matched = ber_strdup_x(
646 entry->e_name.bv_val, op->o_tmpmemctx );
652 } else if ( default_referral != NULL ) {
654 rs->sr_ref = referral_rewrite( default_referral,
655 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
658 if ( rs->sr_ref != NULL ) {
660 rc = rs->sr_err = LDAP_REFERRAL;
661 send_ldap_result( op, rs );
662 ber_bvarray_free( rs->sr_ref );
665 } else if ( rc != LDAP_SUCCESS ) {
667 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
668 send_ldap_result( op, rs );
671 if ( rs->sr_matched ) {
672 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
673 rs->sr_matched = NULL;
679 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
681 if ( is_entry_referral( entry ) ) {
682 /* entry is a referral */
683 BerVarray refs = get_entry_referrals( op, entry );
684 rs->sr_ref = referral_rewrite(
685 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
687 Debug( LDAP_DEBUG_TRACE,
688 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
689 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
691 rs->sr_matched = entry->e_name.bv_val;
692 if ( rs->sr_ref != NULL ) {
693 rc = rs->sr_err = LDAP_REFERRAL;
694 send_ldap_result( op, rs );
695 ber_bvarray_free( rs->sr_ref );
699 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
703 rs->sr_matched = NULL;
704 ber_bvarray_free( refs );
713 ldif_back_bind( Operation *op, SlapReply *rs )
715 struct ldif_info *ni = NULL;
716 Attribute * a = NULL;
717 AttributeDescription *password = slap_schema.si_ad_userPassword;
719 Entry * entry = NULL;
721 ni = (struct ldif_info *) op->o_bd->be_private;
722 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
723 entry = (Entry *) get_entry(op, &ni->li_base_path);
725 /* no object is found for them */
727 if(be_isroot_pw(op)) {
728 rs->sr_err = return_val = LDAP_SUCCESS;
730 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
735 /* they don't have userpassword */
736 if((a = attr_find(entry->e_attrs, password)) == NULL) {
737 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
742 /* authentication actually failed */
743 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
744 &rs->sr_text) != 0) {
745 rs->sr_err = LDAP_INVALID_CREDENTIALS;
750 /* let the front-end send success */
755 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
757 send_ldap_result( op, rs );
763 static int ldif_back_search(Operation *op, SlapReply *rs)
765 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
766 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
770 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
771 rs->sr_err = enum_tree( &ck );
772 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
773 send_ldap_result(op, rs);
778 static int ldif_back_add(Operation *op, SlapReply *rs) {
779 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
780 Entry * e = op->ora_e;
781 struct berval dn = e->e_nname;
782 struct berval leaf_path = BER_BVNULL;
785 char textbuf[SLAP_TEXT_BUFLEN];
787 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
788 slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
790 rs->sr_err = entry_schema_check(op, e, NULL, 0,
791 &rs->sr_text, textbuf, sizeof( textbuf ) );
792 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
794 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
796 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
798 if(leaf_path.bv_val != NULL) {
799 struct berval base = BER_BVNULL;
800 /* build path to container and ldif of container */
801 get_parent_path(&leaf_path, &base);
803 statres = stat(base.bv_val, &stats); /* check if container exists */
804 if(statres == -1 && errno == ENOENT) { /* container missing */
805 base.bv_val[base.bv_len] = '.';
806 statres = stat(base.bv_val, &stats); /* check for leaf node */
807 base.bv_val[base.bv_len] = '\0';
808 if(statres == -1 && errno == ENOENT) {
809 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
810 rs->sr_text = "Parent does not exist";
812 else if(statres != -1) { /* create parent */
813 int mkdirres = mkdir(base.bv_val, 0750);
815 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
816 rs->sr_text = "Could not create parent folder";
817 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
818 base.bv_val, strerror( errno ), 0 );
822 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
823 }/* container was possibly created, move on to add the entry */
824 if(rs->sr_err == LDAP_SUCCESS) {
825 statres = stat(leaf_path.bv_val, &stats);
826 if(statres == -1 && errno == ENOENT) {
827 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
828 rs->sr_err = (int) spew_entry(e, &leaf_path);
829 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
831 else if ( statres == -1 ) {
832 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
833 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
834 leaf_path.bv_val, strerror( errno ), 0 );
836 else /* it already exists */
837 rs->sr_err = LDAP_ALREADY_EXISTS;
839 SLAP_FREE(base.bv_val);
840 SLAP_FREE(leaf_path.bv_val);
843 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
846 Debug( LDAP_DEBUG_TRACE,
847 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text, 0);
848 send_ldap_result(op, rs);
849 slap_graduate_commit_csn( op );
853 static int ldif_back_modify(Operation *op, SlapReply *rs) {
854 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
855 Modifications * modlst = op->orm_modlist;
856 struct berval path = BER_BVNULL;
857 Entry * entry = NULL;
860 slap_mods_opattrs( op, &op->orm_modlist, 1 );
862 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
863 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
865 entry = (Entry *) get_entry(op, &ni->li_base_path);
868 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
869 if(rs->sr_err == LDAP_SUCCESS) {
871 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
872 spew_res = spew_entry(entry, &path);
874 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
876 Debug( LDAP_DEBUG_ANY,
877 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
878 op->o_log_prefix, entry->e_name.bv_val, strerror( save_errno ) );
879 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
884 rs->sr_err = LDAP_NO_SUCH_OBJECT;
889 if(path.bv_val != NULL)
890 SLAP_FREE(path.bv_val);
892 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
893 send_ldap_result(op, rs);
894 slap_graduate_commit_csn( op );
898 static int ldif_back_delete(Operation *op, SlapReply *rs) {
899 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
900 struct berval path = BER_BVNULL;
903 if ( BER_BVISEMPTY( &op->o_csn )) {
905 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
908 csn.bv_len = sizeof( csnbuf );
909 slap_get_csn( op, &csn, 1 );
912 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
913 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
915 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
916 res = rmdir(path.bv_val);
917 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
918 if ( res && errno != ENOENT ) {
919 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
921 res = unlink(path.bv_val);
926 rs->sr_err = LDAP_NO_SUCH_OBJECT;
928 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
931 rs->sr_err = LDAP_SUCCESS;
933 SLAP_FREE(path.bv_val);
934 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
935 send_ldap_result(op, rs);
936 slap_graduate_commit_csn( op );
941 static int move_entry(Entry * entry, struct berval * ndn,
942 struct berval * newndn, struct berval * suffixdn,
943 struct berval * base_path) {
947 struct berval newpath;
949 dn2path(ndn, suffixdn, base_path, &path);
950 dn2path(newndn, suffixdn, base_path, &newpath);
952 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
953 /* some object doesn't exist */
954 res = LDAP_NO_SUCH_OBJECT;
956 else { /* do the modrdn */
957 exists_res = open(newpath.bv_val, O_RDONLY);
958 if(exists_res == -1 && errno == ENOENT) {
959 res = spew_entry(entry, &newpath);
961 /* if this fails we should log something bad */
962 res = unlink(path.bv_val);
967 res = LDAP_NO_SUCH_OBJECT;
969 res = LDAP_UNWILLING_TO_PERFORM;
970 unlink(newpath.bv_val); /* in case file was created */
973 else if(exists_res) {
974 int close_res = close(exists_res);
975 res = LDAP_ALREADY_EXISTS;
976 if(close_res == -1) {
977 /* log heinous error */
981 res = LDAP_UNWILLING_TO_PERFORM;
985 if(newpath.bv_val != NULL)
986 SLAP_FREE(newpath.bv_val);
987 if(path.bv_val != NULL)
988 SLAP_FREE(path.bv_val);
993 ldif_back_modrdn(Operation *op, SlapReply *rs)
995 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
996 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
998 Entry * entry = NULL;
1001 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1003 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
1004 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1005 entry = (Entry *) get_entry( op, &ni->li_base_path );
1007 /* build the mods to the entry */
1008 if ( entry != NULL ) {
1009 /* build new dn, and new ndn for the entry */
1010 if ( op->oq_modrdn.rs_newSup != NULL ) {
1012 p_dn = *op->oq_modrdn.rs_newSup;
1014 p_dn = slap_empty_bv;
1016 dnParent( &entry->e_name, &p_dn );
1017 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1018 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1019 ber_memfree_x( entry->e_name.bv_val, NULL );
1020 ber_memfree_x( entry->e_nname.bv_val, NULL );
1021 entry->e_name = new_dn;
1022 entry->e_nname = new_ndn;
1024 /* perform the modifications */
1025 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1026 if ( res == LDAP_SUCCESS ) {
1027 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1029 &op->o_bd->be_nsuffix[0],
1030 &ni->li_base_path );
1035 /* entry was null */
1036 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1039 if ( entry != NULL ) {
1040 entry_free( entry );
1043 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1044 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1045 send_ldap_result( op, rs );
1046 slap_graduate_commit_csn( op );
1050 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1052 int ldif_back_entry_get(
1056 AttributeDescription *at,
1060 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1061 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1063 assert( ndn != NULL );
1064 assert( !BER_BVISNULL( ndn ) );
1066 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
1067 op->o_req_dn = *ndn;
1068 op->o_req_ndn = *ndn;
1069 *ent = (Entry *) get_entry( op, &ni->li_base_path );
1070 op->o_req_dn = op_dn;
1071 op->o_req_ndn = op_ndn;
1072 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1074 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1079 return ( *ent == NULL ? 1 : 0 );
1082 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1083 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1084 ni->li_tool_current = 0;
1088 static int ldif_tool_entry_close(BackendDB * be) {
1089 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1091 SLAP_FREE(ni->li_tool_cookie.entries);
1096 ldif_tool_entry_first(BackendDB *be)
1098 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1099 ID id = 1; /* first entry in the array of entries shifted by one */
1101 ni->li_tool_current = 1;
1102 if(ni->li_tool_cookie.entries == NULL) {
1106 op.o_req_dn = *be->be_suffix;
1107 op.o_req_ndn = *be->be_nsuffix;
1108 op.ors_scope = LDAP_SCOPE_SUBTREE;
1109 ni->li_tool_cookie.op = &op;
1110 (void)enum_tree( &ni->li_tool_cookie );
1111 ni->li_tool_cookie.op = NULL;
1116 static ID ldif_tool_entry_next(BackendDB *be)
1118 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1119 ni->li_tool_current += 1;
1120 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1123 return ni->li_tool_current;
1126 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1127 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1130 if(id > ni->li_tool_cookie.eind || id < 1)
1133 e = ni->li_tool_cookie.entries[id - 1];
1134 ni->li_tool_cookie.entries[id - 1] = NULL;
1139 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1140 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1141 struct berval dn = e->e_nname;
1142 struct berval leaf_path = BER_BVNULL;
1145 int res = LDAP_SUCCESS;
1147 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1149 if(leaf_path.bv_val != NULL) {
1150 struct berval base = BER_BVNULL;
1151 /* build path to container, and path to ldif of container */
1152 get_parent_path(&leaf_path, &base);
1154 statres = stat(base.bv_val, &stats); /* check if container exists */
1155 if(statres == -1 && errno == ENOENT) { /* container missing */
1156 base.bv_val[base.bv_len] = '.';
1157 statres = stat(base.bv_val, &stats); /* check for leaf node */
1158 base.bv_val[base.bv_len] = '\0';
1159 if(statres == -1 && errno == ENOENT) {
1160 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1162 else if(statres != -1) { /* create parent */
1163 int mkdirres = mkdir(base.bv_val, 0750);
1164 if(mkdirres == -1) {
1165 res = LDAP_UNWILLING_TO_PERFORM;
1169 res = LDAP_UNWILLING_TO_PERFORM;
1170 }/* container was possibly created, move on to add the entry */
1171 if(res == LDAP_SUCCESS) {
1172 statres = stat(leaf_path.bv_val, &stats);
1173 if(statres == -1 && errno == ENOENT) {
1174 res = (int) spew_entry(e, &leaf_path);
1176 else /* it already exists */
1177 res = LDAP_ALREADY_EXISTS;
1179 SLAP_FREE(base.bv_val);
1180 SLAP_FREE(leaf_path.bv_val);
1183 if(res == LDAP_SUCCESS) {
1191 ldif_back_db_init( BackendDB *be )
1193 struct ldif_info *ni;
1195 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1196 be->be_private = ni;
1197 be->be_cf_ocs = ldifocs;
1198 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1203 ldif_back_db_destroy(
1207 struct ldif_info *ni = be->be_private;
1209 ch_free(ni->li_base_path.bv_val);
1210 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1211 free( be->be_private );
1220 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1221 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1222 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1229 ldif_back_initialize(
1233 static char *controls[] = {
1234 LDAP_CONTROL_MANAGEDSAIT,
1240 SLAP_BFLAG_INCREMENT |
1241 SLAP_BFLAG_REFERRALS;
1243 bi->bi_controls = controls;
1250 bi->bi_db_init = ldif_back_db_init;
1251 bi->bi_db_config = config_generic_wrapper;
1252 bi->bi_db_open = ldif_back_db_open;
1253 bi->bi_db_close = 0;
1254 bi->bi_db_destroy = ldif_back_db_destroy;
1256 bi->bi_op_bind = ldif_back_bind;
1257 bi->bi_op_unbind = 0;
1258 bi->bi_op_search = ldif_back_search;
1259 bi->bi_op_compare = 0;
1260 bi->bi_op_modify = ldif_back_modify;
1261 bi->bi_op_modrdn = ldif_back_modrdn;
1262 bi->bi_op_add = ldif_back_add;
1263 bi->bi_op_delete = ldif_back_delete;
1264 bi->bi_op_abandon = 0;
1266 bi->bi_extended = 0;
1268 bi->bi_chk_referrals = ldif_back_referrals;
1270 bi->bi_connection_init = 0;
1271 bi->bi_connection_destroy = 0;
1273 bi->bi_entry_get_rw = ldif_back_entry_get;
1275 #if 0 /* NOTE: uncomment to completely disable access control */
1276 bi->bi_access_allowed = slap_access_always_allowed;
1279 bi->bi_tool_entry_open = ldif_tool_entry_open;
1280 bi->bi_tool_entry_close = ldif_tool_entry_close;
1281 bi->bi_tool_entry_first = ldif_tool_entry_first;
1282 bi->bi_tool_entry_next = ldif_tool_entry_next;
1283 bi->bi_tool_entry_get = ldif_tool_entry_get;
1284 bi->bi_tool_entry_put = ldif_tool_entry_put;
1285 bi->bi_tool_entry_reindex = 0;
1286 bi->bi_tool_sync = 0;
1288 bi->bi_tool_dn2id_get = 0;
1289 bi->bi_tool_id2entry_get = 0;
1290 bi->bi_tool_entry_modify = 0;
1292 bi->bi_cf_ocs = ldifocs;
1294 rc = config_register_schema( ldifcfg, ldifocs );
1295 if ( rc ) return rc;