2020-08-13 17:58:55 +03:00
|
|
|
#include "builtin.h"
|
2023-03-21 09:25:58 +03:00
|
|
|
#include "abspath.h"
|
2023-03-21 09:25:54 +03:00
|
|
|
#include "gettext.h"
|
2023-04-11 10:41:53 +03:00
|
|
|
#include "object-file.h"
|
2020-08-13 17:58:55 +03:00
|
|
|
#include "parse-options.h"
|
|
|
|
|
|
|
|
#ifndef NO_UNIX_SOCKETS
|
|
|
|
|
2017-06-14 21:07:36 +03:00
|
|
|
#include "config.h"
|
2015-08-10 12:47:51 +03:00
|
|
|
#include "tempfile.h"
|
2011-12-10 14:34:14 +04:00
|
|
|
#include "credential.h"
|
|
|
|
#include "unix-socket.h"
|
|
|
|
|
|
|
|
struct credential_cache_entry {
|
|
|
|
struct credential item;
|
2017-04-26 22:29:31 +03:00
|
|
|
timestamp_t expiration;
|
2011-12-10 14:34:14 +04: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;
|
|
|
|
}
|
|
|
|
|
2023-06-15 22:19:33 +03:00
|
|
|
static struct credential_cache_entry *lookup_credential(const struct credential *c)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < entries_nr; i++) {
|
|
|
|
struct credential *e = &entries[i].item;
|
2023-06-15 22:19:33 +03:00
|
|
|
if (credential_match(c, e, 0))
|
2011-12-10 14:34:14 +04:00
|
|
|
return &entries[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-06-15 22:19:32 +03:00
|
|
|
static void remove_credential(const struct credential *c, int match_password)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
|
|
|
struct credential_cache_entry *e;
|
|
|
|
|
2023-06-15 22:19:33 +03:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < entries_nr; i++) {
|
|
|
|
e = &entries[i];
|
|
|
|
if (credential_match(c, &e->item, match_password))
|
|
|
|
e->expiration = 0;
|
|
|
|
}
|
2011-12-10 14:34:14 +04:00
|
|
|
}
|
|
|
|
|
2017-04-26 22:29:31 +03:00
|
|
|
static timestamp_t check_expirations(void)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
2017-04-26 22:29:31 +03:00
|
|
|
static timestamp_t wait_for_entry_until;
|
2011-12-10 14:34:14 +04:00
|
|
|
int i = 0;
|
2017-04-26 22:29:31 +03:00
|
|
|
timestamp_t now = time(NULL);
|
|
|
|
timestamp_t next = TIME_MAX;
|
2011-12-10 14:34:14 +04: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,
|
2018-12-09 13:25:21 +03:00
|
|
|
struct strbuf *action, int *timeout)
|
|
|
|
{
|
2011-12-10 14:34:14 +04:00
|
|
|
static struct strbuf item = STRBUF_INIT;
|
|
|
|
const char *p;
|
|
|
|
|
2016-01-14 02:31:17 +03: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 23:44:19 +04:00
|
|
|
if (!skip_prefix(item.buf, "action=", &p))
|
2011-12-10 14:34:14 +04:00
|
|
|
return error("client sent bogus action line: %s", item.buf);
|
|
|
|
strbuf_addstr(action, p);
|
|
|
|
|
2016-01-14 02:31:17 +03: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 23:44:19 +04:00
|
|
|
if (!skip_prefix(item.buf, "timeout=", &p))
|
2011-12-10 14:34:14 +04: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")) {
|
2023-06-15 22:19:33 +03:00
|
|
|
struct credential_cache_entry *e = lookup_credential(&c);
|
2011-12-10 14:34:14 +04:00
|
|
|
if (e) {
|
|
|
|
fprintf(out, "username=%s\n", e->item.username);
|
|
|
|
fprintf(out, "password=%s\n", e->item.password);
|
credential: new attribute password_expiry_utc
Some passwords have an expiry date known at generation. This may be
years away for a personal access token or hours for an OAuth access
token.
When multiple credential helpers are configured, `credential fill` tries
each helper in turn until it has a username and password, returning
early. If Git authentication succeeds, `credential approve`
stores the successful credential in all helpers. If authentication
fails, `credential reject` erases matching credentials in all helpers.
Helpers implement corresponding operations: get, store, erase.
The credential protocol has no expiry attribute, so helpers cannot
store expiry information. Even if a helper returned an improvised
expiry attribute, git credential discards unrecognised attributes
between operations and between helpers.
This is a particular issue when a storage helper and a
credential-generating helper are configured together:
[credential]
helper = storage # eg. cache or osxkeychain
helper = generate # eg. oauth
`credential approve` stores the generated credential in both helpers
without expiry information. Later `credential fill` may return an
expired credential from storage. There is no workaround, no matter how
clever the second helper. The user sees authentication fail (a retry
will succeed).
Introduce a password expiry attribute. In `credential fill`, ignore
expired passwords and continue to query subsequent helpers.
In the example above, `credential fill` ignores the expired password
and a fresh credential is generated. If authentication succeeds,
`credential approve` replaces the expired password in storage.
If authentication fails, the expired credential is erased by
`credential reject`. It is unnecessary but harmless for storage
helpers to self prune expired credentials.
Add support for the new attribute to credential-cache.
Eventually, I hope to see support in other popular storage helpers.
Example usage in a credential-generating helper
https://github.com/hickford/git-credential-oauth/pull/16
Signed-off-by: M Hickford <mirth.hickford@gmail.com>
Reviewed-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-02-18 09:32:57 +03:00
|
|
|
if (e->item.password_expiry_utc != TIME_MAX)
|
|
|
|
fprintf(out, "password_expiry_utc=%"PRItime"\n",
|
|
|
|
e->item.password_expiry_utc);
|
2023-04-21 12:47:59 +03:00
|
|
|
if (e->item.oauth_refresh_token)
|
|
|
|
fprintf(out, "oauth_refresh_token=%s\n",
|
|
|
|
e->item.oauth_refresh_token);
|
2011-12-10 14:34:14 +04:00
|
|
|
}
|
|
|
|
}
|
2016-03-18 09:12:01 +03: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 14:34:14 +04:00
|
|
|
exit(0);
|
2016-03-18 09:12:01 +03:00
|
|
|
}
|
2011-12-10 14:34:14 +04:00
|
|
|
else if (!strcmp(action.buf, "erase"))
|
2023-06-15 22:19:32 +03:00
|
|
|
remove_credential(&c, 1);
|
2011-12-10 14:34:14 +04:00
|
|
|
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 {
|
2023-06-15 22:19:32 +03:00
|
|
|
remove_credential(&c, 0);
|
2011-12-10 14:34:14 +04:00
|
|
|
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 22:29:31 +03:00
|
|
|
timestamp_t wakeup;
|
2011-12-10 14:34:14 +04: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 12:47:41 +03:00
|
|
|
warning_errno("accept failed");
|
2011-12-10 14:34:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
client2 = dup(client);
|
|
|
|
if (client2 < 0) {
|
2016-05-08 12:47:41 +03:00
|
|
|
warning_errno("dup failed");
|
2011-12-10 14:34:14 +04: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 11:35:06 +04:00
|
|
|
static void serve_cache(const char *socket_path, int debug)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
2021-03-16 00:08:25 +03:00
|
|
|
struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
|
2011-12-10 14:34:14 +04:00
|
|
|
int fd;
|
|
|
|
|
2021-03-16 00:08:25 +03:00
|
|
|
fd = unix_stream_listen(socket_path, &opts);
|
2011-12-10 14:34:14 +04:00
|
|
|
if (fd < 0)
|
|
|
|
die_errno("unable to bind to '%s'", socket_path);
|
|
|
|
|
|
|
|
printf("ok\n");
|
|
|
|
fclose(stdout);
|
2014-09-14 11:35:06 +04:00
|
|
|
if (!debug) {
|
|
|
|
if (!freopen("/dev/null", "w", stderr))
|
|
|
|
die_errno("unable to point stderr to /dev/null");
|
|
|
|
}
|
2011-12-10 14:34:14 +04:00
|
|
|
|
|
|
|
while (serve_cache_loop(fd))
|
|
|
|
; /* nothing */
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:15:28 +03:00
|
|
|
static const char permissions_advice[] = N_(
|
2011-12-10 14:34:14 +04: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 16:15:28 +03:00
|
|
|
" chmod 0700 %s");
|
2016-02-23 10:15:15 +03:00
|
|
|
static void init_socket_directory(const char *path)
|
2011-12-10 14:34:14 +04: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 16:15:28 +03:00
|
|
|
die(_(permissions_advice), dir);
|
2016-02-23 10:15:15 +03: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 14:34:14 +04:00
|
|
|
}
|
|
|
|
|
2016-02-23 10:16:04 +03: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 14:34:14 +04:00
|
|
|
free(path_copy);
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:58:55 +03:00
|
|
|
int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
tempfile: auto-allocate tempfiles on heap
The previous commit taught the tempfile code to give up
ownership over tempfiles that have been renamed or deleted.
That makes it possible to use a stack variable like this:
struct tempfile t;
create_tempfile(&t, ...);
...
if (!err)
rename_tempfile(&t, ...);
else
delete_tempfile(&t);
But doing it this way has a high potential for creating
memory errors. The tempfile we pass to create_tempfile()
ends up on a global linked list, and it's not safe for it to
go out of scope until we've called one of those two
deactivation functions.
Imagine that we add an early return from the function that
forgets to call delete_tempfile(). With a static or heap
tempfile variable, the worst case is that the tempfile hangs
around until the program exits (and some functions like
setup_shallow_temporary rely on this intentionally, creating
a tempfile and then leaving it for later cleanup).
But with a stack variable as above, this is a serious memory
error: the variable goes out of scope and may be filled with
garbage by the time the tempfile code looks at it. Let's
see if we can make it harder to get this wrong.
Since many callers need to allocate arbitrary numbers of
tempfiles, we can't rely on static storage as a general
solution. So we need to turn to the heap. We could just ask
all callers to pass us a heap variable, but that puts the
burden on them to call free() at the right time.
Instead, let's have the tempfile code handle the heap
allocation _and_ the deallocation (when the tempfile is
deactivated and removed from the list).
This changes the return value of all of the creation
functions. For the cleanup functions (delete and rename),
we'll add one extra bit of safety: instead of taking a
tempfile pointer, we'll take a pointer-to-pointer and set it
to NULL after freeing the object. This makes it safe to
double-call functions like delete_tempfile(), as the second
call treats the NULL input as a noop. Several callsites
follow this pattern.
The resulting patch does have a fair bit of noise, as each
caller needs to be converted to handle:
1. Storing a pointer instead of the struct itself.
2. Passing the pointer instead of taking the struct
address.
3. Handling a "struct tempfile *" return instead of a file
descriptor.
We could play games to make this less noisy. For example, by
defining the tempfile like this:
struct tempfile {
struct heap_allocated_part_of_tempfile {
int fd;
...etc
} *actual_data;
}
Callers would continue to have a "struct tempfile", and it
would be "active" only when the inner pointer was non-NULL.
But that just makes things more awkward in the long run.
There aren't that many callers, so we can simply bite
the bullet and adjust all of them. And the compiler makes it
easy for us to find them all.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-09-05 15:15:08 +03:00
|
|
|
struct tempfile *socket_file;
|
2015-08-10 12:47:51 +03:00
|
|
|
const char *socket_path;
|
2015-11-10 03:26:29 +03:00
|
|
|
int ignore_sighup = 0;
|
2014-09-14 11:35:06 +04:00
|
|
|
static const char *usage[] = {
|
2022-10-13 18:39:17 +03:00
|
|
|
"git credential-cache--daemon [--debug] <socket-path>",
|
2014-09-14 11:35:06 +04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int debug = 0;
|
|
|
|
const struct option options[] = {
|
|
|
|
OPT_BOOL(0, "debug", &debug,
|
|
|
|
N_("print debugging messages to stderr")),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2015-11-10 03:26:29 +03:00
|
|
|
git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
|
|
|
|
|
2020-08-13 17:58:55 +03:00
|
|
|
argc = parse_options(argc, argv, prefix, options, usage, 0);
|
2014-09-14 11:35:06 +04:00
|
|
|
socket_path = argv[0];
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2024-04-03 18:42:02 +03:00
|
|
|
if (!have_unix_sockets())
|
|
|
|
die(_("credential-cache--daemon unavailable; no unix socket support"));
|
2011-12-10 14:34:14 +04:00
|
|
|
if (!socket_path)
|
2014-09-14 11:35:06 +04:00
|
|
|
usage_with_options(usage, options);
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2016-02-23 10:15:41 +03:00
|
|
|
if (!is_absolute_path(socket_path))
|
|
|
|
die("socket directory must be an absolute path");
|
|
|
|
|
2016-02-23 10:15:15 +03:00
|
|
|
init_socket_directory(socket_path);
|
tempfile: auto-allocate tempfiles on heap
The previous commit taught the tempfile code to give up
ownership over tempfiles that have been renamed or deleted.
That makes it possible to use a stack variable like this:
struct tempfile t;
create_tempfile(&t, ...);
...
if (!err)
rename_tempfile(&t, ...);
else
delete_tempfile(&t);
But doing it this way has a high potential for creating
memory errors. The tempfile we pass to create_tempfile()
ends up on a global linked list, and it's not safe for it to
go out of scope until we've called one of those two
deactivation functions.
Imagine that we add an early return from the function that
forgets to call delete_tempfile(). With a static or heap
tempfile variable, the worst case is that the tempfile hangs
around until the program exits (and some functions like
setup_shallow_temporary rely on this intentionally, creating
a tempfile and then leaving it for later cleanup).
But with a stack variable as above, this is a serious memory
error: the variable goes out of scope and may be filled with
garbage by the time the tempfile code looks at it. Let's
see if we can make it harder to get this wrong.
Since many callers need to allocate arbitrary numbers of
tempfiles, we can't rely on static storage as a general
solution. So we need to turn to the heap. We could just ask
all callers to pass us a heap variable, but that puts the
burden on them to call free() at the right time.
Instead, let's have the tempfile code handle the heap
allocation _and_ the deallocation (when the tempfile is
deactivated and removed from the list).
This changes the return value of all of the creation
functions. For the cleanup functions (delete and rename),
we'll add one extra bit of safety: instead of taking a
tempfile pointer, we'll take a pointer-to-pointer and set it
to NULL after freeing the object. This makes it safe to
double-call functions like delete_tempfile(), as the second
call treats the NULL input as a noop. Several callsites
follow this pattern.
The resulting patch does have a fair bit of noise, as each
caller needs to be converted to handle:
1. Storing a pointer instead of the struct itself.
2. Passing the pointer instead of taking the struct
address.
3. Handling a "struct tempfile *" return instead of a file
descriptor.
We could play games to make this less noisy. For example, by
defining the tempfile like this:
struct tempfile {
struct heap_allocated_part_of_tempfile {
int fd;
...etc
} *actual_data;
}
Callers would continue to have a "struct tempfile", and it
would be "active" only when the inner pointer was non-NULL.
But that just makes things more awkward in the long run.
There aren't that many callers, so we can simply bite
the bullet and adjust all of them. And the compiler makes it
easy for us to find them all.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-09-05 15:15:08 +03:00
|
|
|
socket_file = register_tempfile(socket_path);
|
2015-11-10 03:26:29 +03:00
|
|
|
|
|
|
|
if (ignore_sighup)
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
|
2014-09-14 11:35:06 +04:00
|
|
|
serve_cache(socket_path, debug);
|
2015-08-10 12:47:51 +03:00
|
|
|
delete_tempfile(&socket_file);
|
2015-08-10 12:47:50 +03:00
|
|
|
|
2011-12-10 14:34:14 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-13 17:58:55 +03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
|
|
|
const char * const usage[] = {
|
2022-10-13 18:39:17 +03:00
|
|
|
"git credential-cache--daemon [--debug] <socket-path>",
|
2020-08-13 17:58:55 +03:00
|
|
|
"",
|
|
|
|
"credential-cache--daemon is disabled in this build of Git",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
struct option options[] = { OPT_END() };
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, prefix, options, usage, 0);
|
|
|
|
die(_("credential-cache--daemon unavailable; no unix socket support"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* NO_UNIX_SOCKET */
|