3 #include "credential.h"
4 #include "string-list.h"
5 #include "run-command.h"
11 void credential_init(struct credential *c)
13 memset(c, 0, sizeof(*c));
14 c->helpers.strdup_strings = 1;
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 void credential_apply_config(struct credential *c)
92 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
93 struct strbuf url = STRBUF_INIT;
96 die(_("refusing to work with credential missing host field"));
98 die(_("refusing to work with credential missing protocol field"));
103 config.section = "credential";
105 config.collect_fn = credential_config_callback;
106 config.cascade_fn = NULL;
107 config.select_fn = select_all;
110 credential_format(c, &url);
111 normalized_url = url_normalize(url.buf, &config.url);
113 git_config(urlmatch_config_entry, &config);
114 free(normalized_url);
115 strbuf_release(&url);
119 if (!c->use_http_path && proto_is_http(c->protocol)) {
120 FREE_AND_NULL(c->path);
124 static void credential_describe(struct credential *c, struct strbuf *out)
128 strbuf_addf(out, "%s://", c->protocol);
129 if (c->username && *c->username)
130 strbuf_addf(out, "%s@", c->username);
132 strbuf_addstr(out, c->host);
134 strbuf_addf(out, "/%s", c->path);
137 static void credential_format(struct credential *c, struct strbuf *out)
141 strbuf_addf(out, "%s://", c->protocol);
142 if (c->username && *c->username) {
143 strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
144 strbuf_addch(out, '@');
147 strbuf_addstr(out, c->host);
149 strbuf_addch(out, '/');
150 strbuf_add_percentencode(out, c->path, 0);
154 static char *credential_ask_one(const char *what, struct credential *c,
157 struct strbuf desc = STRBUF_INIT;
158 struct strbuf prompt = STRBUF_INIT;
161 credential_describe(c, &desc);
163 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
165 strbuf_addf(&prompt, "%s: ", what);
167 r = git_prompt(prompt.buf, flags);
169 strbuf_release(&desc);
170 strbuf_release(&prompt);
174 static void credential_getpass(struct credential *c)
177 c->username = credential_ask_one("Username", c,
178 PROMPT_ASKPASS|PROMPT_ECHO);
180 c->password = credential_ask_one("Password", c,
184 int credential_read(struct credential *c, FILE *fp)
186 struct strbuf line = STRBUF_INIT;
188 while (strbuf_getline_lf(&line, fp) != EOF) {
189 char *key = line.buf;
190 char *value = strchr(key, '=');
196 warning("invalid credential line: %s", key);
197 strbuf_release(&line);
202 if (!strcmp(key, "username")) {
204 c->username = xstrdup(value);
205 c->username_from_proto = 1;
206 } else if (!strcmp(key, "password")) {
208 c->password = xstrdup(value);
209 } else if (!strcmp(key, "protocol")) {
211 c->protocol = xstrdup(value);
212 } else if (!strcmp(key, "host")) {
214 c->host = xstrdup(value);
215 } else if (!strcmp(key, "path")) {
217 c->path = xstrdup(value);
218 } else if (!strcmp(key, "url")) {
219 credential_from_url(c, value);
220 } else if (!strcmp(key, "quit")) {
221 c->quit = !!git_config_bool("quit", value);
224 * Ignore other lines; we don't know what they mean, but
225 * this future-proofs us when later versions of git do
226 * learn new lines, and the helpers are updated to match.
230 strbuf_release(&line);
234 static void credential_write_item(FILE *fp, const char *key, const char *value,
237 if (!value && required)
238 BUG("credential value for %s is missing", key);
241 if (strchr(value, '\n'))
242 die("credential value for %s contains newline", key);
243 fprintf(fp, "%s=%s\n", key, value);
246 void credential_write(const struct credential *c, FILE *fp)
248 credential_write_item(fp, "protocol", c->protocol, 1);
249 credential_write_item(fp, "host", c->host, 1);
250 credential_write_item(fp, "path", c->path, 0);
251 credential_write_item(fp, "username", c->username, 0);
252 credential_write_item(fp, "password", c->password, 0);
255 static int run_credential_helper(struct credential *c,
259 struct child_process helper = CHILD_PROCESS_INIT;
260 const char *argv[] = { NULL, NULL };
265 helper.use_shell = 1;
270 helper.no_stdout = 1;
272 if (start_command(&helper) < 0)
275 fp = xfdopen(helper.in, "w");
276 sigchain_push(SIGPIPE, SIG_IGN);
277 credential_write(c, fp);
279 sigchain_pop(SIGPIPE);
283 fp = xfdopen(helper.out, "r");
284 r = credential_read(c, fp);
287 finish_command(&helper);
292 if (finish_command(&helper))
297 static int credential_do(struct credential *c, const char *helper,
298 const char *operation)
300 struct strbuf cmd = STRBUF_INIT;
303 if (helper[0] == '!')
304 strbuf_addstr(&cmd, helper + 1);
305 else if (is_absolute_path(helper))
306 strbuf_addstr(&cmd, helper);
308 strbuf_addf(&cmd, "git credential-%s", helper);
310 strbuf_addf(&cmd, " %s", operation);
311 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
313 strbuf_release(&cmd);
317 void credential_fill(struct credential *c)
321 if (c->username && c->password)
324 credential_apply_config(c);
326 for (i = 0; i < c->helpers.nr; i++) {
327 credential_do(c, c->helpers.items[i].string, "get");
328 if (c->username && c->password)
331 die("credential helper '%s' told us to quit",
332 c->helpers.items[i].string);
335 credential_getpass(c);
336 if (!c->username && !c->password)
337 die("unable to get password from user");
340 void credential_approve(struct credential *c)
346 if (!c->username || !c->password)
349 credential_apply_config(c);
351 for (i = 0; i < c->helpers.nr; i++)
352 credential_do(c, c->helpers.items[i].string, "store");
356 void credential_reject(struct credential *c)
360 credential_apply_config(c);
362 for (i = 0; i < c->helpers.nr; i++)
363 credential_do(c, c->helpers.items[i].string, "erase");
365 FREE_AND_NULL(c->username);
366 FREE_AND_NULL(c->password);
370 static int check_url_component(const char *url, int quiet,
371 const char *name, const char *value)
375 if (!strchr(value, '\n'))
379 warning(_("url contains a newline in its %s component: %s"),
385 * Potentially-partial URLs can, but do not have to, contain
387 * - a protocol (or scheme) of the form "<protocol>://"
389 * - a host name (the part after the protocol and before the first slash after
392 * - a user name and potentially a password (as "<user>[:<password>]@" part of
395 * - a path (the part after the host name, if any, starting with the slash)
397 * Missing parts will be left unset in `struct credential`. Thus, `https://`
398 * will have only the `protocol` set, `example.com` only the host name, and
399 * `/git` only the path.
401 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
402 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
403 * be potentially partial, and only then (otherwise, the empty string is used).
405 * The credential_from_url() function does not allow partial URLs.
407 static int credential_from_url_1(struct credential *c, const char *url,
408 int allow_partial_url, int quiet)
410 const char *at, *colon, *cp, *slash, *host, *proto_end;
416 * (1) proto://<host>/...
417 * (2) proto://<user>@<host>/...
418 * (3) proto://<user>:<pass>@<host>/...
420 proto_end = strstr(url, "://");
421 if (!allow_partial_url && (!proto_end || proto_end == url)) {
423 warning(_("url has no scheme: %s"), url);
426 cp = proto_end ? proto_end + 3 : url;
427 at = strchr(cp, '@');
428 colon = strchr(cp, ':');
431 * A query or fragment marker before the slash ends the host portion.
432 * We'll just continue to call this "slash" for simplicity. Notably our
433 * "trim leading slashes" part won't skip over this part of the path,
434 * but that's what we'd want.
436 slash = cp + strcspn(cp, "/?#");
438 if (!at || slash <= at) {
442 else if (!colon || at <= colon) {
444 c->username = url_decode_mem(cp, at - cp);
445 if (c->username && *c->username)
446 c->username_from_proto = 1;
450 c->username = url_decode_mem(cp, colon - cp);
451 if (c->username && *c->username)
452 c->username_from_proto = 1;
453 c->password = url_decode_mem(colon + 1, at - (colon + 1));
457 if (proto_end && proto_end - url > 0)
458 c->protocol = xmemdupz(url, proto_end - url);
459 if (!allow_partial_url || slash - host > 0)
460 c->host = url_decode_mem(host, slash - host);
461 /* Trim leading and trailing slashes from path */
462 while (*slash == '/')
466 c->path = url_decode(slash);
467 p = c->path + strlen(c->path) - 1;
468 while (p > c->path && *p == '/')
472 if (check_url_component(url, quiet, "username", c->username) < 0 ||
473 check_url_component(url, quiet, "password", c->password) < 0 ||
474 check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
475 check_url_component(url, quiet, "host", c->host) < 0 ||
476 check_url_component(url, quiet, "path", c->path) < 0)
482 static int credential_from_potentially_partial_url(struct credential *c,
485 return credential_from_url_1(c, url, 1, 0);
488 int credential_from_url_gently(struct credential *c, const char *url, int quiet)
490 return credential_from_url_1(c, url, 0, quiet);
493 void credential_from_url(struct credential *c, const char *url)
495 if (credential_from_url_gently(c, url, 0) < 0)
496 die(_("credential url cannot be parsed: %s"), url);