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