Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
92 changes: 78 additions & 14 deletions cf-net/cf-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static const Description COMMANDS[] =
{"opendir", "List files and folders in a directory",
"cf-net opendir masterfiles"},
{"getdir", "Recursively downloads files and folders in a directory",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

Changelog: Files and directories copied by `cf-net getdir` are now created with permissions `0600` / `0700` receptively. Use the `-p` / `--preserve-perms` option to preserve the remote permissions.

"cf-net getdir masterfiles/ -o /tmp/"},
"cf-net getdir masterfiles/ -o /tmp/ [-p]"},
{NULL, NULL, NULL}
};

Expand Down Expand Up @@ -172,6 +172,11 @@ static const char *command_strings[] =
//*******************************************************************
#define RECURSIONLIMIT 50 // GetDir

/* Modes applied by getdir unless --preserve-perms is given, so that a remote
* server cannot choose local permissions (e.g. setuid bits) */
#define GETDIR_FILE_PERMS (S_IRUSR | S_IWUSR)
#define GETDIR_DIR_PERMS (S_IRWXU)


//*******************************************************************
// FUNCTION DECLARATIONS:
Expand Down Expand Up @@ -611,7 +616,12 @@ static int CFNetHelpTopic(const char *topic)
"\nto download files. By default the directory is saved with its basename"
"\nin the current working directory (cwd). Override the destination using"
"\nthe -o path option."
"\n\nUsage: cf-net getdir [-o output_path] <remote_directory>"
"\n\nBy default, downloaded files are created with mode 0600 and"
"\ndirectories with mode 0700, regardless of their remote permissions."
"\nUse the -p (--preserve-perms) option to apply the remote permissions"
"\ninstead. With -p, a warning is logged for every remote file that has"
"\nthe setuid or setgid bit set."
"\n\nUsage: cf-net getdir [-o output_path] [-p] <remote_directory>"
"\n\nExample: cf-net getdir -o /tmp/backup masterfiles/\n");
}
else
Expand Down Expand Up @@ -1007,21 +1017,33 @@ enum get_dir_error
};

