rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/openssh-8.7p1-sftpscp-dir-create.patch

9070b3
diff -up openssh-8.7p1/scp.c.sftpdirs openssh-8.7p1/scp.c
9070b3
--- openssh-8.7p1/scp.c.sftpdirs	2022-02-02 14:11:12.553447509 +0100
9070b3
+++ openssh-8.7p1/scp.c	2022-02-02 14:12:56.081316414 +0100
9070b3
@@ -130,6 +130,7 @@
9070b3
 #include "misc.h"
9070b3
 #include "progressmeter.h"
9070b3
 #include "utf8.h"
9070b3
+#include "sftp.h"
9070b3
 
9070b3
 #include "sftp-common.h"
9070b3
 #include "sftp-client.h"
9070b3
@@ -660,7 +661,7 @@ main(int argc, char **argv)
9070b3
 	 * Finally check the exit status of the ssh process, if one was forked
9070b3
 	 * and no error has occurred yet
9070b3
 	 */
9070b3
-	if (do_cmd_pid != -1 && errs == 0) {
9070b3
+	if (do_cmd_pid != -1 && (mode == MODE_SFTP || errs == 0)) {
9070b3
 		if (remin != -1)
9070b3
 		    (void) close(remin);
9070b3
 		if (remout != -1)
9070b3
@@ -1264,13 +1265,18 @@ tolocal(int argc, char **argv, enum scp_
9070b3
 static char *
9070b3
 prepare_remote_path(struct sftp_conn *conn, const char *path)
9070b3
 {
9070b3
+	size_t nslash;
9070b3
+
9070b3
 	/* Handle ~ prefixed paths */
9070b3
-	if (*path != '~')
9070b3
-		return xstrdup(path);
9070b3
 	if (*path == '\0' || strcmp(path, "~") == 0)
9070b3
 		return xstrdup(".");
9070b3
-	if (strncmp(path, "~/", 2) == 0)
9070b3
-		return xstrdup(path + 2);
9070b3
+	if (*path != '~')
9070b3
+		return xstrdup(path);
9070b3
+	if (strncmp(path, "~/", 2) == 0) {
9070b3
+		if ((nslash = strspn(path + 2, "/")) == strlen(path + 2))
9070b3
+			return xstrdup(".");
9070b3
+		return xstrdup(path + 2 + nslash);
9070b3
+	}
9070b3
 	if (can_expand_path(conn))
9070b3
 		return do_expand_path(conn, path);
9070b3
 	/* No protocol extension */
9070b3
@@ -1282,10 +1288,16 @@ void
9070b3
 source_sftp(int argc, char *src, char *targ, struct sftp_conn *conn)
9070b3
 {
9070b3
 	char *target = NULL, *filename = NULL, *abs_dst = NULL;
9070b3
-	int target_is_dir;
9070b3
-
9070b3
+	int src_is_dir, target_is_dir;
9070b3
+	Attrib a;
9070b3
+	struct stat st;
9070b3
+
9070b3
+	memset(&a, '\0', sizeof(a));
9070b3
+	if (stat(src, &st) != 0)
9070b3
+		fatal("stat local \"%s\": %s", src, strerror(errno));
9070b3
+	src_is_dir = S_ISDIR(st.st_mode);
9070b3
 	if ((filename = basename(src)) == NULL)
9070b3
-		fatal("basename %s: %s", src, strerror(errno));
9070b3
+		fatal("basename \"%s\": %s", src, strerror(errno));
9070b3
 
9070b3
 	/*
9070b3
 	 * No need to glob here - the local shell already took care of
9070b3
@@ -1295,8 +1307,12 @@ source_sftp(int argc, char *src, char *t
9070b3
 		cleanup_exit(255);
9070b3
 	target_is_dir = remote_is_dir(conn, target);
9070b3
 	if (targetshouldbedirectory && !target_is_dir) {
9070b3
-		fatal("Target is not a directory, but more files selected "
9070b3
-		    "for upload");
9070b3
+		debug("target directory \"%s\" does not exist", target);
9070b3
+		a.flags = SSH2_FILEXFER_ATTR_PERMISSIONS;
9070b3
+		a.perm = st.st_mode | 0700; /* ensure writable */
9070b3
+		if (do_mkdir(conn, target, &a, 1) != 0)
9070b3
+			cleanup_exit(255); /* error already logged */
9070b3
+		target_is_dir = 1;
9070b3
 	}
9070b3
 	if (target_is_dir)
9070b3
 		abs_dst = path_append(target, filename);
9070b3
@@ -1306,14 +1322,17 @@ source_sftp(int argc, char *src, char *t
9070b3
 	}
9070b3
 	debug3_f("copying local %s to remote %s", src, abs_dst);
9070b3
 
9070b3
-	if (local_is_dir(src) && iamrecursive) {
9070b3
+	if (src_is_dir && iamrecursive) {
9070b3
 		if (upload_dir(conn, src, abs_dst, pflag,
9070b3
 		    SFTP_PROGRESS_ONLY, 0, 0, 1) != 0) {
9070b3
-			fatal("failed to upload directory %s to %s",
9070b3
+			error("failed to upload directory %s to %s",
9070b3
 				src, abs_dst);
9070b3
+			errs = 1;
9070b3
 		}
9070b3
-	} else if (do_upload(conn, src, abs_dst, pflag, 0, 0) != 0)
9070b3
-		fatal("failed to upload file %s to %s", src, abs_dst);
9070b3
+	} else if (do_upload(conn, src, abs_dst, pflag, 0, 0) != 0) {
9070b3
+		error("failed to upload file %s to %s", src, abs_dst);
9070b3
+		errs = 1;
9070b3
+	}
9070b3
 
9070b3
 	free(abs_dst);
9070b3
 	free(target);
9070b3
@@ -1487,14 +1506,15 @@ sink_sftp(int argc, char *dst, const cha
9070b3
 	char *abs_dst = NULL;
9070b3
 	glob_t g;
9070b3
 	char *filename, *tmp = NULL;
9070b3
-	int i, r, err = 0;
9070b3
+	int i, r, err = 0, dst_is_dir;
9070b3
+	struct stat st;
9070b3
 
9070b3
 	memset(&g, 0, sizeof(g));
9070b3
+
9070b3
 	/*
9070b3
 	 * Here, we need remote glob as SFTP can not depend on remote shell
9070b3
 	 * expansions
9070b3
 	 */
9070b3
-
9070b3
 	if ((abs_src = prepare_remote_path(conn, src)) == NULL) {
9070b3
 		err = -1;
9070b3
 		goto out;
9070b3
@@ -1510,11 +1530,24 @@ sink_sftp(int argc, char *dst, const cha
9070b3
 		goto out;
9070b3
 	}
9070b3
 
9070b3
-	if (g.gl_matchc > 1 && !local_is_dir(dst)) {
9070b3
-		error("Multiple files match pattern, but destination "
9070b3
-		    "\"%s\" is not a directory", dst);
9070b3
-		err = -1;
9070b3
-		goto out;
9070b3
+	if ((r = stat(dst, &st)) != 0)
9070b3
+		debug2_f("stat local \"%s\": %s", dst, strerror(errno));
9070b3
+	dst_is_dir = r == 0 && S_ISDIR(st.st_mode);
9070b3
+
9070b3
+	if (g.gl_matchc > 1 && !dst_is_dir) {
9070b3
+		if (r == 0) {
9070b3
+			error("Multiple files match pattern, but destination "
9070b3
+			    "\"%s\" is not a directory", dst);
9070b3
+			err = -1;
9070b3
+			goto out;
9070b3
+		}
9070b3
+		debug2_f("creating destination \"%s\"", dst);
9070b3
+		if (mkdir(dst, 0777) != 0) {
9070b3
+			error("local mkdir \"%s\": %s", dst, strerror(errno));
9070b3
+			err = -1;
9070b3
+			goto out;
9070b3
+		}
9070b3
+		dst_is_dir = 1;
9070b3
 	}
9070b3
 
9070b3
 	for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
9070b3
@@ -1525,7 +1558,7 @@ sink_sftp(int argc, char *dst, const cha
9070b3
 			goto out;
9070b3
 		}
9070b3
 
9070b3
-		if (local_is_dir(dst))
9070b3
+		if (dst_is_dir)
9070b3
 			abs_dst = path_append(dst, filename);
9070b3
 		else
9070b3
 			abs_dst = xstrdup(dst);
9070b3
@@ -1551,7 +1584,8 @@ out:
9070b3
 	free(tmp);
9070b3
 	globfree(&g);
9070b3
 	if (err == -1) {
9070b3
-		fatal("Failed to download file '%s'", src);
9070b3
+		error("Failed to download '%s'", src);
9070b3
+		errs = 1;
9070b3
 	}
9070b3
 }
9070b3