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