1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005 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 ObjectClass *ldif_oc;
66 static ConfigDriver ldif_cf;
68 static ConfigTable ldifcfg[] = {
69 { "", "", 0, 0, 0, ARG_MAGIC,
70 ldif_cf, NULL, NULL, NULL },
71 { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
72 (void *)offsetof(struct ldif_info, li_base_path),
73 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
74 "DESC 'Directory for database content' "
75 "EQUALITY caseIgnoreMatch "
76 "SYNTAX OMsDirectoryString )", NULL, NULL },
77 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
78 NULL, NULL, NULL, NULL }
81 static ConfigOCs ldifocs[] = {
83 "NAME 'olcLdifConfig' "
84 "DESC 'LDIF backend configuration' "
85 "SUP olcDatabaseConfig "
86 "MUST ( olcDbDirectory ) )", Cft_Database,
92 ldif_cf( ConfigArgs *c )
94 if ( c->op == SLAP_CONFIG_EMIT ) {
95 value_add_one( &c->rvalue_vals, &ldif_oc->soc_cname );
102 dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
105 char *ptr, *sep, *end;
107 res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
108 res->bv_val = ch_malloc( res->bv_len + 1 );
109 ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
110 *ptr++ = LDAP_DIRSEP[0];
111 ptr = lutil_strcopy( ptr, rootdn->bv_val );
112 end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
113 while ( end > dn->bv_val ) {
114 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
115 *ptr++ = LDAP_DIRSEP[0];
116 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
122 while( ptr=strchr(ptr, IX_DNL) ) {
124 ptr = strchr(ptr, IX_DNR);
133 static char * slurp_file(int fd) {
134 int read_chars_total = 0;
142 entry_size = st.st_size;
143 entry = ch_malloc( entry_size+1 );
147 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
148 if(read_chars == -1) {
152 if(read_chars == 0) {
153 entry[read_chars_total] = '\0';
157 read_chars_total += read_chars;
158 entry_pos += read_chars;
164 static int spew_file(int fd, char * spew) {
167 int len = strlen(spew);
168 char * spewptr = spew;
170 while(written < len) {
171 writeres = write(fd, spewptr, len - written);
173 perror("could not spew write");
184 static int spew_entry(Entry * e, struct berval * path) {
189 char * entry_as_string;
191 openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
194 rs = LDAP_NO_SUCH_OBJECT;
196 rs = LDAP_UNWILLING_TO_PERFORM;
202 /* Only save the RDN onto disk */
203 dnRdn( &e->e_name, &rdn );
204 if ( rdn.bv_len != e->e_name.bv_len ) {
205 e->e_name.bv_val[rdn.bv_len] = '\0';
206 tmp = e->e_name.bv_len;
207 e->e_name.bv_len = rdn.bv_len;
211 entry_as_string = entry2str(e, &entry_length);
213 /* Restore full DN */
214 if ( rdn.bv_len != e->e_name.bv_len ) {
215 e->e_name.bv_val[e->e_name.bv_len] = ',';
216 e->e_name.bv_len = rdn.bv_len;
219 if(entry_as_string == NULL) {
220 rs = LDAP_UNWILLING_TO_PERFORM;
224 spew_res = spew_file(openres, entry_as_string);
227 rs = LDAP_UNWILLING_TO_PERFORM;
235 static Entry * get_entry_for_fd(int fd,
239 char * entry = (char *) slurp_file(fd);
240 Entry * ldentry = NULL;
242 /* error reading file */
247 ldentry = str2entry(entry);
250 rdn = ldentry->e_name;
251 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
252 ch_free( rdn.bv_val );
253 rdn = ldentry->e_nname;
254 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
255 ch_free( rdn.bv_val );
269 static Entry * get_entry(Operation *op, struct berval *base_path) {
270 struct berval path, pdn, pndn;
273 dnParent(&op->o_req_dn, &pdn);
274 dnParent(&op->o_req_ndn, &pndn);
275 dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
276 fd = open(path.bv_val, O_RDONLY);
277 /* error opening file (mebbe should log error) */
279 perror("failed to open file");
282 if(path.bv_val != NULL)
283 SLAP_FREE(path.bv_val);
286 return get_entry_for_fd(fd, &pdn, &pndn);
292 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
294 res->bv_len = name->bv_len + base->bv_len + 1;
295 res->bv_val = ch_malloc( res->bv_len + 1 );
296 strcpy(res->bv_val, base->bv_val);
297 ptr = res->bv_val + base->bv_len;
298 *ptr++ = LDAP_DIRSEP[0];
299 strcpy(ptr, name->bv_val);
302 typedef struct bvlist {
311 static int r_enum_tree(enumCookie *ck, struct berval *path,
312 struct berval *pdn, struct berval *pndn)
315 int fd, rc = LDAP_SUCCESS;
317 fd = open( path->bv_val, O_RDONLY );
319 Debug( LDAP_DEBUG_TRACE,
320 "=> ldif_enum_tree: failed to open %s\n",
321 path->bv_val, 0, 0 );
322 return LDAP_NO_SUCH_OBJECT;
325 e = get_entry_for_fd(fd, pdn, pndn);
327 Debug( LDAP_DEBUG_ANY,
328 "=> ldif_enum_tree: failed to read entry for %s\n",
329 path->bv_val, 0, 0 );
333 if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
334 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
335 /* Send right away? */
338 * if it's a referral, add it to the list of referrals. only do
339 * this for non-base searches, and don't check the filter
340 * explicitly here since it's only a candidate anyway.
342 if ( !get_manageDSAit( ck->op )
343 && ck->op->ors_scope != LDAP_SCOPE_BASE
344 && is_entry_referral( e ) )
346 BerVarray erefs = get_entry_referrals( ck->op, e );
347 ck->rs->sr_ref = referral_rewrite( erefs,
349 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
350 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
352 send_search_reference( ck->op, ck->rs );
354 ber_bvarray_free( ck->rs->sr_ref );
355 ber_bvarray_free( erefs );
356 ck->rs->sr_ref = NULL;
358 } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
360 ck->rs->sr_entry = e;
361 ck->rs->sr_attrs = ck->op->ors_attrs;
362 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
363 send_search_entry(ck->op, ck->rs);
367 /* Queueing up for tool mode */
368 if(ck->entries == NULL) {
369 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
370 ck->elen = ENTRY_BUFF_INCREMENT;
372 if(ck->eind >= ck->elen) { /* grow entries if necessary */
373 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
377 ck->entries[ck->eind++] = e;
384 if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
386 bvlist *list = NULL, *ptr;
388 path->bv_len -= STRLENOF( LDIF );
389 path->bv_val[path->bv_len] = '\0';
391 dir_of_path = opendir(path->bv_val);
392 if(dir_of_path == NULL) { /* can't open directory */
393 if ( errno != ENOENT ) {
394 /* it shouldn't be treated as an error
395 * only if the directory doesn't exist */
397 Debug( LDAP_DEBUG_TRACE,
398 "=> ldif_enum_tree: failed to opendir %s (%d)\n",
399 path->bv_val, errno, 0 );
405 struct berval fname, itmp;
409 dir = readdir(dir_of_path);
410 if(dir == NULL) break; /* end of the directory */
411 fname.bv_len = strlen( dir->d_name );
412 if ( fname.bv_len <= STRLENOF( LDIF ))
414 if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
416 fname.bv_val = dir->d_name;
418 bvl = ch_malloc( sizeof(bvlist) );
419 ber_dupbv( &bvl->bv, &fname );
420 BER_BVZERO( &bvl->num );
421 itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
425 ptr = strchr( itmp.bv_val, IX_FSR );
427 itmp.bv_len = ptr - itmp.bv_val;
428 ber_dupbv( &bvl->num, &itmp );
429 bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
430 itmp.bv_val[0] = '\0';
431 bvl->off = itmp.bv_val - bvl->bv.bv_val;
435 for (ptr = list, prev = (bvlist *)&list; ptr;
436 prev = ptr, ptr = ptr->next) {
437 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
438 if ( !cmp && bvl->num.bv_val )
439 cmp = bvl->inum - ptr->inum;
447 closedir(dir_of_path);
449 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
450 ck->op->ors_scope = LDAP_SCOPE_BASE;
451 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
452 ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
454 while ( ( ptr = list ) ) {
459 if ( ptr->num.bv_val )
460 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
462 fullpath( path, &ptr->bv, &fpath );
463 r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
465 if ( ptr->num.bv_val )
466 free( ptr->num.bv_val );
467 free(ptr->bv.bv_val);
472 if ( fd ) entry_free( e );
481 struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
483 struct berval pdn, pndn;
485 dnParent( &ck->op->o_req_dn, &pdn );
486 dnParent( &ck->op->o_req_ndn, &pndn );
487 dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
488 return r_enum_tree(ck, &path, &pdn, &pndn);
491 /* Get the parent path plus the LDIF suffix */
492 static void get_parent_path(struct berval * dnpath, struct berval *res) {
493 int dnpathlen = dnpath->bv_len;
496 for(i = dnpathlen;i>0;i--) /* find the first path seperator */
497 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
500 res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
501 strncpy(res->bv_val, dnpath->bv_val, i);
502 strcpy(res->bv_val+i, LDIF);
503 res->bv_val[i] = '\0';
506 static int apply_modify_to_entry(Entry * entry,
507 Modifications * modlist,
511 char textbuf[SLAP_TEXT_BUFLEN];
512 int rc = LDAP_UNWILLING_TO_PERFORM;
513 Modification *mods = NULL;
515 if (!acl_check_modlist(op, entry, modlist)) {
516 return LDAP_INSUFFICIENT_ACCESS;
519 for (; modlist != NULL; modlist = modlist->sml_next) {
520 mods = &modlist->sml_mod;
522 switch (mods->sm_op) {
524 rc = modify_add_values(entry, mods,
525 get_permissiveModify(op),
526 &rs->sr_text, textbuf,
530 case LDAP_MOD_DELETE:
531 rc = modify_delete_values(entry, mods,
532 get_permissiveModify(op),
533 &rs->sr_text, textbuf,
538 case LDAP_MOD_REPLACE:
539 rc = modify_replace_values(entry, mods,
540 get_permissiveModify(op),
541 &rs->sr_text, textbuf,
545 case LDAP_MOD_INCREMENT:
547 case SLAP_MOD_SOFTADD:
548 mods->sm_op = LDAP_MOD_ADD;
549 rc = modify_add_values(entry, mods,
550 get_permissiveModify(op),
551 &rs->sr_text, textbuf,
553 mods->sm_op = SLAP_MOD_SOFTADD;
554 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
561 if(rc != LDAP_SUCCESS) break;
564 if(rc == LDAP_SUCCESS) {
565 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
566 entry->e_ocflags = 0;
568 /* check that the entry still obeys the schema */
569 rc = entry_schema_check(op->o_bd, entry, NULL,
570 &rs->sr_text, textbuf, sizeof( textbuf ) );
576 ldif_back_referrals( Operation *op, SlapReply *rs )
578 struct ldif_info *ni = NULL;
580 int rc = LDAP_SUCCESS;
583 if ( op->o_tag == LDAP_REQ_SEARCH ) {
584 /* let search take care of itself */
589 if ( get_manageDSAit( op ) ) {
590 /* let op take care of DSA management */
594 ni = (struct ldif_info *)op->o_bd->be_private;
595 ldap_pvt_thread_mutex_lock( &ni->li_mutex );
596 entry = (Entry *)get_entry( op, &ni->li_base_path );
598 /* no object is found for them */
599 if ( entry == NULL ) {
600 struct berval odn = op->o_req_dn;
601 struct berval ondn = op->o_req_ndn;
603 struct berval pndn = op->o_req_ndn;
605 for ( ; entry == NULL; ) {
606 dnParent( &pndn, &pndn );
608 if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
613 op->o_req_ndn = pndn;
615 entry = (Entry *)get_entry( op, &ni->li_base_path );
618 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
621 op->o_req_ndn = ondn;
624 rs->sr_matched = NULL;
625 if ( entry != NULL ) {
626 Debug( LDAP_DEBUG_TRACE,
627 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
628 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
630 if ( is_entry_referral( entry ) ) {
632 rs->sr_ref = get_entry_referrals( op, entry );
634 rs->sr_matched = ber_strdup_x(
635 entry->e_name.bv_val, op->o_tmpmemctx );
641 } else if ( default_referral != NULL ) {
643 rs->sr_ref = referral_rewrite( default_referral,
644 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
647 if ( rs->sr_ref != NULL ) {
649 rc = rs->sr_err = LDAP_REFERRAL;
650 send_ldap_result( op, rs );
651 ber_bvarray_free( rs->sr_ref );
654 } else if ( rc != LDAP_SUCCESS ) {
656 rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
657 send_ldap_result( op, rs );
660 if ( rs->sr_matched ) {
661 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
662 rs->sr_matched = NULL;
668 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
670 if ( is_entry_referral( entry ) ) {
671 /* entry is a referral */
672 BerVarray refs = get_entry_referrals( op, entry );
673 rs->sr_ref = referral_rewrite(
674 refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
676 Debug( LDAP_DEBUG_TRACE,
677 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
678 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
680 rs->sr_matched = entry->e_name.bv_val;
681 if ( rs->sr_ref != NULL ) {
682 rc = rs->sr_err = LDAP_REFERRAL;
683 send_ldap_result( op, rs );
684 ber_bvarray_free( rs->sr_ref );
688 send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
692 rs->sr_matched = NULL;
693 ber_bvarray_free( refs );
702 ldif_back_bind( Operation *op, SlapReply *rs )
704 struct ldif_info *ni = NULL;
705 Attribute * a = NULL;
706 AttributeDescription *password = slap_schema.si_ad_userPassword;
708 Entry * entry = NULL;
710 ni = (struct ldif_info *) op->o_bd->be_private;
711 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
712 entry = (Entry *) get_entry(op, &ni->li_base_path);
714 /* no object is found for them */
716 if(be_isroot_pw(op)) {
717 rs->sr_err = return_val = LDAP_SUCCESS;
719 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
724 /* they don't have userpassword */
725 if((a = attr_find(entry->e_attrs, password)) == NULL) {
726 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
731 /* authentication actually failed */
732 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
733 &rs->sr_text) != 0) {
734 rs->sr_err = LDAP_INVALID_CREDENTIALS;
739 /* let the front-end send success */
744 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
746 send_ldap_result( op, rs );
752 static int ldif_back_search(Operation *op, SlapReply *rs)
754 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
759 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
760 rs->sr_err = enum_tree( &ck );
761 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
762 send_ldap_result(op, rs);
767 static int ldif_back_add(Operation *op, SlapReply *rs) {
768 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
769 Entry * e = op->ora_e;
770 struct berval dn = e->e_nname;
771 struct berval leaf_path = BER_BVNULL;
774 char textbuf[SLAP_TEXT_BUFLEN];
776 rs->sr_err = entry_schema_check(op->o_bd, e,
777 NULL, &rs->sr_text, textbuf, sizeof( textbuf ) );
778 if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
780 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
782 dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
784 if(leaf_path.bv_val != NULL) {
785 struct berval base = BER_BVNULL;
786 /* build path to container and ldif of container */
787 get_parent_path(&leaf_path, &base);
789 statres = stat(base.bv_val, &stats); /* check if container exists */
790 if(statres == -1 && errno == ENOENT) { /* container missing */
791 base.bv_val[base.bv_len] = '.';
792 statres = stat(base.bv_val, &stats); /* check for leaf node */
793 base.bv_val[base.bv_len] = '\0';
794 if(statres == -1 && errno == ENOENT) {
795 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
797 else if(statres != -1) { /* create parent */
798 int mkdirres = mkdir(base.bv_val, 0750);
800 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
804 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
805 }/* container was possibly created, move on to add the entry */
806 if(rs->sr_err == LDAP_SUCCESS) {
807 statres = stat(leaf_path.bv_val, &stats);
808 if(statres == -1 && errno == ENOENT) {
809 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
810 rs->sr_err = (int) spew_entry(e, &leaf_path);
811 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
813 else /* it already exists */
814 rs->sr_err = LDAP_ALREADY_EXISTS;
816 SLAP_FREE(base.bv_val);
817 SLAP_FREE(leaf_path.bv_val);
820 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
823 send_ldap_result(op, rs);
827 static int ldif_back_modify(Operation *op, SlapReply *rs) {
828 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
829 Modifications * modlst = op->orm_modlist;
830 struct berval path = BER_BVNULL;
831 Entry * entry = NULL;
834 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
835 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
837 entry = (Entry *) get_entry(op, &ni->li_base_path);
840 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
841 if(rs->sr_err == LDAP_SUCCESS) {
842 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
843 spew_res = spew_entry(entry, &path);
844 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
846 perror("could not output entry");
847 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
852 rs->sr_err = LDAP_NO_SUCH_OBJECT;
857 if(path.bv_val != NULL)
858 SLAP_FREE(path.bv_val);
860 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
861 send_ldap_result(op, rs);
865 static int ldif_back_delete(Operation *op, SlapReply *rs) {
866 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
867 struct berval path = BER_BVNULL;
870 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
871 dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
873 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
874 res = rmdir(path.bv_val);
875 path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
876 if ( res && errno != ENOENT ) {
877 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
879 res = unlink(path.bv_val);
884 rs->sr_err = LDAP_NO_SUCH_OBJECT;
886 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
889 rs->sr_err = LDAP_SUCCESS;
891 SLAP_FREE(path.bv_val);
892 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
893 send_ldap_result(op, rs);
898 static int move_entry(Entry * entry, struct berval * ndn,
899 struct berval * newndn, struct berval * rootdn,
900 struct berval * base_path) {
904 struct berval newpath;
906 dn2path(ndn, rootdn, base_path, &path);
907 dn2path(newndn, rootdn, base_path, &newpath);
909 if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
910 /* some object doesn't exist */
911 res = LDAP_NO_SUCH_OBJECT;
913 else { /* do the modrdn */
914 exists_res = open(newpath.bv_val, O_RDONLY);
915 if(exists_res == -1 && errno == ENOENT) {
916 res = spew_entry(entry, &newpath);
918 /* if this fails we should log something bad */
919 res = unlink(path.bv_val);
924 res = LDAP_NO_SUCH_OBJECT;
926 res = LDAP_UNWILLING_TO_PERFORM;
927 unlink(newpath.bv_val); /* in case file was created */
930 else if(exists_res) {
931 int close_res = close(exists_res);
932 res = LDAP_ALREADY_EXISTS;
933 if(close_res == -1) {
934 /* log heinous error */
938 res = LDAP_UNWILLING_TO_PERFORM;
942 if(newpath.bv_val != NULL)
943 SLAP_FREE(newpath.bv_val);
944 if(path.bv_val != NULL)
945 SLAP_FREE(path.bv_val);
949 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
950 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
951 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
952 struct berval p_dn, bv = BER_BVNULL;
953 Entry * entry = NULL;
954 LDAPRDN new_rdn = NULL;
955 LDAPRDN old_rdn = NULL;
956 Modifications * mods = NULL;
959 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
960 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
961 entry = (Entry *) get_entry(op, &ni->li_base_path);
963 /* build the mods to the entry */
965 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
966 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
967 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
969 else if(op->oq_modrdn.rs_deleteoldrdn &&
970 ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
971 LDAP_DN_FORMAT_LDAP)) {
972 rs->sr_err = LDAP_OTHER;
974 else { /* got both rdns successfully, ready to build mods */
975 if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
977 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
979 else { /* built mods successfully */
981 /* build new dn, and new ndn for the entry */
982 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
983 p_dn = *op->oq_modrdn.rs_newSup;
985 p_dn = slap_empty_bv;
986 dnParent(&entry->e_name, &p_dn);
987 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL);
988 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
989 ber_dupbv( &new_ndn, &bv );
990 entry->e_name = new_dn;
991 entry->e_nname = new_ndn;
993 /* perform the modifications */
994 res = apply_modify_to_entry(entry, mods, op, rs);
995 if(res == LDAP_SUCCESS) {
996 rs->sr_err = move_entry(entry, &op->o_req_ndn,
998 &op->o_bd->be_nsuffix[0],
1006 else /* entry was null */
1007 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1012 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1013 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1014 send_ldap_result(op, rs);
1018 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1019 struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1023 ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1025 e = (Entry *) get_entry(op, &ni->li_base_path);
1027 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1029 a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1030 rs->sr_err = LDAP_COMPARE_FALSE;
1032 if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1033 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1034 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1035 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1036 op->o_tmpmemctx ) == 0) {
1037 rs->sr_err = LDAP_COMPARE_TRUE;
1043 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1048 ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1049 send_ldap_result(op, rs);
1053 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1054 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1055 ni->li_tool_current = 0;
1059 static int ldif_tool_entry_close(BackendDB * be) {
1060 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1062 SLAP_FREE(ni->li_tool_cookie.entries);
1067 ldif_tool_entry_first(BackendDB *be)
1069 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1070 ID id = 1; /* first entry in the array of entries shifted by one */
1072 ni->li_tool_current = 1;
1073 if(ni->li_tool_cookie.entries == NULL) {
1077 op.o_req_dn = *be->be_suffix;
1078 op.o_req_ndn = *be->be_nsuffix;
1079 op.ors_scope = LDAP_SCOPE_SUBTREE;
1080 ni->li_tool_cookie.op = &op;
1081 (void)enum_tree( &ni->li_tool_cookie );
1082 ni->li_tool_cookie.op = NULL;
1087 static ID ldif_tool_entry_next(BackendDB *be)
1089 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1090 ni->li_tool_current += 1;
1091 if(ni->li_tool_current > ni->li_tool_cookie.eind)
1094 return ni->li_tool_current;
1097 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1098 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1101 if(id > ni->li_tool_cookie.eind || id < 1)
1104 e = ni->li_tool_cookie.entries[id - 1];
1105 ni->li_tool_cookie.entries[id - 1] = NULL;
1110 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1111 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1112 struct berval dn = e->e_nname;
1113 struct berval leaf_path = BER_BVNULL;
1116 int res = LDAP_SUCCESS;
1118 dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1120 if(leaf_path.bv_val != NULL) {
1121 struct berval base = BER_BVNULL;
1122 /* build path to container, and path to ldif of container */
1123 get_parent_path(&leaf_path, &base);
1125 statres = stat(base.bv_val, &stats); /* check if container exists */
1126 if(statres == -1 && errno == ENOENT) { /* container missing */
1127 base.bv_val[base.bv_len] = '.';
1128 statres = stat(base.bv_val, &stats); /* check for leaf node */
1129 base.bv_val[base.bv_len] = '\0';
1130 if(statres == -1 && errno == ENOENT) {
1131 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1133 else if(statres != -1) { /* create parent */
1134 int mkdirres = mkdir(base.bv_val, 0750);
1135 if(mkdirres == -1) {
1136 res = LDAP_UNWILLING_TO_PERFORM;
1140 res = LDAP_UNWILLING_TO_PERFORM;
1141 }/* container was possibly created, move on to add the entry */
1142 if(res == LDAP_SUCCESS) {
1143 statres = stat(leaf_path.bv_val, &stats);
1144 if(statres == -1 && errno == ENOENT) {
1145 res = (int) spew_entry(e, &leaf_path);
1147 else /* it already exists */
1148 res = LDAP_ALREADY_EXISTS;
1150 SLAP_FREE(base.bv_val);
1151 SLAP_FREE(leaf_path.bv_val);
1154 if(res == LDAP_SUCCESS) {
1162 ldif_back_db_init( BackendDB *be )
1164 struct ldif_info *ni;
1166 ni = ch_calloc( 1, sizeof(struct ldif_info) );
1167 be->be_private = ni;
1168 be->be_cf_table = be->bd_info->bi_cf_table;
1169 ldap_pvt_thread_mutex_init(&ni->li_mutex);
1174 ldif_back_db_destroy(
1178 struct ldif_info *ni = be->be_private;
1179 ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1180 free( be->be_private );
1189 struct ldif_info *ni = (struct ldif_info *) be->be_private;
1190 if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1191 fprintf(stderr, "missing base path for back-ldif\n");
1198 ldif_back_initialize(
1202 static char *controls[] = {
1203 LDAP_CONTROL_MANAGEDSAIT,
1209 SLAP_BFLAG_REFERRALS;
1211 bi->bi_controls = controls;
1213 bi->bi_cf_table = ldifcfg;
1220 bi->bi_db_init = ldif_back_db_init;
1221 bi->bi_db_config = config_generic_wrapper;
1222 bi->bi_db_open = ldif_back_db_open;
1223 bi->bi_db_close = 0;
1224 bi->bi_db_destroy = ldif_back_db_destroy;
1226 bi->bi_op_bind = ldif_back_bind;
1227 bi->bi_op_unbind = 0;
1228 bi->bi_op_search = ldif_back_search;
1229 bi->bi_op_compare = ldif_back_compare;
1230 bi->bi_op_modify = ldif_back_modify;
1231 bi->bi_op_modrdn = ldif_back_modrdn;
1232 bi->bi_op_add = ldif_back_add;
1233 bi->bi_op_delete = ldif_back_delete;
1234 bi->bi_op_abandon = 0;
1236 bi->bi_extended = 0;
1238 bi->bi_chk_referrals = ldif_back_referrals;
1240 bi->bi_connection_init = 0;
1241 bi->bi_connection_destroy = 0;
1243 bi->bi_tool_entry_open = ldif_tool_entry_open;
1244 bi->bi_tool_entry_close = ldif_tool_entry_close;
1245 bi->bi_tool_entry_first = ldif_tool_entry_first;
1246 bi->bi_tool_entry_next = ldif_tool_entry_next;
1247 bi->bi_tool_entry_get = ldif_tool_entry_get;
1248 bi->bi_tool_entry_put = ldif_tool_entry_put;
1249 bi->bi_tool_entry_reindex = 0;
1250 bi->bi_tool_sync = 0;
1252 bi->bi_tool_dn2id_get = 0;
1253 bi->bi_tool_id2entry_get = 0;
1254 bi->bi_tool_entry_modify = 0;
1256 rc = config_register_schema( ldifcfg, ldifocs );
1257 if ( rc ) return rc;
1258 ldifcfg[0].ad = slap_schema.si_ad_objectClass;