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 li = (struct ldif_info *) op->o_bd->be_private;
777 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
778 entry = get_entry(op, &li->li_base_path);
780 /* no object is found for them */
782 if(be_isroot_pw(op)) {
783 rs->sr_err = return_val = LDAP_SUCCESS;
785 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
790 /* they don't have userpassword */
791 if((a = attr_find(entry->e_attrs, password)) == NULL) {
792 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
797 /* authentication actually failed */
798 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
799 &rs->sr_text) != 0) {
800 rs->sr_err = LDAP_INVALID_CREDENTIALS;
805 /* let the front-end send success */
810 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
812 send_ldap_result( op, rs );
818 static int ldif_back_search(Operation *op, SlapReply *rs)
820 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
821 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
825 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
826 rs->sr_err = enum_tree( &ck );
827 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
828 send_ldap_result(op, rs);
833 static int ldif_back_add(Operation *op, SlapReply *rs) {
834 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
835 Entry * e = op->ora_e;
836 struct berval dn = e->e_nname;
837 struct berval leaf_path = BER_BVNULL;
840 char textbuf[SLAP_TEXT_BUFLEN];
842 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
844 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
845 &rs->sr_text, textbuf, sizeof( textbuf ) );
846 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
848 rs->sr_err = slap_add_opattrs( op,
849 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
850 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
852 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
854 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
856 if(leaf_path.bv_val != NULL) {
857 struct berval base = BER_BVNULL;
858 /* build path to container and ldif of container */
859 get_parent_path(&leaf_path, &base);
861 statres = stat(base.bv_val, &stats); /* check if container exists */
862 if(statres == -1 && errno == ENOENT) { /* container missing */
863 base.bv_val[base.bv_len] = '.';
864 statres = stat(base.bv_val, &stats); /* check for leaf node */
865 base.bv_val[base.bv_len] = '\0';
866 if(statres == -1 && errno == ENOENT) {
867 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
868 rs->sr_text = "Parent does not exist";
870 else if(statres != -1) { /* create parent */
871 int mkdirres = mkdir(base.bv_val, 0750);
873 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
874 rs->sr_text = "Could not create parent folder";
875 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
876 base.bv_val, STRERROR( errno ), 0 );
880 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
881 }/* container was possibly created, move on to add the entry */
882 if(rs->sr_err == LDAP_SUCCESS) {
883 statres = stat(leaf_path.bv_val, &stats);
884 if(statres == -1 && errno == ENOENT) {
885 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
887 else if ( statres == -1 ) {
888 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
889 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
890 leaf_path.bv_val, STRERROR( errno ), 0 );
892 else /* it already exists */
893 rs->sr_err = LDAP_ALREADY_EXISTS;
895 SLAP_FREE(base.bv_val);
896 SLAP_FREE(leaf_path.bv_val);
899 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
902 Debug( LDAP_DEBUG_TRACE,
903 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
904 rs->sr_text : "", 0);
905 send_ldap_result(op, rs);
906 slap_graduate_commit_csn( op );
910 static int ldif_back_modify(Operation *op, SlapReply *rs) {
911 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
912 Modifications * modlst = op->orm_modlist;
913 struct berval path = BER_BVNULL;
914 Entry * entry = NULL;
917 slap_mods_opattrs( op, &op->orm_modlist, 1 );
919 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
920 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
922 entry = get_entry(op, &li->li_base_path);
925 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
926 if(rs->sr_err == LDAP_SUCCESS) {
928 spew_res = spew_entry(entry, &path, 1, &save_errno);
930 Debug( LDAP_DEBUG_ANY,
931 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
932 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
933 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
938 rs->sr_err = LDAP_NO_SUCH_OBJECT;
943 if(path.bv_val != NULL)
944 SLAP_FREE(path.bv_val);
946 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
947 send_ldap_result(op, rs);
948 slap_graduate_commit_csn( op );
952 static int ldif_back_delete(Operation *op, SlapReply *rs) {
953 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
954 struct berval path = BER_BVNULL;
957 if ( BER_BVISEMPTY( &op->o_csn )) {
959 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
962 csn.bv_len = sizeof( csnbuf );
963 slap_get_csn( op, &csn, 1 );
966 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
967 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
969 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
970 res = rmdir(path.bv_val);
971 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
972 rs->sr_err = LDAP_SUCCESS;
976 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
980 rs->sr_err = LDAP_NO_SUCH_OBJECT;
984 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
989 res = unlink(path.bv_val);
993 rs->sr_err = LDAP_NO_SUCH_OBJECT;
997 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1003 SLAP_FREE(path.bv_val);
1004 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1005 send_ldap_result(op, rs);
1006 slap_graduate_commit_csn( op );
1011 static int move_entry(Entry * entry, struct berval * ndn,
1012 struct berval * newndn, struct berval * suffixdn,
1013 struct berval * base_path) {
1017 struct berval newpath;
1019 dn2path(ndn, suffixdn, base_path, &path);
1020 dn2path(newndn, suffixdn, base_path, &newpath);
1022 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1023 /* some object doesn't exist */
1024 res = LDAP_NO_SUCH_OBJECT;
1026 else { /* do the modrdn */
1027 exists_res = open(newpath.bv_val, O_RDONLY);
1028 if(exists_res == -1 && errno == ENOENT) {
1029 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1030 res = spew_entry(entry, &newpath, 0, NULL);
1032 /* if this fails we should log something bad */
1033 res = unlink(path.bv_val);
1034 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1035 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1036 res = rename(path.bv_val, newpath.bv_val);
1041 res = LDAP_NO_SUCH_OBJECT;
1043 res = LDAP_UNWILLING_TO_PERFORM;
1044 unlink(newpath.bv_val); /* in case file was created */
1046 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1048 else if(exists_res) {
1049 int close_res = close(exists_res);
1050 res = LDAP_ALREADY_EXISTS;
1051 if(close_res == -1) {
1052 /* log heinous error */
1056 res = LDAP_UNWILLING_TO_PERFORM;
1060 if(newpath.bv_val != NULL)
1061 SLAP_FREE(newpath.bv_val);
1062 if(path.bv_val != NULL)
1063 SLAP_FREE(path.bv_val);
1068 ldif_back_modrdn(Operation *op, SlapReply *rs)
1070 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1071 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1073 Entry * entry = NULL;
1076 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1078 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1079 entry = get_entry( op, &li->li_base_path );
1081 /* build the mods to the entry */
1082 if ( entry != NULL ) {
1083 /* build new dn, and new ndn for the entry */
1084 if ( op->oq_modrdn.rs_newSup != NULL ) {
1085 struct berval op_dn = op->o_req_dn,
1086 op_ndn = op->o_req_ndn;
1090 p_dn = *op->oq_modrdn.rs_newSup;
1091 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1092 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1093 np = get_entry( op, &li->li_base_path );
1094 op->o_req_dn = op_dn;
1095 op->o_req_ndn = op_ndn;
1097 goto no_such_object;
1101 dnParent( &entry->e_name, &p_dn );
1103 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1104 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1105 ber_memfree_x( entry->e_name.bv_val, NULL );
1106 ber_memfree_x( entry->e_nname.bv_val, NULL );
1107 entry->e_name = new_dn;
1108 entry->e_nname = new_ndn;
1110 /* perform the modifications */
1111 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1112 if ( res == LDAP_SUCCESS ) {
1113 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1115 &op->o_bd->be_nsuffix[0],
1116 &li->li_base_path );
1122 /* entry was null */
1123 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1126 if ( entry != NULL ) {
1127 entry_free( entry );
1130 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1131 send_ldap_result( op, rs );
1132 slap_graduate_commit_csn( op );
1136 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1138 int ldif_back_entry_get(
1142 AttributeDescription *at,
1146 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1147 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1149 assert( ndn != NULL );
1150 assert( !BER_BVISNULL( ndn ) );
1152 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1153 op->o_req_dn = *ndn;
1154 op->o_req_ndn = *ndn;
1155 *ent = get_entry( op, &li->li_base_path );
1156 op->o_req_dn = op_dn;
1157 op->o_req_ndn = op_ndn;
1158 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1160 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1165 return ( *ent == NULL ? 1 : 0 );
1168 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1169 struct ldif_info *li = (struct ldif_info *) be->be_private;
1170 li->li_tool_current = 0;
1174 static int ldif_tool_entry_close(BackendDB * be) {
1175 struct ldif_info *li = (struct ldif_info *) be->be_private;
1177 SLAP_FREE(li->li_tool_cookie.entries);
1181 static ID ldif_tool_entry_next(BackendDB *be)
1183 struct ldif_info *li = (struct ldif_info *) be->be_private;
1184 if(li->li_tool_current >= li->li_tool_cookie.eind)
1187 return ++li->li_tool_current;
1191 ldif_tool_entry_first(BackendDB *be)
1193 struct ldif_info *li = (struct ldif_info *) be->be_private;
1195 if(li->li_tool_cookie.entries == NULL) {
1199 op.o_req_dn = *be->be_suffix;
1200 op.o_req_ndn = *be->be_nsuffix;
1201 op.ors_scope = LDAP_SCOPE_SUBTREE;
1202 li->li_tool_cookie.op = &op;
1203 (void)enum_tree( &li->li_tool_cookie );
1204 li->li_tool_cookie.op = NULL;
1206 return ldif_tool_entry_next( be );
1209 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1210 struct ldif_info *li = (struct ldif_info *) be->be_private;
1213 if(id > li->li_tool_cookie.eind || id < 1)
1216 e = li->li_tool_cookie.entries[id - 1];
1217 li->li_tool_cookie.entries[id - 1] = NULL;
1222 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1223 struct ldif_info *li = (struct ldif_info *) be->be_private;
1224 struct berval dn = e->e_nname;
1225 struct berval leaf_path = BER_BVNULL;
1228 int res = LDAP_SUCCESS;
1230 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1232 if(leaf_path.bv_val != NULL) {
1233 struct berval base = BER_BVNULL;
1234 /* build path to container, and path to ldif of container */
1235 get_parent_path(&leaf_path, &base);
1237 statres = stat(base.bv_val, &stats); /* check if container exists */
1238 if(statres == -1 && errno == ENOENT) { /* container missing */
1239 base.bv_val[base.bv_len] = '.';
1240 statres = stat(base.bv_val, &stats); /* check for leaf node */
1241 base.bv_val[base.bv_len] = '\0';
1242 if(statres == -1 && errno == ENOENT) {
1243 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1245 else if(statres != -1) { /* create parent */
1246 int mkdirres = mkdir(base.bv_val, 0750);
1247 if(mkdirres == -1) {
1248 res = LDAP_UNWILLING_TO_PERFORM;
1252 res = LDAP_UNWILLING_TO_PERFORM;
1253 }/* container was possibly created, move on to add the entry */
1254 if(res == LDAP_SUCCESS) {
1255 statres = stat(leaf_path.bv_val, &stats);
1256 if(statres == -1 && errno == ENOENT) {
1257 res = spew_entry(e, &leaf_path, 0, NULL);
1259 else /* it already exists */
1260 res = LDAP_ALREADY_EXISTS;
1262 SLAP_FREE(base.bv_val);
1263 SLAP_FREE(leaf_path.bv_val);
1266 if(res == LDAP_SUCCESS) {
1274 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1276 struct ldif_info *li;
1278 li = ch_calloc( 1, sizeof(struct ldif_info) );
1279 be->be_private = li;
1280 be->be_cf_ocs = ldifocs;
1281 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1286 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1288 struct ldif_info *li = be->be_private;
1290 ch_free(li->li_base_path.bv_val);
1291 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1292 free( be->be_private );
1297 ldif_back_db_open( Backend *be, ConfigReply *cr)
1299 struct ldif_info *li = (struct ldif_info *) be->be_private;
1300 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1301 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1308 ldif_back_initialize(
1312 static char *controls[] = {
1313 LDAP_CONTROL_MANAGEDSAIT,
1319 SLAP_BFLAG_INCREMENT |
1320 SLAP_BFLAG_REFERRALS;
1322 bi->bi_controls = controls;
1329 bi->bi_db_init = ldif_back_db_init;
1330 bi->bi_db_config = config_generic_wrapper;
1331 bi->bi_db_open = ldif_back_db_open;
1332 bi->bi_db_close = 0;
1333 bi->bi_db_destroy = ldif_back_db_destroy;
1335 bi->bi_op_bind = ldif_back_bind;
1336 bi->bi_op_unbind = 0;
1337 bi->bi_op_search = ldif_back_search;
1338 bi->bi_op_compare = 0;
1339 bi->bi_op_modify = ldif_back_modify;
1340 bi->bi_op_modrdn = ldif_back_modrdn;
1341 bi->bi_op_add = ldif_back_add;
1342 bi->bi_op_delete = ldif_back_delete;
1343 bi->bi_op_abandon = 0;
1345 bi->bi_extended = 0;
1347 bi->bi_chk_referrals = ldif_back_referrals;
1349 bi->bi_connection_init = 0;
1350 bi->bi_connection_destroy = 0;
1352 bi->bi_entry_get_rw = ldif_back_entry_get;
1354 #if 0 /* NOTE: uncomment to completely disable access control */
1355 bi->bi_access_allowed = slap_access_always_allowed;
1358 bi->bi_tool_entry_open = ldif_tool_entry_open;
1359 bi->bi_tool_entry_close = ldif_tool_entry_close;
1360 bi->bi_tool_entry_first = ldif_tool_entry_first;
1361 bi->bi_tool_entry_next = ldif_tool_entry_next;
1362 bi->bi_tool_entry_get = ldif_tool_entry_get;
1363 bi->bi_tool_entry_put = ldif_tool_entry_put;
1364 bi->bi_tool_entry_reindex = 0;
1365 bi->bi_tool_sync = 0;
1367 bi->bi_tool_dn2id_get = 0;
1368 bi->bi_tool_entry_modify = 0;
1370 bi->bi_cf_ocs = ldifocs;
1372 rc = config_register_schema( ldifcfg, ldifocs );
1373 if ( rc ) return rc;