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) {
150 int len = strlen(spew);
151 char * spewptr = spew;
153 while(written < len) {
154 writeres = write(fd, spewptr, len - written);
156 perror("could not spew write");
167 static int spew_entry(Entry * e, struct berval * path) {
172 char * entry_as_string;
174 openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
177 rs = LDAP_NO_SUCH_OBJECT;
179 rs = LDAP_UNWILLING_TO_PERFORM;
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);
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 perror("failed to open file");
265 if(path.bv_val != NULL)
266 SLAP_FREE(path.bv_val);
269 return get_entry_for_fd(fd, &pdn, &pndn);
275 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
277 res->bv_len = name->bv_len + base->bv_len + 1;
278 res->bv_val = ch_malloc( res->bv_len + 1 );
279 strcpy(res->bv_val, base->bv_val);
280 ptr = res->bv_val + base->bv_len;
281 *ptr++ = LDAP_DIRSEP[0];
282 strcpy(ptr, name->bv_val);
285 typedef struct bvlist {
294 static int r_enum_tree(enumCookie *ck, struct berval *path,
295 struct berval *pdn, struct berval *pndn)
298 int fd, rc = LDAP_SUCCESS;
300 fd = open( path->bv_val, O_RDONLY );
302 Debug( LDAP_DEBUG_TRACE,
303 "=> ldif_enum_tree: failed to open %s\n",
304 path->bv_val, 0, 0 );
305 return LDAP_NO_SUCH_OBJECT;
308 e = get_entry_for_fd(fd, pdn, pndn);
310 Debug( LDAP_DEBUG_ANY,
311 "=> ldif_enum_tree: failed to read entry for %s\n",
312 path->bv_val, 0, 0 );
316 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
317 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
318 /* Send right away? */
321 * if it's a referral, add it to the list of referrals. only do
322 * this for non-base searches, and don't check the filter
323 * explicitly here since it's only a candidate anyway.
325 if ( !get_manageDSAit( ck->op )
326 && ck->op->ors_scope != LDAP_SCOPE_BASE
327 && is_entry_referral( e ) )
329 BerVarray erefs = get_entry_referrals( ck->op, e );
330 ck->rs->sr_ref = referral_rewrite( erefs,
332 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
333 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
335 ck->rs->sr_entry = e;
336 rc = send_search_reference( ck->op, ck->rs );
337 ber_bvarray_free( ck->rs->sr_ref );
338 ber_bvarray_free( erefs );
339 ck->rs->sr_ref = NULL;
340 ck->rs->sr_entry = NULL;
342 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
344 ck->rs->sr_entry = e;
345 ck->rs->sr_attrs = ck->op->ors_attrs;
346 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
347 rc = send_search_entry(ck->op, ck->rs);
348 ck->rs->sr_entry = NULL;
354 /* Queueing up for tool mode */
355 if(ck->entries == NULL) {
356 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
357 ck->elen = ENTRY_BUFF_INCREMENT;
359 if(ck->eind >= ck->elen) { /* grow entries if necessary */
360 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
364 ck->entries[ck->eind++] = e;
371 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
373 bvlist *list = NULL, *ptr;
375 path->bv_len -= STRLENOF( LDIF );
376 path->bv_val[path->bv_len] = '\0';
378 dir_of_path = opendir(path->bv_val);
379 if(dir_of_path == NULL) { /* can't open directory */
380 if ( errno != ENOENT ) {
381 /* it shouldn't be treated as an error
382 * only if the directory doesn't exist */
384 Debug( LDAP_DEBUG_TRACE,
385 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
386 path->bv_val, errno, 0 );
392 struct berval fname, itmp;
396 dir = readdir(dir_of_path);
397 if(dir == NULL) break; /* end of the directory */
398 fname.bv_len = strlen( dir->d_name );
399 if ( fname.bv_len <= STRLENOF( LDIF ))
401 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
403 fname.bv_val = dir->d_name;
405 bvl = ch_malloc( sizeof(bvlist) );
406 ber_dupbv( &bvl->bv, &fname );
407 BER_BVZERO( &bvl->num );
408 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
412 ptr = strchr( itmp.bv_val, IX_FSR );
414 itmp.bv_len = ptr - itmp.bv_val;
415 ber_dupbv( &bvl->num, &itmp );
416 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
417 itmp.bv_val[0] = '\0';
418 bvl->off = itmp.bv_val - bvl->bv.bv_val;
422 for (ptr = list, prev = (bvlist *)&list; ptr;
423 prev = ptr, ptr = 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;
474 dnParent( &ck->op->o_req_dn, &pdn );
475 dnParent( &ck->op->o_req_ndn, &pndn );
476 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
477 return r_enum_tree(ck, &path, &pdn, &pndn);
480 /* Get the parent path plus the LDIF suffix */
481 static void get_parent_path(struct berval * dnpath, struct berval *res) {
482 int dnpathlen = dnpath->bv_len;
485 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
486 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
489 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
490 strncpy(res->bv_val, dnpath->bv_val, i);
491 strcpy(res->bv_val+i, LDIF);
492 res->bv_val[i] = '\0';
495 static int apply_modify_to_entry(Entry * entry,
496 Modifications * modlist,
500 char textbuf[SLAP_TEXT_BUFLEN];
501 int rc = LDAP_UNWILLING_TO_PERFORM;
502 Modification *mods = NULL;
504 if (!acl_check_modlist(op, entry, modlist)) {
505 return LDAP_INSUFFICIENT_ACCESS;
508 for (; modlist != NULL; modlist = modlist->sml_next) {
509 mods = &modlist->sml_mod;
511 switch (mods->sm_op) {
513 rc = modify_add_values(entry, mods,
514 get_permissiveModify(op),
515 &rs->sr_text, textbuf,
519 case LDAP_MOD_DELETE:
520 rc = modify_delete_values(entry, mods,
521 get_permissiveModify(op),
522 &rs->sr_text, textbuf,
527 case LDAP_MOD_REPLACE:
528 rc = modify_replace_values(entry, mods,
529 get_permissiveModify(op),
530 &rs->sr_text, textbuf,
534 case LDAP_MOD_INCREMENT:
536 case SLAP_MOD_SOFTADD:
537 mods->sm_op = LDAP_MOD_ADD;
538 rc = modify_add_values(entry, mods,
539 get_permissiveModify(op),
540 &rs->sr_text, textbuf,
542 mods->sm_op = SLAP_MOD_SOFTADD;
543 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
550 if(rc != LDAP_SUCCESS) break;
553 if(rc == LDAP_SUCCESS) {
554 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
555 entry->e_ocflags = 0;
557 /* check that the entry still obeys the schema */
558 rc = entry_schema_check(op->o_bd, entry, NULL, 0,
559 &rs->sr_text, textbuf, sizeof( textbuf ) );
565 ldif_back_referrals( Operation *op, SlapReply *rs )
567 struct ldif_info *ni = NULL;
569 int rc = LDAP_SUCCESS;
572 if ( op->o_tag == LDAP_REQ_SEARCH ) {
573 /* let search take care of itself */
578 if ( get_manageDSAit( op ) ) {
579 /* let op take care of DSA management */
583 ni = (struct ldif_info *)op->o_bd->be_private;
584 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
585 entry = (Entry *)get_entry( op, &ni->li_base_path );
587 /* no object is found for them */
588 if ( entry == NULL ) {
589 struct berval odn = op->o_req_dn;
590 struct berval ondn = op->o_req_ndn;
592 struct berval pndn = op->o_req_ndn;
594 for ( ; entry == NULL; ) {
595 dnParent( &pndn, &pndn );
597 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
602 op->o_req_ndn = pndn;
604 entry = (Entry *)get_entry( op, &ni->li_base_path );
607 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
610 op->o_req_ndn = ondn;
613 rs->sr_matched = NULL;
614 if ( entry != NULL ) {
615 Debug( LDAP_DEBUG_TRACE,
616 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
617 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
619 if ( is_entry_referral( entry ) ) {
621 rs->sr_ref = get_entry_referrals( op, entry );
623 rs->sr_matched = ber_strdup_x(
624 entry->e_name.bv_val, op->o_tmpmemctx );
630 } else if ( default_referral != NULL ) {
632 rs->sr_ref = referral_rewrite( default_referral,
633 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
636 if ( rs->sr_ref != NULL ) {
638 rc = rs->sr_err = LDAP_REFERRAL;
639 send_ldap_result( op, rs );
640 ber_bvarray_free( rs->sr_ref );
643 } else if ( rc != LDAP_SUCCESS ) {
645 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
646 send_ldap_result( op, rs );
649 if ( rs->sr_matched ) {
650 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
651 rs->sr_matched = NULL;
657 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
659 if ( is_entry_referral( entry ) ) {
660 /* entry is a referral */
661 BerVarray refs = get_entry_referrals( op, entry );
662 rs->sr_ref = referral_rewrite(
663 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
665 Debug( LDAP_DEBUG_TRACE,
666 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
667 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
669 rs->sr_matched = entry->e_name.bv_val;
670 if ( rs->sr_ref != NULL ) {
671 rc = rs->sr_err = LDAP_REFERRAL;
672 send_ldap_result( op, rs );
673 ber_bvarray_free( rs->sr_ref );
677 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
681 rs->sr_matched = NULL;
682 ber_bvarray_free( refs );
691 ldif_back_bind( Operation *op, SlapReply *rs )
693 struct ldif_info *ni = NULL;
694 Attribute * a = NULL;
695 AttributeDescription *password = slap_schema.si_ad_userPassword;
697 Entry * entry = NULL;
699 ni = (struct ldif_info *) op->o_bd->be_private;
700 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
701 entry = (Entry *) get_entry(op, &ni->li_base_path);
703 /* no object is found for them */
705 if(be_isroot_pw(op)) {
706 rs->sr_err = return_val = LDAP_SUCCESS;
708 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
713 /* they don't have userpassword */
714 if((a = attr_find(entry->e_attrs, password)) == NULL) {
715 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
720 /* authentication actually failed */
721 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
722 &rs->sr_text) != 0) {
723 rs->sr_err = LDAP_INVALID_CREDENTIALS;
728 /* let the front-end send success */
733 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
735 send_ldap_result( op, rs );
741 static int ldif_back_search(Operation *op, SlapReply *rs)
743 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
748 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
749 rs->sr_err = enum_tree( &ck );
750 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
751 send_ldap_result(op, rs);
756 static int ldif_back_add(Operation *op, SlapReply *rs) {
757 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
758 Entry * e = op->ora_e;
759 struct berval dn = e->e_nname;
760 struct berval leaf_path = BER_BVNULL;
763 char textbuf[SLAP_TEXT_BUFLEN];
765 rs->sr_err = entry_schema_check(op->o_bd, e, NULL, 0,
766 &rs->sr_text, textbuf, sizeof( textbuf ) );
767 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
769 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
771 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
773 if(leaf_path.bv_val != NULL) {
774 struct berval base = BER_BVNULL;
775 /* build path to container and ldif of container */
776 get_parent_path(&leaf_path, &base);
778 statres = stat(base.bv_val, &stats); /* check if container exists */
779 if(statres == -1 && errno == ENOENT) { /* container missing */
780 base.bv_val[base.bv_len] = '.';
781 statres = stat(base.bv_val, &stats); /* check for leaf node */
782 base.bv_val[base.bv_len] = '\0';
783 if(statres == -1 && errno == ENOENT) {
784 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
786 else if(statres != -1) { /* create parent */
787 int mkdirres = mkdir(base.bv_val, 0750);
789 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
793 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
794 }/* container was possibly created, move on to add the entry */
795 if(rs->sr_err == LDAP_SUCCESS) {
796 statres = stat(leaf_path.bv_val, &stats);
797 if(statres == -1 && errno == ENOENT) {
798 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
799 rs->sr_err = (int) spew_entry(e, &leaf_path);
800 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
802 else /* it already exists */
803 rs->sr_err = LDAP_ALREADY_EXISTS;
805 SLAP_FREE(base.bv_val);
806 SLAP_FREE(leaf_path.bv_val);
809 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
812 send_ldap_result(op, rs);
816 static int ldif_back_modify(Operation *op, SlapReply *rs) {
817 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
818 Modifications * modlst = op->orm_modlist;
819 struct berval path = BER_BVNULL;
820 Entry * entry = NULL;
823 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
824 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
826 entry = (Entry *) get_entry(op, &ni->li_base_path);
829 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
830 if(rs->sr_err == LDAP_SUCCESS) {
831 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
832 spew_res = spew_entry(entry, &path);
833 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
835 perror("could not output entry");
836 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
841 rs->sr_err = LDAP_NO_SUCH_OBJECT;
846 if(path.bv_val != NULL)
847 SLAP_FREE(path.bv_val);
849 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
850 send_ldap_result(op, rs);
854 static int ldif_back_delete(Operation *op, SlapReply *rs) {
855 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
856 struct berval path = BER_BVNULL;
859 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
860 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
862 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
863 res = rmdir(path.bv_val);
864 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
865 if ( res && errno != ENOENT ) {
866 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
868 res = unlink(path.bv_val);
873 rs->sr_err = LDAP_NO_SUCH_OBJECT;
875 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
878 rs->sr_err = LDAP_SUCCESS;
880 SLAP_FREE(path.bv_val);
881 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
882 send_ldap_result(op, rs);
887 static int move_entry(Entry * entry, struct berval * ndn,
888 struct berval * newndn, struct berval * rootdn,
889 struct berval * base_path) {
893 struct berval newpath;
895 dn2path(ndn, rootdn, base_path, &path);
896 dn2path(newndn, rootdn, base_path, &newpath);
898 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
899 /* some object doesn't exist */
900 res = LDAP_NO_SUCH_OBJECT;
902 else { /* do the modrdn */
903 exists_res = open(newpath.bv_val, O_RDONLY);
904 if(exists_res == -1 && errno == ENOENT) {
905 res = spew_entry(entry, &newpath);
907 /* if this fails we should log something bad */
908 res = unlink(path.bv_val);
913 res = LDAP_NO_SUCH_OBJECT;
915 res = LDAP_UNWILLING_TO_PERFORM;
916 unlink(newpath.bv_val); /* in case file was created */
919 else if(exists_res) {
920 int close_res = close(exists_res);
921 res = LDAP_ALREADY_EXISTS;
922 if(close_res == -1) {
923 /* log heinous error */
927 res = LDAP_UNWILLING_TO_PERFORM;
931 if(newpath.bv_val != NULL)
932 SLAP_FREE(newpath.bv_val);
933 if(path.bv_val != NULL)
934 SLAP_FREE(path.bv_val);
938 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
939 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
940 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
941 struct berval p_dn, bv = BER_BVNULL;
942 Entry * entry = NULL;
943 LDAPRDN new_rdn = NULL;
944 LDAPRDN old_rdn = NULL;
945 Modifications * mods = NULL;
948 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
949 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
950 entry = (Entry *) get_entry(op, &ni->li_base_path);
952 /* build the mods to the entry */
954 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
955 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
956 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
958 else if(op->oq_modrdn.rs_deleteoldrdn &&
959 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
960 LDAP_DN_FORMAT_LDAP)) {
961 rs->sr_err = LDAP_OTHER;
963 else { /* got both rdns successfully, ready to build mods */
964 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
966 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
968 else { /* built mods successfully */
970 /* build new dn, and new ndn for the entry */
971 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
972 p_dn = *op->oq_modrdn.rs_newSup;
974 p_dn = slap_empty_bv;
975 dnParent(&entry->e_name, &p_dn);
976 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
977 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
978 ber_dupbv( &new_ndn, &bv );
979 entry->e_name = new_dn;
980 entry->e_nname = new_ndn;
982 /* perform the modifications */
983 res = apply_modify_to_entry(entry, mods, op, rs);
984 if(res == LDAP_SUCCESS) {
985 rs->sr_err = move_entry(entry, &op->o_req_ndn,
987 &op->o_bd->be_nsuffix[0],
995 else /* entry was null */
996 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1001 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1002 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1003 send_ldap_result(op, rs);
1007 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1008 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1012 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1014 e = (Entry *) get_entry(op, &ni->li_base_path);
1016 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1018 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1019 rs->sr_err = LDAP_COMPARE_FALSE;
1021 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1022 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1023 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1024 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1025 op->o_tmpmemctx ) == 0) {
1026 rs->sr_err = LDAP_COMPARE_TRUE;
1032 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1037 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1038 send_ldap_result(op, rs);
1042 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1043 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1044 ni->li_tool_current = 0;
1048 static int ldif_tool_entry_close(BackendDB * be) {
1049 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1051 SLAP_FREE(ni->li_tool_cookie.entries);
1056 ldif_tool_entry_first(BackendDB *be)
1058 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1059 ID id = 1; /* first entry in the array of entries shifted by one */
1061 ni->li_tool_current = 1;
1062 if(ni->li_tool_cookie.entries == NULL) {
1066 op.o_req_dn = *be->be_suffix;
1067 op.o_req_ndn = *be->be_nsuffix;
1068 op.ors_scope = LDAP_SCOPE_SUBTREE;
1069 ni->li_tool_cookie.op = &op;
1070 (void)enum_tree( &ni->li_tool_cookie );
1071 ni->li_tool_cookie.op = NULL;
1076 static ID ldif_tool_entry_next(BackendDB *be)
1078 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1079 ni->li_tool_current += 1;
1080 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1083 return ni->li_tool_current;
1086 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1087 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1090 if(id > ni->li_tool_cookie.eind || id < 1)
1093 e = ni->li_tool_cookie.entries[id - 1];
1094 ni->li_tool_cookie.entries[id - 1] = NULL;
1099 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1100 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1101 struct berval dn = e->e_nname;
1102 struct berval leaf_path = BER_BVNULL;
1105 int res = LDAP_SUCCESS;
1107 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1109 if(leaf_path.bv_val != NULL) {
1110 struct berval base = BER_BVNULL;
1111 /* build path to container, and path to ldif of container */
1112 get_parent_path(&leaf_path, &base);
1114 statres = stat(base.bv_val, &stats); /* check if container exists */
1115 if(statres == -1 && errno == ENOENT) { /* container missing */
1116 base.bv_val[base.bv_len] = '.';
1117 statres = stat(base.bv_val, &stats); /* check for leaf node */
1118 base.bv_val[base.bv_len] = '\0';
1119 if(statres == -1 && errno == ENOENT) {
1120 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1122 else if(statres != -1) { /* create parent */
1123 int mkdirres = mkdir(base.bv_val, 0750);
1124 if(mkdirres == -1) {
1125 res = LDAP_UNWILLING_TO_PERFORM;
1129 res = LDAP_UNWILLING_TO_PERFORM;
1130 }/* container was possibly created, move on to add the entry */
1131 if(res == LDAP_SUCCESS) {
1132 statres = stat(leaf_path.bv_val, &stats);
1133 if(statres == -1 && errno == ENOENT) {
1134 res = (int) spew_entry(e, &leaf_path);
1136 else /* it already exists */
1137 res = LDAP_ALREADY_EXISTS;
1139 SLAP_FREE(base.bv_val);
1140 SLAP_FREE(leaf_path.bv_val);
1143 if(res == LDAP_SUCCESS) {
1151 ldif_back_db_init( BackendDB *be )
1153 struct ldif_info *ni;
1155 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1156 be->be_private = ni;
1157 be->be_cf_ocs = ldifocs;
1158 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1163 ldif_back_db_destroy(
1167 struct ldif_info *ni = be->be_private;
1168 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1169 free( be->be_private );
1178 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1179 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1180 fprintf(stderr, "missing base path for back-ldif\n");
1187 ldif_back_initialize(
1191 static char *controls[] = {
1192 LDAP_CONTROL_MANAGEDSAIT,
1198 SLAP_BFLAG_REFERRALS;
1200 bi->bi_controls = controls;
1207 bi->bi_db_init = ldif_back_db_init;
1208 bi->bi_db_config = config_generic_wrapper;
1209 bi->bi_db_open = ldif_back_db_open;
1210 bi->bi_db_close = 0;
1211 bi->bi_db_destroy = ldif_back_db_destroy;
1213 bi->bi_op_bind = ldif_back_bind;
1214 bi->bi_op_unbind = 0;
1215 bi->bi_op_search = ldif_back_search;
1216 bi->bi_op_compare = ldif_back_compare;
1217 bi->bi_op_modify = ldif_back_modify;
1218 bi->bi_op_modrdn = ldif_back_modrdn;
1219 bi->bi_op_add = ldif_back_add;
1220 bi->bi_op_delete = ldif_back_delete;
1221 bi->bi_op_abandon = 0;
1223 bi->bi_extended = 0;
1225 bi->bi_chk_referrals = ldif_back_referrals;
1227 bi->bi_connection_init = 0;
1228 bi->bi_connection_destroy = 0;
1230 bi->bi_tool_entry_open = ldif_tool_entry_open;
1231 bi->bi_tool_entry_close = ldif_tool_entry_close;
1232 bi->bi_tool_entry_first = ldif_tool_entry_first;
1233 bi->bi_tool_entry_next = ldif_tool_entry_next;
1234 bi->bi_tool_entry_get = ldif_tool_entry_get;
1235 bi->bi_tool_entry_put = ldif_tool_entry_put;
1236 bi->bi_tool_entry_reindex = 0;
1237 bi->bi_tool_sync = 0;
1239 bi->bi_tool_dn2id_get = 0;
1240 bi->bi_tool_id2entry_get = 0;
1241 bi->bi_tool_entry_modify = 0;
1243 rc = config_register_schema( ldifcfg, ldifocs );
1244 if ( rc ) return rc;