🌐 AI搜索 & 代理 主页
blob: e7240f3f636f9d687836b7fe6df65b548ce285fd [file] [log] [blame]
Jeff Kingabca9272011-12-10 10:31:111#include "cache.h"
Brandon Williamsb2141fc2017-06-14 18:07:362#include "config.h"
Jeff Kingabca9272011-12-10 10:31:113#include "credential.h"
4#include "string-list.h"
5#include "run-command.h"
Jeff Kingd3e847c2011-12-10 10:31:176#include "url.h"
Jeff Kingd3c58b82011-12-10 10:40:547#include "prompt.h"
Erik E Bradya0d51e82018-03-29 18:00:568#include "sigchain.h"
brian m. carlson46fd7b32020-02-20 02:24:139#include "urlmatch.h"
Jeff Kingabca9272011-12-10 10:31:1110
11void credential_init(struct credential *c)
12{
Ævar Arnfjörð Bjarmason5726a6b2021-07-01 10:51:2613 struct credential blank = CREDENTIAL_INIT;
14 memcpy(c, &blank, sizeof(*c));
Jeff Kingabca9272011-12-10 10:31:1115}
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 10:31:2429int 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:5240
41static int credential_from_potentially_partial_url(struct credential *c,
42 const char *url);
43
Jeff King11825072011-12-10 10:31:2444static 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:1348 const char *key;
Jeff King11825072011-12-10 10:31:2449
Jeff Kingcf4fff52014-06-18 19:44:1950 if (!skip_prefix(var, "credential.", &key))
Jeff King11825072011-12-10 10:31:2451 return 0;
52
53 if (!value)
54 return config_error_nonbool(var);
55
Jeff King24321372016-02-26 10:51:3556 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:1262 if (!c->username_from_proto) {
63 free(c->username);
Jeff Kingd5742422011-12-10 10:31:3064 c->username = xstrdup(value);
brian m. carlson82eb2492020-02-20 02:24:1265 }
Jeff Kingd5742422011-12-10 10:31:3066 }
Jeff Kinga78fbb42011-12-10 10:31:3467 else if (!strcmp(key, "usehttppath"))
68 c->use_http_path = git_config_bool(var, value);
Jeff King11825072011-12-10 10:31:2469
70 return 0;
71}
72
Jeff Kinga78fbb42011-12-10 10:31:3473static 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:1380static 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:4989static 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 10:31:24105static void credential_apply_config(struct credential *c)
106{
brian m. carlson46fd7b32020-02-20 02:24:13107 char *normalized_url;
Ævar Arnfjörð Bjarmason73ee4492021-10-01 10:27:33108 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
brian m. carlson46fd7b32020-02-20 02:24:13109 struct strbuf url = STRBUF_INIT;
110
Jeff King8ba8ed52020-04-19 03:50:48111 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 10:31:24116 if (c->configured)
117 return;
brian m. carlson46fd7b32020-02-20 02:24:13118
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:49124 config.fallback_match_fn = match_partial_url;
brian m. carlson46fd7b32020-02-20 02:24:13125 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);
Mike Hommey5146c2f2021-08-20 08:44:13131 string_list_clear(&config.vars, 1);
brian m. carlson46fd7b32020-02-20 02:24:13132 free(normalized_url);
133 strbuf_release(&url);
134
Jeff King11825072011-12-10 10:31:24135 c->configured = 1;
Jeff Kinga78fbb42011-12-10 10:31:34136
137 if (!c->use_http_path && proto_is_http(c->protocol)) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46138 FREE_AND_NULL(c->path);
Jeff Kinga78fbb42011-12-10 10:31:34139 }
Jeff King11825072011-12-10 10:31:24140}
141
Jeff Kingabca9272011-12-10 10:31:11142static void credential_describe(struct credential *c, struct strbuf *out)
143{
144 if (!c->protocol)
145 return;
146 strbuf_addf(out, "%s://", c->protocol);
147 if (c->username && *c->username)
148 strbuf_addf(out, "%s@", c->username);
149 if (c->host)
150 strbuf_addstr(out, c->host);
151 if (c->path)
152 strbuf_addf(out, "/%s", c->path);
153}
154
brian m. carlson46fd7b32020-02-20 02:24:13155static void credential_format(struct credential *c, struct strbuf *out)
156{
157 if (!c->protocol)
158 return;
159 strbuf_addf(out, "%s://", c->protocol);
160 if (c->username && *c->username) {
brian m. carlsonb44d0112020-04-27 01:18:08161 strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
brian m. carlson46fd7b32020-02-20 02:24:13162 strbuf_addch(out, '@');
163 }
164 if (c->host)
165 strbuf_addstr(out, c->host);
166 if (c->path) {
167 strbuf_addch(out, '/');
brian m. carlsonb44d0112020-04-27 01:18:08168 strbuf_add_percentencode(out, c->path, 0);
brian m. carlson46fd7b32020-02-20 02:24:13169 }
170}
171
Jeff Kingce77aa42011-12-10 10:41:23172static char *credential_ask_one(const char *what, struct credential *c,
173 int flags)
Jeff Kingabca9272011-12-10 10:31:11174{
175 struct strbuf desc = STRBUF_INIT;
176 struct strbuf prompt = STRBUF_INIT;
177 char *r;
178
179 credential_describe(c, &desc);
180 if (desc.len)
181 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
182 else
183 strbuf_addf(&prompt, "%s: ", what);
184
Jeff Kingce77aa42011-12-10 10:41:23185 r = git_prompt(prompt.buf, flags);
Jeff Kingabca9272011-12-10 10:31:11186
187 strbuf_release(&desc);
188 strbuf_release(&prompt);
189 return xstrdup(r);
190}
191
192static void credential_getpass(struct credential *c)
193{
194 if (!c->username)
Jeff Kingce77aa42011-12-10 10:41:23195 c->username = credential_ask_one("Username", c,
196 PROMPT_ASKPASS|PROMPT_ECHO);
Jeff Kingabca9272011-12-10 10:31:11197 if (!c->password)
Jeff Kingce77aa42011-12-10 10:41:23198 c->password = credential_ask_one("Password", c,
199 PROMPT_ASKPASS);
Jeff Kingabca9272011-12-10 10:31:11200}
201
202int credential_read(struct credential *c, FILE *fp)
203{
204 struct strbuf line = STRBUF_INIT;
205
Nikita Leonov356c4732020-10-03 13:29:12206 while (strbuf_getline(&line, fp) != EOF) {
Jeff Kingabca9272011-12-10 10:31:11207 char *key = line.buf;
208 char *value = strchr(key, '=');
209
210 if (!line.len)
211 break;
212
213 if (!value) {
214 warning("invalid credential line: %s", key);
215 strbuf_release(&line);
216 return -1;
217 }
218 *value++ = '\0';
219
220 if (!strcmp(key, "username")) {
221 free(c->username);
222 c->username = xstrdup(value);
brian m. carlson82eb2492020-02-20 02:24:12223 c->username_from_proto = 1;
Jeff Kingabca9272011-12-10 10:31:11224 } else if (!strcmp(key, "password")) {
225 free(c->password);
226 c->password = xstrdup(value);
227 } else if (!strcmp(key, "protocol")) {
228 free(c->protocol);
229 c->protocol = xstrdup(value);
230 } else if (!strcmp(key, "host")) {
231 free(c->host);
232 c->host = xstrdup(value);
233 } else if (!strcmp(key, "path")) {
234 free(c->path);
235 c->path = xstrdup(value);
Jeff King9c183a72012-07-18 12:06:26236 } else if (!strcmp(key, "url")) {
237 credential_from_url(c, value);
Jeff King59b38652014-12-04 03:46:48238 } else if (!strcmp(key, "quit")) {
239 c->quit = !!git_config_bool("quit", value);
Jeff Kingabca9272011-12-10 10:31:11240 }
241 /*
242 * Ignore other lines; we don't know what they mean, but
243 * this future-proofs us when later versions of git do
244 * learn new lines, and the helpers are updated to match.
245 */
246 }
247
248 strbuf_release(&line);
249 return 0;
250}
251
Jeff King8ba8ed52020-04-19 03:50:48252static void credential_write_item(FILE *fp, const char *key, const char *value,
253 int required)
Jeff Kingabca9272011-12-10 10:31:11254{
Jeff King8ba8ed52020-04-19 03:50:48255 if (!value && required)
256 BUG("credential value for %s is missing", key);
Jeff Kingabca9272011-12-10 10:31:11257 if (!value)
258 return;
Jeff King9a6bbee2020-03-11 21:53:41259 if (strchr(value, '\n'))
260 die("credential value for %s contains newline", key);
Jeff Kingabca9272011-12-10 10:31:11261 fprintf(fp, "%s=%s\n", key, value);
262}
263
Matthieu Moy2d6dc182012-06-24 11:40:00264void credential_write(const struct credential *c, FILE *fp)
Jeff Kingabca9272011-12-10 10:31:11265{
Jeff King8ba8ed52020-04-19 03:50:48266 credential_write_item(fp, "protocol", c->protocol, 1);
267 credential_write_item(fp, "host", c->host, 1);
268 credential_write_item(fp, "path", c->path, 0);
269 credential_write_item(fp, "username", c->username, 0);
270 credential_write_item(fp, "password", c->password, 0);
Jeff Kingabca9272011-12-10 10:31:11271}
272
273static int run_credential_helper(struct credential *c,
274 const char *cmd,
275 int want_output)
276{
René Scharfed3180272014-08-19 19:09:35277 struct child_process helper = CHILD_PROCESS_INIT;
Jeff Kingabca9272011-12-10 10:31:11278 FILE *fp;
279
Junio C Hamanoafbdba32020-08-26 22:25:03280 strvec_push(&helper.args, cmd);
Jeff Kingabca9272011-12-10 10:31:11281 helper.use_shell = 1;
282 helper.in = -1;
283 if (want_output)
284 helper.out = -1;
285 else
286 helper.no_stdout = 1;
287
288 if (start_command(&helper) < 0)
289 return -1;
290
291 fp = xfdopen(helper.in, "w");
Erik E Bradya0d51e82018-03-29 18:00:56292 sigchain_push(SIGPIPE, SIG_IGN);
Jeff Kingabca9272011-12-10 10:31:11293 credential_write(c, fp);
294 fclose(fp);
Erik E Bradya0d51e82018-03-29 18:00:56295 sigchain_pop(SIGPIPE);
Jeff Kingabca9272011-12-10 10:31:11296
297 if (want_output) {
298 int r;
299 fp = xfdopen(helper.out, "r");
300 r = credential_read(c, fp);
301 fclose(fp);
302 if (r < 0) {
303 finish_command(&helper);
304 return -1;
305 }
306 }
307
308 if (finish_command(&helper))
309 return -1;
310 return 0;
311}
312
313static int credential_do(struct credential *c, const char *helper,
314 const char *operation)
315{
316 struct strbuf cmd = STRBUF_INIT;
317 int r;
318
319 if (helper[0] == '!')
320 strbuf_addstr(&cmd, helper + 1);
321 else if (is_absolute_path(helper))
322 strbuf_addstr(&cmd, helper);
323 else
324 strbuf_addf(&cmd, "git credential-%s", helper);
325
326 strbuf_addf(&cmd, " %s", operation);
327 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
328
329 strbuf_release(&cmd);
330 return r;
331}
332
333void credential_fill(struct credential *c)
334{
335 int i;
336
337 if (c->username && c->password)
338 return;
339
Jeff King11825072011-12-10 10:31:24340 credential_apply_config(c);
341
Jeff Kingabca9272011-12-10 10:31:11342 for (i = 0; i < c->helpers.nr; i++) {
343 credential_do(c, c->helpers.items[i].string, "get");
344 if (c->username && c->password)
345 return;
Jeff King59b38652014-12-04 03:46:48346 if (c->quit)
347 die("credential helper '%s' told us to quit",
348 c->helpers.items[i].string);
Jeff Kingabca9272011-12-10 10:31:11349 }
350
351 credential_getpass(c);
352 if (!c->username && !c->password)
353 die("unable to get password from user");
354}
355
356void credential_approve(struct credential *c)
357{
358 int i;
359
360 if (c->approved)
361 return;
362 if (!c->username || !c->password)
363 return;
364
Jeff King11825072011-12-10 10:31:24365 credential_apply_config(c);
366
Jeff Kingabca9272011-12-10 10:31:11367 for (i = 0; i < c->helpers.nr; i++)
368 credential_do(c, c->helpers.items[i].string, "store");
369 c->approved = 1;
370}
371
372void credential_reject(struct credential *c)
373{
374 int i;
375
Jeff King11825072011-12-10 10:31:24376 credential_apply_config(c);
377
Jeff Kingabca9272011-12-10 10:31:11378 for (i = 0; i < c->helpers.nr; i++)
379 credential_do(c, c->helpers.items[i].string, "erase");
380
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49381 FREE_AND_NULL(c->username);
382 FREE_AND_NULL(c->password);
Jeff Kingabca9272011-12-10 10:31:11383 c->approved = 0;
384}
Jeff Kingd3e847c2011-12-10 10:31:17385
Jeff Kingc716fe42020-03-12 05:31:11386static int check_url_component(const char *url, int quiet,
387 const char *name, const char *value)
388{
389 if (!value)
390 return 0;
391 if (!strchr(value, '\n'))
392 return 0;
393
394 if (!quiet)
395 warning(_("url contains a newline in its %s component: %s"),
396 name, url);
397 return -1;
398}
399
Johannes Schindelin6828e592020-04-24 11:49:51400/*
401 * Potentially-partial URLs can, but do not have to, contain
402 *
403 * - a protocol (or scheme) of the form "<protocol>://"
404 *
405 * - a host name (the part after the protocol and before the first slash after
406 * that, if any)
407 *
408 * - a user name and potentially a password (as "<user>[:<password>]@" part of
409 * the host name)
410 *
411 * - a path (the part after the host name, if any, starting with the slash)
412 *
413 * Missing parts will be left unset in `struct credential`. Thus, `https://`
414 * will have only the `protocol` set, `example.com` only the host name, and
415 * `/git` only the path.
416 *
417 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
418 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
419 * be potentially partial, and only then (otherwise, the empty string is used).
420 *
421 * The credential_from_url() function does not allow partial URLs.
422 */
423static int credential_from_url_1(struct credential *c, const char *url,
424 int allow_partial_url, int quiet)
Jeff Kingd3e847c2011-12-10 10:31:17425{
426 const char *at, *colon, *cp, *slash, *host, *proto_end;
427
428 credential_clear(c);
429
430 /*
431 * Match one of:
432 * (1) proto://<host>/...
433 * (2) proto://<user>@<host>/...
434 * (3) proto://<user>:<pass>@<host>/...
435 */
436 proto_end = strstr(url, "://");
Johannes Schindelin6828e592020-04-24 11:49:51437 if (!allow_partial_url && (!proto_end || proto_end == url)) {
Jonathan Niederc44088e2020-04-19 03:54:13438 if (!quiet)
439 warning(_("url has no scheme: %s"), url);
440 return -1;
441 }
Johannes Schindelin6828e592020-04-24 11:49:51442 cp = proto_end ? proto_end + 3 : url;
Jeff Kingd3e847c2011-12-10 10:31:17443 at = strchr(cp, '@');
444 colon = strchr(cp, ':');
Jeff King4c5971e2020-04-14 21:43:04445
446 /*
447 * A query or fragment marker before the slash ends the host portion.
448 * We'll just continue to call this "slash" for simplicity. Notably our
449 * "trim leading slashes" part won't skip over this part of the path,
450 * but that's what we'd want.
451 */
452 slash = cp + strcspn(cp, "/?#");
Jeff Kingd3e847c2011-12-10 10:31:17453
454 if (!at || slash <= at) {
455 /* Case (1) */
456 host = cp;
457 }
458 else if (!colon || at <= colon) {
459 /* Case (2) */
460 c->username = url_decode_mem(cp, at - cp);
brian m. carlson82eb2492020-02-20 02:24:12461 if (c->username && *c->username)
462 c->username_from_proto = 1;
Jeff Kingd3e847c2011-12-10 10:31:17463 host = at + 1;
464 } else {
465 /* Case (3) */
466 c->username = url_decode_mem(cp, colon - cp);
brian m. carlson82eb2492020-02-20 02:24:12467 if (c->username && *c->username)
468 c->username_from_proto = 1;
Jeff Kingd3e847c2011-12-10 10:31:17469 c->password = url_decode_mem(colon + 1, at - (colon + 1));
470 host = at + 1;
471 }
472
Johannes Schindelin6828e592020-04-24 11:49:51473 if (proto_end && proto_end - url > 0)
474 c->protocol = xmemdupz(url, proto_end - url);
475 if (!allow_partial_url || slash - host > 0)
476 c->host = url_decode_mem(host, slash - host);
Jeff Kingd3e847c2011-12-10 10:31:17477 /* Trim leading and trailing slashes from path */
478 while (*slash == '/')
479 slash++;
480 if (*slash) {
481 char *p;
482 c->path = url_decode(slash);
483 p = c->path + strlen(c->path) - 1;
484 while (p > c->path && *p == '/')
485 *p-- = '\0';
486 }
Jeff Kingc716fe42020-03-12 05:31:11487
488 if (check_url_component(url, quiet, "username", c->username) < 0 ||
489 check_url_component(url, quiet, "password", c->password) < 0 ||
490 check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
491 check_url_component(url, quiet, "host", c->host) < 0 ||
492 check_url_component(url, quiet, "path", c->path) < 0)
493 return -1;
494
495 return 0;
496}
497
Johannes Schindelin9a121b02020-04-24 11:49:52498static int credential_from_potentially_partial_url(struct credential *c,
499 const char *url)
500{
501 return credential_from_url_1(c, url, 1, 0);
502}
503
Johannes Schindelin6828e592020-04-24 11:49:51504int credential_from_url_gently(struct credential *c, const char *url, int quiet)
505{
506 return credential_from_url_1(c, url, 0, quiet);
507}
508
Jeff Kingc716fe42020-03-12 05:31:11509void credential_from_url(struct credential *c, const char *url)
510{
Jeff Kingfe29a9b2020-04-19 03:53:09511 if (credential_from_url_gently(c, url, 0) < 0)
512 die(_("credential url cannot be parsed: %s"), url);
Jeff Kingd3e847c2011-12-10 10:31:17513}