Dmitry Belyavskiy 149505
diff -up openssh-8.7p1/scp.c.scp-sftpdirs openssh-8.7p1/scp.c
Dmitry Belyavskiy 149505
--- openssh-8.7p1/scp.c.scp-sftpdirs	2022-02-07 12:31:07.407740407 +0100
Dmitry Belyavskiy 149505
+++ openssh-8.7p1/scp.c	2022-02-07 12:31:07.409740424 +0100
Dmitry Belyavskiy 149505
@@ -1324,7 +1324,7 @@ source_sftp(int argc, char *src, char *t
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 	if (src_is_dir && iamrecursive) {
Dmitry Belyavskiy 149505
 		if (upload_dir(conn, src, abs_dst, pflag,
Dmitry Belyavskiy 149505
-		    SFTP_PROGRESS_ONLY, 0, 0, 1) != 0) {
Dmitry Belyavskiy 149505
+		    SFTP_PROGRESS_ONLY, 0, 0, 1, 1) != 0) {
Dmitry Belyavskiy 149505
 			error("failed to upload directory %s to %s",
Dmitry Belyavskiy 149505
 				src, abs_dst);
Dmitry Belyavskiy 149505
 			errs = 1;
Dmitry Belyavskiy 149505
diff -up openssh-8.7p1/sftp-client.c.scp-sftpdirs openssh-8.7p1/sftp-client.c
Dmitry Belyavskiy 149505
--- openssh-8.7p1/sftp-client.c.scp-sftpdirs	2021-08-20 06:03:49.000000000 +0200
Dmitry Belyavskiy 149505
+++ openssh-8.7p1/sftp-client.c	2022-02-07 12:47:59.117516131 +0100
Dmitry Belyavskiy 149505
@@ -971,7 +971,7 @@ do_fsetstat(struct sftp_conn *conn, cons
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 /* Implements both the realpath and expand-path operations */
Dmitry Belyavskiy 149505
 static char *
Dmitry Belyavskiy 149505
-do_realpath_expand(struct sftp_conn *conn, const char *path, int expand)
Dmitry Belyavskiy 149505
+do_realpath_expand(struct sftp_conn *conn, const char *path, int expand, int create_dir)
Dmitry Belyavskiy 149505
 {
Dmitry Belyavskiy 149505
 	struct sshbuf *msg;
Dmitry Belyavskiy 149505
 	u_int expected_id, count, id;
Dmitry Belyavskiy cf05a2
@@ -1012,9 +1012,38 @@ do_realpath_expand(struct sftp_conn *con
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 		if ((r = sshbuf_get_u32(msg, &status)) != 0)
Dmitry Belyavskiy 149505
 			fatal_fr(r, "parse status");
Dmitry Belyavskiy 149505
-		error("Couldn't canonicalize: %s", fx2txt(status));
Dmitry Belyavskiy 149505
-		sshbuf_free(msg);
Dmitry Belyavskiy 149505
-		return NULL;
Dmitry Belyavskiy 149505
+		if ((status == SSH2_FX_NO_SUCH_FILE) && create_dir)  {
Dmitry Belyavskiy cf05a2
+			memset(&a, '\0', sizeof(a));
Dmitry Belyavskiy 149505
+			if ((r = do_mkdir(conn, path, &a, 0)) != 0) {
Dmitry Belyavskiy 149505
+				sshbuf_free(msg);
Dmitry Belyavskiy 149505
+				return NULL;
Dmitry Belyavskiy 149505
+			}
Dmitry Belyavskiy 149505
+
Dmitry Belyavskiy 149505
+			send_string_request(conn, id, SSH2_FXP_REALPATH,
Dmitry Belyavskiy 149505
+					path, strlen(path));
Dmitry Belyavskiy 149505
+
Dmitry Belyavskiy 149505
+			get_msg(conn, msg);
Dmitry Belyavskiy 149505
+			if ((r = sshbuf_get_u8(msg, &type)) != 0 ||
Dmitry Belyavskiy 149505
+					(r = sshbuf_get_u32(msg, &id)) != 0)
Dmitry Belyavskiy 149505
+				fatal_fr(r, "parse");
Dmitry Belyavskiy 149505
+
Dmitry Belyavskiy 149505
+			if (id != expected_id)
Dmitry Belyavskiy 149505
+				fatal("ID mismatch (%u != %u)", id, expected_id);
Dmitry Belyavskiy 149505
+
Dmitry Belyavskiy 149505
+			if (type == SSH2_FXP_STATUS) {
Dmitry Belyavskiy 149505
+				u_int status;
Dmitry Belyavskiy 149505
+
Dmitry Belyavskiy 149505
+				if ((r = sshbuf_get_u32(msg, &status)) != 0)
Dmitry Belyavskiy 149505
+					fatal_fr(r, "parse status");
Dmitry Belyavskiy 149505
+				error("Couldn't canonicalize: %s", fx2txt(status));
Dmitry Belyavskiy 149505
+				sshbuf_free(msg);
Dmitry Belyavskiy 149505
+				return NULL;
Dmitry Belyavskiy 149505
+			}
Dmitry Belyavskiy 149505
+		} else {
Dmitry Belyavskiy 149505
+			error("Couldn't canonicalize: %s", fx2txt(status));
Dmitry Belyavskiy 149505
+			sshbuf_free(msg);
Dmitry Belyavskiy 149505
+			return NULL;
Dmitry Belyavskiy 149505
+		}
Dmitry Belyavskiy 149505
 	} else if (type != SSH2_FXP_NAME)
Dmitry Belyavskiy 149505
 		fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Dmitry Belyavskiy 149505
 		    SSH2_FXP_NAME, type);
Dmitry Belyavskiy 149505
@@ -1039,9 +1067,9 @@ do_realpath_expand(struct sftp_conn *con
Dmitry Belyavskiy 149505
 }
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 char *
Dmitry Belyavskiy 149505
-do_realpath(struct sftp_conn *conn, const char *path)
Dmitry Belyavskiy 149505
+do_realpath(struct sftp_conn *conn, const char *path, int create_dir)
Dmitry Belyavskiy 149505
 {
Dmitry Belyavskiy 149505
-	return do_realpath_expand(conn, path, 0);
Dmitry Belyavskiy 149505
+	return do_realpath_expand(conn, path, 0, create_dir);
Dmitry Belyavskiy 149505
 }
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 int
Dmitry Belyavskiy 149505
@@ -1055,9 +1083,9 @@ do_expand_path(struct sftp_conn *conn, c
Dmitry Belyavskiy 149505
 {
Dmitry Belyavskiy 149505
 	if (!can_expand_path(conn)) {
Dmitry Belyavskiy 149505
 		debug3_f("no server support, fallback to realpath");
Dmitry Belyavskiy 149505
-		return do_realpath_expand(conn, path, 0);
Dmitry Belyavskiy 149505
+		return do_realpath_expand(conn, path, 0, 0);
Dmitry Belyavskiy 149505
 	}
Dmitry Belyavskiy 149505
-	return do_realpath_expand(conn, path, 1);
Dmitry Belyavskiy 149505
+	return do_realpath_expand(conn, path, 1, 0);
Dmitry Belyavskiy 149505
 }
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 int
Dmitry Belyavskiy 149505
@@ -1807,7 +1835,7 @@ download_dir(struct sftp_conn *conn, con
Dmitry Belyavskiy 149505
 	char *src_canon;
Dmitry Belyavskiy 149505
 	int ret;
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
-	if ((src_canon = do_realpath(conn, src)) == NULL) {
Dmitry Belyavskiy 149505
+	if ((src_canon = do_realpath(conn, src, 0)) == NULL) {
Dmitry Belyavskiy 149505
 		error("Unable to canonicalize path \"%s\"", src);
Dmitry Belyavskiy 149505
 		return -1;
Dmitry Belyavskiy 149505
 	}
Dmitry Belyavskiy 149505
@@ -2115,12 +2143,12 @@ upload_dir_internal(struct sftp_conn *co
Dmitry Belyavskiy 149505
 int
Dmitry Belyavskiy 149505
 upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
Dmitry Belyavskiy 149505
     int preserve_flag, int print_flag, int resume, int fsync_flag,
Dmitry Belyavskiy 149505
-    int follow_link_flag)
Dmitry Belyavskiy 149505
+    int follow_link_flag, int create_dir)
Dmitry Belyavskiy 149505
 {
Dmitry Belyavskiy 149505
 	char *dst_canon;
Dmitry Belyavskiy 149505
 	int ret;
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
-	if ((dst_canon = do_realpath(conn, dst)) == NULL) {
Dmitry Belyavskiy 149505
+	if ((dst_canon = do_realpath(conn, dst, create_dir)) == NULL) {
Dmitry Belyavskiy 149505
 		error("Unable to canonicalize path \"%s\"", dst);
Dmitry Belyavskiy 149505
 		return -1;
Dmitry Belyavskiy 149505
 	}
Dmitry Belyavskiy 149505
@@ -2557,7 +2585,7 @@ crossload_dir(struct sftp_conn *from, st
Dmitry Belyavskiy 149505
 	char *from_path_canon;
Dmitry Belyavskiy 149505
 	int ret;
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
-	if ((from_path_canon = do_realpath(from, from_path)) == NULL) {
Dmitry Belyavskiy 149505
+	if ((from_path_canon = do_realpath(from, from_path, 0)) == NULL) {
Dmitry Belyavskiy 149505
 		error("Unable to canonicalize path \"%s\"", from_path);
Dmitry Belyavskiy 149505
 		return -1;
Dmitry Belyavskiy 149505
 	}
Dmitry Belyavskiy 149505
diff -up openssh-8.7p1/sftp-client.h.scp-sftpdirs openssh-8.7p1/sftp-client.h
Dmitry Belyavskiy 149505
--- openssh-8.7p1/sftp-client.h.scp-sftpdirs	2021-08-20 06:03:49.000000000 +0200
Dmitry Belyavskiy 149505
+++ openssh-8.7p1/sftp-client.h	2022-02-07 12:31:07.410740433 +0100
Dmitry Belyavskiy 149505
@@ -111,7 +111,7 @@ int do_fsetstat(struct sftp_conn *, cons
Dmitry Belyavskiy 149505
 int do_lsetstat(struct sftp_conn *conn, const char *path, Attrib *a);
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 /* Canonicalise 'path' - caller must free result */
Dmitry Belyavskiy 149505
-char *do_realpath(struct sftp_conn *, const char *);
Dmitry Belyavskiy 149505
+char *do_realpath(struct sftp_conn *, const char *, int);
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 /* Canonicalisation with tilde expansion (requires server extension) */
Dmitry Belyavskiy 149505
 char *do_expand_path(struct sftp_conn *, const char *);
Dmitry Belyavskiy 149505
@@ -159,7 +159,7 @@ int do_upload(struct sftp_conn *, const
Dmitry Belyavskiy 149505
  * times if 'pflag' is set
Dmitry Belyavskiy 149505
  */
Dmitry Belyavskiy 149505
 int upload_dir(struct sftp_conn *, const char *, const char *, int, int, int,
Dmitry Belyavskiy 149505
-    int, int);
Dmitry Belyavskiy 149505
+    int, int, int);
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
 /*
Dmitry Belyavskiy 149505
  * Download a 'from_path' from the 'from' connection and upload it to
Dmitry Belyavskiy 149505
diff -up openssh-8.7p1/sftp.c.scp-sftpdirs openssh-8.7p1/sftp.c
Dmitry Belyavskiy 149505
--- openssh-8.7p1/sftp.c.scp-sftpdirs	2021-08-20 06:03:49.000000000 +0200
Dmitry Belyavskiy 149505
+++ openssh-8.7p1/sftp.c	2022-02-07 12:31:07.411740442 +0100
Dmitry Belyavskiy 149505
@@ -760,7 +760,7 @@ process_put(struct sftp_conn *conn, cons
Dmitry Belyavskiy 149505
 		if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
Dmitry Belyavskiy 149505
 			if (upload_dir(conn, g.gl_pathv[i], abs_dst,
Dmitry Belyavskiy 149505
 			    pflag || global_pflag, 1, resume,
Dmitry Belyavskiy 149505
-			    fflag || global_fflag, 0) == -1)
Dmitry Belyavskiy 149505
+			    fflag || global_fflag, 0, 0) == -1)
Dmitry Belyavskiy 149505
 				err = -1;
Dmitry Belyavskiy 149505
 		} else {
Dmitry Belyavskiy 149505
 			if (do_upload(conn, g.gl_pathv[i], abs_dst,
Dmitry Belyavskiy 149505
@@ -1577,7 +1577,7 @@ parse_dispatch_command(struct sftp_conn
Dmitry Belyavskiy 149505
 		if (path1 == NULL || *path1 == '\0')
Dmitry Belyavskiy 149505
 			path1 = xstrdup(startdir);
Dmitry Belyavskiy 149505
 		path1 = make_absolute(path1, *pwd);
Dmitry Belyavskiy 149505
-		if ((tmp = do_realpath(conn, path1)) == NULL) {
Dmitry Belyavskiy 149505
+		if ((tmp = do_realpath(conn, path1, 0)) == NULL) {
Dmitry Belyavskiy 149505
 			err = 1;
Dmitry Belyavskiy 149505
 			break;
Dmitry Belyavskiy 149505
 		}
Dmitry Belyavskiy 149505
@@ -2160,7 +2160,7 @@ interactive_loop(struct sftp_conn *conn,
Dmitry Belyavskiy 149505
 	}
Dmitry Belyavskiy 149505
 #endif /* USE_LIBEDIT */
Dmitry Belyavskiy 149505
 
Dmitry Belyavskiy 149505
-	remote_path = do_realpath(conn, ".");
Dmitry Belyavskiy 149505
+	remote_path = do_realpath(conn, ".", 0);
Dmitry Belyavskiy 149505
 	if (remote_path == NULL)
Dmitry Belyavskiy 149505
 		fatal("Need cwd");
Dmitry Belyavskiy 149505
 	startdir = xstrdup(remote_path);