1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2006 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_mutex_t li_mutex;
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 * rootdn, struct berval * base_path,
88 char *ptr, *sep, *end;
90 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
91 res->bv_val = ch_malloc( res->bv_len + 1 );
92 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
93 *ptr++ = LDAP_DIRSEP[0];
94 ptr = lutil_strcopy( ptr, rootdn->bv_val );
95 end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
96 while ( end > dn->bv_val ) {
97 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
98 *ptr++ = LDAP_DIRSEP[0];
99 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
105 while( ptr=strchr(ptr, IX_DNL) ) {
107 ptr = strchr(ptr, IX_DNR);
116 static char * slurp_file(int fd) {
117 int read_chars_total = 0;
125 entry_size = st.st_size;
126 entry = ch_malloc( entry_size+1 );
130 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
131 if(read_chars == -1) {
135 if(read_chars == 0) {
136 entry[read_chars_total] = '\0';
140 read_chars_total += read_chars;
141 entry_pos += read_chars;
147 static int spew_file(int fd, char * spew, int len) {
151 writeres = write(fd, spew, len);
153 perror("could not spew write");
164 static int spew_entry(Entry * e, struct berval * path) {
169 char * entry_as_string;
171 openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
174 rs = LDAP_NO_SUCH_OBJECT;
176 rs = LDAP_UNWILLING_TO_PERFORM;
177 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
178 path->bv_val, strerror( errno ), 0 );
184 /* Only save the RDN onto disk */
185 dnRdn( &e->e_name, &rdn );
186 if ( rdn.bv_len != e->e_name.bv_len ) {
187 e->e_name.bv_val[rdn.bv_len] = '\0';
188 tmp = e->e_name.bv_len;
189 e->e_name.bv_len = rdn.bv_len;
193 entry_as_string = entry2str(e, &entry_length);
195 /* Restore full DN */
196 if ( rdn.bv_len != e->e_name.bv_len ) {
197 e->e_name.bv_val[e->e_name.bv_len] = ',';
198 e->e_name.bv_len = rdn.bv_len;
201 if(entry_as_string == NULL) {
202 rs = LDAP_UNWILLING_TO_PERFORM;
206 spew_res = spew_file(openres, entry_as_string, entry_length);
209 rs = LDAP_UNWILLING_TO_PERFORM;
217 static Entry * get_entry_for_fd(int fd,
221 char * entry = (char *) slurp_file(fd);
222 Entry * ldentry = NULL;
224 /* error reading file */
229 ldentry = str2entry(entry);
232 rdn = ldentry->e_name;
233 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
234 ch_free( rdn.bv_val );
235 rdn = ldentry->e_nname;
236 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
237 ch_free( rdn.bv_val );
251 static Entry * get_entry(Operation *op, struct berval *base_path) {
252 struct berval path, pdn, pndn;
255 dnParent(&op->o_req_dn, &pdn);
256 dnParent(&op->o_req_ndn, &pndn);
257 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
258 fd = open(path.bv_val, O_RDONLY);
259 /* error opening file (mebbe should log error) */
261 perror("failed to open file");
264 if(path.bv_val != NULL)
265 SLAP_FREE(path.bv_val);
268 return get_entry_for_fd(fd, &pdn, &pndn);
274 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
276 res->bv_len = name->bv_len + base->bv_len + 1;
277 res->bv_val = ch_malloc( res->bv_len + 1 );
278 strcpy(res->bv_val, base->bv_val);
279 ptr = res->bv_val + base->bv_len;
280 *ptr++ = LDAP_DIRSEP[0];
281 strcpy(ptr, name->bv_val);
284 typedef struct bvlist {
293 static int r_enum_tree(enumCookie *ck, struct berval *path,
294 struct berval *pdn, struct berval *pndn)
297 int fd, rc = LDAP_SUCCESS;
299 fd = open( path->bv_val, O_RDONLY );
301 Debug( LDAP_DEBUG_ANY,
302 "=> ldif_enum_tree: failed to open %s: %s\n",
303 path->bv_val, strerror(errno), 0 );
304 return LDAP_NO_SUCH_OBJECT;
307 e = get_entry_for_fd(fd, pdn, pndn);
309 Debug( LDAP_DEBUG_ANY,
310 "=> ldif_enum_tree: failed to read entry for %s\n",
311 path->bv_val, 0, 0 );
315 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
316 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
317 /* Send right away? */
320 * if it's a referral, add it to the list of referrals. only do
321 * this for non-base searches, and don't check the filter
322 * explicitly here since it's only a candidate anyway.
324 if ( !get_manageDSAit( ck->op )
325 && ck->op->ors_scope != LDAP_SCOPE_BASE
326 && is_entry_referral( e ) )
328 BerVarray erefs = get_entry_referrals( ck->op, e );
329 ck->rs->sr_ref = referral_rewrite( erefs,
331 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
332 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
334 ck->rs->sr_entry = e;
335 rc = send_search_reference( ck->op, ck->rs );
336 ber_bvarray_free( ck->rs->sr_ref );
337 ber_bvarray_free( erefs );
338 ck->rs->sr_ref = NULL;
339 ck->rs->sr_entry = NULL;
341 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
343 ck->rs->sr_entry = e;
344 ck->rs->sr_attrs = ck->op->ors_attrs;
345 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
346 rc = send_search_entry(ck->op, ck->rs);
347 ck->rs->sr_entry = NULL;
353 /* Queueing up for tool mode */
354 if(ck->entries == NULL) {
355 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
356 ck->elen = ENTRY_BUFF_INCREMENT;
358 if(ck->eind >= ck->elen) { /* grow entries if necessary */
359 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
363 ck->entries[ck->eind++] = e;
370 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
372 bvlist *list = NULL, *ptr;
374 path->bv_len -= STRLENOF( LDIF );
375 path->bv_val[path->bv_len] = '\0';
377 dir_of_path = opendir(path->bv_val);
378 if(dir_of_path == NULL) { /* can't open directory */
379 if ( errno != ENOENT ) {
380 /* it shouldn't be treated as an error
381 * only if the directory doesn't exist */
383 Debug( LDAP_DEBUG_TRACE,
384 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
385 path->bv_val, errno, 0 );
391 struct berval fname, itmp;
395 dir = readdir(dir_of_path);
396 if(dir == NULL) break; /* end of the directory */
397 fname.bv_len = strlen( dir->d_name );
398 if ( fname.bv_len <= STRLENOF( LDIF ))
400 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
402 fname.bv_val = dir->d_name;
404 bvl = ch_malloc( sizeof(bvlist) );
405 ber_dupbv( &bvl->bv, &fname );
406 BER_BVZERO( &bvl->num );
407 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
411 ptr = strchr( itmp.bv_val, IX_FSR );
413 itmp.bv_len = ptr - itmp.bv_val;
414 ber_dupbv( &bvl->num, &itmp );
415 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
416 itmp.bv_val[0] = '\0';
417 bvl->off = itmp.bv_val - bvl->bv.bv_val;
421 for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
422 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
423 if ( !cmp && bvl->num.bv_val )
424 cmp = bvl->inum - ptr->inum;
432 closedir(dir_of_path);
434 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
435 ck->op->ors_scope = LDAP_SCOPE_BASE;
436 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
437 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
439 while ( ( ptr = list ) ) {
444 if ( rc == LDAP_SUCCESS ) {
445 if ( ptr->num.bv_val )
446 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
448 fullpath( path, &ptr->bv, &fpath );
449 rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
452 if ( ptr->num.bv_val )
453 free( ptr->num.bv_val );
454 free(ptr->bv.bv_val);
459 if ( fd ) entry_free( e );
468 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
470 struct berval pdn, pndn;
473 dnParent( &ck->op->o_req_dn, &pdn );
474 dnParent( &ck->op->o_req_ndn, &pndn );
475 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
476 rc = r_enum_tree(ck, &path, &pdn, &pndn);
477 ch_free( path.bv_val );
481 /* Get the parent path plus the LDIF suffix */
482 static void get_parent_path(struct berval * dnpath, struct berval *res) {
483 int dnpathlen = dnpath->bv_len;
486 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
487 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
490 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
491 strncpy(res->bv_val, dnpath->bv_val, i);
492 strcpy(res->bv_val+i, LDIF);
493 res->bv_val[i] = '\0';
496 static int apply_modify_to_entry(Entry * entry,
497 Modifications * modlist,
501 char textbuf[SLAP_TEXT_BUFLEN];
502 int rc = LDAP_UNWILLING_TO_PERFORM;
503 Modification *mods = NULL;
505 if (!acl_check_modlist(op, entry, modlist)) {
506 return LDAP_INSUFFICIENT_ACCESS;
509 for (; modlist != NULL; modlist = modlist->sml_next) {
510 mods = &modlist->sml_mod;
512 switch (mods->sm_op) {
514 rc = modify_add_values(entry, mods,
515 get_permissiveModify(op),
516 &rs->sr_text, textbuf,
520 case LDAP_MOD_DELETE:
521 rc = modify_delete_values(entry, mods,
522 get_permissiveModify(op),
523 &rs->sr_text, textbuf,
527 case LDAP_MOD_REPLACE:
528 rc = modify_replace_values(entry, mods,
529 get_permissiveModify(op),
530 &rs->sr_text, textbuf,
534 case LDAP_MOD_INCREMENT:
535 rc = modify_increment_values( entry,
536 mods, get_permissiveModify(op),
537 &rs->sr_text, textbuf,
543 case SLAP_MOD_SOFTADD:
544 mods->sm_op = LDAP_MOD_ADD;
545 rc = modify_add_values(entry, mods,
546 get_permissiveModify(op),
547 &rs->sr_text, textbuf,
549 mods->sm_op = SLAP_MOD_SOFTADD;
550 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
557 if(rc != LDAP_SUCCESS) break;
560 if(rc == LDAP_SUCCESS) {
561 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
562 entry->e_ocflags = 0;
564 /* check that the entry still obeys the schema */
565 rc = entry_schema_check(op, entry, NULL, 0,
566 &rs->sr_text, textbuf, sizeof( textbuf ) );
572 ldif_back_referrals( Operation *op, SlapReply *rs )
574 struct ldif_info *ni = NULL;
576 int rc = LDAP_SUCCESS;
579 if ( op->o_tag == LDAP_REQ_SEARCH ) {
580 /* let search take care of itself */
585 if ( get_manageDSAit( op ) ) {
586 /* let op take care of DSA management */
590 ni = (struct ldif_info *)op->o_bd->be_private;
591 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
592 entry = (Entry *)get_entry( op, &ni->li_base_path );
594 /* no object is found for them */
595 if ( entry == NULL ) {
596 struct berval odn = op->o_req_dn;
597 struct berval ondn = op->o_req_ndn;
599 struct berval pndn = op->o_req_ndn;
601 for ( ; entry == NULL; ) {
602 dnParent( &pndn, &pndn );
604 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
609 op->o_req_ndn = pndn;
611 entry = (Entry *)get_entry( op, &ni->li_base_path );
614 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
617 op->o_req_ndn = ondn;
620 rs->sr_matched = NULL;
621 if ( entry != NULL ) {
622 Debug( LDAP_DEBUG_TRACE,
623 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
624 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
626 if ( is_entry_referral( entry ) ) {
628 rs->sr_ref = get_entry_referrals( op, entry );
630 rs->sr_matched = ber_strdup_x(
631 entry->e_name.bv_val, op->o_tmpmemctx );
637 } else if ( default_referral != NULL ) {
639 rs->sr_ref = referral_rewrite( default_referral,
640 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
643 if ( rs->sr_ref != NULL ) {
645 rc = rs->sr_err = LDAP_REFERRAL;
646 send_ldap_result( op, rs );
647 ber_bvarray_free( rs->sr_ref );
650 } else if ( rc != LDAP_SUCCESS ) {
652 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
653 send_ldap_result( op, rs );
656 if ( rs->sr_matched ) {
657 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
658 rs->sr_matched = NULL;
664 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
666 if ( is_entry_referral( entry ) ) {
667 /* entry is a referral */
668 BerVarray refs = get_entry_referrals( op, entry );
669 rs->sr_ref = referral_rewrite(
670 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
672 Debug( LDAP_DEBUG_TRACE,
673 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
674 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
676 rs->sr_matched = entry->e_name.bv_val;
677 if ( rs->sr_ref != NULL ) {
678 rc = rs->sr_err = LDAP_REFERRAL;
679 send_ldap_result( op, rs );
680 ber_bvarray_free( rs->sr_ref );
684 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
688 rs->sr_matched = NULL;
689 ber_bvarray_free( refs );
698 ldif_back_bind( Operation *op, SlapReply *rs )
700 struct ldif_info *ni = NULL;
701 Attribute * a = NULL;
702 AttributeDescription *password = slap_schema.si_ad_userPassword;
704 Entry * entry = NULL;
706 ni = (struct ldif_info *) op->o_bd->be_private;
707 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
708 entry = (Entry *) get_entry(op, &ni->li_base_path);
710 /* no object is found for them */
712 if(be_isroot_pw(op)) {
713 rs->sr_err = return_val = LDAP_SUCCESS;
715 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
720 /* they don't have userpassword */
721 if((a = attr_find(entry->e_attrs, password)) == NULL) {
722 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
727 /* authentication actually failed */
728 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
729 &rs->sr_text) != 0) {
730 rs->sr_err = LDAP_INVALID_CREDENTIALS;
735 /* let the front-end send success */
740 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
742 send_ldap_result( op, rs );
748 static int ldif_back_search(Operation *op, SlapReply *rs)
750 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
751 enumCookie ck = { NULL, NULL, NULL, 0, 0 };
755 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
756 rs->sr_err = enum_tree( &ck );
757 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
758 send_ldap_result(op, rs);
763 static int ldif_back_add(Operation *op, SlapReply *rs) {
764 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
765 Entry * e = op->ora_e;
766 struct berval dn = e->e_nname;
767 struct berval leaf_path = BER_BVNULL;
770 char textbuf[SLAP_TEXT_BUFLEN];
772 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
773 slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
775 rs->sr_err = entry_schema_check(op, e, NULL, 0,
776 &rs->sr_text, textbuf, sizeof( textbuf ) );
777 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
779 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
781 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
783 if(leaf_path.bv_val != NULL) {
784 struct berval base = BER_BVNULL;
785 /* build path to container and ldif of container */
786 get_parent_path(&leaf_path, &base);
788 statres = stat(base.bv_val, &stats); /* check if container exists */
789 if(statres == -1 && errno == ENOENT) { /* container missing */
790 base.bv_val[base.bv_len] = '.';
791 statres = stat(base.bv_val, &stats); /* check for leaf node */
792 base.bv_val[base.bv_len] = '\0';
793 if(statres == -1 && errno == ENOENT) {
794 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
795 rs->sr_text = "Parent does not exist";
797 else if(statres != -1) { /* create parent */
798 int mkdirres = mkdir(base.bv_val, 0750);
800 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
801 rs->sr_text = "Could not create parent folder";
802 Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
803 base.bv_val, strerror( errno ), 0 );
807 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
808 }/* container was possibly created, move on to add the entry */
809 if(rs->sr_err == LDAP_SUCCESS) {
810 statres = stat(leaf_path.bv_val, &stats);
811 if(statres == -1 && errno == ENOENT) {
812 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
813 rs->sr_err = (int) spew_entry(e, &leaf_path);
814 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
816 else if ( statres == -1 ) {
817 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
818 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
819 leaf_path.bv_val, strerror( errno ), 0 );
821 else /* it already exists */
822 rs->sr_err = LDAP_ALREADY_EXISTS;
824 SLAP_FREE(base.bv_val);
825 SLAP_FREE(leaf_path.bv_val);
828 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
831 Debug( LDAP_DEBUG_TRACE,
832 "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text, 0);
833 send_ldap_result(op, rs);
834 slap_graduate_commit_csn( op );
838 static int ldif_back_modify(Operation *op, SlapReply *rs) {
839 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
840 Modifications * modlst = op->orm_modlist;
841 struct berval path = BER_BVNULL;
842 Entry * entry = NULL;
845 slap_mods_opattrs( op, &op->orm_modlist, 1 );
847 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
848 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
850 entry = (Entry *) get_entry(op, &ni->li_base_path);
853 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
854 if(rs->sr_err == LDAP_SUCCESS) {
855 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
856 spew_res = spew_entry(entry, &path);
857 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
859 perror("could not output entry");
860 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
865 rs->sr_err = LDAP_NO_SUCH_OBJECT;
870 if(path.bv_val != NULL)
871 SLAP_FREE(path.bv_val);
873 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
874 send_ldap_result(op, rs);
875 slap_graduate_commit_csn( op );
879 static int ldif_back_delete(Operation *op, SlapReply *rs) {
880 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
881 struct berval path = BER_BVNULL;
884 if ( BER_BVISEMPTY( &op->o_csn )) {
886 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
889 csn.bv_len = sizeof( csnbuf );
890 slap_get_csn( op, &csn, 1 );
893 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
894 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
896 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
897 res = rmdir(path.bv_val);
898 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
899 if ( res && errno != ENOENT ) {
900 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
902 res = unlink(path.bv_val);
907 rs->sr_err = LDAP_NO_SUCH_OBJECT;
909 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
912 rs->sr_err = LDAP_SUCCESS;
914 SLAP_FREE(path.bv_val);
915 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
916 send_ldap_result(op, rs);
917 slap_graduate_commit_csn( op );
922 static int move_entry(Entry * entry, struct berval * ndn,
923 struct berval * newndn, struct berval * rootdn,
924 struct berval * base_path) {
928 struct berval newpath;
930 dn2path(ndn, rootdn, base_path, &path);
931 dn2path(newndn, rootdn, base_path, &newpath);
933 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
934 /* some object doesn't exist */
935 res = LDAP_NO_SUCH_OBJECT;
937 else { /* do the modrdn */
938 exists_res = open(newpath.bv_val, O_RDONLY);
939 if(exists_res == -1 && errno == ENOENT) {
940 res = spew_entry(entry, &newpath);
942 /* if this fails we should log something bad */
943 res = unlink(path.bv_val);
948 res = LDAP_NO_SUCH_OBJECT;
950 res = LDAP_UNWILLING_TO_PERFORM;
951 unlink(newpath.bv_val); /* in case file was created */
954 else if(exists_res) {
955 int close_res = close(exists_res);
956 res = LDAP_ALREADY_EXISTS;
957 if(close_res == -1) {
958 /* log heinous error */
962 res = LDAP_UNWILLING_TO_PERFORM;
966 if(newpath.bv_val != NULL)
967 SLAP_FREE(newpath.bv_val);
968 if(path.bv_val != NULL)
969 SLAP_FREE(path.bv_val);
973 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
974 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
975 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
976 struct berval p_dn, bv = BER_BVNULL;
977 Entry * entry = NULL;
978 LDAPRDN new_rdn = NULL;
979 LDAPRDN old_rdn = NULL;
980 Modifications * mods = NULL;
983 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
984 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
985 entry = (Entry *) get_entry(op, &ni->li_base_path);
987 /* build the mods to the entry */
989 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
990 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
991 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
993 else if(op->oq_modrdn.rs_deleteoldrdn &&
994 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
995 LDAP_DN_FORMAT_LDAP)) {
996 rs->sr_err = LDAP_OTHER;
998 else { /* got both rdns successfully, ready to build mods */
999 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
1001 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1003 else { /* built mods successfully */
1005 /* build new dn, and new ndn for the entry */
1006 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
1007 p_dn = *op->oq_modrdn.rs_newSup;
1009 p_dn = slap_empty_bv;
1010 dnParent(&entry->e_name, &p_dn);
1011 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
1012 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
1013 ber_dupbv( &new_ndn, &bv );
1014 entry->e_name = new_dn;
1015 entry->e_nname = new_ndn;
1017 /* perform the modifications */
1018 res = apply_modify_to_entry(entry, mods, op, rs);
1019 if(res == LDAP_SUCCESS) {
1020 rs->sr_err = move_entry(entry, &op->o_req_ndn,
1022 &op->o_bd->be_nsuffix[0],
1030 else /* entry was null */
1031 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1036 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1037 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1038 send_ldap_result(op, rs);
1039 slap_graduate_commit_csn( op );
1043 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1045 int ldif_back_entry_get(
1049 AttributeDescription *at,
1053 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1055 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
1057 *ent = (Entry *) get_entry( op, &ni->li_base_path );
1059 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1061 return ( *ent == NULL ? 1 : 0 );
1064 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1065 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1066 ni->li_tool_current = 0;
1070 static int ldif_tool_entry_close(BackendDB * be) {
1071 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1073 SLAP_FREE(ni->li_tool_cookie.entries);
1078 ldif_tool_entry_first(BackendDB *be)
1080 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1081 ID id = 1; /* first entry in the array of entries shifted by one */
1083 ni->li_tool_current = 1;
1084 if(ni->li_tool_cookie.entries == NULL) {
1088 op.o_req_dn = *be->be_suffix;
1089 op.o_req_ndn = *be->be_nsuffix;
1090 op.ors_scope = LDAP_SCOPE_SUBTREE;
1091 ni->li_tool_cookie.op = &op;
1092 (void)enum_tree( &ni->li_tool_cookie );
1093 ni->li_tool_cookie.op = NULL;
1098 static ID ldif_tool_entry_next(BackendDB *be)
1100 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1101 ni->li_tool_current += 1;
1102 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1105 return ni->li_tool_current;
1108 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1109 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1112 if(id > ni->li_tool_cookie.eind || id < 1)
1115 e = ni->li_tool_cookie.entries[id - 1];
1116 ni->li_tool_cookie.entries[id - 1] = NULL;
1121 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1122 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1123 struct berval dn = e->e_nname;
1124 struct berval leaf_path = BER_BVNULL;
1127 int res = LDAP_SUCCESS;
1129 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1131 if(leaf_path.bv_val != NULL) {
1132 struct berval base = BER_BVNULL;
1133 /* build path to container, and path to ldif of container */
1134 get_parent_path(&leaf_path, &base);
1136 statres = stat(base.bv_val, &stats); /* check if container exists */
1137 if(statres == -1 && errno == ENOENT) { /* container missing */
1138 base.bv_val[base.bv_len] = '.';
1139 statres = stat(base.bv_val, &stats); /* check for leaf node */
1140 base.bv_val[base.bv_len] = '\0';
1141 if(statres == -1 && errno == ENOENT) {
1142 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1144 else if(statres != -1) { /* create parent */
1145 int mkdirres = mkdir(base.bv_val, 0750);
1146 if(mkdirres == -1) {
1147 res = LDAP_UNWILLING_TO_PERFORM;
1151 res = LDAP_UNWILLING_TO_PERFORM;
1152 }/* container was possibly created, move on to add the entry */
1153 if(res == LDAP_SUCCESS) {
1154 statres = stat(leaf_path.bv_val, &stats);
1155 if(statres == -1 && errno == ENOENT) {
1156 res = (int) spew_entry(e, &leaf_path);
1158 else /* it already exists */
1159 res = LDAP_ALREADY_EXISTS;
1161 SLAP_FREE(base.bv_val);
1162 SLAP_FREE(leaf_path.bv_val);
1165 if(res == LDAP_SUCCESS) {
1173 ldif_back_db_init( BackendDB *be )
1175 struct ldif_info *ni;
1177 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1178 be->be_private = ni;
1179 be->be_cf_ocs = ldifocs;
1180 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1185 ldif_back_db_destroy(
1189 struct ldif_info *ni = be->be_private;
1191 ch_free(ni->li_base_path.bv_val);
1192 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1193 free( be->be_private );
1202 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1203 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1204 fprintf(stderr, "missing base path for back-ldif\n");
1211 ldif_back_initialize(
1215 static char *controls[] = {
1216 LDAP_CONTROL_MANAGEDSAIT,
1222 SLAP_BFLAG_INCREMENT |
1223 SLAP_BFLAG_REFERRALS;
1225 bi->bi_controls = controls;
1232 bi->bi_db_init = ldif_back_db_init;
1233 bi->bi_db_config = config_generic_wrapper;
1234 bi->bi_db_open = ldif_back_db_open;
1235 bi->bi_db_close = 0;
1236 bi->bi_db_destroy = ldif_back_db_destroy;
1238 bi->bi_op_bind = ldif_back_bind;
1239 bi->bi_op_unbind = 0;
1240 bi->bi_op_search = ldif_back_search;
1241 bi->bi_op_compare = 0;
1242 bi->bi_op_modify = ldif_back_modify;
1243 bi->bi_op_modrdn = ldif_back_modrdn;
1244 bi->bi_op_add = ldif_back_add;
1245 bi->bi_op_delete = ldif_back_delete;
1246 bi->bi_op_abandon = 0;
1248 bi->bi_extended = 0;
1250 bi->bi_chk_referrals = ldif_back_referrals;
1252 bi->bi_connection_init = 0;
1253 bi->bi_connection_destroy = 0;
1255 bi->bi_entry_get_rw = ldif_back_entry_get;
1257 #if 0 /* NOTE: uncomment to completely disable access control */
1258 #ifdef SLAP_OVERLAY_ACCESS
1259 bi->bi_access_allowed = slap_access_always_allowed;
1260 #endif /* SLAP_OVERLAY_ACCESS */
1263 bi->bi_tool_entry_open = ldif_tool_entry_open;
1264 bi->bi_tool_entry_close = ldif_tool_entry_close;
1265 bi->bi_tool_entry_first = ldif_tool_entry_first;
1266 bi->bi_tool_entry_next = ldif_tool_entry_next;
1267 bi->bi_tool_entry_get = ldif_tool_entry_get;
1268 bi->bi_tool_entry_put = ldif_tool_entry_put;
1269 bi->bi_tool_entry_reindex = 0;
1270 bi->bi_tool_sync = 0;
1272 bi->bi_tool_dn2id_get = 0;
1273 bi->bi_tool_id2entry_get = 0;
1274 bi->bi_tool_entry_modify = 0;
1276 bi->bi_cf_ocs = ldifocs;
1278 rc = config_register_schema( ldifcfg, ldifocs );
1279 if ( rc ) return rc;