]> andersk Git - gssapi-openssh.git/blobdiff - openssh/sftp-server.c
Import of OpenSSH 3.6.1p1
[gssapi-openssh.git] / openssh / sftp-server.c
index 84264693d73aeab364eedf8d0a0232e058f77334..9a66b4de7c61fa8c77daab322840f13b2ab6f821 100644 (file)
@@ -22,7 +22,7 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "includes.h"
-RCSID("$OpenBSD: sftp-server.c,v 1.38 2002/09/11 22:41:50 djm Exp $");
+RCSID("$OpenBSD: sftp-server.c,v 1.41 2003/03/26 04:02:51 deraadt Exp $");
 
 #include "buffer.h"
 #include "bufaux.h"
@@ -158,7 +158,7 @@ handle_new(int use, char *name, int fd, DIR *dirp)
                        handles[i].use = use;
                        handles[i].dirp = dirp;
                        handles[i].fd = fd;
-                       handles[i].name = name;
+                       handles[i].name = xstrdup(name);
                        return i;
                }
        }
@@ -230,9 +230,11 @@ handle_close(int handle)
        if (handle_is_ok(handle, HANDLE_FILE)) {
                ret = close(handles[handle].fd);
                handles[handle].use = HANDLE_UNUSED;
+               xfree(handles[handle].name);
        } else if (handle_is_ok(handle, HANDLE_DIR)) {
                ret = closedir(handles[handle].dirp);
                handles[handle].use = HANDLE_UNUSED;
+               xfree(handles[handle].name);
        } else {
                errno = ENOENT;
        }
@@ -396,7 +398,7 @@ process_open(void)
        if (fd < 0) {
                status = errno_to_portable(errno);
        } else {
-               handle = handle_new(HANDLE_FILE, xstrdup(name), fd, NULL);
+               handle = handle_new(HANDLE_FILE, name, fd, NULL);
                if (handle < 0) {
                        close(fd);
                } else {
@@ -681,7 +683,7 @@ process_opendir(void)
        if (dirp == NULL) {
                status = errno_to_portable(errno);
        } else {
-               handle = handle_new(HANDLE_DIR, xstrdup(path), 0, dirp);
+               handle = handle_new(HANDLE_DIR, path, 0, dirp);
                if (handle < 0) {
                        closedir(dirp);
                } else {
@@ -832,18 +834,32 @@ static void
 process_rename(void)
 {
        u_int32_t id;
-       struct stat st;
        char *oldpath, *newpath;
-       int ret, status = SSH2_FX_FAILURE;
+       int status;
+       struct stat sb;
 
        id = get_int();
        oldpath = get_string(NULL);
        newpath = get_string(NULL);
        TRACE("rename id %u old %s new %s", id, oldpath, newpath);
-       /* fail if 'newpath' exists */
-       if (stat(newpath, &st) == -1) {
-               ret = rename(oldpath, newpath);
-               status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
+       status = SSH2_FX_FAILURE;
+       if (lstat(oldpath, &sb) == -1)
+               status = errno_to_portable(errno);
+       else if (S_ISREG(sb.st_mode)) {
+               /* Race-free rename of regular files */
+               if (link(oldpath, newpath) == -1)
+                       status = errno_to_portable(errno);
+               else if (unlink(oldpath) == -1) {
+                       status = errno_to_portable(errno);
+                       /* clean spare link */
+                       unlink(newpath);
+               } else
+                       status = SSH2_FX_OK;
+       } else if (stat(newpath, &sb) == -1) {
+               if (rename(oldpath, newpath) == -1)
+                       status = errno_to_portable(errno);
+               else
+                       status = SSH2_FX_OK;
        }
        send_status(id, status);
        xfree(oldpath);
@@ -878,19 +894,16 @@ static void
 process_symlink(void)
 {
        u_int32_t id;
-       struct stat st;
        char *oldpath, *newpath;
-       int ret, status = SSH2_FX_FAILURE;
+       int ret, status;
 
        id = get_int();
        oldpath = get_string(NULL);
        newpath = get_string(NULL);
        TRACE("symlink id %u old %s new %s", id, oldpath, newpath);
-       /* fail if 'newpath' exists */
-       if (stat(newpath, &st) == -1) {
-               ret = symlink(oldpath, newpath);
-               status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
-       }
+       /* this will fail if 'newpath' exists */
+       ret = symlink(oldpath, newpath);
+       status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
        send_status(id, status);
        xfree(oldpath);
        xfree(newpath);
This page took 0.038142 seconds and 4 git commands to generate.