mirror of
https://git.openldap.org/openldap/openldap.git
synced 2024-12-21 03:10:25 +08:00
More cleanup. Still needs work.
This commit is contained in:
parent
6ea6a72d5d
commit
49bf49de06
@ -81,14 +81,15 @@ ldif_cf( ConfigArgs *c )
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static void
|
||||||
dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path)
|
dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
|
||||||
|
struct berval *res)
|
||||||
{
|
{
|
||||||
char *result = ch_malloc( dn->bv_len + base_path->bv_len + 2 +
|
|
||||||
STRLENOF( LDIF ));
|
|
||||||
char *ptr, *sep, *end;
|
char *ptr, *sep, *end;
|
||||||
|
|
||||||
ptr = lutil_strcopy( result, base_path->bv_val );
|
res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
|
||||||
|
res->bv_val = ch_malloc( res->bv_len + 1 );
|
||||||
|
ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
|
||||||
*ptr++ = LDAP_DIRSEP[0];
|
*ptr++ = LDAP_DIRSEP[0];
|
||||||
ptr = lutil_strcopy( ptr, rootdn->bv_val );
|
ptr = lutil_strcopy( ptr, rootdn->bv_val );
|
||||||
end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
|
end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
|
||||||
@ -99,7 +100,6 @@ dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path)
|
|||||||
end = sep;
|
end = sep;
|
||||||
}
|
}
|
||||||
strcpy(ptr, LDIF);
|
strcpy(ptr, LDIF);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char * slurp_file(int fd) {
|
static char * slurp_file(int fd) {
|
||||||
@ -158,14 +158,14 @@ static int spew_file(int fd, char * spew) {
|
|||||||
return writeres;
|
return writeres;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spew_entry(Entry * e, char * path) {
|
static int spew_entry(Entry * e, struct berval * path) {
|
||||||
int rs;
|
int rs;
|
||||||
int openres;
|
int openres;
|
||||||
int spew_res;
|
int spew_res;
|
||||||
int entry_length;
|
int entry_length;
|
||||||
char * entry_as_string;
|
char * entry_as_string;
|
||||||
|
|
||||||
openres = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
||||||
if(openres == -1) {
|
if(openres == -1) {
|
||||||
if(errno == ENOENT)
|
if(errno == ENOENT)
|
||||||
rs = LDAP_NO_SUCH_OBJECT;
|
rs = LDAP_NO_SUCH_OBJECT;
|
||||||
@ -213,44 +213,38 @@ static Entry * get_entry_for_fd(int fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Entry * get_entry(struct berval * dn, struct berval * rootdn, struct berval * base_path) {
|
static Entry * get_entry(struct berval * dn, struct berval * rootdn, struct berval * base_path) {
|
||||||
char * path = (char *) dn2path(dn, rootdn, base_path);
|
struct berval path;
|
||||||
int fd = open(path, O_RDONLY);
|
int fd;
|
||||||
|
|
||||||
|
dn2path(dn, rootdn, base_path, &path);
|
||||||
|
fd = open(path.bv_val, O_RDONLY);
|
||||||
/* error opening file (mebbe should log error) */
|
/* error opening file (mebbe should log error) */
|
||||||
if(fd == -1) {
|
if(fd == -1) {
|
||||||
perror("failed to open file");
|
perror("failed to open file");
|
||||||
goto return_value;
|
|
||||||
}
|
}
|
||||||
goto return_value;
|
|
||||||
|
|
||||||
return_value:
|
if(path.bv_val != NULL)
|
||||||
if(path != NULL)
|
SLAP_FREE(path.bv_val);
|
||||||
SLAP_FREE(path);
|
|
||||||
return get_entry_for_fd(fd);
|
return get_entry_for_fd(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* takes a base path and a filename and opens that file */
|
static void fullpath(struct berval *base, char *name, struct berval *res) {
|
||||||
static int fd_for_path_components(char * base, char * name) {
|
char *ptr;
|
||||||
char * absolutepath;
|
res->bv_len = strlen(name) + base->bv_len + 1;
|
||||||
int fd;
|
res->bv_val = ch_malloc( res->bv_len + 1 );
|
||||||
absolutepath = (char *) SLAP_MALLOC(sizeof(char) *
|
strcpy(res->bv_val, base->bv_val);
|
||||||
(strlen(base) +
|
ptr = res->bv_val + base->bv_len;
|
||||||
strlen(name) + 2));
|
*ptr++ = LDAP_DIRSEP[0];
|
||||||
absolutepath[0] = '\0';
|
strcpy(ptr, name);
|
||||||
strcat(absolutepath, base);
|
|
||||||
strcat(absolutepath, LDAP_DIRSEP);
|
|
||||||
strcat(absolutepath, name);
|
|
||||||
fd = open(absolutepath, O_RDONLY);
|
|
||||||
SLAP_FREE(absolutepath);
|
|
||||||
return fd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Entry ** r_enum_tree(Entry ** entries, int *elen, int *eind, char * path) {
|
static Entry ** r_enum_tree(Entry ** entries, int *elen, int *eind,
|
||||||
DIR * dir_of_path = opendir(path);
|
struct berval * path, int scope) {
|
||||||
|
DIR * dir_of_path = opendir(path->bv_val);
|
||||||
int fd;
|
int fd;
|
||||||
struct dirent * dir;
|
struct dirent * dir;
|
||||||
char * newpath;
|
|
||||||
Entry * e;
|
Entry * e;
|
||||||
|
struct berval fpath;
|
||||||
|
|
||||||
if(entries == NULL) {
|
if(entries == NULL) {
|
||||||
entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
|
entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
|
||||||
@ -261,15 +255,37 @@ static Entry ** r_enum_tree(Entry ** entries, int *elen, int *eind, char * path)
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BER_BVZERO(&fpath);
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
|
if (fpath.bv_val) {
|
||||||
|
free(fpath.bv_val);
|
||||||
|
fpath.bv_val = NULL;
|
||||||
|
}
|
||||||
dir = readdir(dir_of_path);
|
dir = readdir(dir_of_path);
|
||||||
if(dir == NULL) break; /* end of the directory */
|
if(dir == NULL) break; /* end of the directory */
|
||||||
|
if(dir->d_name[0] == '.') continue; /* skip '.' and '..' */
|
||||||
|
if(dir->d_type == DT_UNKNOWN) { /* must stat to determine... */
|
||||||
|
struct stat stbuf;
|
||||||
|
fullpath( path, dir->d_name, &fpath );
|
||||||
|
if (stat(fpath.bv_val, &stbuf)) {
|
||||||
|
perror("stat");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ( S_ISREG(stbuf.st_mode) ) dir->d_type = DT_REG;
|
||||||
|
else if ( S_ISDIR(stbuf.st_mode) ) dir->d_type = DT_DIR;
|
||||||
|
}
|
||||||
if(dir->d_type == DT_REG) { /* regular file, read the entry into memory */
|
if(dir->d_type == DT_REG) { /* regular file, read the entry into memory */
|
||||||
|
if ( scope == LDAP_SCOPE_ONELEVEL ) continue;
|
||||||
if(! (*eind < *elen)) { /* grow entries if necessary */
|
if(! (*eind < *elen)) { /* grow entries if necessary */
|
||||||
entries = (Entry **) SLAP_REALLOC(entries, sizeof(Entry *) * (*elen) * 2);
|
entries = (Entry **) SLAP_REALLOC(entries, sizeof(Entry *) * (*elen) * 2);
|
||||||
*elen = *elen * 2;
|
*elen = *elen * 2;
|
||||||
}
|
}
|
||||||
fd = fd_for_path_components(path, dir->d_name);
|
if ( !fpath.bv_val )
|
||||||
|
fullpath( path, dir->d_name, &fpath );
|
||||||
|
fd = open( fpath.bv_val, O_RDONLY );
|
||||||
|
SLAP_FREE(fpath.bv_val);
|
||||||
|
fpath.bv_val = NULL;
|
||||||
if(fd != -1) {
|
if(fd != -1) {
|
||||||
e = get_entry_for_fd(fd);
|
e = get_entry_for_fd(fd);
|
||||||
if(e != NULL) {
|
if(e != NULL) {
|
||||||
@ -283,39 +299,35 @@ static Entry ** r_enum_tree(Entry ** entries, int *elen, int *eind, char * path)
|
|||||||
perror("failed to open fd");
|
perror("failed to open fd");
|
||||||
}
|
}
|
||||||
else if(dir->d_type == DT_DIR) {
|
else if(dir->d_type == DT_DIR) {
|
||||||
if(! (strcasecmp(dir->d_name, ".") == 0 || strcasecmp(dir->d_name, "..") == 0)) {
|
if ( scope == LDAP_SCOPE_BASE ) continue;
|
||||||
newpath = (char *) SLAP_MALLOC(sizeof(char) *
|
if ( !fpath.bv_val )
|
||||||
(strlen(path) + strlen(dir->d_name) + 2));
|
fullpath( path, dir->d_name, &fpath );
|
||||||
newpath[0] = '\0';
|
entries = r_enum_tree(entries, elen, eind, &fpath,
|
||||||
strcat(newpath, path);
|
scope == LDAP_SCOPE_ONELEVEL ? LDAP_SCOPE_BASE : scope);
|
||||||
strcat(newpath, LDAP_DIRSEP);
|
|
||||||
strcat(newpath, dir->d_name);
|
|
||||||
entries = r_enum_tree(entries, elen, eind, newpath);
|
|
||||||
SLAP_FREE(newpath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dir_of_path);
|
closedir(dir_of_path);
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Entry ** enum_tree(struct berval * path, int * length) {
|
static Entry ** enum_tree(struct berval * path, int * length, int scope) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
return r_enum_tree(NULL, &index, length, path->bv_val);
|
return r_enum_tree(NULL, &index, length, path, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char * get_parent_path(char * dnpath) {
|
/* Get the parent path plus the LDIF suffix */
|
||||||
int dnpathlen = strlen(dnpath);
|
static void get_parent_path(struct berval * dnpath, struct berval *res) {
|
||||||
char * result;
|
int dnpathlen = dnpath->bv_len;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = dnpathlen;i>0;i--) /* find the first path seperator */
|
for(i = dnpathlen;i>0;i--) /* find the first path seperator */
|
||||||
if(dnpath[i] == LDAP_DIRSEP[0])
|
if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
|
||||||
break;
|
break;
|
||||||
result = ch_malloc( i + 1 );
|
res->bv_len = i;
|
||||||
strncpy(result, dnpath, i);
|
res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
|
||||||
result[i] = '\0';
|
strncpy(res->bv_val, dnpath->bv_val, i);
|
||||||
return result;
|
strcpy(res->bv_val+i, LDIF);
|
||||||
|
res->bv_val[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
static int apply_modify_to_entry(Entry * entry,
|
static int apply_modify_to_entry(Entry * entry,
|
||||||
@ -461,7 +473,7 @@ static int ldif_back_search(Operation *op, SlapReply *rs)
|
|||||||
Entry ** entries = NULL;
|
Entry ** entries = NULL;
|
||||||
|
|
||||||
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
||||||
entries = (Entry **) enum_tree(&ni->li_base_path, &numentries);
|
entries = (Entry **) enum_tree(&ni->li_base_path, &numentries, op->ors_scope);
|
||||||
|
|
||||||
if(entries != NULL) {
|
if(entries != NULL) {
|
||||||
for(i=0;i<numentries;i++) {
|
for(i=0;i<numentries;i++) {
|
||||||
@ -490,45 +502,36 @@ static int ldif_back_search(Operation *op, SlapReply *rs)
|
|||||||
static int ldif_back_add(Operation *op, SlapReply *rs) {
|
static int ldif_back_add(Operation *op, SlapReply *rs) {
|
||||||
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
|
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
|
||||||
Entry * e = op->ora_e;
|
Entry * e = op->ora_e;
|
||||||
Attribute *save_attrs;
|
|
||||||
struct berval dn = e->e_nname;
|
struct berval dn = e->e_nname;
|
||||||
char * leaf_path = NULL;
|
struct berval leaf_path = BER_BVNULL;
|
||||||
char * base = NULL;
|
|
||||||
char * base_ldif = NULL;
|
|
||||||
struct stat stats;
|
struct stat stats;
|
||||||
int statres;
|
int statres;
|
||||||
char textbuf[SLAP_TEXT_BUFLEN];
|
char textbuf[SLAP_TEXT_BUFLEN];
|
||||||
size_t textlen = sizeof textbuf;
|
size_t textlen = sizeof textbuf;
|
||||||
|
|
||||||
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
|
||||||
ldap_pvt_thread_mutex_lock(&entry2str_mutex);
|
|
||||||
|
|
||||||
leaf_path = (char *) dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path);
|
|
||||||
|
|
||||||
/* save_attrs = e->e_attrs; why?
|
|
||||||
e->e_attrs = attrs_dup(e->e_attrs);*/
|
|
||||||
|
|
||||||
if(leaf_path != NULL) {
|
|
||||||
char * tmp;
|
|
||||||
/* build path to container, and path to ldif of container */
|
|
||||||
base = (char *) get_parent_path(leaf_path);
|
|
||||||
base_ldif = (char *) SLAP_MALLOC(sizeof(char) * (strlen(base) + 6));
|
|
||||||
tmp = (char *) lutil_strcopy(base_ldif, base);
|
|
||||||
lutil_strcopy(tmp, LDIF);
|
|
||||||
|
|
||||||
rs->sr_err = entry_schema_check(op->o_bd, e,
|
rs->sr_err = entry_schema_check(op->o_bd, e,
|
||||||
save_attrs,
|
NULL, &rs->sr_text, textbuf, textlen);
|
||||||
&rs->sr_text,
|
if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
|
||||||
textbuf, textlen);
|
|
||||||
if(rs->sr_err == LDAP_SUCCESS) {
|
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
||||||
statres = stat(base, &stats); /* check if container exists */
|
|
||||||
|
dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
|
||||||
|
|
||||||
|
if(leaf_path.bv_val != NULL) {
|
||||||
|
struct berval base = BER_BVNULL;
|
||||||
|
/* build path to container and ldif of container */
|
||||||
|
get_parent_path(&leaf_path, &base);
|
||||||
|
|
||||||
|
statres = stat(base.bv_val, &stats); /* check if container exists */
|
||||||
if(statres == -1 && errno == ENOENT) { /* container missing */
|
if(statres == -1 && errno == ENOENT) { /* container missing */
|
||||||
statres = stat(base_ldif, &stats); /* check for leaf node */
|
base.bv_val[base.bv_len] = '.';
|
||||||
|
statres = stat(base.bv_val, &stats); /* check for leaf node */
|
||||||
|
base.bv_val[base.bv_len] = '\0';
|
||||||
if(statres == -1 && errno == ENOENT) {
|
if(statres == -1 && errno == ENOENT) {
|
||||||
rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
|
rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
|
||||||
}
|
}
|
||||||
else if(statres != -1) { /* create parent */
|
else if(statres != -1) { /* create parent */
|
||||||
int mkdirres = mkdir(base, 0750);
|
int mkdirres = mkdir(base.bv_val, 0750);
|
||||||
if(mkdirres == -1) {
|
if(mkdirres == -1) {
|
||||||
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
||||||
}
|
}
|
||||||
@ -537,45 +540,45 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
|
|||||||
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
||||||
}/* container was possibly created, move on to add the entry */
|
}/* container was possibly created, move on to add the entry */
|
||||||
if(rs->sr_err == LDAP_SUCCESS) {
|
if(rs->sr_err == LDAP_SUCCESS) {
|
||||||
statres = stat(leaf_path, &stats);
|
statres = stat(leaf_path.bv_val, &stats);
|
||||||
if(statres == -1 && errno == ENOENT) {
|
if(statres == -1 && errno == ENOENT) {
|
||||||
rs->sr_err = (int) spew_entry(e, leaf_path);
|
ldap_pvt_thread_mutex_lock(&entry2str_mutex);
|
||||||
|
rs->sr_err = (int) spew_entry(e, &leaf_path);
|
||||||
|
ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
|
||||||
}
|
}
|
||||||
else /* it already exists */
|
else /* it already exists */
|
||||||
rs->sr_err = LDAP_ALREADY_EXISTS;
|
rs->sr_err = LDAP_ALREADY_EXISTS;
|
||||||
}
|
}
|
||||||
}
|
SLAP_FREE(base.bv_val);
|
||||||
|
SLAP_FREE(leaf_path.bv_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
|
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
|
||||||
ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
|
|
||||||
|
|
||||||
|
send_res:
|
||||||
send_ldap_result(op, rs);
|
send_ldap_result(op, rs);
|
||||||
if(leaf_path != NULL)
|
|
||||||
SLAP_FREE(leaf_path);
|
|
||||||
if(base != NULL)
|
|
||||||
SLAP_FREE(base);
|
|
||||||
if(base_ldif != NULL)
|
|
||||||
SLAP_FREE(base_ldif);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ldif_back_modify(Operation *op, SlapReply *rs) {
|
static int ldif_back_modify(Operation *op, SlapReply *rs) {
|
||||||
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
|
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
|
||||||
Modifications * modlst = op->orm_modlist;
|
Modifications * modlst = op->orm_modlist;
|
||||||
char * path = NULL;
|
struct berval path = BER_BVNULL;
|
||||||
Entry * entry = NULL;
|
Entry * entry = NULL;
|
||||||
int spew_res;
|
int spew_res;
|
||||||
|
|
||||||
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
||||||
ldap_pvt_thread_mutex_lock(&entry2str_mutex);
|
dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
|
||||||
path = (char *) dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path);
|
&path);
|
||||||
entry = (Entry *) get_entry(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path);
|
entry = (Entry *) get_entry(&op->o_req_ndn, &op->o_bd->be_nsuffix[0],
|
||||||
|
&ni->li_base_path);
|
||||||
|
|
||||||
if(entry != NULL) {
|
if(entry != NULL) {
|
||||||
rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
|
rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
|
||||||
if(rs->sr_err == LDAP_SUCCESS) {
|
if(rs->sr_err == LDAP_SUCCESS) {
|
||||||
spew_res = spew_entry(entry, path);
|
ldap_pvt_thread_mutex_lock(&entry2str_mutex);
|
||||||
|
spew_res = spew_entry(entry, &path);
|
||||||
|
ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
|
||||||
if(spew_res == -1) {
|
if(spew_res == -1) {
|
||||||
perror("could not output entry");
|
perror("could not output entry");
|
||||||
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
||||||
@ -586,25 +589,24 @@ static int ldif_back_modify(Operation *op, SlapReply *rs) {
|
|||||||
rs->sr_err = LDAP_NO_SUCH_OBJECT;
|
rs->sr_err = LDAP_NO_SUCH_OBJECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(path != NULL)
|
|
||||||
SLAP_FREE(path);
|
|
||||||
if(entry != NULL)
|
if(entry != NULL)
|
||||||
entry_free(entry);
|
entry_free(entry);
|
||||||
|
if(path.bv_val != NULL)
|
||||||
|
SLAP_FREE(path.bv_val);
|
||||||
rs->sr_text = NULL;
|
rs->sr_text = NULL;
|
||||||
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
|
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
|
||||||
ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
|
|
||||||
send_ldap_result(op, rs);
|
send_ldap_result(op, rs);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ldif_back_delete(Operation *op, SlapReply *rs) {
|
static int ldif_back_delete(Operation *op, SlapReply *rs) {
|
||||||
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
|
struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
|
||||||
char * path = NULL;
|
struct berval path = BER_BVNULL;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
ldap_pvt_thread_mutex_lock(&ni->li_mutex);
|
||||||
path = (char *) dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path);
|
dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
|
||||||
res = unlink(path);
|
res = unlink(path.bv_val);
|
||||||
|
|
||||||
if(res == -1) {
|
if(res == -1) {
|
||||||
if(errno == ENOENT)
|
if(errno == ENOENT)
|
||||||
@ -615,21 +617,20 @@ static int ldif_back_delete(Operation *op, SlapReply *rs) {
|
|||||||
else
|
else
|
||||||
rs->sr_err = LDAP_SUCCESS;
|
rs->sr_err = LDAP_SUCCESS;
|
||||||
|
|
||||||
SLAP_FREE(path);
|
SLAP_FREE(path.bv_val);
|
||||||
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
|
ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
|
||||||
send_ldap_result(op, rs);
|
send_ldap_result(op, rs);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int is_leaf_node(char * path) {
|
static int is_leaf_node(struct berval * path) {
|
||||||
DIR * nonleafnode;
|
DIR * nonleafnode;
|
||||||
int path_len = strlen(path);
|
int path_len = path->bv_len;
|
||||||
char * nonleafpath = (char *) SLAP_MALLOC(sizeof(char) * path_len + 1);
|
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
strncpy(nonleafpath, path, path_len);
|
path->bv_val[path->bv_len - STRLENOF(LDIF)] = '\0';
|
||||||
nonleafpath[path_len - 5] = '\0';
|
nonleafnode = opendir(path->bv_val);
|
||||||
nonleafnode = opendir(nonleafpath);
|
path->bv_val[path->bv_len - STRLENOF(LDIF)] = '.';
|
||||||
if(nonleafnode == NULL) {
|
if(nonleafnode == NULL) {
|
||||||
res = 1;
|
res = 1;
|
||||||
}
|
}
|
||||||
@ -637,7 +638,6 @@ static int is_leaf_node(char * path) {
|
|||||||
closedir(nonleafnode);
|
closedir(nonleafnode);
|
||||||
res = 0;
|
res = 0;
|
||||||
}
|
}
|
||||||
SLAP_FREE(nonleafpath);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -646,23 +646,26 @@ static int move_entry(Entry * entry, struct berval * ndn,
|
|||||||
struct berval * base_path) {
|
struct berval * base_path) {
|
||||||
int res;
|
int res;
|
||||||
int exists_res;
|
int exists_res;
|
||||||
char * path = (char *) dn2path(ndn, rootdn, base_path);
|
struct berval path;
|
||||||
char * newpath = (char *) dn2path(newndn, rootdn, base_path);
|
struct berval newpath;
|
||||||
int path_len = strlen(path);
|
|
||||||
|
|
||||||
if((entry == NULL || path == NULL) || newpath == NULL) { /* some object doesn't exist */
|
dn2path(ndn, rootdn, base_path, &path);
|
||||||
|
dn2path(newndn, rootdn, base_path, &newpath);
|
||||||
|
|
||||||
|
if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
|
||||||
|
/* some object doesn't exist */
|
||||||
res = LDAP_NO_SUCH_OBJECT;
|
res = LDAP_NO_SUCH_OBJECT;
|
||||||
}
|
}
|
||||||
else if(! is_leaf_node(path)) { /* entry is not a leaf node */
|
else if(! is_leaf_node(&path)) { /* entry is not a leaf node */
|
||||||
res = LDAP_NOT_ALLOWED_ON_NONLEAF;
|
res = LDAP_NOT_ALLOWED_ON_NONLEAF;
|
||||||
}
|
}
|
||||||
else { /* do the modrdn */
|
else { /* do the modrdn */
|
||||||
exists_res = open(newpath, O_RDONLY);
|
exists_res = open(newpath.bv_val, O_RDONLY);
|
||||||
if(exists_res == -1 && errno == ENOENT) {
|
if(exists_res == -1 && errno == ENOENT) {
|
||||||
res = spew_entry(entry, newpath);
|
res = spew_entry(entry, &newpath);
|
||||||
if(res != -1) {
|
if(res != -1) {
|
||||||
/* if this fails we should log something bad */
|
/* if this fails we should log something bad */
|
||||||
res = unlink(path);
|
res = unlink(path.bv_val);
|
||||||
res = LDAP_SUCCESS;
|
res = LDAP_SUCCESS;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -670,13 +673,12 @@ static int move_entry(Entry * entry, struct berval * ndn,
|
|||||||
res = LDAP_NO_SUCH_OBJECT;
|
res = LDAP_NO_SUCH_OBJECT;
|
||||||
else
|
else
|
||||||
res = LDAP_UNWILLING_TO_PERFORM;
|
res = LDAP_UNWILLING_TO_PERFORM;
|
||||||
unlink(newpath); /* in case file was created */
|
unlink(newpath.bv_val); /* in case file was created */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(exists_res) {
|
else if(exists_res) {
|
||||||
int close_res;
|
int close_res = close(exists_res);
|
||||||
res = LDAP_ALREADY_EXISTS;
|
res = LDAP_ALREADY_EXISTS;
|
||||||
close_res = close(exists_res);
|
|
||||||
if(close_res == -1) {
|
if(close_res == -1) {
|
||||||
/* log heinous error */
|
/* log heinous error */
|
||||||
}
|
}
|
||||||
@ -686,10 +688,10 @@ static int move_entry(Entry * entry, struct berval * ndn,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(path != NULL)
|
if(newpath.bv_val != NULL)
|
||||||
SLAP_FREE(path);
|
SLAP_FREE(newpath.bv_val);
|
||||||
if(newpath != NULL)
|
if(path.bv_val != NULL)
|
||||||
SLAP_FREE(newpath);
|
SLAP_FREE(path.bv_val);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -824,7 +826,7 @@ static ID ldif_tool_entry_first(BackendDB *be) {
|
|||||||
ID id = 1; /* first entry in the array of entries shifted by one */
|
ID id = 1; /* first entry in the array of entries shifted by one */
|
||||||
ni->tool_current = 1;
|
ni->tool_current = 1;
|
||||||
if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
|
if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
|
||||||
ni->tool_entries = (Entry **) enum_tree(&ni->li_base_path, &ni->tool_numentries);
|
ni->tool_entries = (Entry **) enum_tree(&ni->li_base_path, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
|
||||||
ni->tool_put_entry_flag = 0;
|
ni->tool_put_entry_flag = 0;
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
@ -834,7 +836,7 @@ static ID ldif_tool_entry_next(BackendDB *be) {
|
|||||||
struct ldif_info *ni = (struct ldif_info *) be->be_private;
|
struct ldif_info *ni = (struct ldif_info *) be->be_private;
|
||||||
ni->tool_current += 1;
|
ni->tool_current += 1;
|
||||||
if(ni->tool_put_entry_flag) {
|
if(ni->tool_put_entry_flag) {
|
||||||
ni->tool_entries = (Entry **) enum_tree(&ni->li_base_path, &ni->tool_numentries);
|
ni->tool_entries = (Entry **) enum_tree(&ni->li_base_path, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
|
||||||
ni->tool_put_entry_flag = 0;
|
ni->tool_put_entry_flag = 0;
|
||||||
}
|
}
|
||||||
if(ni->tool_current > ni->tool_numentries)
|
if(ni->tool_current > ni->tool_numentries)
|
||||||
@ -860,36 +862,30 @@ static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
|
|||||||
struct ldif_info *ni = (struct ldif_info *) be->be_private;
|
struct ldif_info *ni = (struct ldif_info *) be->be_private;
|
||||||
Attribute *save_attrs;
|
Attribute *save_attrs;
|
||||||
struct berval dn = e->e_nname;
|
struct berval dn = e->e_nname;
|
||||||
char * leaf_path = NULL;
|
struct berval leaf_path = BER_BVNULL;
|
||||||
char * base = NULL;
|
|
||||||
char * base_ldif = NULL;
|
|
||||||
struct stat stats;
|
struct stat stats;
|
||||||
int statres;
|
int statres;
|
||||||
char textbuf[SLAP_TEXT_BUFLEN];
|
char textbuf[SLAP_TEXT_BUFLEN];
|
||||||
size_t textlen = sizeof textbuf;
|
size_t textlen = sizeof textbuf;
|
||||||
int res = LDAP_SUCCESS;
|
int res = LDAP_SUCCESS;
|
||||||
|
|
||||||
leaf_path = (char *) dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path);
|
dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
|
||||||
|
|
||||||
/* save_attrs = e->e_attrs; why?
|
if(leaf_path.bv_val != NULL) {
|
||||||
e->e_attrs = attrs_dup(e->e_attrs);*/
|
struct berval base = BER_BVNULL;
|
||||||
|
|
||||||
if(leaf_path != NULL) {
|
|
||||||
char * tmp;
|
|
||||||
/* build path to container, and path to ldif of container */
|
/* build path to container, and path to ldif of container */
|
||||||
base = (char *) get_parent_path(leaf_path);
|
get_parent_path(&leaf_path, &base);
|
||||||
base_ldif = (char *) SLAP_MALLOC(sizeof(char) * (strlen(base) + 6));
|
|
||||||
tmp = (char *) lutil_strcopy(base_ldif, base);
|
|
||||||
lutil_strcopy(tmp, LDIF);
|
|
||||||
|
|
||||||
statres = stat(base, &stats); /* check if container exists */
|
statres = stat(base.bv_val, &stats); /* check if container exists */
|
||||||
if(statres == -1 && errno == ENOENT) { /* container missing */
|
if(statres == -1 && errno == ENOENT) { /* container missing */
|
||||||
statres = stat(base_ldif, &stats); /* check for leaf node */
|
base.bv_val[base.bv_len] = '.';
|
||||||
|
statres = stat(base.bv_val, &stats); /* check for leaf node */
|
||||||
|
base.bv_val[base.bv_len] = '\0';
|
||||||
if(statres == -1 && errno == ENOENT) {
|
if(statres == -1 && errno == ENOENT) {
|
||||||
res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
|
res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
|
||||||
}
|
}
|
||||||
else if(statres != -1) { /* create parent */
|
else if(statres != -1) { /* create parent */
|
||||||
int mkdirres = mkdir(base, 0750);
|
int mkdirres = mkdir(base.bv_val, 0750);
|
||||||
if(mkdirres == -1) {
|
if(mkdirres == -1) {
|
||||||
res = LDAP_UNWILLING_TO_PERFORM;
|
res = LDAP_UNWILLING_TO_PERFORM;
|
||||||
}
|
}
|
||||||
@ -898,21 +894,17 @@ static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
|
|||||||
res = LDAP_UNWILLING_TO_PERFORM;
|
res = LDAP_UNWILLING_TO_PERFORM;
|
||||||
}/* container was possibly created, move on to add the entry */
|
}/* container was possibly created, move on to add the entry */
|
||||||
if(res == LDAP_SUCCESS) {
|
if(res == LDAP_SUCCESS) {
|
||||||
statres = stat(leaf_path, &stats);
|
statres = stat(leaf_path.bv_val, &stats);
|
||||||
if(statres == -1 && errno == ENOENT) {
|
if(statres == -1 && errno == ENOENT) {
|
||||||
res = (int) spew_entry(e, leaf_path);
|
res = (int) spew_entry(e, &leaf_path);
|
||||||
}
|
}
|
||||||
else /* it already exists */
|
else /* it already exists */
|
||||||
res = LDAP_ALREADY_EXISTS;
|
res = LDAP_ALREADY_EXISTS;
|
||||||
}
|
}
|
||||||
|
SLAP_FREE(base.bv_val);
|
||||||
|
SLAP_FREE(leaf_path.bv_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(leaf_path != NULL)
|
|
||||||
SLAP_FREE(leaf_path);
|
|
||||||
if(base != NULL)
|
|
||||||
SLAP_FREE(base);
|
|
||||||
if(base_ldif != NULL)
|
|
||||||
SLAP_FREE(base_ldif);
|
|
||||||
if(res == LDAP_SUCCESS) {
|
if(res == LDAP_SUCCESS) {
|
||||||
ni->tool_put_entry_flag = 1;
|
ni->tool_put_entry_flag = 1;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user