blob: d8d226b97e34805735cc14bdc3e549d012b33f8d [file] [log] [blame]
Jeff Kingabca9272011-12-10 05:31:11 -05001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff Kingabca9272011-12-10 05:31:11 -05003#include "credential.h"
4#include "string-list.h"
5#include "run-command.h"
Jeff Kingd3e847c2011-12-10 05:31:17 -05006#include "url.h"
Jeff Kingd3c58b82011-12-10 05:40:54 -05007#include "prompt.h"
Erik E Bradya0d51e82018-03-29 11:00:56 -07008#include "sigchain.h"
brian m. carlson46fd7b32020-02-20 02:24:13 +00009#include "urlmatch.h"
Jeff Kingabca9272011-12-10 05:31:11 -050010
11void credential_init(struct credential *c)
12{
13 memset(c, 0, sizeof(*c));
14 c->helpers.strdup_strings = 1;
15}
16
17void credential_clear(struct credential *c)
18{
19 free(c->protocol);
20 free(c->host);
21 free(c->path);
22 free(c->username);
23 free(c->password);
24 string_list_clear(&c->helpers, 0);
25
26 credential_init(c);
27}
28
Jeff King11825072011-12-10 05:31:24 -050029int credential_match(const struct credential *want,
30 const struct credential *have)
31{
32#define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
33 return CHECK(protocol) &&
34 CHECK(host) &&
35 CHECK(path) &&
36 CHECK(username);
37#undef CHECK
38}
39
Johannes Schindelin9a121b02020-04-24 11:49:52 +000040
41static int credential_from_potentially_partial_url(struct credential *c,
42 const char *url);
43
Jeff King11825072011-12-10 05:31:24 -050044static int credential_config_callback(const char *var, const char *value,
45 void *data)
46{
47 struct credential *c = data;
brian m. carlson46fd7b32020-02-20 02:24:13 +000048 const char *key;
Jeff King11825072011-12-10 05:31:24 -050049
Jeff Kingcf4fff52014-06-18 15:44:19 -040050 if (!skip_prefix(var, "credential.", &key))
Jeff King11825072011-12-10 05:31:24 -050051 return 0;
52
53 if (!value)
54 return config_error_nonbool(var);
55
Jeff King24321372016-02-26 05:51:35 -050056 if (!strcmp(key, "helper")) {
57 if (*value)
58 string_list_append(&c->helpers, value);
59 else
60 string_list_clear(&c->helpers, 0);
61 } else if (!strcmp(key, "username")) {
brian m. carlson82eb2492020-02-20 02:24:12 +000062 if (!c->username_from_proto) {
63 free(c->username);
Jeff Kingd5742422011-12-10 05:31:30 -050064 c->username = xstrdup(value);
brian m. carlson82eb2492020-02-20 02:24:12 +000065 }
Jeff Kingd5742422011-12-10 05:31:30 -050066 }
Jeff Kinga78fbb42011-12-10 05:31:34 -050067 else if (!strcmp(key, "usehttppath"))
68 c->use_http_path = git_config_bool(var, value);
Jeff King11825072011-12-10 05:31:24 -050069
70 return 0;
71}
72
Jeff Kinga78fbb42011-12-10 05:31:34 -050073static int proto_is_http(const char *s)
74{
75 if (!s)
76 return 0;
77 return !strcmp(s, "https") || !strcmp(s, "http");
78}
79
brian m. carlson46fd7b32020-02-20 02:24:13 +000080static void credential_describe(struct credential *c, struct strbuf *out);
81static void credential_format(struct credential *c, struct strbuf *out);
82
83static int select_all(const struct urlmatch_item *a,
84 const struct urlmatch_item *b)
85{
86 return 0;
87}
88
Johannes Schindelin12294992020-04-24 22:35:49 +000089static int match_partial_url(const char *url, void *cb)
90{
91 struct credential *c = cb;
92 struct credential want = CREDENTIAL_INIT;
93 int matches = 0;
94
95 if (credential_from_potentially_partial_url(&want, url) < 0)
96 warning(_("skipping credential lookup for key: credential.%s"),
97 url);
98 else
99 matches = credential_match(&want, c);
100 credential_clear(&want);
101
102 return matches;
103}
104
Jeff King11825072011-12-10 05:31:24 -0500105static void credential_apply_config(struct credential *c)
106{
brian m. carlson46fd7b32020-02-20 02:24:13 +0000107 char *normalized_url;
108 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
109 struct strbuf url = STRBUF_INIT;
110
Jeff King8ba8ed52020-04-18 20:50:48 -0700111 if (!c->host)
112 die(_("refusing to work with credential missing host field"));
113 if (!c->protocol)
114 die(_("refusing to work with credential missing protocol field"));
115
Jeff King11825072011-12-10 05:31:24 -0500116 if (c->configured)
117 return;
brian m. carlson46fd7b32020-02-20 02:24:13 +0000118
119 config.section = "credential";
120 config.key = NULL;
121 config.collect_fn = credential_config_callback;
122 config.cascade_fn = NULL;
123 config.select_fn = select_all;
Johannes Schindelin12294992020-04-24 22:35:49 +0000124 config.fallback_match_fn = match_partial_url;
brian m. carlson46fd7b32020-02-20 02:24:13 +0000125 config.cb = c;
126
127 credential_format(c, &url);
128 normalized_url = url_normalize(url.buf, &config.url);
129
130 git_config(urlmatch_config_entry, &config);
131 free(normalized_url);
132 strbuf_release(&url);
133
Jeff King11825072011-12-10 05:31:24 -0500134 c->configured = 1;
Jeff Kinga78fbb42011-12-10 05:31:34 -0500135
136 if (!c->use_http_path && proto_is_http(c->protocol)) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000137 FREE_AND_NULL(c->path);
Jeff Kinga78fbb42011-12-10 05:31:34 -0500138 }
Jeff King11825072011-12-10 05:31:24 -0500139}
140
Jeff Kingabca9272011-12-10 05:31:11 -0500141static void credential_describe(struct credential *c, struct strbuf *out)
142{
143 if (!c->protocol)
144 return;
145 strbuf_addf(out, "%s://", c->protocol);
146 if (c->username && *c->username)
147 strbuf_addf(out, "%s@", c->username);
148 if (c->host)
149 strbuf_addstr(out, c->host);
150 if (c->path)
151 strbuf_addf(out, "/%s", c->path);
152}
153
brian m. carlson46fd7b32020-02-20 02:24:13 +0000154static void credential_format(struct credential *c, struct strbuf *out)
155{
156 if (!c->protocol)
157 return;
158 strbuf_addf(out, "%s://", c->protocol);
159 if (c->username && *c->username) {
brian m. carlsonb44d0112020-04-27 01:18:08 +0000160 strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
brian m. carlson46fd7b32020-02-20 02:24:13 +0000161 strbuf_addch(out, '@');
162 }
163 if (c->host)
164 strbuf_addstr(out, c->host);
165 if (c->path) {
166 strbuf_addch(out, '/');
brian m. carlsonb44d0112020-04-27 01:18:08 +0000167 strbuf_add_percentencode(out, c->path, 0);
brian m. carlson46fd7b32020-02-20 02:24:13 +0000168 }
169}
170
Jeff Kingce77aa42011-12-10 05:41:23 -0500171static char *credential_ask_one(const char *what, struct credential *c,
172 int flags)
Jeff Kingabca9272011-12-10 05:31:11 -0500173{
174 struct strbuf desc = STRBUF_INIT;
175 struct strbuf prompt = STRBUF_INIT;
176 char *r;
177
178 credential_describe(c, &desc);
179 if (desc.len)
180 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
181 else
182 strbuf_addf(&prompt, "%s: ", what);
183
Jeff Kingce77aa42011-12-10 05:41:23 -0500184 r = git_prompt(prompt.buf, flags);
Jeff Kingabca9272011-12-10 05:31:11 -0500185
186 strbuf_release(&desc);
187 strbuf_release(&prompt);
188 return xstrdup(r);
189}
190
191static void credential_getpass(struct credential *c)
192{
193 if (!c->username)
Jeff Kingce77aa42011-12-10 05:41:23 -0500194 c->username = credential_ask_one("Username", c,
195 PROMPT_ASKPASS|PROMPT_ECHO);
Jeff Kingabca9272011-12-10 05:31:11 -0500196 if (!c->password)
Jeff Kingce77aa42011-12-10 05:41:23 -0500197 c->password = credential_ask_one("Password", c,
198 PROMPT_ASKPASS);
Jeff Kingabca9272011-12-10 05:31:11 -0500199}
200
201int credential_read(struct credential *c, FILE *fp)
202{
203 struct strbuf line = STRBUF_INIT;
204
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800205 while (strbuf_getline_lf(&line, fp) != EOF) {
Jeff Kingabca9272011-12-10 05:31:11 -0500206 char *key = line.buf;
207 char *value = strchr(key, '=');
208
209 if (!line.len)
210 break;
211
212 if (!value) {
213 warning("invalid credential line: %s", key);
214 strbuf_release(&line);
215 return -1;
216 }
217 *value++ = '\0';
218
219 if (!strcmp(key, "username")) {
220 free(c->username);
221 c->username = xstrdup(value);
brian m. carlson82eb2492020-02-20 02:24:12 +0000222 c->username_from_proto = 1;
Jeff Kingabca9272011-12-10 05:31:11 -0500223 } else if (!strcmp(key, "password")) {
224 free(c->password);
225 c->password = xstrdup(value);
226 } else if (!strcmp(key, "protocol")) {
227 free(c->protocol);
228 c->protocol = xstrdup(value);
229 } else if (!strcmp(key, "host")) {
230 free(c->host);
231 c->host = xstrdup(value);
232 } else if (!strcmp(key, "path")) {
233 free(c->path);
234 c->path = xstrdup(value);
Jeff King9c183a72012-07-18 08:06:26 -0400235 } else if (!strcmp(key, "url")) {
236 credential_from_url(c, value);
Jeff King59b38652014-12-03 22:46:48 -0500237 } else if (!strcmp(key, "quit")) {
238 c->quit = !!git_config_bool("quit", value);
Jeff Kingabca9272011-12-10 05:31:11 -0500239 }
240 /*
241 * Ignore other lines; we don't know what they mean, but
242 * this future-proofs us when later versions of git do
243 * learn new lines, and the helpers are updated to match.
244 */
245 }
246
247 strbuf_release(&line);
248 return 0;
249}
250
Jeff King8ba8ed52020-04-18 20:50:48 -0700251static void credential_write_item(FILE *fp, const char *key, const char *value,
252 int required)
Jeff Kingabca9272011-12-10 05:31:11 -0500253{
Jeff King8ba8ed52020-04-18 20:50:48 -0700254 if (!value && required)
255 BUG("credential value for %s is missing", key);
Jeff Kingabca9272011-12-10 05:31:11 -0500256 if (!value)
257 return;
Jeff King9a6bbee2020-03-11 17:53:41 -0400258 if (strchr(value, '\n'))
259 die("credential value for %s contains newline", key);
Jeff Kingabca9272011-12-10 05:31:11 -0500260 fprintf(fp, "%s=%s\n", key, value);
261}
262
Matthieu Moy2d6dc182012-06-24 13:40:00 +0200263void credential_write(const struct credential *c, FILE *fp)
Jeff Kingabca9272011-12-10 05:31:11 -0500264{
Jeff King8ba8ed52020-04-18 20:50:48 -0700265 credential_write_item(fp, "protocol", c->protocol, 1);
266 credential_write_item(fp, "host", c->host, 1);
267 credential_write_item(fp, "path", c->path, 0);
268 credential_write_item(fp, "username", c->username, 0);
269 credential_write_item(fp, "password", c->password, 0);
Jeff Kingabca9272011-12-10 05:31:11 -0500270}
271
272static int run_credential_helper(struct credential *c,
273 const char *cmd,
274 int want_output)
275{
René Scharfed3180272014-08-19 21:09:35 +0200276 struct child_process helper = CHILD_PROCESS_INIT;
Jeff Kingabca9272011-12-10 05:31:11 -0500277 const char *argv[] = { NULL, NULL };
278 FILE *fp;
279
Jeff Kingabca9272011-12-10 05:31:11 -0500280 argv[0] = cmd;
281 helper.argv = argv;
282 helper.use_shell = 1;
283 helper.in = -1;
284 if (want_output)
285 helper.out = -1;
286 else
287 helper.no_stdout = 1;
288
289 if (start_command(&helper) < 0)
290 return -1;
291
292 fp = xfdopen(helper.in, "w");
Erik E Bradya0d51e82018-03-29 11:00:56 -0700293 sigchain_push(SIGPIPE, SIG_IGN);
Jeff Kingabca9272011-12-10 05:31:11 -0500294 credential_write(c, fp);
295 fclose(fp);
Erik E Bradya0d51e82018-03-29 11:00:56 -0700296 sigchain_pop(SIGPIPE);
Jeff Kingabca9272011-12-10 05:31:11 -0500297
298 if (want_output) {
299 int r;
300 fp = xfdopen(helper.out, "r");
301 r = credential_read(c, fp);
302 fclose(fp);
303 if (r < 0) {
304 finish_command(&helper);
305 return -1;
306 }
307 }
308
309 if (finish_command(&helper))
310 return -1;
311 return 0;
312}
313
314static int credential_do(struct credential *c, const char *helper,
315 const char *operation)
316{
317 struct strbuf cmd = STRBUF_INIT;
318 int r;
319
320 if (helper[0] == '!')
321 strbuf_addstr(&cmd, helper + 1);
322 else if (is_absolute_path(helper))
323 strbuf_addstr(&cmd, helper);
324 else
325 strbuf_addf(&cmd, "git credential-%s", helper);
326
327 strbuf_addf(&cmd, " %s", operation);
328 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
329
330 strbuf_release(&cmd);
331 return r;
332}
333
334void credential_fill(struct credential *c)
335{
336 int i;
337
338 if (c->username && c->password)
339 return;
340
Jeff King11825072011-12-10 05:31:24 -0500341 credential_apply_config(c);
342
Jeff Kingabca9272011-12-10 05:31:11 -0500343 for (i = 0; i < c->helpers.nr; i++) {
344 credential_do(c, c->helpers.items[i].string, "get");
345 if (c->username && c->password)
346 return;
Jeff King59b38652014-12-03 22:46:48 -0500347 if (c->quit)
348 die("credential helper '%s' told us to quit",
349 c->helpers.items[i].string);
Jeff Kingabca9272011-12-10 05:31:11 -0500350 }
351
352 credential_getpass(c);
353 if (!c->username && !c->password)
354 die("unable to get password from user");
355}
356
357void credential_approve(struct credential *c)
358{
359 int i;
360
361 if (c->approved)
362 return;
363 if (!c->username || !c->password)
364 return;
365
Jeff King11825072011-12-10 05:31:24 -0500366 credential_apply_config(c);
367
Jeff Kingabca9272011-12-10 05:31:11 -0500368 for (i = 0; i < c->helpers.nr; i++)
369 credential_do(c, c->helpers.items[i].string, "store");
370 c->approved = 1;
371}
372
373void credential_reject(struct credential *c)
374{
375 int i;
376
Jeff King11825072011-12-10 05:31:24 -0500377 credential_apply_config(c);
378
Jeff Kingabca9272011-12-10 05:31:11 -0500379 for (i = 0; i < c->helpers.nr; i++)
380 credential_do(c, c->helpers.items[i].string, "erase");
381
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000382 FREE_AND_NULL(c->username);
383 FREE_AND_NULL(c->password);
Jeff Kingabca9272011-12-10 05:31:11 -0500384 c->approved = 0;
385}
Jeff Kingd3e847c2011-12-10 05:31:17 -0500386
Jeff Kingc716fe42020-03-12 01:31:11 -0400387static int check_url_component(const char *url, int quiet,
388 const char *name, const char *value)
389{
390 if (!value)
391 return 0;
392 if (!strchr(value, '\n'))
393 return 0;
394
395 if (!quiet)
396 warning(_("url contains a newline in its %s component: %s"),
397 name, url);
398 return -1;
399}
400
Johannes Schindelin6828e592020-04-24 11:49:51 +0000401/*
402 * Potentially-partial URLs can, but do not have to, contain
403 *
404 * - a protocol (or scheme) of the form "<protocol>://"
405 *
406 * - a host name (the part after the protocol and before the first slash after
407 * that, if any)
408 *
409 * - a user name and potentially a password (as "<user>[:<password>]@" part of
410 * the host name)
411 *
412 * - a path (the part after the host name, if any, starting with the slash)
413 *
414 * Missing parts will be left unset in `struct credential`. Thus, `https://`
415 * will have only the `protocol` set, `example.com` only the host name, and
416 * `/git` only the path.
417 *
418 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
419 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
420 * be potentially partial, and only then (otherwise, the empty string is used).
421 *
422 * The credential_from_url() function does not allow partial URLs.
423 */
424static int credential_from_url_1(struct credential *c, const char *url,
425 int allow_partial_url, int quiet)
Jeff Kingd3e847c2011-12-10 05:31:17 -0500426{
427 const char *at, *colon, *cp, *slash, *host, *proto_end;
428
429 credential_clear(c);
430
431 /*
432 * Match one of:
433 * (1) proto://<host>/...
434 * (2) proto://<user>@<host>/...
435 * (3) proto://<user>:<pass>@<host>/...
436 */
437 proto_end = strstr(url, "://");
Johannes Schindelin6828e592020-04-24 11:49:51 +0000438 if (!allow_partial_url && (!proto_end || proto_end == url)) {
Jonathan Niederc44088e2020-04-18 20:54:13 -0700439 if (!quiet)
440 warning(_("url has no scheme: %s"), url);
441 return -1;
442 }
Johannes Schindelin6828e592020-04-24 11:49:51 +0000443 cp = proto_end ? proto_end + 3 : url;
Jeff Kingd3e847c2011-12-10 05:31:17 -0500444 at = strchr(cp, '@');
445 colon = strchr(cp, ':');
Jeff King4c5971e2020-04-14 17:43:04 -0400446
447 /*
448 * A query or fragment marker before the slash ends the host portion.
449 * We'll just continue to call this "slash" for simplicity. Notably our
450 * "trim leading slashes" part won't skip over this part of the path,
451 * but that's what we'd want.
452 */
453 slash = cp + strcspn(cp, "/?#");
Jeff Kingd3e847c2011-12-10 05:31:17 -0500454
455 if (!at || slash <= at) {
456 /* Case (1) */
457 host = cp;
458 }
459 else if (!colon || at <= colon) {
460 /* Case (2) */
461 c->username = url_decode_mem(cp, at - cp);
brian m. carlson82eb2492020-02-20 02:24:12 +0000462 if (c->username && *c->username)
463 c->username_from_proto = 1;
Jeff Kingd3e847c2011-12-10 05:31:17 -0500464 host = at + 1;
465 } else {
466 /* Case (3) */
467 c->username = url_decode_mem(cp, colon - cp);
brian m. carlson82eb2492020-02-20 02:24:12 +0000468 if (c->username && *c->username)
469 c->username_from_proto = 1;
Jeff Kingd3e847c2011-12-10 05:31:17 -0500470 c->password = url_decode_mem(colon + 1, at - (colon + 1));
471 host = at + 1;
472 }
473
Johannes Schindelin6828e592020-04-24 11:49:51 +0000474 if (proto_end && proto_end - url > 0)
475 c->protocol = xmemdupz(url, proto_end - url);
476 if (!allow_partial_url || slash - host > 0)
477 c->host = url_decode_mem(host, slash - host);
Jeff Kingd3e847c2011-12-10 05:31:17 -0500478 /* Trim leading and trailing slashes from path */
479 while (*slash == '/')
480 slash++;
481 if (*slash) {
482 char *p;
483 c->path = url_decode(slash);
484 p = c->path + strlen(c->path) - 1;
485 while (p > c->path && *p == '/')
486 *p-- = '\0';
487 }
Jeff Kingc716fe42020-03-12 01:31:11 -0400488
489 if (check_url_component(url, quiet, "username", c->username) < 0 ||
490 check_url_component(url, quiet, "password", c->password) < 0 ||
491 check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
492 check_url_component(url, quiet, "host", c->host) < 0 ||
493 check_url_component(url, quiet, "path", c->path) < 0)
494 return -1;
495
496 return 0;
497}
498
Johannes Schindelin9a121b02020-04-24 11:49:52 +0000499static int credential_from_potentially_partial_url(struct credential *c,
500 const char *url)
501{
502 return credential_from_url_1(c, url, 1, 0);
503}
504
Johannes Schindelin6828e592020-04-24 11:49:51 +0000505int credential_from_url_gently(struct credential *c, const char *url, int quiet)
506{
507 return credential_from_url_1(c, url, 0, quiet);
508}
509
Jeff Kingc716fe42020-03-12 01:31:11 -0400510void credential_from_url(struct credential *c, const char *url)
511{
Jeff Kingfe29a9b2020-04-18 20:53:09 -0700512 if (credential_from_url_gently(c, url, 0) < 0)
513 die(_("credential url cannot be parsed: %s"), url);
Jeff Kingd3e847c2011-12-10 05:31:17 -0500514}