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;
91 assert( !BER_BVISNULL( dn ) );
92 assert( suffixdn != NULL );
93 assert( !BER_BVISNULL( suffixdn ) );
94 assert( dnIsSuffix( dn, suffixdn ) );
96 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
97 res->bv_val = ch_malloc( res->bv_len + 1 );
98 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
99 *ptr++ = LDAP_DIRSEP[0];
100 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
101 end = dn->bv_val + dn->bv_len - suffixdn->bv_len - 1;
102 while ( end > dn->bv_val ) {
103 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
104 *ptr++ = LDAP_DIRSEP[0];
105 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
113 while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
115 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
116 bv.bv_len -= ( ptr - bv.bv_val );
118 ptr = ber_bvchr( &bv, IX_DNR );
122 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
123 bv.bv_len -= ( ptr - bv.bv_val );
130 static char * slurp_file(int fd) {
131 int read_chars_total = 0;
139 entry_size = st.st_size;
140 entry = ch_malloc( entry_size+1 );
144 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
145 if(read_chars == -1) {
149 if(read_chars == 0) {
150 entry[read_chars_total] = '\0';
154 read_chars_total += read_chars;
155 entry_pos += read_chars;
161 static int spew_file(int fd, char * spew, int len) {
165 writeres = write(fd, spew, len);
178 spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
180 int rs, save_errno = 0;
184 char * entry_as_string;
185 char *tmpfname = NULL;
187 tmpfname = ch_malloc( path->bv_len + STRLENOF( "XXXXXX" ) + 1 );
188 AC_MEMCPY( tmpfname, path->bv_val, path->bv_len );
189 AC_MEMCPY( &tmpfname[ path->bv_len ], "XXXXXX", STRLENOF( "XXXXXX" ) + 1 );
191 openres = mkstemp( tmpfname );
192 if ( openres == -1 ) {
194 rs = LDAP_UNWILLING_TO_PERFORM;
195 Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
196 tmpfname, STRERROR( save_errno ), 0 );
202 /* Only save the RDN onto disk */
203 dnRdn( &e->e_name, &rdn );
204 if ( rdn.bv_len != e->e_name.bv_len ) {
205 e->e_name.bv_val[rdn.bv_len] = '\0';
206 tmp = e->e_name.bv_len;
207 e->e_name.bv_len = rdn.bv_len;
212 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
215 entry_as_string = entry2str(e, &entry_length);
217 /* Restore full DN */
218 if ( rdn.bv_len != e->e_name.bv_len ) {
219 e->e_name.bv_val[e->e_name.bv_len] = ',';
220 e->e_name.bv_len = rdn.bv_len;
223 if ( entry_as_string == NULL ) {
224 rs = LDAP_UNWILLING_TO_PERFORM;
228 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
232 spew_res = spew_file( openres,
233 entry_as_string, entry_length );
234 if ( spew_res == -1 )
238 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
241 res = close( openres );
242 rs = LDAP_UNWILLING_TO_PERFORM;
243 if ( res == -1 || spew_res == -1 ) {
244 if ( save_errno == 0 )
246 Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
247 tmpfname, STRERROR( save_errno ), 0 );
249 res = rename( tmpfname, path->bv_val );
255 switch ( save_errno ) {
257 rs = LDAP_NO_SUCH_OBJECT;
267 if ( rs != LDAP_SUCCESS )
273 if ( rs != LDAP_SUCCESS && save_errnop != NULL )
274 *save_errnop = save_errno;
279 static Entry * get_entry_for_fd(int fd,
283 char * entry = (char *) slurp_file(fd);
284 Entry * ldentry = NULL;
286 /* error reading file */
291 ldentry = str2entry(entry);
294 rdn = ldentry->e_name;
295 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
296 ch_free( rdn.bv_val );
297 rdn = ldentry->e_nname;
298 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
299 ch_free( rdn.bv_val );
313 static Entry * get_entry(Operation *op, struct berval *base_path) {
314 struct berval path, pdn, pndn;
317 dnParent(&op->o_req_dn, &pdn);
318 dnParent(&op->o_req_ndn, &pndn);
319 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
320 fd = open(path.bv_val, O_RDONLY);
321 /* error opening file (mebbe should log error) */
322 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
323 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
324 path.bv_val, STRERROR(errno), 0 );
327 if(path.bv_val != NULL)
328 SLAP_FREE(path.bv_val);
331 return get_entry_for_fd(fd, &pdn, &pndn);
337 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
339 res->bv_len = name->bv_len + base->bv_len + 1;
340 res->bv_val = ch_malloc( res->bv_len + 1 );
341 strcpy(res->bv_val, base->bv_val);
342 ptr = res->bv_val + base->bv_len;
343 *ptr++ = LDAP_DIRSEP[0];
344 strcpy(ptr, name->bv_val);
347 typedef struct bvlist {
356 static int r_enum_tree(enumCookie *ck, struct berval *path,
357 struct berval *pdn, struct berval *pndn)
360 int fd, rc = LDAP_SUCCESS;
362 fd = open( path->bv_val, O_RDONLY );
364 Debug( LDAP_DEBUG_TRACE,
365 "=> ldif_enum_tree: failed to open %s: %s\n",
366 path->bv_val, STRERROR(errno), 0 );
367 return LDAP_NO_SUCH_OBJECT;
370 e = get_entry_for_fd(fd, pdn, pndn);
372 Debug( LDAP_DEBUG_ANY,
373 "=> ldif_enum_tree: failed to read entry for %s\n",
374 path->bv_val, 0, 0 );
378 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
379 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
380 /* Send right away? */
383 * if it's a referral, add it to the list of referrals. only do
384 * this for non-base searches, and don't check the filter
385 * explicitly here since it's only a candidate anyway.
387 if ( !get_manageDSAit( ck->op )
388 && ck->op->ors_scope != LDAP_SCOPE_BASE
389 && is_entry_referral( e ) )
391 BerVarray erefs = get_entry_referrals( ck->op, e );
392 ck->rs->sr_ref = referral_rewrite( erefs,
394 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
395 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
397 ck->rs->sr_entry = e;
398 rc = send_search_reference( ck->op, ck->rs );
399 ber_bvarray_free( ck->rs->sr_ref );
400 ber_bvarray_free( erefs );
401 ck->rs->sr_ref = NULL;
402 ck->rs->sr_entry = NULL;
404 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
406 ck->rs->sr_entry = e;
407 ck->rs->sr_attrs = ck->op->ors_attrs;
408 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
409 rc = send_search_entry(ck->op, ck->rs);
410 ck->rs->sr_entry = NULL;
416 /* Queueing up for tool mode */
417 if(ck->entries == NULL) {
418 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
419 ck->elen = ENTRY_BUFF_INCREMENT;
421 if(ck->eind >= ck->elen) { /* grow entries if necessary */
422 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
426 ck->entries[ck->eind++] = e;
433 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
435 bvlist *list = NULL, *ptr;
437 path->bv_len -= STRLENOF( LDIF );
438 path->bv_val[path->bv_len] = '\0';
440 dir_of_path = opendir(path->bv_val);
441 if(dir_of_path == NULL) { /* can't open directory */
442 if ( errno != ENOENT ) {
443 /* it shouldn't be treated as an error
444 * only if the directory doesn't exist */
446 Debug( LDAP_DEBUG_ANY,
447 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
448 path->bv_val, errno, 0 );
454 struct berval fname, itmp;
458 dir = readdir(dir_of_path);
459 if(dir == NULL) break; /* end of the directory */
460 fname.bv_len = strlen( dir->d_name );
461 if ( fname.bv_len <= STRLENOF( LDIF ))
463 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
465 fname.bv_val = dir->d_name;
467 bvl = ch_malloc( sizeof(bvlist) );
468 ber_dupbv( &bvl->bv, &fname );
469 BER_BVZERO( &bvl->num );
470 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
474 itmp.bv_len = bvl->bv.bv_len
475 - ( itmp.bv_val - bvl->bv.bv_val );
476 ptr = ber_bvchr( &itmp, IX_FSR );
478 itmp.bv_len = ptr - itmp.bv_val;
479 ber_dupbv( &bvl->num, &itmp );
480 bvl->inum = strtol( itmp.bv_val, NULL, 0 );
481 itmp.bv_val[0] = '\0';
482 bvl->off = itmp.bv_val - bvl->bv.bv_val;
486 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
487 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
488 if ( !cmp && bvl->num.bv_val )
489 cmp = bvl->inum - ptr->inum;
497 closedir(dir_of_path);
499 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
500 ck->op->ors_scope = LDAP_SCOPE_BASE;
501 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
502 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
504 while ( ( ptr = list ) ) {
509 if ( rc == LDAP_SUCCESS ) {
510 if ( ptr->num.bv_val )
511 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
513 fullpath( path, &ptr->bv, &fpath );
514 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
517 if ( ptr->num.bv_val )
518 free( ptr->num.bv_val );
519 free(ptr->bv.bv_val);
524 if ( fd ) entry_free( e );
533 struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
535 struct berval pdn, pndn;
538 dnParent( &ck->op->o_req_dn, &pdn );
539 dnParent( &ck->op->o_req_ndn, &pndn );
540 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
541 rc = r_enum_tree(ck, &path, &pdn, &pndn);
542 ch_free( path.bv_val );
546 /* Get the parent path plus the LDIF suffix */
547 static void get_parent_path(struct berval * dnpath, struct berval *res) {
548 int dnpathlen = dnpath->bv_len;
551 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
552 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
555 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
556 strncpy(res->bv_val, dnpath->bv_val, i);
557 strcpy(res->bv_val+i, LDIF);
558 res->bv_val[i] = '\0';
561 static int apply_modify_to_entry(Entry * entry,
562 Modifications * modlist,
566 char textbuf[SLAP_TEXT_BUFLEN];
567 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
569 Modification *mods = NULL;
571 if (!acl_check_modlist(op, entry, modlist)) {
572 return LDAP_INSUFFICIENT_ACCESS;
575 for (; modlist != NULL; modlist = modlist->sml_next) {
576 mods = &modlist->sml_mod;
578 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
581 switch (mods->sm_op) {
583 rc = modify_add_values(entry, mods,
584 get_permissiveModify(op),
585 &rs->sr_text, textbuf,
589 case LDAP_MOD_DELETE:
590 rc = modify_delete_values(entry, mods,
591 get_permissiveModify(op),
592 &rs->sr_text, textbuf,
596 case LDAP_MOD_REPLACE:
597 rc = modify_replace_values(entry, mods,
598 get_permissiveModify(op),
599 &rs->sr_text, textbuf,
603 case LDAP_MOD_INCREMENT:
604 rc = modify_increment_values( entry,
605 mods, get_permissiveModify(op),
606 &rs->sr_text, textbuf,
612 case SLAP_MOD_SOFTADD:
613 mods->sm_op = LDAP_MOD_ADD;
614 rc = modify_add_values(entry, mods,
615 get_permissiveModify(op),
616 &rs->sr_text, textbuf,
618 mods->sm_op = SLAP_MOD_SOFTADD;
619 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
626 if(rc != LDAP_SUCCESS) break;
629 if(rc == LDAP_SUCCESS) {
631 entry->e_ocflags = 0;
633 /* check that the entry still obeys the schema */
634 rc = entry_schema_check( op, entry, NULL, 0, 0,
635 &rs->sr_text, textbuf, sizeof( textbuf ) );
642 ldif_back_referrals( Operation *op, SlapReply *rs )
644 struct ldif_info *li = NULL;
646 int rc = LDAP_SUCCESS;
649 if ( op->o_tag == LDAP_REQ_SEARCH ) {
650 /* let search take care of itself */
655 if ( get_manageDSAit( op ) ) {
656 /* let op take care of DSA management */
660 li = (struct ldif_info *)op->o_bd->be_private;
661 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
662 entry = get_entry( op, &li->li_base_path );
664 /* no object is found for them */
665 if ( entry == NULL ) {
666 struct berval odn = op->o_req_dn;
667 struct berval ondn = op->o_req_ndn;
669 struct berval pndn = op->o_req_ndn;
671 for ( ; entry == NULL; ) {
672 dnParent( &pndn, &pndn );
674 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
679 op->o_req_ndn = pndn;
681 entry = get_entry( op, &li->li_base_path );
684 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
687 op->o_req_ndn = ondn;
690 rs->sr_matched = NULL;
691 if ( entry != NULL ) {
692 Debug( LDAP_DEBUG_TRACE,
693 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
694 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
696 if ( is_entry_referral( entry ) ) {
698 rs->sr_ref = get_entry_referrals( op, entry );
700 rs->sr_matched = ber_strdup_x(
701 entry->e_name.bv_val, op->o_tmpmemctx );
707 } else if ( default_referral != NULL ) {
709 rs->sr_ref = referral_rewrite( default_referral,
710 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
713 if ( rs->sr_ref != NULL ) {
715 rc = rs->sr_err = LDAP_REFERRAL;
716 send_ldap_result( op, rs );
717 ber_bvarray_free( rs->sr_ref );
720 } else if ( rc != LDAP_SUCCESS ) {
722 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
723 send_ldap_result( op, rs );
726 if ( rs->sr_matched ) {
727 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
728 rs->sr_matched = NULL;
734 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
736 if ( is_entry_referral( entry ) ) {
737 /* entry is a referral */
738 BerVarray refs = get_entry_referrals( op, entry );
739 rs->sr_ref = referral_rewrite(
740 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
742 Debug( LDAP_DEBUG_TRACE,
743 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
744 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
746 rs->sr_matched = entry->e_name.bv_val;
747 if ( rs->sr_ref != NULL ) {
748 rc = rs->sr_err = LDAP_REFERRAL;
749 send_ldap_result( op, rs );
750 ber_bvarray_free( rs->sr_ref );
754 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
758 rs->sr_matched = NULL;
759 ber_bvarray_free( refs );
768 ldif_back_bind( Operation *op, SlapReply *rs )
770 struct ldif_info *li = NULL;
771 Attribute * a = NULL;
772 AttributeDescription *password = slap_schema.si_ad_userPassword;
774 Entry * entry = NULL;
776 switch ( be_rootdn_bind( op, rs ) ) {
777 case SLAP_CB_CONTINUE:
781 /* in case of success, front end will send result;
782 * otherwise, be_rootdn_bind() did */
786 li = (struct ldif_info *) op->o_bd->be_private;
787 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
788 entry = get_entry(op, &li->li_base_path);
790 /* no object is found for them */
792 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
796 /* they don't have userpassword */
797 if((a = attr_find(entry->e_attrs, password)) == NULL) {
798 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
803 /* authentication actually failed */
804 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
805 &rs->sr_text) != 0) {
806 rs->sr_err = LDAP_INVALID_CREDENTIALS;
811 /* let the front-end send success */
816 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
818 send_ldap_result( op, rs );
824 static int ldif_back_search(Operation *op, SlapReply *rs)
826 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
827 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
831 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
832 rs->sr_err = enum_tree( &ck );
833 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
834 send_ldap_result(op, rs);
839 static int ldif_back_add(Operation *op, SlapReply *rs) {
840 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
841 Entry * e = op->ora_e;
842 struct berval dn = e->e_nname;
843 struct berval leaf_path = BER_BVNULL;
846 char textbuf[SLAP_TEXT_BUFLEN];
848 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
850 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
851 &rs->sr_text, textbuf, sizeof( textbuf ) );
852 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
854 rs->sr_err = slap_add_opattrs( op,
855 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
856 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
858 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
860 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
862 if(leaf_path.bv_val != NULL) {
863 struct berval base = BER_BVNULL;
864 /* build path to container and ldif of container */
865 get_parent_path(&leaf_path, &base);
867 statres = stat(base.bv_val, &stats); /* check if container exists */
868 if(statres == -1 && errno == ENOENT) { /* container missing */
869 base.bv_val[base.bv_len] = '.';
870 statres = stat(base.bv_val, &stats); /* check for leaf node */
871 base.bv_val[base.bv_len] = '\0';
872 if(statres == -1 && errno == ENOENT) {
873 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
874 rs->sr_text = "Parent does not exist";
876 else if(statres != -1) { /* create parent */
877 int mkdirres = mkdir(base.bv_val, 0750);
879 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
880 rs->sr_text = "Could not create parent folder";
881 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
882 base.bv_val, STRERROR( errno ), 0 );
886 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
887 }/* container was possibly created, move on to add the entry */
888 if(rs->sr_err == LDAP_SUCCESS) {
889 statres = stat(leaf_path.bv_val, &stats);
890 if(statres == -1 && errno == ENOENT) {
891 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
893 else if ( statres == -1 ) {
894 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
895 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
896 leaf_path.bv_val, STRERROR( errno ), 0 );
898 else /* it already exists */
899 rs->sr_err = LDAP_ALREADY_EXISTS;
901 SLAP_FREE(base.bv_val);
902 SLAP_FREE(leaf_path.bv_val);
905 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
908 Debug( LDAP_DEBUG_TRACE,
909 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
910 rs->sr_text : "", 0);
911 send_ldap_result(op, rs);
912 slap_graduate_commit_csn( op );
916 static int ldif_back_modify(Operation *op, SlapReply *rs) {
917 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
918 Modifications * modlst = op->orm_modlist;
919 struct berval path = BER_BVNULL;
920 Entry * entry = NULL;
923 slap_mods_opattrs( op, &op->orm_modlist, 1 );
925 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
926 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
928 entry = get_entry(op, &li->li_base_path);
931 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
932 if(rs->sr_err == LDAP_SUCCESS) {
934 spew_res = spew_entry(entry, &path, 1, &save_errno);
936 Debug( LDAP_DEBUG_ANY,
937 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
938 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
939 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
944 rs->sr_err = LDAP_NO_SUCH_OBJECT;
949 if(path.bv_val != NULL)
950 SLAP_FREE(path.bv_val);
952 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
953 send_ldap_result(op, rs);
954 slap_graduate_commit_csn( op );
958 static int ldif_back_delete(Operation *op, SlapReply *rs) {
959 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
960 struct berval path = BER_BVNULL;
963 if ( BER_BVISEMPTY( &op->o_csn )) {
965 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
968 csn.bv_len = sizeof( csnbuf );
969 slap_get_csn( op, &csn, 1 );
972 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
973 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
975 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
976 res = rmdir(path.bv_val);
977 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
978 rs->sr_err = LDAP_SUCCESS;
982 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
986 rs->sr_err = LDAP_NO_SUCH_OBJECT;
990 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
995 res = unlink(path.bv_val);
999 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1003 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1009 SLAP_FREE(path.bv_val);
1010 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1011 send_ldap_result(op, rs);
1012 slap_graduate_commit_csn( op );
1017 static int move_entry(Entry * entry, struct berval * ndn,
1018 struct berval * newndn, struct berval * suffixdn,
1019 struct berval * base_path) {
1023 struct berval newpath;
1025 dn2path(ndn, suffixdn, base_path, &path);
1026 dn2path(newndn, suffixdn, base_path, &newpath);
1028 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1029 /* some object doesn't exist */
1030 res = LDAP_NO_SUCH_OBJECT;
1032 else { /* do the modrdn */
1033 exists_res = open(newpath.bv_val, O_RDONLY);
1034 if(exists_res == -1 && errno == ENOENT) {
1035 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1036 res = spew_entry(entry, &newpath, 0, NULL);
1038 /* if this fails we should log something bad */
1039 res = unlink(path.bv_val);
1040 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1041 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1042 res = rename(path.bv_val, newpath.bv_val);
1047 res = LDAP_NO_SUCH_OBJECT;
1049 res = LDAP_UNWILLING_TO_PERFORM;
1050 unlink(newpath.bv_val); /* in case file was created */
1052 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1054 else if(exists_res) {
1055 int close_res = close(exists_res);
1056 res = LDAP_ALREADY_EXISTS;
1057 if(close_res == -1) {
1058 /* log heinous error */
1062 res = LDAP_UNWILLING_TO_PERFORM;
1066 if(newpath.bv_val != NULL)
1067 SLAP_FREE(newpath.bv_val);
1068 if(path.bv_val != NULL)
1069 SLAP_FREE(path.bv_val);
1074 ldif_back_modrdn(Operation *op, SlapReply *rs)
1076 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1077 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1079 Entry * entry = NULL;
1082 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1084 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1085 entry = get_entry( op, &li->li_base_path );
1087 /* build the mods to the entry */
1088 if ( entry != NULL ) {
1089 /* build new dn, and new ndn for the entry */
1090 if ( op->oq_modrdn.rs_newSup != NULL ) {
1091 struct berval op_dn = op->o_req_dn,
1092 op_ndn = op->o_req_ndn;
1096 p_dn = *op->oq_modrdn.rs_newSup;
1097 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1098 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1099 np = get_entry( op, &li->li_base_path );
1100 op->o_req_dn = op_dn;
1101 op->o_req_ndn = op_ndn;
1103 goto no_such_object;
1107 dnParent( &entry->e_name, &p_dn );
1109 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1110 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1111 ber_memfree_x( entry->e_name.bv_val, NULL );
1112 ber_memfree_x( entry->e_nname.bv_val, NULL );
1113 entry->e_name = new_dn;
1114 entry->e_nname = new_ndn;
1116 /* perform the modifications */
1117 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1118 if ( res == LDAP_SUCCESS ) {
1119 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1121 &op->o_bd->be_nsuffix[0],
1122 &li->li_base_path );
1128 /* entry was null */
1129 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1132 if ( entry != NULL ) {
1133 entry_free( entry );
1136 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1137 send_ldap_result( op, rs );
1138 slap_graduate_commit_csn( op );
1142 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1144 int ldif_back_entry_get(
1148 AttributeDescription *at,
1152 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1153 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1155 assert( ndn != NULL );
1156 assert( !BER_BVISNULL( ndn ) );
1158 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1159 op->o_req_dn = *ndn;
1160 op->o_req_ndn = *ndn;
1161 *ent = get_entry( op, &li->li_base_path );
1162 op->o_req_dn = op_dn;
1163 op->o_req_ndn = op_ndn;
1164 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1166 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1171 return ( *ent == NULL ? 1 : 0 );
1174 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1175 struct ldif_info *li = (struct ldif_info *) be->be_private;
1176 li->li_tool_current = 0;
1180 static int ldif_tool_entry_close(BackendDB * be) {
1181 struct ldif_info *li = (struct ldif_info *) be->be_private;
1183 SLAP_FREE(li->li_tool_cookie.entries);
1187 static ID ldif_tool_entry_next(BackendDB *be)
1189 struct ldif_info *li = (struct ldif_info *) be->be_private;
1190 if(li->li_tool_current >= li->li_tool_cookie.eind)
1193 return ++li->li_tool_current;
1197 ldif_tool_entry_first(BackendDB *be)
1199 struct ldif_info *li = (struct ldif_info *) be->be_private;
1201 if(li->li_tool_cookie.entries == NULL) {
1205 op.o_req_dn = *be->be_suffix;
1206 op.o_req_ndn = *be->be_nsuffix;
1207 op.ors_scope = LDAP_SCOPE_SUBTREE;
1208 li->li_tool_cookie.op = &op;
1209 (void)enum_tree( &li->li_tool_cookie );
1210 li->li_tool_cookie.op = NULL;
1212 return ldif_tool_entry_next( be );
1215 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1216 struct ldif_info *li = (struct ldif_info *) be->be_private;
1219 if(id > li->li_tool_cookie.eind || id < 1)
1222 e = li->li_tool_cookie.entries[id - 1];
1223 li->li_tool_cookie.entries[id - 1] = NULL;
1228 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1229 struct ldif_info *li = (struct ldif_info *) be->be_private;
1230 struct berval dn = e->e_nname;
1231 struct berval leaf_path = BER_BVNULL;
1234 int res = LDAP_SUCCESS;
1236 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1238 if(leaf_path.bv_val != NULL) {
1239 struct berval base = BER_BVNULL;
1240 /* build path to container, and path to ldif of container */
1241 get_parent_path(&leaf_path, &base);
1243 statres = stat(base.bv_val, &stats); /* check if container exists */
1244 if(statres == -1 && errno == ENOENT) { /* container missing */
1245 base.bv_val[base.bv_len] = '.';
1246 statres = stat(base.bv_val, &stats); /* check for leaf node */
1247 base.bv_val[base.bv_len] = '\0';
1248 if(statres == -1 && errno == ENOENT) {
1249 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1251 else if(statres != -1) { /* create parent */
1252 int mkdirres = mkdir(base.bv_val, 0750);
1253 if(mkdirres == -1) {
1254 res = LDAP_UNWILLING_TO_PERFORM;
1258 res = LDAP_UNWILLING_TO_PERFORM;
1259 }/* container was possibly created, move on to add the entry */
1260 if(res == LDAP_SUCCESS) {
1261 statres = stat(leaf_path.bv_val, &stats);
1262 if(statres == -1 && errno == ENOENT) {
1263 res = spew_entry(e, &leaf_path, 0, NULL);
1265 else /* it already exists */
1266 res = LDAP_ALREADY_EXISTS;
1268 SLAP_FREE(base.bv_val);
1269 SLAP_FREE(leaf_path.bv_val);
1272 if(res == LDAP_SUCCESS) {
1280 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1282 struct ldif_info *li;
1284 li = ch_calloc( 1, sizeof(struct ldif_info) );
1285 be->be_private = li;
1286 be->be_cf_ocs = ldifocs;
1287 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1292 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1294 struct ldif_info *li = be->be_private;
1296 ch_free(li->li_base_path.bv_val);
1297 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1298 free( be->be_private );
1303 ldif_back_db_open( Backend *be, ConfigReply *cr)
1305 struct ldif_info *li = (struct ldif_info *) be->be_private;
1306 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1307 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1314 ldif_back_initialize(
1318 static char *controls[] = {
1319 LDAP_CONTROL_MANAGEDSAIT,
1325 SLAP_BFLAG_INCREMENT |
1326 SLAP_BFLAG_REFERRALS;
1328 bi->bi_controls = controls;
1335 bi->bi_db_init = ldif_back_db_init;
1336 bi->bi_db_config = config_generic_wrapper;
1337 bi->bi_db_open = ldif_back_db_open;
1338 bi->bi_db_close = 0;
1339 bi->bi_db_destroy = ldif_back_db_destroy;
1341 bi->bi_op_bind = ldif_back_bind;
1342 bi->bi_op_unbind = 0;
1343 bi->bi_op_search = ldif_back_search;
1344 bi->bi_op_compare = 0;
1345 bi->bi_op_modify = ldif_back_modify;
1346 bi->bi_op_modrdn = ldif_back_modrdn;
1347 bi->bi_op_add = ldif_back_add;
1348 bi->bi_op_delete = ldif_back_delete;
1349 bi->bi_op_abandon = 0;
1351 bi->bi_extended = 0;
1353 bi->bi_chk_referrals = ldif_back_referrals;
1355 bi->bi_connection_init = 0;
1356 bi->bi_connection_destroy = 0;
1358 bi->bi_entry_get_rw = ldif_back_entry_get;
1360 #if 0 /* NOTE: uncomment to completely disable access control */
1361 bi->bi_access_allowed = slap_access_always_allowed;
1364 bi->bi_tool_entry_open = ldif_tool_entry_open;
1365 bi->bi_tool_entry_close = ldif_tool_entry_close;
1366 bi->bi_tool_entry_first = ldif_tool_entry_first;
1367 bi->bi_tool_entry_next = ldif_tool_entry_next;
1368 bi->bi_tool_entry_get = ldif_tool_entry_get;
1369 bi->bi_tool_entry_put = ldif_tool_entry_put;
1370 bi->bi_tool_entry_reindex = 0;
1371 bi->bi_tool_sync = 0;
1373 bi->bi_tool_dn2id_get = 0;
1374 bi->bi_tool_entry_modify = 0;
1376 bi->bi_cf_ocs = ldifocs;
1378 rc = config_register_schema( ldifcfg, ldifocs );
1379 if ( rc ) return rc;