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) &&
40 static int credential_config_callback(const char *var, const char *value,
43 struct credential *c = data;
46 if (!skip_prefix(var, "credential.", &key))
50 return config_error_nonbool(var);
52 if (!strcmp(key, "helper")) {
54 string_list_append(&c->helpers, value);
56 string_list_clear(&c->helpers, 0);
57 } else if (!strcmp(key, "username")) {
58 if (!c->username_from_proto) {
60 c->username = xstrdup(value);
63 else if (!strcmp(key, "usehttppath"))
64 c->use_http_path = git_config_bool(var, value);
69 static int proto_is_http(const char *s)
73 return !strcmp(s, "https") || !strcmp(s, "http");
76 static void credential_describe(struct credential *c, struct strbuf *out);
77 static void credential_format(struct credential *c, struct strbuf *out);
79 static int select_all(const struct urlmatch_item *a,
80 const struct urlmatch_item *b)
85 static void credential_apply_config(struct credential *c)
88 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
89 struct strbuf url = STRBUF_INIT;
92 die(_("refusing to work with credential missing host field"));
94 die(_("refusing to work with credential missing protocol field"));
99 config.section = "credential";
101 config.collect_fn = credential_config_callback;
102 config.cascade_fn = NULL;
103 config.select_fn = select_all;
106 credential_format(c, &url);
107 normalized_url = url_normalize(url.buf, &config.url);
109 git_config(urlmatch_config_entry, &config);
110 free(normalized_url);
111 strbuf_release(&url);
115 if (!c->use_http_path && proto_is_http(c->protocol)) {
116 FREE_AND_NULL(c->path);
120 static void credential_describe(struct credential *c, struct strbuf *out)
124 strbuf_addf(out, "%s://", c->protocol);
125 if (c->username && *c->username)
126 strbuf_addf(out, "%s@", c->username);
128 strbuf_addstr(out, c->host);
130 strbuf_addf(out, "/%s", c->path);
133 static void credential_format(struct credential *c, struct strbuf *out)
137 strbuf_addf(out, "%s://", c->protocol);
138 if (c->username && *c->username) {
139 strbuf_add_percentencode(out, c->username);
140 strbuf_addch(out, '@');
143 strbuf_addstr(out, c->host);
145 strbuf_addch(out, '/');
146 strbuf_add_percentencode(out, c->path);
150 static char *credential_ask_one(const char *what, struct credential *c,
153 struct strbuf desc = STRBUF_INIT;
154 struct strbuf prompt = STRBUF_INIT;
157 credential_describe(c, &desc);
159 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
161 strbuf_addf(&prompt, "%s: ", what);
163 r = git_prompt(prompt.buf, flags);
165 strbuf_release(&desc);
166 strbuf_release(&prompt);
170 static void credential_getpass(struct credential *c)
173 c->username = credential_ask_one("Username", c,
174 PROMPT_ASKPASS|PROMPT_ECHO);
176 c->password = credential_ask_one("Password", c,
180 int credential_read(struct credential *c, FILE *fp)
182 struct strbuf line = STRBUF_INIT;
184 while (strbuf_getline_lf(&line, fp) != EOF) {
185 char *key = line.buf;
186 char *value = strchr(key, '=');
192 warning("invalid credential line: %s", key);
193 strbuf_release(&line);
198 if (!strcmp(key, "username")) {
200 c->username = xstrdup(value);
201 c->username_from_proto = 1;
202 } else if (!strcmp(key, "password")) {
204 c->password = xstrdup(value);
205 } else if (!strcmp(key, "protocol")) {
207 c->protocol = xstrdup(value);
208 } else if (!strcmp(key, "host")) {
210 c->host = xstrdup(value);
211 } else if (!strcmp(key, "path")) {
213 c->path = xstrdup(value);
214 } else if (!strcmp(key, "url")) {
215 credential_from_url(c, value);
216 } else if (!strcmp(key, "quit")) {
217 c->quit = !!git_config_bool("quit", value);
220 * Ignore other lines; we don't know what they mean, but
221 * this future-proofs us when later versions of git do
222 * learn new lines, and the helpers are updated to match.
226 strbuf_release(&line);
230 static void credential_write_item(FILE *fp, const char *key, const char *value,
233 if (!value && required)
234 BUG("credential value for %s is missing", key);
237 if (strchr(value, '\n'))
238 die("credential value for %s contains newline", key);
239 fprintf(fp, "%s=%s\n", key, value);
242 void credential_write(const struct credential *c, FILE *fp)
244 credential_write_item(fp, "protocol", c->protocol, 1);
245 credential_write_item(fp, "host", c->host, 1);
246 credential_write_item(fp, "path", c->path, 0);
247 credential_write_item(fp, "username", c->username, 0);
248 credential_write_item(fp, "password", c->password, 0);
251 static int run_credential_helper(struct credential *c,
255 struct child_process helper = CHILD_PROCESS_INIT;
256 const char *argv[] = { NULL, NULL };
261 helper.use_shell = 1;
266 helper.no_stdout = 1;
268 if (start_command(&helper) < 0)
271 fp = xfdopen(helper.in, "w");
272 sigchain_push(SIGPIPE, SIG_IGN);
273 credential_write(c, fp);
275 sigchain_pop(SIGPIPE);
279 fp = xfdopen(helper.out, "r");
280 r = credential_read(c, fp);
283 finish_command(&helper);
288 if (finish_command(&helper))
293 static int credential_do(struct credential *c, const char *helper,
294 const char *operation)
296 struct strbuf cmd = STRBUF_INIT;
299 if (helper[0] == '!')
300 strbuf_addstr(&cmd, helper + 1);
301 else if (is_absolute_path(helper))
302 strbuf_addstr(&cmd, helper);
304 strbuf_addf(&cmd, "git credential-%s", helper);
306 strbuf_addf(&cmd, " %s", operation);
307 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
309 strbuf_release(&cmd);
313 void credential_fill(struct credential *c)
317 if (c->username && c->password)
320 credential_apply_config(c);
322 for (i = 0; i < c->helpers.nr; i++) {
323 credential_do(c, c->helpers.items[i].string, "get");
324 if (c->username && c->password)
327 die("credential helper '%s' told us to quit",
328 c->helpers.items[i].string);
331 credential_getpass(c);
332 if (!c->username && !c->password)
333 die("unable to get password from user");
336 void credential_approve(struct credential *c)
342 if (!c->username || !c->password)
345 credential_apply_config(c);
347 for (i = 0; i < c->helpers.nr; i++)
348 credential_do(c, c->helpers.items[i].string, "store");
352 void credential_reject(struct credential *c)
356 credential_apply_config(c);
358 for (i = 0; i < c->helpers.nr; i++)
359 credential_do(c, c->helpers.items[i].string, "erase");
361 FREE_AND_NULL(c->username);
362 FREE_AND_NULL(c->password);
366 static int check_url_component(const char *url, int quiet,
367 const char *name, const char *value)
371 if (!strchr(value, '\n'))
375 warning(_("url contains a newline in its %s component: %s"),
381 * Potentially-partial URLs can, but do not have to, contain
383 * - a protocol (or scheme) of the form "<protocol>://"
385 * - a host name (the part after the protocol and before the first slash after
388 * - a user name and potentially a password (as "<user>[:<password>]@" part of
391 * - a path (the part after the host name, if any, starting with the slash)
393 * Missing parts will be left unset in `struct credential`. Thus, `https://`
394 * will have only the `protocol` set, `example.com` only the host name, and
395 * `/git` only the path.
397 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
398 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
399 * be potentially partial, and only then (otherwise, the empty string is used).
401 * The credential_from_url() function does not allow partial URLs.
403 static int credential_from_url_1(struct credential *c, const char *url,
404 int allow_partial_url, int quiet)
406 const char *at, *colon, *cp, *slash, *host, *proto_end;
412 * (1) proto://<host>/...
413 * (2) proto://<user>@<host>/...
414 * (3) proto://<user>:<pass>@<host>/...
416 proto_end = strstr(url, "://");
417 if (!allow_partial_url && (!proto_end || proto_end == url)) {
419 warning(_("url has no scheme: %s"), url);
422 cp = proto_end ? proto_end + 3 : url;
423 at = strchr(cp, '@');
424 colon = strchr(cp, ':');
425 slash = strchrnul(cp, '/');
427 if (!at || slash <= at) {
431 else if (!colon || at <= colon) {
433 c->username = url_decode_mem(cp, at - cp);
434 if (c->username && *c->username)
435 c->username_from_proto = 1;
439 c->username = url_decode_mem(cp, colon - cp);
440 if (c->username && *c->username)
441 c->username_from_proto = 1;
442 c->password = url_decode_mem(colon + 1, at - (colon + 1));
446 if (proto_end && proto_end - url > 0)
447 c->protocol = xmemdupz(url, proto_end - url);
448 if (!allow_partial_url || slash - host > 0)
449 c->host = url_decode_mem(host, slash - host);
450 /* Trim leading and trailing slashes from path */
451 while (*slash == '/')
455 c->path = url_decode(slash);
456 p = c->path + strlen(c->path) - 1;
457 while (p > c->path && *p == '/')
461 if (check_url_component(url, quiet, "username", c->username) < 0 ||
462 check_url_component(url, quiet, "password", c->password) < 0 ||
463 check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
464 check_url_component(url, quiet, "host", c->host) < 0 ||
465 check_url_component(url, quiet, "path", c->path) < 0)
471 int credential_from_url_gently(struct credential *c, const char *url, int quiet)
473 return credential_from_url_1(c, url, 0, quiet);
476 void credential_from_url(struct credential *c, const char *url)
478 if (credential_from_url_gently(c, url, 0) < 0)
479 die(_("credential url cannot be parsed: %s"), url);