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>
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 perror("could not spew write");
164 static int spew_entry(Entry * e, struct berval * path) {
169 char * entry_as_string;
171 openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
174 rs = LDAP_NO_SUCH_OBJECT;
176 rs = LDAP_UNWILLING_TO_PERFORM;
182 /* Only save the RDN onto disk */
183 dnRdn( &e->e_name, &rdn );
184 if ( rdn.bv_len != e->e_name.bv_len ) {
185 e->e_name.bv_val[rdn.bv_len] = '\0';
186 tmp = e->e_name.bv_len;
187 e->e_name.bv_len = rdn.bv_len;
191 entry_as_string = entry2str(e, &entry_length);
193 /* Restore full DN */
194 if ( rdn.bv_len != e->e_name.bv_len ) {
195 e->e_name.bv_val[e->e_name.bv_len] = ',';
196 e->e_name.bv_len = rdn.bv_len;
199 if(entry_as_string == NULL) {
200 rs = LDAP_UNWILLING_TO_PERFORM;
204 spew_res = spew_file(openres, entry_as_string, entry_length);
207 rs = LDAP_UNWILLING_TO_PERFORM;
215 static Entry * get_entry_for_fd(int fd,
219 char * entry = (char *) slurp_file(fd);
220 Entry * ldentry = NULL;
222 /* error reading file */
227 ldentry = str2entry(entry);
230 rdn = ldentry->e_name;
231 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
232 ch_free( rdn.bv_val );
233 rdn = ldentry->e_nname;
234 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
235 ch_free( rdn.bv_val );
249 static Entry * get_entry(Operation *op, struct berval *base_path) {
250 struct berval path, pdn, pndn;
253 dnParent(&op->o_req_dn, &pdn);
254 dnParent(&op->o_req_ndn, &pndn);
255 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
256 fd = open(path.bv_val, O_RDONLY);
257 /* error opening file (mebbe should log error) */
259 perror("failed to open file");
262 if(path.bv_val != NULL)
263 SLAP_FREE(path.bv_val);
266 return get_entry_for_fd(fd, &pdn, &pndn);
272 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
274 res->bv_len = name->bv_len + base->bv_len + 1;
275 res->bv_val = ch_malloc( res->bv_len + 1 );
276 strcpy(res->bv_val, base->bv_val);
277 ptr = res->bv_val + base->bv_len;
278 *ptr++ = LDAP_DIRSEP[0];
279 strcpy(ptr, name->bv_val);
282 typedef struct bvlist {
291 static int r_enum_tree(enumCookie *ck, struct berval *path,
292 struct berval *pdn, struct berval *pndn)
295 int fd, rc = LDAP_SUCCESS;
297 fd = open( path->bv_val, O_RDONLY );
299 Debug( LDAP_DEBUG_TRACE,
300 "=> ldif_enum_tree: failed to open %s\n",
301 path->bv_val, 0, 0 );
302 return LDAP_NO_SUCH_OBJECT;
305 e = get_entry_for_fd(fd, pdn, pndn);
307 Debug( LDAP_DEBUG_ANY,
308 "=> ldif_enum_tree: failed to read entry for %s\n",
309 path->bv_val, 0, 0 );
313 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
314 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
315 /* Send right away? */
318 * if it's a referral, add it to the list of referrals. only do
319 * this for non-base searches, and don't check the filter
320 * explicitly here since it's only a candidate anyway.
322 if ( !get_manageDSAit( ck->op )
323 && ck->op->ors_scope != LDAP_SCOPE_BASE
324 && is_entry_referral( e ) )
326 BerVarray erefs = get_entry_referrals( ck->op, e );
327 ck->rs->sr_ref = referral_rewrite( erefs,
329 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
330 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
332 ck->rs->sr_entry = e;
333 rc = send_search_reference( ck->op, ck->rs );
334 ber_bvarray_free( ck->rs->sr_ref );
335 ber_bvarray_free( erefs );
336 ck->rs->sr_ref = NULL;
337 ck->rs->sr_entry = NULL;
339 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
341 ck->rs->sr_entry = e;
342 ck->rs->sr_attrs = ck->op->ors_attrs;
343 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
344 rc = send_search_entry(ck->op, ck->rs);
345 ck->rs->sr_entry = NULL;
351 /* Queueing up for tool mode */
352 if(ck->entries == NULL) {
353 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
354 ck->elen = ENTRY_BUFF_INCREMENT;
356 if(ck->eind >= ck->elen) { /* grow entries if necessary */
357 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
361 ck->entries[ck->eind++] = e;
368 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
370 bvlist *list = NULL, *ptr;
372 path->bv_len -= STRLENOF( LDIF );
373 path->bv_val[path->bv_len] = '\0';
375 dir_of_path = opendir(path->bv_val);
376 if(dir_of_path == NULL) { /* can't open directory */
377 if ( errno != ENOENT ) {
378 /* it shouldn't be treated as an error
379 * only if the directory doesn't exist */
381 Debug( LDAP_DEBUG_TRACE,
382 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
383 path->bv_val, errno, 0 );
389 struct berval fname, itmp;
393 dir = readdir(dir_of_path);
394 if(dir == NULL) break; /* end of the directory */
395 fname.bv_len = strlen( dir->d_name );
396 if ( fname.bv_len <= STRLENOF( LDIF ))
398 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
400 fname.bv_val = dir->d_name;
402 bvl = ch_malloc( sizeof(bvlist) );
403 ber_dupbv( &bvl->bv, &fname );
404 BER_BVZERO( &bvl->num );
405 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
409 ptr = strchr( itmp.bv_val, IX_FSR );
411 itmp.bv_len = ptr - itmp.bv_val;
412 ber_dupbv( &bvl->num, &itmp );
413 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
414 itmp.bv_val[0] = '\0';
415 bvl->off = itmp.bv_val - bvl->bv.bv_val;
419 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
420 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
421 if ( !cmp && bvl->num.bv_val )
422 cmp = bvl->inum - ptr->inum;
430 closedir(dir_of_path);
432 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
433 ck->op->ors_scope = LDAP_SCOPE_BASE;
434 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
435 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
437 while ( ( ptr = list ) ) {
442 if ( rc == LDAP_SUCCESS ) {
443 if ( ptr->num.bv_val )
444 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
446 fullpath( path, &ptr->bv, &fpath );
447 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
450 if ( ptr->num.bv_val )
451 free( ptr->num.bv_val );
452 free(ptr->bv.bv_val);
457 if ( fd ) entry_free( e );
466 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
468 struct berval pdn, pndn;
471 dnParent( &ck->op->o_req_dn, &pdn );
472 dnParent( &ck->op->o_req_ndn, &pndn );
473 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
474 rc = r_enum_tree(ck, &path, &pdn, &pndn);
475 ch_free( path.bv_val );
479 /* Get the parent path plus the LDIF suffix */
480 static void get_parent_path(struct berval * dnpath, struct berval *res) {
481 int dnpathlen = dnpath->bv_len;
484 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
485 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
488 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
489 strncpy(res->bv_val, dnpath->bv_val, i);
490 strcpy(res->bv_val+i, LDIF);
491 res->bv_val[i] = '\0';
494 static int apply_modify_to_entry(Entry * entry,
495 Modifications * modlist,
499 char textbuf[SLAP_TEXT_BUFLEN];
500 int rc = LDAP_UNWILLING_TO_PERFORM;
501 Modification *mods = NULL;
503 if (!acl_check_modlist(op, entry, modlist)) {
504 return LDAP_INSUFFICIENT_ACCESS;
507 for (; modlist != NULL; modlist = modlist->sml_next) {
508 mods = &modlist->sml_mod;
510 switch (mods->sm_op) {
512 rc = modify_add_values(entry, mods,
513 get_permissiveModify(op),
514 &rs->sr_text, textbuf,
518 case LDAP_MOD_DELETE:
519 rc = modify_delete_values(entry, mods,
520 get_permissiveModify(op),
521 &rs->sr_text, textbuf,
525 case LDAP_MOD_REPLACE:
526 rc = modify_replace_values(entry, mods,
527 get_permissiveModify(op),
528 &rs->sr_text, textbuf,
532 case LDAP_MOD_INCREMENT:
533 rc = modify_increment_values( entry,
534 mods, get_permissiveModify(op),
535 &rs->sr_text, textbuf,
541 case SLAP_MOD_SOFTADD:
542 mods->sm_op = LDAP_MOD_ADD;
543 rc = modify_add_values(entry, mods,
544 get_permissiveModify(op),
545 &rs->sr_text, textbuf,
547 mods->sm_op = SLAP_MOD_SOFTADD;
548 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
555 if(rc != LDAP_SUCCESS) break;
558 if(rc == LDAP_SUCCESS) {
559 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
560 entry->e_ocflags = 0;
562 /* check that the entry still obeys the schema */
563 rc = entry_schema_check(op, entry, NULL, 0,
564 &rs->sr_text, textbuf, sizeof( textbuf ) );
570 ldif_back_referrals( Operation *op, SlapReply *rs )
572 struct ldif_info *ni = NULL;
574 int rc = LDAP_SUCCESS;
577 if ( op->o_tag == LDAP_REQ_SEARCH ) {
578 /* let search take care of itself */
583 if ( get_manageDSAit( op ) ) {
584 /* let op take care of DSA management */
588 ni = (struct ldif_info *)op->o_bd->be_private;
589 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
590 entry = (Entry *)get_entry( op, &ni->li_base_path );
592 /* no object is found for them */
593 if ( entry == NULL ) {
594 struct berval odn = op->o_req_dn;
595 struct berval ondn = op->o_req_ndn;
597 struct berval pndn = op->o_req_ndn;
599 for ( ; entry == NULL; ) {
600 dnParent( &pndn, &pndn );
602 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
607 op->o_req_ndn = pndn;
609 entry = (Entry *)get_entry( op, &ni->li_base_path );
612 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
615 op->o_req_ndn = ondn;
618 rs->sr_matched = NULL;
619 if ( entry != NULL ) {
620 Debug( LDAP_DEBUG_TRACE,
621 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
622 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
624 if ( is_entry_referral( entry ) ) {
626 rs->sr_ref = get_entry_referrals( op, entry );
628 rs->sr_matched = ber_strdup_x(
629 entry->e_name.bv_val, op->o_tmpmemctx );
635 } else if ( default_referral != NULL ) {
637 rs->sr_ref = referral_rewrite( default_referral,
638 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
641 if ( rs->sr_ref != NULL ) {
643 rc = rs->sr_err = LDAP_REFERRAL;
644 send_ldap_result( op, rs );
645 ber_bvarray_free( rs->sr_ref );
648 } else if ( rc != LDAP_SUCCESS ) {
650 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
651 send_ldap_result( op, rs );
654 if ( rs->sr_matched ) {
655 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
656 rs->sr_matched = NULL;
662 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
664 if ( is_entry_referral( entry ) ) {
665 /* entry is a referral */
666 BerVarray refs = get_entry_referrals( op, entry );
667 rs->sr_ref = referral_rewrite(
668 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
670 Debug( LDAP_DEBUG_TRACE,
671 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
672 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
674 rs->sr_matched = entry->e_name.bv_val;
675 if ( rs->sr_ref != NULL ) {
676 rc = rs->sr_err = LDAP_REFERRAL;
677 send_ldap_result( op, rs );
678 ber_bvarray_free( rs->sr_ref );
682 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
686 rs->sr_matched = NULL;
687 ber_bvarray_free( refs );
696 ldif_back_bind( Operation *op, SlapReply *rs )
698 struct ldif_info *ni = NULL;
699 Attribute * a = NULL;
700 AttributeDescription *password = slap_schema.si_ad_userPassword;
702 Entry * entry = NULL;
704 ni = (struct ldif_info *) op->o_bd->be_private;
705 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
706 entry = (Entry *) get_entry(op, &ni->li_base_path);
708 /* no object is found for them */
710 if(be_isroot_pw(op)) {
711 rs->sr_err = return_val = LDAP_SUCCESS;
713 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
718 /* they don't have userpassword */
719 if((a = attr_find(entry->e_attrs, password)) == NULL) {
720 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
725 /* authentication actually failed */
726 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
727 &rs->sr_text) != 0) {
728 rs->sr_err = LDAP_INVALID_CREDENTIALS;
733 /* let the front-end send success */
738 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
740 send_ldap_result( op, rs );
746 static int ldif_back_search(Operation *op, SlapReply *rs)
748 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
749 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
753 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
754 rs->sr_err = enum_tree( &ck );
755 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
756 send_ldap_result(op, rs);
761 static int ldif_back_add(Operation *op, SlapReply *rs) {
762 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
763 Entry * e = op->ora_e;
764 struct berval dn = e->e_nname;
765 struct berval leaf_path = BER_BVNULL;
768 char textbuf[SLAP_TEXT_BUFLEN];
770 slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
772 rs->sr_err = entry_schema_check(op, e, NULL, 0,
773 &rs->sr_text, textbuf, sizeof( textbuf ) );
774 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
776 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
778 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
780 if(leaf_path.bv_val != NULL) {
781 struct berval base = BER_BVNULL;
782 /* build path to container and ldif of container */
783 get_parent_path(&leaf_path, &base);
785 statres = stat(base.bv_val, &stats); /* check if container exists */
786 if(statres == -1 && errno == ENOENT) { /* container missing */
787 base.bv_val[base.bv_len] = '.';
788 statres = stat(base.bv_val, &stats); /* check for leaf node */
789 base.bv_val[base.bv_len] = '\0';
790 if(statres == -1 && errno == ENOENT) {
791 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
793 else if(statres != -1) { /* create parent */
794 int mkdirres = mkdir(base.bv_val, 0750);
796 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
800 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
801 }/* container was possibly created, move on to add the entry */
802 if(rs->sr_err == LDAP_SUCCESS) {
803 statres = stat(leaf_path.bv_val, &stats);
804 if(statres == -1 && errno == ENOENT) {
805 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
806 rs->sr_err = (int) spew_entry(e, &leaf_path);
807 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
809 else /* it already exists */
810 rs->sr_err = LDAP_ALREADY_EXISTS;
812 SLAP_FREE(base.bv_val);
813 SLAP_FREE(leaf_path.bv_val);
816 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
819 send_ldap_result(op, rs);
820 if ( !SLAP_SHADOW( op->o_bd ))
821 slap_graduate_commit_csn( op );
825 static int ldif_back_modify(Operation *op, SlapReply *rs) {
826 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
827 Modifications * modlst = op->orm_modlist;
828 struct berval path = BER_BVNULL;
829 Entry * entry = NULL;
832 if ( !SLAP_SHADOW( op->o_bd ))
833 slap_mods_opattrs( op, &op->orm_modlist, 1 );
835 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
836 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
838 entry = (Entry *) get_entry(op, &ni->li_base_path);
841 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
842 if(rs->sr_err == LDAP_SUCCESS) {
843 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
844 spew_res = spew_entry(entry, &path);
845 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
847 perror("could not output entry");
848 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
853 rs->sr_err = LDAP_NO_SUCH_OBJECT;
858 if(path.bv_val != NULL)
859 SLAP_FREE(path.bv_val);
861 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
862 send_ldap_result(op, rs);
863 if ( !SLAP_SHADOW( op->o_bd ))
864 slap_graduate_commit_csn( op );
868 static int ldif_back_delete(Operation *op, SlapReply *rs) {
869 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
870 struct berval path = BER_BVNULL;
873 if ( !SLAP_SHADOW( op->o_bd )) {
875 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
878 csn.bv_len = sizeof( csnbuf );
879 slap_get_csn( op, &csn, 1 );
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);
906 if ( !SLAP_SHADOW( op->o_bd ))
907 slap_graduate_commit_csn( op );
912 static int move_entry(Entry * entry, struct berval * ndn,
913 struct berval * newndn, struct berval * rootdn,
914 struct berval * base_path) {
918 struct berval newpath;
920 dn2path(ndn, rootdn, base_path, &path);
921 dn2path(newndn, rootdn, base_path, &newpath);
923 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
924 /* some object doesn't exist */
925 res = LDAP_NO_SUCH_OBJECT;
927 else { /* do the modrdn */
928 exists_res = open(newpath.bv_val, O_RDONLY);
929 if(exists_res == -1 && errno == ENOENT) {
930 res = spew_entry(entry, &newpath);
932 /* if this fails we should log something bad */
933 res = unlink(path.bv_val);
938 res = LDAP_NO_SUCH_OBJECT;
940 res = LDAP_UNWILLING_TO_PERFORM;
941 unlink(newpath.bv_val); /* in case file was created */
944 else if(exists_res) {
945 int close_res = close(exists_res);
946 res = LDAP_ALREADY_EXISTS;
947 if(close_res == -1) {
948 /* log heinous error */
952 res = LDAP_UNWILLING_TO_PERFORM;
956 if(newpath.bv_val != NULL)
957 SLAP_FREE(newpath.bv_val);
958 if(path.bv_val != NULL)
959 SLAP_FREE(path.bv_val);
964 ldif_back_modrdn(Operation *op, SlapReply *rs)
966 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
967 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
969 Entry * entry = NULL;
970 LDAPRDN new_rdn = NULL;
971 LDAPRDN old_rdn = NULL;
972 Modifications * mods = NULL;
975 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
976 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
977 entry = (Entry *) get_entry( op, &ni->li_base_path );
979 /* build the mods to the entry */
980 if ( entry != NULL ) {
981 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn,
982 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP ) )
984 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
985 } else if ( op->oq_modrdn.rs_deleteoldrdn &&
986 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
987 LDAP_DN_FORMAT_LDAP ) )
989 rs->sr_err = LDAP_OTHER;
991 /* got both rdns successfully, ready to build mods */
992 if ( slap_modrdn2mods( op, rs, entry, old_rdn, new_rdn, &mods )
995 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
997 /* built mods successfully */
999 /* build new dn, and new ndn for the entry */
1000 if ( op->oq_modrdn.rs_newSup != NULL ) {
1002 p_dn = *op->oq_modrdn.rs_newSup;
1004 p_dn = slap_empty_bv;
1006 dnParent( &entry->e_name, &p_dn );
1007 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1008 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1009 ber_memfree_x( entry->e_name.bv_val, NULL );
1010 ber_memfree_x( entry->e_nname.bv_val, NULL );
1011 entry->e_name = new_dn;
1012 entry->e_nname = new_ndn;
1014 /* perform the modifications */
1015 res = apply_modify_to_entry( entry, mods, op, rs );
1016 slap_modrdn2mods_free( mods );
1017 if ( res == LDAP_SUCCESS ) {
1018 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1020 &op->o_bd->be_nsuffix[0],
1021 &ni->li_base_path );
1028 if ( new_rdn != NULL ) {
1029 ldap_rdnfree( new_rdn );
1032 if ( old_rdn != NULL ) {
1033 ldap_rdnfree( old_rdn );
1036 /* entry was null */
1037 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1040 if ( entry != NULL ) {
1041 entry_free( entry );
1044 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1045 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1046 send_ldap_result( op, rs );
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;
1062 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
1064 *ent = (Entry *) get_entry( op, &ni->li_base_path );
1066 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1068 return ( *ent == NULL ? 1 : 0 );
1071 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1072 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1073 ni->li_tool_current = 0;
1077 static int ldif_tool_entry_close(BackendDB * be) {
1078 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1080 SLAP_FREE(ni->li_tool_cookie.entries);
1085 ldif_tool_entry_first(BackendDB *be)
1087 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1088 ID id = 1; /* first entry in the array of entries shifted by one */
1090 ni->li_tool_current = 1;
1091 if(ni->li_tool_cookie.entries == NULL) {
1095 op.o_req_dn = *be->be_suffix;
1096 op.o_req_ndn = *be->be_nsuffix;
1097 op.ors_scope = LDAP_SCOPE_SUBTREE;
1098 ni->li_tool_cookie.op = &op;
1099 (void)enum_tree( &ni->li_tool_cookie );
1100 ni->li_tool_cookie.op = NULL;
1105 static ID ldif_tool_entry_next(BackendDB *be)
1107 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1108 ni->li_tool_current += 1;
1109 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1112 return ni->li_tool_current;
1115 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1116 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1119 if(id > ni->li_tool_cookie.eind || id < 1)
1122 e = ni->li_tool_cookie.entries[id - 1];
1123 ni->li_tool_cookie.entries[id - 1] = NULL;
1128 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1129 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1130 struct berval dn = e->e_nname;
1131 struct berval leaf_path = BER_BVNULL;
1134 int res = LDAP_SUCCESS;
1136 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1138 if(leaf_path.bv_val != NULL) {
1139 struct berval base = BER_BVNULL;
1140 /* build path to container, and path to ldif of container */
1141 get_parent_path(&leaf_path, &base);
1143 statres = stat(base.bv_val, &stats); /* check if container exists */
1144 if(statres == -1 && errno == ENOENT) { /* container missing */
1145 base.bv_val[base.bv_len] = '.';
1146 statres = stat(base.bv_val, &stats); /* check for leaf node */
1147 base.bv_val[base.bv_len] = '\0';
1148 if(statres == -1 && errno == ENOENT) {
1149 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1151 else if(statres != -1) { /* create parent */
1152 int mkdirres = mkdir(base.bv_val, 0750);
1153 if(mkdirres == -1) {
1154 res = LDAP_UNWILLING_TO_PERFORM;
1158 res = LDAP_UNWILLING_TO_PERFORM;
1159 }/* container was possibly created, move on to add the entry */
1160 if(res == LDAP_SUCCESS) {
1161 statres = stat(leaf_path.bv_val, &stats);
1162 if(statres == -1 && errno == ENOENT) {
1163 res = (int) spew_entry(e, &leaf_path);
1165 else /* it already exists */
1166 res = LDAP_ALREADY_EXISTS;
1168 SLAP_FREE(base.bv_val);
1169 SLAP_FREE(leaf_path.bv_val);
1172 if(res == LDAP_SUCCESS) {
1180 ldif_back_db_init( BackendDB *be )
1182 struct ldif_info *ni;
1184 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1185 be->be_private = ni;
1186 be->be_cf_ocs = ldifocs;
1187 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1192 ldif_back_db_destroy(
1196 struct ldif_info *ni = be->be_private;
1198 ch_free(ni->li_base_path.bv_val);
1199 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1200 free( be->be_private );
1209 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1210 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1211 fprintf(stderr, "missing base path for back-ldif\n");
1218 ldif_back_initialize(
1222 static char *controls[] = {
1223 LDAP_CONTROL_MANAGEDSAIT,
1229 SLAP_BFLAG_INCREMENT |
1230 SLAP_BFLAG_REFERRALS;
1232 bi->bi_controls = controls;
1239 bi->bi_db_init = ldif_back_db_init;
1240 bi->bi_db_config = config_generic_wrapper;
1241 bi->bi_db_open = ldif_back_db_open;
1242 bi->bi_db_close = 0;
1243 bi->bi_db_destroy = ldif_back_db_destroy;
1245 bi->bi_op_bind = ldif_back_bind;
1246 bi->bi_op_unbind = 0;
1247 bi->bi_op_search = ldif_back_search;
1248 bi->bi_op_compare = 0;
1249 bi->bi_op_modify = ldif_back_modify;
1250 bi->bi_op_modrdn = ldif_back_modrdn;
1251 bi->bi_op_add = ldif_back_add;
1252 bi->bi_op_delete = ldif_back_delete;
1253 bi->bi_op_abandon = 0;
1255 bi->bi_extended = 0;
1257 bi->bi_chk_referrals = ldif_back_referrals;
1259 bi->bi_connection_init = 0;
1260 bi->bi_connection_destroy = 0;
1262 bi->bi_entry_get_rw = ldif_back_entry_get;
1264 #if 0 /* NOTE: uncomment to completely disable access control */
1265 #ifdef SLAP_OVERLAY_ACCESS
1266 bi->bi_access_allowed = slap_access_always_allowed;
1267 #endif /* SLAP_OVERLAY_ACCESS */
1270 bi->bi_tool_entry_open = ldif_tool_entry_open;
1271 bi->bi_tool_entry_close = ldif_tool_entry_close;
1272 bi->bi_tool_entry_first = ldif_tool_entry_first;
1273 bi->bi_tool_entry_next = ldif_tool_entry_next;
1274 bi->bi_tool_entry_get = ldif_tool_entry_get;
1275 bi->bi_tool_entry_put = ldif_tool_entry_put;
1276 bi->bi_tool_entry_reindex = 0;
1277 bi->bi_tool_sync = 0;
1279 bi->bi_tool_dn2id_get = 0;
1280 bi->bi_tool_id2entry_get = 0;
1281 bi->bi_tool_entry_modify = 0;
1283 bi->bi_cf_ocs = ldifocs;
1285 rc = config_register_schema( ldifcfg, ldifocs );
1286 if ( rc ) return rc;