2020-09-11 20:49:16 +03:00
|
|
|
#include "builtin.h"
|
2023-05-16 09:33:57 +03:00
|
|
|
#include "config.h"
|
2023-03-21 09:25:54 +03:00
|
|
|
#include "gettext.h"
|
2020-09-11 20:49:16 +03:00
|
|
|
#include "parse-options.h"
|
2023-04-22 23:17:20 +03:00
|
|
|
#include "path.h"
|
|
|
|
#include "repository.h"
|
2020-09-11 20:49:16 +03:00
|
|
|
#include "run-command.h"
|
|
|
|
#include "string-list.h"
|
|
|
|
|
|
|
|
static const char * const for_each_repo_usage[] = {
|
2022-10-13 18:39:11 +03:00
|
|
|
N_("git for-each-repo --config=<config> [--] <arguments>"),
|
2020-09-11 20:49:16 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2021-07-25 16:08:22 +03:00
|
|
|
static int run_command_on_repo(const char *path, int argc, const char ** argv)
|
2020-09-11 20:49:16 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct child_process child = CHILD_PROCESS_INIT;
|
2022-11-09 22:07:07 +03:00
|
|
|
char *abspath = interpolate_path(path, 0);
|
2020-09-11 20:49:16 +03:00
|
|
|
|
|
|
|
child.git_cmd = 1;
|
2022-11-09 22:07:07 +03:00
|
|
|
strvec_pushl(&child.args, "-C", abspath, NULL);
|
2020-09-11 20:49:16 +03:00
|
|
|
|
2021-07-25 16:08:22 +03:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
strvec_push(&child.args, argv[i]);
|
2020-09-11 20:49:16 +03:00
|
|
|
|
2022-11-09 22:07:07 +03:00
|
|
|
free(abspath);
|
|
|
|
|
2020-09-11 20:49:16 +03:00
|
|
|
return run_command(&child);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
|
|
|
static const char *config_key = NULL;
|
|
|
|
int i, result = 0;
|
|
|
|
const struct string_list *values;
|
2023-03-28 17:04:25 +03:00
|
|
|
int err;
|
2020-09-11 20:49:16 +03:00
|
|
|
|
|
|
|
const struct option options[] = {
|
|
|
|
OPT_STRING(0, "config", &config_key, N_("config"),
|
|
|
|
N_("config key storing a list of repository paths")),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
|
|
|
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
|
|
|
|
|
|
|
if (!config_key)
|
|
|
|
die(_("missing --config=<config>"));
|
|
|
|
|
for-each-repo: with bad config, don't conflate <path> and <cmd>
Fix a logic error in 4950b2a2b5c (for-each-repo: run subcommands on
configured repos, 2020-09-11). Due to assuming that elements returned
from the repo_config_get_value_multi() call wouldn't be "NULL" we'd
conflate the <path> and <command> part of the argument list when
running commands.
As noted in the preceding commit the fix is to move to a safer
"*_string_multi()" version of the *_multi() API. This change is
separated from the rest because those all segfaulted. In this change
we ended up with different behavior.
When using the "--config=<config>" form we take each element of the
list as a path to a repository. E.g. with a configuration like:
[repo] list = /some/repo
We would, with this command:
git for-each-repo --config=repo.list status builtin
Run a "git status" in /some/repo, as:
git -C /some/repo status builtin
I.e. ask "status" to report on the "builtin" directory. But since a
configuration such as this would result in a "struct string_list *"
with one element, whose "string" member is "NULL":
[repo] list
We would, when constructing our command-line in
"builtin/for-each-repo.c"...
strvec_pushl(&child.args, "-C", path, NULL);
for (i = 0; i < argc; i++)
strvec_push(&child.args, argv[i]);
...have that "path" be "NULL", and as strvec_pushl() stops when it
sees NULL we'd end with the first "argv" element as the argument to
the "-C" option, e.g.:
git -C status builtin
I.e. we'd run the command "builtin" in the "status" directory.
In another context this might be an interesting security
vulnerability, but I think that this amounts to a nothingburger on
that front.
A hypothetical attacker would need to be able to write config for the
victim to run, if they're able to do that there's more interesting
attack vectors. See the "safe.directory" facility added in
8d1a7448206 (setup.c: create `safe.bareRepository`, 2022-07-14).
An even more unlikely possibility would be an attacker able to
generate the config used for "for-each-repo --config=<key>", but
nothing else (e.g. an automated system producing that list).
Even in that case the attack vector is limited to the user running
commands whose name matches a directory that's interesting to the
attacker (e.g. a "log" directory in a repository). The second
argument (if any) of the command is likely to make git die without
doing anything interesting (e.g. "-p" to "log", there being no "-p"
built-in command to run).
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-03-28 17:04:28 +03:00
|
|
|
err = repo_config_get_string_multi(the_repository, config_key, &values);
|
2023-03-28 17:04:25 +03:00
|
|
|
if (err < 0)
|
|
|
|
usage_msg_optf(_("got bad config --config=%s"),
|
|
|
|
for_each_repo_usage, options, config_key);
|
|
|
|
else if (err)
|
2021-01-08 05:30:46 +03:00
|
|
|
return 0;
|
|
|
|
|
2020-09-11 20:49:16 +03:00
|
|
|
for (i = 0; !result && i < values->nr; i++)
|
2021-07-25 16:08:22 +03:00
|
|
|
result = run_command_on_repo(values->items[i].string, argc, argv);
|
2020-09-11 20:49:16 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|