]> git.ipfire.org Git - thirdparty/git.git/blame - credential.c
Git 2.45-rc0
[thirdparty/git.git] / credential.c
CommitLineData
5e3f94df 1#include "git-compat-util.h"
0b027f6c 2#include "abspath.h"
b2141fc1 3#include "config.h"
abca927d 4#include "credential.h"
f394e093 5#include "gettext.h"
abca927d
JK
6#include "string-list.h"
7#include "run-command.h"
d3e847c1 8#include "url.h"
d3c58b83 9#include "prompt.h"
a0d51e8d 10#include "sigchain.h"
69a63fe6 11#include "strbuf.h"
46fd7b39 12#include "urlmatch.h"
d208bfdf 13#include "git-compat-util.h"
abca927d
JK
14
15void credential_init(struct credential *c)
16{
5726a6b4
ÆAB
17 struct credential blank = CREDENTIAL_INIT;
18 memcpy(c, &blank, sizeof(*c));
abca927d
JK
19}
20
21void credential_clear(struct credential *c)
22{
23 free(c->protocol);
24 free(c->host);
25 free(c->path);
26 free(c->username);
27 free(c->password);
a5c76569 28 free(c->oauth_refresh_token);
abca927d 29 string_list_clear(&c->helpers, 0);
6b8dda9a 30 strvec_clear(&c->wwwauth_headers);
abca927d
JK
31
32 credential_init(c);
33}
34
11825072 35int credential_match(const struct credential *want,
aeb21ce2 36 const struct credential *have, int match_password)
11825072
JK
37{
38#define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
39 return CHECK(protocol) &&
40 CHECK(host) &&
41 CHECK(path) &&
aeb21ce2
H
42 CHECK(username) &&
43 (!match_password || CHECK(password));
11825072
JK
44#undef CHECK
45}
46
9a121b0d
JS
47
48static int credential_from_potentially_partial_url(struct credential *c,
49 const char *url);
50
11825072 51static int credential_config_callback(const char *var, const char *value,
a4e7e317 52 const struct config_context *ctx UNUSED,
11825072
JK
53 void *data)
54{
55 struct credential *c = data;
46fd7b39 56 const char *key;
11825072 57
cf4fff57 58 if (!skip_prefix(var, "credential.", &key))
11825072
JK
59 return 0;
60
61 if (!value)
62 return config_error_nonbool(var);
63
24321375
JK
64 if (!strcmp(key, "helper")) {
65 if (*value)
66 string_list_append(&c->helpers, value);
67 else
68 string_list_clear(&c->helpers, 0);
69 } else if (!strcmp(key, "username")) {
82eb2498 70 if (!c->username_from_proto) {
71 free(c->username);
d5742425 72 c->username = xstrdup(value);
82eb2498 73 }
d5742425 74 }
a78fbb4f
JK
75 else if (!strcmp(key, "usehttppath"))
76 c->use_http_path = git_config_bool(var, value);
11825072
JK
77
78 return 0;
79}
80
a78fbb4f
JK
81static int proto_is_http(const char *s)
82{
83 if (!s)
84 return 0;
85 return !strcmp(s, "https") || !strcmp(s, "http");
86}
87
46fd7b39 88static void credential_describe(struct credential *c, struct strbuf *out);
89static void credential_format(struct credential *c, struct strbuf *out);
90
8ca19951
JK
91static int select_all(const struct urlmatch_item *a UNUSED,
92 const struct urlmatch_item *b UNUSED)
46fd7b39 93{
94 return 0;
95}
96
12294990
JS
97static int match_partial_url(const char *url, void *cb)
98{
99 struct credential *c = cb;
100 struct credential want = CREDENTIAL_INIT;
101 int matches = 0;
102
103 if (credential_from_potentially_partial_url(&want, url) < 0)
104 warning(_("skipping credential lookup for key: credential.%s"),
105 url);
106 else
aeb21ce2 107 matches = credential_match(&want, c, 0);
12294990
JS
108 credential_clear(&want);
109
110 return matches;
111}
112
11825072
JK
113static void credential_apply_config(struct credential *c)
114{
46fd7b39 115 char *normalized_url;
73ee449b 116 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
46fd7b39 117 struct strbuf url = STRBUF_INIT;
118
8ba8ed56
JK
119 if (!c->host)
120 die(_("refusing to work with credential missing host field"));
121 if (!c->protocol)
122 die(_("refusing to work with credential missing protocol field"));
123
11825072
JK
124 if (c->configured)
125 return;
46fd7b39 126
127 config.section = "credential";
128 config.key = NULL;
129 config.collect_fn = credential_config_callback;
130 config.cascade_fn = NULL;
131 config.select_fn = select_all;
12294990 132 config.fallback_match_fn = match_partial_url;
46fd7b39 133 config.cb = c;
134
135 credential_format(c, &url);
136 normalized_url = url_normalize(url.buf, &config.url);
137
138 git_config(urlmatch_config_entry, &config);
5146c2f1 139 string_list_clear(&config.vars, 1);
46fd7b39 140 free(normalized_url);
a41e8e74 141 urlmatch_config_release(&config);
46fd7b39 142 strbuf_release(&url);
143
11825072 144 c->configured = 1;
a78fbb4f
JK
145
146 if (!c->use_http_path && proto_is_http(c->protocol)) {
6a83d902 147 FREE_AND_NULL(c->path);
a78fbb4f 148 }
11825072
JK
149}
150
abca927d
JK
151static void credential_describe(struct credential *c, struct strbuf *out)
152{
153 if (!c->protocol)
154 return;
155 strbuf_addf(out, "%s://", c->protocol);
156 if (c->username && *c->username)
157 strbuf_addf(out, "%s@", c->username);
158 if (c->host)
159 strbuf_addstr(out, c->host);
160 if (c->path)
161 strbuf_addf(out, "/%s", c->path);
162}
163
46fd7b39 164static void credential_format(struct credential *c, struct strbuf *out)
165{
166 if (!c->protocol)
167 return;
168 strbuf_addf(out, "%s://", c->protocol);
169 if (c->username && *c->username) {
b44d0118 170 strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
46fd7b39 171 strbuf_addch(out, '@');
172 }
173 if (c->host)
174 strbuf_addstr(out, c->host);
175 if (c->path) {
176 strbuf_addch(out, '/');
b44d0118 177 strbuf_add_percentencode(out, c->path, 0);
46fd7b39 178 }
179}
180
ce77aa48
JK
181static char *credential_ask_one(const char *what, struct credential *c,
182 int flags)
abca927d
JK
183{
184 struct strbuf desc = STRBUF_INIT;
185 struct strbuf prompt = STRBUF_INIT;
186 char *r;
187
188 credential_describe(c, &desc);
189 if (desc.len)
190 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
191 else
192 strbuf_addf(&prompt, "%s: ", what);
193
ce77aa48 194 r = git_prompt(prompt.buf, flags);
abca927d
JK
195
196 strbuf_release(&desc);
197 strbuf_release(&prompt);
198 return xstrdup(r);
199}
200
201static void credential_getpass(struct credential *c)
202{
203 if (!c->username)
ce77aa48
JK
204 c->username = credential_ask_one("Username", c,
205 PROMPT_ASKPASS|PROMPT_ECHO);
abca927d 206 if (!c->password)
ce77aa48
JK
207 c->password = credential_ask_one("Password", c,
208 PROMPT_ASKPASS);
abca927d
JK
209}
210
211int credential_read(struct credential *c, FILE *fp)
212{
213 struct strbuf line = STRBUF_INIT;
214
356c4732 215 while (strbuf_getline(&line, fp) != EOF) {
abca927d
JK
216 char *key = line.buf;
217 char *value = strchr(key, '=');
218
219 if (!line.len)
220 break;
221
222 if (!value) {
223 warning("invalid credential line: %s", key);
224 strbuf_release(&line);
225 return -1;
226 }
227 *value++ = '\0';
228
229 if (!strcmp(key, "username")) {
230 free(c->username);
231 c->username = xstrdup(value);
82eb2498 232 c->username_from_proto = 1;
abca927d
JK
233 } else if (!strcmp(key, "password")) {
234 free(c->password);
235 c->password = xstrdup(value);
236 } else if (!strcmp(key, "protocol")) {
237 free(c->protocol);
238 c->protocol = xstrdup(value);
239 } else if (!strcmp(key, "host")) {
240 free(c->host);
241 c->host = xstrdup(value);
242 } else if (!strcmp(key, "path")) {
243 free(c->path);
244 c->path = xstrdup(value);
16b305cd
TB
245 } else if (!strcmp(key, "wwwauth[]")) {
246 strvec_push(&c->wwwauth_headers, value);
d208bfdf
H
247 } else if (!strcmp(key, "password_expiry_utc")) {
248 errno = 0;
249 c->password_expiry_utc = parse_timestamp(value, NULL, 10);
250 if (c->password_expiry_utc == 0 || errno == ERANGE)
251 c->password_expiry_utc = TIME_MAX;
a5c76569
H
252 } else if (!strcmp(key, "oauth_refresh_token")) {
253 free(c->oauth_refresh_token);
254 c->oauth_refresh_token = xstrdup(value);
9c183a70
JK
255 } else if (!strcmp(key, "url")) {
256 credential_from_url(c, value);
59b38652
JK
257 } else if (!strcmp(key, "quit")) {
258 c->quit = !!git_config_bool("quit", value);
abca927d
JK
259 }
260 /*
261 * Ignore other lines; we don't know what they mean, but
262 * this future-proofs us when later versions of git do
263 * learn new lines, and the helpers are updated to match.
264 */
265 }
266
267 strbuf_release(&line);
268 return 0;
269}
270
8ba8ed56
JK
271static void credential_write_item(FILE *fp, const char *key, const char *value,
272 int required)
abca927d 273{
8ba8ed56
JK
274 if (!value && required)
275 BUG("credential value for %s is missing", key);
abca927d
JK
276 if (!value)
277 return;
9a6bbee8
JK
278 if (strchr(value, '\n'))
279 die("credential value for %s contains newline", key);
abca927d
JK
280 fprintf(fp, "%s=%s\n", key, value);
281}
282
2d6dc182 283void credential_write(const struct credential *c, FILE *fp)
abca927d 284{
8ba8ed56
JK
285 credential_write_item(fp, "protocol", c->protocol, 1);
286 credential_write_item(fp, "host", c->host, 1);
287 credential_write_item(fp, "path", c->path, 0);
288 credential_write_item(fp, "username", c->username, 0);
289 credential_write_item(fp, "password", c->password, 0);
a5c76569 290 credential_write_item(fp, "oauth_refresh_token", c->oauth_refresh_token, 0);
d208bfdf
H
291 if (c->password_expiry_utc != TIME_MAX) {
292 char *s = xstrfmt("%"PRItime, c->password_expiry_utc);
293 credential_write_item(fp, "password_expiry_utc", s, 0);
294 free(s);
295 }
5f2117b2 296 for (size_t i = 0; i < c->wwwauth_headers.nr; i++)
92c56da0 297 credential_write_item(fp, "wwwauth[]", c->wwwauth_headers.v[i], 0);
abca927d
JK
298}
299
300static int run_credential_helper(struct credential *c,
301 const char *cmd,
302 int want_output)
303{
d3180279 304 struct child_process helper = CHILD_PROCESS_INIT;
abca927d
JK
305 FILE *fp;
306
afbdba39 307 strvec_push(&helper.args, cmd);
abca927d
JK
308 helper.use_shell = 1;
309 helper.in = -1;
310 if (want_output)
311 helper.out = -1;
312 else
313 helper.no_stdout = 1;
314
315 if (start_command(&helper) < 0)
316 return -1;
317
318 fp = xfdopen(helper.in, "w");
a0d51e8d 319 sigchain_push(SIGPIPE, SIG_IGN);
abca927d
JK
320 credential_write(c, fp);
321 fclose(fp);
a0d51e8d 322 sigchain_pop(SIGPIPE);
abca927d
JK
323
324 if (want_output) {
325 int r;
326 fp = xfdopen(helper.out, "r");
327 r = credential_read(c, fp);
328 fclose(fp);
329 if (r < 0) {
330 finish_command(&helper);
331 return -1;
332 }
333 }
334
335 if (finish_command(&helper))
336 return -1;
337 return 0;
338}
339
340static int credential_do(struct credential *c, const char *helper,
341 const char *operation)
342{
343 struct strbuf cmd = STRBUF_INIT;
344 int r;
345
346 if (helper[0] == '!')
347 strbuf_addstr(&cmd, helper + 1);
348 else if (is_absolute_path(helper))
349 strbuf_addstr(&cmd, helper);
350 else
351 strbuf_addf(&cmd, "git credential-%s", helper);
352
353 strbuf_addf(&cmd, " %s", operation);
354 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
355
356 strbuf_release(&cmd);
357 return r;
358}
359
360void credential_fill(struct credential *c)
361{
362 int i;
363
364 if (c->username && c->password)
365 return;
366
11825072
JK
367 credential_apply_config(c);
368
abca927d
JK
369 for (i = 0; i < c->helpers.nr; i++) {
370 credential_do(c, c->helpers.items[i].string, "get");
d208bfdf
H
371 if (c->password_expiry_utc < time(NULL)) {
372 /* Discard expired password */
373 FREE_AND_NULL(c->password);
374 /* Reset expiry to maintain consistency */
375 c->password_expiry_utc = TIME_MAX;
376 }
abca927d
JK
377 if (c->username && c->password)
378 return;
59b38652
JK
379 if (c->quit)
380 die("credential helper '%s' told us to quit",
381 c->helpers.items[i].string);
abca927d
JK
382 }
383
384 credential_getpass(c);
385 if (!c->username && !c->password)
386 die("unable to get password from user");
387}
388
389void credential_approve(struct credential *c)
390{
391 int i;
392
393 if (c->approved)
394 return;
d208bfdf 395 if (!c->username || !c->password || c->password_expiry_utc < time(NULL))
abca927d
JK
396 return;
397
11825072
JK
398 credential_apply_config(c);
399
abca927d
JK
400 for (i = 0; i < c->helpers.nr; i++)
401 credential_do(c, c->helpers.items[i].string, "store");
402 c->approved = 1;
403}
404
405void credential_reject(struct credential *c)
406{
407 int i;
408
11825072
JK
409 credential_apply_config(c);
410
abca927d
JK
411 for (i = 0; i < c->helpers.nr; i++)
412 credential_do(c, c->helpers.items[i].string, "erase");
413
88ce3ef6
ÆAB
414 FREE_AND_NULL(c->username);
415 FREE_AND_NULL(c->password);
a5c76569 416 FREE_AND_NULL(c->oauth_refresh_token);
d208bfdf 417 c->password_expiry_utc = TIME_MAX;
abca927d
JK
418 c->approved = 0;
419}
d3e847c1 420
c716fe4b
JK
421static int check_url_component(const char *url, int quiet,
422 const char *name, const char *value)
423{
424 if (!value)
425 return 0;
426 if (!strchr(value, '\n'))
427 return 0;
428
429 if (!quiet)
430 warning(_("url contains a newline in its %s component: %s"),
431 name, url);
432 return -1;
433}
434
6828e597
JS
435/*
436 * Potentially-partial URLs can, but do not have to, contain
437 *
438 * - a protocol (or scheme) of the form "<protocol>://"
439 *
440 * - a host name (the part after the protocol and before the first slash after
441 * that, if any)
442 *
443 * - a user name and potentially a password (as "<user>[:<password>]@" part of
444 * the host name)
445 *
446 * - a path (the part after the host name, if any, starting with the slash)
447 *
448 * Missing parts will be left unset in `struct credential`. Thus, `https://`
449 * will have only the `protocol` set, `example.com` only the host name, and
450 * `/git` only the path.
451 *
452 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
453 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
454 * be potentially partial, and only then (otherwise, the empty string is used).
455 *
456 * The credential_from_url() function does not allow partial URLs.
457 */
458static int credential_from_url_1(struct credential *c, const char *url,
459 int allow_partial_url, int quiet)
d3e847c1
JK
460{
461 const char *at, *colon, *cp, *slash, *host, *proto_end;
462
463 credential_clear(c);
464
465 /*
466 * Match one of:
467 * (1) proto://<host>/...
468 * (2) proto://<user>@<host>/...
469 * (3) proto://<user>:<pass>@<host>/...
470 */
471 proto_end = strstr(url, "://");
6828e597 472 if (!allow_partial_url && (!proto_end || proto_end == url)) {
c44088ec
JN
473 if (!quiet)
474 warning(_("url has no scheme: %s"), url);
475 return -1;
476 }
6828e597 477 cp = proto_end ? proto_end + 3 : url;
d3e847c1
JK
478 at = strchr(cp, '@');
479 colon = strchr(cp, ':');
4c5971e1
JK
480
481 /*
482 * A query or fragment marker before the slash ends the host portion.
483 * We'll just continue to call this "slash" for simplicity. Notably our
484 * "trim leading slashes" part won't skip over this part of the path,
485 * but that's what we'd want.
486 */
487 slash = cp + strcspn(cp, "/?#");
d3e847c1
JK
488
489 if (!at || slash <= at) {
490 /* Case (1) */
491 host = cp;
492 }
493 else if (!colon || at <= colon) {
494 /* Case (2) */
495 c->username = url_decode_mem(cp, at - cp);
82eb2498 496 if (c->username && *c->username)
497 c->username_from_proto = 1;
d3e847c1
JK
498 host = at + 1;
499 } else {
500 /* Case (3) */
501 c->username = url_decode_mem(cp, colon - cp);
82eb2498 502 if (c->username && *c->username)
503 c->username_from_proto = 1;
d3e847c1
JK
504 c->password = url_decode_mem(colon + 1, at - (colon + 1));
505 host = at + 1;
506 }
507
6828e597
JS
508 if (proto_end && proto_end - url > 0)
509 c->protocol = xmemdupz(url, proto_end - url);
510 if (!allow_partial_url || slash - host > 0)
511 c->host = url_decode_mem(host, slash - host);
d3e847c1
JK
512 /* Trim leading and trailing slashes from path */
513 while (*slash == '/')
514 slash++;
515 if (*slash) {
516 char *p;
517 c->path = url_decode(slash);
518 p = c->path + strlen(c->path) - 1;
519 while (p > c->path && *p == '/')
520 *p-- = '\0';
521 }
c716fe4b
JK
522
523 if (check_url_component(url, quiet, "username", c->username) < 0 ||
524 check_url_component(url, quiet, "password", c->password) < 0 ||
525 check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
526 check_url_component(url, quiet, "host", c->host) < 0 ||
527 check_url_component(url, quiet, "path", c->path) < 0)
528 return -1;
529
530 return 0;
531}
532
9a121b0d
JS
533static int credential_from_potentially_partial_url(struct credential *c,
534 const char *url)
535{
536 return credential_from_url_1(c, url, 1, 0);
537}
538
6828e597
JS
539int credential_from_url_gently(struct credential *c, const char *url, int quiet)
540{
541 return credential_from_url_1(c, url, 0, quiet);
542}
543
c716fe4b
JK
544void credential_from_url(struct credential *c, const char *url)
545{
fe29a9b7
JK
546 if (credential_from_url_gently(c, url, 0) < 0)
547 die(_("credential url cannot be parsed: %s"), url);
d3e847c1 548}