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 <k.franke@science-computing.de> Signed-off-by: Elia Pinto <gitter.spiros@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Helped-by: Elia Pinto <gitter.spiros@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Knut Franke committed Jan 26, 2016 at 13:02 UTC ef976395e26a25fb6d048d859a1c8cddb2640b9a
5 files changed +96 -3
Documentation/config.txt
+26
@@ -1600,6 +1600,27 @@ http.proxy::
1600 `curl(1)`). This can be overridden on a per-remote basis; see
1601 remote.<name>.proxy
1602
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 +
1624 http.cookieFile::
1625 File containing previously stored cookie lines which should be used
1626 in the Git http session, if they match the server. The file format
@@ -2407,6 +2428,11 @@ remote.<name>.proxy::
2428 the proxy to use for that remote. Set to the empty string to
2429 disable proxying for that remote.
2430
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 +
2436 remote.<name>.fetch::
2437 The default set of "refspec" for linkgit:git-fetch[1]. See
2438 linkgit:git-fetch[1].
Documentation/technical/api-remote.txt
+4
@@ -51,6 +51,10 @@ struct remote
51
52 The proxy to use for curl (http, https, ftp, etc.) URLs.
53
54 +`http_proxy_authmethod`::
55 +
56 + The method used for authenticating against `http_proxy`.
57 +
58 struct remotes can be found by name with remote_get(), and iterated
59 through with for_each_remote(). remote_get(NULL) will return the
60 default remote, given the current branch and configuration.
http.c
+62 -3
@@ -62,6 +62,24 @@ static long curl_low_speed_limit = -1;
62 static long curl_low_speed_time = -1;
63 static int curl_ftp_no_epsv;
64 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 +};
83 static const char *curl_cookie_file;
84 static int curl_save_cookies;
85 struct credential http_auth = CREDENTIAL_INIT;
@@ -256,6 +274,9 @@ static int http_options(const char *var, const char *value, void *cb)
274 if (!strcmp("http.proxy", var))
275 return git_config_string(&curl_http_proxy, var, value);
276
277 + if (!strcmp("http.proxyauthmethod", var))
278 + return git_config_string(&http_proxy_authmethod, var, value);
279 +
280 if (!strcmp("http.cookiefile", var))
281 return git_config_string(&curl_cookie_file, var, value);
282 if (!strcmp("http.savecookies", var)) {
@@ -304,6 +325,40 @@ static void init_curl_http_auth(CURL *result)
325 #endif
326 }
327
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 +
362 static int has_cert_password(void)
363 {
364 if (ssl_cert == NULL || ssl_cert_password_required != 1)
@@ -476,9 +531,7 @@ static CURL *get_curl_handle(void)
531 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
532 #endif
533 }
479 -#if LIBCURL_VERSION_NUM >= 0x070a07
480 - curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
481 -#endif
534 + init_curl_proxy_auth(result);
535
536 set_curl_keepalive(result);
537
@@ -519,6 +572,9 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
572 if (remote && remote->http_proxy)
573 curl_http_proxy = xstrdup(remote->http_proxy);
574
575 + if (remote)
576 + var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
577 +
578 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
579 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
580
@@ -617,6 +673,9 @@ void http_cleanup(void)
673 curl_http_proxy = NULL;
674 }
675
676 + free((void *)http_proxy_authmethod);
677 + http_proxy_authmethod = NULL;
678 +
679 if (cert_auth.password != NULL) {
680 memset(cert_auth.password, 0, strlen(cert_auth.password));
681 free(cert_auth.password);
remote.c
+3
@@ -428,6 +428,9 @@ static int handle_config(const char *key, const char *value, void *cb)
428 } else if (!strcmp(subkey, ".proxy")) {
429 return git_config_string((const char **)&remote->http_proxy,
430 key, value);
431 + } else if (!strcmp(subkey, ".proxyauthmethod")) {
432 + return git_config_string((const char **)&remote->http_proxy_authmethod,
433 + key, value);
434 } else if (!strcmp(subkey, ".vcs")) {
435 return git_config_string(&remote->foreign_vcs, key, value);
436 }
remote.h
+1
@@ -54,6 +54,7 @@ struct remote {
54 * for curl remotes only
55 */
56 char *http_proxy;
57 + char *http_proxy_authmethod;
58 };
59
60 struct remote *remote_get(const char *name);