Merge branch 'pb/complete-diff-options'
[git/debian.git] / credential.c
blob8825c6f132018c8527563c4ca0f1a1ea747b92e0
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "credential.h"
5 #include "gettext.h"
6 #include "string-list.h"
7 #include "run-command.h"
8 #include "url.h"
9 #include "prompt.h"
10 #include "sigchain.h"
11 #include "strbuf.h"
12 #include "urlmatch.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)
23 free(c->protocol);
24 free(c->host);
25 free(c->path);
26 free(c->username);
27 free(c->password);
28 free(c->oauth_refresh_token);
29 string_list_clear(&c->helpers, 0);
30 strvec_clear(&c->wwwauth_headers);
32 credential_init(c);
35 int credential_match(const struct credential *want,
36 const struct credential *have, int match_password)
38 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
39 return CHECK(protocol) &&
40 CHECK(host) &&
41 CHECK(path) &&
42 CHECK(username) &&
43 (!match_password || CHECK(password));
44 #undef CHECK
48 static int credential_from_potentially_partial_url(struct credential *c,
49 const char *url);
51 static int credential_config_callback(const char *var, const char *value,
52 void *data)
54 struct credential *c = data;
55 const char *key;
57 if (!skip_prefix(var, "credential.", &key))
58 return 0;
60 if (!value)
61 return config_error_nonbool(var);
63 if (!strcmp(key, "helper")) {
64 if (*value)
65 string_list_append(&c->helpers, value);
66 else
67 string_list_clear(&c->helpers, 0);
68 } else if (!strcmp(key, "username")) {
69 if (!c->username_from_proto) {
70 free(c->username);
71 c->username = xstrdup(value);
74 else if (!strcmp(key, "usehttppath"))
75 c->use_http_path = git_config_bool(var, value);
77 return 0;
80 static int proto_is_http(const char *s)
82 if (!s)
83 return 0;
84 return !strcmp(s, "https") || !strcmp(s, "http");
87 static void credential_describe(struct credential *c, struct strbuf *out);
88 static void credential_format(struct credential *c, struct strbuf *out);
90 static int select_all(const struct urlmatch_item *a,
91 const struct urlmatch_item *b)
93 return 0;
96 static int match_partial_url(const char *url, void *cb)
98 struct credential *c = cb;
99 struct credential want = CREDENTIAL_INIT;
100 int matches = 0;
102 if (credential_from_potentially_partial_url(&want, url) < 0)
103 warning(_("skipping credential lookup for key: credential.%s"),
104 url);
105 else
106 matches = credential_match(&want, c, 0);
107 credential_clear(&want);
109 return matches;
112 static void credential_apply_config(struct credential *c)
114 char *normalized_url;
115 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
116 struct strbuf url = STRBUF_INIT;
118 if (!c->host)
119 die(_("refusing to work with credential missing host field"));
120 if (!c->protocol)
121 die(_("refusing to work with credential missing protocol field"));
123 if (c->configured)
124 return;
126 config.section = "credential";
127 config.key = NULL;
128 config.collect_fn = credential_config_callback;
129 config.cascade_fn = NULL;
130 config.select_fn = select_all;
131 config.fallback_match_fn = match_partial_url;
132 config.cb = c;
134 credential_format(c, &url);
135 normalized_url = url_normalize(url.buf, &config.url);
137 git_config(urlmatch_config_entry, &config);
138 string_list_clear(&config.vars, 1);
139 free(normalized_url);
140 urlmatch_config_release(&config);
141 strbuf_release(&url);
143 c->configured = 1;
145 if (!c->use_http_path && proto_is_http(c->protocol)) {
146 FREE_AND_NULL(c->path);
150 static void credential_describe(struct credential *c, struct strbuf *out)
152 if (!c->protocol)
153 return;
154 strbuf_addf(out, "%s://", c->protocol);
155 if (c->username && *c->username)
156 strbuf_addf(out, "%s@", c->username);
157 if (c->host)
158 strbuf_addstr(out, c->host);
159 if (c->path)
160 strbuf_addf(out, "/%s", c->path);
163 static void credential_format(struct credential *c, struct strbuf *out)
165 if (!c->protocol)
166 return;
167 strbuf_addf(out, "%s://", c->protocol);
168 if (c->username && *c->username) {
169 strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
170 strbuf_addch(out, '@');
172 if (c->host)
173 strbuf_addstr(out, c->host);
174 if (c->path) {
175 strbuf_addch(out, '/');
176 strbuf_add_percentencode(out, c->path, 0);
180 static char *credential_ask_one(const char *what, struct credential *c,
181 int flags)
183 struct strbuf desc = STRBUF_INIT;
184 struct strbuf prompt = STRBUF_INIT;
185 char *r;
187 credential_describe(c, &desc);
188 if (desc.len)
189 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
190 else
191 strbuf_addf(&prompt, "%s: ", what);
193 r = git_prompt(prompt.buf, flags);
195 strbuf_release(&desc);
196 strbuf_release(&prompt);
197 return xstrdup(r);
200 static void credential_getpass(struct credential *c)
202 if (!c->username)
203 c->username = credential_ask_one("Username", c,
204 PROMPT_ASKPASS|PROMPT_ECHO);
205 if (!c->password)
206 c->password = credential_ask_one("Password", c,
207 PROMPT_ASKPASS);
210 int credential_read(struct credential *c, FILE *fp)
212 struct strbuf line = STRBUF_INIT;
214 while (strbuf_getline(&line, fp) != EOF) {
215 char *key = line.buf;
216 char *value = strchr(key, '=');
218 if (!line.len)
219 break;
221 if (!value) {
222 warning("invalid credential line: %s", key);
223 strbuf_release(&line);
224 return -1;
226 *value++ = '\0';
228 if (!strcmp(key, "username")) {
229 free(c->username);
230 c->username = xstrdup(value);
231 c->username_from_proto = 1;
232 } else if (!strcmp(key, "password")) {
233 free(c->password);
234 c->password = xstrdup(value);
235 } else if (!strcmp(key, "protocol")) {
236 free(c->protocol);
237 c->protocol = xstrdup(value);
238 } else if (!strcmp(key, "host")) {
239 free(c->host);
240 c->host = xstrdup(value);
241 } else if (!strcmp(key, "path")) {
242 free(c->path);
243 c->path = xstrdup(value);
244 } else if (!strcmp(key, "wwwauth[]")) {
245 strvec_push(&c->wwwauth_headers, value);
246 } else if (!strcmp(key, "password_expiry_utc")) {
247 errno = 0;
248 c->password_expiry_utc = parse_timestamp(value, NULL, 10);
249 if (c->password_expiry_utc == 0 || errno == ERANGE)
250 c->password_expiry_utc = TIME_MAX;
251 } else if (!strcmp(key, "oauth_refresh_token")) {
252 free(c->oauth_refresh_token);
253 c->oauth_refresh_token = xstrdup(value);
254 } else if (!strcmp(key, "url")) {
255 credential_from_url(c, value);
256 } else if (!strcmp(key, "quit")) {
257 c->quit = !!git_config_bool("quit", value);
260 * Ignore other lines; we don't know what they mean, but
261 * this future-proofs us when later versions of git do
262 * learn new lines, and the helpers are updated to match.
266 strbuf_release(&line);
267 return 0;
270 static void credential_write_item(FILE *fp, const char *key, const char *value,
271 int required)
273 if (!value && required)
274 BUG("credential value for %s is missing", key);
275 if (!value)
276 return;
277 if (strchr(value, '\n'))
278 die("credential value for %s contains newline", key);
279 fprintf(fp, "%s=%s\n", key, value);
282 void credential_write(const struct credential *c, FILE *fp)
284 credential_write_item(fp, "protocol", c->protocol, 1);
285 credential_write_item(fp, "host", c->host, 1);
286 credential_write_item(fp, "path", c->path, 0);
287 credential_write_item(fp, "username", c->username, 0);
288 credential_write_item(fp, "password", c->password, 0);
289 credential_write_item(fp, "oauth_refresh_token", c->oauth_refresh_token, 0);
290 if (c->password_expiry_utc != TIME_MAX) {
291 char *s = xstrfmt("%"PRItime, c->password_expiry_utc);
292 credential_write_item(fp, "password_expiry_utc", s, 0);
293 free(s);
295 for (size_t i = 0; i < c->wwwauth_headers.nr; i++)
296 credential_write_item(fp, "wwwauth[]", c->wwwauth_headers.v[i], 0);
299 static int run_credential_helper(struct credential *c,
300 const char *cmd,
301 int want_output)
303 struct child_process helper = CHILD_PROCESS_INIT;
304 FILE *fp;
306 strvec_push(&helper.args, cmd);
307 helper.use_shell = 1;
308 helper.in = -1;
309 if (want_output)
310 helper.out = -1;
311 else
312 helper.no_stdout = 1;
314 if (start_command(&helper) < 0)
315 return -1;
317 fp = xfdopen(helper.in, "w");
318 sigchain_push(SIGPIPE, SIG_IGN);
319 credential_write(c, fp);
320 fclose(fp);
321 sigchain_pop(SIGPIPE);
323 if (want_output) {
324 int r;
325 fp = xfdopen(helper.out, "r");
326 r = credential_read(c, fp);
327 fclose(fp);
328 if (r < 0) {
329 finish_command(&helper);
330 return -1;
334 if (finish_command(&helper))
335 return -1;
336 return 0;
339 static int credential_do(struct credential *c, const char *helper,
340 const char *operation)
342 struct strbuf cmd = STRBUF_INIT;
343 int r;
345 if (helper[0] == '!')
346 strbuf_addstr(&cmd, helper + 1);
347 else if (is_absolute_path(helper))
348 strbuf_addstr(&cmd, helper);
349 else
350 strbuf_addf(&cmd, "git credential-%s", helper);
352 strbuf_addf(&cmd, " %s", operation);
353 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
355 strbuf_release(&cmd);
356 return r;
359 void credential_fill(struct credential *c)
361 int i;
363 if (c->username && c->password)
364 return;
366 credential_apply_config(c);
368 for (i = 0; i < c->helpers.nr; i++) {
369 credential_do(c, c->helpers.items[i].string, "get");
370 if (c->password_expiry_utc < time(NULL)) {
371 /* Discard expired password */
372 FREE_AND_NULL(c->password);
373 /* Reset expiry to maintain consistency */
374 c->password_expiry_utc = TIME_MAX;
376 if (c->username && c->password)
377 return;
378 if (c->quit)
379 die("credential helper '%s' told us to quit",
380 c->helpers.items[i].string);
383 credential_getpass(c);
384 if (!c->username && !c->password)
385 die("unable to get password from user");
388 void credential_approve(struct credential *c)
390 int i;
392 if (c->approved)
393 return;
394 if (!c->username || !c->password || c->password_expiry_utc < time(NULL))
395 return;
397 credential_apply_config(c);
399 for (i = 0; i < c->helpers.nr; i++)
400 credential_do(c, c->helpers.items[i].string, "store");
401 c->approved = 1;
404 void credential_reject(struct credential *c)
406 int i;
408 credential_apply_config(c);
410 for (i = 0; i < c->helpers.nr; i++)
411 credential_do(c, c->helpers.items[i].string, "erase");
413 FREE_AND_NULL(c->username);
414 FREE_AND_NULL(c->password);
415 FREE_AND_NULL(c->oauth_refresh_token);
416 c->password_expiry_utc = TIME_MAX;
417 c->approved = 0;
420 static int check_url_component(const char *url, int quiet,
421 const char *name, const char *value)
423 if (!value)
424 return 0;
425 if (!strchr(value, '\n'))
426 return 0;
428 if (!quiet)
429 warning(_("url contains a newline in its %s component: %s"),
430 name, url);
431 return -1;
435 * Potentially-partial URLs can, but do not have to, contain
437 * - a protocol (or scheme) of the form "<protocol>://"
439 * - a host name (the part after the protocol and before the first slash after
440 * that, if any)
442 * - a user name and potentially a password (as "<user>[:<password>]@" part of
443 * the host name)
445 * - a path (the part after the host name, if any, starting with the slash)
447 * Missing parts will be left unset in `struct credential`. Thus, `https://`
448 * will have only the `protocol` set, `example.com` only the host name, and
449 * `/git` only the path.
451 * Note that an empty host name in an otherwise fully-qualified URL (e.g.
452 * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
453 * be potentially partial, and only then (otherwise, the empty string is used).
455 * The credential_from_url() function does not allow partial URLs.
457 static int credential_from_url_1(struct credential *c, const char *url,
458 int allow_partial_url, int quiet)
460 const char *at, *colon, *cp, *slash, *host, *proto_end;
462 credential_clear(c);
465 * Match one of:
466 * (1) proto://<host>/...
467 * (2) proto://<user>@<host>/...
468 * (3) proto://<user>:<pass>@<host>/...
470 proto_end = strstr(url, "://");
471 if (!allow_partial_url && (!proto_end || proto_end == url)) {
472 if (!quiet)
473 warning(_("url has no scheme: %s"), url);
474 return -1;
476 cp = proto_end ? proto_end + 3 : url;
477 at = strchr(cp, '@');
478 colon = strchr(cp, ':');
481 * A query or fragment marker before the slash ends the host portion.
482 * We'll just continue to call this "slash" for simplicity. Notably our
483 * "trim leading slashes" part won't skip over this part of the path,
484 * but that's what we'd want.
486 slash = cp + strcspn(cp, "/?#");
488 if (!at || slash <= at) {
489 /* Case (1) */
490 host = cp;
492 else if (!colon || at <= colon) {
493 /* Case (2) */
494 c->username = url_decode_mem(cp, at - cp);
495 if (c->username && *c->username)
496 c->username_from_proto = 1;
497 host = at + 1;
498 } else {
499 /* Case (3) */
500 c->username = url_decode_mem(cp, colon - cp);
501 if (c->username && *c->username)
502 c->username_from_proto = 1;
503 c->password = url_decode_mem(colon + 1, at - (colon + 1));
504 host = at + 1;
507 if (proto_end && proto_end - url > 0)
508 c->protocol = xmemdupz(url, proto_end - url);
509 if (!allow_partial_url || slash - host > 0)
510 c->host = url_decode_mem(host, slash - host);
511 /* Trim leading and trailing slashes from path */
512 while (*slash == '/')
513 slash++;
514 if (*slash) {
515 char *p;
516 c->path = url_decode(slash);
517 p = c->path + strlen(c->path) - 1;
518 while (p > c->path && *p == '/')
519 *p-- = '\0';
522 if (check_url_component(url, quiet, "username", c->username) < 0 ||
523 check_url_component(url, quiet, "password", c->password) < 0 ||
524 check_url_component(url, quiet, "protocol", c->protocol) < 0 ||
525 check_url_component(url, quiet, "host", c->host) < 0 ||
526 check_url_component(url, quiet, "path", c->path) < 0)
527 return -1;
529 return 0;
532 static int credential_from_potentially_partial_url(struct credential *c,
533 const char *url)
535 return credential_from_url_1(c, url, 1, 0);
538 int credential_from_url_gently(struct credential *c, const char *url, int quiet)
540 return credential_from_url_1(c, url, 0, quiet);
543 void credential_from_url(struct credential *c, const char *url)
545 if (credential_from_url_gently(c, url, 0) < 0)
546 die(_("credential url cannot be parsed: %s"), url);