1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2008 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 ) );
101 * use "\" + hexpair, so the escaped DN remains formally valid */
102 for ( ptr = dn.bv_val, end = &dn.bv_val[dn.bv_len]; ptr < end; ptr++ ) {
103 if ( ptr[0] == LDAP_DIRSEP[0] ) {
112 dn.bv_val = ch_malloc( dn.bv_len + 1 );
114 for ( ptr = orig_dn->bv_val, end = &orig_dn->bv_val[orig_dn->bv_len], p = dn.bv_val;
115 ptr < end; ptr++, p++)
117 static const char hex[] = "0123456789ABCDEF";
118 if ( ptr[0] == LDAP_DIRSEP[0] ) {
120 *p++ = hex[(LDAP_DIRSEP[0] & 0xF0U) >> 4];
121 *p = hex[LDAP_DIRSEP[0] & 0x0FU];
129 res->bv_len = dn.bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
130 res->bv_val = ch_malloc( res->bv_len + 1 );
131 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
132 end = dn.bv_val + dn.bv_len;
133 if ( !BER_BVISEMPTY( suffixdn ) ) {
134 *ptr++ = LDAP_DIRSEP[0];
135 ptr = lutil_strcopy( ptr, suffixdn->bv_val );
136 end -= suffixdn->bv_len + 1;
138 } else if ( BER_BVISEMPTY( &dn ) ) {
139 *ptr++ = LDAP_DIRSEP[0];
141 while ( end > dn.bv_val ) {
142 for (sep = end-1; sep >= dn.bv_val && !DN_SEPARATOR( *sep ); sep--);
143 *ptr++ = LDAP_DIRSEP[0];
144 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
152 while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
154 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
155 bv.bv_len -= ( ptr - bv.bv_val );
157 ptr = ber_bvchr( &bv, IX_DNR );
161 assert( ( ptr - bv.bv_val ) <= bv.bv_len );
162 bv.bv_len -= ( ptr - bv.bv_val );
167 if ( dn.bv_val != orig_dn->bv_val ) {
168 ch_free( dn.bv_val );
171 assert( strlen( res->bv_val ) == res->bv_len );
174 static char * slurp_file(int fd) {
175 int read_chars_total = 0;
183 entry_size = st.st_size;
184 entry = ch_malloc( entry_size+1 );
188 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
189 if(read_chars == -1) {
193 if(read_chars == 0) {
194 entry[read_chars_total] = '\0';
198 read_chars_total += read_chars;
199 entry_pos += read_chars;
206 * return nonnegative for success or -1 for error
207 * do not return numbers less than -1
209 static int spew_file(int fd, char * spew, int len) {
213 writeres = write(fd, spew, len);
226 spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
228 int rs, save_errno = 0;
232 char * entry_as_string;
233 char *tmpfname = NULL;
235 tmpfname = ch_malloc( path->bv_len + STRLENOF( "XXXXXX" ) + 1 );
236 AC_MEMCPY( tmpfname, path->bv_val, path->bv_len );
237 AC_MEMCPY( &tmpfname[ path->bv_len ], "XXXXXX", STRLENOF( "XXXXXX" ) + 1 );
239 openres = mkstemp( tmpfname );
240 if ( openres == -1 ) {
242 rs = LDAP_UNWILLING_TO_PERFORM;
243 Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
244 tmpfname, STRERROR( save_errno ), 0 );
250 /* Only save the RDN onto disk */
251 dnRdn( &e->e_name, &rdn );
252 if ( rdn.bv_len != e->e_name.bv_len ) {
253 e->e_name.bv_val[rdn.bv_len] = '\0';
254 tmp = e->e_name.bv_len;
255 e->e_name.bv_len = rdn.bv_len;
261 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
264 entry_as_string = entry2str(e, &entry_length);
265 if ( entry_as_string != NULL ) {
266 spew_res = spew_file( openres,
267 entry_as_string, entry_length );
268 if ( spew_res == -1 ) {
274 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
277 /* Restore full DN */
278 if ( rdn.bv_len != e->e_name.bv_len ) {
279 e->e_name.bv_val[e->e_name.bv_len] = ',';
280 e->e_name.bv_len = rdn.bv_len;
283 res = close( openres );
284 rs = LDAP_UNWILLING_TO_PERFORM;
286 if ( spew_res > -2 ) {
287 if ( res == -1 || spew_res == -1 ) {
288 if ( save_errno == 0 ) {
291 Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
292 tmpfname, STRERROR( save_errno ), 0 );
295 res = rename( tmpfname, path->bv_val );
301 switch ( save_errno ) {
303 rs = LDAP_NO_SUCH_OBJECT;
313 if ( rs != LDAP_SUCCESS ) {
320 if ( rs != LDAP_SUCCESS && save_errnop != NULL ) {
321 *save_errnop = save_errno;
327 static Entry * get_entry_for_fd(int fd,
331 char * entry = (char *) slurp_file(fd);
332 Entry * ldentry = NULL;
334 /* error reading file */
339 ldentry = str2entry(entry);
342 rdn = ldentry->e_name;
343 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
344 ch_free( rdn.bv_val );
345 rdn = ldentry->e_nname;
346 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
347 ch_free( rdn.bv_val );
361 static Entry * get_entry(Operation *op, struct berval *base_path) {
362 struct berval path, pdn, pndn;
365 dnParent(&op->o_req_dn, &pdn);
366 dnParent(&op->o_req_ndn, &pndn);
367 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
368 fd = open(path.bv_val, O_RDONLY);
369 /* error opening file (mebbe should log error) */
370 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
371 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
372 path.bv_val, STRERROR(errno), 0 );
375 if(path.bv_val != NULL)
376 SLAP_FREE(path.bv_val);
379 return get_entry_for_fd(fd, &pdn, &pndn);
385 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
387 res->bv_len = name->bv_len + base->bv_len + 1;
388 res->bv_val = ch_malloc( res->bv_len + 1 );
389 strcpy(res->bv_val, base->bv_val);
390 ptr = res->bv_val + base->bv_len;
391 *ptr++ = LDAP_DIRSEP[0];
392 strcpy(ptr, name->bv_val);
395 typedef struct bvlist {
404 static int r_enum_tree(enumCookie *ck, struct berval *path, int base,
405 struct berval *pdn, struct berval *pndn)
408 int fd = 0, rc = LDAP_SUCCESS;
411 fd = open( path->bv_val, O_RDONLY );
413 Debug( LDAP_DEBUG_TRACE,
414 "=> ldif_enum_tree: failed to open %s: %s\n",
415 path->bv_val, STRERROR(errno), 0 );
416 return LDAP_NO_SUCH_OBJECT;
419 e = get_entry_for_fd(fd, pdn, pndn);
421 Debug( LDAP_DEBUG_ANY,
422 "=> ldif_enum_tree: failed to read entry for %s\n",
423 path->bv_val, 0, 0 );
427 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
428 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
429 /* Send right away? */
432 * if it's a referral, add it to the list of referrals. only do
433 * this for non-base searches, and don't check the filter
434 * explicitly here since it's only a candidate anyway.
436 if ( !get_manageDSAit( ck->op )
437 && ck->op->ors_scope != LDAP_SCOPE_BASE
438 && is_entry_referral( e ) )
440 BerVarray erefs = get_entry_referrals( ck->op, e );
441 ck->rs->sr_ref = referral_rewrite( erefs,
443 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
444 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
446 ck->rs->sr_entry = e;
447 rc = send_search_reference( ck->op, ck->rs );
448 ber_bvarray_free( ck->rs->sr_ref );
449 ber_bvarray_free( erefs );
450 ck->rs->sr_ref = NULL;
451 ck->rs->sr_entry = NULL;
453 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
455 ck->rs->sr_entry = e;
456 ck->rs->sr_attrs = ck->op->ors_attrs;
457 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
458 rc = send_search_entry(ck->op, ck->rs);
459 ck->rs->sr_entry = NULL;
465 /* Queueing up for tool mode */
466 if(ck->entries == NULL) {
467 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
468 ck->elen = ENTRY_BUFF_INCREMENT;
470 if(ck->eind >= ck->elen) { /* grow entries if necessary */
471 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
475 ck->entries[ck->eind++] = e;
483 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
485 bvlist *list = NULL, *ptr;
487 path->bv_len -= STRLENOF( LDIF );
488 path->bv_val[path->bv_len] = '\0';
490 dir_of_path = opendir(path->bv_val);
491 if(dir_of_path == NULL) { /* can't open directory */
492 if ( errno != ENOENT ) {
493 /* it shouldn't be treated as an error
494 * only if the directory doesn't exist */
496 Debug( LDAP_DEBUG_ANY,
497 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
498 path->bv_val, errno, 0 );
504 struct berval fname, itmp;
508 dir = readdir(dir_of_path);
509 if(dir == NULL) break; /* end of the directory */
510 fname.bv_len = strlen( dir->d_name );
511 if ( fname.bv_len <= STRLENOF( LDIF ))
513 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
515 fname.bv_val = dir->d_name;
517 bvl = ch_malloc( sizeof(bvlist) );
518 ber_dupbv( &bvl->bv, &fname );
519 BER_BVZERO( &bvl->num );
520 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
524 itmp.bv_len = bvl->bv.bv_len
525 - ( itmp.bv_val - bvl->bv.bv_val );
526 ptr = ber_bvchr( &itmp, IX_FSR );
528 itmp.bv_len = ptr - itmp.bv_val;
529 ber_dupbv( &bvl->num, &itmp );
530 bvl->inum = strtol( itmp.bv_val, NULL, 0 );
531 itmp.bv_val[0] = '\0';
532 bvl->off = itmp.bv_val - bvl->bv.bv_val;
536 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
537 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
538 if ( !cmp && bvl->num.bv_val )
539 cmp = bvl->inum - ptr->inum;
547 closedir(dir_of_path);
549 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
550 ck->op->ors_scope = LDAP_SCOPE_BASE;
551 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
552 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
554 while ( ( ptr = list ) ) {
559 if ( rc == LDAP_SUCCESS ) {
560 if ( ptr->num.bv_val )
561 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
563 fullpath( path, &ptr->bv, &fpath );
564 rc = r_enum_tree(ck, &fpath, 0,
565 e != NULL ? &e->e_name : pdn,
566 e != NULL ? &e->e_nname : pndn );
569 if ( ptr->num.bv_val )
570 free( ptr->num.bv_val );
571 free(ptr->bv.bv_val);
576 if ( fd ) entry_free( e );
585 struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
587 struct berval pdn, pndn;
590 dnParent( &ck->op->o_req_dn, &pdn );
591 dnParent( &ck->op->o_req_ndn, &pndn );
592 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
593 rc = r_enum_tree(ck, &path, BER_BVISEMPTY( &ck->op->o_req_ndn ) ? 1 : 0, &pdn, &pndn);
594 ch_free( path.bv_val );
598 /* Get the parent path plus the LDIF suffix */
599 static void get_parent_path(struct berval * dnpath, struct berval *res) {
600 int dnpathlen = dnpath->bv_len;
603 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
604 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
607 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
608 strncpy(res->bv_val, dnpath->bv_val, i);
609 strcpy(res->bv_val+i, LDIF);
610 res->bv_val[i] = '\0';
613 static int apply_modify_to_entry(Entry * entry,
614 Modifications * modlist,
618 char textbuf[SLAP_TEXT_BUFLEN];
619 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
621 Modification *mods = NULL;
623 if (!acl_check_modlist(op, entry, modlist)) {
624 return LDAP_INSUFFICIENT_ACCESS;
627 for (; modlist != NULL; modlist = modlist->sml_next) {
628 mods = &modlist->sml_mod;
630 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
633 switch (mods->sm_op) {
635 rc = modify_add_values(entry, mods,
636 get_permissiveModify(op),
637 &rs->sr_text, textbuf,
641 case LDAP_MOD_DELETE:
642 rc = modify_delete_values(entry, mods,
643 get_permissiveModify(op),
644 &rs->sr_text, textbuf,
648 case LDAP_MOD_REPLACE:
649 rc = modify_replace_values(entry, mods,
650 get_permissiveModify(op),
651 &rs->sr_text, textbuf,
655 case LDAP_MOD_INCREMENT:
656 rc = modify_increment_values( entry,
657 mods, get_permissiveModify(op),
658 &rs->sr_text, textbuf,
664 case SLAP_MOD_SOFTADD:
665 mods->sm_op = LDAP_MOD_ADD;
666 rc = modify_add_values(entry, mods,
667 get_permissiveModify(op),
668 &rs->sr_text, textbuf,
670 mods->sm_op = SLAP_MOD_SOFTADD;
671 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
678 if(rc != LDAP_SUCCESS) break;
681 if(rc == LDAP_SUCCESS) {
683 entry->e_ocflags = 0;
685 /* check that the entry still obeys the schema */
686 rc = entry_schema_check( op, entry, NULL, 0, 0,
687 &rs->sr_text, textbuf, sizeof( textbuf ) );
694 ldif_back_referrals( Operation *op, SlapReply *rs )
696 struct ldif_info *li = NULL;
698 int rc = LDAP_SUCCESS;
701 if ( op->o_tag == LDAP_REQ_SEARCH ) {
702 /* let search take care of itself */
707 if ( get_manageDSAit( op ) ) {
708 /* let op take care of DSA management */
712 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
713 /* the empty DN cannot be a referral */
717 li = (struct ldif_info *)op->o_bd->be_private;
718 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
719 entry = get_entry( op, &li->li_base_path );
721 /* no object is found for them */
722 if ( entry == NULL ) {
723 struct berval odn = op->o_req_dn;
724 struct berval ondn = op->o_req_ndn;
726 struct berval pndn = op->o_req_ndn;
728 for ( ; entry == NULL; ) {
729 dnParent( &pndn, &pndn );
731 if ( BER_BVISEMPTY( &pndn )
732 || !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) )
738 op->o_req_ndn = pndn;
740 entry = get_entry( op, &li->li_base_path );
743 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
746 op->o_req_ndn = ondn;
749 rs->sr_matched = NULL;
750 if ( entry != NULL ) {
751 Debug( LDAP_DEBUG_TRACE,
752 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
753 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
755 if ( is_entry_referral( entry ) ) {
757 rs->sr_ref = get_entry_referrals( op, entry );
759 rs->sr_matched = ber_strdup_x(
760 entry->e_name.bv_val, op->o_tmpmemctx );
766 } else if ( default_referral != NULL ) {
768 rs->sr_ref = referral_rewrite( default_referral,
769 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
772 if ( rs->sr_ref != NULL ) {
774 rc = rs->sr_err = LDAP_REFERRAL;
775 send_ldap_result( op, rs );
776 ber_bvarray_free( rs->sr_ref );
779 } else if ( rc != LDAP_SUCCESS ) {
781 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
782 send_ldap_result( op, rs );
785 if ( rs->sr_matched ) {
786 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
787 rs->sr_matched = NULL;
793 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
795 if ( is_entry_referral( entry ) ) {
796 /* entry is a referral */
797 BerVarray refs = get_entry_referrals( op, entry );
798 rs->sr_ref = referral_rewrite(
799 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
801 Debug( LDAP_DEBUG_TRACE,
802 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
803 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
805 rs->sr_matched = entry->e_name.bv_val;
806 if ( rs->sr_ref != NULL ) {
807 rc = rs->sr_err = LDAP_REFERRAL;
808 send_ldap_result( op, rs );
809 ber_bvarray_free( rs->sr_ref );
813 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
817 rs->sr_matched = NULL;
818 ber_bvarray_free( refs );
827 ldif_back_bind( Operation *op, SlapReply *rs )
829 struct ldif_info *li = NULL;
830 Attribute * a = NULL;
831 AttributeDescription *password = slap_schema.si_ad_userPassword;
833 Entry * entry = NULL;
835 switch ( be_rootdn_bind( op, rs ) ) {
836 case SLAP_CB_CONTINUE:
840 /* in case of success, front end will send result;
841 * otherwise, be_rootdn_bind() did */
845 li = (struct ldif_info *) op->o_bd->be_private;
846 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
847 entry = get_entry(op, &li->li_base_path);
849 /* no object is found for them */
851 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
855 /* they don't have userpassword */
856 if((a = attr_find(entry->e_attrs, password)) == NULL) {
857 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
862 /* authentication actually failed */
863 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
864 &rs->sr_text) != 0) {
865 rs->sr_err = LDAP_INVALID_CREDENTIALS;
870 /* let the front-end send success */
875 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
877 send_ldap_result( op, rs );
883 static int ldif_back_search(Operation *op, SlapReply *rs)
885 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
886 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
890 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
891 rs->sr_err = enum_tree( &ck );
892 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
893 send_ldap_result(op, rs);
898 static int ldif_back_add(Operation *op, SlapReply *rs) {
899 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
900 Entry * e = op->ora_e;
901 struct berval dn = e->e_nname;
902 struct berval leaf_path = BER_BVNULL;
905 char textbuf[SLAP_TEXT_BUFLEN];
907 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
909 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
910 &rs->sr_text, textbuf, sizeof( textbuf ) );
911 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
913 rs->sr_err = slap_add_opattrs( op,
914 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
915 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
917 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
919 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
921 if(leaf_path.bv_val != NULL) {
922 struct berval base = BER_BVNULL;
923 /* build path to container and ldif of container */
924 get_parent_path(&leaf_path, &base);
926 statres = stat(base.bv_val, &stats); /* check if container exists */
927 if(statres == -1 && errno == ENOENT) { /* container missing */
928 base.bv_val[base.bv_len] = '.';
929 statres = stat(base.bv_val, &stats); /* check for leaf node */
930 base.bv_val[base.bv_len] = '\0';
931 if(statres == -1 && errno == ENOENT) {
932 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
933 rs->sr_text = "Parent does not exist";
935 else if(statres != -1) { /* create parent */
936 int mkdirres = mkdir(base.bv_val, 0750);
938 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
939 rs->sr_text = "Could not create parent folder";
940 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
941 base.bv_val, STRERROR( errno ), 0 );
945 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
946 }/* container was possibly created, move on to add the entry */
947 if(rs->sr_err == LDAP_SUCCESS) {
948 statres = stat(leaf_path.bv_val, &stats);
949 if(statres == -1 && errno == ENOENT) {
950 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
952 else if ( statres == -1 ) {
953 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
954 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
955 leaf_path.bv_val, STRERROR( errno ), 0 );
957 else /* it already exists */
958 rs->sr_err = LDAP_ALREADY_EXISTS;
960 SLAP_FREE(base.bv_val);
961 SLAP_FREE(leaf_path.bv_val);
964 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
967 Debug( LDAP_DEBUG_TRACE,
968 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
969 rs->sr_text : "", 0);
970 send_ldap_result(op, rs);
971 slap_graduate_commit_csn( op );
975 static int ldif_back_modify(Operation *op, SlapReply *rs) {
976 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
977 Modifications * modlst = op->orm_modlist;
978 struct berval path = BER_BVNULL;
979 Entry * entry = NULL;
982 slap_mods_opattrs( op, &op->orm_modlist, 1 );
984 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
985 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
987 entry = get_entry(op, &li->li_base_path);
990 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
991 if(rs->sr_err == LDAP_SUCCESS) {
993 spew_res = spew_entry(entry, &path, 1, &save_errno);
995 Debug( LDAP_DEBUG_ANY,
996 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
997 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
998 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1003 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1008 if(path.bv_val != NULL)
1009 SLAP_FREE(path.bv_val);
1011 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1012 send_ldap_result(op, rs);
1013 slap_graduate_commit_csn( op );
1017 static int ldif_back_delete(Operation *op, SlapReply *rs) {
1018 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1019 struct berval path = BER_BVNULL;
1022 if ( BER_BVISEMPTY( &op->o_csn )) {
1024 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1026 csn.bv_val = csnbuf;
1027 csn.bv_len = sizeof( csnbuf );
1028 slap_get_csn( op, &csn, 1 );
1031 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
1032 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
1034 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
1035 res = rmdir(path.bv_val);
1036 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
1037 rs->sr_err = LDAP_SUCCESS;
1041 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
1045 /* is leaf, go on */
1050 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1056 res = unlink(path.bv_val);
1060 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1064 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1070 SLAP_FREE(path.bv_val);
1071 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1072 send_ldap_result(op, rs);
1073 slap_graduate_commit_csn( op );
1078 static int move_entry(Entry * entry, struct berval * ndn,
1079 struct berval * newndn, struct berval * suffixdn,
1080 struct berval * base_path) {
1084 struct berval newpath;
1086 dn2path(ndn, suffixdn, base_path, &path);
1087 dn2path(newndn, suffixdn, base_path, &newpath);
1089 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1090 /* some object doesn't exist */
1091 res = LDAP_NO_SUCH_OBJECT;
1093 else { /* do the modrdn */
1094 exists_res = open(newpath.bv_val, O_RDONLY);
1095 if(exists_res == -1 && errno == ENOENT) {
1096 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1097 res = spew_entry(entry, &newpath, 0, NULL);
1099 /* if this fails we should log something bad */
1100 res = unlink(path.bv_val);
1101 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1102 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1103 res = rename(path.bv_val, newpath.bv_val);
1108 res = LDAP_NO_SUCH_OBJECT;
1110 res = LDAP_UNWILLING_TO_PERFORM;
1111 unlink(newpath.bv_val); /* in case file was created */
1113 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1115 else if(exists_res) {
1116 int close_res = close(exists_res);
1117 res = LDAP_ALREADY_EXISTS;
1118 if(close_res == -1) {
1119 /* log heinous error */
1123 res = LDAP_UNWILLING_TO_PERFORM;
1127 if(newpath.bv_val != NULL)
1128 SLAP_FREE(newpath.bv_val);
1129 if(path.bv_val != NULL)
1130 SLAP_FREE(path.bv_val);
1135 ldif_back_modrdn(Operation *op, SlapReply *rs)
1137 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1138 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1140 Entry * entry = NULL;
1143 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1145 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1146 entry = get_entry( op, &li->li_base_path );
1148 /* build the mods to the entry */
1149 if ( entry != NULL ) {
1150 /* build new dn, and new ndn for the entry */
1151 if ( op->oq_modrdn.rs_newSup != NULL ) {
1152 struct berval op_dn = op->o_req_dn,
1153 op_ndn = op->o_req_ndn;
1157 p_dn = *op->oq_modrdn.rs_newSup;
1158 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1159 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1160 np = get_entry( op, &li->li_base_path );
1161 op->o_req_dn = op_dn;
1162 op->o_req_ndn = op_ndn;
1164 goto no_such_object;
1168 dnParent( &entry->e_name, &p_dn );
1170 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1171 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1172 ber_memfree_x( entry->e_name.bv_val, NULL );
1173 ber_memfree_x( entry->e_nname.bv_val, NULL );
1174 entry->e_name = new_dn;
1175 entry->e_nname = new_ndn;
1177 /* perform the modifications */
1178 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1179 if ( res == LDAP_SUCCESS ) {
1180 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1182 &op->o_bd->be_nsuffix[0],
1183 &li->li_base_path );
1189 /* entry was null */
1190 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1193 if ( entry != NULL ) {
1194 entry_free( entry );
1197 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1198 send_ldap_result( op, rs );
1199 slap_graduate_commit_csn( op );
1203 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1205 int ldif_back_entry_get(
1209 AttributeDescription *at,
1213 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1214 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1216 assert( ndn != NULL );
1217 assert( !BER_BVISNULL( ndn ) );
1219 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1220 op->o_req_dn = *ndn;
1221 op->o_req_ndn = *ndn;
1222 *ent = get_entry( op, &li->li_base_path );
1223 op->o_req_dn = op_dn;
1224 op->o_req_ndn = op_ndn;
1225 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1227 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1232 return ( *ent == NULL ? 1 : 0 );
1235 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1236 struct ldif_info *li = (struct ldif_info *) be->be_private;
1237 li->li_tool_current = 0;
1241 static int ldif_tool_entry_close(BackendDB * be) {
1242 struct ldif_info *li = (struct ldif_info *) be->be_private;
1244 SLAP_FREE(li->li_tool_cookie.entries);
1248 static ID ldif_tool_entry_next(BackendDB *be)
1250 struct ldif_info *li = (struct ldif_info *) be->be_private;
1251 if(li->li_tool_current >= li->li_tool_cookie.eind)
1254 return ++li->li_tool_current;
1258 ldif_tool_entry_first(BackendDB *be)
1260 struct ldif_info *li = (struct ldif_info *) be->be_private;
1262 if(li->li_tool_cookie.entries == NULL) {
1266 op.o_req_dn = *be->be_suffix;
1267 op.o_req_ndn = *be->be_nsuffix;
1268 op.ors_scope = LDAP_SCOPE_SUBTREE;
1269 li->li_tool_cookie.op = &op;
1270 (void)enum_tree( &li->li_tool_cookie );
1271 li->li_tool_cookie.op = NULL;
1273 return ldif_tool_entry_next( be );
1276 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1277 struct ldif_info *li = (struct ldif_info *) be->be_private;
1280 if(id > li->li_tool_cookie.eind || id < 1)
1283 e = li->li_tool_cookie.entries[id - 1];
1284 li->li_tool_cookie.entries[id - 1] = NULL;
1289 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1290 struct ldif_info *li = (struct ldif_info *) be->be_private;
1291 struct berval dn = e->e_nname;
1292 struct berval leaf_path = BER_BVNULL;
1295 int res = LDAP_SUCCESS;
1297 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1299 if(leaf_path.bv_val != NULL) {
1300 struct berval base = BER_BVNULL;
1301 /* build path to container, and path to ldif of container */
1302 get_parent_path(&leaf_path, &base);
1304 statres = stat(base.bv_val, &stats); /* check if container exists */
1305 if(statres == -1 && errno == ENOENT) { /* container missing */
1306 base.bv_val[base.bv_len] = '.';
1307 statres = stat(base.bv_val, &stats); /* check for leaf node */
1308 base.bv_val[base.bv_len] = '\0';
1309 if(statres == -1 && errno == ENOENT) {
1310 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1312 else if(statres != -1) { /* create parent */
1313 int mkdirres = mkdir(base.bv_val, 0750);
1314 if(mkdirres == -1) {
1315 res = LDAP_UNWILLING_TO_PERFORM;
1319 res = LDAP_UNWILLING_TO_PERFORM;
1320 }/* container was possibly created, move on to add the entry */
1321 if(res == LDAP_SUCCESS) {
1322 statres = stat(leaf_path.bv_val, &stats);
1323 if(statres == -1 && errno == ENOENT) {
1324 res = spew_entry(e, &leaf_path, 0, NULL);
1326 else /* it already exists */
1327 res = LDAP_ALREADY_EXISTS;
1329 SLAP_FREE(base.bv_val);
1330 SLAP_FREE(leaf_path.bv_val);
1333 if(res == LDAP_SUCCESS) {
1341 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1343 struct ldif_info *li;
1345 li = ch_calloc( 1, sizeof(struct ldif_info) );
1346 be->be_private = li;
1347 be->be_cf_ocs = ldifocs;
1348 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1353 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1355 struct ldif_info *li = be->be_private;
1357 ch_free(li->li_base_path.bv_val);
1358 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1359 free( be->be_private );
1364 ldif_back_db_open( Backend *be, ConfigReply *cr)
1366 struct ldif_info *li = (struct ldif_info *) be->be_private;
1367 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1368 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1375 ldif_back_initialize(
1379 static char *controls[] = {
1380 LDAP_CONTROL_MANAGEDSAIT,
1386 SLAP_BFLAG_INCREMENT |
1387 SLAP_BFLAG_REFERRALS;
1389 bi->bi_controls = controls;
1396 bi->bi_db_init = ldif_back_db_init;
1397 bi->bi_db_config = config_generic_wrapper;
1398 bi->bi_db_open = ldif_back_db_open;
1399 bi->bi_db_close = 0;
1400 bi->bi_db_destroy = ldif_back_db_destroy;
1402 bi->bi_op_bind = ldif_back_bind;
1403 bi->bi_op_unbind = 0;
1404 bi->bi_op_search = ldif_back_search;
1405 bi->bi_op_compare = 0;
1406 bi->bi_op_modify = ldif_back_modify;
1407 bi->bi_op_modrdn = ldif_back_modrdn;
1408 bi->bi_op_add = ldif_back_add;
1409 bi->bi_op_delete = ldif_back_delete;
1410 bi->bi_op_abandon = 0;
1412 bi->bi_extended = 0;
1414 bi->bi_chk_referrals = ldif_back_referrals;
1416 bi->bi_connection_init = 0;
1417 bi->bi_connection_destroy = 0;
1419 bi->bi_entry_get_rw = ldif_back_entry_get;
1421 #if 0 /* NOTE: uncomment to completely disable access control */
1422 bi->bi_access_allowed = slap_access_always_allowed;
1425 bi->bi_tool_entry_open = ldif_tool_entry_open;
1426 bi->bi_tool_entry_close = ldif_tool_entry_close;
1427 bi->bi_tool_entry_first = ldif_tool_entry_first;
1428 bi->bi_tool_entry_next = ldif_tool_entry_next;
1429 bi->bi_tool_entry_get = ldif_tool_entry_get;
1430 bi->bi_tool_entry_put = ldif_tool_entry_put;
1431 bi->bi_tool_entry_reindex = 0;
1432 bi->bi_tool_sync = 0;
1434 bi->bi_tool_dn2id_get = 0;
1435 bi->bi_tool_entry_modify = 0;
1437 bi->bi_cf_ocs = ldifocs;
1439 rc = config_register_schema( ldifcfg, ldifocs );
1440 if ( rc ) return rc;