From dd48ca17c36718d1fc6238ba4370bd0fc82aef14 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Fri, 24 Sep 2010 14:03:42 -0400 Subject: [PATCH] symlink: moved follow_link() to symlink.c [cosmetic] Signed-off-by: Casey Bodley --- daemon/open.c | 109 ---------------------------------------------- daemon/symlink.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 109 deletions(-) diff --git a/daemon/open.c b/daemon/open.c index 6fc291c..e2b65bb 100644 --- a/daemon/open.c +++ b/daemon/open.c @@ -212,115 +212,6 @@ static int check_execute_access(nfs41_open_state *state) return status; } -int abs_path_link( - OUT nfs41_abs_path *path, - IN char *path_pos, - IN const char *link, - IN uint32_t link_len) -{ - nfs41_component name; - const char *path_max = path->path + NFS41_MAX_PATH_LEN; - const char *link_pos = link; - const char *link_end = link + link_len; - int status = NO_ERROR; - - dprintf(2, "--> abs_path_link('%s', '%s')\n", path->path, link); - - /* if link is an absolute path, start path_pos at the beginning */ - if (is_delimiter(*link)) - path_pos = path->path; - - /* eat trailing slashes from the path */ - path_pos = (char*)prev_non_delimiter(path_pos, path->path); - if (path_pos > path->path) - path_pos = (char*)next_delimiter(path_pos, path_pos); - - /* copy each component of link into the path */ - while (next_component(link_pos, link_end, &name)) { - link_pos = name.name + name.len; - - /* handle special components . and .. */ - if (name.len == 1 && name.name[0] == '.') - continue; - if (name.len == 2 && name.name[0] == '.' && name.name[1] == '.') { - /* back path_pos up by one component */ - if (!last_component(path->path, path_pos, &name)) { - eprintf("symlink with .. that points below server root!\n"); - status = ERROR_BAD_NETPATH; - goto out; - } - path_pos = (char*)prev_delimiter(name.name, path->path); - continue; - } - - /* add a \ and copy the component */ - if (FAILED(StringCchCopyNA(path_pos, - path_max-path_pos, "\\", 1))) { - status = ERROR_BUFFER_OVERFLOW; - goto out; - } - path_pos++; - if (FAILED(StringCchCopyNA(path_pos, - path_max-path_pos, name.name, name.len))) { - status = ERROR_BUFFER_OVERFLOW; - goto out; - } - path_pos += name.len; - } - - /* make sure the path is null terminated */ - if (path_pos == path_max) { - status = ERROR_BUFFER_OVERFLOW; - goto out; - } - *path_pos = '\0'; -out: - path->len = (unsigned short)(path_pos - path->path); - dprintf(2, "<-- abs_path_link('%s') returning %d\n", path->path, status); - return status; -} - -static int follow_link( - IN open_upcall_args *args, - IN nfs41_open_state *state) -{ - char link[NFS41_MAX_PATH_LEN]; - ptrdiff_t path_offset; - uint32_t link_len; - int status; - - /* read the link */ - status = nfs41_readlink(state->session, &state->file, - NFS41_MAX_PATH_LEN, link, &link_len); - if (status) { - eprintf("nfs41_readlink() failed with %s\n", - nfs_error_string(status)); - status = ERROR_PATH_NOT_FOUND; - goto out; - } - - /* overwrite the last component of the path; get the starting offset */ - path_offset = (state->parent.name.name + state->parent.name.len) - - state->path.path; - - /* copy the path and update it with the results from link */ - args->symlink.len = state->path.len; - if (FAILED(StringCchCopyNA(args->symlink.path, NFS41_MAX_PATH_LEN, - state->path.path, state->path.len))) { - status = ERROR_BUFFER_OVERFLOW; - goto out; - } - status = abs_path_link(&args->symlink, - args->symlink.path + path_offset, link, link_len); - if (status) { - eprintf("abs_path_link() failed with %d\n", status); - status = ERROR_PATH_NOT_FOUND; - goto out; - } -out: - return status; -} - int handle_open(nfs41_upcall *upcall) { int status = 0; diff --git a/daemon/symlink.c b/daemon/symlink.c index 680b64f..2ea6e01 100644 --- a/daemon/symlink.c +++ b/daemon/symlink.c @@ -30,6 +30,116 @@ #include "daemon_debug.h" +int abs_path_link( + OUT nfs41_abs_path *path, + IN char *path_pos, + IN const char *link, + IN uint32_t link_len) +{ + nfs41_component name; + const char *path_max = path->path + NFS41_MAX_PATH_LEN; + const char *link_pos = link; + const char *link_end = link + link_len; + int status = NO_ERROR; + + dprintf(2, "--> abs_path_link('%s', '%s')\n", path->path, link); + + /* if link is an absolute path, start path_pos at the beginning */ + if (is_delimiter(*link)) + path_pos = path->path; + + /* eat trailing slashes from the path */ + path_pos = (char*)prev_non_delimiter(path_pos, path->path); + if (path_pos > path->path) + path_pos = (char*)next_delimiter(path_pos, path_pos); + + /* copy each component of link into the path */ + while (next_component(link_pos, link_end, &name)) { + link_pos = name.name + name.len; + + /* handle special components . and .. */ + if (name.len == 1 && name.name[0] == '.') + continue; + if (name.len == 2 && name.name[0] == '.' && name.name[1] == '.') { + /* back path_pos up by one component */ + if (!last_component(path->path, path_pos, &name)) { + eprintf("symlink with .. that points below server root!\n"); + status = ERROR_BAD_NETPATH; + goto out; + } + path_pos = (char*)prev_delimiter(name.name, path->path); + continue; + } + + /* add a \ and copy the component */ + if (FAILED(StringCchCopyNA(path_pos, + path_max-path_pos, "\\", 1))) { + status = ERROR_BUFFER_OVERFLOW; + goto out; + } + path_pos++; + if (FAILED(StringCchCopyNA(path_pos, + path_max-path_pos, name.name, name.len))) { + status = ERROR_BUFFER_OVERFLOW; + goto out; + } + path_pos += name.len; + } + + /* make sure the path is null terminated */ + if (path_pos == path_max) { + status = ERROR_BUFFER_OVERFLOW; + goto out; + } + *path_pos = '\0'; +out: + path->len = (unsigned short)(path_pos - path->path); + dprintf(2, "<-- abs_path_link('%s') returning %d\n", path->path, status); + return status; +} + +static int follow_link( + IN open_upcall_args *args, + IN nfs41_open_state *state) +{ + char link[NFS41_MAX_PATH_LEN]; + ptrdiff_t path_offset; + uint32_t link_len; + int status; + + /* read the link */ + status = nfs41_readlink(state->session, &state->file, + NFS41_MAX_PATH_LEN, link, &link_len); + if (status) { + eprintf("nfs41_readlink() failed with %s\n", + nfs_error_string(status)); + status = ERROR_PATH_NOT_FOUND; + goto out; + } + + /* overwrite the last component of the path; get the starting offset */ + path_offset = (state->parent.name.name + state->parent.name.len) - + state->path.path; + + /* copy the path and update it with the results from link */ + args->symlink.len = state->path.len; + if (FAILED(StringCchCopyNA(args->symlink.path, NFS41_MAX_PATH_LEN, + state->path.path, state->path.len))) { + status = ERROR_BUFFER_OVERFLOW; + goto out; + } + status = abs_path_link(&args->symlink, + args->symlink.path + path_offset, link, link_len); + if (status) { + eprintf("abs_path_link() failed with %d\n", status); + status = ERROR_PATH_NOT_FOUND; + goto out; + } +out: + return status; +} + + /* NFS41_SYMLINK */ int parse_symlink(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall) {