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