2011-12-10 18:31:11 +08:00
|
|
|
#include "cache.h"
|
2017-06-15 02:07:36 +08:00
|
|
|
#include "config.h"
|
2011-12-10 18:31:11 +08:00
|
|
|
#include "credential.h"
|
|
|
|
#include "string-list.h"
|
|
|
|
#include "run-command.h"
|
2011-12-10 18:31:17 +08:00
|
|
|
#include "url.h"
|
2011-12-10 18:40:54 +08:00
|
|
|
#include "prompt.h"
|
2018-03-30 02:00:56 +08:00
|
|
|
#include "sigchain.h"
|
credential: allow wildcard patterns when matching config
In some cases, a user will want to use a specific credential helper for
a wildcard pattern, such as https://*.corp.example.com. We have code
that handles this already with the urlmatch code, so let's use that
instead of our custom code.
Since the urlmatch code is a superset of our current matching in terms
of capabilities, there shouldn't be any cases of things that matched
previously that don't match now. However, in addition to wildcard
matching, we now use partial path matching, which can cause slightly
different behavior in the case that a helper applies to the prefix
(considering path components) of the remote URL. While different, this
is probably the behavior people were wanting anyway.
Since we're using the urlmatch code, we need to encode the components
we've gotten into a URL to match, so add a function to percent-encode
data and format the URL with it. We now also no longer need to the
custom code to match URLs, so let's remove it.
Additionally, the urlmatch code always looks for the best match, whereas
we want all matches for credential helpers to preserve existing
behavior. Let's add an optional field, select_fn, that lets us control
which items we want (in this case, all of them) and default it to the
best-match code that already exists for other users.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:13 +08:00
|
|
|
#include "urlmatch.h"
|
2011-12-10 18:31:11 +08:00
|
|
|
|
|
|
|
void credential_init(struct credential *c)
|
|
|
|
{
|
|
|
|
memset(c, 0, sizeof(*c));
|
|
|
|
c->helpers.strdup_strings = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void credential_clear(struct credential *c)
|
|
|
|
{
|
|
|
|
free(c->protocol);
|
|
|
|
free(c->host);
|
|
|
|
free(c->path);
|
|
|
|
free(c->username);
|
|
|
|
free(c->password);
|
|
|
|
string_list_clear(&c->helpers, 0);
|
|
|
|
|
|
|
|
credential_init(c);
|
|
|
|
}
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
int credential_match(const struct credential *want,
|
|
|
|
const struct credential *have)
|
|
|
|
{
|
|
|
|
#define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
|
|
|
|
return CHECK(protocol) &&
|
|
|
|
CHECK(host) &&
|
|
|
|
CHECK(path) &&
|
|
|
|
CHECK(username);
|
|
|
|
#undef CHECK
|
|
|
|
}
|
|
|
|
|
|
|
|
static int credential_config_callback(const char *var, const char *value,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct credential *c = data;
|
credential: allow wildcard patterns when matching config
In some cases, a user will want to use a specific credential helper for
a wildcard pattern, such as https://*.corp.example.com. We have code
that handles this already with the urlmatch code, so let's use that
instead of our custom code.
Since the urlmatch code is a superset of our current matching in terms
of capabilities, there shouldn't be any cases of things that matched
previously that don't match now. However, in addition to wildcard
matching, we now use partial path matching, which can cause slightly
different behavior in the case that a helper applies to the prefix
(considering path components) of the remote URL. While different, this
is probably the behavior people were wanting anyway.
Since we're using the urlmatch code, we need to encode the components
we've gotten into a URL to match, so add a function to percent-encode
data and format the URL with it. We now also no longer need to the
custom code to match URLs, so let's remove it.
Additionally, the urlmatch code always looks for the best match, whereas
we want all matches for credential helpers to preserve existing
behavior. Let's add an optional field, select_fn, that lets us control
which items we want (in this case, all of them) and default it to the
best-match code that already exists for other users.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:13 +08:00
|
|
|
const char *key;
|
2011-12-10 18:31:24 +08:00
|
|
|
|
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-19 03:44:19 +08:00
|
|
|
if (!skip_prefix(var, "credential.", &key))
|
2011-12-10 18:31:24 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return config_error_nonbool(var);
|
|
|
|
|
2016-02-26 18:51:35 +08:00
|
|
|
if (!strcmp(key, "helper")) {
|
|
|
|
if (*value)
|
|
|
|
string_list_append(&c->helpers, value);
|
|
|
|
else
|
|
|
|
string_list_clear(&c->helpers, 0);
|
|
|
|
} else if (!strcmp(key, "username")) {
|
credential: use the last matching username in the config
Everywhere else in the codebase, we use the rule that the last matching
configuration option is the one that takes effect. This is helpful
because it allows more specific configuration settings (e.g., per-repo
configuration) to override less specific settings (e.g., per-user
configuration).
However, in the credential code, we didn't honor this setting, and
instead picked the first setting we had, and stuck with it. This was
likely to ensure we picked the value from the URL, which we want to
honor over the configuration.
It's possible to do both, though, so let's check if the value is the one
we've gotten over our protocol connection, which if present will have
come from the URL, and keep it if so. Otherwise, let's overwrite the
value with the latest version we've got from the configuration, so we
keep the last configuration value.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:12 +08:00
|
|
|
if (!c->username_from_proto) {
|
|
|
|
free(c->username);
|
2011-12-10 18:31:30 +08:00
|
|
|
c->username = xstrdup(value);
|
credential: use the last matching username in the config
Everywhere else in the codebase, we use the rule that the last matching
configuration option is the one that takes effect. This is helpful
because it allows more specific configuration settings (e.g., per-repo
configuration) to override less specific settings (e.g., per-user
configuration).
However, in the credential code, we didn't honor this setting, and
instead picked the first setting we had, and stuck with it. This was
likely to ensure we picked the value from the URL, which we want to
honor over the configuration.
It's possible to do both, though, so let's check if the value is the one
we've gotten over our protocol connection, which if present will have
come from the URL, and keep it if so. Otherwise, let's overwrite the
value with the latest version we've got from the configuration, so we
keep the last configuration value.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:12 +08:00
|
|
|
}
|
2011-12-10 18:31:30 +08:00
|
|
|
}
|
credential: make relevance of http path configurable
When parsing a URL into a credential struct, we carefully
record each part of the URL, including the path on the
remote host, and use the result as part of the credential
context.
This had two practical implications:
1. Credential helpers which store a credential for later
access are likely to use the "path" portion as part of
the storage key. That means that a request to
https://example.com/foo.git
would not use the same credential that was stored in an
earlier request for:
https://example.com/bar.git
2. The prompt shown to the user includes all relevant
context, including the path.
In most cases, however, users will have a single password
per host. The behavior in (1) will be inconvenient, and the
prompt in (2) will be overly long.
This patch introduces a config option to toggle the
relevance of http paths. When turned on, we use the path as
before. When turned off, we drop the path component from the
context: helpers don't see it, and it does not appear in the
prompt.
This is nothing you couldn't do with a clever credential
helper at the start of your stack, like:
[credential "http://"]
helper = "!f() { grep -v ^path= ; }; f"
helper = your_real_helper
But doing this:
[credential]
useHttpPath = false
is way easier and more readable. Furthermore, since most
users will want the "off" behavior, that is the new default.
Users who want it "on" can set the variable (either for all
credentials, or just for a subset using
credential.*.useHttpPath).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-12-10 18:31:34 +08:00
|
|
|
else if (!strcmp(key, "usehttppath"))
|
|
|
|
c->use_http_path = git_config_bool(var, value);
|
2011-12-10 18:31:24 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
credential: make relevance of http path configurable
When parsing a URL into a credential struct, we carefully
record each part of the URL, including the path on the
remote host, and use the result as part of the credential
context.
This had two practical implications:
1. Credential helpers which store a credential for later
access are likely to use the "path" portion as part of
the storage key. That means that a request to
https://example.com/foo.git
would not use the same credential that was stored in an
earlier request for:
https://example.com/bar.git
2. The prompt shown to the user includes all relevant
context, including the path.
In most cases, however, users will have a single password
per host. The behavior in (1) will be inconvenient, and the
prompt in (2) will be overly long.
This patch introduces a config option to toggle the
relevance of http paths. When turned on, we use the path as
before. When turned off, we drop the path component from the
context: helpers don't see it, and it does not appear in the
prompt.
This is nothing you couldn't do with a clever credential
helper at the start of your stack, like:
[credential "http://"]
helper = "!f() { grep -v ^path= ; }; f"
helper = your_real_helper
But doing this:
[credential]
useHttpPath = false
is way easier and more readable. Furthermore, since most
users will want the "off" behavior, that is the new default.
Users who want it "on" can set the variable (either for all
credentials, or just for a subset using
credential.*.useHttpPath).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-12-10 18:31:34 +08:00
|
|
|
static int proto_is_http(const char *s)
|
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
return !strcmp(s, "https") || !strcmp(s, "http");
|
|
|
|
}
|
|
|
|
|
credential: allow wildcard patterns when matching config
In some cases, a user will want to use a specific credential helper for
a wildcard pattern, such as https://*.corp.example.com. We have code
that handles this already with the urlmatch code, so let's use that
instead of our custom code.
Since the urlmatch code is a superset of our current matching in terms
of capabilities, there shouldn't be any cases of things that matched
previously that don't match now. However, in addition to wildcard
matching, we now use partial path matching, which can cause slightly
different behavior in the case that a helper applies to the prefix
(considering path components) of the remote URL. While different, this
is probably the behavior people were wanting anyway.
Since we're using the urlmatch code, we need to encode the components
we've gotten into a URL to match, so add a function to percent-encode
data and format the URL with it. We now also no longer need to the
custom code to match URLs, so let's remove it.
Additionally, the urlmatch code always looks for the best match, whereas
we want all matches for credential helpers to preserve existing
behavior. Let's add an optional field, select_fn, that lets us control
which items we want (in this case, all of them) and default it to the
best-match code that already exists for other users.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:13 +08:00
|
|
|
static void credential_describe(struct credential *c, struct strbuf *out);
|
|
|
|
static void credential_format(struct credential *c, struct strbuf *out);
|
|
|
|
|
|
|
|
static int select_all(const struct urlmatch_item *a,
|
|
|
|
const struct urlmatch_item *b)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
static void credential_apply_config(struct credential *c)
|
|
|
|
{
|
credential: allow wildcard patterns when matching config
In some cases, a user will want to use a specific credential helper for
a wildcard pattern, such as https://*.corp.example.com. We have code
that handles this already with the urlmatch code, so let's use that
instead of our custom code.
Since the urlmatch code is a superset of our current matching in terms
of capabilities, there shouldn't be any cases of things that matched
previously that don't match now. However, in addition to wildcard
matching, we now use partial path matching, which can cause slightly
different behavior in the case that a helper applies to the prefix
(considering path components) of the remote URL. While different, this
is probably the behavior people were wanting anyway.
Since we're using the urlmatch code, we need to encode the components
we've gotten into a URL to match, so add a function to percent-encode
data and format the URL with it. We now also no longer need to the
custom code to match URLs, so let's remove it.
Additionally, the urlmatch code always looks for the best match, whereas
we want all matches for credential helpers to preserve existing
behavior. Let's add an optional field, select_fn, that lets us control
which items we want (in this case, all of them) and default it to the
best-match code that already exists for other users.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:13 +08:00
|
|
|
char *normalized_url;
|
|
|
|
struct urlmatch_config config = { STRING_LIST_INIT_DUP };
|
|
|
|
struct strbuf url = STRBUF_INIT;
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
if (c->configured)
|
|
|
|
return;
|
credential: allow wildcard patterns when matching config
In some cases, a user will want to use a specific credential helper for
a wildcard pattern, such as https://*.corp.example.com. We have code
that handles this already with the urlmatch code, so let's use that
instead of our custom code.
Since the urlmatch code is a superset of our current matching in terms
of capabilities, there shouldn't be any cases of things that matched
previously that don't match now. However, in addition to wildcard
matching, we now use partial path matching, which can cause slightly
different behavior in the case that a helper applies to the prefix
(considering path components) of the remote URL. While different, this
is probably the behavior people were wanting anyway.
Since we're using the urlmatch code, we need to encode the components
we've gotten into a URL to match, so add a function to percent-encode
data and format the URL with it. We now also no longer need to the
custom code to match URLs, so let's remove it.
Additionally, the urlmatch code always looks for the best match, whereas
we want all matches for credential helpers to preserve existing
behavior. Let's add an optional field, select_fn, that lets us control
which items we want (in this case, all of them) and default it to the
best-match code that already exists for other users.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:13 +08:00
|
|
|
|
|
|
|
config.section = "credential";
|
|
|
|
config.key = NULL;
|
|
|
|
config.collect_fn = credential_config_callback;
|
|
|
|
config.cascade_fn = NULL;
|
|
|
|
config.select_fn = select_all;
|
|
|
|
config.cb = c;
|
|
|
|
|
|
|
|
credential_format(c, &url);
|
|
|
|
normalized_url = url_normalize(url.buf, &config.url);
|
|
|
|
|
|
|
|
git_config(urlmatch_config_entry, &config);
|
|
|
|
free(normalized_url);
|
|
|
|
strbuf_release(&url);
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
c->configured = 1;
|
credential: make relevance of http path configurable
When parsing a URL into a credential struct, we carefully
record each part of the URL, including the path on the
remote host, and use the result as part of the credential
context.
This had two practical implications:
1. Credential helpers which store a credential for later
access are likely to use the "path" portion as part of
the storage key. That means that a request to
https://example.com/foo.git
would not use the same credential that was stored in an
earlier request for:
https://example.com/bar.git
2. The prompt shown to the user includes all relevant
context, including the path.
In most cases, however, users will have a single password
per host. The behavior in (1) will be inconvenient, and the
prompt in (2) will be overly long.
This patch introduces a config option to toggle the
relevance of http paths. When turned on, we use the path as
before. When turned off, we drop the path component from the
context: helpers don't see it, and it does not appear in the
prompt.
This is nothing you couldn't do with a clever credential
helper at the start of your stack, like:
[credential "http://"]
helper = "!f() { grep -v ^path= ; }; f"
helper = your_real_helper
But doing this:
[credential]
useHttpPath = false
is way easier and more readable. Furthermore, since most
users will want the "off" behavior, that is the new default.
Users who want it "on" can set the variable (either for all
credentials, or just for a subset using
credential.*.useHttpPath).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-12-10 18:31:34 +08:00
|
|
|
|
|
|
|
if (!c->use_http_path && proto_is_http(c->protocol)) {
|
2017-06-16 07:15:46 +08:00
|
|
|
FREE_AND_NULL(c->path);
|
credential: make relevance of http path configurable
When parsing a URL into a credential struct, we carefully
record each part of the URL, including the path on the
remote host, and use the result as part of the credential
context.
This had two practical implications:
1. Credential helpers which store a credential for later
access are likely to use the "path" portion as part of
the storage key. That means that a request to
https://example.com/foo.git
would not use the same credential that was stored in an
earlier request for:
https://example.com/bar.git
2. The prompt shown to the user includes all relevant
context, including the path.
In most cases, however, users will have a single password
per host. The behavior in (1) will be inconvenient, and the
prompt in (2) will be overly long.
This patch introduces a config option to toggle the
relevance of http paths. When turned on, we use the path as
before. When turned off, we drop the path component from the
context: helpers don't see it, and it does not appear in the
prompt.
This is nothing you couldn't do with a clever credential
helper at the start of your stack, like:
[credential "http://"]
helper = "!f() { grep -v ^path= ; }; f"
helper = your_real_helper
But doing this:
[credential]
useHttpPath = false
is way easier and more readable. Furthermore, since most
users will want the "off" behavior, that is the new default.
Users who want it "on" can set the variable (either for all
credentials, or just for a subset using
credential.*.useHttpPath).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-12-10 18:31:34 +08:00
|
|
|
}
|
2011-12-10 18:31:24 +08:00
|
|
|
}
|
|
|
|
|
2011-12-10 18:31:11 +08:00
|
|
|
static void credential_describe(struct credential *c, struct strbuf *out)
|
|
|
|
{
|
|
|
|
if (!c->protocol)
|
|
|
|
return;
|
|
|
|
strbuf_addf(out, "%s://", c->protocol);
|
|
|
|
if (c->username && *c->username)
|
|
|
|
strbuf_addf(out, "%s@", c->username);
|
|
|
|
if (c->host)
|
|
|
|
strbuf_addstr(out, c->host);
|
|
|
|
if (c->path)
|
|
|
|
strbuf_addf(out, "/%s", c->path);
|
|
|
|
}
|
|
|
|
|
credential: allow wildcard patterns when matching config
In some cases, a user will want to use a specific credential helper for
a wildcard pattern, such as https://*.corp.example.com. We have code
that handles this already with the urlmatch code, so let's use that
instead of our custom code.
Since the urlmatch code is a superset of our current matching in terms
of capabilities, there shouldn't be any cases of things that matched
previously that don't match now. However, in addition to wildcard
matching, we now use partial path matching, which can cause slightly
different behavior in the case that a helper applies to the prefix
(considering path components) of the remote URL. While different, this
is probably the behavior people were wanting anyway.
Since we're using the urlmatch code, we need to encode the components
we've gotten into a URL to match, so add a function to percent-encode
data and format the URL with it. We now also no longer need to the
custom code to match URLs, so let's remove it.
Additionally, the urlmatch code always looks for the best match, whereas
we want all matches for credential helpers to preserve existing
behavior. Let's add an optional field, select_fn, that lets us control
which items we want (in this case, all of them) and default it to the
best-match code that already exists for other users.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:13 +08:00
|
|
|
static void credential_format(struct credential *c, struct strbuf *out)
|
|
|
|
{
|
|
|
|
if (!c->protocol)
|
|
|
|
return;
|
|
|
|
strbuf_addf(out, "%s://", c->protocol);
|
|
|
|
if (c->username && *c->username) {
|
|
|
|
strbuf_add_percentencode(out, c->username);
|
|
|
|
strbuf_addch(out, '@');
|
|
|
|
}
|
|
|
|
if (c->host)
|
|
|
|
strbuf_addstr(out, c->host);
|
|
|
|
if (c->path) {
|
|
|
|
strbuf_addch(out, '/');
|
|
|
|
strbuf_add_percentencode(out, c->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 18:41:23 +08:00
|
|
|
static char *credential_ask_one(const char *what, struct credential *c,
|
|
|
|
int flags)
|
2011-12-10 18:31:11 +08:00
|
|
|
{
|
|
|
|
struct strbuf desc = STRBUF_INIT;
|
|
|
|
struct strbuf prompt = STRBUF_INIT;
|
|
|
|
char *r;
|
|
|
|
|
|
|
|
credential_describe(c, &desc);
|
|
|
|
if (desc.len)
|
|
|
|
strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
|
|
|
|
else
|
|
|
|
strbuf_addf(&prompt, "%s: ", what);
|
|
|
|
|
2011-12-10 18:41:23 +08:00
|
|
|
r = git_prompt(prompt.buf, flags);
|
2011-12-10 18:31:11 +08:00
|
|
|
|
|
|
|
strbuf_release(&desc);
|
|
|
|
strbuf_release(&prompt);
|
|
|
|
return xstrdup(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void credential_getpass(struct credential *c)
|
|
|
|
{
|
|
|
|
if (!c->username)
|
2011-12-10 18:41:23 +08:00
|
|
|
c->username = credential_ask_one("Username", c,
|
|
|
|
PROMPT_ASKPASS|PROMPT_ECHO);
|
2011-12-10 18:31:11 +08:00
|
|
|
if (!c->password)
|
2011-12-10 18:41:23 +08:00
|
|
|
c->password = credential_ask_one("Password", c,
|
|
|
|
PROMPT_ASKPASS);
|
2011-12-10 18:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int credential_read(struct credential *c, FILE *fp)
|
|
|
|
{
|
|
|
|
struct strbuf line = STRBUF_INIT;
|
|
|
|
|
2016-01-14 07:31:17 +08:00
|
|
|
while (strbuf_getline_lf(&line, fp) != EOF) {
|
2011-12-10 18:31:11 +08:00
|
|
|
char *key = line.buf;
|
|
|
|
char *value = strchr(key, '=');
|
|
|
|
|
|
|
|
if (!line.len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
warning("invalid credential line: %s", key);
|
|
|
|
strbuf_release(&line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*value++ = '\0';
|
|
|
|
|
|
|
|
if (!strcmp(key, "username")) {
|
|
|
|
free(c->username);
|
|
|
|
c->username = xstrdup(value);
|
credential: use the last matching username in the config
Everywhere else in the codebase, we use the rule that the last matching
configuration option is the one that takes effect. This is helpful
because it allows more specific configuration settings (e.g., per-repo
configuration) to override less specific settings (e.g., per-user
configuration).
However, in the credential code, we didn't honor this setting, and
instead picked the first setting we had, and stuck with it. This was
likely to ensure we picked the value from the URL, which we want to
honor over the configuration.
It's possible to do both, though, so let's check if the value is the one
we've gotten over our protocol connection, which if present will have
come from the URL, and keep it if so. Otherwise, let's overwrite the
value with the latest version we've got from the configuration, so we
keep the last configuration value.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:12 +08:00
|
|
|
c->username_from_proto = 1;
|
2011-12-10 18:31:11 +08:00
|
|
|
} else if (!strcmp(key, "password")) {
|
|
|
|
free(c->password);
|
|
|
|
c->password = xstrdup(value);
|
|
|
|
} else if (!strcmp(key, "protocol")) {
|
|
|
|
free(c->protocol);
|
|
|
|
c->protocol = xstrdup(value);
|
|
|
|
} else if (!strcmp(key, "host")) {
|
|
|
|
free(c->host);
|
|
|
|
c->host = xstrdup(value);
|
|
|
|
} else if (!strcmp(key, "path")) {
|
|
|
|
free(c->path);
|
|
|
|
c->path = xstrdup(value);
|
2012-07-18 20:06:26 +08:00
|
|
|
} else if (!strcmp(key, "url")) {
|
|
|
|
credential_from_url(c, value);
|
credential: let helpers tell us to quit
When we are trying to fill a credential, we loop over the
set of defined credential-helpers, then fall back to running
askpass, and then finally prompt on the terminal. Helpers
which cannot find a credential are free to tell us nothing,
but they cannot currently ask us to stop prompting.
This patch lets them provide a "quit" attribute, which asks
us to stop the process entirely (avoiding running more
helpers, as well as the askpass/terminal prompt).
This has a few possible uses:
1. A helper which prompts the user itself (e.g., in a
dialog) can provide a "cancel" button to the user to
stop further prompts.
2. Some helpers may know that prompting cannot possibly
work. For example, if their role is to broker a ticket
from an external auth system and that auth system
cannot be contacted, there is no point in continuing
(we need a ticket to authenticate, and the user cannot
provide one by typing it in).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-12-04 11:46:48 +08:00
|
|
|
} else if (!strcmp(key, "quit")) {
|
|
|
|
c->quit = !!git_config_bool("quit", value);
|
2011-12-10 18:31:11 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Ignore other lines; we don't know what they mean, but
|
|
|
|
* this future-proofs us when later versions of git do
|
|
|
|
* learn new lines, and the helpers are updated to match.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&line);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void credential_write_item(FILE *fp, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
fprintf(fp, "%s=%s\n", key, value);
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:40:00 +08:00
|
|
|
void credential_write(const struct credential *c, FILE *fp)
|
2011-12-10 18:31:11 +08:00
|
|
|
{
|
|
|
|
credential_write_item(fp, "protocol", c->protocol);
|
|
|
|
credential_write_item(fp, "host", c->host);
|
|
|
|
credential_write_item(fp, "path", c->path);
|
|
|
|
credential_write_item(fp, "username", c->username);
|
|
|
|
credential_write_item(fp, "password", c->password);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_credential_helper(struct credential *c,
|
|
|
|
const char *cmd,
|
|
|
|
int want_output)
|
|
|
|
{
|
2014-08-20 03:09:35 +08:00
|
|
|
struct child_process helper = CHILD_PROCESS_INIT;
|
2011-12-10 18:31:11 +08:00
|
|
|
const char *argv[] = { NULL, NULL };
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
argv[0] = cmd;
|
|
|
|
helper.argv = argv;
|
|
|
|
helper.use_shell = 1;
|
|
|
|
helper.in = -1;
|
|
|
|
if (want_output)
|
|
|
|
helper.out = -1;
|
|
|
|
else
|
|
|
|
helper.no_stdout = 1;
|
|
|
|
|
|
|
|
if (start_command(&helper) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fp = xfdopen(helper.in, "w");
|
2018-03-30 02:00:56 +08:00
|
|
|
sigchain_push(SIGPIPE, SIG_IGN);
|
2011-12-10 18:31:11 +08:00
|
|
|
credential_write(c, fp);
|
|
|
|
fclose(fp);
|
2018-03-30 02:00:56 +08:00
|
|
|
sigchain_pop(SIGPIPE);
|
2011-12-10 18:31:11 +08:00
|
|
|
|
|
|
|
if (want_output) {
|
|
|
|
int r;
|
|
|
|
fp = xfdopen(helper.out, "r");
|
|
|
|
r = credential_read(c, fp);
|
|
|
|
fclose(fp);
|
|
|
|
if (r < 0) {
|
|
|
|
finish_command(&helper);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (finish_command(&helper))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int credential_do(struct credential *c, const char *helper,
|
|
|
|
const char *operation)
|
|
|
|
{
|
|
|
|
struct strbuf cmd = STRBUF_INIT;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (helper[0] == '!')
|
|
|
|
strbuf_addstr(&cmd, helper + 1);
|
|
|
|
else if (is_absolute_path(helper))
|
|
|
|
strbuf_addstr(&cmd, helper);
|
|
|
|
else
|
|
|
|
strbuf_addf(&cmd, "git credential-%s", helper);
|
|
|
|
|
|
|
|
strbuf_addf(&cmd, " %s", operation);
|
|
|
|
r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
|
|
|
|
|
|
|
|
strbuf_release(&cmd);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void credential_fill(struct credential *c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (c->username && c->password)
|
|
|
|
return;
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
credential_apply_config(c);
|
|
|
|
|
2011-12-10 18:31:11 +08:00
|
|
|
for (i = 0; i < c->helpers.nr; i++) {
|
|
|
|
credential_do(c, c->helpers.items[i].string, "get");
|
|
|
|
if (c->username && c->password)
|
|
|
|
return;
|
credential: let helpers tell us to quit
When we are trying to fill a credential, we loop over the
set of defined credential-helpers, then fall back to running
askpass, and then finally prompt on the terminal. Helpers
which cannot find a credential are free to tell us nothing,
but they cannot currently ask us to stop prompting.
This patch lets them provide a "quit" attribute, which asks
us to stop the process entirely (avoiding running more
helpers, as well as the askpass/terminal prompt).
This has a few possible uses:
1. A helper which prompts the user itself (e.g., in a
dialog) can provide a "cancel" button to the user to
stop further prompts.
2. Some helpers may know that prompting cannot possibly
work. For example, if their role is to broker a ticket
from an external auth system and that auth system
cannot be contacted, there is no point in continuing
(we need a ticket to authenticate, and the user cannot
provide one by typing it in).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-12-04 11:46:48 +08:00
|
|
|
if (c->quit)
|
|
|
|
die("credential helper '%s' told us to quit",
|
|
|
|
c->helpers.items[i].string);
|
2011-12-10 18:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
credential_getpass(c);
|
|
|
|
if (!c->username && !c->password)
|
|
|
|
die("unable to get password from user");
|
|
|
|
}
|
|
|
|
|
|
|
|
void credential_approve(struct credential *c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (c->approved)
|
|
|
|
return;
|
|
|
|
if (!c->username || !c->password)
|
|
|
|
return;
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
credential_apply_config(c);
|
|
|
|
|
2011-12-10 18:31:11 +08:00
|
|
|
for (i = 0; i < c->helpers.nr; i++)
|
|
|
|
credential_do(c, c->helpers.items[i].string, "store");
|
|
|
|
c->approved = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void credential_reject(struct credential *c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-12-10 18:31:24 +08:00
|
|
|
credential_apply_config(c);
|
|
|
|
|
2011-12-10 18:31:11 +08:00
|
|
|
for (i = 0; i < c->helpers.nr; i++)
|
|
|
|
credential_do(c, c->helpers.items[i].string, "erase");
|
|
|
|
|
2017-06-16 07:15:49 +08:00
|
|
|
FREE_AND_NULL(c->username);
|
|
|
|
FREE_AND_NULL(c->password);
|
2011-12-10 18:31:11 +08:00
|
|
|
c->approved = 0;
|
|
|
|
}
|
2011-12-10 18:31:17 +08:00
|
|
|
|
|
|
|
void credential_from_url(struct credential *c, const char *url)
|
|
|
|
{
|
|
|
|
const char *at, *colon, *cp, *slash, *host, *proto_end;
|
|
|
|
|
|
|
|
credential_clear(c);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match one of:
|
|
|
|
* (1) proto://<host>/...
|
|
|
|
* (2) proto://<user>@<host>/...
|
|
|
|
* (3) proto://<user>:<pass>@<host>/...
|
|
|
|
*/
|
|
|
|
proto_end = strstr(url, "://");
|
|
|
|
if (!proto_end)
|
|
|
|
return;
|
|
|
|
cp = proto_end + 3;
|
|
|
|
at = strchr(cp, '@');
|
|
|
|
colon = strchr(cp, ':');
|
|
|
|
slash = strchrnul(cp, '/');
|
|
|
|
|
|
|
|
if (!at || slash <= at) {
|
|
|
|
/* Case (1) */
|
|
|
|
host = cp;
|
|
|
|
}
|
|
|
|
else if (!colon || at <= colon) {
|
|
|
|
/* Case (2) */
|
|
|
|
c->username = url_decode_mem(cp, at - cp);
|
credential: use the last matching username in the config
Everywhere else in the codebase, we use the rule that the last matching
configuration option is the one that takes effect. This is helpful
because it allows more specific configuration settings (e.g., per-repo
configuration) to override less specific settings (e.g., per-user
configuration).
However, in the credential code, we didn't honor this setting, and
instead picked the first setting we had, and stuck with it. This was
likely to ensure we picked the value from the URL, which we want to
honor over the configuration.
It's possible to do both, though, so let's check if the value is the one
we've gotten over our protocol connection, which if present will have
come from the URL, and keep it if so. Otherwise, let's overwrite the
value with the latest version we've got from the configuration, so we
keep the last configuration value.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:12 +08:00
|
|
|
if (c->username && *c->username)
|
|
|
|
c->username_from_proto = 1;
|
2011-12-10 18:31:17 +08:00
|
|
|
host = at + 1;
|
|
|
|
} else {
|
|
|
|
/* Case (3) */
|
|
|
|
c->username = url_decode_mem(cp, colon - cp);
|
credential: use the last matching username in the config
Everywhere else in the codebase, we use the rule that the last matching
configuration option is the one that takes effect. This is helpful
because it allows more specific configuration settings (e.g., per-repo
configuration) to override less specific settings (e.g., per-user
configuration).
However, in the credential code, we didn't honor this setting, and
instead picked the first setting we had, and stuck with it. This was
likely to ensure we picked the value from the URL, which we want to
honor over the configuration.
It's possible to do both, though, so let's check if the value is the one
we've gotten over our protocol connection, which if present will have
come from the URL, and keep it if so. Otherwise, let's overwrite the
value with the latest version we've got from the configuration, so we
keep the last configuration value.
Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-20 10:24:12 +08:00
|
|
|
if (c->username && *c->username)
|
|
|
|
c->username_from_proto = 1;
|
2011-12-10 18:31:17 +08:00
|
|
|
c->password = url_decode_mem(colon + 1, at - (colon + 1));
|
|
|
|
host = at + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proto_end - url > 0)
|
|
|
|
c->protocol = xmemdupz(url, proto_end - url);
|
|
|
|
if (slash - host > 0)
|
|
|
|
c->host = url_decode_mem(host, slash - host);
|
|
|
|
/* Trim leading and trailing slashes from path */
|
|
|
|
while (*slash == '/')
|
|
|
|
slash++;
|
|
|
|
if (*slash) {
|
|
|
|
char *p;
|
|
|
|
c->path = url_decode(slash);
|
|
|
|
p = c->path + strlen(c->path) - 1;
|
|
|
|
while (p > c->path && *p == '/')
|
|
|
|
*p-- = '\0';
|
|
|
|
}
|
|
|
|
}
|