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;
162 * return number of bytes written, or -1 in case of error
163 * do not return numbers less than -1
165 static int spew_file(int fd, char * spew, int len) {
169 writeres = write(fd, spew, len);
182 spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
184 int rs, save_errno = 0;
188 char * entry_as_string;
189 char *tmpfname = NULL;
191 tmpfname = ch_malloc( path->bv_len + STRLENOF( "XXXXXX" ) + 1 );
192 AC_MEMCPY( tmpfname, path->bv_val, path->bv_len );
193 AC_MEMCPY( &tmpfname[ path->bv_len ], "XXXXXX", STRLENOF( "XXXXXX" ) + 1 );
195 openres = mkstemp( tmpfname );
196 if ( openres == -1 ) {
198 rs = LDAP_UNWILLING_TO_PERFORM;
199 Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
200 tmpfname, STRERROR( save_errno ), 0 );
206 /* Only save the RDN onto disk */
207 dnRdn( &e->e_name, &rdn );
208 if ( rdn.bv_len != e->e_name.bv_len ) {
209 e->e_name.bv_val[rdn.bv_len] = '\0';
210 tmp = e->e_name.bv_len;
211 e->e_name.bv_len = rdn.bv_len;
217 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
220 entry_as_string = entry2str(e, &entry_length);
221 if ( entry_as_string != NULL ) {
222 spew_res = spew_file( openres,
223 entry_as_string, entry_length );
224 if ( spew_res == -1 ) {
230 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
233 /* Restore full DN */
234 if ( rdn.bv_len != e->e_name.bv_len ) {
235 e->e_name.bv_val[e->e_name.bv_len] = ',';
236 e->e_name.bv_len = rdn.bv_len;
239 res = close( openres );
240 rs = LDAP_UNWILLING_TO_PERFORM;
242 if ( spew_res > -2 ) {
243 if ( res == -1 || spew_res == -1 ) {
244 if ( save_errno == 0 ) {
247 Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
248 tmpfname, STRERROR( save_errno ), 0 );
251 res = rename( tmpfname, path->bv_val );
257 switch ( save_errno ) {
259 rs = LDAP_NO_SUCH_OBJECT;
269 if ( rs != LDAP_SUCCESS ) {
276 if ( rs != LDAP_SUCCESS && save_errnop != NULL ) {
277 *save_errnop = save_errno;
283 static Entry * get_entry_for_fd(int fd,
287 char * entry = (char *) slurp_file(fd);
288 Entry * ldentry = NULL;
290 /* error reading file */
295 ldentry = str2entry(entry);
298 rdn = ldentry->e_name;
299 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
300 ch_free( rdn.bv_val );
301 rdn = ldentry->e_nname;
302 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
303 ch_free( rdn.bv_val );
317 static Entry * get_entry(Operation *op, struct berval *base_path) {
318 struct berval path, pdn, pndn;
321 dnParent(&op->o_req_dn, &pdn);
322 dnParent(&op->o_req_ndn, &pndn);
323 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
324 fd = open(path.bv_val, O_RDONLY);
325 /* error opening file (mebbe should log error) */
326 if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
327 Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
328 path.bv_val, STRERROR(errno), 0 );
331 if(path.bv_val != NULL)
332 SLAP_FREE(path.bv_val);
335 return get_entry_for_fd(fd, &pdn, &pndn);
341 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
343 res->bv_len = name->bv_len + base->bv_len + 1;
344 res->bv_val = ch_malloc( res->bv_len + 1 );
345 strcpy(res->bv_val, base->bv_val);
346 ptr = res->bv_val + base->bv_len;
347 *ptr++ = LDAP_DIRSEP[0];
348 strcpy(ptr, name->bv_val);
351 typedef struct bvlist {
360 static int r_enum_tree(enumCookie *ck, struct berval *path,
361 struct berval *pdn, struct berval *pndn)
364 int fd, rc = LDAP_SUCCESS;
366 fd = open( path->bv_val, O_RDONLY );
368 Debug( LDAP_DEBUG_TRACE,
369 "=> ldif_enum_tree: failed to open %s: %s\n",
370 path->bv_val, STRERROR(errno), 0 );
371 return LDAP_NO_SUCH_OBJECT;
374 e = get_entry_for_fd(fd, pdn, pndn);
376 Debug( LDAP_DEBUG_ANY,
377 "=> ldif_enum_tree: failed to read entry for %s\n",
378 path->bv_val, 0, 0 );
382 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
383 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
384 /* Send right away? */
387 * if it's a referral, add it to the list of referrals. only do
388 * this for non-base searches, and don't check the filter
389 * explicitly here since it's only a candidate anyway.
391 if ( !get_manageDSAit( ck->op )
392 && ck->op->ors_scope != LDAP_SCOPE_BASE
393 && is_entry_referral( e ) )
395 BerVarray erefs = get_entry_referrals( ck->op, e );
396 ck->rs->sr_ref = referral_rewrite( erefs,
398 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
399 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
401 ck->rs->sr_entry = e;
402 rc = send_search_reference( ck->op, ck->rs );
403 ber_bvarray_free( ck->rs->sr_ref );
404 ber_bvarray_free( erefs );
405 ck->rs->sr_ref = NULL;
406 ck->rs->sr_entry = NULL;
408 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
410 ck->rs->sr_entry = e;
411 ck->rs->sr_attrs = ck->op->ors_attrs;
412 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
413 rc = send_search_entry(ck->op, ck->rs);
414 ck->rs->sr_entry = NULL;
420 /* Queueing up for tool mode */
421 if(ck->entries == NULL) {
422 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
423 ck->elen = ENTRY_BUFF_INCREMENT;
425 if(ck->eind >= ck->elen) { /* grow entries if necessary */
426 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
430 ck->entries[ck->eind++] = e;
437 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
439 bvlist *list = NULL, *ptr;
441 path->bv_len -= STRLENOF( LDIF );
442 path->bv_val[path->bv_len] = '\0';
444 dir_of_path = opendir(path->bv_val);
445 if(dir_of_path == NULL) { /* can't open directory */
446 if ( errno != ENOENT ) {
447 /* it shouldn't be treated as an error
448 * only if the directory doesn't exist */
450 Debug( LDAP_DEBUG_ANY,
451 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
452 path->bv_val, errno, 0 );
458 struct berval fname, itmp;
462 dir = readdir(dir_of_path);
463 if(dir == NULL) break; /* end of the directory */
464 fname.bv_len = strlen( dir->d_name );
465 if ( fname.bv_len <= STRLENOF( LDIF ))
467 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
469 fname.bv_val = dir->d_name;
471 bvl = ch_malloc( sizeof(bvlist) );
472 ber_dupbv( &bvl->bv, &fname );
473 BER_BVZERO( &bvl->num );
474 itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
478 itmp.bv_len = bvl->bv.bv_len
479 - ( itmp.bv_val - bvl->bv.bv_val );
480 ptr = ber_bvchr( &itmp, IX_FSR );
482 itmp.bv_len = ptr - itmp.bv_val;
483 ber_dupbv( &bvl->num, &itmp );
484 bvl->inum = strtol( itmp.bv_val, NULL, 0 );
485 itmp.bv_val[0] = '\0';
486 bvl->off = itmp.bv_val - bvl->bv.bv_val;
490 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
491 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
492 if ( !cmp && bvl->num.bv_val )
493 cmp = bvl->inum - ptr->inum;
501 closedir(dir_of_path);
503 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
504 ck->op->ors_scope = LDAP_SCOPE_BASE;
505 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
506 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
508 while ( ( ptr = list ) ) {
513 if ( rc == LDAP_SUCCESS ) {
514 if ( ptr->num.bv_val )
515 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
517 fullpath( path, &ptr->bv, &fpath );
518 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
521 if ( ptr->num.bv_val )
522 free( ptr->num.bv_val );
523 free(ptr->bv.bv_val);
528 if ( fd ) entry_free( e );
537 struct ldif_info *li = (struct ldif_info *) ck->op->o_bd->be_private;
539 struct berval pdn, pndn;
542 dnParent( &ck->op->o_req_dn, &pdn );
543 dnParent( &ck->op->o_req_ndn, &pndn );
544 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
545 rc = r_enum_tree(ck, &path, &pdn, &pndn);
546 ch_free( path.bv_val );
550 /* Get the parent path plus the LDIF suffix */
551 static void get_parent_path(struct berval * dnpath, struct berval *res) {
552 int dnpathlen = dnpath->bv_len;
555 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
556 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
559 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
560 strncpy(res->bv_val, dnpath->bv_val, i);
561 strcpy(res->bv_val+i, LDIF);
562 res->bv_val[i] = '\0';
565 static int apply_modify_to_entry(Entry * entry,
566 Modifications * modlist,
570 char textbuf[SLAP_TEXT_BUFLEN];
571 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
573 Modification *mods = NULL;
575 if (!acl_check_modlist(op, entry, modlist)) {
576 return LDAP_INSUFFICIENT_ACCESS;
579 for (; modlist != NULL; modlist = modlist->sml_next) {
580 mods = &modlist->sml_mod;
582 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
585 switch (mods->sm_op) {
587 rc = modify_add_values(entry, mods,
588 get_permissiveModify(op),
589 &rs->sr_text, textbuf,
593 case LDAP_MOD_DELETE:
594 rc = modify_delete_values(entry, mods,
595 get_permissiveModify(op),
596 &rs->sr_text, textbuf,
600 case LDAP_MOD_REPLACE:
601 rc = modify_replace_values(entry, mods,
602 get_permissiveModify(op),
603 &rs->sr_text, textbuf,
607 case LDAP_MOD_INCREMENT:
608 rc = modify_increment_values( entry,
609 mods, get_permissiveModify(op),
610 &rs->sr_text, textbuf,
616 case SLAP_MOD_SOFTADD:
617 mods->sm_op = LDAP_MOD_ADD;
618 rc = modify_add_values(entry, mods,
619 get_permissiveModify(op),
620 &rs->sr_text, textbuf,
622 mods->sm_op = SLAP_MOD_SOFTADD;
623 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
630 if(rc != LDAP_SUCCESS) break;
633 if(rc == LDAP_SUCCESS) {
635 entry->e_ocflags = 0;
637 /* check that the entry still obeys the schema */
638 rc = entry_schema_check( op, entry, NULL, 0, 0,
639 &rs->sr_text, textbuf, sizeof( textbuf ) );
646 ldif_back_referrals( Operation *op, SlapReply *rs )
648 struct ldif_info *li = NULL;
650 int rc = LDAP_SUCCESS;
653 if ( op->o_tag == LDAP_REQ_SEARCH ) {
654 /* let search take care of itself */
659 if ( get_manageDSAit( op ) ) {
660 /* let op take care of DSA management */
664 li = (struct ldif_info *)op->o_bd->be_private;
665 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
666 entry = get_entry( op, &li->li_base_path );
668 /* no object is found for them */
669 if ( entry == NULL ) {
670 struct berval odn = op->o_req_dn;
671 struct berval ondn = op->o_req_ndn;
673 struct berval pndn = op->o_req_ndn;
675 for ( ; entry == NULL; ) {
676 dnParent( &pndn, &pndn );
678 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
683 op->o_req_ndn = pndn;
685 entry = get_entry( op, &li->li_base_path );
688 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
691 op->o_req_ndn = ondn;
694 rs->sr_matched = NULL;
695 if ( entry != NULL ) {
696 Debug( LDAP_DEBUG_TRACE,
697 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
698 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
700 if ( is_entry_referral( entry ) ) {
702 rs->sr_ref = get_entry_referrals( op, entry );
704 rs->sr_matched = ber_strdup_x(
705 entry->e_name.bv_val, op->o_tmpmemctx );
711 } else if ( default_referral != NULL ) {
713 rs->sr_ref = referral_rewrite( default_referral,
714 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
717 if ( rs->sr_ref != NULL ) {
719 rc = rs->sr_err = LDAP_REFERRAL;
720 send_ldap_result( op, rs );
721 ber_bvarray_free( rs->sr_ref );
724 } else if ( rc != LDAP_SUCCESS ) {
726 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
727 send_ldap_result( op, rs );
730 if ( rs->sr_matched ) {
731 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
732 rs->sr_matched = NULL;
738 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
740 if ( is_entry_referral( entry ) ) {
741 /* entry is a referral */
742 BerVarray refs = get_entry_referrals( op, entry );
743 rs->sr_ref = referral_rewrite(
744 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
746 Debug( LDAP_DEBUG_TRACE,
747 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
748 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
750 rs->sr_matched = entry->e_name.bv_val;
751 if ( rs->sr_ref != NULL ) {
752 rc = rs->sr_err = LDAP_REFERRAL;
753 send_ldap_result( op, rs );
754 ber_bvarray_free( rs->sr_ref );
758 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
762 rs->sr_matched = NULL;
763 ber_bvarray_free( refs );
772 ldif_back_bind( Operation *op, SlapReply *rs )
774 struct ldif_info *li = NULL;
775 Attribute * a = NULL;
776 AttributeDescription *password = slap_schema.si_ad_userPassword;
778 Entry * entry = NULL;
780 switch ( be_rootdn_bind( op, rs ) ) {
781 case SLAP_CB_CONTINUE:
785 /* in case of success, front end will send result;
786 * otherwise, be_rootdn_bind() did */
790 li = (struct ldif_info *) op->o_bd->be_private;
791 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
792 entry = get_entry(op, &li->li_base_path);
794 /* no object is found for them */
796 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
800 /* they don't have userpassword */
801 if((a = attr_find(entry->e_attrs, password)) == NULL) {
802 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
807 /* authentication actually failed */
808 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
809 &rs->sr_text) != 0) {
810 rs->sr_err = LDAP_INVALID_CREDENTIALS;
815 /* let the front-end send success */
820 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
822 send_ldap_result( op, rs );
828 static int ldif_back_search(Operation *op, SlapReply *rs)
830 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
831 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
835 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
836 rs->sr_err = enum_tree( &ck );
837 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
838 send_ldap_result(op, rs);
843 static int ldif_back_add(Operation *op, SlapReply *rs) {
844 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
845 Entry * e = op->ora_e;
846 struct berval dn = e->e_nname;
847 struct berval leaf_path = BER_BVNULL;
850 char textbuf[SLAP_TEXT_BUFLEN];
852 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
854 rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
855 &rs->sr_text, textbuf, sizeof( textbuf ) );
856 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
858 rs->sr_err = slap_add_opattrs( op,
859 &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
860 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
862 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
864 dn2path(&dn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &leaf_path);
866 if(leaf_path.bv_val != NULL) {
867 struct berval base = BER_BVNULL;
868 /* build path to container and ldif of container */
869 get_parent_path(&leaf_path, &base);
871 statres = stat(base.bv_val, &stats); /* check if container exists */
872 if(statres == -1 && errno == ENOENT) { /* container missing */
873 base.bv_val[base.bv_len] = '.';
874 statres = stat(base.bv_val, &stats); /* check for leaf node */
875 base.bv_val[base.bv_len] = '\0';
876 if(statres == -1 && errno == ENOENT) {
877 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
878 rs->sr_text = "Parent does not exist";
880 else if(statres != -1) { /* create parent */
881 int mkdirres = mkdir(base.bv_val, 0750);
883 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
884 rs->sr_text = "Could not create parent folder";
885 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
886 base.bv_val, STRERROR( errno ), 0 );
890 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
891 }/* container was possibly created, move on to add the entry */
892 if(rs->sr_err == LDAP_SUCCESS) {
893 statres = stat(leaf_path.bv_val, &stats);
894 if(statres == -1 && errno == ENOENT) {
895 rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
897 else if ( statres == -1 ) {
898 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
899 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
900 leaf_path.bv_val, STRERROR( errno ), 0 );
902 else /* it already exists */
903 rs->sr_err = LDAP_ALREADY_EXISTS;
905 SLAP_FREE(base.bv_val);
906 SLAP_FREE(leaf_path.bv_val);
909 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
912 Debug( LDAP_DEBUG_TRACE,
913 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
914 rs->sr_text : "", 0);
915 send_ldap_result(op, rs);
916 slap_graduate_commit_csn( op );
920 static int ldif_back_modify(Operation *op, SlapReply *rs) {
921 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
922 Modifications * modlst = op->orm_modlist;
923 struct berval path = BER_BVNULL;
924 Entry * entry = NULL;
927 slap_mods_opattrs( op, &op->orm_modlist, 1 );
929 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
930 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path,
932 entry = get_entry(op, &li->li_base_path);
935 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
936 if(rs->sr_err == LDAP_SUCCESS) {
938 spew_res = spew_entry(entry, &path, 1, &save_errno);
940 Debug( LDAP_DEBUG_ANY,
941 "%s ldif_back_modify: could not output entry \"%s\": %s\n",
942 op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
943 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
948 rs->sr_err = LDAP_NO_SUCH_OBJECT;
953 if(path.bv_val != NULL)
954 SLAP_FREE(path.bv_val);
956 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
957 send_ldap_result(op, rs);
958 slap_graduate_commit_csn( op );
962 static int ldif_back_delete(Operation *op, SlapReply *rs) {
963 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
964 struct berval path = BER_BVNULL;
967 if ( BER_BVISEMPTY( &op->o_csn )) {
969 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
972 csn.bv_len = sizeof( csnbuf );
973 slap_get_csn( op, &csn, 1 );
976 ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
977 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &li->li_base_path, &path);
979 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
980 res = rmdir(path.bv_val);
981 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
982 rs->sr_err = LDAP_SUCCESS;
986 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
990 rs->sr_err = LDAP_NO_SUCH_OBJECT;
994 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
999 res = unlink(path.bv_val);
1003 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1007 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1013 SLAP_FREE(path.bv_val);
1014 ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
1015 send_ldap_result(op, rs);
1016 slap_graduate_commit_csn( op );
1021 static int move_entry(Entry * entry, struct berval * ndn,
1022 struct berval * newndn, struct berval * suffixdn,
1023 struct berval * base_path) {
1027 struct berval newpath;
1029 dn2path(ndn, suffixdn, base_path, &path);
1030 dn2path(newndn, suffixdn, base_path, &newpath);
1032 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
1033 /* some object doesn't exist */
1034 res = LDAP_NO_SUCH_OBJECT;
1036 else { /* do the modrdn */
1037 exists_res = open(newpath.bv_val, O_RDONLY);
1038 if(exists_res == -1 && errno == ENOENT) {
1039 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
1040 res = spew_entry(entry, &newpath, 0, NULL);
1042 /* if this fails we should log something bad */
1043 res = unlink(path.bv_val);
1044 path.bv_val[path.bv_len - STRLENOF(".ldif")] = '\0';
1045 newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
1046 res = rename(path.bv_val, newpath.bv_val);
1051 res = LDAP_NO_SUCH_OBJECT;
1053 res = LDAP_UNWILLING_TO_PERFORM;
1054 unlink(newpath.bv_val); /* in case file was created */
1056 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1058 else if(exists_res) {
1059 int close_res = close(exists_res);
1060 res = LDAP_ALREADY_EXISTS;
1061 if(close_res == -1) {
1062 /* log heinous error */
1066 res = LDAP_UNWILLING_TO_PERFORM;
1070 if(newpath.bv_val != NULL)
1071 SLAP_FREE(newpath.bv_val);
1072 if(path.bv_val != NULL)
1073 SLAP_FREE(path.bv_val);
1078 ldif_back_modrdn(Operation *op, SlapReply *rs)
1080 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1081 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1083 Entry * entry = NULL;
1086 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1088 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1089 entry = get_entry( op, &li->li_base_path );
1091 /* build the mods to the entry */
1092 if ( entry != NULL ) {
1093 /* build new dn, and new ndn for the entry */
1094 if ( op->oq_modrdn.rs_newSup != NULL ) {
1095 struct berval op_dn = op->o_req_dn,
1096 op_ndn = op->o_req_ndn;
1100 p_dn = *op->oq_modrdn.rs_newSup;
1101 op->o_req_dn = *op->oq_modrdn.rs_newSup;
1102 op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
1103 np = get_entry( op, &li->li_base_path );
1104 op->o_req_dn = op_dn;
1105 op->o_req_ndn = op_ndn;
1107 goto no_such_object;
1111 dnParent( &entry->e_name, &p_dn );
1113 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1114 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1115 ber_memfree_x( entry->e_name.bv_val, NULL );
1116 ber_memfree_x( entry->e_nname.bv_val, NULL );
1117 entry->e_name = new_dn;
1118 entry->e_nname = new_ndn;
1120 /* perform the modifications */
1121 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1122 if ( res == LDAP_SUCCESS ) {
1123 rs->sr_err = move_entry( entry, &op->o_req_ndn,
1125 &op->o_bd->be_nsuffix[0],
1126 &li->li_base_path );
1132 /* entry was null */
1133 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1136 if ( entry != NULL ) {
1137 entry_free( entry );
1140 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1141 send_ldap_result( op, rs );
1142 slap_graduate_commit_csn( op );
1146 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1148 int ldif_back_entry_get(
1152 AttributeDescription *at,
1156 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1157 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1159 assert( ndn != NULL );
1160 assert( !BER_BVISNULL( ndn ) );
1162 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1163 op->o_req_dn = *ndn;
1164 op->o_req_ndn = *ndn;
1165 *ent = get_entry( op, &li->li_base_path );
1166 op->o_req_dn = op_dn;
1167 op->o_req_ndn = op_ndn;
1168 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1170 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
1175 return ( *ent == NULL ? 1 : 0 );
1178 static int ldif_tool_entry_open(BackendDB *be, int mode) {
1179 struct ldif_info *li = (struct ldif_info *) be->be_private;
1180 li->li_tool_current = 0;
1184 static int ldif_tool_entry_close(BackendDB * be) {
1185 struct ldif_info *li = (struct ldif_info *) be->be_private;
1187 SLAP_FREE(li->li_tool_cookie.entries);
1191 static ID ldif_tool_entry_next(BackendDB *be)
1193 struct ldif_info *li = (struct ldif_info *) be->be_private;
1194 if(li->li_tool_current >= li->li_tool_cookie.eind)
1197 return ++li->li_tool_current;
1201 ldif_tool_entry_first(BackendDB *be)
1203 struct ldif_info *li = (struct ldif_info *) be->be_private;
1205 if(li->li_tool_cookie.entries == NULL) {
1209 op.o_req_dn = *be->be_suffix;
1210 op.o_req_ndn = *be->be_nsuffix;
1211 op.ors_scope = LDAP_SCOPE_SUBTREE;
1212 li->li_tool_cookie.op = &op;
1213 (void)enum_tree( &li->li_tool_cookie );
1214 li->li_tool_cookie.op = NULL;
1216 return ldif_tool_entry_next( be );
1219 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1220 struct ldif_info *li = (struct ldif_info *) be->be_private;
1223 if(id > li->li_tool_cookie.eind || id < 1)
1226 e = li->li_tool_cookie.entries[id - 1];
1227 li->li_tool_cookie.entries[id - 1] = NULL;
1232 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1233 struct ldif_info *li = (struct ldif_info *) be->be_private;
1234 struct berval dn = e->e_nname;
1235 struct berval leaf_path = BER_BVNULL;
1238 int res = LDAP_SUCCESS;
1240 dn2path(&dn, &be->be_nsuffix[0], &li->li_base_path, &leaf_path);
1242 if(leaf_path.bv_val != NULL) {
1243 struct berval base = BER_BVNULL;
1244 /* build path to container, and path to ldif of container */
1245 get_parent_path(&leaf_path, &base);
1247 statres = stat(base.bv_val, &stats); /* check if container exists */
1248 if(statres == -1 && errno == ENOENT) { /* container missing */
1249 base.bv_val[base.bv_len] = '.';
1250 statres = stat(base.bv_val, &stats); /* check for leaf node */
1251 base.bv_val[base.bv_len] = '\0';
1252 if(statres == -1 && errno == ENOENT) {
1253 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1255 else if(statres != -1) { /* create parent */
1256 int mkdirres = mkdir(base.bv_val, 0750);
1257 if(mkdirres == -1) {
1258 res = LDAP_UNWILLING_TO_PERFORM;
1262 res = LDAP_UNWILLING_TO_PERFORM;
1263 }/* container was possibly created, move on to add the entry */
1264 if(res == LDAP_SUCCESS) {
1265 statres = stat(leaf_path.bv_val, &stats);
1266 if(statres == -1 && errno == ENOENT) {
1267 res = spew_entry(e, &leaf_path, 0, NULL);
1269 else /* it already exists */
1270 res = LDAP_ALREADY_EXISTS;
1272 SLAP_FREE(base.bv_val);
1273 SLAP_FREE(leaf_path.bv_val);
1276 if(res == LDAP_SUCCESS) {
1284 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1286 struct ldif_info *li;
1288 li = ch_calloc( 1, sizeof(struct ldif_info) );
1289 be->be_private = li;
1290 be->be_cf_ocs = ldifocs;
1291 ldap_pvt_thread_rdwr_init(&li->li_rdwr);
1296 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1298 struct ldif_info *li = be->be_private;
1300 ch_free(li->li_base_path.bv_val);
1301 ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
1302 free( be->be_private );
1307 ldif_back_db_open( Backend *be, ConfigReply *cr)
1309 struct ldif_info *li = (struct ldif_info *) be->be_private;
1310 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1311 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1318 ldif_back_initialize(
1322 static char *controls[] = {
1323 LDAP_CONTROL_MANAGEDSAIT,
1329 SLAP_BFLAG_INCREMENT |
1330 SLAP_BFLAG_REFERRALS;
1332 bi->bi_controls = controls;
1339 bi->bi_db_init = ldif_back_db_init;
1340 bi->bi_db_config = config_generic_wrapper;
1341 bi->bi_db_open = ldif_back_db_open;
1342 bi->bi_db_close = 0;
1343 bi->bi_db_destroy = ldif_back_db_destroy;
1345 bi->bi_op_bind = ldif_back_bind;
1346 bi->bi_op_unbind = 0;
1347 bi->bi_op_search = ldif_back_search;
1348 bi->bi_op_compare = 0;
1349 bi->bi_op_modify = ldif_back_modify;
1350 bi->bi_op_modrdn = ldif_back_modrdn;
1351 bi->bi_op_add = ldif_back_add;
1352 bi->bi_op_delete = ldif_back_delete;
1353 bi->bi_op_abandon = 0;
1355 bi->bi_extended = 0;
1357 bi->bi_chk_referrals = ldif_back_referrals;
1359 bi->bi_connection_init = 0;
1360 bi->bi_connection_destroy = 0;
1362 bi->bi_entry_get_rw = ldif_back_entry_get;
1364 #if 0 /* NOTE: uncomment to completely disable access control */
1365 bi->bi_access_allowed = slap_access_always_allowed;
1368 bi->bi_tool_entry_open = ldif_tool_entry_open;
1369 bi->bi_tool_entry_close = ldif_tool_entry_close;
1370 bi->bi_tool_entry_first = ldif_tool_entry_first;
1371 bi->bi_tool_entry_next = ldif_tool_entry_next;
1372 bi->bi_tool_entry_get = ldif_tool_entry_get;
1373 bi->bi_tool_entry_put = ldif_tool_entry_put;
1374 bi->bi_tool_entry_reindex = 0;
1375 bi->bi_tool_sync = 0;
1377 bi->bi_tool_dn2id_get = 0;
1378 bi->bi_tool_entry_modify = 0;
1380 bi->bi_cf_ocs = ldifocs;
1382 rc = config_register_schema( ldifcfg, ldifocs );
1383 if ( rc ) return rc;