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
)
28 free(c
->oauth_refresh_token
);
29 string_list_clear(&c
->helpers
, 0);
30 strvec_clear(&c
->wwwauth_headers
);
35 int credential_match(const struct credential
*want
,
36 const struct credential
*have
, int match_password
)
38 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
39 return CHECK(protocol
) &&
43 (!match_password
|| CHECK(password
));
48 static int credential_from_potentially_partial_url(struct credential
*c
,
51 static int credential_config_callback(const char *var
, const char *value
,
52 const struct config_context
*ctx UNUSED
,
55 struct credential
*c
= data
;
58 if (!skip_prefix(var
, "credential.", &key
))
62 return config_error_nonbool(var
);
64 if (!strcmp(key
, "helper")) {
66 string_list_append(&c
->helpers
, value
);
68 string_list_clear(&c
->helpers
, 0);
69 } else if (!strcmp(key
, "username")) {
70 if (!c
->username_from_proto
) {
72 c
->username
= xstrdup(value
);
75 else if (!strcmp(key
, "usehttppath"))
76 c
->use_http_path
= git_config_bool(var
, value
);
81 static int proto_is_http(const char *s
)
85 return !strcmp(s
, "https") || !strcmp(s
, "http");
88 static void credential_describe(struct credential
*c
, struct strbuf
*out
);
89 static void credential_format(struct credential
*c
, struct strbuf
*out
);
91 static int select_all(const struct urlmatch_item
*a UNUSED
,
92 const struct urlmatch_item
*b UNUSED
)
97 static int match_partial_url(const char *url
, void *cb
)
99 struct credential
*c
= cb
;
100 struct credential want
= CREDENTIAL_INIT
;
103 if (credential_from_potentially_partial_url(&want
, url
) < 0)
104 warning(_("skipping credential lookup for key: credential.%s"),
107 matches
= credential_match(&want
, c
, 0);
108 credential_clear(&want
);
113 static void credential_apply_config(struct credential
*c
)
115 char *normalized_url
;
116 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
117 struct strbuf url
= STRBUF_INIT
;
120 die(_("refusing to work with credential missing host field"));
122 die(_("refusing to work with credential missing protocol field"));
127 config
.section
= "credential";
129 config
.collect_fn
= credential_config_callback
;
130 config
.cascade_fn
= NULL
;
131 config
.select_fn
= select_all
;
132 config
.fallback_match_fn
= match_partial_url
;
135 credential_format(c
, &url
);
136 normalized_url
= url_normalize(url
.buf
, &config
.url
);
138 git_config(urlmatch_config_entry
, &config
);
139 string_list_clear(&config
.vars
, 1);
140 free(normalized_url
);
141 urlmatch_config_release(&config
);
142 strbuf_release(&url
);
146 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
147 FREE_AND_NULL(c
->path
);
151 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
155 strbuf_addf(out
, "%s://", c
->protocol
);
156 if (c
->username
&& *c
->username
)
157 strbuf_addf(out
, "%s@", c
->username
);
159 strbuf_addstr(out
, c
->host
);
161 strbuf_addf(out
, "/%s", c
->path
);
164 static void credential_format(struct credential
*c
, struct strbuf
*out
)
168 strbuf_addf(out
, "%s://", c
->protocol
);
169 if (c
->username
&& *c
->username
) {
170 strbuf_add_percentencode(out
, c
->username
, STRBUF_ENCODE_SLASH
);
171 strbuf_addch(out
, '@');
174 strbuf_addstr(out
, c
->host
);
176 strbuf_addch(out
, '/');
177 strbuf_add_percentencode(out
, c
->path
, 0);
181 static char *credential_ask_one(const char *what
, struct credential
*c
,
184 struct strbuf desc
= STRBUF_INIT
;
185 struct strbuf prompt
= STRBUF_INIT
;
188 credential_describe(c
, &desc
);
190 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
192 strbuf_addf(&prompt
, "%s: ", what
);
194 r
= git_prompt(prompt
.buf
, flags
);
196 strbuf_release(&desc
);
197 strbuf_release(&prompt
);
201 static void credential_getpass(struct credential
*c
)
204 c
->username
= credential_ask_one("Username", c
,
205 PROMPT_ASKPASS
|PROMPT_ECHO
);
207 c
->password
= credential_ask_one("Password", c
,
211 int credential_read(struct credential
*c
, FILE *fp
)
213 struct strbuf line
= STRBUF_INIT
;
215 while (strbuf_getline(&line
, fp
) != EOF
) {
216 char *key
= line
.buf
;
217 char *value
= strchr(key
, '=');
223 warning("invalid credential line: %s", key
);
224 strbuf_release(&line
);
229 if (!strcmp(key
, "username")) {
231 c
->username
= xstrdup(value
);
232 c
->username_from_proto
= 1;
233 } else if (!strcmp(key
, "password")) {
235 c
->password
= xstrdup(value
);
236 } else if (!strcmp(key
, "protocol")) {
238 c
->protocol
= xstrdup(value
);
239 } else if (!strcmp(key
, "host")) {
241 c
->host
= xstrdup(value
);
242 } else if (!strcmp(key
, "path")) {
244 c
->path
= xstrdup(value
);
245 } else if (!strcmp(key
, "wwwauth[]")) {
246 strvec_push(&c
->wwwauth_headers
, value
);
247 } else if (!strcmp(key
, "password_expiry_utc")) {
249 c
->password_expiry_utc
= parse_timestamp(value
, NULL
, 10);
250 if (c
->password_expiry_utc
== 0 || errno
== ERANGE
)
251 c
->password_expiry_utc
= TIME_MAX
;
252 } else if (!strcmp(key
, "oauth_refresh_token")) {
253 free(c
->oauth_refresh_token
);
254 c
->oauth_refresh_token
= xstrdup(value
);
255 } else if (!strcmp(key
, "url")) {
256 credential_from_url(c
, value
);
257 } else if (!strcmp(key
, "quit")) {
258 c
->quit
= !!git_config_bool("quit", value
);
261 * Ignore other lines; we don't know what they mean, but
262 * this future-proofs us when later versions of git do
263 * learn new lines, and the helpers are updated to match.
267 strbuf_release(&line
);
271 static void credential_write_item(FILE *fp
, const char *key
, const char *value
,
274 if (!value
&& required
)
275 BUG("credential value for %s is missing", key
);
278 if (strchr(value
, '\n'))
279 die("credential value for %s contains newline", key
);
280 fprintf(fp
, "%s=%s\n", key
, value
);
283 void credential_write(const struct credential
*c
, FILE *fp
)
285 credential_write_item(fp
, "protocol", c
->protocol
, 1);
286 credential_write_item(fp
, "host", c
->host
, 1);
287 credential_write_item(fp
, "path", c
->path
, 0);
288 credential_write_item(fp
, "username", c
->username
, 0);
289 credential_write_item(fp
, "password", c
->password
, 0);
290 credential_write_item(fp
, "oauth_refresh_token", c
->oauth_refresh_token
, 0);
291 if (c
->password_expiry_utc
!= TIME_MAX
) {
292 char *s
= xstrfmt("%"PRItime
, c
->password_expiry_utc
);
293 credential_write_item(fp
, "password_expiry_utc", s
, 0);
296 for (size_t i
= 0; i
< c
->wwwauth_headers
.nr
; i
++)
297 credential_write_item(fp
, "wwwauth[]", c
->wwwauth_headers
.v
[i
], 0);
300 static int run_credential_helper(struct credential
*c
,
304 struct child_process helper
= CHILD_PROCESS_INIT
;
307 strvec_push(&helper
.args
, cmd
);
308 helper
.use_shell
= 1;
313 helper
.no_stdout
= 1;
315 if (start_command(&helper
) < 0)
318 fp
= xfdopen(helper
.in
, "w");
319 sigchain_push(SIGPIPE
, SIG_IGN
);
320 credential_write(c
, fp
);
322 sigchain_pop(SIGPIPE
);
326 fp
= xfdopen(helper
.out
, "r");
327 r
= credential_read(c
, fp
);
330 finish_command(&helper
);
335 if (finish_command(&helper
))
340 static int credential_do(struct credential
*c
, const char *helper
,
341 const char *operation
)
343 struct strbuf cmd
= STRBUF_INIT
;
346 if (helper
[0] == '!')
347 strbuf_addstr(&cmd
, helper
+ 1);
348 else if (is_absolute_path(helper
))
349 strbuf_addstr(&cmd
, helper
);
351 strbuf_addf(&cmd
, "git credential-%s", helper
);
353 strbuf_addf(&cmd
, " %s", operation
);
354 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
356 strbuf_release(&cmd
);
360 void credential_fill(struct credential
*c
)
364 if (c
->username
&& c
->password
)
367 credential_apply_config(c
);
369 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
370 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
371 if (c
->password_expiry_utc
< time(NULL
)) {
372 /* Discard expired password */
373 FREE_AND_NULL(c
->password
);
374 /* Reset expiry to maintain consistency */
375 c
->password_expiry_utc
= TIME_MAX
;
377 if (c
->username
&& c
->password
)
380 die("credential helper '%s' told us to quit",
381 c
->helpers
.items
[i
].string
);
384 credential_getpass(c
);
385 if (!c
->username
&& !c
->password
)
386 die("unable to get password from user");
389 void credential_approve(struct credential
*c
)
395 if (!c
->username
|| !c
->password
|| c
->password_expiry_utc
< time(NULL
))
398 credential_apply_config(c
);
400 for (i
= 0; i
< c
->helpers
.nr
; i
++)
401 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
405 void credential_reject(struct credential
*c
)
409 credential_apply_config(c
);
411 for (i
= 0; i
< c
->helpers
.nr
; i
++)
412 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
414 FREE_AND_NULL(c
->username
);
415 FREE_AND_NULL(c
->password
);
416 FREE_AND_NULL(c
->oauth_refresh_token
);
417 c
->password_expiry_utc
= TIME_MAX
;
421 static int check_url_component(const char *url
, int quiet
,
422 const char *name
, const char *value
)
426 if (!strchr(value
, '\n'))
430 warning(_("url contains a newline in its %s component: %s"),
436 * Potentially-partial URLs can, but do not have to, contain
438 * - a protocol (or scheme) of the form "<protocol>://"
440 * - a host name (the part after the protocol and before the first slash after
443 * - a user name and potentially a password (as "<user>[:<password>]@" part of
446 * - a path (the part after the host name, if any, starting with the slash)
448 * Missing parts will be left unset in `struct credential`. Thus, `https://`
449 * will have only the `protocol` set, `example.com` only the host name, and
450 * `/git` only the path.
452 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
453 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
454 * be potentially partial, and only then (otherwise, the empty string is used).
456 * The credential_from_url() function does not allow partial URLs.
458 static int credential_from_url_1(struct credential
*c
, const char *url
,
459 int allow_partial_url
, int quiet
)
461 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
467 * (1) proto://<host>/...
468 * (2) proto://<user>@<host>/...
469 * (3) proto://<user>:<pass>@<host>/...
471 proto_end
= strstr(url
, "://");
472 if (!allow_partial_url
&& (!proto_end
|| proto_end
== url
)) {
474 warning(_("url has no scheme: %s"), url
);
477 cp
= proto_end
? proto_end
+ 3 : url
;
478 at
= strchr(cp
, '@');
479 colon
= strchr(cp
, ':');
482 * A query or fragment marker before the slash ends the host portion.
483 * We'll just continue to call this "slash" for simplicity. Notably our
484 * "trim leading slashes" part won't skip over this part of the path,
485 * but that's what we'd want.
487 slash
= cp
+ strcspn(cp
, "/?#");
489 if (!at
|| slash
<= at
) {
493 else if (!colon
|| at
<= colon
) {
495 c
->username
= url_decode_mem(cp
, at
- cp
);
496 if (c
->username
&& *c
->username
)
497 c
->username_from_proto
= 1;
501 c
->username
= url_decode_mem(cp
, colon
- cp
);
502 if (c
->username
&& *c
->username
)
503 c
->username_from_proto
= 1;
504 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
508 if (proto_end
&& proto_end
- url
> 0)
509 c
->protocol
= xmemdupz(url
, proto_end
- url
);
510 if (!allow_partial_url
|| slash
- host
> 0)
511 c
->host
= url_decode_mem(host
, slash
- host
);
512 /* Trim leading and trailing slashes from path */
513 while (*slash
== '/')
517 c
->path
= url_decode(slash
);
518 p
= c
->path
+ strlen(c
->path
) - 1;
519 while (p
> c
->path
&& *p
== '/')
523 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
524 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
525 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
526 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
527 check_url_component(url
, quiet
, "path", c
->path
) < 0)
533 static int credential_from_potentially_partial_url(struct credential
*c
,
536 return credential_from_url_1(c
, url
, 1, 0);
539 int credential_from_url_gently(struct credential
*c
, const char *url
, int quiet
)
541 return credential_from_url_1(c
, url
, 0, quiet
);
544 void credential_from_url(struct credential
*c
, const char *url
)
546 if (credential_from_url_gently(c
, url
, 0) < 0)
547 die(_("credential url cannot be parsed: %s"), url
);