Skip to content

Commit ef97639

Browse files
Knut Frankegitster
authored andcommitted
http: allow selection of proxy authentication method
CURLAUTH_ANY does not work with proxies which answer unauthenticated requests with a 307 redirect to an error page instead of a 407 listing supported authentication methods. Therefore, allow the authentication method to be set using the environment variable GIT_HTTP_PROXY_AUTHMETHOD or configuration variables http.proxyAuthmethod and remote.<name>.proxyAuthmethod (in analogy to http.proxy and remote.<name>.proxy). The following values are supported: * anyauth (default) * basic * digest * negotiate * ntlm Signed-off-by: Knut Franke <[email protected]> Signed-off-by: Elia Pinto <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Eric Sunshine <[email protected]> Helped-by: Elia Pinto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7548842 commit ef97639

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

Documentation/config.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,27 @@ http.proxy::
16001600
`curl(1)`). This can be overridden on a per-remote basis; see
16011601
remote.<name>.proxy
16021602

1603+
http.proxyAuthMethod::
1604+
Set the method with which to authenticate against the HTTP proxy. This
1605+
only takes effect if the configured proxy string contains a user name part
1606+
(i.e. is of the form 'user@host' or 'user@host:port'). This can be
1607+
overridden on a per-remote basis; see `remote.<name>.proxyAuthMethod`.
1608+
Both can be overridden by the 'GIT_HTTP_PROXY_AUTHMETHOD' environment
1609+
variable. Possible values are:
1610+
+
1611+
--
1612+
* `anyauth` - Automatically pick a suitable authentication method. It is
1613+
assumed that the proxy answers an unauthenticated request with a 407
1614+
status code and one or more Proxy-authenticate headers with supported
1615+
authentication methods. This is the default.
1616+
* `basic` - HTTP Basic authentication
1617+
* `digest` - HTTP Digest authentication; this prevents the password from being
1618+
transmitted to the proxy in clear text
1619+
* `negotiate` - GSS-Negotiate authentication (compare the --negotiate option
1620+
of `curl(1)`)
1621+
* `ntlm` - NTLM authentication (compare the --ntlm option of `curl(1)`)
1622+
--
1623+
16031624
http.cookieFile::
16041625
File containing previously stored cookie lines which should be used
16051626
in the Git http session, if they match the server. The file format
@@ -2407,6 +2428,11 @@ remote.<name>.proxy::
24072428
the proxy to use for that remote. Set to the empty string to
24082429
disable proxying for that remote.
24092430

2431+
remote.<name>.proxyAuthMethod::
2432+
For remotes that require curl (http, https and ftp), the method to use for
2433+
authenticating against the proxy in use (probably set in
2434+
`remote.<name>.proxy`). See `http.proxyAuthMethod`.
2435+
24102436
remote.<name>.fetch::
24112437
The default set of "refspec" for linkgit:git-fetch[1]. See
24122438
linkgit:git-fetch[1].

Documentation/technical/api-remote.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ struct remote
5151

5252
The proxy to use for curl (http, https, ftp, etc.) URLs.
5353

54+
`http_proxy_authmethod`::
55+
56+
The method used for authenticating against `http_proxy`.
57+
5458
struct remotes can be found by name with remote_get(), and iterated
5559
through with for_each_remote(). remote_get(NULL) will return the
5660
default remote, given the current branch and configuration.

