1 #include "git-compat-util.h"
4 #include "credential.h"
6 #include "string-list.h"
7 #include "run-command.h"
13 #include "git-compat-util.h"
15 void credential_init(struct credential
*c
)
17 struct credential blank
= CREDENTIAL_INIT
;
18 memcpy(c
, &blank
, sizeof(*c
));
21 void credential_clear(struct credential
*c
)
29 free(c
->oauth_refresh_token
);
31 string_list_clear(&c
->helpers
, 0);
32 strvec_clear(&c
->wwwauth_headers
);
33 strvec_clear(&c
->state_headers
);
38 static void credential_set_capability(struct credential_capability
*capa
,
39 enum credential_op_type op_type
)
42 case CREDENTIAL_OP_INITIAL
:
43 capa
->request_initial
= 1;
45 case CREDENTIAL_OP_HELPER
:
46 capa
->request_helper
= 1;
48 case CREDENTIAL_OP_RESPONSE
:
55 void credential_set_all_capabilities(struct credential
*c
,
56 enum credential_op_type op_type
)
58 credential_set_capability(&c
->capa_authtype
, op_type
);
61 int credential_match(const struct credential
*want
,
62 const struct credential
*have
, int match_password
)
64 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
65 return CHECK(protocol
) &&
69 (!match_password
|| CHECK(password
));
74 static int credential_from_potentially_partial_url(struct credential
*c
,
77 static int credential_config_callback(const char *var
, const char *value
,
78 const struct config_context
*ctx UNUSED
,
81 struct credential
*c
= data
;
84 if (!skip_prefix(var
, "credential.", &key
))
88 return config_error_nonbool(var
);
90 if (!strcmp(key
, "helper")) {
92 string_list_append(&c
->helpers
, value
);
94 string_list_clear(&c
->helpers
, 0);
95 } else if (!strcmp(key
, "username")) {
96 if (!c
->username_from_proto
) {
98 c
->username
= xstrdup(value
);
101 else if (!strcmp(key
, "usehttppath"))
102 c
->use_http_path
= git_config_bool(var
, value
);
107 static int proto_is_http(const char *s
)
111 return !strcmp(s
, "https") || !strcmp(s
, "http");
114 static void credential_describe(struct credential
*c
, struct strbuf
*out
);
115 static void credential_format(struct credential
*c
, struct strbuf
*out
);
117 static int select_all(const struct urlmatch_item
*a UNUSED
,
118 const struct urlmatch_item
*b UNUSED
)
123 static int match_partial_url(const char *url
, void *cb
)
125 struct credential
*c
= cb
;
126 struct credential want
= CREDENTIAL_INIT
;
129 if (credential_from_potentially_partial_url(&want
, url
) < 0)
130 warning(_("skipping credential lookup for key: credential.%s"),
133 matches
= credential_match(&want
, c
, 0);
134 credential_clear(&want
);
139 static void credential_apply_config(struct credential
*c
)
141 char *normalized_url
;
142 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
143 struct strbuf url
= STRBUF_INIT
;
146 die(_("refusing to work with credential missing host field"));
148 die(_("refusing to work with credential missing protocol field"));
153 config
.section
= "credential";
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
;
161 credential_format(c
, &url
);
162 normalized_url
= url_normalize(url
.buf
, &config
.url
);
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
);
172 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
173 FREE_AND_NULL(c
->path
);
177 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
181 strbuf_addf(out
, "%s://", c
->protocol
);
182 if (c
->username
&& *c
->username
)
183 strbuf_addf(out
, "%s@", c
->username
);
185 strbuf_addstr(out
, c
->host
);
187 strbuf_addf(out
, "/%s", c
->path
);
190 static void credential_format(struct credential
*c
, struct strbuf
*out
)
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
, '@');
200 strbuf_addstr(out
, c
->host
);
202 strbuf_addch(out
, '/');
203 strbuf_add_percentencode(out
, c
->path
, 0);
207 static char *credential_ask_one(const char *what
, struct credential
*c
,
210 struct strbuf desc
= STRBUF_INIT
;
211 struct strbuf prompt
= STRBUF_INIT
;
214 credential_describe(c
, &desc
);
216 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
218 strbuf_addf(&prompt
, "%s: ", what
);
220 r
= git_prompt(prompt
.buf
, flags
);
222 strbuf_release(&desc
);
223 strbuf_release(&prompt
);
227 static void credential_getpass(struct credential
*c
)
230 c
->username
= credential_ask_one("Username", c
,
231 PROMPT_ASKPASS
|PROMPT_ECHO
);
233 c
->password
= credential_ask_one("Password", c
,
237 static int credential_has_capability(const struct credential_capability
*capa
,
238 enum credential_op_type op_type
)
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.
246 case CREDENTIAL_OP_HELPER
:
247 return capa
->request_initial
;
248 case CREDENTIAL_OP_RESPONSE
:
249 return capa
->request_initial
&& capa
->request_helper
;
255 int credential_read(struct credential
*c
, FILE *fp
,
256 enum credential_op_type op_type
)
258 struct strbuf line
= STRBUF_INIT
;
260 while (strbuf_getline(&line
, fp
) != EOF
) {
261 char *key
= line
.buf
;
262 char *value
= strchr(key
, '=');
268 warning("invalid credential line: %s", key
);
269 strbuf_release(&line
);
274 if (!strcmp(key
, "username")) {
276 c
->username
= xstrdup(value
);
277 c
->username_from_proto
= 1;
278 } else if (!strcmp(key
, "password")) {
280 c
->password
= xstrdup(value
);
281 } else if (!strcmp(key
, "credential")) {
283 c
->credential
= xstrdup(value
);
284 } else if (!strcmp(key
, "protocol")) {
286 c
->protocol
= xstrdup(value
);
287 } else if (!strcmp(key
, "host")) {
289 c
->host
= xstrdup(value
);
290 } else if (!strcmp(key
, "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")) {
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")) {
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
);
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.
327 strbuf_release(&line
);
331 static void credential_write_item(FILE *fp
, const char *key
, const char *value
,
334 if (!value
&& required
)
335 BUG("credential value for %s is missing", key
);
338 if (strchr(value
, '\n'))
339 die("credential value for %s contains newline", key
);
340 fprintf(fp
, "%s=%s\n", key
, value
);
343 void credential_write(const struct credential
*c
, FILE *fp
,
344 enum credential_op_type op_type
)
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);
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);
355 credential_write_item(fp
, "ephemeral", "1", 0);
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);
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);
376 static int run_credential_helper(struct credential
*c
,
380 struct child_process helper
= CHILD_PROCESS_INIT
;
383 strvec_push(&helper
.args
, cmd
);
384 helper
.use_shell
= 1;
389 helper
.no_stdout
= 1;
391 if (start_command(&helper
) < 0)
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
);
398 sigchain_pop(SIGPIPE
);
402 fp
= xfdopen(helper
.out
, "r");
403 r
= credential_read(c
, fp
, CREDENTIAL_OP_HELPER
);
406 finish_command(&helper
);
411 if (finish_command(&helper
))
416 static int credential_do(struct credential
*c
, const char *helper
,
417 const char *operation
)
419 struct strbuf cmd
= STRBUF_INIT
;
422 if (helper
[0] == '!')
423 strbuf_addstr(&cmd
, helper
+ 1);
424 else if (is_absolute_path(helper
))
425 strbuf_addstr(&cmd
, helper
);
427 strbuf_addf(&cmd
, "git credential-%s", helper
);
429 strbuf_addf(&cmd
, " %s", operation
);
430 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
432 strbuf_release(&cmd
);
436 void credential_fill(struct credential
*c
, int all_capabilities
)
440 if ((c
->username
&& c
->password
) || c
->credential
)
443 credential_apply_config(c
);
444 if (all_capabilities
)
445 credential_set_all_capabilities(c
, CREDENTIAL_OP_INITIAL
);
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
;
455 if ((c
->username
&& c
->password
) || c
->credential
)
458 die("credential helper '%s' told us to quit",
459 c
->helpers
.items
[i
].string
);
462 credential_getpass(c
);
463 if (!c
->username
&& !c
->password
&& !c
->credential
)
464 die("unable to get password from user");
467 void credential_approve(struct credential
*c
)
473 if (((!c
->username
|| !c
->password
) && !c
->credential
) || c
->password_expiry_utc
< time(NULL
))
476 credential_apply_config(c
);
478 for (i
= 0; i
< c
->helpers
.nr
; i
++)
479 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
483 void credential_reject(struct credential
*c
)
487 credential_apply_config(c
);
489 for (i
= 0; i
< c
->helpers
.nr
; i
++)
490 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
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
;
500 static int check_url_component(const char *url
, int quiet
,
501 const char *name
, const char *value
)
505 if (!strchr(value
, '\n'))
509 warning(_("url contains a newline in its %s component: %s"),
515 * Potentially-partial URLs can, but do not have to, contain
517 * - a protocol (or scheme) of the form "<protocol>://"
519 * - a host name (the part after the protocol and before the first slash after
522 * - a user name and potentially a password (as "<user>[:<password>]@" part of
525 * - a path (the part after the host name, if any, starting with the slash)
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.
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).
535 * The credential_from_url() function does not allow partial URLs.
537 static int credential_from_url_1(struct credential
*c
, const char *url
,
538 int allow_partial_url
, int quiet
)
540 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
546 * (1) proto://<host>/...
547 * (2) proto://<user>@<host>/...
548 * (3) proto://<user>:<pass>@<host>/...
550 proto_end
= strstr(url
, "://");
551 if (!allow_partial_url
&& (!proto_end
|| proto_end
== url
)) {
553 warning(_("url has no scheme: %s"), url
);
556 cp
= proto_end
? proto_end
+ 3 : url
;
557 at
= strchr(cp
, '@');
558 colon
= strchr(cp
, ':');
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.
566 slash
= cp
+ strcspn(cp
, "/?#");
568 if (!at
|| slash
<= at
) {
572 else if (!colon
|| at
<= colon
) {
574 c
->username
= url_decode_mem(cp
, at
- cp
);
575 if (c
->username
&& *c
->username
)
576 c
->username_from_proto
= 1;
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));
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
== '/')
596 c
->path
= url_decode(slash
);
597 p
= c
->path
+ strlen(c
->path
) - 1;
598 while (p
> c
->path
&& *p
== '/')
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)
612 static int credential_from_potentially_partial_url(struct credential
*c
,
615 return credential_from_url_1(c
, url
, 1, 0);
618 int credential_from_url_gently(struct credential
*c
, const char *url
, int quiet
)
620 return credential_from_url_1(c
, url
, 0, quiet
);
623 void credential_from_url(struct credential
*c
, const char *url
)
625 if (credential_from_url_gently(c
, url
, 0) < 0)
626 die(_("credential url cannot be parsed: %s"), url
);