1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was originally developed by Eric Stokes for inclusion
18 * in OpenLDAP Software.
23 #include <ac/string.h>
24 #include <sys/types.h>
26 #include <ac/dirent.h>
29 #include <ac/unistd.h>
34 typedef struct enumCookie {
43 struct berval li_base_path;
44 enumCookie li_tool_cookie;
46 ldap_pvt_thread_rdwr_t li_rdwr;
50 #define mkdir(a,b) mkdir(a)
62 #define ENTRY_BUFF_INCREMENT 500
64 static ConfigTable ldifcfg[] = {
65 { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
66 (void *)offsetof(struct ldif_info, li_base_path),
67 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
68 "DESC 'Directory for database content' "
69 "EQUALITY caseIgnoreMatch "
70 "SYNTAX OMsDirectoryString )", NULL, NULL },
71 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
72 NULL, NULL, NULL, NULL }
75 static ConfigOCs ldifocs[] = {
77 "NAME 'olcLdifConfig' "
78 "DESC 'LDIF backend configuration' "
79 "SUP olcDatabaseConfig "
80 "MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
85 dn2path(struct berval * dn, struct berval * suffixdn, struct berval * base_path,
88 char *ptr, *sep, *end;
92 assert( !BER_BVISNULL( dn ) );
93 assert( suffixdn != NULL );
94 assert( !BER_BVISNULL( suffixdn ) );
95 assert( dnIsSuffix( dn, suffixdn ) );
97 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
98 res->bv_val = ch_malloc( res->bv_len + 1 );
99 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
100 *ptr++ = LDAP_DIRSEP[0];
101 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
102 end = dn->bv_val + dn->bv_len - suffixdn->bv_len - 1;
103 while ( end > dn->bv_val ) {
104 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
105 *ptr++ = LDAP_DIRSEP[0];
106 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
112 while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
114 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
115 bv.bv_len -= ( ptr - bv.bv_val );
117 ptr = ber_bvchr( &bv, IX_DNR );
121 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
122 bv.bv_len -= ( ptr - bv.bv_val );
128 static char * slurp_file(int fd) {
129 int read_chars_total = 0;
137 entry_size = st.st_size;
138 entry = ch_malloc( entry_size+1 );
142 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
143 if(read_chars == -1) {
147 if(read_chars == 0) {
148 entry[read_chars_total] = '\0';
152 read_chars_total += read_chars;
153 entry_pos += read_chars;
159 static int spew_file(int fd, char * spew, int len) {
163 writeres = write(fd, spew, len);
165 Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
166 STRERROR( errno ), 0, 0 );
177 static int spew_entry(Entry * e, struct berval * path) {
182 char * entry_as_string;
184 openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD | S_IWRITE);
187 rs = LDAP_NO_SUCH_OBJECT;
189 rs = LDAP_UNWILLING_TO_PERFORM;
190 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
191 path->bv_val, STRERROR( errno ), 0 );
197 /* Only save the RDN onto disk */
198 dnRdn( &e->e_name, &rdn );
199 if ( rdn.bv_len != e->e_name.bv_len ) {
200 e->e_name.bv_val[rdn.bv_len] = '\0';
201 tmp = e->e_name.bv_len;
202 e->e_name.bv_len = rdn.bv_len;
206 entry_as_string = entry2str(e, &entry_length);
208 /* Restore full DN */
209 if ( rdn.bv_len != e->e_name.bv_len ) {
210 e->e_name.bv_val[e->e_name.bv_len] = ',';
211 e->e_name.bv_len = rdn.bv_len;
214 if(entry_as_string == NULL) {
215 rs = LDAP_UNWILLING_TO_PERFORM;
219 spew_res = spew_file(openres, entry_as_string, entry_length);
222 rs = LDAP_UNWILLING_TO_PERFORM;
230 static Entry * get_entry_for_fd(int fd,
234 char * entry = (char *) slurp_file(fd);
235 Entry * ldentry = NULL;
237 /* error reading file */
242 ldentry = str2entry(entry);
245 rdn = ldentry->e_name;
246 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
247 ch_free( rdn.bv_val );
248 rdn = ldentry->e_nname;
249 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
250 ch_free( rdn.bv_val );
264 static Entry * get_entry(Operation *op, struct berval *base_path) {
265 struct berval path, pdn, pndn;
268 dnParent(&op->o_req_dn, &pdn);
269 dnParent(&op->o_req_ndn, &pndn);
270 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
271 fd = open(path.bv_val, O_RDONLY);
272 /* error opening file (mebbe should log error) */
273 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
274 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
275 path.bv_val, STRERROR(errno), 0 );
278 if(path.bv_val != NULL)
279 SLAP_FREE(path.bv_val);
282 return get_entry_for_fd(fd, &pdn, &pndn);
288 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
290 res->bv_len = name->bv_len + base->bv_len + 1;
291 res->bv_val = ch_malloc( res->bv_len + 1 );
292 strcpy(res->bv_val, base->bv_val);
293 ptr = res->bv_val + base->bv_len;
294 *ptr++ = LDAP_DIRSEP[0];
295 strcpy(ptr, name->bv_val);
298 typedef struct bvlist {
307 static int r_enum_tree(enumCookie *ck, struct berval *path,
308 struct berval *pdn, struct berval *pndn)
311 int fd, rc = LDAP_SUCCESS;
313 fd = open( path->bv_val, O_RDONLY );
315 Debug( LDAP_DEBUG_TRACE,
316 "=> ldif_enum_tree: failed to open %s: %s\n",
317 path->bv_val, STRERROR(errno), 0 );
318 return LDAP_NO_SUCH_OBJECT;
321 e = get_entry_for_fd(fd, pdn, pndn);
323 Debug( LDAP_DEBUG_ANY,
324 "=> ldif_enum_tree: failed to read entry for %s\n",
325 path->bv_val, 0, 0 );
329 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
330 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
331 /* Send right away? */
334 * if it's a referral, add it to the list of referrals. only do
335 * this for non-base searches, and don't check the filter
336 * explicitly here since it's only a candidate anyway.
338 if ( !get_manageDSAit( ck->op )
339 && ck->op->ors_scope != LDAP_SCOPE_BASE
340 && is_entry_referral( e ) )
342 BerVarray erefs = get_entry_referrals( ck->op, e );
343 ck->rs->sr_ref = referral_rewrite( erefs,
345 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
346 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
348 ck->rs->sr_entry = e;
349 rc = send_search_reference( ck->op, ck->rs );
350 ber_bvarray_free( ck->rs->sr_ref );
351 ber_bvarray_free( erefs );
352 ck->rs->sr_ref = NULL;
353 ck->rs->sr_entry = NULL;
355 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
357 ck->rs->sr_entry = e;
358 ck->rs->sr_attrs = ck->op->ors_attrs;
359 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
360 rc = send_search_entry(ck->op, ck->rs);
361 ck->rs->sr_entry = NULL;
367 /* Queueing up for tool mode */
368 if(ck->entries == NULL) {
369 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
370 ck->elen = ENTRY_BUFF_INCREMENT;
372 if(ck->eind >= ck->elen) { /* grow entries if necessary */
373 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
377 ck->entries[ck->eind++] = e;
384 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
386 bvlist *list = NULL, *ptr;
388 path->bv_len -= STRLENOF( LDIF );
389 path->bv_val[path->bv_len] = '\0';
391 dir_of_path = opendir(path->bv_val);
392 if(dir_of_path == NULL) { /* can't open directory */
393 if ( errno != ENOENT ) {
394 /* it shouldn't be treated as an error
395 * only if the directory doesn't exist */
397 Debug( LDAP_DEBUG_ANY,
398 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
399 path->bv_val, errno, 0 );
405 struct berval fname, itmp;
409 dir = readdir(dir_of_path);
410 if(dir == NULL) break; /* end of the directory */
411 fname.bv_len = strlen( dir->d_name );
412 if ( fname.bv_len <= STRLENOF( LDIF ))
414 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
416 fname.bv_val = dir->d_name;
418 bvl = ch_malloc( sizeof(bvlist) );
419 ber_dupbv( &bvl->bv, &fname );
420 BER_BVZERO( &bvl->num );
421 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
425 itmp.bv_len = bvl->bv.bv_len
426 - ( itmp.bv_val - bvl->bv.bv_val );
427 ptr = ber_bvchr( &itmp, IX_FSR );
429 itmp.bv_len = ptr - itmp.bv_val;
430 ber_dupbv( &bvl->num, &itmp );
431 bvl->inum = strtol( itmp.bv_val, NULL, 0 );
432 itmp.bv_val[0] = '\0';
433 bvl->off = itmp.bv_val - bvl->bv.bv_val;
437 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
438 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
439 if ( !cmp && bvl->num.bv_val )
440 cmp = bvl->inum - ptr->inum;
448 closedir(dir_of_path);
450 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
451 ck->op->ors_scope = LDAP_SCOPE_BASE;
452 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
453 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
455 while ( ( ptr = list ) ) {
460 if ( rc == LDAP_SUCCESS ) {
461 if ( ptr->num.bv_val )
462 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
464 fullpath( path, &ptr->bv, &fpath );
465 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
468 if ( ptr->num.bv_val )
469 free( ptr->num.bv_val );
470 free(ptr->bv.bv_val);
475 if ( fd ) entry_free( e );
484 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
486 struct berval pdn, pndn;
489 dnParent( &ck->op->o_req_dn, &pdn );
490 dnParent( &ck->op->o_req_ndn, &pndn );
491 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
492 rc = r_enum_tree(ck, &path, &pdn, &pndn);
493 ch_free( path.bv_val );
497 /* Get the parent path plus the LDIF suffix */
498 static void get_parent_path(struct berval * dnpath, struct berval *res) {
499 int dnpathlen = dnpath->bv_len;
502 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
503 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
506 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
507 strncpy(res->bv_val, dnpath->bv_val, i);
508 strcpy(res->bv_val+i, LDIF);
509 res->bv_val[i] = '\0';
512 static int apply_modify_to_entry(Entry * entry,
513 Modifications * modlist,
517 char textbuf[SLAP_TEXT_BUFLEN];
518 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
520 Modification *mods = NULL;
522 if (!acl_check_modlist(op, entry, modlist)) {
523 return LDAP_INSUFFICIENT_ACCESS;
526 for (; modlist != NULL; modlist = modlist->sml_next) {
527 mods = &modlist->sml_mod;
529 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
532 switch (mods->sm_op) {
534 rc = modify_add_values(entry, mods,
535 get_permissiveModify(op),
536 &rs->sr_text, textbuf,
540 case LDAP_MOD_DELETE:
541 rc = modify_delete_values(entry, mods,
542 get_permissiveModify(op),
543 &rs->sr_text, textbuf,
547 case LDAP_MOD_REPLACE:
548 rc = modify_replace_values(entry, mods,
549 get_permissiveModify(op),
550 &rs->sr_text, textbuf,
554 case LDAP_MOD_INCREMENT:
555 rc = modify_increment_values( entry,
556 mods, get_permissiveModify(op),
557 &rs->sr_text, textbuf,
563 case SLAP_MOD_SOFTADD:
564 mods->sm_op = LDAP_MOD_ADD;
565 rc = modify_add_values(entry, mods,
566 get_permissiveModify(op),
567 &rs->sr_text, textbuf,
569 mods->sm_op = SLAP_MOD_SOFTADD;
570 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
577 if(rc != LDAP_SUCCESS) break;
580 if(rc == LDAP_SUCCESS) {
582 entry->e_ocflags = 0;
584 /* check that the entry still obeys the schema */
585 rc = entry_schema_check( op, entry, NULL, 0, 0,
586 &rs->sr_text, textbuf, sizeof( textbuf ) );
593 ldif_back_referrals( Operation *op, SlapReply *rs )
595 struct ldif_info *ni = NULL;
597 int rc = LDAP_SUCCESS;
600 if ( op->o_tag == LDAP_REQ_SEARCH ) {
601 /* let search take care of itself */
606 if ( get_manageDSAit( op ) ) {
607 /* let op take care of DSA management */
611 ni = (struct ldif_info *)op->o_bd->be_private;
612 ldap_pvt_thread_rdwr_rlock( &ni->li_rdwr );
613 entry = (Entry *)get_entry( op, &ni->li_base_path );
615 /* no object is found for them */
616 if ( entry == NULL ) {
617 struct berval odn = op->o_req_dn;
618 struct berval ondn = op->o_req_ndn;
620 struct berval pndn = op->o_req_ndn;
622 for ( ; entry == NULL; ) {
623 dnParent( &pndn, &pndn );
625 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
630 op->o_req_ndn = pndn;
632 entry = (Entry *)get_entry( op, &ni->li_base_path );
635 ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr );
638 op->o_req_ndn = ondn;
641 rs->sr_matched = NULL;
642 if ( entry != NULL ) {
643 Debug( LDAP_DEBUG_TRACE,
644 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
645 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
647 if ( is_entry_referral( entry ) ) {
649 rs->sr_ref = get_entry_referrals( op, entry );
651 rs->sr_matched = ber_strdup_x(
652 entry->e_name.bv_val, op->o_tmpmemctx );
658 } else if ( default_referral != NULL ) {
660 rs->sr_ref = referral_rewrite( default_referral,
661 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
664 if ( rs->sr_ref != NULL ) {
666 rc = rs->sr_err = LDAP_REFERRAL;
667 send_ldap_result( op, rs );
668 ber_bvarray_free( rs->sr_ref );
671 } else if ( rc != LDAP_SUCCESS ) {
673 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
674 send_ldap_result( op, rs );
677 if ( rs->sr_matched ) {
678 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
679 rs->sr_matched = NULL;
685 ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr );
687 if ( is_entry_referral( entry ) ) {
688 /* entry is a referral */
689 BerVarray refs = get_entry_referrals( op, entry );
690 rs->sr_ref = referral_rewrite(
691 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
693 Debug( LDAP_DEBUG_TRACE,
694 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
695 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
697 rs->sr_matched = entry->e_name.bv_val;
698 if ( rs->sr_ref != NULL ) {
699 rc = rs->sr_err = LDAP_REFERRAL;
700 send_ldap_result( op, rs );
701 ber_bvarray_free( rs->sr_ref );
705 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
709 rs->sr_matched = NULL;
710 ber_bvarray_free( refs );
719 ldif_back_bind( Operation *op, SlapReply *rs )
721 struct ldif_info *ni = NULL;
722 Attribute * a = NULL;
723 AttributeDescription *password = slap_schema.si_ad_userPassword;
725 Entry * entry = NULL;
727 ni = (struct ldif_info *) op->o_bd->be_private;
728 ldap_pvt_thread_rdwr_rlock(&ni->li_rdwr);
729 entry = (Entry *) get_entry(op, &ni->li_base_path);
731 /* no object is found for them */
733 if(be_isroot_pw(op)) {
734 rs->sr_err = return_val = LDAP_SUCCESS;
736 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
741 /* they don't have userpassword */
742 if((a = attr_find(entry->e_attrs, password)) == NULL) {
743 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
748 /* authentication actually failed */
749 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
750 &rs->sr_text) != 0) {
751 rs->sr_err = LDAP_INVALID_CREDENTIALS;
756 /* let the front-end send success */
761 ldap_pvt_thread_rdwr_runlock(&ni->li_rdwr);
763 send_ldap_result( op, rs );
769 static int ldif_back_search(Operation *op, SlapReply *rs)
771 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
772 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
776 ldap_pvt_thread_rdwr_rlock(&ni->li_rdwr);
777 rs->sr_err = enum_tree( &ck );
778 ldap_pvt_thread_rdwr_runlock(&ni->li_rdwr);
779 send_ldap_result(op, rs);
784 static int ldif_back_add(Operation *op, SlapReply *rs) {
785 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
786 Entry * e = op->ora_e;
787 struct berval dn = e->e_nname;
788 struct berval leaf_path = BER_BVNULL;
791 char textbuf[SLAP_TEXT_BUFLEN];
793 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
795 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
796 &rs->sr_text, textbuf, sizeof( textbuf ) );
797 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
799 rs->sr_err = slap_add_opattrs( op,
800 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
801 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
803 ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr);
805 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
807 if(leaf_path.bv_val != NULL) {
808 struct berval base = BER_BVNULL;
809 /* build path to container and ldif of container */
810 get_parent_path(&leaf_path, &base);
812 statres = stat(base.bv_val, &stats); /* check if container exists */
813 if(statres == -1 && errno == ENOENT) { /* container missing */
814 base.bv_val[base.bv_len] = '.';
815 statres = stat(base.bv_val, &stats); /* check for leaf node */
816 base.bv_val[base.bv_len] = '\0';
817 if(statres == -1 && errno == ENOENT) {
818 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
819 rs->sr_text = "Parent does not exist";
821 else if(statres != -1) { /* create parent */
822 int mkdirres = mkdir(base.bv_val, 0750);
824 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
825 rs->sr_text = "Could not create parent folder";
826 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
827 base.bv_val, STRERROR( errno ), 0 );
831 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
832 }/* container was possibly created, move on to add the entry */
833 if(rs->sr_err == LDAP_SUCCESS) {
834 statres = stat(leaf_path.bv_val, &stats);
835 if(statres == -1 && errno == ENOENT) {
836 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
837 rs->sr_err = (int) spew_entry(e, &leaf_path);
838 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
840 else if ( statres == -1 ) {
841 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
842 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
843 leaf_path.bv_val, STRERROR( errno ), 0 );
845 else /* it already exists */
846 rs->sr_err = LDAP_ALREADY_EXISTS;
848 SLAP_FREE(base.bv_val);
849 SLAP_FREE(leaf_path.bv_val);
852 ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr);
855 Debug( LDAP_DEBUG_TRACE,
856 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
857 rs->sr_text : "", 0);
858 send_ldap_result(op, rs);
859 slap_graduate_commit_csn( op );
863 static int ldif_back_modify(Operation *op, SlapReply *rs) {
864 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
865 Modifications * modlst = op->orm_modlist;
866 struct berval path = BER_BVNULL;
867 Entry * entry = NULL;
870 slap_mods_opattrs( op, &op->orm_modlist, 1 );
872 ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr);
873 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
875 entry = (Entry *) get_entry(op, &ni->li_base_path);
878 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
879 if(rs->sr_err == LDAP_SUCCESS) {
881 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
882 spew_res = spew_entry(entry, &path);
884 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
886 Debug( LDAP_DEBUG_ANY,
887 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
888 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
889 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
894 rs->sr_err = LDAP_NO_SUCH_OBJECT;
899 if(path.bv_val != NULL)
900 SLAP_FREE(path.bv_val);
902 ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr);
903 send_ldap_result(op, rs);
904 slap_graduate_commit_csn( op );
908 static int ldif_back_delete(Operation *op, SlapReply *rs) {
909 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
910 struct berval path = BER_BVNULL;
913 if ( BER_BVISEMPTY( &op->o_csn )) {
915 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
918 csn.bv_len = sizeof( csnbuf );
919 slap_get_csn( op, &csn, 1 );
922 ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr);
923 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
925 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
926 res = rmdir(path.bv_val);
927 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
928 if ( res && errno != ENOENT ) {
929 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
931 res = unlink(path.bv_val);
936 rs->sr_err = LDAP_NO_SUCH_OBJECT;
938 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
941 rs->sr_err = LDAP_SUCCESS;
943 SLAP_FREE(path.bv_val);
944 ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr);
945 send_ldap_result(op, rs);
946 slap_graduate_commit_csn( op );
951 static int move_entry(Entry * entry, struct berval * ndn,
952 struct berval * newndn, struct berval * suffixdn,
953 struct berval * base_path) {
957 struct berval newpath;
959 dn2path(ndn, suffixdn, base_path, &path);
960 dn2path(newndn, suffixdn, base_path, &newpath);
962 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
963 /* some object doesn't exist */
964 res = LDAP_NO_SUCH_OBJECT;
966 else { /* do the modrdn */
967 exists_res = open(newpath.bv_val, O_RDONLY);
968 if(exists_res == -1 && errno == ENOENT) {
969 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
970 res = spew_entry(entry, &newpath);
972 /* if this fails we should log something bad */
973 res = unlink(path.bv_val);
974 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
975 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
976 res = rename(path.bv_val, newpath.bv_val);
981 res = LDAP_NO_SUCH_OBJECT;
983 res = LDAP_UNWILLING_TO_PERFORM;
984 unlink(newpath.bv_val); /* in case file was created */
986 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
988 else if(exists_res) {
989 int close_res = close(exists_res);
990 res = LDAP_ALREADY_EXISTS;
991 if(close_res == -1) {
992 /* log heinous error */
996 res = LDAP_UNWILLING_TO_PERFORM;
1000 if(newpath.bv_val != NULL)
1001 SLAP_FREE(newpath.bv_val);
1002 if(path.bv_val != NULL)
1003 SLAP_FREE(path.bv_val);
1008 ldif_back_modrdn(Operation *op, SlapReply *rs)
1010 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1011 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1013 Entry * entry = NULL;
1016 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1018 ldap_pvt_thread_rdwr_wlock( &ni->li_rdwr );
1019 entry = (Entry *) get_entry( op, &ni->li_base_path );
1021 /* build the mods to the entry */
1022 if ( entry != NULL ) {
1023 /* build new dn, and new ndn for the entry */
1024 if ( op->oq_modrdn.rs_newSup != NULL ) {
1025 struct berval op_dn = op->o_req_dn,
1026 op_ndn = op->o_req_ndn;
1030 p_dn = *op->oq_modrdn.rs_newSup;
1031 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1032 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1033 np = (Entry *)get_entry( op, &ni->li_base_path );
1034 op->o_req_dn = op_dn;
1035 op->o_req_ndn = op_ndn;
1037 goto no_such_object;
1041 dnParent( &entry->e_name, &p_dn );
1043 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1044 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1045 ber_memfree_x( entry->e_name.bv_val, NULL );
1046 ber_memfree_x( entry->e_nname.bv_val, NULL );
1047 entry->e_name = new_dn;
1048 entry->e_nname = new_ndn;
1050 /* perform the modifications */
1051 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1052 if ( res == LDAP_SUCCESS ) {
1053 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1055 &op->o_bd->be_nsuffix[0],
1056 &ni->li_base_path );
1062 /* entry was null */
1063 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1066 if ( entry != NULL ) {
1067 entry_free( entry );
1070 ldap_pvt_thread_rdwr_wunlock( &ni->li_rdwr );
1071 send_ldap_result( op, rs );
1072 slap_graduate_commit_csn( op );
1076 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1078 int ldif_back_entry_get(
1082 AttributeDescription *at,
1086 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1087 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1089 assert( ndn != NULL );
1090 assert( !BER_BVISNULL( ndn ) );
1092 ldap_pvt_thread_rdwr_rlock( &ni->li_rdwr );
1093 op->o_req_dn = *ndn;
1094 op->o_req_ndn = *ndn;
1095 *ent = (Entry *) get_entry( op, &ni->li_base_path );
1096 op->o_req_dn = op_dn;
1097 op->o_req_ndn = op_ndn;
1098 ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr );
1100 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1105 return ( *ent == NULL ? 1 : 0 );
1108 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1109 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1110 ni->li_tool_current = 0;
1114 static int ldif_tool_entry_close(BackendDB * be) {
1115 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1117 SLAP_FREE(ni->li_tool_cookie.entries);
1122 ldif_tool_entry_first(BackendDB *be)
1124 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1125 ID id = 1; /* first entry in the array of entries shifted by one */
1127 ni->li_tool_current = 1;
1128 if(ni->li_tool_cookie.entries == NULL) {
1132 op.o_req_dn = *be->be_suffix;
1133 op.o_req_ndn = *be->be_nsuffix;
1134 op.ors_scope = LDAP_SCOPE_SUBTREE;
1135 ni->li_tool_cookie.op = &op;
1136 (void)enum_tree( &ni->li_tool_cookie );
1137 ni->li_tool_cookie.op = NULL;
1142 static ID ldif_tool_entry_next(BackendDB *be)
1144 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1145 ni->li_tool_current += 1;
1146 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1149 return ni->li_tool_current;
1152 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1153 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1156 if(id > ni->li_tool_cookie.eind || id < 1)
1159 e = ni->li_tool_cookie.entries[id - 1];
1160 ni->li_tool_cookie.entries[id - 1] = NULL;
1165 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1166 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1167 struct berval dn = e->e_nname;
1168 struct berval leaf_path = BER_BVNULL;
1171 int res = LDAP_SUCCESS;
1173 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1175 if(leaf_path.bv_val != NULL) {
1176 struct berval base = BER_BVNULL;
1177 /* build path to container, and path to ldif of container */
1178 get_parent_path(&leaf_path, &base);
1180 statres = stat(base.bv_val, &stats); /* check if container exists */
1181 if(statres == -1 && errno == ENOENT) { /* container missing */
1182 base.bv_val[base.bv_len] = '.';
1183 statres = stat(base.bv_val, &stats); /* check for leaf node */
1184 base.bv_val[base.bv_len] = '\0';
1185 if(statres == -1 && errno == ENOENT) {
1186 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1188 else if(statres != -1) { /* create parent */
1189 int mkdirres = mkdir(base.bv_val, 0750);
1190 if(mkdirres == -1) {
1191 res = LDAP_UNWILLING_TO_PERFORM;
1195 res = LDAP_UNWILLING_TO_PERFORM;
1196 }/* container was possibly created, move on to add the entry */
1197 if(res == LDAP_SUCCESS) {
1198 statres = stat(leaf_path.bv_val, &stats);
1199 if(statres == -1 && errno == ENOENT) {
1200 res = (int) spew_entry(e, &leaf_path);
1202 else /* it already exists */
1203 res = LDAP_ALREADY_EXISTS;
1205 SLAP_FREE(base.bv_val);
1206 SLAP_FREE(leaf_path.bv_val);
1209 if(res == LDAP_SUCCESS) {
1217 ldif_back_db_init( BackendDB *be, ConfigArgs *ca )
1219 struct ldif_info *ni;
1221 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1222 be->be_private = ni;
1223 be->be_cf_ocs = ldifocs;
1224 ldap_pvt_thread_rdwr_init(&ni->li_rdwr);
1229 ldif_back_db_destroy( Backend *be, ConfigArgs *ca )
1231 struct ldif_info *ni = be->be_private;
1233 ch_free(ni->li_base_path.bv_val);
1234 ldap_pvt_thread_rdwr_destroy(&ni->li_rdwr);
1235 free( be->be_private );
1240 ldif_back_db_open( Backend *be, ConfigArgs *ca)
1242 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1243 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1244 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1251 ldif_back_initialize(
1255 static char *controls[] = {
1256 LDAP_CONTROL_MANAGEDSAIT,
1262 SLAP_BFLAG_INCREMENT |
1263 SLAP_BFLAG_REFERRALS;
1265 bi->bi_controls = controls;
1272 bi->bi_db_init = ldif_back_db_init;
1273 bi->bi_db_config = config_generic_wrapper;
1274 bi->bi_db_open = ldif_back_db_open;
1275 bi->bi_db_close = 0;
1276 bi->bi_db_destroy = ldif_back_db_destroy;
1278 bi->bi_op_bind = ldif_back_bind;
1279 bi->bi_op_unbind = 0;
1280 bi->bi_op_search = ldif_back_search;
1281 bi->bi_op_compare = 0;
1282 bi->bi_op_modify = ldif_back_modify;
1283 bi->bi_op_modrdn = ldif_back_modrdn;
1284 bi->bi_op_add = ldif_back_add;
1285 bi->bi_op_delete = ldif_back_delete;
1286 bi->bi_op_abandon = 0;
1288 bi->bi_extended = 0;
1290 bi->bi_chk_referrals = ldif_back_referrals;
1292 bi->bi_connection_init = 0;
1293 bi->bi_connection_destroy = 0;
1295 bi->bi_entry_get_rw = ldif_back_entry_get;
1297 #if 0 /* NOTE: uncomment to completely disable access control */
1298 bi->bi_access_allowed = slap_access_always_allowed;
1301 bi->bi_tool_entry_open = ldif_tool_entry_open;
1302 bi->bi_tool_entry_close = ldif_tool_entry_close;
1303 bi->bi_tool_entry_first = ldif_tool_entry_first;
1304 bi->bi_tool_entry_next = ldif_tool_entry_next;
1305 bi->bi_tool_entry_get = ldif_tool_entry_get;
1306 bi->bi_tool_entry_put = ldif_tool_entry_put;
1307 bi->bi_tool_entry_reindex = 0;
1308 bi->bi_tool_sync = 0;
1310 bi->bi_tool_dn2id_get = 0;
1311 bi->bi_tool_id2entry_get = 0;
1312 bi->bi_tool_entry_modify = 0;
1314 bi->bi_cf_ocs = ldifocs;
1316 rc = config_register_schema( ldifcfg, ldifocs );
1317 if ( rc ) return rc;