http.c

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ static long curl_low_speed_limit = -1;
6262
static long curl_low_speed_time = -1;
6363
static int curl_ftp_no_epsv;
6464
static const char *curl_http_proxy;
65+
static const char *http_proxy_authmethod;
66+
static struct {
67+
const char *name;
68+
long curlauth_param;
69+
} proxy_authmethods[] = {
70+
{ "basic", CURLAUTH_BASIC },
71+
{ "digest", CURLAUTH_DIGEST },
72+
{ "negotiate", CURLAUTH_GSSNEGOTIATE },
73+
{ "ntlm", CURLAUTH_NTLM },
74+
#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
75+
{ "anyauth", CURLAUTH_ANY },
76+
#endif
77+
/*
78+
* CURLAUTH_DIGEST_IE has no corresponding command-line option in
79+
* curl(1) and is not included in CURLAUTH_ANY, so we leave it out
80+
* here, too
81+
*/
82+
};
6583
static const char *curl_cookie_file;
6684
static int curl_save_cookies;
6785
struct credential http_auth = CREDENTIAL_INIT;
@@ -256,6 +274,9 @@ static int http_options(const char *var, const char *value, void *cb)
256274
if (!strcmp("http.proxy", var))
257275
return git_config_string(&curl_http_proxy, var, value);
258276

277+
if (!strcmp("http.proxyauthmethod", var))
278+
return git_config_string(&http_proxy_authmethod, var, value);
279+
259280
if (!strcmp("http.cookiefile", var))
260281
return git_config_string(&curl_cookie_file, var, value);
261282
if (!strcmp("http.savecookies", var)) {
@@ -304,6 +325,40 @@ static void init_curl_http_auth(CURL *result)
304325
#endif
305326
}
306327

328+
/* *var must be free-able */
329+
static void var_override(const char **var, char *value)
330+
{
331+
if (value) {
332+
free((void *)*var);
333+
*var = xstrdup(value);
334+
}
335+
}
336+
337+
static void init_curl_proxy_auth(CURL *result)
338+
{
339+
var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
340+
341+
#if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
342+
if (http_proxy_authmethod) {
343+
int i;
344+
for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
345+
if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
346+
curl_easy_setopt(result, CURLOPT_PROXYAUTH,
347+
proxy_authmethods[i].curlauth_param);
348+
break;
349+
}
350+
}
351+
if (i == ARRAY_SIZE(proxy_authmethods)) {
352+
warning("unsupported proxy authentication method %s: using anyauth",
353+
http_proxy_authmethod);
354+
curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
355+
}
356+
}
357+
else
358+
curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
359+
#endif
360+
}
361+
307362
static int has_cert_password(void)
308363
{
309364
if (ssl_cert == NULL || ssl_cert_password_required != 1)
@@ -476,9 +531,7 @@ static CURL *get_curl_handle(void)
476531
CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
477532
#endif
478533
}
479-
#if LIBCURL_VERSION_NUM >= 0x070a07
480-
curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
481-
#endif
534+
init_curl_proxy_auth(result);
482535

483536
set_curl_keepalive(result);
484537

@@ -519,6 +572,9 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
519572
if (remote && remote->http_proxy)
520573
curl_http_proxy = xstrdup(remote->http_proxy);
521574

575+
if (remote)
576+
var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
577+
522578
pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
523579
no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
524580

@@ -617,6 +673,9 @@ void http_cleanup(void)
617673
curl_http_proxy = NULL;
618674
}
619675

676+
free((void *)http_proxy_authmethod);
677+
http_proxy_authmethod = NULL;
678+
620679
if (cert_auth.password != NULL) {
621680
memset(cert_auth.password, 0, strlen(cert_auth.password));
622681
free(cert_auth.password);

remote.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ static int handle_config(const char *key, const char *value, void *cb)
428428
} else if (!strcmp(subkey, ".proxy")) {
429429
return git_config_string((const char **)&remote->http_proxy,
430430
key, value);
431+
} else if (!strcmp(subkey, ".proxyauthmethod")) {
432+
return git_config_string((const char **)&remote->http_proxy_authmethod,
433+
key, value);
431434
} else if (!strcmp(subkey, ".vcs")) {
432435
return git_config_string(&remote->foreign_vcs, key, value);
433436
}

remote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct remote {
5454
* for curl remotes only
5555
*/
5656
char *http_proxy;
57+
char *http_proxy_authmethod;
5758
};
5859

5960
struct remote *remote_get(const char *name);

0 commit comments

Comments
 (0)