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);
167 Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
168 STRERROR( errno ), 0, 0 );
180 spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
186 char * entry_as_string;
187 char tmpfname[] = "tmpXXXXXX";
189 openres = mkstemp( tmpfname );
190 if ( save_errnop ) *save_errnop = errno;
191 if ( openres == -1 ) {
192 rs = LDAP_UNWILLING_TO_PERFORM;
193 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
194 path->bv_val, STRERROR( *save_errnop ), 0 );
200 /* Only save the RDN onto disk */
201 dnRdn( &e->e_name, &rdn );
202 if ( rdn.bv_len != e->e_name.bv_len ) {
203 e->e_name.bv_val[rdn.bv_len] = '\0';
204 tmp = e->e_name.bv_len;
205 e->e_name.bv_len = rdn.bv_len;
210 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
213 entry_as_string = entry2str(e, &entry_length);
215 /* Restore full DN */
216 if ( rdn.bv_len != e->e_name.bv_len ) {
217 e->e_name.bv_val[e->e_name.bv_len] = ',';
218 e->e_name.bv_len = rdn.bv_len;
221 if ( entry_as_string == NULL ) {
222 rs = LDAP_UNWILLING_TO_PERFORM;
227 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
231 spew_res = spew_file( openres,
232 entry_as_string, entry_length );
235 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
239 rs = LDAP_UNWILLING_TO_PERFORM;
240 if ( spew_res != -1 ) {
241 int res = rename( tmpfname, path->bv_val );
242 if ( save_errnop ) *save_errnop = errno;
247 switch ( *save_errnop ) {
249 rs = LDAP_NO_SUCH_OBJECT;
263 static Entry * get_entry_for_fd(int fd,
267 char * entry = (char *) slurp_file(fd);
268 Entry * ldentry = NULL;
270 /* error reading file */
275 ldentry = str2entry(entry);
278 rdn = ldentry->e_name;
279 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
280 ch_free( rdn.bv_val );
281 rdn = ldentry->e_nname;
282 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
283 ch_free( rdn.bv_val );
297 static Entry * get_entry(Operation *op, struct berval *base_path) {
298 struct berval path, pdn, pndn;
301 dnParent(&op->o_req_dn, &pdn);
302 dnParent(&op->o_req_ndn, &pndn);
303 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
304 fd = open(path.bv_val, O_RDONLY);
305 /* error opening file (mebbe should log error) */
306 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
307 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
308 path.bv_val, STRERROR(errno), 0 );
311 if(path.bv_val != NULL)
312 SLAP_FREE(path.bv_val);
315 return get_entry_for_fd(fd, &pdn, &pndn);
321 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
323 res->bv_len = name->bv_len + base->bv_len + 1;
324 res->bv_val = ch_malloc( res->bv_len + 1 );
325 strcpy(res->bv_val, base->bv_val);
326 ptr = res->bv_val + base->bv_len;
327 *ptr++ = LDAP_DIRSEP[0];
328 strcpy(ptr, name->bv_val);
331 typedef struct bvlist {
340 static int r_enum_tree(enumCookie *ck, struct berval *path,
341 struct berval *pdn, struct berval *pndn)
344 int fd, rc = LDAP_SUCCESS;
346 fd = open( path->bv_val, O_RDONLY );
348 Debug( LDAP_DEBUG_TRACE,
349 "=> ldif_enum_tree: failed to open %s: %s\n",
350 path->bv_val, STRERROR(errno), 0 );
351 return LDAP_NO_SUCH_OBJECT;
354 e = get_entry_for_fd(fd, pdn, pndn);
356 Debug( LDAP_DEBUG_ANY,
357 "=> ldif_enum_tree: failed to read entry for %s\n",
358 path->bv_val, 0, 0 );
362 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
363 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
364 /* Send right away? */
367 * if it's a referral, add it to the list of referrals. only do
368 * this for non-base searches, and don't check the filter
369 * explicitly here since it's only a candidate anyway.
371 if ( !get_manageDSAit( ck->op )
372 && ck->op->ors_scope != LDAP_SCOPE_BASE
373 && is_entry_referral( e ) )
375 BerVarray erefs = get_entry_referrals( ck->op, e );
376 ck->rs->sr_ref = referral_rewrite( erefs,
378 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
379 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
381 ck->rs->sr_entry = e;
382 rc = send_search_reference( ck->op, ck->rs );
383 ber_bvarray_free( ck->rs->sr_ref );
384 ber_bvarray_free( erefs );
385 ck->rs->sr_ref = NULL;
386 ck->rs->sr_entry = NULL;
388 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
390 ck->rs->sr_entry = e;
391 ck->rs->sr_attrs = ck->op->ors_attrs;
392 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
393 rc = send_search_entry(ck->op, ck->rs);
394 ck->rs->sr_entry = NULL;
400 /* Queueing up for tool mode */
401 if(ck->entries == NULL) {
402 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
403 ck->elen = ENTRY_BUFF_INCREMENT;
405 if(ck->eind >= ck->elen) { /* grow entries if necessary */
406 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
410 ck->entries[ck->eind++] = e;
417 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
419 bvlist *list = NULL, *ptr;
421 path->bv_len -= STRLENOF( LDIF );
422 path->bv_val[path->bv_len] = '\0';
424 dir_of_path = opendir(path->bv_val);
425 if(dir_of_path == NULL) { /* can't open directory */
426 if ( errno != ENOENT ) {
427 /* it shouldn't be treated as an error
428 * only if the directory doesn't exist */
430 Debug( LDAP_DEBUG_ANY,
431 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
432 path->bv_val, errno, 0 );
438 struct berval fname, itmp;
442 dir = readdir(dir_of_path);
443 if(dir == NULL) break; /* end of the directory */
444 fname.bv_len = strlen( dir->d_name );
445 if ( fname.bv_len <= STRLENOF( LDIF ))
447 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
449 fname.bv_val = dir->d_name;
451 bvl = ch_malloc( sizeof(bvlist) );
452 ber_dupbv( &bvl->bv, &fname );
453 BER_BVZERO( &bvl->num );
454 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
458 itmp.bv_len = bvl->bv.bv_len
459 - ( itmp.bv_val - bvl->bv.bv_val );
460 ptr = ber_bvchr( &itmp, IX_FSR );
462 itmp.bv_len = ptr - itmp.bv_val;
463 ber_dupbv( &bvl->num, &itmp );
464 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
465 itmp.bv_val[0] = '\0';
466 bvl->off = itmp.bv_val - bvl->bv.bv_val;
470 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
471 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
472 if ( !cmp && bvl->num.bv_val )
473 cmp = bvl->inum - ptr->inum;
481 closedir(dir_of_path);
483 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
484 ck->op->ors_scope = LDAP_SCOPE_BASE;
485 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
486 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
488 while ( ( ptr = list ) ) {
493 if ( rc == LDAP_SUCCESS ) {
494 if ( ptr->num.bv_val )
495 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
497 fullpath( path, &ptr->bv, &fpath );
498 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
501 if ( ptr->num.bv_val )
502 free( ptr->num.bv_val );
503 free(ptr->bv.bv_val);
508 if ( fd ) entry_free( e );
517 struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
519 struct berval pdn, pndn;
522 dnParent( &ck->op->o_req_dn, &pdn );
523 dnParent( &ck->op->o_req_ndn, &pndn );
524 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
525 rc = r_enum_tree(ck, &path, &pdn, &pndn);
526 ch_free( path.bv_val );
530 /* Get the parent path plus the LDIF suffix */
531 static void get_parent_path(struct berval * dnpath, struct berval *res) {
532 int dnpathlen = dnpath->bv_len;
535 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
536 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
539 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
540 strncpy(res->bv_val, dnpath->bv_val, i);
541 strcpy(res->bv_val+i, LDIF);
542 res->bv_val[i] = '\0';
545 static int apply_modify_to_entry(Entry * entry,
546 Modifications * modlist,
550 char textbuf[SLAP_TEXT_BUFLEN];
551 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
553 Modification *mods = NULL;
555 if (!acl_check_modlist(op, entry, modlist)) {
556 return LDAP_INSUFFICIENT_ACCESS;
559 for (; modlist != NULL; modlist = modlist->sml_next) {
560 mods = &modlist->sml_mod;
562 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
565 switch (mods->sm_op) {
567 rc = modify_add_values(entry, mods,
568 get_permissiveModify(op),
569 &rs->sr_text, textbuf,
573 case LDAP_MOD_DELETE:
574 rc = modify_delete_values(entry, mods,
575 get_permissiveModify(op),
576 &rs->sr_text, textbuf,
580 case LDAP_MOD_REPLACE:
581 rc = modify_replace_values(entry, mods,
582 get_permissiveModify(op),
583 &rs->sr_text, textbuf,
587 case LDAP_MOD_INCREMENT:
588 rc = modify_increment_values( entry,
589 mods, get_permissiveModify(op),
590 &rs->sr_text, textbuf,
596 case SLAP_MOD_SOFTADD:
597 mods->sm_op = LDAP_MOD_ADD;
598 rc = modify_add_values(entry, mods,
599 get_permissiveModify(op),
600 &rs->sr_text, textbuf,
602 mods->sm_op = SLAP_MOD_SOFTADD;
603 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
610 if(rc != LDAP_SUCCESS) break;
613 if(rc == LDAP_SUCCESS) {
615 entry->e_ocflags = 0;
617 /* check that the entry still obeys the schema */
618 rc = entry_schema_check( op, entry, NULL, 0, 0,
619 &rs->sr_text, textbuf, sizeof( textbuf ) );
626 ldif_back_referrals( Operation *op, SlapReply *rs )
628 struct ldif_info *li = NULL;
630 int rc = LDAP_SUCCESS;
633 if ( op->o_tag == LDAP_REQ_SEARCH ) {
634 /* let search take care of itself */
639 if ( get_manageDSAit( op ) ) {
640 /* let op take care of DSA management */
644 li = (struct ldif_info *)op->o_bd->be_private;
645 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
646 entry = get_entry( op, &li->li_base_path );
648 /* no object is found for them */
649 if ( entry == NULL ) {
650 struct berval odn = op->o_req_dn;
651 struct berval ondn = op->o_req_ndn;
653 struct berval pndn = op->o_req_ndn;
655 for ( ; entry == NULL; ) {
656 dnParent( &pndn, &pndn );
658 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
663 op->o_req_ndn = pndn;
665 entry = get_entry( op, &li->li_base_path );
668 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
671 op->o_req_ndn = ondn;
674 rs->sr_matched = NULL;
675 if ( entry != NULL ) {
676 Debug( LDAP_DEBUG_TRACE,
677 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
678 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
680 if ( is_entry_referral( entry ) ) {
682 rs->sr_ref = get_entry_referrals( op, entry );
684 rs->sr_matched = ber_strdup_x(
685 entry->e_name.bv_val, op->o_tmpmemctx );
691 } else if ( default_referral != NULL ) {
693 rs->sr_ref = referral_rewrite( default_referral,
694 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
697 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 );
704 } else if ( rc != LDAP_SUCCESS ) {
706 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
707 send_ldap_result( op, rs );
710 if ( rs->sr_matched ) {
711 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
712 rs->sr_matched = NULL;
718 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
720 if ( is_entry_referral( entry ) ) {
721 /* entry is a referral */
722 BerVarray refs = get_entry_referrals( op, entry );
723 rs->sr_ref = referral_rewrite(
724 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
726 Debug( LDAP_DEBUG_TRACE,
727 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
728 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
730 rs->sr_matched = entry->e_name.bv_val;
731 if ( rs->sr_ref != NULL ) {
732 rc = rs->sr_err = LDAP_REFERRAL;
733 send_ldap_result( op, rs );
734 ber_bvarray_free( rs->sr_ref );
738 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
742 rs->sr_matched = NULL;
743 ber_bvarray_free( refs );
752 ldif_back_bind( Operation *op, SlapReply *rs )
754 struct ldif_info *li = NULL;
755 Attribute * a = NULL;
756 AttributeDescription *password = slap_schema.si_ad_userPassword;
758 Entry * entry = NULL;
760 li = (struct ldif_info *) op->o_bd->be_private;
761 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
762 entry = get_entry(op, &li->li_base_path);
764 /* no object is found for them */
766 if(be_isroot_pw(op)) {
767 rs->sr_err = return_val = LDAP_SUCCESS;
769 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
774 /* they don't have userpassword */
775 if((a = attr_find(entry->e_attrs, password)) == NULL) {
776 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
781 /* authentication actually failed */
782 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
783 &rs->sr_text) != 0) {
784 rs->sr_err = LDAP_INVALID_CREDENTIALS;
789 /* let the front-end send success */
794 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
796 send_ldap_result( op, rs );
802 static int ldif_back_search(Operation *op, SlapReply *rs)
804 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
805 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
809 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
810 rs->sr_err = enum_tree( &ck );
811 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
812 send_ldap_result(op, rs);
817 static int ldif_back_add(Operation *op, SlapReply *rs) {
818 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
819 Entry * e = op->ora_e;
820 struct berval dn = e->e_nname;
821 struct berval leaf_path = BER_BVNULL;
824 char textbuf[SLAP_TEXT_BUFLEN];
826 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
828 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
829 &rs->sr_text, textbuf, sizeof( textbuf ) );
830 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
832 rs->sr_err = slap_add_opattrs( op,
833 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
834 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
836 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
838 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
840 if(leaf_path.bv_val != NULL) {
841 struct berval base = BER_BVNULL;
842 /* build path to container and ldif of container */
843 get_parent_path(&leaf_path, &base);
845 statres = stat(base.bv_val, &stats); /* check if container exists */
846 if(statres == -1 && errno == ENOENT) { /* container missing */
847 base.bv_val[base.bv_len] = '.';
848 statres = stat(base.bv_val, &stats); /* check for leaf node */
849 base.bv_val[base.bv_len] = '\0';
850 if(statres == -1 && errno == ENOENT) {
851 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
852 rs->sr_text = "Parent does not exist";
854 else if(statres != -1) { /* create parent */
855 int mkdirres = mkdir(base.bv_val, 0750);
857 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
858 rs->sr_text = "Could not create parent folder";
859 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
860 base.bv_val, STRERROR( errno ), 0 );
864 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
865 }/* container was possibly created, move on to add the entry */
866 if(rs->sr_err == LDAP_SUCCESS) {
867 statres = stat(leaf_path.bv_val, &stats);
868 if(statres == -1 && errno == ENOENT) {
869 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
871 else if ( statres == -1 ) {
872 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
873 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
874 leaf_path.bv_val, STRERROR( errno ), 0 );
876 else /* it already exists */
877 rs->sr_err = LDAP_ALREADY_EXISTS;
879 SLAP_FREE(base.bv_val);
880 SLAP_FREE(leaf_path.bv_val);
883 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
886 Debug( LDAP_DEBUG_TRACE,
887 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
888 rs->sr_text : "", 0);
889 send_ldap_result(op, rs);
890 slap_graduate_commit_csn( op );
894 static int ldif_back_modify(Operation *op, SlapReply *rs) {
895 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
896 Modifications * modlst = op->orm_modlist;
897 struct berval path = BER_BVNULL;
898 Entry * entry = NULL;
901 slap_mods_opattrs( op, &op->orm_modlist, 1 );
903 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
904 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
906 entry = get_entry(op, &li->li_base_path);
909 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
910 if(rs->sr_err == LDAP_SUCCESS) {
912 spew_res = spew_entry(entry, &path, 1, &save_errno);
914 Debug( LDAP_DEBUG_ANY,
915 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
916 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
917 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
922 rs->sr_err = LDAP_NO_SUCH_OBJECT;
927 if(path.bv_val != NULL)
928 SLAP_FREE(path.bv_val);
930 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
931 send_ldap_result(op, rs);
932 slap_graduate_commit_csn( op );
936 static int ldif_back_delete(Operation *op, SlapReply *rs) {
937 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
938 struct berval path = BER_BVNULL;
941 if ( BER_BVISEMPTY( &op->o_csn )) {
943 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
946 csn.bv_len = sizeof( csnbuf );
947 slap_get_csn( op, &csn, 1 );
950 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
951 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
953 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
954 res = rmdir(path.bv_val);
955 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
956 rs->sr_err = LDAP_SUCCESS;
960 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
964 rs->sr_err = LDAP_NO_SUCH_OBJECT;
968 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
973 res = unlink(path.bv_val);
977 rs->sr_err = LDAP_NO_SUCH_OBJECT;
981 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
987 SLAP_FREE(path.bv_val);
988 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
989 send_ldap_result(op, rs);
990 slap_graduate_commit_csn( op );
995 static int move_entry(Entry * entry, struct berval * ndn,
996 struct berval * newndn, struct berval * suffixdn,
997 struct berval * base_path) {
1001 struct berval newpath;
1003 dn2path(ndn, suffixdn, base_path, &path);
1004 dn2path(newndn, suffixdn, base_path, &newpath);
1006 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1007 /* some object doesn't exist */
1008 res = LDAP_NO_SUCH_OBJECT;
1010 else { /* do the modrdn */
1011 exists_res = open(newpath.bv_val, O_RDONLY);
1012 if(exists_res == -1 && errno == ENOENT) {
1013 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1014 res = spew_entry(entry, &newpath, 0, NULL);
1016 /* if this fails we should log something bad */
1017 res = unlink(path.bv_val);
1018 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1019 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1020 res = rename(path.bv_val, newpath.bv_val);
1025 res = LDAP_NO_SUCH_OBJECT;
1027 res = LDAP_UNWILLING_TO_PERFORM;
1028 unlink(newpath.bv_val); /* in case file was created */
1030 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1032 else if(exists_res) {
1033 int close_res = close(exists_res);
1034 res = LDAP_ALREADY_EXISTS;
1035 if(close_res == -1) {
1036 /* log heinous error */
1040 res = LDAP_UNWILLING_TO_PERFORM;
1044 if(newpath.bv_val != NULL)
1045 SLAP_FREE(newpath.bv_val);
1046 if(path.bv_val != NULL)
1047 SLAP_FREE(path.bv_val);
1052 ldif_back_modrdn(Operation *op, SlapReply *rs)
1054 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1055 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1057 Entry * entry = NULL;
1060 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1062 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1063 entry = get_entry( op, &li->li_base_path );
1065 /* build the mods to the entry */
1066 if ( entry != NULL ) {
1067 /* build new dn, and new ndn for the entry */
1068 if ( op->oq_modrdn.rs_newSup != NULL ) {
1069 struct berval op_dn = op->o_req_dn,
1070 op_ndn = op->o_req_ndn;
1074 p_dn = *op->oq_modrdn.rs_newSup;
1075 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1076 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1077 np = get_entry( op, &li->li_base_path );
1078 op->o_req_dn = op_dn;
1079 op->o_req_ndn = op_ndn;
1081 goto no_such_object;
1085 dnParent( &entry->e_name, &p_dn );
1087 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1088 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1089 ber_memfree_x( entry->e_name.bv_val, NULL );
1090 ber_memfree_x( entry->e_nname.bv_val, NULL );
1091 entry->e_name = new_dn;
1092 entry->e_nname = new_ndn;
1094 /* perform the modifications */
1095 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1096 if ( res == LDAP_SUCCESS ) {
1097 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1099 &op->o_bd->be_nsuffix[0],
1100 &li->li_base_path );
1106 /* entry was null */
1107 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1110 if ( entry != NULL ) {
1111 entry_free( entry );
1114 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1115 send_ldap_result( op, rs );
1116 slap_graduate_commit_csn( op );
1120 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1122 int ldif_back_entry_get(
1126 AttributeDescription *at,
1130 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1131 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1133 assert( ndn != NULL );
1134 assert( !BER_BVISNULL( ndn ) );
1136 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1137 op->o_req_dn = *ndn;
1138 op->o_req_ndn = *ndn;
1139 *ent = get_entry( op, &li->li_base_path );
1140 op->o_req_dn = op_dn;
1141 op->o_req_ndn = op_ndn;
1142 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1144 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1149 return ( *ent == NULL ? 1 : 0 );
1152 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1153 struct ldif_info *li = (struct ldif_info *) be->be_private;
1154 li->li_tool_current = 0;
1158 static int ldif_tool_entry_close(BackendDB * be) {
1159 struct ldif_info *li = (struct ldif_info *) be->be_private;
1161 SLAP_FREE(li->li_tool_cookie.entries);
1166 ldif_tool_entry_first(BackendDB *be)
1168 struct ldif_info *li = (struct ldif_info *) be->be_private;
1169 ID id = 1; /* first entry in the array of entries shifted by one */
1171 li->li_tool_current = 1;
1172 if(li->li_tool_cookie.entries == NULL) {
1176 op.o_req_dn = *be->be_suffix;
1177 op.o_req_ndn = *be->be_nsuffix;
1178 op.ors_scope = LDAP_SCOPE_SUBTREE;
1179 li->li_tool_cookie.op = &op;
1180 (void)enum_tree( &li->li_tool_cookie );
1181 li->li_tool_cookie.op = NULL;
1186 static ID ldif_tool_entry_next(BackendDB *be)
1188 struct ldif_info *li = (struct ldif_info *) be->be_private;
1189 li->li_tool_current += 1;
1190 if(li->li_tool_current > li->li_tool_cookie.eind)
1193 return li->li_tool_current;
1196 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1197 struct ldif_info *li = (struct ldif_info *) be->be_private;
1200 if(id > li->li_tool_cookie.eind || id < 1)
1203 e = li->li_tool_cookie.entries[id - 1];
1204 li->li_tool_cookie.entries[id - 1] = NULL;
1209 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1210 struct ldif_info *li = (struct ldif_info *) be->be_private;
1211 struct berval dn = e->e_nname;
1212 struct berval leaf_path = BER_BVNULL;
1215 int res = LDAP_SUCCESS;
1217 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1219 if(leaf_path.bv_val != NULL) {
1220 struct berval base = BER_BVNULL;
1221 /* build path to container, and path to ldif of container */
1222 get_parent_path(&leaf_path, &base);
1224 statres = stat(base.bv_val, &stats); /* check if container exists */
1225 if(statres == -1 && errno == ENOENT) { /* container missing */
1226 base.bv_val[base.bv_len] = '.';
1227 statres = stat(base.bv_val, &stats); /* check for leaf node */
1228 base.bv_val[base.bv_len] = '\0';
1229 if(statres == -1 && errno == ENOENT) {
1230 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1232 else if(statres != -1) { /* create parent */
1233 int mkdirres = mkdir(base.bv_val, 0750);
1234 if(mkdirres == -1) {
1235 res = LDAP_UNWILLING_TO_PERFORM;
1239 res = LDAP_UNWILLING_TO_PERFORM;
1240 }/* container was possibly created, move on to add the entry */
1241 if(res == LDAP_SUCCESS) {
1242 statres = stat(leaf_path.bv_val, &stats);
1243 if(statres == -1 && errno == ENOENT) {
1244 res = spew_entry(e, &leaf_path, 0, NULL);
1246 else /* it already exists */
1247 res = LDAP_ALREADY_EXISTS;
1249 SLAP_FREE(base.bv_val);
1250 SLAP_FREE(leaf_path.bv_val);
1253 if(res == LDAP_SUCCESS) {
1261 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1263 struct ldif_info *li;
1265 li = ch_calloc( 1, sizeof(struct ldif_info) );
1266 be->be_private = li;
1267 be->be_cf_ocs = ldifocs;
1268 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1273 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1275 struct ldif_info *li = be->be_private;
1277 ch_free(li->li_base_path.bv_val);
1278 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1279 free( be->be_private );
1284 ldif_back_db_open( Backend *be, ConfigReply *cr)
1286 struct ldif_info *li = (struct ldif_info *) be->be_private;
1287 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1288 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1295 ldif_back_initialize(
1299 static char *controls[] = {
1300 LDAP_CONTROL_MANAGEDSAIT,
1306 SLAP_BFLAG_INCREMENT |
1307 SLAP_BFLAG_REFERRALS;
1309 bi->bi_controls = controls;
1316 bi->bi_db_init = ldif_back_db_init;
1317 bi->bi_db_config = config_generic_wrapper;
1318 bi->bi_db_open = ldif_back_db_open;
1319 bi->bi_db_close = 0;
1320 bi->bi_db_destroy = ldif_back_db_destroy;
1322 bi->bi_op_bind = ldif_back_bind;
1323 bi->bi_op_unbind = 0;
1324 bi->bi_op_search = ldif_back_search;
1325 bi->bi_op_compare = 0;
1326 bi->bi_op_modify = ldif_back_modify;
1327 bi->bi_op_modrdn = ldif_back_modrdn;
1328 bi->bi_op_add = ldif_back_add;
1329 bi->bi_op_delete = ldif_back_delete;
1330 bi->bi_op_abandon = 0;
1332 bi->bi_extended = 0;
1334 bi->bi_chk_referrals = ldif_back_referrals;
1336 bi->bi_connection_init = 0;
1337 bi->bi_connection_destroy = 0;
1339 bi->bi_entry_get_rw = ldif_back_entry_get;
1341 #if 0 /* NOTE: uncomment to completely disable access control */
1342 bi->bi_access_allowed = slap_access_always_allowed;
1345 bi->bi_tool_entry_open = ldif_tool_entry_open;
1346 bi->bi_tool_entry_close = ldif_tool_entry_close;
1347 bi->bi_tool_entry_first = ldif_tool_entry_first;
1348 bi->bi_tool_entry_next = ldif_tool_entry_next;
1349 bi->bi_tool_entry_get = ldif_tool_entry_get;
1350 bi->bi_tool_entry_put = ldif_tool_entry_put;
1351 bi->bi_tool_entry_reindex = 0;
1352 bi->bi_tool_sync = 0;
1354 bi->bi_tool_dn2id_get = 0;
1355 bi->bi_tool_entry_modify = 0;
1357 bi->bi_cf_ocs = ldifocs;
1359 rc = config_register_schema( ldifcfg, ldifocs );
1360 if ( rc ) return rc;