rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

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

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