#include "lutil.h"
#include "config.h"
+typedef struct enumCookie {
+ Operation *op;
+ SlapReply *rs;
+ Entry **entries;
+ int elen;
+ int eind;
+} enumCookie;
+
struct ldif_info {
struct berval li_base_path;
- ID tool_current;
- Entry ** tool_entries;
- int tool_put_entry_flag;
- int tool_numentries;
+ enumCookie li_tool_cookie;
+ ID li_tool_current;
ldap_pvt_thread_mutex_t li_mutex;
};
+#ifdef _WIN32
+#define mkdir(a,b) mkdir(a)
+#endif
+
#define LDIF ".ldif"
#define IX_DNL '{'
#define ENTRY_BUFF_INCREMENT 500
-static ObjectClass *ldif_oc;
-
-static ConfigDriver ldif_cf;
-
static ConfigTable ldifcfg[] = {
- { "", "", 0, 0, 0, ARG_MAGIC,
- ldif_cf, NULL, NULL, NULL },
{ "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
(void *)offsetof(struct ldif_info, li_base_path),
- "( OLcfgAt:1.1 NAME 'dbDirectory' "
+ "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
"DESC 'Directory for database content' "
"EQUALITY caseIgnoreMatch "
"SYNTAX OMsDirectoryString )", NULL, NULL },
};
static ConfigOCs ldifocs[] = {
- { "( OLcfgOc:2.1 "
- "NAME 'ldifConfig' "
+ { "( OLcfgDbOc:2.1 "
+ "NAME 'olcLdifConfig' "
"DESC 'LDIF backend configuration' "
"SUP olcDatabaseConfig "
- "MUST ( dbDirectory ) )", Cft_Database,
- &ldif_oc },
+ "MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
{ NULL, 0, NULL }
};
-static int
-ldif_cf( ConfigArgs *c )
-{
- if ( c->op == SLAP_CONFIG_EMIT ) {
- value_add_one( &c->rvalue_vals, &ldif_oc->soc_cname );
- return 0;
- }
- return 1;
-}
-
static void
dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
struct berval *res)
return entry;
}
-static int spew_file(int fd, char * spew) {
- int written = 0;
- int writeres;
- int len = strlen(spew);
- char * spewptr = spew;
+static int spew_file(int fd, char * spew, int len) {
+ int writeres = 0;
- while(written < len) {
- writeres = write(fd, spewptr, len - written);
+ while(len > 0) {
+ writeres = write(fd, spew, len);
if(writeres == -1) {
perror("could not spew write");
return -1;
}
else {
- spewptr += writeres;
- written += writeres;
+ spew += writeres;
+ len -= writeres;
}
}
return writeres;
int entry_length;
char * entry_as_string;
- openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+ openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
if(openres == -1) {
if(errno == ENOENT)
rs = LDAP_NO_SUCH_OBJECT;
close(openres);
}
else {
- spew_res = spew_file(openres, entry_as_string);
+ spew_res = spew_file(openres, entry_as_string, entry_length);
close(openres);
if(spew_res == -1)
rs = LDAP_UNWILLING_TO_PERFORM;
int off;
} bvlist;
-typedef struct enumCookie {
- Entry **entries;
- int elen;
- int eind;
- int scope;
-} enumCookie;
static int r_enum_tree(enumCookie *ck, struct berval *path,
struct berval *pdn, struct berval *pndn)
return LDAP_NO_SUCH_OBJECT;
}
- if(ck->entries == NULL) {
- ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
- ck->elen = ENTRY_BUFF_INCREMENT;
- }
-
e = get_entry_for_fd(fd, pdn, pndn);
if ( !e ) {
Debug( LDAP_DEBUG_ANY,
return LDAP_BUSY;
}
- if ( ck->scope == LDAP_SCOPE_BASE || ck->scope == LDAP_SCOPE_SUBTREE ) {
- if(! (ck->eind < ck->elen)) { /* grow entries if necessary */
- ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
- ck->elen *= 2;
- }
+ if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
+ ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
+ /* Send right away? */
+ if ( ck->rs ) {
+ /*
+ * if it's a referral, add it to the list of referrals. only do
+ * this for non-base searches, and don't check the filter
+ * explicitly here since it's only a candidate anyway.
+ */
+ if ( !get_manageDSAit( ck->op )
+ && ck->op->ors_scope != LDAP_SCOPE_BASE
+ && is_entry_referral( e ) )
+ {
+ BerVarray erefs = get_entry_referrals( ck->op, e );
+ ck->rs->sr_ref = referral_rewrite( erefs,
+ &e->e_name, NULL,
+ ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
+ ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
+
+ ck->rs->sr_entry = e;
+ rc = send_search_reference( ck->op, ck->rs ) < 0;
+ ber_bvarray_free( ck->rs->sr_ref );
+ ber_bvarray_free( erefs );
+ ck->rs->sr_ref = NULL;
+ ck->rs->sr_entry = NULL;
+
+ } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
+ {
+ ck->rs->sr_entry = e;
+ ck->rs->sr_attrs = ck->op->ors_attrs;
+ ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
+ rc = send_search_entry(ck->op, ck->rs) < 0;
+ ck->rs->sr_entry = NULL;
+ }
+ fd = 1;
+ if ( rc )
+ goto leave;
+ } else {
+ /* Queueing up for tool mode */
+ if(ck->entries == NULL) {
+ ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
+ ck->elen = ENTRY_BUFF_INCREMENT;
+ }
+ if(ck->eind >= ck->elen) { /* grow entries if necessary */
+ ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
+ ck->elen *= 2;
+ }
- ck->entries[ck->eind] = e;
- ck->eind++;
- fd = 0;
+ ck->entries[ck->eind++] = e;
+ fd = 0;
+ }
} else {
fd = 1;
}
- if ( ck->scope != LDAP_SCOPE_BASE ) {
+ if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
DIR * dir_of_path;
bvlist *list = NULL, *ptr;
dir_of_path = opendir(path->bv_val);
if(dir_of_path == NULL) { /* can't open directory */
- Debug( LDAP_DEBUG_TRACE,
- "=> ldif_enum_tree: failed to opendir %s\n",
- path->bv_val, 0, 0 );
-#if 0
- /* so, what? */
- rc = LDAP_BUSY;
-#endif
+ if ( errno != ENOENT ) {
+ /* it shouldn't be treated as an error
+ * only if the directory doesn't exist */
+ rc = LDAP_BUSY;
+ Debug( LDAP_DEBUG_TRACE,
+ "=> ldif_enum_tree: failed to opendir %s (%d)\n",
+ path->bv_val, errno, 0 );
+ }
goto leave;
}
while(1) {
struct berval fname, itmp;
struct dirent * dir;
- bvlist *bvl, *prev;
+ bvlist *bvl, **prev;
dir = readdir(dir_of_path);
if(dir == NULL) break; /* end of the directory */
}
}
- for (ptr = list, prev = (bvlist *)&list; ptr;
- prev = ptr, ptr = ptr->next) {
+ for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
if ( !cmp && bvl->num.bv_val )
cmp = bvl->inum - ptr->inum;
if ( cmp < 0 )
break;
}
- prev->next = bvl;
+ *prev = bvl;
bvl->next = ptr;
}
closedir(dir_of_path);
- if (ck->scope == LDAP_SCOPE_ONELEVEL)
- ck->scope = LDAP_SCOPE_BASE;
- else if ( ck->scope == LDAP_SCOPE_SUBORDINATE)
- ck->scope = LDAP_SCOPE_SUBTREE;
+ if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
+ ck->op->ors_scope = LDAP_SCOPE_BASE;
+ else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
+ ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
while ( ( ptr = list ) ) {
struct berval fpath;
list = ptr->next;
- if ( ptr->num.bv_val )
- AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
- ptr->num.bv_len );
- fullpath( path, &ptr->bv, &fpath );
- r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
- free(fpath.bv_val);
+ if ( rc == LDAP_SUCCESS ) {
+ if ( ptr->num.bv_val )
+ AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
+ ptr->num.bv_len );
+ fullpath( path, &ptr->bv, &fpath );
+ rc = r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
+ free(fpath.bv_val);
+ }
if ( ptr->num.bv_val )
free( ptr->num.bv_val );
free(ptr->bv.bv_val);
static int
enum_tree(
- BackendDB *be,
- struct berval *dn,
- struct berval *ndn,
- int * length,
- Entry ***e,
- int scope )
+ enumCookie *ck
+)
{
- struct ldif_info *ni = (struct ldif_info *) be->be_private;
+ struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
struct berval path;
- int index = 0, rc;
- enumCookie ck = {0};
struct berval pdn, pndn;
+ int rc;
- assert( e != NULL );
- *e = NULL;
-
- ck.scope = scope;
- dnParent( dn, &pdn );
- dnParent( ndn, &pndn );
- dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
- rc = r_enum_tree(&ck, &path, &pdn, &pndn);
- *length = ck.eind;
- *e = ck.entries;
+ dnParent( &ck->op->o_req_dn, &pdn );
+ dnParent( &ck->op->o_req_ndn, &pndn );
+ dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
+ rc = r_enum_tree(ck, &path, &pdn, &pndn);
+ ch_free( path.bv_val );
return rc;
}
SlapReply * rs)
{
char textbuf[SLAP_TEXT_BUFLEN];
- size_t textlen = sizeof textbuf;
- int rc;
- int tempdebug;
+ int rc = LDAP_UNWILLING_TO_PERFORM;
Modification *mods = NULL;
- Attribute *save_attrs;
if (!acl_check_modlist(op, entry, modlist)) {
return LDAP_INSUFFICIENT_ACCESS;
}
- /* save_attrs = entry->e_attrs; Why?
- entry->e_attrs = attrs_dup(entry->e_attrs); */
-
for (; modlist != NULL; modlist = modlist->sml_next) {
mods = &modlist->sml_mod;
rc = modify_add_values(entry, mods,
get_permissiveModify(op),
&rs->sr_text, textbuf,
- textlen);
+ sizeof( textbuf ) );
break;
case LDAP_MOD_DELETE:
rc = modify_delete_values(entry, mods,
get_permissiveModify(op),
&rs->sr_text, textbuf,
- textlen);
-
+ sizeof( textbuf ) );
break;
case LDAP_MOD_REPLACE:
rc = modify_replace_values(entry, mods,
get_permissiveModify(op),
&rs->sr_text, textbuf,
- textlen);
-
+ sizeof( textbuf ) );
break;
+
case LDAP_MOD_INCREMENT:
+ rc = modify_increment_values( entry,
+ mods, get_permissiveModify(op),
+ &rs->sr_text, textbuf,
+ sizeof( textbuf ) );
+ break;
+
break;
+
case SLAP_MOD_SOFTADD:
mods->sm_op = LDAP_MOD_ADD;
rc = modify_add_values(entry, mods,
get_permissiveModify(op),
&rs->sr_text, textbuf,
- textlen);
+ sizeof( textbuf ) );
mods->sm_op = SLAP_MOD_SOFTADD;
if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
rc = LDAP_SUCCESS;
entry->e_ocflags = 0;
}
/* check that the entry still obeys the schema */
- rc = entry_schema_check(op->o_bd, entry,
- save_attrs, &rs->sr_text,
- textbuf, textlen);
+ rc = entry_schema_check(op->o_bd, entry, NULL, 0,
+ &rs->sr_text, textbuf, sizeof( textbuf ) );
}
return rc;
}
struct berval pndn = op->o_req_ndn;
for ( ; entry == NULL; ) {
- struct berval opndn = pndn;
- dnParent( &opndn, &pndn );
+ dnParent( &pndn, &pndn );
if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
break;
static int ldif_back_search(Operation *op, SlapReply *rs)
{
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
- int numentries = 0;
- int i = 0;
- Entry ** entries = NULL;
+ enumCookie ck = {0};
+ ck.op = op;
+ ck.rs = rs;
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
- rs->sr_err = enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, &entries, op->ors_scope);
- if ( rs->sr_err == LDAP_SUCCESS ) {
- for ( i = 0; i < numentries; i++ ) {
-
-
- /*
- * if it's a referral, add it to the list of referrals. only do
- * this for non-base searches, and don't check the filter
- * explicitly here since it's only a candidate anyway.
- */
- if ( !get_manageDSAit( op )
- && op->oq_search.rs_scope != LDAP_SCOPE_BASE
- && is_entry_referral( entries[i] ) )
- {
- BerVarray erefs = get_entry_referrals( op, entries[i] );
- rs->sr_ref = referral_rewrite( erefs,
- &entries[i]->e_name, NULL,
- op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
- ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
-
- send_search_reference( op, rs );
-
- ber_bvarray_free( rs->sr_ref );
- ber_bvarray_free( erefs );
- rs->sr_ref = NULL;
-
- } else if ( test_filter( op, entries[i], op->ors_filter ) == LDAP_COMPARE_TRUE )
- {
- rs->sr_entry = entries[i];
- rs->sr_attrs = op->ors_attrs;
- rs->sr_flags = REP_ENTRY_MODIFIABLE;
- send_search_entry(op, rs);
- }
- entry_free(entries[i]);
- }
- }
- SLAP_FREE(entries);
+ rs->sr_err = enum_tree( &ck );
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
send_ldap_result(op, rs);
struct stat stats;
int statres;
char textbuf[SLAP_TEXT_BUFLEN];
- size_t textlen = sizeof textbuf;
- rs->sr_err = entry_schema_check(op->o_bd, e,
- NULL, &rs->sr_text, textbuf, textlen);
+ rs->sr_err = entry_schema_check(op->o_bd, e, NULL, 0,
+ &rs->sr_text, textbuf, sizeof( textbuf ) );
if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
- struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
- struct berval * new_parent_dn = NULL;
- struct berval p_dn, bv = {0, NULL};
+ struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
+ struct berval p_dn, bv = BER_BVNULL;
Entry * entry = NULL;
LDAPRDN new_rdn = NULL;
LDAPRDN old_rdn = NULL;
static int ldif_tool_entry_open(BackendDB * be, int mode) {
struct ldif_info *ni = (struct ldif_info *) be->be_private;
- ni->tool_entries = NULL;
- ni->tool_numentries = 0;
- ni->tool_current = 0;
- ni->tool_put_entry_flag = 0;
+ ni->li_tool_current = 0;
return 0;
}
static int ldif_tool_entry_close(BackendDB * be) {
struct ldif_info *ni = (struct ldif_info *) be->be_private;
- SLAP_FREE(ni->tool_entries);
+ SLAP_FREE(ni->li_tool_cookie.entries);
return 0;
}
struct ldif_info *ni = (struct ldif_info *) be->be_private;
ID id = 1; /* first entry in the array of entries shifted by one */
- ni->tool_current = 1;
- if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
- (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
- &ni->tool_numentries, &ni->tool_entries,
- LDAP_SCOPE_SUBTREE);
- ni->tool_put_entry_flag = 0;
+ ni->li_tool_current = 1;
+ if(ni->li_tool_cookie.entries == NULL) {
+ Operation op = {0};
+
+ op.o_bd = be;
+ op.o_req_dn = *be->be_suffix;
+ op.o_req_ndn = *be->be_nsuffix;
+ op.ors_scope = LDAP_SCOPE_SUBTREE;
+ ni->li_tool_cookie.op = &op;
+ (void)enum_tree( &ni->li_tool_cookie );
+ ni->li_tool_cookie.op = NULL;
}
return id;
}
static ID ldif_tool_entry_next(BackendDB *be)
{
struct ldif_info *ni = (struct ldif_info *) be->be_private;
- ni->tool_current += 1;
- if(ni->tool_put_entry_flag) {
- (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
- &ni->tool_numentries, &ni->tool_entries,
- LDAP_SCOPE_SUBTREE);
- ni->tool_put_entry_flag = 0;
- }
- if(ni->tool_current > ni->tool_numentries)
+ ni->li_tool_current += 1;
+ if(ni->li_tool_current > ni->li_tool_cookie.eind)
return NOID;
else
- return ni->tool_current;
+ return ni->li_tool_current;
}
static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
struct ldif_info *ni = (struct ldif_info *) be->be_private;
Entry * e;
- if(id > ni->tool_numentries || id < 1)
+ if(id > ni->li_tool_cookie.eind || id < 1)
return NULL;
else {
- e = ni->tool_entries[id - 1];
- ni->tool_entries[id - 1] = NULL;
+ e = ni->li_tool_cookie.entries[id - 1];
+ ni->li_tool_cookie.entries[id - 1] = NULL;
return e;
}
}
static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
struct ldif_info *ni = (struct ldif_info *) be->be_private;
- Attribute *save_attrs;
struct berval dn = e->e_nname;
struct berval leaf_path = BER_BVNULL;
struct stat stats;
int statres;
- char textbuf[SLAP_TEXT_BUFLEN];
- size_t textlen = sizeof textbuf;
int res = LDAP_SUCCESS;
dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
}
if(res == LDAP_SUCCESS) {
- ni->tool_put_entry_flag = 1;
return 1;
}
else
ni = ch_calloc( 1, sizeof(struct ldif_info) );
be->be_private = ni;
- be->be_cf_table = be->bd_info->bi_cf_table;
+ be->be_cf_ocs = ldifocs;
ldap_pvt_thread_mutex_init(&ni->li_mutex);
return 0;
}
)
{
struct ldif_info *ni = be->be_private;
+
+ ch_free(ni->li_base_path.bv_val);
ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
free( be->be_private );
return 0;
int rc;
bi->bi_flags |=
+ SLAP_BFLAG_INCREMENT |
SLAP_BFLAG_REFERRALS;
bi->bi_controls = controls;
- bi->bi_cf_table = ldifcfg;
-
bi->bi_open = 0;
bi->bi_close = 0;
bi->bi_config = 0;
bi->bi_tool_id2entry_get = 0;
bi->bi_tool_entry_modify = 0;
+ bi->bi_cf_ocs = ldifocs;
+
rc = config_register_schema( ldifcfg, ldifocs );
if ( rc ) return rc;
- ldifcfg[0].ad = slap_schema.si_ad_objectClass;
return 0;
}