summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2003-03-26 04:02:52 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2003-03-26 04:02:52 +0000
commitc62d928d7a665d2378e8113e67f612ad5554c541 (patch)
treef57342f069e2b7f7e04ee2f51089640a96664e82
parenta44f7cffd2abc2e5768f35f1001724bec654207b (diff)
one last fix to the tree: race fix broke stuff; pr 3169; srp@srparish.net,
help from djm
-rw-r--r--usr.bin/ssh/sftp-server.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/usr.bin/ssh/sftp-server.c b/usr.bin/ssh/sftp-server.c
index e3dfc688baf..050570cc3f0 100644
--- a/usr.bin/ssh/sftp-server.c
+++ b/usr.bin/ssh/sftp-server.c
@@ -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.40 2003/03/05 22:33:43 markus Exp $");
+RCSID("$OpenBSD: sftp-server.c,v 1.41 2003/03/26 04:02:51 deraadt Exp $");
#include "buffer.h"
#include "bufaux.h"
@@ -816,20 +816,31 @@ process_rename(void)
u_int32_t id;
char *oldpath, *newpath;
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 (link(oldpath, newpath) == -1)
+ status = SSH2_FX_FAILURE;
+ if (lstat(oldpath, &sb) == -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 (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);
xfree(newpath);