Skip to content

Commit c33976c

Browse files
committed
http authentication via prompts
Curl is designed not to ask for password when only username is given in the URL, but has a way for application to feed a (username, password) pair to it. With this patch, you do not have to keep your password in plaintext in your $HOME/.netrc file when talking with a password protected URL with http://<username>@<host>/path/to/repository.git/ syntax. The code handles only the http-walker side, not the push side. At least, not yet. But interested parties can add support for it. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7059cd9 commit c33976c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

http.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static long curl_low_speed_limit = -1;
2525
static long curl_low_speed_time = -1;
2626
static int curl_ftp_no_epsv;
2727
static const char *curl_http_proxy;
28+
static char *user_name, *user_pass;
2829

2930
static struct curl_slist *pragma_header;
3031

@@ -135,6 +136,20 @@ static int http_options(const char *var, const char *value, void *cb)
135136
return git_default_config(var, value, cb);
136137
}
137138

139+
static void init_curl_http_auth(CURL *result)
140+
{
141+
if (!user_name)
142+
curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
143+
else {
144+
struct strbuf up = STRBUF_INIT;
145+
if (!user_pass)
146+
user_pass = xstrdup(getpass("Password: "));
147+
strbuf_addf(&up, "%s:%s", user_name, user_pass);
148+
curl_easy_setopt(result, CURLOPT_USERPWD,
149+
strbuf_detach(&up, NULL));
150+
}
151+
}
152+
138153
static CURL *get_curl_handle(void)
139154
{
140155
CURL *result = curl_easy_init();
@@ -153,6 +168,8 @@ static CURL *get_curl_handle(void)
153168
curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
154169
#endif
155170

171+
init_curl_http_auth(result);
172+
156173
if (ssl_cert != NULL)
157174
curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
158175
#if LIBCURL_VERSION_NUM >= 0x070902
@@ -190,6 +207,46 @@ static CURL *get_curl_handle(void)
190207
return result;
191208
}
192209

210+
static void http_auth_init(const char *url)
211+
{
212+
char *at, *colon, *cp, *slash;
213+
int len;
214+
215+
cp = strstr(url, "://");
216+
if (!cp)
217+
return;
218+
219+
/*
220+
* Ok, the URL looks like "proto://something". Which one?
221+
* "proto://<user>:<pass>@<host>/...",
222+
* "proto://<user>@<host>/...", or just
223+
* "proto://<host>/..."?
224+
*/
225+
cp += 3;
226+
at = strchr(cp, '@');
227+
colon = strchr(cp, ':');
228+
slash = strchrnul(cp, '/');
229+
if (!at || slash <= at)
230+
return; /* No credentials */
231+
if (!colon || at <= colon) {
232+
/* Only username */
233+
len = at - cp;
234+
user_name = xmalloc(len + 1);
235+
memcpy(user_name, cp, len);
236+
user_name[len] = '\0';
237+
user_pass = NULL;
238+
} else {
239+
len = colon - cp;
240+
user_name = xmalloc(len + 1);
241+
memcpy(user_name, cp, len);
242+
user_name[len] = '\0';
243+
len = at - (colon + 1);
244+
user_pass = xmalloc(len + 1);
245+
memcpy(user_pass, colon + 1, len);
246+
user_pass[len] = '\0';
247+
}
248+
}
249+
193250
static void set_from_env(const char **var, const char *envname)
194251
{
195252
const char *val = getenv(envname);
@@ -255,6 +312,9 @@ void http_init(struct remote *remote)
255312
if (getenv("GIT_CURL_FTP_NO_EPSV"))
256313
curl_ftp_no_epsv = 1;
257314

315+
if (remote && remote->url && remote->url[0])
316+
http_auth_init(remote->url[0]);
317+
258318
#ifndef NO_CURL_EASY_DUPHANDLE
259319
curl_default = get_curl_handle();
260320
#endif

0 commit comments

Comments
 (0)