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 * orig_dn, struct berval * suffixdn, struct berval * base_path,
88 char *ptr, *sep, *end;
92 assert( orig_dn != NULL );
93 assert( !BER_BVISNULL( orig_dn ) );
94 assert( suffixdn != NULL );
95 assert( !BER_BVISNULL( suffixdn ) );
96 assert( dnIsSuffix( orig_dn, suffixdn ) );
100 for ( ptr = dn.bv_val, end = &dn.bv_val[dn.bv_len]; ptr < end; ptr++) {
101 if ( ptr[0] == LDAP_DIRSEP[0] ) {
110 dn.bv_val = ch_malloc( dn.bv_len + 1 );
112 for ( ptr = orig_dn->bv_val, end = &orig_dn->bv_val[orig_dn->bv_len], p = dn.bv_val;
113 ptr < end; ptr++, p++)
115 static const char hex[] = "0123456789ABCDEF";
116 if ( ptr[0] == LDAP_DIRSEP[0] ) {
117 *p++ = '\\'; /* FIXME: fs-escape */
118 *p++ = hex[(LDAP_DIRSEP[0] & 0xF0U) >> 4];
119 *p = hex[LDAP_DIRSEP[0] & 0x0FU];
127 res->bv_len = dn.bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
128 res->bv_val = ch_malloc( res->bv_len + 1 );
129 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
130 *ptr++ = LDAP_DIRSEP[0];
131 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
132 end = dn.bv_val + dn.bv_len - suffixdn->bv_len - 1;
133 while ( end > dn.bv_val ) {
134 for (sep = end-1; sep >= dn.bv_val && !DN_SEPARATOR( *sep ); sep--);
135 *ptr++ = LDAP_DIRSEP[0];
136 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
144 while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
146 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
147 bv.bv_len -= ( ptr - bv.bv_val );
149 ptr = ber_bvchr( &bv, IX_DNR );
153 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
154 bv.bv_len -= ( ptr - bv.bv_val );
159 if ( dn.bv_val != orig_dn->bv_val ) {
160 ch_free( dn.bv_val );
164 static char * slurp_file(int fd) {
165 int read_chars_total = 0;
173 entry_size = st.st_size;
174 entry = ch_malloc( entry_size+1 );
178 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
179 if(read_chars == -1) {
183 if(read_chars == 0) {
184 entry[read_chars_total] = '\0';
188 read_chars_total += read_chars;
189 entry_pos += read_chars;
196 * return number of bytes written, or -1 in case of error
197 * do not return numbers less than -1
199 static int spew_file(int fd, char * spew, int len) {
203 writeres = write(fd, spew, len);
216 spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
218 int rs, save_errno = 0;
222 char * entry_as_string;
223 char *tmpfname = NULL;
225 tmpfname = ch_malloc( path->bv_len + STRLENOF( "XXXXXX" ) + 1 );
226 AC_MEMCPY( tmpfname, path->bv_val, path->bv_len );
227 AC_MEMCPY( &tmpfname[ path->bv_len ], "XXXXXX", STRLENOF( "XXXXXX" ) + 1 );
229 openres = mkstemp( tmpfname );
230 if ( openres == -1 ) {
232 rs = LDAP_UNWILLING_TO_PERFORM;
233 Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
234 tmpfname, STRERROR( save_errno ), 0 );
240 /* Only save the RDN onto disk */
241 dnRdn( &e->e_name, &rdn );
242 if ( rdn.bv_len != e->e_name.bv_len ) {
243 e->e_name.bv_val[rdn.bv_len] = '\0';
244 tmp = e->e_name.bv_len;
245 e->e_name.bv_len = rdn.bv_len;
251 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
254 entry_as_string = entry2str(e, &entry_length);
255 if ( entry_as_string != NULL ) {
256 spew_res = spew_file( openres,
257 entry_as_string, entry_length );
258 if ( spew_res == -1 ) {
264 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
267 /* Restore full DN */
268 if ( rdn.bv_len != e->e_name.bv_len ) {
269 e->e_name.bv_val[e->e_name.bv_len] = ',';
270 e->e_name.bv_len = rdn.bv_len;
273 res = close( openres );
274 rs = LDAP_UNWILLING_TO_PERFORM;
276 if ( spew_res > -2 ) {
277 if ( res == -1 || spew_res == -1 ) {
278 if ( save_errno == 0 ) {
281 Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
282 tmpfname, STRERROR( save_errno ), 0 );
285 res = rename( tmpfname, path->bv_val );
291 switch ( save_errno ) {
293 rs = LDAP_NO_SUCH_OBJECT;
303 if ( rs != LDAP_SUCCESS ) {
310 if ( rs != LDAP_SUCCESS && save_errnop != NULL ) {
311 *save_errnop = save_errno;
317 static Entry * get_entry_for_fd(int fd,
321 char * entry = (char *) slurp_file(fd);
322 Entry * ldentry = NULL;
324 /* error reading file */
329 ldentry = str2entry(entry);
332 rdn = ldentry->e_name;
333 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
334 ch_free( rdn.bv_val );
335 rdn = ldentry->e_nname;
336 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
337 ch_free( rdn.bv_val );
351 static Entry * get_entry(Operation *op, struct berval *base_path) {
352 struct berval path, pdn, pndn;
355 dnParent(&op->o_req_dn, &pdn);
356 dnParent(&op->o_req_ndn, &pndn);
357 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
358 fd = open(path.bv_val, O_RDONLY);
359 /* error opening file (mebbe should log error) */
360 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
361 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
362 path.bv_val, STRERROR(errno), 0 );
365 if(path.bv_val != NULL)
366 SLAP_FREE(path.bv_val);
369 return get_entry_for_fd(fd, &pdn, &pndn);
375 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
377 res->bv_len = name->bv_len + base->bv_len + 1;
378 res->bv_val = ch_malloc( res->bv_len + 1 );
379 strcpy(res->bv_val, base->bv_val);
380 ptr = res->bv_val + base->bv_len;
381 *ptr++ = LDAP_DIRSEP[0];
382 strcpy(ptr, name->bv_val);
385 typedef struct bvlist {
394 static int r_enum_tree(enumCookie *ck, struct berval *path,
395 struct berval *pdn, struct berval *pndn)
398 int fd, rc = LDAP_SUCCESS;
400 fd = open( path->bv_val, O_RDONLY );
402 Debug( LDAP_DEBUG_TRACE,
403 "=> ldif_enum_tree: failed to open %s: %s\n",
404 path->bv_val, STRERROR(errno), 0 );
405 return LDAP_NO_SUCH_OBJECT;
408 e = get_entry_for_fd(fd, pdn, pndn);
410 Debug( LDAP_DEBUG_ANY,
411 "=> ldif_enum_tree: failed to read entry for %s\n",
412 path->bv_val, 0, 0 );
416 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
417 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
418 /* Send right away? */
421 * if it's a referral, add it to the list of referrals. only do
422 * this for non-base searches, and don't check the filter
423 * explicitly here since it's only a candidate anyway.
425 if ( !get_manageDSAit( ck->op )
426 && ck->op->ors_scope != LDAP_SCOPE_BASE
427 && is_entry_referral( e ) )
429 BerVarray erefs = get_entry_referrals( ck->op, e );
430 ck->rs->sr_ref = referral_rewrite( erefs,
432 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
433 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
435 ck->rs->sr_entry = e;
436 rc = send_search_reference( ck->op, ck->rs );
437 ber_bvarray_free( ck->rs->sr_ref );
438 ber_bvarray_free( erefs );
439 ck->rs->sr_ref = NULL;
440 ck->rs->sr_entry = NULL;
442 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
444 ck->rs->sr_entry = e;
445 ck->rs->sr_attrs = ck->op->ors_attrs;
446 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
447 rc = send_search_entry(ck->op, ck->rs);
448 ck->rs->sr_entry = NULL;
454 /* Queueing up for tool mode */
455 if(ck->entries == NULL) {
456 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
457 ck->elen = ENTRY_BUFF_INCREMENT;
459 if(ck->eind >= ck->elen) { /* grow entries if necessary */
460 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
464 ck->entries[ck->eind++] = e;
471 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
473 bvlist *list = NULL, *ptr;
475 path->bv_len -= STRLENOF( LDIF );
476 path->bv_val[path->bv_len] = '\0';
478 dir_of_path = opendir(path->bv_val);
479 if(dir_of_path == NULL) { /* can't open directory */
480 if ( errno != ENOENT ) {
481 /* it shouldn't be treated as an error
482 * only if the directory doesn't exist */
484 Debug( LDAP_DEBUG_ANY,
485 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
486 path->bv_val, errno, 0 );
492 struct berval fname, itmp;
496 dir = readdir(dir_of_path);
497 if(dir == NULL) break; /* end of the directory */
498 fname.bv_len = strlen( dir->d_name );
499 if ( fname.bv_len <= STRLENOF( LDIF ))
501 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
503 fname.bv_val = dir->d_name;
505 bvl = ch_malloc( sizeof(bvlist) );
506 ber_dupbv( &bvl->bv, &fname );
507 BER_BVZERO( &bvl->num );
508 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
512 itmp.bv_len = bvl->bv.bv_len
513 - ( itmp.bv_val - bvl->bv.bv_val );
514 ptr = ber_bvchr( &itmp, IX_FSR );
516 itmp.bv_len = ptr - itmp.bv_val;
517 ber_dupbv( &bvl->num, &itmp );
518 bvl->inum = strtol( itmp.bv_val, NULL, 0 );
519 itmp.bv_val[0] = '\0';
520 bvl->off = itmp.bv_val - bvl->bv.bv_val;
524 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
525 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
526 if ( !cmp && bvl->num.bv_val )
527 cmp = bvl->inum - ptr->inum;
535 closedir(dir_of_path);
537 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
538 ck->op->ors_scope = LDAP_SCOPE_BASE;
539 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
540 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
542 while ( ( ptr = list ) ) {
547 if ( rc == LDAP_SUCCESS ) {
548 if ( ptr->num.bv_val )
549 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
551 fullpath( path, &ptr->bv, &fpath );
552 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
555 if ( ptr->num.bv_val )
556 free( ptr->num.bv_val );
557 free(ptr->bv.bv_val);
562 if ( fd ) entry_free( e );
571 struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
573 struct berval pdn, pndn;
576 dnParent( &ck->op->o_req_dn, &pdn );
577 dnParent( &ck->op->o_req_ndn, &pndn );
578 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
579 rc = r_enum_tree(ck, &path, &pdn, &pndn);
580 ch_free( path.bv_val );
584 /* Get the parent path plus the LDIF suffix */
585 static void get_parent_path(struct berval * dnpath, struct berval *res) {
586 int dnpathlen = dnpath->bv_len;
589 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
590 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
593 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
594 strncpy(res->bv_val, dnpath->bv_val, i);
595 strcpy(res->bv_val+i, LDIF);
596 res->bv_val[i] = '\0';
599 static int apply_modify_to_entry(Entry * entry,
600 Modifications * modlist,
604 char textbuf[SLAP_TEXT_BUFLEN];
605 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
607 Modification *mods = NULL;
609 if (!acl_check_modlist(op, entry, modlist)) {
610 return LDAP_INSUFFICIENT_ACCESS;
613 for (; modlist != NULL; modlist = modlist->sml_next) {
614 mods = &modlist->sml_mod;
616 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
619 switch (mods->sm_op) {
621 rc = modify_add_values(entry, mods,
622 get_permissiveModify(op),
623 &rs->sr_text, textbuf,
627 case LDAP_MOD_DELETE:
628 rc = modify_delete_values(entry, mods,
629 get_permissiveModify(op),
630 &rs->sr_text, textbuf,
634 case LDAP_MOD_REPLACE:
635 rc = modify_replace_values(entry, mods,
636 get_permissiveModify(op),
637 &rs->sr_text, textbuf,
641 case LDAP_MOD_INCREMENT:
642 rc = modify_increment_values( entry,
643 mods, get_permissiveModify(op),
644 &rs->sr_text, textbuf,
650 case SLAP_MOD_SOFTADD:
651 mods->sm_op = LDAP_MOD_ADD;
652 rc = modify_add_values(entry, mods,
653 get_permissiveModify(op),
654 &rs->sr_text, textbuf,
656 mods->sm_op = SLAP_MOD_SOFTADD;
657 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
664 if(rc != LDAP_SUCCESS) break;
667 if(rc == LDAP_SUCCESS) {
669 entry->e_ocflags = 0;
671 /* check that the entry still obeys the schema */
672 rc = entry_schema_check( op, entry, NULL, 0, 0,
673 &rs->sr_text, textbuf, sizeof( textbuf ) );
680 ldif_back_referrals( Operation *op, SlapReply *rs )
682 struct ldif_info *li = NULL;
684 int rc = LDAP_SUCCESS;
687 if ( op->o_tag == LDAP_REQ_SEARCH ) {
688 /* let search take care of itself */
693 if ( get_manageDSAit( op ) ) {
694 /* let op take care of DSA management */
698 li = (struct ldif_info *)op->o_bd->be_private;
699 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
700 entry = get_entry( op, &li->li_base_path );
702 /* no object is found for them */
703 if ( entry == NULL ) {
704 struct berval odn = op->o_req_dn;
705 struct berval ondn = op->o_req_ndn;
707 struct berval pndn = op->o_req_ndn;
709 for ( ; entry == NULL; ) {
710 dnParent( &pndn, &pndn );
712 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
717 op->o_req_ndn = pndn;
719 entry = get_entry( op, &li->li_base_path );
722 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
725 op->o_req_ndn = ondn;
728 rs->sr_matched = NULL;
729 if ( entry != NULL ) {
730 Debug( LDAP_DEBUG_TRACE,
731 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
732 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
734 if ( is_entry_referral( entry ) ) {
736 rs->sr_ref = get_entry_referrals( op, entry );
738 rs->sr_matched = ber_strdup_x(
739 entry->e_name.bv_val, op->o_tmpmemctx );
745 } else if ( default_referral != NULL ) {
747 rs->sr_ref = referral_rewrite( default_referral,
748 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
751 if ( rs->sr_ref != NULL ) {
753 rc = rs->sr_err = LDAP_REFERRAL;
754 send_ldap_result( op, rs );
755 ber_bvarray_free( rs->sr_ref );
758 } else if ( rc != LDAP_SUCCESS ) {
760 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
761 send_ldap_result( op, rs );
764 if ( rs->sr_matched ) {
765 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
766 rs->sr_matched = NULL;
772 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
774 if ( is_entry_referral( entry ) ) {
775 /* entry is a referral */
776 BerVarray refs = get_entry_referrals( op, entry );
777 rs->sr_ref = referral_rewrite(
778 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
780 Debug( LDAP_DEBUG_TRACE,
781 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
782 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
784 rs->sr_matched = entry->e_name.bv_val;
785 if ( rs->sr_ref != NULL ) {
786 rc = rs->sr_err = LDAP_REFERRAL;
787 send_ldap_result( op, rs );
788 ber_bvarray_free( rs->sr_ref );
792 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
796 rs->sr_matched = NULL;
797 ber_bvarray_free( refs );
806 ldif_back_bind( Operation *op, SlapReply *rs )
808 struct ldif_info *li = NULL;
809 Attribute * a = NULL;
810 AttributeDescription *password = slap_schema.si_ad_userPassword;
812 Entry * entry = NULL;
814 switch ( be_rootdn_bind( op, rs ) ) {
815 case SLAP_CB_CONTINUE:
819 /* in case of success, front end will send result;
820 * otherwise, be_rootdn_bind() did */
824 li = (struct ldif_info *) op->o_bd->be_private;
825 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
826 entry = get_entry(op, &li->li_base_path);
828 /* no object is found for them */
830 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
834 /* they don't have userpassword */
835 if((a = attr_find(entry->e_attrs, password)) == NULL) {
836 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
841 /* authentication actually failed */
842 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
843 &rs->sr_text) != 0) {
844 rs->sr_err = LDAP_INVALID_CREDENTIALS;
849 /* let the front-end send success */
854 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
856 send_ldap_result( op, rs );
862 static int ldif_back_search(Operation *op, SlapReply *rs)
864 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
865 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
869 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
870 rs->sr_err = enum_tree( &ck );
871 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
872 send_ldap_result(op, rs);
877 static int ldif_back_add(Operation *op, SlapReply *rs) {
878 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
879 Entry * e = op->ora_e;
880 struct berval dn = e->e_nname;
881 struct berval leaf_path = BER_BVNULL;
884 char textbuf[SLAP_TEXT_BUFLEN];
886 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
888 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
889 &rs->sr_text, textbuf, sizeof( textbuf ) );
890 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
892 rs->sr_err = slap_add_opattrs( op,
893 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
894 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
896 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
898 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
900 if(leaf_path.bv_val != NULL) {
901 struct berval base = BER_BVNULL;
902 /* build path to container and ldif of container */
903 get_parent_path(&leaf_path, &base);
905 statres = stat(base.bv_val, &stats); /* check if container exists */
906 if(statres == -1 && errno == ENOENT) { /* container missing */
907 base.bv_val[base.bv_len] = '.';
908 statres = stat(base.bv_val, &stats); /* check for leaf node */
909 base.bv_val[base.bv_len] = '\0';
910 if(statres == -1 && errno == ENOENT) {
911 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
912 rs->sr_text = "Parent does not exist";
914 else if(statres != -1) { /* create parent */
915 int mkdirres = mkdir(base.bv_val, 0750);
917 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
918 rs->sr_text = "Could not create parent folder";
919 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
920 base.bv_val, STRERROR( errno ), 0 );
924 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
925 }/* container was possibly created, move on to add the entry */
926 if(rs->sr_err == LDAP_SUCCESS) {
927 statres = stat(leaf_path.bv_val, &stats);
928 if(statres == -1 && errno == ENOENT) {
929 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
931 else if ( statres == -1 ) {
932 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
933 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
934 leaf_path.bv_val, STRERROR( errno ), 0 );
936 else /* it already exists */
937 rs->sr_err = LDAP_ALREADY_EXISTS;
939 SLAP_FREE(base.bv_val);
940 SLAP_FREE(leaf_path.bv_val);
943 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
946 Debug( LDAP_DEBUG_TRACE,
947 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
948 rs->sr_text : "", 0);
949 send_ldap_result(op, rs);
950 slap_graduate_commit_csn( op );
954 static int ldif_back_modify(Operation *op, SlapReply *rs) {
955 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
956 Modifications * modlst = op->orm_modlist;
957 struct berval path = BER_BVNULL;
958 Entry * entry = NULL;
961 slap_mods_opattrs( op, &op->orm_modlist, 1 );
963 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
964 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
966 entry = get_entry(op, &li->li_base_path);
969 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
970 if(rs->sr_err == LDAP_SUCCESS) {
972 spew_res = spew_entry(entry, &path, 1, &save_errno);
974 Debug( LDAP_DEBUG_ANY,
975 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
976 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
977 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
982 rs->sr_err = LDAP_NO_SUCH_OBJECT;
987 if(path.bv_val != NULL)
988 SLAP_FREE(path.bv_val);
990 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
991 send_ldap_result(op, rs);
992 slap_graduate_commit_csn( op );
996 static int ldif_back_delete(Operation *op, SlapReply *rs) {
997 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
998 struct berval path = BER_BVNULL;
1001 if ( BER_BVISEMPTY( &op->o_csn )) {
1003 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1005 csn.bv_val = csnbuf;
1006 csn.bv_len = sizeof( csnbuf );
1007 slap_get_csn( op, &csn, 1 );
1010 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
1011 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
1013 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
1014 res = rmdir(path.bv_val);
1015 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
1016 rs->sr_err = LDAP_SUCCESS;
1020 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
1024 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1028 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1033 res = unlink(path.bv_val);
1037 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1041 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1047 SLAP_FREE(path.bv_val);
1048 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1049 send_ldap_result(op, rs);
1050 slap_graduate_commit_csn( op );
1055 static int move_entry(Entry * entry, struct berval * ndn,
1056 struct berval * newndn, struct berval * suffixdn,
1057 struct berval * base_path) {
1061 struct berval newpath;
1063 dn2path(ndn, suffixdn, base_path, &path);
1064 dn2path(newndn, suffixdn, base_path, &newpath);
1066 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1067 /* some object doesn't exist */
1068 res = LDAP_NO_SUCH_OBJECT;
1070 else { /* do the modrdn */
1071 exists_res = open(newpath.bv_val, O_RDONLY);
1072 if(exists_res == -1 && errno == ENOENT) {
1073 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1074 res = spew_entry(entry, &newpath, 0, NULL);
1076 /* if this fails we should log something bad */
1077 res = unlink(path.bv_val);
1078 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1079 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1080 res = rename(path.bv_val, newpath.bv_val);
1085 res = LDAP_NO_SUCH_OBJECT;
1087 res = LDAP_UNWILLING_TO_PERFORM;
1088 unlink(newpath.bv_val); /* in case file was created */
1090 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1092 else if(exists_res) {
1093 int close_res = close(exists_res);
1094 res = LDAP_ALREADY_EXISTS;
1095 if(close_res == -1) {
1096 /* log heinous error */
1100 res = LDAP_UNWILLING_TO_PERFORM;
1104 if(newpath.bv_val != NULL)
1105 SLAP_FREE(newpath.bv_val);
1106 if(path.bv_val != NULL)
1107 SLAP_FREE(path.bv_val);
1112 ldif_back_modrdn(Operation *op, SlapReply *rs)
1114 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1115 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1117 Entry * entry = NULL;
1120 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1122 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1123 entry = get_entry( op, &li->li_base_path );
1125 /* build the mods to the entry */
1126 if ( entry != NULL ) {
1127 /* build new dn, and new ndn for the entry */
1128 if ( op->oq_modrdn.rs_newSup != NULL ) {
1129 struct berval op_dn = op->o_req_dn,
1130 op_ndn = op->o_req_ndn;
1134 p_dn = *op->oq_modrdn.rs_newSup;
1135 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1136 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1137 np = get_entry( op, &li->li_base_path );
1138 op->o_req_dn = op_dn;
1139 op->o_req_ndn = op_ndn;
1141 goto no_such_object;
1145 dnParent( &entry->e_name, &p_dn );
1147 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1148 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1149 ber_memfree_x( entry->e_name.bv_val, NULL );
1150 ber_memfree_x( entry->e_nname.bv_val, NULL );
1151 entry->e_name = new_dn;
1152 entry->e_nname = new_ndn;
1154 /* perform the modifications */
1155 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1156 if ( res == LDAP_SUCCESS ) {
1157 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1159 &op->o_bd->be_nsuffix[0],
1160 &li->li_base_path );
1166 /* entry was null */
1167 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1170 if ( entry != NULL ) {
1171 entry_free( entry );
1174 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1175 send_ldap_result( op, rs );
1176 slap_graduate_commit_csn( op );
1180 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1182 int ldif_back_entry_get(
1186 AttributeDescription *at,
1190 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1191 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1193 assert( ndn != NULL );
1194 assert( !BER_BVISNULL( ndn ) );
1196 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1197 op->o_req_dn = *ndn;
1198 op->o_req_ndn = *ndn;
1199 *ent = get_entry( op, &li->li_base_path );
1200 op->o_req_dn = op_dn;
1201 op->o_req_ndn = op_ndn;
1202 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1204 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1209 return ( *ent == NULL ? 1 : 0 );
1212 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1213 struct ldif_info *li = (struct ldif_info *) be->be_private;
1214 li->li_tool_current = 0;
1218 static int ldif_tool_entry_close(BackendDB * be) {
1219 struct ldif_info *li = (struct ldif_info *) be->be_private;
1221 SLAP_FREE(li->li_tool_cookie.entries);
1225 static ID ldif_tool_entry_next(BackendDB *be)
1227 struct ldif_info *li = (struct ldif_info *) be->be_private;
1228 if(li->li_tool_current >= li->li_tool_cookie.eind)
1231 return ++li->li_tool_current;
1235 ldif_tool_entry_first(BackendDB *be)
1237 struct ldif_info *li = (struct ldif_info *) be->be_private;
1239 if(li->li_tool_cookie.entries == NULL) {
1243 op.o_req_dn = *be->be_suffix;
1244 op.o_req_ndn = *be->be_nsuffix;
1245 op.ors_scope = LDAP_SCOPE_SUBTREE;
1246 li->li_tool_cookie.op = &op;
1247 (void)enum_tree( &li->li_tool_cookie );
1248 li->li_tool_cookie.op = NULL;
1250 return ldif_tool_entry_next( be );
1253 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1254 struct ldif_info *li = (struct ldif_info *) be->be_private;
1257 if(id > li->li_tool_cookie.eind || id < 1)
1260 e = li->li_tool_cookie.entries[id - 1];
1261 li->li_tool_cookie.entries[id - 1] = NULL;
1266 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1267 struct ldif_info *li = (struct ldif_info *) be->be_private;
1268 struct berval dn = e->e_nname;
1269 struct berval leaf_path = BER_BVNULL;
1272 int res = LDAP_SUCCESS;
1274 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1276 if(leaf_path.bv_val != NULL) {
1277 struct berval base = BER_BVNULL;
1278 /* build path to container, and path to ldif of container */
1279 get_parent_path(&leaf_path, &base);
1281 statres = stat(base.bv_val, &stats); /* check if container exists */
1282 if(statres == -1 && errno == ENOENT) { /* container missing */
1283 base.bv_val[base.bv_len] = '.';
1284 statres = stat(base.bv_val, &stats); /* check for leaf node */
1285 base.bv_val[base.bv_len] = '\0';
1286 if(statres == -1 && errno == ENOENT) {
1287 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1289 else if(statres != -1) { /* create parent */
1290 int mkdirres = mkdir(base.bv_val, 0750);
1291 if(mkdirres == -1) {
1292 res = LDAP_UNWILLING_TO_PERFORM;
1296 res = LDAP_UNWILLING_TO_PERFORM;
1297 }/* container was possibly created, move on to add the entry */
1298 if(res == LDAP_SUCCESS) {
1299 statres = stat(leaf_path.bv_val, &stats);
1300 if(statres == -1 && errno == ENOENT) {
1301 res = spew_entry(e, &leaf_path, 0, NULL);
1303 else /* it already exists */
1304 res = LDAP_ALREADY_EXISTS;
1306 SLAP_FREE(base.bv_val);
1307 SLAP_FREE(leaf_path.bv_val);
1310 if(res == LDAP_SUCCESS) {
1318 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1320 struct ldif_info *li;
1322 li = ch_calloc( 1, sizeof(struct ldif_info) );
1323 be->be_private = li;
1324 be->be_cf_ocs = ldifocs;
1325 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1330 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1332 struct ldif_info *li = be->be_private;
1334 ch_free(li->li_base_path.bv_val);
1335 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1336 free( be->be_private );
1341 ldif_back_db_open( Backend *be, ConfigReply *cr)
1343 struct ldif_info *li = (struct ldif_info *) be->be_private;
1344 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1345 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1352 ldif_back_initialize(
1356 static char *controls[] = {
1357 LDAP_CONTROL_MANAGEDSAIT,
1363 SLAP_BFLAG_INCREMENT |
1364 SLAP_BFLAG_REFERRALS;
1366 bi->bi_controls = controls;
1373 bi->bi_db_init = ldif_back_db_init;
1374 bi->bi_db_config = config_generic_wrapper;
1375 bi->bi_db_open = ldif_back_db_open;
1376 bi->bi_db_close = 0;
1377 bi->bi_db_destroy = ldif_back_db_destroy;
1379 bi->bi_op_bind = ldif_back_bind;
1380 bi->bi_op_unbind = 0;
1381 bi->bi_op_search = ldif_back_search;
1382 bi->bi_op_compare = 0;
1383 bi->bi_op_modify = ldif_back_modify;
1384 bi->bi_op_modrdn = ldif_back_modrdn;
1385 bi->bi_op_add = ldif_back_add;
1386 bi->bi_op_delete = ldif_back_delete;
1387 bi->bi_op_abandon = 0;
1389 bi->bi_extended = 0;
1391 bi->bi_chk_referrals = ldif_back_referrals;
1393 bi->bi_connection_init = 0;
1394 bi->bi_connection_destroy = 0;
1396 bi->bi_entry_get_rw = ldif_back_entry_get;
1398 #if 0 /* NOTE: uncomment to completely disable access control */
1399 bi->bi_access_allowed = slap_access_always_allowed;
1402 bi->bi_tool_entry_open = ldif_tool_entry_open;
1403 bi->bi_tool_entry_close = ldif_tool_entry_close;
1404 bi->bi_tool_entry_first = ldif_tool_entry_first;
1405 bi->bi_tool_entry_next = ldif_tool_entry_next;
1406 bi->bi_tool_entry_get = ldif_tool_entry_get;
1407 bi->bi_tool_entry_put = ldif_tool_entry_put;
1408 bi->bi_tool_entry_reindex = 0;
1409 bi->bi_tool_sync = 0;
1411 bi->bi_tool_dn2id_get = 0;
1412 bi->bi_tool_entry_modify = 0;
1414 bi->bi_cf_ocs = ldifocs;
1416 rc = config_register_schema( ldifcfg, ldifocs );
1417 if ( rc ) return rc;