Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add motw to scp and sftp #614

Merged
merged 17 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions contrib/win32/win32compat/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2119,3 +2119,38 @@ strrstr(const char *inStr, const char *pattern)

return last;
}

int
add_mark_of_web(const char* filename)
{
// ZoneId=3 indicates the file comes from the Internet Zone
const char zoneIdentifier[] = "[ZoneTransfer]\nZoneId=3";
tgauth marked this conversation as resolved.
Show resolved Hide resolved
const DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
int status = 0;
char* filestreamPath = NULL;
DWORD numWritten = 0;
BOOL writeResult;
HANDLE file;
size_t filestreampath_length = strlen(filename) + strlen(":Zone.Identifier") + 1;

filestreamPath = malloc(filestreampath_length);
if (filestreamPath == NULL) {
return -1;
}
// create zone identifer file stream and write the Mark of the Web to it
tgauth marked this conversation as resolved.
Show resolved Hide resolved
sprintf_s(filestreamPath, filestreampath_length, "%s:Zone.Identifier", filename);
file = CreateFile(filestreamPath, GENERIC_WRITE, shareMode, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == file) {
status = -1;
goto cleanup;
}
writeResult = WriteFile(file, zoneIdentifier, (DWORD)strlen(zoneIdentifier), &numWritten, NULL);
CloseHandle(file);
if (!writeResult) {
status = -1;
}
cleanup:
free(filestreamPath);
return status;
}

3 changes: 2 additions & 1 deletion contrib/win32/win32compat/misc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ wchar_t* get_final_path_by_handle(HANDLE h);
int lookup_principal_name(const wchar_t * sam_account_name, wchar_t * user_principal_name);
BOOL is_bash_test_env();
int bash_to_win_path(const char *in, char *out, const size_t out_len);
void debug_assert_internal();
void debug_assert_internal();
int add_mark_of_web(const char* filename);
8 changes: 8 additions & 0 deletions scp.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@

#include "sftp-common.h"
#include "sftp-client.h"
#ifdef WINDOWS
#include "misc_internal.h"
#endif // WINDOWS

extern char *__progname;

Expand Down Expand Up @@ -2074,6 +2077,11 @@ sink(int argc, char **argv, const char *src)
omode = mode;
mode |= S_IWUSR;
#ifdef WINDOWS
if (add_mark_of_web(np) == -1 && verbose_mode) {
run_err("scp: %s: failed to add mark of the web\n", np);
exit(1);
};

// In windows, we would like to inherit the parent folder permissions by setting mode to USHRT_MAX.
if ((ofd = open(np, O_WRONLY|O_CREAT, USHRT_MAX)) == -1) {
#else
Expand Down
9 changes: 9 additions & 0 deletions sftp-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,15 @@ do_download(struct sftp_conn *conn, const char *remote_path,
}
}
close(local_fd);
#ifdef WINDOWS
if (add_mark_of_web(local_path) == -1) {
// (resume_flag ? 0 : O_TRUNC) on line 1355 requires us
// to add the mark of the web after transferring the file.
// if MOTW fails, we need to remove the file before exiting
remove(local_path);
fatal_f("%s: failed to add mark of the web", local_path);
}
#endif // WINDOWS
sshbuf_free(msg);
free(handle);

Expand Down
9 changes: 9 additions & 0 deletions sftp-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@

#include "sftp.h"
#include "sftp-common.h"
#ifdef WINDOWS
#include "misc_internal.h"
#endif // WINDOWS

char *sftp_realpath(const char *, char *); /* sftp-realpath.c */

Expand Down Expand Up @@ -863,6 +866,12 @@ process_write(u_int32_t id)
(r = sshbuf_get_string(iqueue, &data, &len)) != 0)
fatal_fr(r, "parse");

#ifdef WINDOWS
if (add_mark_of_web(resolved_path_utf8(handle_to_name(handle))) == -1) {
tgauth marked this conversation as resolved.
Show resolved Hide resolved
fatal_f("%s: failed to add mark of the web", resolved_path_utf8(handle_to_name(handle)));
}
#endif // WINDOWS

debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
id, handle_to_name(handle), handle, (unsigned long long)off, len);
fd = handle_to_fd(handle);
Expand Down