2011-12-10 11:34:14 +01:00
|
|
|
#include "cache.h"
|
2015-08-10 11:47:51 +02:00
|
|
|
#include "tempfile.h"
|
2011-12-10 11:34:14 +01:00
|
|
|
#include "credential.h"
|
|
|
|
#include "unix-socket.h"
|
2014-09-14 09:35:06 +02:00
|
|
|
#include "parse-options.h"
|
2011-12-10 11:34:14 +01:00
|
|
|
|
2015-08-10 11:47:51 +02:00
|
|
|
static struct tempfile socket_file;
|
2011-12-10 11:34:14 +01:00
|
|
|
|
|
|
|
struct credential_cache_entry {
|
|
|
|
struct credential item;
|
2017-04-26 21:29:31 +02:00
|
|
|
timestamp_t expiration;
|
2011-12-10 11:34:14 +01:00
|
|
|
};
|
|
|
|
static struct credential_cache_entry *entries;
|
|
|
|
static int entries_nr;
|
|
|
|
static int entries_alloc;
|
|
|
|
|
|
|
|
static void cache_credential(struct credential *c, int timeout)
|
|
|
|
{
|
|
|
|
struct credential_cache_entry *e;
|
|
|
|
|
|
|
|
ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
|
|
|
|
e = &entries[entries_nr++];
|
|
|
|
|
|
|
|
/* take ownership of pointers */
|
|
|
|
memcpy(&e->item, c, sizeof(*c));
|
|
|
|
memset(c, 0, sizeof(*c));
|
|
|
|
e->expiration = time(NULL) + timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct credential_cache_entry *lookup_credential(const struct credential *c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < entries_nr; i++) {
|
|
|
|
struct credential *e = &entries[i].item;
|
|
|
|
if (credential_match(c, e))
|
|
|
|
return &entries[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_credential(const struct credential *c)
|
|
|
|
{
|
|
|
|
struct credential_cache_entry *e;
|
|
|
|
|
|
|
|
e = lookup_credential(c);
|
|
|
|
if (e)
|
|
|
|
e->expiration = 0;
|
|
|
|
}
|
|
|
|
|
2017-04-26 21:29:31 +02:00
|
|
|
static timestamp_t check_expirations(void)
|
2011-12-10 11:34:14 +01:00
|
|
|
{
|
2017-04-26 21:29:31 +02:00
|
|
|
static timestamp_t wait_for_entry_until;
|
2011-12-10 11:34:14 +01:00
|
|
|
int i = 0;
|
2017-04-26 21:29:31 +02:00
|
|
|
timestamp_t now = time(NULL);
|
|
|
|
timestamp_t next = TIME_MAX;
|
2011-12-10 11:34:14 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initially give the client 30 seconds to actually contact us
|
|
|
|
* and store a credential before we decide there's no point in
|
|
|
|
* keeping the daemon around.
|
|
|
|
*/
|
|
|
|
if (!wait_for_entry_until)
|
|
|
|
wait_for_entry_until = now + 30;
|
|
|
|
|
|
|
|
while (i < entries_nr) {
|
|
|
|
if (entries[i].expiration <= now) {
|
|
|
|
entries_nr--;
|
|
|
|
credential_clear(&entries[i].item);
|
|
|
|
if (i != entries_nr)
|
|
|
|
memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
|
|
|
|
/*
|
|
|
|
* Stick around 30 seconds in case a new credential
|
|
|
|
* shows up (e.g., because we just removed a failed
|
|
|
|
* one, and we will soon get the correct one).
|
|
|
|
*/
|
|
|
|
wait_for_entry_until = now + 30;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (entries[i].expiration < next)
|
|
|
|
next = entries[i].expiration;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entries_nr) {
|
|
|
|
if (wait_for_entry_until <= now)
|
|
|
|
return 0;
|
|
|
|
next = wait_for_entry_until;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next - now;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_request(FILE *fh, struct credential *c,
|
|
|
|
struct strbuf *action, int *timeout) {
|
|
|
|
static struct strbuf item = STRBUF_INIT;
|
|
|
|
const char *p;
|
|
|
|
|
2016-01-14 00:31:17 +01:00
|
|
|
strbuf_getline_lf(&item, fh);
|
refactor skip_prefix to return a boolean
The skip_prefix() function returns a pointer to the content
past the prefix, or NULL if the prefix was not found. While
this is nice and simple, in practice it makes it hard to use
for two reasons:
1. When you want to conditionally skip or keep the string
as-is, you have to introduce a temporary variable.
For example:
tmp = skip_prefix(buf, "foo");
if (tmp)
buf = tmp;
2. It is verbose to check the outcome in a conditional, as
you need extra parentheses to silence compiler
warnings. For example:
if ((cp = skip_prefix(buf, "foo"))
/* do something with cp */
Both of these make it harder to use for long if-chains, and
we tend to use starts_with() instead. However, the first line
of "do something" is often to then skip forward in buf past
the prefix, either using a magic constant or with an extra
strlen(3) (which is generally computed at compile time, but
means we are repeating ourselves).
This patch refactors skip_prefix() to return a simple boolean,
and to provide the pointer value as an out-parameter. If the
prefix is not found, the out-parameter is untouched. This
lets you write:
if (skip_prefix(arg, "foo ", &arg))
do_foo(arg);
else if (skip_prefix(arg, "bar ", &arg))
do_bar(arg);
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-06-18 21:44:19 +02:00
|
|
|
if (!skip_prefix(item.buf, "action=", &p))
|
2011-12-10 11:34:14 +01:00
|
|
|
return error("client sent bogus action line: %s", item.buf);
|
|
|
|
strbuf_addstr(action, p);
|
|
|
|
|
2016-01-14 00:31:17 +01:00
|
|
|
strbuf_getline_lf(&item, fh);
|
refactor skip_prefix to return a boolean
The skip_prefix() function returns a pointer to the content
past the prefix, or NULL if the prefix was not found. While
this is nice and simple, in practice it makes it hard to use
for two reasons:
1. When you want to conditionally skip or keep the string
as-is, you have to introduce a temporary variable.
For example:
tmp = skip_prefix(buf, "foo");
if (tmp)
buf = tmp;
2. It is verbose to check the outcome in a conditional, as
you need extra parentheses to silence compiler
warnings. For example:
if ((cp = skip_prefix(buf, "foo"))
/* do something with cp */
Both of these make it harder to use for long if-chains, and
we tend to use starts_with() instead. However, the first line
of "do something" is often to then skip forward in buf past
the prefix, either using a magic constant or with an extra
strlen(3) (which is generally computed at compile time, but
means we are repeating ourselves).
This patch refactors skip_prefix() to return a simple boolean,
and to provide the pointer value as an out-parameter. If the
prefix is not found, the out-parameter is untouched. This
lets you write:
if (skip_prefix(arg, "foo ", &arg))
do_foo(arg);
else if (skip_prefix(arg, "bar ", &arg))
do_bar(arg);
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-06-18 21:44:19 +02:00
|
|
|
if (!skip_prefix(item.buf, "timeout=", &p))
|
2011-12-10 11:34:14 +01:00
|
|
|
return error("client sent bogus timeout line: %s", item.buf);
|
|
|
|
*timeout = atoi(p);
|
|
|
|
|
|
|
|
if (credential_read(c, fh) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serve_one_client(FILE *in, FILE *out)
|
|
|
|
{
|
|
|
|
struct credential c = CREDENTIAL_INIT;
|
|
|
|
struct strbuf action = STRBUF_INIT;
|
|
|
|
int timeout = -1;
|
|
|
|
|
|
|
|
if (read_request(in, &c, &action, &timeout) < 0)
|
|
|
|
/* ignore error */ ;
|
|
|
|
else if (!strcmp(action.buf, "get")) {
|
|
|
|
struct credential_cache_entry *e = lookup_credential(&c);
|
|
|
|
if (e) {
|
|
|
|
fprintf(out, "username=%s\n", e->item.username);
|
|
|
|
fprintf(out, "password=%s\n", e->item.password);
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 07:12:01 +01:00
|
|
|
else if (!strcmp(action.buf, "exit")) {
|
|
|
|
/*
|
|
|
|
* It's important that we clean up our socket first, and then
|
|
|
|
* signal the client only once we have finished the cleanup.
|
|
|
|
* Calling exit() directly does this, because we clean up in
|
|
|
|
* our atexit() handler, and then signal the client when our
|
|
|
|
* process actually ends, which closes the socket and gives
|
|
|
|
* them EOF.
|
|
|
|
*/
|
2011-12-10 11:34:14 +01:00
|
|
|
exit(0);
|
2016-03-18 07:12:01 +01:00
|
|
|
}
|
2011-12-10 11:34:14 +01:00
|
|
|
else if (!strcmp(action.buf, "erase"))
|
|
|
|
remove_credential(&c);
|
|
|
|
else if (!strcmp(action.buf, "store")) {
|
|
|
|
if (timeout < 0)
|
|
|
|
warning("cache client didn't specify a timeout");
|
|
|
|
else if (!c.username || !c.password)
|
|
|
|
warning("cache client gave us a partial credential");
|
|
|
|
else {
|
|
|
|
remove_credential(&c);
|
|
|
|
cache_credential(&c, timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warning("cache client sent unknown action: %s", action.buf);
|
|
|
|
|
|
|
|
credential_clear(&c);
|
|
|
|
strbuf_release(&action);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int serve_cache_loop(int fd)
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
2017-04-26 21:29:31 +02:00
|
|
|
timestamp_t wakeup;
|
2011-12-10 11:34:14 +01:00
|
|
|
|
|
|
|
wakeup = check_expirations();
|
|
|
|
if (!wakeup)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
if (poll(&pfd, 1, 1000 * wakeup) < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
die_errno("poll failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pfd.revents & POLLIN) {
|
|
|
|
int client, client2;
|
|
|
|
FILE *in, *out;
|
|
|
|
|
|
|
|
client = accept(fd, NULL, NULL);
|
|
|
|
if (client < 0) {
|
2016-05-08 11:47:41 +02:00
|
|
|
warning_errno("accept failed");
|
2011-12-10 11:34:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
client2 = dup(client);
|
|
|
|
if (client2 < 0) {
|
2016-05-08 11:47:41 +02:00
|
|
|
warning_errno("dup failed");
|
2011-12-10 11:34:14 +01:00
|
|
|
close(client);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
in = xfdopen(client, "r");
|
|
|
|
out = xfdopen(client2, "w");
|
|
|
|
serve_one_client(in, out);
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-14 09:35:06 +02:00
|
|
|
static void serve_cache(const char *socket_path, int debug)
|
2011-12-10 11:34:14 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = unix_stream_listen(socket_path);
|
|
|
|
if (fd < 0)
|
|
|
|
die_errno("unable to bind to '%s'", socket_path);
|
|
|
|
|
|
|
|
printf("ok\n");
|
|
|
|
fclose(stdout);
|
2014-09-14 09:35:06 +02:00
|
|
|
if (!debug) {
|
|
|
|
if (!freopen("/dev/null", "w", stderr))
|
|
|
|
die_errno("unable to point stderr to /dev/null");
|
|
|
|
}
|
2011-12-10 11:34:14 +01:00
|
|
|
|
|
|
|
while (serve_cache_loop(fd))
|
|
|
|
; /* nothing */
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2016-10-17 15:15:28 +02:00
|
|
|
static const char permissions_advice[] = N_(
|
2011-12-10 11:34:14 +01:00
|
|
|
"The permissions on your socket directory are too loose; other\n"
|
|
|
|
"users may be able to read your cached credentials. Consider running:\n"
|
|
|
|
"\n"
|
2016-10-17 15:15:28 +02:00
|
|
|
" chmod 0700 %s");
|
2016-02-23 08:15:15 +01:00
|
|
|
static void init_socket_directory(const char *path)
|
2011-12-10 11:34:14 +01:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *path_copy = xstrdup(path);
|
|
|
|
char *dir = dirname(path_copy);
|
|
|
|
|
|
|
|
if (!stat(dir, &st)) {
|
|
|
|
if (st.st_mode & 077)
|
2016-10-17 15:15:28 +02:00
|
|
|
die(_(permissions_advice), dir);
|
2016-02-23 08:15:15 +01:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We must be sure to create the directory with the correct mode,
|
|
|
|
* not just chmod it after the fact; otherwise, there is a race
|
|
|
|
* condition in which somebody can chdir to it, sleep, then try to open
|
|
|
|
* our protected socket.
|
|
|
|
*/
|
|
|
|
if (safe_create_leading_directories_const(dir) < 0)
|
|
|
|
die_errno("unable to create directories for '%s'", dir);
|
|
|
|
if (mkdir(dir, 0700) < 0)
|
|
|
|
die_errno("unable to mkdir '%s'", dir);
|
2011-12-10 11:34:14 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 08:16:04 +01:00
|
|
|
if (chdir(dir))
|
|
|
|
/*
|
|
|
|
* We don't actually care what our cwd is; we chdir here just to
|
|
|
|
* be a friendly daemon and avoid tying up our original cwd.
|
|
|
|
* If this fails, it's OK to just continue without that benefit.
|
|
|
|
*/
|
|
|
|
;
|
|
|
|
|
2011-12-10 11:34:14 +01:00
|
|
|
free(path_copy);
|
|
|
|
}
|
|
|
|
|
add an extra level of indirection to main()
There are certain startup tasks that we expect every git
process to do. In some cases this is just to improve the
quality of the program (e.g., setting up gettext()). In
others it is a requirement for using certain functions in
libgit.a (e.g., system_path() expects that you have called
git_extract_argv0_path()).
Most commands are builtins and are covered by the git.c
version of main(). However, there are still a few external
commands that use their own main(). Each of these has to
remember to include the correct startup sequence, and we are
not always consistent.
Rather than just fix the inconsistencies, let's make this
harder to get wrong by providing a common main() that can
run this standard startup.
We basically have two options to do this:
- the compat/mingw.h file already does something like this by
adding a #define that replaces the definition of main with a
wrapper that calls mingw_startup().
The upside is that the code in each program doesn't need
to be changed at all; it's rewritten on the fly by the
preprocessor.
The downside is that it may make debugging of the startup
sequence a bit more confusing, as the preprocessor is
quietly inserting new code.
- the builtin functions are all of the form cmd_foo(),
and git.c's main() calls them.
This is much more explicit, which may make things more
obvious to somebody reading the code. It's also more
flexible (because of course we have to figure out _which_
cmd_foo() to call).
The downside is that each of the builtins must define
cmd_foo(), instead of just main().
This patch chooses the latter option, preferring the more
explicit approach, even though it is more invasive. We
introduce a new file common-main.c, with the "real" main. It
expects to call cmd_main() from whatever other objects it is
linked against.
We link common-main.o against anything that links against
libgit.a, since we know that such programs will need to do
this setup. Note that common-main.o can't actually go inside
libgit.a, as the linker would not pick up its main()
function automatically (it has no callers).
The rest of the patch is just adjusting all of the various
external programs (mostly in t/helper) to use cmd_main().
I've provided a global declaration for cmd_main(), which
means that all of the programs also need to match its
signature. In particular, many functions need to switch to
"const char **" instead of "char **" for argv. This effect
ripples out to a few other variables and functions, as well.
This makes the patch even more invasive, but the end result
is much better. We should be treating argv strings as const
anyway, and now all programs conform to the same signature
(which also matches the way builtins are defined).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-07-01 07:58:58 +02:00
|
|
|
int cmd_main(int argc, const char **argv)
|
2011-12-10 11:34:14 +01:00
|
|
|
{
|
2015-08-10 11:47:51 +02:00
|
|
|
const char *socket_path;
|
2015-11-10 01:26:29 +01:00
|
|
|
int ignore_sighup = 0;
|
2014-09-14 09:35:06 +02:00
|
|
|
static const char *usage[] = {
|
|
|
|
"git-credential-cache--daemon [opts] <socket_path>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int debug = 0;
|
|
|
|
const struct option options[] = {
|
|
|
|
OPT_BOOL(0, "debug", &debug,
|
|
|
|
N_("print debugging messages to stderr")),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2015-11-10 01:26:29 +01:00
|
|
|
git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
|
|
|
|
|
2014-09-14 09:35:06 +02:00
|
|
|
argc = parse_options(argc, argv, NULL, options, usage, 0);
|
|
|
|
socket_path = argv[0];
|
2011-12-10 11:34:14 +01:00
|
|
|
|
|
|
|
if (!socket_path)
|
2014-09-14 09:35:06 +02:00
|
|
|
usage_with_options(usage, options);
|
2011-12-10 11:34:14 +01:00
|
|
|
|
2016-02-23 08:15:41 +01:00
|
|
|
if (!is_absolute_path(socket_path))
|
|
|
|
die("socket directory must be an absolute path");
|
|
|
|
|
2016-02-23 08:15:15 +01:00
|
|
|
init_socket_directory(socket_path);
|
2015-08-10 11:47:51 +02:00
|
|
|
register_tempfile(&socket_file, socket_path);
|
2015-11-10 01:26:29 +01:00
|
|
|
|
|
|
|
if (ignore_sighup)
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
|
2014-09-14 09:35:06 +02:00
|
|
|
serve_cache(socket_path, debug);
|
2015-08-10 11:47:51 +02:00
|
|
|
delete_tempfile(&socket_file);
|
2015-08-10 11:47:50 +02:00
|
|
|
|
2011-12-10 11:34:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|