4 #include "credential.h"
6 #include "string-list.h"
7 #include "run-command.h"
12 #include "git-compat-util.h"
14 void credential_init(struct credential
*c
)
16 struct credential blank
= CREDENTIAL_INIT
;
17 memcpy(c
, &blank
, sizeof(*c
));
20 void credential_clear(struct credential
*c
)
27 string_list_clear(&c
->helpers
, 0);
28 strvec_clear(&c
->wwwauth_headers
);
33 int credential_match(const struct credential
*want
,
34 const struct credential
*have
)
36 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
37 return CHECK(protocol
) &&
45 static int credential_from_potentially_partial_url(struct credential
*c
,
48 static int credential_config_callback(const char *var
, const char *value
,
51 struct credential
*c
= data
;
54 if (!skip_prefix(var
, "credential.", &key
))
58 return config_error_nonbool(var
);
60 if (!strcmp(key
, "helper")) {
62 string_list_append(&c
->helpers
, value
);
64 string_list_clear(&c
->helpers
, 0);
65 } else if (!strcmp(key
, "username")) {
66 if (!c
->username_from_proto
) {
68 c
->username
= xstrdup(value
);
71 else if (!strcmp(key
, "usehttppath"))
72 c
->use_http_path
= git_config_bool(var
, value
);
77 static int proto_is_http(const char *s
)
81 return !strcmp(s
, "https") || !strcmp(s
, "http");
84 static void credential_describe(struct credential
*c
, struct strbuf
*out
);
85 static void credential_format(struct credential
*c
, struct strbuf
*out
);
87 static int select_all(const struct urlmatch_item
*a
,
88 const struct urlmatch_item
*b
)
93 static int match_partial_url(const char *url
, void *cb
)
95 struct credential
*c
= cb
;
96 struct credential want
= CREDENTIAL_INIT
;
99 if (credential_from_potentially_partial_url(&want
, url
) < 0)
100 warning(_("skipping credential lookup for key: credential.%s"),
103 matches
= credential_match(&want
, c
);
104 credential_clear(&want
);
109 static void credential_apply_config(struct credential
*c
)
111 char *normalized_url
;
112 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
113 struct strbuf url
= STRBUF_INIT
;
116 die(_("refusing to work with credential missing host field"));
118 die(_("refusing to work with credential missing protocol field"));
123 config
.section
= "credential";
125 config
.collect_fn
= credential_config_callback
;
126 config
.cascade_fn
= NULL
;
127 config
.select_fn
= select_all
;
128 config
.fallback_match_fn
= match_partial_url
;
131 credential_format(c
, &url
);
132 normalized_url
= url_normalize(url
.buf
, &config
.url
);
134 git_config(urlmatch_config_entry
, &config
);
135 string_list_clear(&config
.vars
, 1);
136 free(normalized_url
);
137 urlmatch_config_release(&config
);
138 strbuf_release(&url
);
142 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
143 FREE_AND_NULL(c
->path
);
147 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
151 strbuf_addf(out
, "%s://", c
->protocol
);
152 if (c
->username
&& *c
->username
)
153 strbuf_addf(out
, "%s@", c
->username
);
155 strbuf_addstr(out
, c
->host
);
157 strbuf_addf(out
, "/%s", c
->path
);
160 static void credential_format(struct credential
*c
, struct strbuf
*out
)
164 strbuf_addf(out
, "%s://", c
->protocol
);
165 if (c
->username
&& *c
->username
) {
166 strbuf_add_percentencode(out
, c
->username
, STRBUF_ENCODE_SLASH
);
167 strbuf_addch(out
, '@');
170 strbuf_addstr(out
, c
->host
);
172 strbuf_addch(out
, '/');
173 strbuf_add_percentencode(out
, c
->path
, 0);
177 static char *credential_ask_one(const char *what
, struct credential
*c
,
180 struct strbuf desc
= STRBUF_INIT
;
181 struct strbuf prompt
= STRBUF_INIT
;
184 credential_describe(c
, &desc
);
186 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
188 strbuf_addf(&prompt
, "%s: ", what
);
190 r
= git_prompt(prompt
.buf
, flags
);
192 strbuf_release(&desc
);
193 strbuf_release(&prompt
);
197 static void credential_getpass(struct credential
*c
)
200 c
->username
= credential_ask_one("Username", c
,
201 PROMPT_ASKPASS
|PROMPT_ECHO
);
203 c
->password
= credential_ask_one("Password", c
,
207 int credential_read(struct credential
*c
, FILE *fp
)
209 struct strbuf line
= STRBUF_INIT
;
211 while (strbuf_getline(&line
, fp
) != EOF
) {
212 char *key
= line
.buf
;
213 char *value
= strchr(key
, '=');
219 warning("invalid credential line: %s", key
);
220 strbuf_release(&line
);
225 if (!strcmp(key
, "username")) {
227 c
->username
= xstrdup(value
);
228 c
->username_from_proto
= 1;
229 } else if (!strcmp(key
, "password")) {
231 c
->password
= xstrdup(value
);
232 } else if (!strcmp(key
, "protocol")) {
234 c
->protocol
= xstrdup(value
);
235 } else if (!strcmp(key
, "host")) {
237 c
->host
= xstrdup(value
);
238 } else if (!strcmp(key
, "path")) {
240 c
->path
= xstrdup(value
);
241 } else if (!strcmp(key
, "password_expiry_utc")) {
243 c
->password_expiry_utc
= parse_timestamp(value
, NULL
, 10);
244 if (c
->password_expiry_utc
== 0 || errno
== ERANGE
)
245 c
->password_expiry_utc
= TIME_MAX
;
246 } else if (!strcmp(key
, "url")) {
247 credential_from_url(c
, value
);
248 } else if (!strcmp(key
, "quit")) {
249 c
->quit
= !!git_config_bool("quit", value
);
252 * Ignore other lines; we don't know what they mean, but
253 * this future-proofs us when later versions of git do
254 * learn new lines, and the helpers are updated to match.
258 strbuf_release(&line
);
262 static void credential_write_item(FILE *fp
, const char *key
, const char *value
,
265 if (!value
&& required
)
266 BUG("credential value for %s is missing", key
);
269 if (strchr(value
, '\n'))
270 die("credential value for %s contains newline", key
);
271 fprintf(fp
, "%s=%s\n", key
, value
);
274 void credential_write(const struct credential
*c
, FILE *fp
)
276 credential_write_item(fp
, "protocol", c
->protocol
, 1);
277 credential_write_item(fp
, "host", c
->host
, 1);
278 credential_write_item(fp
, "path", c
->path
, 0);
279 credential_write_item(fp
, "username", c
->username
, 0);
280 credential_write_item(fp
, "password", c
->password
, 0);
281 if (c
->password_expiry_utc
!= TIME_MAX
) {
282 char *s
= xstrfmt("%"PRItime
, c
->password_expiry_utc
);
283 credential_write_item(fp
, "password_expiry_utc", s
, 0);
286 for (size_t i
= 0; i
< c
->wwwauth_headers
.nr
; i
++)
287 credential_write_item(fp
, "wwwauth[]", c
->wwwauth_headers
.v
[i
], 0);
290 static int run_credential_helper(struct credential
*c
,
294 struct child_process helper
= CHILD_PROCESS_INIT
;
297 strvec_push(&helper
.args
, cmd
);
298 helper
.use_shell
= 1;
303 helper
.no_stdout
= 1;
305 if (start_command(&helper
) < 0)
308 fp
= xfdopen(helper
.in
, "w");
309 sigchain_push(SIGPIPE
, SIG_IGN
);
310 credential_write(c
, fp
);
312 sigchain_pop(SIGPIPE
);
316 fp
= xfdopen(helper
.out
, "r");
317 r
= credential_read(c
, fp
);
320 finish_command(&helper
);
325 if (finish_command(&helper
))
330 static int credential_do(struct credential
*c
, const char *helper
,
331 const char *operation
)
333 struct strbuf cmd
= STRBUF_INIT
;
336 if (helper
[0] == '!')
337 strbuf_addstr(&cmd
, helper
+ 1);
338 else if (is_absolute_path(helper
))
339 strbuf_addstr(&cmd
, helper
);
341 strbuf_addf(&cmd
, "git credential-%s", helper
);
343 strbuf_addf(&cmd
, " %s", operation
);
344 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
346 strbuf_release(&cmd
);
350 void credential_fill(struct credential
*c
)
354 if (c
->username
&& c
->password
)
357 credential_apply_config(c
);
359 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
360 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
361 if (c
->password_expiry_utc
< time(NULL
)) {
362 /* Discard expired password */
363 FREE_AND_NULL(c
->password
);
364 /* Reset expiry to maintain consistency */
365 c
->password_expiry_utc
= TIME_MAX
;
367 if (c
->username
&& c
->password
)
370 die("credential helper '%s' told us to quit",
371 c
->helpers
.items
[i
].string
);
374 credential_getpass(c
);
375 if (!c
->username
&& !c
->password
)
376 die("unable to get password from user");
379 void credential_approve(struct credential
*c
)
385 if (!c
->username
|| !c
->password
|| c
->password_expiry_utc
< time(NULL
))
388 credential_apply_config(c
);
390 for (i
= 0; i
< c
->helpers
.nr
; i
++)
391 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
395 void credential_reject(struct credential
*c
)
399 credential_apply_config(c
);
401 for (i
= 0; i
< c
->helpers
.nr
; i
++)
402 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
404 FREE_AND_NULL(c
->username
);
405 FREE_AND_NULL(c
->password
);
406 c
->password_expiry_utc
= TIME_MAX
;
410 static int check_url_component(const char *url
, int quiet
,
411 const char *name
, const char *value
)
415 if (!strchr(value
, '\n'))
419 warning(_("url contains a newline in its %s component: %s"),
425 * Potentially-partial URLs can, but do not have to, contain
427 * - a protocol (or scheme) of the form "<protocol>://"
429 * - a host name (the part after the protocol and before the first slash after
432 * - a user name and potentially a password (as "<user>[:<password>]@" part of
435 * - a path (the part after the host name, if any, starting with the slash)
437 * Missing parts will be left unset in `struct credential`. Thus, `https://`
438 * will have only the `protocol` set, `example.com` only the host name, and
439 * `/git` only the path.
441 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
442 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
443 * be potentially partial, and only then (otherwise, the empty string is used).
445 * The credential_from_url() function does not allow partial URLs.
447 static int credential_from_url_1(struct credential
*c
, const char *url
,
448 int allow_partial_url
, int quiet
)
450 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
456 * (1) proto://<host>/...
457 * (2) proto://<user>@<host>/...
458 * (3) proto://<user>:<pass>@<host>/...
460 proto_end
= strstr(url
, "://");
461 if (!allow_partial_url
&& (!proto_end
|| proto_end
== url
)) {
463 warning(_("url has no scheme: %s"), url
);
466 cp
= proto_end
? proto_end
+ 3 : url
;
467 at
= strchr(cp
, '@');
468 colon
= strchr(cp
, ':');
471 * A query or fragment marker before the slash ends the host portion.
472 * We'll just continue to call this "slash" for simplicity. Notably our
473 * "trim leading slashes" part won't skip over this part of the path,
474 * but that's what we'd want.
476 slash
= cp
+ strcspn(cp
, "/?#");
478 if (!at
|| slash
<= at
) {
482 else if (!colon
|| at
<= colon
) {
484 c
->username
= url_decode_mem(cp
, at
- cp
);
485 if (c
->username
&& *c
->username
)
486 c
->username_from_proto
= 1;
490 c
->username
= url_decode_mem(cp
, colon
- cp
);
491 if (c
->username
&& *c
->username
)
492 c
->username_from_proto
= 1;
493 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
497 if (proto_end
&& proto_end
- url
> 0)
498 c
->protocol
= xmemdupz(url
, proto_end
- url
);
499 if (!allow_partial_url
|| slash
- host
> 0)
500 c
->host
= url_decode_mem(host
, slash
- host
);
501 /* Trim leading and trailing slashes from path */
502 while (*slash
== '/')
506 c
->path
= url_decode(slash
);
507 p
= c
->path
+ strlen(c
->path
) - 1;
508 while (p
> c
->path
&& *p
== '/')
512 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
513 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
514 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
515 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
516 check_url_component(url
, quiet
, "path", c
->path
) < 0)
522 static int credential_from_potentially_partial_url(struct credential
*c
,
525 return credential_from_url_1(c
, url
, 1, 0);
528 int credential_from_url_gently(struct credential
*c
, const char *url
, int quiet
)
530 return credential_from_url_1(c
, url
, 0, quiet
);
533 void credential_from_url(struct credential
*c
, const char *url
)
535 if (credential_from_url_gently(c
, url
, 0) < 0)
536 die(_("credential url cannot be parsed: %s"), url
);