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 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;
341 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
343 ck->rs->sr_entry = e;
344 ck->rs->sr_attrs = ck->op->ors_attrs;
345 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
346 rc = send_search_entry(ck->op, ck->rs);
352 /* Queueing up for tool mode */
353 if(ck->entries == NULL) {
354 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
355 ck->elen = ENTRY_BUFF_INCREMENT;
357 if(ck->eind >= ck->elen) { /* grow entries if necessary */
358 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
362 ck->entries[ck->eind++] = e;
369 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
371 bvlist *list = NULL, *ptr;
373 path->bv_len -= STRLENOF( LDIF );
374 path->bv_val[path->bv_len] = '\0';
376 dir_of_path = opendir(path->bv_val);
377 if(dir_of_path == NULL) { /* can't open directory */
378 if ( errno != ENOENT ) {
379 /* it shouldn't be treated as an error
380 * only if the directory doesn't exist */
382 Debug( LDAP_DEBUG_TRACE,
383 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
384 path->bv_val, errno, 0 );
390 struct berval fname, itmp;
394 dir = readdir(dir_of_path);
395 if(dir == NULL) break; /* end of the directory */
396 fname.bv_len = strlen( dir->d_name );
397 if ( fname.bv_len <= STRLENOF( LDIF ))
399 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
401 fname.bv_val = dir->d_name;
403 bvl = ch_malloc( sizeof(bvlist) );
404 ber_dupbv( &bvl->bv, &fname );
405 BER_BVZERO( &bvl->num );
406 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
410 ptr = strchr( itmp.bv_val, IX_FSR );
412 itmp.bv_len = ptr - itmp.bv_val;
413 ber_dupbv( &bvl->num, &itmp );
414 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
415 itmp.bv_val[0] = '\0';
416 bvl->off = itmp.bv_val - bvl->bv.bv_val;
420 for (ptr = list, prev = (bvlist *)&list; ptr;
421 prev = ptr, ptr = ptr->next) {
422 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
423 if ( !cmp && bvl->num.bv_val )
424 cmp = bvl->inum - ptr->inum;
432 closedir(dir_of_path);
434 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
435 ck->op->ors_scope = LDAP_SCOPE_BASE;
436 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
437 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
439 while ( ( ptr = list ) ) {
444 if ( rc == LDAP_SUCCESS ) {
445 if ( ptr->num.bv_val )
446 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
448 fullpath( path, &ptr->bv, &fpath );
449 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
452 if ( ptr->num.bv_val )
453 free( ptr->num.bv_val );
454 free(ptr->bv.bv_val);
459 if ( fd ) entry_free( e );
468 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
470 struct berval pdn, pndn;
472 dnParent( &ck->op->o_req_dn, &pdn );
473 dnParent( &ck->op->o_req_ndn, &pndn );
474 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
475 return r_enum_tree(ck, &path, &pdn, &pndn);
478 /* Get the parent path plus the LDIF suffix */
479 static void get_parent_path(struct berval * dnpath, struct berval *res) {
480 int dnpathlen = dnpath->bv_len;
483 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
484 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
487 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
488 strncpy(res->bv_val, dnpath->bv_val, i);
489 strcpy(res->bv_val+i, LDIF);
490 res->bv_val[i] = '\0';
493 static int apply_modify_to_entry(Entry * entry,
494 Modifications * modlist,
498 char textbuf[SLAP_TEXT_BUFLEN];
499 int rc = LDAP_UNWILLING_TO_PERFORM;
500 Modification *mods = NULL;
502 if (!acl_check_modlist(op, entry, modlist)) {
503 return LDAP_INSUFFICIENT_ACCESS;
506 for (; modlist != NULL; modlist = modlist->sml_next) {
507 mods = &modlist->sml_mod;
509 switch (mods->sm_op) {
511 rc = modify_add_values(entry, mods,
512 get_permissiveModify(op),
513 &rs->sr_text, textbuf,
517 case LDAP_MOD_DELETE:
518 rc = modify_delete_values(entry, mods,
519 get_permissiveModify(op),
520 &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:
534 case SLAP_MOD_SOFTADD:
535 mods->sm_op = LDAP_MOD_ADD;
536 rc = modify_add_values(entry, mods,
537 get_permissiveModify(op),
538 &rs->sr_text, textbuf,
540 mods->sm_op = SLAP_MOD_SOFTADD;
541 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
548 if(rc != LDAP_SUCCESS) break;
551 if(rc == LDAP_SUCCESS) {
552 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
553 entry->e_ocflags = 0;
555 /* check that the entry still obeys the schema */
556 rc = entry_schema_check(op->o_bd, entry, NULL,
557 &rs->sr_text, textbuf, sizeof( textbuf ) );
563 ldif_back_referrals( Operation *op, SlapReply *rs )
565 struct ldif_info *ni = NULL;
567 int rc = LDAP_SUCCESS;
570 if ( op->o_tag == LDAP_REQ_SEARCH ) {
571 /* let search take care of itself */
576 if ( get_manageDSAit( op ) ) {
577 /* let op take care of DSA management */
581 ni = (struct ldif_info *)op->o_bd->be_private;
582 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
583 entry = (Entry *)get_entry( op, &ni->li_base_path );
585 /* no object is found for them */
586 if ( entry == NULL ) {
587 struct berval odn = op->o_req_dn;
588 struct berval ondn = op->o_req_ndn;
590 struct berval pndn = op->o_req_ndn;
592 for ( ; entry == NULL; ) {
593 dnParent( &pndn, &pndn );
595 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
600 op->o_req_ndn = pndn;
602 entry = (Entry *)get_entry( op, &ni->li_base_path );
605 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
608 op->o_req_ndn = ondn;
611 rs->sr_matched = NULL;
612 if ( entry != NULL ) {
613 Debug( LDAP_DEBUG_TRACE,
614 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
615 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
617 if ( is_entry_referral( entry ) ) {
619 rs->sr_ref = get_entry_referrals( op, entry );
621 rs->sr_matched = ber_strdup_x(
622 entry->e_name.bv_val, op->o_tmpmemctx );
628 } else if ( default_referral != NULL ) {
630 rs->sr_ref = referral_rewrite( default_referral,
631 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
634 if ( rs->sr_ref != NULL ) {
636 rc = rs->sr_err = LDAP_REFERRAL;
637 send_ldap_result( op, rs );
638 ber_bvarray_free( rs->sr_ref );
641 } else if ( rc != LDAP_SUCCESS ) {
643 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
644 send_ldap_result( op, rs );
647 if ( rs->sr_matched ) {
648 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
649 rs->sr_matched = NULL;
655 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
657 if ( is_entry_referral( entry ) ) {
658 /* entry is a referral */
659 BerVarray refs = get_entry_referrals( op, entry );
660 rs->sr_ref = referral_rewrite(
661 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
663 Debug( LDAP_DEBUG_TRACE,
664 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
665 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
667 rs->sr_matched = entry->e_name.bv_val;
668 if ( rs->sr_ref != NULL ) {
669 rc = rs->sr_err = LDAP_REFERRAL;
670 send_ldap_result( op, rs );
671 ber_bvarray_free( rs->sr_ref );
675 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
679 rs->sr_matched = NULL;
680 ber_bvarray_free( refs );
689 ldif_back_bind( Operation *op, SlapReply *rs )
691 struct ldif_info *ni = NULL;
692 Attribute * a = NULL;
693 AttributeDescription *password = slap_schema.si_ad_userPassword;
695 Entry * entry = NULL;
697 ni = (struct ldif_info *) op->o_bd->be_private;
698 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
699 entry = (Entry *) get_entry(op, &ni->li_base_path);
701 /* no object is found for them */
703 if(be_isroot_pw(op)) {
704 rs->sr_err = return_val = LDAP_SUCCESS;
706 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
711 /* they don't have userpassword */
712 if((a = attr_find(entry->e_attrs, password)) == NULL) {
713 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
718 /* authentication actually failed */
719 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
720 &rs->sr_text) != 0) {
721 rs->sr_err = LDAP_INVALID_CREDENTIALS;
726 /* let the front-end send success */
731 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
733 send_ldap_result( op, rs );
739 static int ldif_back_search(Operation *op, SlapReply *rs)
741 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
746 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
747 rs->sr_err = enum_tree( &ck );
748 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
749 send_ldap_result(op, rs);
754 static int ldif_back_add(Operation *op, SlapReply *rs) {
755 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
756 Entry * e = op->ora_e;
757 struct berval dn = e->e_nname;
758 struct berval leaf_path = BER_BVNULL;
761 char textbuf[SLAP_TEXT_BUFLEN];
763 rs->sr_err = entry_schema_check(op->o_bd, e,
764 NULL, &rs->sr_text, textbuf, sizeof( textbuf ) );
765 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
767 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
769 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
771 if(leaf_path.bv_val != NULL) {
772 struct berval base = BER_BVNULL;
773 /* build path to container and ldif of container */
774 get_parent_path(&leaf_path, &base);
776 statres = stat(base.bv_val, &stats); /* check if container exists */
777 if(statres == -1 && errno == ENOENT) { /* container missing */
778 base.bv_val[base.bv_len] = '.';
779 statres = stat(base.bv_val, &stats); /* check for leaf node */
780 base.bv_val[base.bv_len] = '\0';
781 if(statres == -1 && errno == ENOENT) {
782 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
784 else if(statres != -1) { /* create parent */
785 int mkdirres = mkdir(base.bv_val, 0750);
787 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
791 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
792 }/* container was possibly created, move on to add the entry */
793 if(rs->sr_err == LDAP_SUCCESS) {
794 statres = stat(leaf_path.bv_val, &stats);
795 if(statres == -1 && errno == ENOENT) {
796 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
797 rs->sr_err = (int) spew_entry(e, &leaf_path);
798 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
800 else /* it already exists */
801 rs->sr_err = LDAP_ALREADY_EXISTS;
803 SLAP_FREE(base.bv_val);
804 SLAP_FREE(leaf_path.bv_val);
807 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
810 send_ldap_result(op, rs);
814 static int ldif_back_modify(Operation *op, SlapReply *rs) {
815 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
816 Modifications * modlst = op->orm_modlist;
817 struct berval path = BER_BVNULL;
818 Entry * entry = NULL;
821 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
822 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
824 entry = (Entry *) get_entry(op, &ni->li_base_path);
827 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
828 if(rs->sr_err == LDAP_SUCCESS) {
829 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
830 spew_res = spew_entry(entry, &path);
831 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
833 perror("could not output entry");
834 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
839 rs->sr_err = LDAP_NO_SUCH_OBJECT;
844 if(path.bv_val != NULL)
845 SLAP_FREE(path.bv_val);
847 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
848 send_ldap_result(op, rs);
852 static int ldif_back_delete(Operation *op, SlapReply *rs) {
853 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
854 struct berval path = BER_BVNULL;
857 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
858 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
860 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
861 res = rmdir(path.bv_val);
862 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
863 if ( res && errno != ENOENT ) {
864 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
866 res = unlink(path.bv_val);
871 rs->sr_err = LDAP_NO_SUCH_OBJECT;
873 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
876 rs->sr_err = LDAP_SUCCESS;
878 SLAP_FREE(path.bv_val);
879 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
880 send_ldap_result(op, rs);
885 static int move_entry(Entry * entry, struct berval * ndn,
886 struct berval * newndn, struct berval * rootdn,
887 struct berval * base_path) {
891 struct berval newpath;
893 dn2path(ndn, rootdn, base_path, &path);
894 dn2path(newndn, rootdn, base_path, &newpath);
896 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
897 /* some object doesn't exist */
898 res = LDAP_NO_SUCH_OBJECT;
900 else { /* do the modrdn */
901 exists_res = open(newpath.bv_val, O_RDONLY);
902 if(exists_res == -1 && errno == ENOENT) {
903 res = spew_entry(entry, &newpath);
905 /* if this fails we should log something bad */
906 res = unlink(path.bv_val);
911 res = LDAP_NO_SUCH_OBJECT;
913 res = LDAP_UNWILLING_TO_PERFORM;
914 unlink(newpath.bv_val); /* in case file was created */
917 else if(exists_res) {
918 int close_res = close(exists_res);
919 res = LDAP_ALREADY_EXISTS;
920 if(close_res == -1) {
921 /* log heinous error */
925 res = LDAP_UNWILLING_TO_PERFORM;
929 if(newpath.bv_val != NULL)
930 SLAP_FREE(newpath.bv_val);
931 if(path.bv_val != NULL)
932 SLAP_FREE(path.bv_val);
936 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
937 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
938 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
939 struct berval p_dn, bv = BER_BVNULL;
940 Entry * entry = NULL;
941 LDAPRDN new_rdn = NULL;
942 LDAPRDN old_rdn = NULL;
943 Modifications * mods = NULL;
946 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
947 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
948 entry = (Entry *) get_entry(op, &ni->li_base_path);
950 /* build the mods to the entry */
952 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
953 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
954 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
956 else if(op->oq_modrdn.rs_deleteoldrdn &&
957 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
958 LDAP_DN_FORMAT_LDAP)) {
959 rs->sr_err = LDAP_OTHER;
961 else { /* got both rdns successfully, ready to build mods */
962 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
964 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
966 else { /* built mods successfully */
968 /* build new dn, and new ndn for the entry */
969 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
970 p_dn = *op->oq_modrdn.rs_newSup;
972 p_dn = slap_empty_bv;
973 dnParent(&entry->e_name, &p_dn);
974 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
975 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
976 ber_dupbv( &new_ndn, &bv );
977 entry->e_name = new_dn;
978 entry->e_nname = new_ndn;
980 /* perform the modifications */
981 res = apply_modify_to_entry(entry, mods, op, rs);
982 if(res == LDAP_SUCCESS) {
983 rs->sr_err = move_entry(entry, &op->o_req_ndn,
985 &op->o_bd->be_nsuffix[0],
993 else /* entry was null */
994 rs->sr_err = LDAP_NO_SUCH_OBJECT;
999 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1000 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1001 send_ldap_result(op, rs);
1005 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1006 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1010 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1012 e = (Entry *) get_entry(op, &ni->li_base_path);
1014 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1016 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1017 rs->sr_err = LDAP_COMPARE_FALSE;
1019 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1020 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1021 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1022 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1023 op->o_tmpmemctx ) == 0) {
1024 rs->sr_err = LDAP_COMPARE_TRUE;
1030 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1035 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1036 send_ldap_result(op, rs);
1040 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1041 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1042 ni->li_tool_current = 0;
1046 static int ldif_tool_entry_close(BackendDB * be) {
1047 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1049 SLAP_FREE(ni->li_tool_cookie.entries);
1054 ldif_tool_entry_first(BackendDB *be)
1056 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1057 ID id = 1; /* first entry in the array of entries shifted by one */
1059 ni->li_tool_current = 1;
1060 if(ni->li_tool_cookie.entries == NULL) {
1064 op.o_req_dn = *be->be_suffix;
1065 op.o_req_ndn = *be->be_nsuffix;
1066 op.ors_scope = LDAP_SCOPE_SUBTREE;
1067 ni->li_tool_cookie.op = &op;
1068 (void)enum_tree( &ni->li_tool_cookie );
1069 ni->li_tool_cookie.op = NULL;
1074 static ID ldif_tool_entry_next(BackendDB *be)
1076 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1077 ni->li_tool_current += 1;
1078 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1081 return ni->li_tool_current;
1084 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1085 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1088 if(id > ni->li_tool_cookie.eind || id < 1)
1091 e = ni->li_tool_cookie.entries[id - 1];
1092 ni->li_tool_cookie.entries[id - 1] = NULL;
1097 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1098 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1099 struct berval dn = e->e_nname;
1100 struct berval leaf_path = BER_BVNULL;
1103 int res = LDAP_SUCCESS;
1105 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1107 if(leaf_path.bv_val != NULL) {
1108 struct berval base = BER_BVNULL;
1109 /* build path to container, and path to ldif of container */
1110 get_parent_path(&leaf_path, &base);
1112 statres = stat(base.bv_val, &stats); /* check if container exists */
1113 if(statres == -1 && errno == ENOENT) { /* container missing */
1114 base.bv_val[base.bv_len] = '.';
1115 statres = stat(base.bv_val, &stats); /* check for leaf node */
1116 base.bv_val[base.bv_len] = '\0';
1117 if(statres == -1 && errno == ENOENT) {
1118 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1120 else if(statres != -1) { /* create parent */
1121 int mkdirres = mkdir(base.bv_val, 0750);
1122 if(mkdirres == -1) {
1123 res = LDAP_UNWILLING_TO_PERFORM;
1127 res = LDAP_UNWILLING_TO_PERFORM;
1128 }/* container was possibly created, move on to add the entry */
1129 if(res == LDAP_SUCCESS) {
1130 statres = stat(leaf_path.bv_val, &stats);
1131 if(statres == -1 && errno == ENOENT) {
1132 res = (int) spew_entry(e, &leaf_path);
1134 else /* it already exists */
1135 res = LDAP_ALREADY_EXISTS;
1137 SLAP_FREE(base.bv_val);
1138 SLAP_FREE(leaf_path.bv_val);
1141 if(res == LDAP_SUCCESS) {
1149 ldif_back_db_init( BackendDB *be )
1151 struct ldif_info *ni;
1153 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1154 be->be_private = ni;
1155 be->be_cf_ocs = ldifocs;
1156 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1161 ldif_back_db_destroy(
1165 struct ldif_info *ni = be->be_private;
1166 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1167 free( be->be_private );
1176 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1177 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1178 fprintf(stderr, "missing base path for back-ldif\n");
1185 ldif_back_initialize(
1189 static char *controls[] = {
1190 LDAP_CONTROL_MANAGEDSAIT,
1196 SLAP_BFLAG_REFERRALS;
1198 bi->bi_controls = controls;
1205 bi->bi_db_init = ldif_back_db_init;
1206 bi->bi_db_config = config_generic_wrapper;
1207 bi->bi_db_open = ldif_back_db_open;
1208 bi->bi_db_close = 0;
1209 bi->bi_db_destroy = ldif_back_db_destroy;
1211 bi->bi_op_bind = ldif_back_bind;
1212 bi->bi_op_unbind = 0;
1213 bi->bi_op_search = ldif_back_search;
1214 bi->bi_op_compare = ldif_back_compare;
1215 bi->bi_op_modify = ldif_back_modify;
1216 bi->bi_op_modrdn = ldif_back_modrdn;
1217 bi->bi_op_add = ldif_back_add;
1218 bi->bi_op_delete = ldif_back_delete;
1219 bi->bi_op_abandon = 0;
1221 bi->bi_extended = 0;
1223 bi->bi_chk_referrals = ldif_back_referrals;
1225 bi->bi_connection_init = 0;
1226 bi->bi_connection_destroy = 0;
1228 bi->bi_tool_entry_open = ldif_tool_entry_open;
1229 bi->bi_tool_entry_close = ldif_tool_entry_close;
1230 bi->bi_tool_entry_first = ldif_tool_entry_first;
1231 bi->bi_tool_entry_next = ldif_tool_entry_next;
1232 bi->bi_tool_entry_get = ldif_tool_entry_get;
1233 bi->bi_tool_entry_put = ldif_tool_entry_put;
1234 bi->bi_tool_entry_reindex = 0;
1235 bi->bi_tool_sync = 0;
1237 bi->bi_tool_dn2id_get = 0;
1238 bi->bi_tool_id2entry_get = 0;
1239 bi->bi_tool_entry_modify = 0;
1241 rc = config_register_schema( ldifcfg, ldifocs );
1242 if ( rc ) return rc;