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[] = "tmpXXXXXX";
187 openres = mkstemp( tmpfname );
188 if ( openres == -1 ) {
190 rs = LDAP_UNWILLING_TO_PERFORM;
191 Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
192 tmpfname, STRERROR( save_errno ), 0 );
198 /* Only save the RDN onto disk */
199 dnRdn( &e->e_name, &rdn );
200 if ( rdn.bv_len != e->e_name.bv_len ) {
201 e->e_name.bv_val[rdn.bv_len] = '\0';
202 tmp = e->e_name.bv_len;
203 e->e_name.bv_len = rdn.bv_len;
208 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
211 entry_as_string = entry2str(e, &entry_length);
213 /* Restore full DN */
214 if ( rdn.bv_len != e->e_name.bv_len ) {
215 e->e_name.bv_val[e->e_name.bv_len] = ',';
216 e->e_name.bv_len = rdn.bv_len;
219 if ( entry_as_string == NULL ) {
220 rs = LDAP_UNWILLING_TO_PERFORM;
224 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
228 spew_res = spew_file( openres,
229 entry_as_string, entry_length );
230 if ( spew_res == -1 )
234 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
237 res = close( openres );
238 rs = LDAP_UNWILLING_TO_PERFORM;
239 if ( res == -1 || spew_res == -1 ) {
240 if ( save_errno == 0 )
242 Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
243 tmpfname, STRERROR( save_errno ), 0 );
245 res = rename( tmpfname, path->bv_val );
251 switch ( save_errno ) {
253 rs = LDAP_NO_SUCH_OBJECT;
263 if ( rs != LDAP_SUCCESS )
267 if ( rs != LDAP_SUCCESS )
268 *save_errnop = save_errno;
272 static Entry * get_entry_for_fd(int fd,
276 char * entry = (char *) slurp_file(fd);
277 Entry * ldentry = NULL;
279 /* error reading file */
284 ldentry = str2entry(entry);
287 rdn = ldentry->e_name;
288 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
289 ch_free( rdn.bv_val );
290 rdn = ldentry->e_nname;
291 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
292 ch_free( rdn.bv_val );
306 static Entry * get_entry(Operation *op, struct berval *base_path) {
307 struct berval path, pdn, pndn;
310 dnParent(&op->o_req_dn, &pdn);
311 dnParent(&op->o_req_ndn, &pndn);
312 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
313 fd = open(path.bv_val, O_RDONLY);
314 /* error opening file (mebbe should log error) */
315 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
316 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
317 path.bv_val, STRERROR(errno), 0 );
320 if(path.bv_val != NULL)
321 SLAP_FREE(path.bv_val);
324 return get_entry_for_fd(fd, &pdn, &pndn);
330 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
332 res->bv_len = name->bv_len + base->bv_len + 1;
333 res->bv_val = ch_malloc( res->bv_len + 1 );
334 strcpy(res->bv_val, base->bv_val);
335 ptr = res->bv_val + base->bv_len;
336 *ptr++ = LDAP_DIRSEP[0];
337 strcpy(ptr, name->bv_val);
340 typedef struct bvlist {
349 static int r_enum_tree(enumCookie *ck, struct berval *path,
350 struct berval *pdn, struct berval *pndn)
353 int fd, rc = LDAP_SUCCESS;
355 fd = open( path->bv_val, O_RDONLY );
357 Debug( LDAP_DEBUG_TRACE,
358 "=> ldif_enum_tree: failed to open %s: %s\n",
359 path->bv_val, STRERROR(errno), 0 );
360 return LDAP_NO_SUCH_OBJECT;
363 e = get_entry_for_fd(fd, pdn, pndn);
365 Debug( LDAP_DEBUG_ANY,
366 "=> ldif_enum_tree: failed to read entry for %s\n",
367 path->bv_val, 0, 0 );
371 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
372 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
373 /* Send right away? */
376 * if it's a referral, add it to the list of referrals. only do
377 * this for non-base searches, and don't check the filter
378 * explicitly here since it's only a candidate anyway.
380 if ( !get_manageDSAit( ck->op )
381 && ck->op->ors_scope != LDAP_SCOPE_BASE
382 && is_entry_referral( e ) )
384 BerVarray erefs = get_entry_referrals( ck->op, e );
385 ck->rs->sr_ref = referral_rewrite( erefs,
387 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
388 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
390 ck->rs->sr_entry = e;
391 rc = send_search_reference( ck->op, ck->rs );
392 ber_bvarray_free( ck->rs->sr_ref );
393 ber_bvarray_free( erefs );
394 ck->rs->sr_ref = NULL;
395 ck->rs->sr_entry = NULL;
397 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
399 ck->rs->sr_entry = e;
400 ck->rs->sr_attrs = ck->op->ors_attrs;
401 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
402 rc = send_search_entry(ck->op, ck->rs);
403 ck->rs->sr_entry = NULL;
409 /* Queueing up for tool mode */
410 if(ck->entries == NULL) {
411 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
412 ck->elen = ENTRY_BUFF_INCREMENT;
414 if(ck->eind >= ck->elen) { /* grow entries if necessary */
415 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
419 ck->entries[ck->eind++] = e;
426 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
428 bvlist *list = NULL, *ptr;
430 path->bv_len -= STRLENOF( LDIF );
431 path->bv_val[path->bv_len] = '\0';
433 dir_of_path = opendir(path->bv_val);
434 if(dir_of_path == NULL) { /* can't open directory */
435 if ( errno != ENOENT ) {
436 /* it shouldn't be treated as an error
437 * only if the directory doesn't exist */
439 Debug( LDAP_DEBUG_ANY,
440 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
441 path->bv_val, errno, 0 );
447 struct berval fname, itmp;
451 dir = readdir(dir_of_path);
452 if(dir == NULL) break; /* end of the directory */
453 fname.bv_len = strlen( dir->d_name );
454 if ( fname.bv_len <= STRLENOF( LDIF ))
456 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
458 fname.bv_val = dir->d_name;
460 bvl = ch_malloc( sizeof(bvlist) );
461 ber_dupbv( &bvl->bv, &fname );
462 BER_BVZERO( &bvl->num );
463 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
467 itmp.bv_len = bvl->bv.bv_len
468 - ( itmp.bv_val - bvl->bv.bv_val );
469 ptr = ber_bvchr( &itmp, IX_FSR );
471 itmp.bv_len = ptr - itmp.bv_val;
472 ber_dupbv( &bvl->num, &itmp );
473 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
474 itmp.bv_val[0] = '\0';
475 bvl->off = itmp.bv_val - bvl->bv.bv_val;
479 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
480 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
481 if ( !cmp && bvl->num.bv_val )
482 cmp = bvl->inum - ptr->inum;
490 closedir(dir_of_path);
492 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
493 ck->op->ors_scope = LDAP_SCOPE_BASE;
494 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
495 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
497 while ( ( ptr = list ) ) {
502 if ( rc == LDAP_SUCCESS ) {
503 if ( ptr->num.bv_val )
504 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
506 fullpath( path, &ptr->bv, &fpath );
507 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
510 if ( ptr->num.bv_val )
511 free( ptr->num.bv_val );
512 free(ptr->bv.bv_val);
517 if ( fd ) entry_free( e );
526 struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
528 struct berval pdn, pndn;
531 dnParent( &ck->op->o_req_dn, &pdn );
532 dnParent( &ck->op->o_req_ndn, &pndn );
533 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
534 rc = r_enum_tree(ck, &path, &pdn, &pndn);
535 ch_free( path.bv_val );
539 /* Get the parent path plus the LDIF suffix */
540 static void get_parent_path(struct berval * dnpath, struct berval *res) {
541 int dnpathlen = dnpath->bv_len;
544 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
545 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
548 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
549 strncpy(res->bv_val, dnpath->bv_val, i);
550 strcpy(res->bv_val+i, LDIF);
551 res->bv_val[i] = '\0';
554 static int apply_modify_to_entry(Entry * entry,
555 Modifications * modlist,
559 char textbuf[SLAP_TEXT_BUFLEN];
560 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
562 Modification *mods = NULL;
564 if (!acl_check_modlist(op, entry, modlist)) {
565 return LDAP_INSUFFICIENT_ACCESS;
568 for (; modlist != NULL; modlist = modlist->sml_next) {
569 mods = &modlist->sml_mod;
571 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
574 switch (mods->sm_op) {
576 rc = modify_add_values(entry, mods,
577 get_permissiveModify(op),
578 &rs->sr_text, textbuf,
582 case LDAP_MOD_DELETE:
583 rc = modify_delete_values(entry, mods,
584 get_permissiveModify(op),
585 &rs->sr_text, textbuf,
589 case LDAP_MOD_REPLACE:
590 rc = modify_replace_values(entry, mods,
591 get_permissiveModify(op),
592 &rs->sr_text, textbuf,
596 case LDAP_MOD_INCREMENT:
597 rc = modify_increment_values( entry,
598 mods, get_permissiveModify(op),
599 &rs->sr_text, textbuf,
605 case SLAP_MOD_SOFTADD:
606 mods->sm_op = LDAP_MOD_ADD;
607 rc = modify_add_values(entry, mods,
608 get_permissiveModify(op),
609 &rs->sr_text, textbuf,
611 mods->sm_op = SLAP_MOD_SOFTADD;
612 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
619 if(rc != LDAP_SUCCESS) break;
622 if(rc == LDAP_SUCCESS) {
624 entry->e_ocflags = 0;
626 /* check that the entry still obeys the schema */
627 rc = entry_schema_check( op, entry, NULL, 0, 0,
628 &rs->sr_text, textbuf, sizeof( textbuf ) );
635 ldif_back_referrals( Operation *op, SlapReply *rs )
637 struct ldif_info *li = NULL;
639 int rc = LDAP_SUCCESS;
642 if ( op->o_tag == LDAP_REQ_SEARCH ) {
643 /* let search take care of itself */
648 if ( get_manageDSAit( op ) ) {
649 /* let op take care of DSA management */
653 li = (struct ldif_info *)op->o_bd->be_private;
654 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
655 entry = get_entry( op, &li->li_base_path );
657 /* no object is found for them */
658 if ( entry == NULL ) {
659 struct berval odn = op->o_req_dn;
660 struct berval ondn = op->o_req_ndn;
662 struct berval pndn = op->o_req_ndn;
664 for ( ; entry == NULL; ) {
665 dnParent( &pndn, &pndn );
667 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
672 op->o_req_ndn = pndn;
674 entry = get_entry( op, &li->li_base_path );
677 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
680 op->o_req_ndn = ondn;
683 rs->sr_matched = NULL;
684 if ( entry != NULL ) {
685 Debug( LDAP_DEBUG_TRACE,
686 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
687 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
689 if ( is_entry_referral( entry ) ) {
691 rs->sr_ref = get_entry_referrals( op, entry );
693 rs->sr_matched = ber_strdup_x(
694 entry->e_name.bv_val, op->o_tmpmemctx );
700 } else if ( default_referral != NULL ) {
702 rs->sr_ref = referral_rewrite( default_referral,
703 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
706 if ( rs->sr_ref != NULL ) {
708 rc = rs->sr_err = LDAP_REFERRAL;
709 send_ldap_result( op, rs );
710 ber_bvarray_free( rs->sr_ref );
713 } else if ( rc != LDAP_SUCCESS ) {
715 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
716 send_ldap_result( op, rs );
719 if ( rs->sr_matched ) {
720 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
721 rs->sr_matched = NULL;
727 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
729 if ( is_entry_referral( entry ) ) {
730 /* entry is a referral */
731 BerVarray refs = get_entry_referrals( op, entry );
732 rs->sr_ref = referral_rewrite(
733 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
735 Debug( LDAP_DEBUG_TRACE,
736 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
737 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
739 rs->sr_matched = entry->e_name.bv_val;
740 if ( rs->sr_ref != NULL ) {
741 rc = rs->sr_err = LDAP_REFERRAL;
742 send_ldap_result( op, rs );
743 ber_bvarray_free( rs->sr_ref );
747 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
751 rs->sr_matched = NULL;
752 ber_bvarray_free( refs );
761 ldif_back_bind( Operation *op, SlapReply *rs )
763 struct ldif_info *li = NULL;
764 Attribute * a = NULL;
765 AttributeDescription *password = slap_schema.si_ad_userPassword;
767 Entry * entry = NULL;
769 li = (struct ldif_info *) op->o_bd->be_private;
770 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
771 entry = get_entry(op, &li->li_base_path);
773 /* no object is found for them */
775 if(be_isroot_pw(op)) {
776 rs->sr_err = return_val = LDAP_SUCCESS;
778 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
783 /* they don't have userpassword */
784 if((a = attr_find(entry->e_attrs, password)) == NULL) {
785 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
790 /* authentication actually failed */
791 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
792 &rs->sr_text) != 0) {
793 rs->sr_err = LDAP_INVALID_CREDENTIALS;
798 /* let the front-end send success */
803 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
805 send_ldap_result( op, rs );
811 static int ldif_back_search(Operation *op, SlapReply *rs)
813 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
814 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
818 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
819 rs->sr_err = enum_tree( &ck );
820 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
821 send_ldap_result(op, rs);
826 static int ldif_back_add(Operation *op, SlapReply *rs) {
827 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
828 Entry * e = op->ora_e;
829 struct berval dn = e->e_nname;
830 struct berval leaf_path = BER_BVNULL;
833 char textbuf[SLAP_TEXT_BUFLEN];
835 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
837 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
838 &rs->sr_text, textbuf, sizeof( textbuf ) );
839 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
841 rs->sr_err = slap_add_opattrs( op,
842 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
843 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
845 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
847 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
849 if(leaf_path.bv_val != NULL) {
850 struct berval base = BER_BVNULL;
851 /* build path to container and ldif of container */
852 get_parent_path(&leaf_path, &base);
854 statres = stat(base.bv_val, &stats); /* check if container exists */
855 if(statres == -1 && errno == ENOENT) { /* container missing */
856 base.bv_val[base.bv_len] = '.';
857 statres = stat(base.bv_val, &stats); /* check for leaf node */
858 base.bv_val[base.bv_len] = '\0';
859 if(statres == -1 && errno == ENOENT) {
860 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
861 rs->sr_text = "Parent does not exist";
863 else if(statres != -1) { /* create parent */
864 int mkdirres = mkdir(base.bv_val, 0750);
866 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
867 rs->sr_text = "Could not create parent folder";
868 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
869 base.bv_val, STRERROR( errno ), 0 );
873 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
874 }/* container was possibly created, move on to add the entry */
875 if(rs->sr_err == LDAP_SUCCESS) {
876 statres = stat(leaf_path.bv_val, &stats);
877 if(statres == -1 && errno == ENOENT) {
878 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
880 else if ( statres == -1 ) {
881 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
882 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
883 leaf_path.bv_val, STRERROR( errno ), 0 );
885 else /* it already exists */
886 rs->sr_err = LDAP_ALREADY_EXISTS;
888 SLAP_FREE(base.bv_val);
889 SLAP_FREE(leaf_path.bv_val);
892 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
895 Debug( LDAP_DEBUG_TRACE,
896 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
897 rs->sr_text : "", 0);
898 send_ldap_result(op, rs);
899 slap_graduate_commit_csn( op );
903 static int ldif_back_modify(Operation *op, SlapReply *rs) {
904 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
905 Modifications * modlst = op->orm_modlist;
906 struct berval path = BER_BVNULL;
907 Entry * entry = NULL;
910 slap_mods_opattrs( op, &op->orm_modlist, 1 );
912 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
913 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
915 entry = get_entry(op, &li->li_base_path);
918 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
919 if(rs->sr_err == LDAP_SUCCESS) {
921 spew_res = spew_entry(entry, &path, 1, &save_errno);
923 Debug( LDAP_DEBUG_ANY,
924 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
925 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
926 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
931 rs->sr_err = LDAP_NO_SUCH_OBJECT;
936 if(path.bv_val != NULL)
937 SLAP_FREE(path.bv_val);
939 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
940 send_ldap_result(op, rs);
941 slap_graduate_commit_csn( op );
945 static int ldif_back_delete(Operation *op, SlapReply *rs) {
946 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
947 struct berval path = BER_BVNULL;
950 if ( BER_BVISEMPTY( &op->o_csn )) {
952 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
955 csn.bv_len = sizeof( csnbuf );
956 slap_get_csn( op, &csn, 1 );
959 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
960 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
962 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
963 res = rmdir(path.bv_val);
964 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
965 rs->sr_err = LDAP_SUCCESS;
969 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
973 rs->sr_err = LDAP_NO_SUCH_OBJECT;
977 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
982 res = unlink(path.bv_val);
986 rs->sr_err = LDAP_NO_SUCH_OBJECT;
990 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
996 SLAP_FREE(path.bv_val);
997 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
998 send_ldap_result(op, rs);
999 slap_graduate_commit_csn( op );
1004 static int move_entry(Entry * entry, struct berval * ndn,
1005 struct berval * newndn, struct berval * suffixdn,
1006 struct berval * base_path) {
1010 struct berval newpath;
1012 dn2path(ndn, suffixdn, base_path, &path);
1013 dn2path(newndn, suffixdn, base_path, &newpath);
1015 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1016 /* some object doesn't exist */
1017 res = LDAP_NO_SUCH_OBJECT;
1019 else { /* do the modrdn */
1020 exists_res = open(newpath.bv_val, O_RDONLY);
1021 if(exists_res == -1 && errno == ENOENT) {
1022 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1023 res = spew_entry(entry, &newpath, 0, NULL);
1025 /* if this fails we should log something bad */
1026 res = unlink(path.bv_val);
1027 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1028 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1029 res = rename(path.bv_val, newpath.bv_val);
1034 res = LDAP_NO_SUCH_OBJECT;
1036 res = LDAP_UNWILLING_TO_PERFORM;
1037 unlink(newpath.bv_val); /* in case file was created */
1039 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1041 else if(exists_res) {
1042 int close_res = close(exists_res);
1043 res = LDAP_ALREADY_EXISTS;
1044 if(close_res == -1) {
1045 /* log heinous error */
1049 res = LDAP_UNWILLING_TO_PERFORM;
1053 if(newpath.bv_val != NULL)
1054 SLAP_FREE(newpath.bv_val);
1055 if(path.bv_val != NULL)
1056 SLAP_FREE(path.bv_val);
1061 ldif_back_modrdn(Operation *op, SlapReply *rs)
1063 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1064 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1066 Entry * entry = NULL;
1069 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1071 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1072 entry = get_entry( op, &li->li_base_path );
1074 /* build the mods to the entry */
1075 if ( entry != NULL ) {
1076 /* build new dn, and new ndn for the entry */
1077 if ( op->oq_modrdn.rs_newSup != NULL ) {
1078 struct berval op_dn = op->o_req_dn,
1079 op_ndn = op->o_req_ndn;
1083 p_dn = *op->oq_modrdn.rs_newSup;
1084 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1085 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1086 np = get_entry( op, &li->li_base_path );
1087 op->o_req_dn = op_dn;
1088 op->o_req_ndn = op_ndn;
1090 goto no_such_object;
1094 dnParent( &entry->e_name, &p_dn );
1096 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1097 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1098 ber_memfree_x( entry->e_name.bv_val, NULL );
1099 ber_memfree_x( entry->e_nname.bv_val, NULL );
1100 entry->e_name = new_dn;
1101 entry->e_nname = new_ndn;
1103 /* perform the modifications */
1104 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1105 if ( res == LDAP_SUCCESS ) {
1106 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1108 &op->o_bd->be_nsuffix[0],
1109 &li->li_base_path );
1115 /* entry was null */
1116 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1119 if ( entry != NULL ) {
1120 entry_free( entry );
1123 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1124 send_ldap_result( op, rs );
1125 slap_graduate_commit_csn( op );
1129 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1131 int ldif_back_entry_get(
1135 AttributeDescription *at,
1139 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1140 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1142 assert( ndn != NULL );
1143 assert( !BER_BVISNULL( ndn ) );
1145 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1146 op->o_req_dn = *ndn;
1147 op->o_req_ndn = *ndn;
1148 *ent = get_entry( op, &li->li_base_path );
1149 op->o_req_dn = op_dn;
1150 op->o_req_ndn = op_ndn;
1151 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1153 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1158 return ( *ent == NULL ? 1 : 0 );
1161 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1162 struct ldif_info *li = (struct ldif_info *) be->be_private;
1163 li->li_tool_current = 0;
1167 static int ldif_tool_entry_close(BackendDB * be) {
1168 struct ldif_info *li = (struct ldif_info *) be->be_private;
1170 SLAP_FREE(li->li_tool_cookie.entries);
1174 static ID ldif_tool_entry_next(BackendDB *be)
1176 struct ldif_info *li = (struct ldif_info *) be->be_private;
1177 if(li->li_tool_current >= li->li_tool_cookie.eind)
1180 return ++li->li_tool_current;
1184 ldif_tool_entry_first(BackendDB *be)
1186 struct ldif_info *li = (struct ldif_info *) be->be_private;
1188 if(li->li_tool_cookie.entries == NULL) {
1192 op.o_req_dn = *be->be_suffix;
1193 op.o_req_ndn = *be->be_nsuffix;
1194 op.ors_scope = LDAP_SCOPE_SUBTREE;
1195 li->li_tool_cookie.op = &op;
1196 (void)enum_tree( &li->li_tool_cookie );
1197 li->li_tool_cookie.op = NULL;
1199 return ldif_tool_entry_next( be );
1202 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1203 struct ldif_info *li = (struct ldif_info *) be->be_private;
1206 if(id > li->li_tool_cookie.eind || id < 1)
1209 e = li->li_tool_cookie.entries[id - 1];
1210 li->li_tool_cookie.entries[id - 1] = NULL;
1215 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1216 struct ldif_info *li = (struct ldif_info *) be->be_private;
1217 struct berval dn = e->e_nname;
1218 struct berval leaf_path = BER_BVNULL;
1221 int res = LDAP_SUCCESS;
1223 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1225 if(leaf_path.bv_val != NULL) {
1226 struct berval base = BER_BVNULL;
1227 /* build path to container, and path to ldif of container */
1228 get_parent_path(&leaf_path, &base);
1230 statres = stat(base.bv_val, &stats); /* check if container exists */
1231 if(statres == -1 && errno == ENOENT) { /* container missing */
1232 base.bv_val[base.bv_len] = '.';
1233 statres = stat(base.bv_val, &stats); /* check for leaf node */
1234 base.bv_val[base.bv_len] = '\0';
1235 if(statres == -1 && errno == ENOENT) {
1236 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1238 else if(statres != -1) { /* create parent */
1239 int mkdirres = mkdir(base.bv_val, 0750);
1240 if(mkdirres == -1) {
1241 res = LDAP_UNWILLING_TO_PERFORM;
1245 res = LDAP_UNWILLING_TO_PERFORM;
1246 }/* container was possibly created, move on to add the entry */
1247 if(res == LDAP_SUCCESS) {
1248 statres = stat(leaf_path.bv_val, &stats);
1249 if(statres == -1 && errno == ENOENT) {
1250 res = spew_entry(e, &leaf_path, 0, NULL);
1252 else /* it already exists */
1253 res = LDAP_ALREADY_EXISTS;
1255 SLAP_FREE(base.bv_val);
1256 SLAP_FREE(leaf_path.bv_val);
1259 if(res == LDAP_SUCCESS) {
1267 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1269 struct ldif_info *li;
1271 li = ch_calloc( 1, sizeof(struct ldif_info) );
1272 be->be_private = li;
1273 be->be_cf_ocs = ldifocs;
1274 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1279 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1281 struct ldif_info *li = be->be_private;
1283 ch_free(li->li_base_path.bv_val);
1284 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1285 free( be->be_private );
1290 ldif_back_db_open( Backend *be, ConfigReply *cr)
1292 struct ldif_info *li = (struct ldif_info *) be->be_private;
1293 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1294 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1301 ldif_back_initialize(
1305 static char *controls[] = {
1306 LDAP_CONTROL_MANAGEDSAIT,
1312 SLAP_BFLAG_INCREMENT |
1313 SLAP_BFLAG_REFERRALS;
1315 bi->bi_controls = controls;
1322 bi->bi_db_init = ldif_back_db_init;
1323 bi->bi_db_config = config_generic_wrapper;
1324 bi->bi_db_open = ldif_back_db_open;
1325 bi->bi_db_close = 0;
1326 bi->bi_db_destroy = ldif_back_db_destroy;
1328 bi->bi_op_bind = ldif_back_bind;
1329 bi->bi_op_unbind = 0;
1330 bi->bi_op_search = ldif_back_search;
1331 bi->bi_op_compare = 0;
1332 bi->bi_op_modify = ldif_back_modify;
1333 bi->bi_op_modrdn = ldif_back_modrdn;
1334 bi->bi_op_add = ldif_back_add;
1335 bi->bi_op_delete = ldif_back_delete;
1336 bi->bi_op_abandon = 0;
1338 bi->bi_extended = 0;
1340 bi->bi_chk_referrals = ldif_back_referrals;
1342 bi->bi_connection_init = 0;
1343 bi->bi_connection_destroy = 0;
1345 bi->bi_entry_get_rw = ldif_back_entry_get;
1347 #if 0 /* NOTE: uncomment to completely disable access control */
1348 bi->bi_access_allowed = slap_access_always_allowed;
1351 bi->bi_tool_entry_open = ldif_tool_entry_open;
1352 bi->bi_tool_entry_close = ldif_tool_entry_close;
1353 bi->bi_tool_entry_first = ldif_tool_entry_first;
1354 bi->bi_tool_entry_next = ldif_tool_entry_next;
1355 bi->bi_tool_entry_get = ldif_tool_entry_get;
1356 bi->bi_tool_entry_put = ldif_tool_entry_put;
1357 bi->bi_tool_entry_reindex = 0;
1358 bi->bi_tool_sync = 0;
1360 bi->bi_tool_dn2id_get = 0;
1361 bi->bi_tool_entry_modify = 0;
1363 bi->bi_cf_ocs = ldifocs;
1365 rc = config_register_schema( ldifcfg, ldifocs );
1366 if ( rc ) return rc;