static bool CFNetGetWithPerms(AgentConnection *conn, const char *remote_path,
const char *local_path, bool print_stats)
const char *local_path, bool print_stats,
bool preserve_perms)
{
assert(conn != NULL);
assert(remote_path != NULL);
assert(local_path != NULL);

struct stat perms;
if (!ProtocolStat(conn, remote_path, &perms))
struct stat sb;
if (!ProtocolStat(conn, remote_path, &sb))
{
Log(LOG_LEVEL_ERR, "Failed to stat remote file: %s:%s",
conn->this_server, remote_path);
return false;
}

if (!ProtocolGet(conn, remote_path, local_path, perms.st_size, perms.st_mode, print_stats))
const mode_t remote_perms = sb.st_mode & 07777;
const mode_t local_perms = preserve_perms ? remote_perms : GETDIR_FILE_PERMS;

if (preserve_perms && (remote_perms & (S_ISUID | S_ISGID)) != 0)
{
Log(LOG_LEVEL_WARNING,
"Copying file %s:%s with setuid/setgid bit to '%s' (mode %04jo)",
conn->this_server, remote_path, local_path,
(uintmax_t) remote_perms);
}

if (!ProtocolGet(conn, remote_path, local_path, sb.st_size, local_perms, print_stats))
{
Log(LOG_LEVEL_ERR, "Failed to get remote file: %s:%s",
conn->this_server, remote_path);
Expand Down Expand Up @@ -1064,6 +1086,7 @@ static int process_dir_recursive(AgentConnection *conn,
const char *remote_path,
const char *local_path,
bool print_stats,
bool preserve_perms,
int limit)
{
int ret = 0;
Expand Down Expand Up @@ -1140,14 +1163,17 @@ static int process_dir_recursive(AgentConnection *conn,

if (S_ISDIR(sb.st_mode)) // Is directory
{
if (!create_local_dir(local_path, item, sb.st_mode))
const mode_t dir_perms =
preserve_perms ? (sb.st_mode & 07777) : GETDIR_DIR_PERMS;
if (!create_local_dir(local_path, item, dir_perms))
{
// Error already logged
SeqDestroy(items);
return GET_DIR_ERROR;
}
ret = process_dir_recursive(conn, remote_full, local_full,
print_stats, (limit - 1));
print_stats, preserve_perms,
(limit - 1));

if (ret != GET_DIR_OK)
{
Expand All @@ -1157,7 +1183,8 @@ static int process_dir_recursive(AgentConnection *conn,
}
else
{
if (!CFNetGetWithPerms(conn, remote_full, local_full, print_stats))
if (!CFNetGetWithPerms(conn, remote_full, local_full, print_stats,
preserve_perms))
{
SeqDestroy(items);
return GET_DIR_ERROR;
Expand All @@ -1177,6 +1204,7 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)

char *local_dir = NULL;
bool specified_path = false;
bool preserve_perms = false;

int argc = 0;
while (args[argc] != NULL)
Expand All @@ -1185,8 +1213,9 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
}

static struct option longopts[] = {
{ "output", required_argument, NULL, 'o' },
{ NULL, 0, NULL, 0 }
{ "output", required_argument, NULL, 'o' },
{ "preserve-perms", no_argument, NULL, 'p' },
{ NULL, 0, NULL, 0 }
};
if (argc <= 1)
{
Expand All @@ -1196,7 +1225,7 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
optind = 0;
extern char *optarg;
int c = 0;
const char *optstr = "o:";
const char *optstr = "o:p";
while ((c = getopt_long(argc, args, optstr, longopts, NULL))
!= -1)
{
Expand All @@ -1215,6 +1244,11 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
specified_path = true;
break;
}
case 'p':
{
preserve_perms = true;
break;
}
case ':':
case '?':
{
Expand Down Expand Up @@ -1243,8 +1277,12 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
char *base = xstrdup(basename(tmp));
free(tmp);

/* MakeParentDirectoryPerms() only accepts absolute paths, so a relative
* output path is resolved against the current working directory */
Comment on lines +1246 to +1247

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  /* MakeParentDirectoryPerms() rejects relative paths.
   * Make a relative -o path absolute by prefixing the cwd. */

I was a bit confused by that comment, maybe this reads better?


This commit deserves a change log entry.


Bug already present on master.

It goes without saying.

const bool use_cwd = !specified_path || !IsAbsoluteFileName(local_dir);

char cwd[PATH_MAX];
if (!specified_path && getcwd(cwd, sizeof(cwd)) == NULL)
if (use_cwd && getcwd(cwd, sizeof(cwd)) == NULL)
{
Log(LOG_LEVEL_ERR, "Failed to get current working directory: %s", GetErrorStr());
free(base);
Expand All @@ -1253,7 +1291,18 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
}

tmp = local_dir;
local_dir = StringFormat("%s/%s/", (specified_path ? local_dir : cwd), base);
if (!specified_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you treat "no -o as -o ., then maybe this logic could be simpler. IDK

{
local_dir = StringFormat("%s/%s/", cwd, base);
}
else if (use_cwd)
{
local_dir = StringFormat("%s/%s/%s/", cwd, local_dir, base);
}
else
{
local_dir = StringFormat("%s/%s/", local_dir, base);
}
free(tmp);

if (strlen(local_dir) > PATH_MAX)
Expand Down Expand Up @@ -1290,10 +1339,25 @@ static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
return -1;
}

/* Subdirectories are created as they are found, but files directly in
* remote_dir need the top-level local directory to exist already */
bool force = false;
const mode_t dir_perms =
preserve_perms ? (sb.st_mode & 07777) : GETDIR_DIR_PERMS;
if (!MakeParentDirectoryPerms(local_dir, force, NULL, dir_perms))
{
// Error already logged
free(local_dir);
free(base);
CFNetDisconnect(conn);
return -1;
}

int ret = process_dir_recursive(conn,
remote_dir,
local_dir,
opts->print_stats,
preserve_perms,
RECURSIONLIMIT);
if (ret == GET_DIR_RECURSION_LIMIT)
{
Expand Down
59 changes: 59 additions & 0 deletions tests/acceptance/26_cf-net/serial/cf-net_getdir_create_output.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
body common control
{
inputs => { "../../default.sub.cf", "../../run_with_server.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
}

bundle agent init
{
files:
# Only a file, so no subdirectory creation creates the output directory
"$(G.testdir)/remote/top.txt"
create => "true",
content => "top";

methods:
"any" usebundle => dcs_fini("$(G.testdir)/out");
"any" usebundle => generate_key;
"any" usebundle => trust_key;

"any"
usebundle => start_server(
"$(this.promise_dirname)/serverd_admit_localhost_15000.cf.srv"
);
}

bundle agent test
{
meta:
"description"
string => "getdir creates the output directory if it does not exist";

"test_skip_needs_work"
string => "windows",
meta => { "ENT-10254" };

commands:
"$(sys.cf_net)"
args => " -H 127.0.0.1:15000 getdir -o $(G.testdir)/out $(G.testdir)/remote";
}

bundle agent check
{
methods:
"check"
usebundle => dcs_check_diff(
"$(G.testdir)/out/remote/top.txt",
"$(G.testdir)/remote/top.txt",
"$(this.promise_filename)"
);
}

bundle agent destroy
{
methods:
"any"
usebundle => stop_server(
"$(this.promise_dirname)/serverd_admit_localhost_15000.cf.srv"
);
}
59 changes: 59 additions & 0 deletions tests/acceptance/26_cf-net/serial/cf-net_getdir_relative_output.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
body common control
{
inputs => { "../../default.sub.cf", "../../run_with_server.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
}

bundle agent init
{
files:
# Only a subdirectory, which getdir has to create locally
"$(G.testdir)/remote/sub/nested.txt"
create => "true",
content => "nested";

methods:
"any" usebundle => dcs_fini("$(G.testdir)/out");
"any" usebundle => generate_key;
"any" usebundle => trust_key;

"any"
usebundle => start_server(
"$(this.promise_dirname)/serverd_admit_localhost_15000.cf.srv"
);
}

bundle agent test
{
meta:
"description" string => "getdir with a relative output path (-o)";

"test_skip_needs_work"
string => "windows",
meta => { "ENT-10254" };

commands:
"$(sys.cf_net)"
args => " -H 127.0.0.1:15000 getdir -o out $(G.testdir)/remote",
contain => in_dir_shell("$(G.testdir)");
}

bundle agent check
{
methods:
"check"
usebundle => dcs_check_diff(
"$(G.testdir)/out/remote/sub/nested.txt",
"$(G.testdir)/remote/sub/nested.txt",
"$(this.promise_filename)"
);
}

bundle agent destroy
{
methods:
"any"
usebundle => stop_server(
"$(this.promise_dirname)/serverd_admit_localhost_15000.cf.srv"
);
}
Loading