git
/
credential.c
672 строки · 17.5 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "abspath.h"
5#include "config.h"
6#include "credential.h"
7#include "gettext.h"
8#include "string-list.h"
9#include "run-command.h"
10#include "url.h"
11#include "prompt.h"
12#include "sigchain.h"
13#include "strbuf.h"
14#include "urlmatch.h"
15#include "git-compat-util.h"
16
17void credential_init(struct credential *c)
18{
19struct credential blank = CREDENTIAL_INIT;
20memcpy(c, &blank, sizeof(*c));
21}
22
23void credential_clear(struct credential *c)
24{
25credential_clear_secrets(c);
26free(c->protocol);
27free(c->host);
28free(c->path);
29free(c->username);
30free(c->oauth_refresh_token);
31free(c->authtype);
32string_list_clear(&c->helpers, 0);
33strvec_clear(&c->wwwauth_headers);
34strvec_clear(&c->state_headers);
35strvec_clear(&c->state_headers_to_send);
36
37credential_init(c);
38}
39
40void credential_next_state(struct credential *c)
41{
42strvec_clear(&c->state_headers_to_send);
43SWAP(c->state_headers, c->state_headers_to_send);
44}
45
46void credential_clear_secrets(struct credential *c)
47{
48FREE_AND_NULL(c->password);
49FREE_AND_NULL(c->credential);
50}
51
52static void credential_set_capability(struct credential_capability *capa,
53enum credential_op_type op_type)
54{
55switch (op_type) {
56case CREDENTIAL_OP_INITIAL:
57capa->request_initial = 1;
58break;
59case CREDENTIAL_OP_HELPER:
60capa->request_helper = 1;
61break;
62case CREDENTIAL_OP_RESPONSE:
63capa->response = 1;
64break;
65}
66}
67
68
69void credential_set_all_capabilities(struct credential *c,
70enum credential_op_type op_type)
71{
72credential_set_capability(&c->capa_authtype, op_type);
73credential_set_capability(&c->capa_state, op_type);
74}
75
76static void announce_one(struct credential_capability *cc, const char *name, FILE *fp) {
77if (cc->request_initial)
78fprintf(fp, "capability %s\n", name);
79}
80
81void credential_announce_capabilities(struct credential *c, FILE *fp) {
82fprintf(fp, "version 0\n");
83announce_one(&c->capa_authtype, "authtype", fp);
84announce_one(&c->capa_state, "state", fp);
85}
86
87int credential_match(const struct credential *want,
88const struct credential *have, int match_password)
89{
90#define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
91return CHECK(protocol) &&
92CHECK(host) &&
93CHECK(path) &&
94CHECK(username) &&
95(!match_password || CHECK(password)) &&
96(!match_password || CHECK(credential));
97#undef CHECK
98}
99
100
101static int credential_from_potentially_partial_url(struct credential *c,
102const char *url);
103
104static int credential_config_callback(const char *var, const char *value,
105const struct config_context *ctx UNUSED,
106void *data)
107{
108struct credential *c = data;
109const char *key;
110
111if (!skip_prefix(var, "credential.", &key))
112return 0;
113
114if (!value)
115return config_error_nonbool(var);
116
117if (!strcmp(key, "helper")) {
118if (*value)
119string_list_append(&c->helpers, value);
120else
121string_list_clear(&c->helpers, 0);
122} else if (!strcmp(key, "username")) {
123if (!c->username_from_proto) {
124free(c->username);
125c->username = xstrdup(value);
126}
127}
128else if (!strcmp(key, "usehttppath"))
129c->use_http_path = git_config_bool(var, value);
130
131return 0;
132}
133
134static int proto_is_http(const char *s)
135{
136if (!s)
137return 0;
138return !strcmp(s, "https") || !strcmp(s, "http");
139}
140
141static void credential_describe(struct credential *c, struct strbuf *out);
142static void credential_format(struct credential *c, struct strbuf *out);
143
144static int select_all(const struct urlmatch_item *a UNUSED,
145const struct urlmatch_item *b UNUSED)
146{
147return 0;
148}
149
150static int match_partial_url(const char *url, void *cb)
151{
152struct credential *c = cb;
153struct credential want = CREDENTIAL_INIT;
154int matches = 0;
155
156if (credential_from_potentially_partial_url(&want, url) < 0)
157warning(_("skipping credential lookup for key: credential.%s"),
158url);
159else
160matches = credential_match(&want, c, 0);
161credential_clear(&want);
162
163return matches;
164}
165
166static void credential_apply_config(struct credential *c)
167{
168char *normalized_url;
169struct urlmatch_config config = URLMATCH_CONFIG_INIT;
170struct strbuf url = STRBUF_INIT;
171
172if (!c->host)
173die(_("refusing to work with credential missing host field"));
174if (!c->protocol)
175die(_("refusing to work with credential missing protocol field"));
176
177if (c->configured)
178return;
179
180config.section = "credential";
181config.key = NULL;
182config.collect_fn = credential_config_callback;
183config.cascade_fn = NULL;
184config.select_fn = select_all;
185config.fallback_match_fn = match_partial_url;
186config.cb = c;
187
188credential_format(c, &url);
189normalized_url = url_normalize(url.buf, &config.url);
190
191git_config(urlmatch_config_entry, &config);
192string_list_clear(&config.vars, 1);
193free(normalized_url);
194urlmatch_config_release(&config);
195strbuf_release(&url);
196
197c->configured = 1;
198
199if (!c->use_http_path && proto_is_http(c->protocol)) {
200FREE_AND_NULL(c->path);
201}
202}
203
204static void credential_describe(struct credential *c, struct strbuf *out)
205{
206if (!c->protocol)
207return;
208strbuf_addf(out, "%s://", c->protocol);
209if (c->username && *c->username)
210strbuf_addf(out, "%s@", c->username);
211if (c->host)
212strbuf_addstr(out, c->host);
213if (c->path)
214strbuf_addf(out, "/%s", c->path);
215}
216
217static void credential_format(struct credential *c, struct strbuf *out)
218{
219if (!c->protocol)
220return;
221strbuf_addf(out, "%s://", c->protocol);
222if (c->username && *c->username) {
223strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
224strbuf_addch(out, '@');
225}
226if (c->host)
227strbuf_addstr(out, c->host);
228if (c->path) {
229strbuf_addch(out, '/');
230strbuf_add_percentencode(out, c->path, 0);
231}
232}
233
234static char *credential_ask_one(const char *what, struct credential *c,
235int flags)
236{
237struct strbuf desc = STRBUF_INIT;
238struct strbuf prompt = STRBUF_INIT;
239char *r;
240
241credential_describe(c, &desc);
242if (desc.len)
243strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
244else
245strbuf_addf(&prompt, "%s: ", what);
246
247r = git_prompt(prompt.buf, flags);
248
249strbuf_release(&desc);
250strbuf_release(&prompt);
251return xstrdup(r);
252}
253
254static void credential_getpass(struct credential *c)
255{
256if (!c->username)
257c->username = credential_ask_one("Username", c,
258PROMPT_ASKPASS|PROMPT_ECHO);
259if (!c->password)
260c->password = credential_ask_one("Password", c,
261PROMPT_ASKPASS);
262}
263
264int credential_has_capability(const struct credential_capability *capa,
265enum credential_op_type op_type)
266{
267/*
268* We're checking here if each previous step indicated that we had the
269* capability. If it did, then we want to pass it along; conversely, if
270* it did not, we don't want to report that to our caller.
271*/
272switch (op_type) {
273case CREDENTIAL_OP_HELPER:
274return capa->request_initial;
275case CREDENTIAL_OP_RESPONSE:
276return capa->request_initial && capa->request_helper;
277default:
278return 0;
279}
280}
281
282int credential_read(struct credential *c, FILE *fp,
283enum credential_op_type op_type)
284{
285struct strbuf line = STRBUF_INIT;
286
287while (strbuf_getline(&line, fp) != EOF) {
288char *key = line.buf;
289char *value = strchr(key, '=');
290
291if (!line.len)
292break;
293
294if (!value) {
295warning("invalid credential line: %s", key);
296strbuf_release(&line);
297return -1;
298}
299*value++ = '\0';
300
301if (!strcmp(key, "username")) {
302free(c->username);
303c->username = xstrdup(value);
304c->username_from_proto = 1;
305} else if (!strcmp(key, "password")) {
306free(c->password);
307c->password = xstrdup(value);
308} else if (!strcmp(key, "credential")) {
309free(c->credential);
310c->credential = xstrdup(value);
311} else if (!strcmp(key, "protocol")) {
312free(c->protocol);
313c->protocol = xstrdup(value);
314} else if (!strcmp(key, "host")) {
315free(c->host);
316c->host = xstrdup(value);
317} else if (!strcmp(key, "path")) {
318free(c->path);
319c->path = xstrdup(value);
320} else if (!strcmp(key, "ephemeral")) {
321c->ephemeral = !!git_config_bool("ephemeral", value);
322} else if (!strcmp(key, "wwwauth[]")) {
323strvec_push(&c->wwwauth_headers, value);
324} else if (!strcmp(key, "state[]")) {
325strvec_push(&c->state_headers, value);
326} else if (!strcmp(key, "capability[]")) {
327if (!strcmp(value, "authtype"))
328credential_set_capability(&c->capa_authtype, op_type);
329else if (!strcmp(value, "state"))
330credential_set_capability(&c->capa_state, op_type);
331} else if (!strcmp(key, "continue")) {
332c->multistage = !!git_config_bool("continue", value);
333} else if (!strcmp(key, "password_expiry_utc")) {
334errno = 0;
335c->password_expiry_utc = parse_timestamp(value, NULL, 10);
336if (c->password_expiry_utc == 0 || errno == ERANGE)
337c->password_expiry_utc = TIME_MAX;
338} else if (!strcmp(key, "oauth_refresh_token")) {
339free(c->oauth_refresh_token);
340c->oauth_refresh_token = xstrdup(value);
341} else if (!strcmp(key, "authtype")) {
342free(c->authtype);
343c->authtype = xstrdup(value);
344} else if (!strcmp(key, "url")) {
345credential_from_url(c, value);
346} else if (!strcmp(key, "quit")) {
347c->quit = !!git_config_bool("quit", value);
348}
349/*
350* Ignore other lines; we don't know what they mean, but
351* this future-proofs us when later versions of git do
352* learn new lines, and the helpers are updated to match.
353*/
354}
355
356strbuf_release(&line);
357return 0;
358}
359
360static void credential_write_item(FILE *fp, const char *key, const char *value,
361int required)
362{
363if (!value && required)
364BUG("credential value for %s is missing", key);
365if (!value)
366return;
367if (strchr(value, '\n'))
368die("credential value for %s contains newline", key);
369fprintf(fp, "%s=%s\n", key, value);
370}
371
372void credential_write(const struct credential *c, FILE *fp,
373enum credential_op_type op_type)
374{
375if (credential_has_capability(&c->capa_authtype, op_type))
376credential_write_item(fp, "capability[]", "authtype", 0);
377if (credential_has_capability(&c->capa_state, op_type))
378credential_write_item(fp, "capability[]", "state", 0);
379
380if (credential_has_capability(&c->capa_authtype, op_type)) {
381credential_write_item(fp, "authtype", c->authtype, 0);
382credential_write_item(fp, "credential", c->credential, 0);
383if (c->ephemeral)
384credential_write_item(fp, "ephemeral", "1", 0);
385}
386credential_write_item(fp, "protocol", c->protocol, 1);
387credential_write_item(fp, "host", c->host, 1);
388credential_write_item(fp, "path", c->path, 0);
389credential_write_item(fp, "username", c->username, 0);
390credential_write_item(fp, "password", c->password, 0);
391credential_write_item(fp, "oauth_refresh_token", c->oauth_refresh_token, 0);
392if (c->password_expiry_utc != TIME_MAX) {
393char *s = xstrfmt("%"PRItime, c->password_expiry_utc);
394credential_write_item(fp, "password_expiry_utc", s, 0);
395free(s);
396}
397for (size_t i = 0; i < c->wwwauth_headers.nr; i++)
398credential_write_item(fp, "wwwauth[]", c->wwwauth_headers.v[i], 0);
399if (credential_has_capability(&c->capa_state, op_type)) {
400if (c->multistage)
401credential_write_item(fp, "continue", "1", 0);
402for (size_t i = 0; i < c->state_headers_to_send.nr; i++)
403credential_write_item(fp, "state[]", c->state_headers_to_send.v[i], 0);
404}
405}
406
407static int run_credential_helper(struct credential *c,
408const char *cmd,
409int want_output)
410{
411struct child_process helper = CHILD_PROCESS_INIT;
412FILE *fp;
413
414strvec_push(&helper.args, cmd);
415helper.use_shell = 1;
416helper.in = -1;
417if (want_output)
418helper.out = -1;
419else
420helper.no_stdout = 1;
421
422if (start_command(&helper) < 0)
423return -1;
424
425fp = xfdopen(helper.in, "w");
426sigchain_push(SIGPIPE, SIG_IGN);
427credential_write(c, fp, want_output ? CREDENTIAL_OP_HELPER : CREDENTIAL_OP_RESPONSE);
428fclose(fp);
429sigchain_pop(SIGPIPE);
430
431if (want_output) {
432int r;
433fp = xfdopen(helper.out, "r");
434r = credential_read(c, fp, CREDENTIAL_OP_HELPER);
435fclose(fp);
436if (r < 0) {
437finish_command(&helper);
438return -1;
439}
440}
441
442if (finish_command(&helper))
443return -1;
444return 0;
445}
446
447static int credential_do(struct credential *c, const char *helper,
448const char *operation)
449{
450struct strbuf cmd = STRBUF_INIT;
451int r;
452
453if (helper[0] == '!')
454strbuf_addstr(&cmd, helper + 1);
455else if (is_absolute_path(helper))
456strbuf_addstr(&cmd, helper);
457else
458strbuf_addf(&cmd, "git credential-%s", helper);
459
460strbuf_addf(&cmd, " %s", operation);
461r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
462
463strbuf_release(&cmd);
464return r;
465}
466
467void credential_fill(struct credential *c, int all_capabilities)
468{
469int i;
470
471if ((c->username && c->password) || c->credential)
472return;
473
474credential_next_state(c);
475c->multistage = 0;
476
477credential_apply_config(c);
478if (all_capabilities)
479credential_set_all_capabilities(c, CREDENTIAL_OP_INITIAL);
480
481for (i = 0; i < c->helpers.nr; i++) {
482credential_do(c, c->helpers.items[i].string, "get");
483
484if (c->password_expiry_utc < time(NULL)) {
485/*
486* Don't use credential_clear() here: callers such as
487* cmd_credential() expect to still be able to call
488* credential_write() on a struct credential whose
489* secrets have expired.
490*/
491credential_clear_secrets(c);
492/* Reset expiry to maintain consistency */
493c->password_expiry_utc = TIME_MAX;
494}
495if ((c->username && c->password) || c->credential) {
496strvec_clear(&c->wwwauth_headers);
497return;
498}
499if (c->quit)
500die("credential helper '%s' told us to quit",
501c->helpers.items[i].string);
502}
503
504credential_getpass(c);
505if (!c->username && !c->password && !c->credential)
506die("unable to get password from user");
507}
508
509void credential_approve(struct credential *c)
510{
511int i;
512
513if (c->approved)
514return;
515if (((!c->username || !c->password) && !c->credential) || c->password_expiry_utc < time(NULL))
516return;
517
518credential_next_state(c);
519
520credential_apply_config(c);
521
522for (i = 0; i < c->helpers.nr; i++)
523credential_do(c, c->helpers.items[i].string, "store");
524c->approved = 1;
525}
526
527void credential_reject(struct credential *c)
528{
529int i;
530
531credential_next_state(c);
532
533credential_apply_config(c);
534
535for (i = 0; i < c->helpers.nr; i++)
536credential_do(c, c->helpers.items[i].string, "erase");
537
538credential_clear_secrets(c);
539FREE_AND_NULL(c->username);
540FREE_AND_NULL(c->oauth_refresh_token);
541c->password_expiry_utc = TIME_MAX;
542c->approved = 0;
543}
544
545static int check_url_component(const char *url, int quiet,
546const char *name, const char *value)
547{
548if (!value)
549return 0;
550if (!strchr(value, '\n'))
551return 0;
552
553if (!quiet)
554warning(_("url contains a newline in its %s component: %s"),
555name, url);
556return -1;
557}
558
559/*
560* Potentially-partial URLs can, but do not have to, contain
561*
562* - a protocol (or scheme) of the form "<protocol>://"
563*
564* - a host name (the part after the protocol and before the first slash after
565* that, if any)
566*
567* - a user name and potentially a password (as "<user>[:<password>]@" part of
568* the host name)
569*
570* - a path (the part after the host name, if any, starting with the slash)
571*
572* Missing parts will be left unset in `struct credential`. Thus, `https://`
573* will have only the `protocol` set, `example.com` only the host name, and
574* `/git` only the path.
575*
576* Note that an empty host name in an otherwise fully-qualified URL (e.g.
577* `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
578* be potentially partial, and only then (otherwise, the empty string is used).
579*
580* The credential_from_url() function does not allow partial URLs.
581*/
582static int credential_from_url_1(struct credential *c, const char *url,
583int allow_partial_url, int quiet)
584{
585const char *at, *colon, *cp, *slash, *host, *proto_end;
586
587credential_clear(c);
588
589/*
590* Match one of:
591* (1) proto://<host>/...
592* (2) proto://<user>@<host>/...
593* (3) proto://<user>:<pass>@<host>/...
594*/
595proto_end = strstr(url, "://");
596if (!allow_partial_url && (!proto_end || proto_end == url)) {
597if (!quiet)
598warning(_("url has no scheme: %s"), url);
599return -1;
600}
601cp = proto_end ? proto_end + 3 : url;
602at = strchr(cp, '@');
603colon = strchr(cp, ':');
604
605/*
606* A query or fragment marker before the slash ends the host portion.
607* We'll just continue to call this "slash" for simplicity. Notably our
608* "trim leading slashes" part won't skip over this part of the path,
609* but that's what we'd want.
610*/
611slash = cp + strcspn(cp, "/?#");
612
613if (!at || slash <= at) {
614/* Case (1) */
615host = cp;
616}
617else if (!colon || at <= colon) {
618/* Case (2) */
619c->username = url_decode_mem(cp, at - cp);
620if (c->username && *c->username)
621c->username_from_proto = 1;
622host = at + 1;
623} else {
624/* Case (3) */
625c->username = url_decode_mem(cp, colon - cp);
626if (c->username && *c->username)
627c->username_from_proto = 1;
628c->password = url_decode_mem(colon + 1, at - (colon + 1));
629host = at + 1;
630}
631
632if (proto_end && proto_end - url > 0)
633c->protocol = xmemdupz(url, proto_end - url);
634if (!allow_partial_url || slash - host > 0)
635c->host = url_decode_mem(host, slash - host);
636/* Trim leading and trailing slashes from path */
637while (*slash == '/')
638slash++;
639if (*slash) {
640char *p;
641c->path = url_decode(slash);
642p = c->path + strlen(c->path) - 1;
643while (p > c->path && *p == '/')
644*p-- = '\0';
645}
646
647if (check_url_component(url, quiet, "username", c->username) < 0 ||
648check_url_component(url, quiet, "password", c->password) < 0 ||
649check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
650check_url_component(url, quiet, "host", c->host) < 0 ||
651check_url_component(url, quiet, "path", c->path) < 0)
652return -1;
653
654return 0;
655}
656
657static int credential_from_potentially_partial_url(struct credential *c,
658const char *url)
659{
660return credential_from_url_1(c, url, 1, 0);
661}
662
663int credential_from_url_gently(struct credential *c, const char *url, int quiet)
664{
665return credential_from_url_1(c, url, 0, quiet);
666}
667
668void credential_from_url(struct credential *c, const char *url)
669{
670if (credential_from_url_gently(c, url, 0) < 0)
671die(_("credential url cannot be parsed: %s"), url);
672}
673