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 string_list_clear(&c
->helpers
, 0);
29 strvec_clear(&c
->wwwauth_headers
);
34 int credential_match(const struct credential
*want
,
35 const struct credential
*have
)
37 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
38 return CHECK(protocol
) &&
46 static int credential_from_potentially_partial_url(struct credential
*c
,
49 static int credential_config_callback(const char *var
, const char *value
,
52 struct credential
*c
= data
;
55 if (!skip_prefix(var
, "credential.", &key
))
59 return config_error_nonbool(var
);
61 if (!strcmp(key
, "helper")) {
63 string_list_append(&c
->helpers
, value
);
65 string_list_clear(&c
->helpers
, 0);
66 } else if (!strcmp(key
, "username")) {
67 if (!c
->username_from_proto
) {
69 c
->username
= xstrdup(value
);
72 else if (!strcmp(key
, "usehttppath"))
73 c
->use_http_path
= git_config_bool(var
, value
);
78 static int proto_is_http(const char *s
)
82 return !strcmp(s
, "https") || !strcmp(s
, "http");
85 static void credential_describe(struct credential
*c
, struct strbuf
*out
);
86 static void credential_format(struct credential
*c
, struct strbuf
*out
);
88 static int select_all(const struct urlmatch_item
*a
,
89 const struct urlmatch_item
*b
)
94 static int match_partial_url(const char *url
, void *cb
)
96 struct credential
*c
= cb
;
97 struct credential want
= CREDENTIAL_INIT
;
100 if (credential_from_potentially_partial_url(&want
, url
) < 0)
101 warning(_("skipping credential lookup for key: credential.%s"),
104 matches
= credential_match(&want
, c
);
105 credential_clear(&want
);
110 static void credential_apply_config(struct credential
*c
)
112 char *normalized_url
;
113 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
114 struct strbuf url
= STRBUF_INIT
;
117 die(_("refusing to work with credential missing host field"));
119 die(_("refusing to work with credential missing protocol field"));
124 config
.section
= "credential";
126 config
.collect_fn
= credential_config_callback
;
127 config
.cascade_fn
= NULL
;
128 config
.select_fn
= select_all
;
129 config
.fallback_match_fn
= match_partial_url
;
132 credential_format(c
, &url
);
133 normalized_url
= url_normalize(url
.buf
, &config
.url
);
135 git_config(urlmatch_config_entry
, &config
);
136 string_list_clear(&config
.vars
, 1);
137 free(normalized_url
);
138 urlmatch_config_release(&config
);
139 strbuf_release(&url
);
143 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
144 FREE_AND_NULL(c
->path
);
148 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
152 strbuf_addf(out
, "%s://", c
->protocol
);
153 if (c
->username
&& *c
->username
)
154 strbuf_addf(out
, "%s@", c
->username
);
156 strbuf_addstr(out
, c
->host
);
158 strbuf_addf(out
, "/%s", c
->path
);
161 static void credential_format(struct credential
*c
, struct strbuf
*out
)
165 strbuf_addf(out
, "%s://", c
->protocol
);
166 if (c
->username
&& *c
->username
) {
167 strbuf_add_percentencode(out
, c
->username
, STRBUF_ENCODE_SLASH
);
168 strbuf_addch(out
, '@');
171 strbuf_addstr(out
, c
->host
);
173 strbuf_addch(out
, '/');
174 strbuf_add_percentencode(out
, c
->path
, 0);
178 static char *credential_ask_one(const char *what
, struct credential
*c
,
181 struct strbuf desc
= STRBUF_INIT
;
182 struct strbuf prompt
= STRBUF_INIT
;
185 credential_describe(c
, &desc
);
187 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
189 strbuf_addf(&prompt
, "%s: ", what
);
191 r
= git_prompt(prompt
.buf
, flags
);
193 strbuf_release(&desc
);
194 strbuf_release(&prompt
);
198 static void credential_getpass(struct credential
*c
)
201 c
->username
= credential_ask_one("Username", c
,
202 PROMPT_ASKPASS
|PROMPT_ECHO
);
204 c
->password
= credential_ask_one("Password", c
,
208 int credential_read(struct credential
*c
, FILE *fp
)
210 struct strbuf line
= STRBUF_INIT
;
212 while (strbuf_getline(&line
, fp
) != EOF
) {
213 char *key
= line
.buf
;
214 char *value
= strchr(key
, '=');
220 warning("invalid credential line: %s", key
);
221 strbuf_release(&line
);
226 if (!strcmp(key
, "username")) {
228 c
->username
= xstrdup(value
);
229 c
->username_from_proto
= 1;
230 } else if (!strcmp(key
, "password")) {
232 c
->password
= xstrdup(value
);
233 } else if (!strcmp(key
, "protocol")) {
235 c
->protocol
= xstrdup(value
);
236 } else if (!strcmp(key
, "host")) {
238 c
->host
= xstrdup(value
);
239 } else if (!strcmp(key
, "path")) {
241 c
->path
= xstrdup(value
);
242 } else if (!strcmp(key
, "password_expiry_utc")) {
244 c
->password_expiry_utc
= parse_timestamp(value
, NULL
, 10);
245 if (c
->password_expiry_utc
== 0 || errno
== ERANGE
)
246 c
->password_expiry_utc
= TIME_MAX
;
247 } else if (!strcmp(key
, "url")) {
248 credential_from_url(c
, value
);
249 } else if (!strcmp(key
, "quit")) {
250 c
->quit
= !!git_config_bool("quit", value
);
253 * Ignore other lines; we don't know what they mean, but
254 * this future-proofs us when later versions of git do
255 * learn new lines, and the helpers are updated to match.
259 strbuf_release(&line
);
263 static void credential_write_item(FILE *fp
, const char *key
, const char *value
,
266 if (!value
&& required
)
267 BUG("credential value for %s is missing", key
);
270 if (strchr(value
, '\n'))
271 die("credential value for %s contains newline", key
);
272 fprintf(fp
, "%s=%s\n", key
, value
);
275 void credential_write(const struct credential
*c
, FILE *fp
)
277 credential_write_item(fp
, "protocol", c
->protocol
, 1);
278 credential_write_item(fp
, "host", c
->host
, 1);
279 credential_write_item(fp
, "path", c
->path
, 0);
280 credential_write_item(fp
, "username", c
->username
, 0);
281 credential_write_item(fp
, "password", c
->password
, 0);
282 if (c
->password_expiry_utc
!= TIME_MAX
) {
283 char *s
= xstrfmt("%"PRItime
, c
->password_expiry_utc
);
284 credential_write_item(fp
, "password_expiry_utc", s
, 0);
287 for (size_t i
= 0; i
< c
->wwwauth_headers
.nr
; i
++)
288 credential_write_item(fp
, "wwwauth[]", c
->wwwauth_headers
.v
[i
], 0);
291 static int run_credential_helper(struct credential
*c
,
295 struct child_process helper
= CHILD_PROCESS_INIT
;
298 strvec_push(&helper
.args
, cmd
);
299 helper
.use_shell
= 1;
304 helper
.no_stdout
= 1;
306 if (start_command(&helper
) < 0)
309 fp
= xfdopen(helper
.in
, "w");
310 sigchain_push(SIGPIPE
, SIG_IGN
);
311 credential_write(c
, fp
);
313 sigchain_pop(SIGPIPE
);
317 fp
= xfdopen(helper
.out
, "r");
318 r
= credential_read(c
, fp
);
321 finish_command(&helper
);
326 if (finish_command(&helper
))
331 static int credential_do(struct credential
*c
, const char *helper
,
332 const char *operation
)
334 struct strbuf cmd
= STRBUF_INIT
;
337 if (helper
[0] == '!')
338 strbuf_addstr(&cmd
, helper
+ 1);
339 else if (is_absolute_path(helper
))
340 strbuf_addstr(&cmd
, helper
);
342 strbuf_addf(&cmd
, "git credential-%s", helper
);
344 strbuf_addf(&cmd
, " %s", operation
);
345 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
347 strbuf_release(&cmd
);
351 void credential_fill(struct credential
*c
)
355 if (c
->username
&& c
->password
)
358 credential_apply_config(c
);
360 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
361 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
362 if (c
->password_expiry_utc
< time(NULL
)) {
363 /* Discard expired password */
364 FREE_AND_NULL(c
->password
);
365 /* Reset expiry to maintain consistency */
366 c
->password_expiry_utc
= TIME_MAX
;
368 if (c
->username
&& c
->password
)
371 die("credential helper '%s' told us to quit",
372 c
->helpers
.items
[i
].string
);
375 credential_getpass(c
);
376 if (!c
->username
&& !c
->password
)
377 die("unable to get password from user");
380 void credential_approve(struct credential
*c
)
386 if (!c
->username
|| !c
->password
|| c
->password_expiry_utc
< time(NULL
))
389 credential_apply_config(c
);
391 for (i
= 0; i
< c
->helpers
.nr
; i
++)
392 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
396 void credential_reject(struct credential
*c
)
400 credential_apply_config(c
);
402 for (i
= 0; i
< c
->helpers
.nr
; i
++)
403 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
405 FREE_AND_NULL(c
->username
);
406 FREE_AND_NULL(c
->password
);
407 c
->password_expiry_utc
= TIME_MAX
;
411 static int check_url_component(const char *url
, int quiet
,
412 const char *name
, const char *value
)
416 if (!strchr(value
, '\n'))
420 warning(_("url contains a newline in its %s component: %s"),
426 * Potentially-partial URLs can, but do not have to, contain
428 * - a protocol (or scheme) of the form "<protocol>://"
430 * - a host name (the part after the protocol and before the first slash after
433 * - a user name and potentially a password (as "<user>[:<password>]@" part of
436 * - a path (the part after the host name, if any, starting with the slash)
438 * Missing parts will be left unset in `struct credential`. Thus, `https://`
439 * will have only the `protocol` set, `example.com` only the host name, and
440 * `/git` only the path.
442 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
443 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
444 * be potentially partial, and only then (otherwise, the empty string is used).
446 * The credential_from_url() function does not allow partial URLs.
448 static int credential_from_url_1(struct credential
*c
, const char *url
,
449 int allow_partial_url
, int quiet
)
451 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
457 * (1) proto://<host>/...
458 * (2) proto://<user>@<host>/...
459 * (3) proto://<user>:<pass>@<host>/...
461 proto_end
= strstr(url
, "://");
462 if (!allow_partial_url
&& (!proto_end
|| proto_end
== url
)) {
464 warning(_("url has no scheme: %s"), url
);
467 cp
= proto_end
? proto_end
+ 3 : url
;
468 at
= strchr(cp
, '@');
469 colon
= strchr(cp
, ':');
472 * A query or fragment marker before the slash ends the host portion.
473 * We'll just continue to call this "slash" for simplicity. Notably our
474 * "trim leading slashes" part won't skip over this part of the path,
475 * but that's what we'd want.
477 slash
= cp
+ strcspn(cp
, "/?#");
479 if (!at
|| slash
<= at
) {
483 else if (!colon
|| at
<= colon
) {
485 c
->username
= url_decode_mem(cp
, at
- cp
);
486 if (c
->username
&& *c
->username
)
487 c
->username_from_proto
= 1;
491 c
->username
= url_decode_mem(cp
, colon
- cp
);
492 if (c
->username
&& *c
->username
)
493 c
->username_from_proto
= 1;
494 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
498 if (proto_end
&& proto_end
- url
> 0)
499 c
->protocol
= xmemdupz(url
, proto_end
- url
);
500 if (!allow_partial_url
|| slash
- host
> 0)
501 c
->host
= url_decode_mem(host
, slash
- host
);
502 /* Trim leading and trailing slashes from path */
503 while (*slash
== '/')
507 c
->path
= url_decode(slash
);
508 p
= c
->path
+ strlen(c
->path
) - 1;
509 while (p
> c
->path
&& *p
== '/')
513 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
514 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
515 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
516 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
517 check_url_component(url
, quiet
, "path", c
->path
) < 0)
523 static int credential_from_potentially_partial_url(struct credential
*c
,
526 return credential_from_url_1(c
, url
, 1, 0);
529 int credential_from_url_gently(struct credential
*c
, const char *url
, int quiet
)
531 return credential_from_url_1(c
, url
, 0, quiet
);
534 void credential_from_url(struct credential
*c
, const char *url
)
536 if (credential_from_url_gently(c
, url
, 0) < 0)
537 die(_("credential url cannot be parsed: %s"), url
);