3 #include "credential.h"
4 #include "string-list.h"
5 #include "run-command.h"
11 void credential_init(struct credential
*c
)
13 struct credential blank
= CREDENTIAL_INIT
;
14 memcpy(c
, &blank
, sizeof(*c
));
17 void credential_clear(struct credential
*c
)
24 string_list_clear(&c
->helpers
, 0);
29 int credential_match(const struct credential
*want
,
30 const struct credential
*have
)
32 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
33 return CHECK(protocol
) &&
41 static int credential_from_potentially_partial_url(struct credential
*c
,
44 static int credential_config_callback(const char *var
, const char *value
,
47 struct credential
*c
= data
;
50 if (!skip_prefix(var
, "credential.", &key
))
54 return config_error_nonbool(var
);
56 if (!strcmp(key
, "helper")) {
58 string_list_append(&c
->helpers
, value
);
60 string_list_clear(&c
->helpers
, 0);
61 } else if (!strcmp(key
, "username")) {
62 if (!c
->username_from_proto
) {
64 c
->username
= xstrdup(value
);
67 else if (!strcmp(key
, "usehttppath"))
68 c
->use_http_path
= git_config_bool(var
, value
);
73 static int proto_is_http(const char *s
)
77 return !strcmp(s
, "https") || !strcmp(s
, "http");
80 static void credential_describe(struct credential
*c
, struct strbuf
*out
);
81 static void credential_format(struct credential
*c
, struct strbuf
*out
);
83 static int select_all(const struct urlmatch_item
*a
,
84 const struct urlmatch_item
*b
)
89 static int match_partial_url(const char *url
, void *cb
)
91 struct credential
*c
= cb
;
92 struct credential want
= CREDENTIAL_INIT
;
95 if (credential_from_potentially_partial_url(&want
, url
) < 0)
96 warning(_("skipping credential lookup for key: credential.%s"),
99 matches
= credential_match(&want
, c
);
100 credential_clear(&want
);
105 static void credential_apply_config(struct credential
*c
)
107 char *normalized_url
;
108 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
109 struct strbuf url
= STRBUF_INIT
;
112 die(_("refusing to work with credential missing host field"));
114 die(_("refusing to work with credential missing protocol field"));
119 config
.section
= "credential";
121 config
.collect_fn
= credential_config_callback
;
122 config
.cascade_fn
= NULL
;
123 config
.select_fn
= select_all
;
124 config
.fallback_match_fn
= match_partial_url
;
127 credential_format(c
, &url
);
128 normalized_url
= url_normalize(url
.buf
, &config
.url
);
130 git_config(urlmatch_config_entry
, &config
);
131 string_list_clear(&config
.vars
, 1);
132 free(normalized_url
);
133 urlmatch_config_release(&config
);
134 strbuf_release(&url
);
138 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
139 FREE_AND_NULL(c
->path
);
143 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
147 strbuf_addf(out
, "%s://", c
->protocol
);
148 if (c
->username
&& *c
->username
)
149 strbuf_addf(out
, "%s@", c
->username
);
151 strbuf_addstr(out
, c
->host
);
153 strbuf_addf(out
, "/%s", c
->path
);
156 static void credential_format(struct credential
*c
, struct strbuf
*out
)
160 strbuf_addf(out
, "%s://", c
->protocol
);
161 if (c
->username
&& *c
->username
) {
162 strbuf_add_percentencode(out
, c
->username
, STRBUF_ENCODE_SLASH
);
163 strbuf_addch(out
, '@');
166 strbuf_addstr(out
, c
->host
);
168 strbuf_addch(out
, '/');
169 strbuf_add_percentencode(out
, c
->path
, 0);
173 static char *credential_ask_one(const char *what
, struct credential
*c
,
176 struct strbuf desc
= STRBUF_INIT
;
177 struct strbuf prompt
= STRBUF_INIT
;
180 credential_describe(c
, &desc
);
182 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
184 strbuf_addf(&prompt
, "%s: ", what
);
186 r
= git_prompt(prompt
.buf
, flags
);
188 strbuf_release(&desc
);
189 strbuf_release(&prompt
);
193 static void credential_getpass(struct credential
*c
)
196 c
->username
= credential_ask_one("Username", c
,
197 PROMPT_ASKPASS
|PROMPT_ECHO
);
199 c
->password
= credential_ask_one("Password", c
,
203 int credential_read(struct credential
*c
, FILE *fp
)
205 struct strbuf line
= STRBUF_INIT
;
207 while (strbuf_getline(&line
, fp
) != EOF
) {
208 char *key
= line
.buf
;
209 char *value
= strchr(key
, '=');
215 warning("invalid credential line: %s", key
);
216 strbuf_release(&line
);
221 if (!strcmp(key
, "username")) {
223 c
->username
= xstrdup(value
);
224 c
->username_from_proto
= 1;
225 } else if (!strcmp(key
, "password")) {
227 c
->password
= xstrdup(value
);
228 } else if (!strcmp(key
, "protocol")) {
230 c
->protocol
= xstrdup(value
);
231 } else if (!strcmp(key
, "host")) {
233 c
->host
= xstrdup(value
);
234 } else if (!strcmp(key
, "path")) {
236 c
->path
= xstrdup(value
);
237 } else if (!strcmp(key
, "url")) {
238 credential_from_url(c
, value
);
239 } else if (!strcmp(key
, "quit")) {
240 c
->quit
= !!git_config_bool("quit", value
);
243 * Ignore other lines; we don't know what they mean, but
244 * this future-proofs us when later versions of git do
245 * learn new lines, and the helpers are updated to match.
249 strbuf_release(&line
);
253 static void credential_write_item(FILE *fp
, const char *key
, const char *value
,
256 if (!value
&& required
)
257 BUG("credential value for %s is missing", key
);
260 if (strchr(value
, '\n'))
261 die("credential value for %s contains newline", key
);
262 fprintf(fp
, "%s=%s\n", key
, value
);
265 void credential_write(const struct credential
*c
, FILE *fp
)
267 credential_write_item(fp
, "protocol", c
->protocol
, 1);
268 credential_write_item(fp
, "host", c
->host
, 1);
269 credential_write_item(fp
, "path", c
->path
, 0);
270 credential_write_item(fp
, "username", c
->username
, 0);
271 credential_write_item(fp
, "password", c
->password
, 0);
274 static int run_credential_helper(struct credential
*c
,
278 struct child_process helper
= CHILD_PROCESS_INIT
;
281 strvec_push(&helper
.args
, cmd
);
282 helper
.use_shell
= 1;
287 helper
.no_stdout
= 1;
289 if (start_command(&helper
) < 0)
292 fp
= xfdopen(helper
.in
, "w");
293 sigchain_push(SIGPIPE
, SIG_IGN
);
294 credential_write(c
, fp
);
296 sigchain_pop(SIGPIPE
);
300 fp
= xfdopen(helper
.out
, "r");
301 r
= credential_read(c
, fp
);
304 finish_command(&helper
);
309 if (finish_command(&helper
))
314 static int credential_do(struct credential
*c
, const char *helper
,
315 const char *operation
)
317 struct strbuf cmd
= STRBUF_INIT
;
320 if (helper
[0] == '!')
321 strbuf_addstr(&cmd
, helper
1);
322 else if (is_absolute_path(helper
))
323 strbuf_addstr(&cmd
, helper
);
325 strbuf_addf(&cmd
, "git credential-%s", helper
);
327 strbuf_addf(&cmd
, " %s", operation
);
328 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
330 strbuf_release(&cmd
);
334 void credential_fill(struct credential
*c
)
338 if (c
->username
&& c
->password
)
341 credential_apply_config(c
);
343 for (i
= 0; i
< c
->helpers
.nr
; i
) {
344 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
345 if (c
->username
&& c
->password
)
348 die("credential helper '%s' told us to quit",
349 c
->helpers
.items
[i
].string
);
352 credential_getpass(c
);
353 if (!c
->username
&& !c
->password
)
354 die("unable to get password from user");
357 void credential_approve(struct credential
*c
)
363 if (!c
->username
|| !c
->password
)
366 credential_apply_config(c
);
368 for (i
= 0; i
< c
->helpers
.nr
; i
)
369 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
373 void credential_reject(struct credential
*c
)
377 credential_apply_config(c
);
379 for (i
= 0; i
< c
->helpers
.nr
; i
)
380 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
382 FREE_AND_NULL(c
->username
);
383 FREE_AND_NULL(c
->password
);
387 static int check_url_component(const char *url
, int quiet
,
388 const char *name
, const char *value
)
392 if (!strchr(value
, '\n'))
396 warning(_("url contains a newline in its %s component: %s"),
402 * Potentially-partial URLs can, but do not have to, contain
404 * - a protocol (or scheme) of the form "<protocol>://"
406 * - a host name (the part after the protocol and before the first slash after
409 * - a user name and potentially a password (as "<user>[:<password>]@" part of
412 * - a path (the part after the host name, if any, starting with the slash)
414 * Missing parts will be left unset in `struct credential`. Thus, `https://`
415 * will have only the `protocol` set, `example.com` only the host name, and
416 * `/git` only the path.
418 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
419 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
420 * be potentially partial, and only then (otherwise, the empty string is used).
422 * The credential_from_url() function does not allow partial URLs.
424 static int credential_from_url_1(struct credential
*c
, const char *url
,
425 int allow_partial_url
, int quiet
)
427 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
433 * (1) proto://<host>/...
434 * (2) proto://<user>@<host>/...
435 * (3) proto://<user>:<pass>@<host>/...
437 proto_end
= strstr(url
, "://");
438 if (!allow_partial_url
&& (!proto_end
|| proto_end
== url
)) {
440 warning(_("url has no scheme: %s"), url
);
443 cp
= proto_end
? proto_end
3 : url
;
444 at
= strchr(cp
, '@');
445 colon
= strchr(cp
, ':');
448 * A query or fragment marker before the slash ends the host portion.
449 * We'll just continue to call this "slash" for simplicity. Notably our
450 * "trim leading slashes" part won't skip over this part of the path,
451 * but that's what we'd want.
453 slash
= cp
strcspn(cp
, "/?#");
455 if (!at
|| slash
<= at
) {
459 else if (!colon
|| at
<= colon
) {
461 c
->username
= url_decode_mem(cp
, at
- cp
);
462 if (c
->username
&& *c
->username
)
463 c
->username_from_proto
= 1;
467 c
->username
= url_decode_mem(cp
, colon
- cp
);
468 if (c
->username
&& *c
->username
)
469 c
->username_from_proto
= 1;
470 c
->password
= url_decode_mem(colon
1, at
- (colon
1));
474 if (proto_end
&& proto_end
- url
> 0)
475 c
->protocol
= xmemdupz(url
, proto_end
- url
);
476 if (!allow_partial_url
|| slash
- host
> 0)
477 c
->host
= url_decode_mem(host
, slash
- host
);
478 /* Trim leading and trailing slashes from path */
479 while (*slash
== '/')
483 c
->path
= url_decode(slash
);
484 p
= c
->path
strlen(c
->path
) - 1;
485 while (p
> c
->path
&& *p
== '/')
489 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
490 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
491 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
492 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
493 check_url_component(url
, quiet
, "path", c
->path
) < 0)
499 static int credential_from_potentially_partial_url(struct credential
*c
,
502 return credential_from_url_1(c
, url
, 1, 0);
505 int credential_from_url_gently(struct credential
*c
, const char *url
, int quiet
)
507 return credential_from_url_1(c
, url
, 0, quiet
);
510 void credential_from_url(struct credential
*c
, const char *url
)
512 if (credential_from_url_gently(c
, url
, 0) < 0)
513 die(_("credential url cannot be parsed: %s"), url
);