cocci: remove 'unused.cocci'
[git.git] / credential.c
blobe6417bf88046e3afc28bc66460efc626e523ffd2
1 #include "cache.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 "urlmatch.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)
22 free(c->protocol);
23 free(c->host);
24 free(c->path);
25 free(c->username);
26 free(c->password);
27 string_list_clear(&c->helpers, 0);
28 strvec_clear(&c->wwwauth_headers);
30 credential_init(c);
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) &&
38 CHECK(host) &&
39 CHECK(path) &&
40 CHECK(username);
41 #undef CHECK
45 static int credential_from_potentially_partial_url(struct credential *c,
46 const char *url);
48 static int credential_config_callback(const char *var, const char *value,
49 void *data)
51 struct credential *c = data;
52 const char *key;
54 if (!skip_prefix(var, "credential.", &key))
55 return 0;
57 if (!value)
58 return config_error_nonbool(var);
60 if (!strcmp(key, "helper")) {
61 if (*value)
62 string_list_append(&c->helpers, value);
63 else
64 string_list_clear(&c->helpers, 0);
65 } else if (!strcmp(key, "username")) {
66 if (!c->username_from_proto) {
67 free(c->username);
68 c->username = xstrdup(value);
71 else if (!strcmp(key, "usehttppath"))
72 c->use_http_path = git_config_bool(var, value);
74 return 0;
77 static int proto_is_http(const char *s)
79 if (!s)
80 return 0;
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)
90 return 0;
93 static int match_partial_url(const char *url, void *cb)
95 struct credential *c = cb;
96 struct credential want = CREDENTIAL_INIT;
97 int matches = 0;
99 if (credential_from_potentially_partial_url(&want, url) < 0)
100 warning(_("skipping credential lookup for key: credential.%s"),
101 url);
102 else
103 matches = credential_match(&want, c);
104 credential_clear(&want);
106 return matches;
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;
115 if (!c->host)
116 die(_("refusing to work with credential missing host field"));
117 if (!c->protocol)
118 die(_("refusing to work with credential missing protocol field"));
120 if (c->configured)
121 return;
123 config.section = "credential";
124 config.key = NULL;
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;
129 config.cb = c;
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);
140 c->configured = 1;
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)
149 if (!c->protocol)
150 return;
151 strbuf_addf(out, "%s://", c->protocol);
152 if (c->username && *c->username)
153 strbuf_addf(out, "%s@", c->username);
154 if (c->host)
155 strbuf_addstr(out, c->host);
156 if (c->path)
157 strbuf_addf(out, "/%s", c->path);
160 static void credential_format(struct credential *c, struct strbuf *out)
162 if (!c->protocol)
163 return;
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, '@');
169 if (c->host)
170 strbuf_addstr(out, c->host);
171 if (c->path) {
172 strbuf_addch(out, '/');
173 strbuf_add_percentencode(out, c->path, 0);
177 static char *credential_ask_one(const char *what, struct credential *c,
178 int flags)
180 struct strbuf desc = STRBUF_INIT;
181 struct strbuf prompt = STRBUF_INIT;
182 char *r;
184 credential_describe(c, &desc);
185 if (desc.len)
186 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
187 else
188 strbuf_addf(&prompt, "%s: ", what);
190 r = git_prompt(prompt.buf, flags);
192 strbuf_release(&desc);
193 strbuf_release(&prompt);
194 return xstrdup(r);
197 static void credential_getpass(struct credential *c)
199 if (!c->username)
200 c->username = credential_ask_one("Username", c,
201 PROMPT_ASKPASS|PROMPT_ECHO);
202 if (!c->password)
203 c->password = credential_ask_one("Password", c,
204 PROMPT_ASKPASS);
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, '=');
215 if (!line.len)
216 break;
218 if (!value) {
219 warning("invalid credential line: %s", key);
220 strbuf_release(&line);
221 return -1;
223 *value++ = '\0';
225 if (!strcmp(key, "username")) {
226 free(c->username);
227 c->username = xstrdup(value);
228 c->username_from_proto = 1;
229 } else if (!strcmp(key, "password")) {
230 free(c->password);
231 c->password = xstrdup(value);
232 } else if (!strcmp(key, "protocol")) {
233 free(c->protocol);
234 c->protocol = xstrdup(value);
235 } else if (!strcmp(key, "host")) {
236 free(c->host);
237 c->host = xstrdup(value);
238 } else if (!strcmp(key, "path")) {
239 free(c->path);
240 c->path = xstrdup(value);
241 } else if (!strcmp(key, "password_expiry_utc")) {
242 errno = 0;
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);
259 return 0;
262 static void credential_write_item(FILE *fp, const char *key, const char *value,
263 int required)
265 if (!value && required)
266 BUG("credential value for %s is missing", key);
267 if (!value)
268 return;
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);
284 free(s);
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,
291 const char *cmd,
292 int want_output)
294 struct child_process helper = CHILD_PROCESS_INIT;
295 FILE *fp;
297 strvec_push(&helper.args, cmd);
298 helper.use_shell = 1;
299 helper.in = -1;
300 if (want_output)
301 helper.out = -1;
302 else
303 helper.no_stdout = 1;
305 if (start_command(&helper) < 0)
306 return -1;
308 fp = xfdopen(helper.in, "w");
309 sigchain_push(SIGPIPE, SIG_IGN);
310 credential_write(c, fp);
311 fclose(fp);
312 sigchain_pop(SIGPIPE);
314 if (want_output) {
315 int r;
316 fp = xfdopen(helper.out, "r");
317 r = credential_read(c, fp);
318 fclose(fp);
319 if (r < 0) {
320 finish_command(&helper);
321 return -1;
325 if (finish_command(&helper))
326 return -1;
327 return 0;
330 static int credential_do(struct credential *c, const char *helper,
331 const char *operation)
333 struct strbuf cmd = STRBUF_INIT;
334 int r;
336 if (helper[0] == '!')
337 strbuf_addstr(&cmd, helper + 1);
338 else if (is_absolute_path(helper))
339 strbuf_addstr(&cmd, helper);
340 else
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);
347 return r;
350 void credential_fill(struct credential *c)
352 int i;
354 if (c->username && c->password)
355 return;
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)
368 return;
369 if (c->quit)
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)
381 int i;
383 if (c->approved)
384 return;
385 if (!c->username || !c->password || c->password_expiry_utc < time(NULL))
386 return;
388 credential_apply_config(c);
390 for (i = 0; i < c->helpers.nr; i++)
391 credential_do(c, c->helpers.items[i].string, "store");
392 c->approved = 1;
395 void credential_reject(struct credential *c)
397 int i;
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;
407 c->approved = 0;
410 static int check_url_component(const char *url, int quiet,
411 const char *name, const char *value)
413 if (!value)
414 return 0;
415 if (!strchr(value, '\n'))
416 return 0;
418 if (!quiet)
419 warning(_("url contains a newline in its %s component: %s"),
420 name, url);
421 return -1;
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
430 * that, if any)
432 * - a user name and potentially a password (as "<user>[:<password>]@" part of
433 * the host name)
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;
452 credential_clear(c);
455 * Match one of:
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)) {
462 if (!quiet)
463 warning(_("url has no scheme: %s"), url);
464 return -1;
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) {
479 /* Case (1) */
480 host = cp;
482 else if (!colon || at <= colon) {
483 /* Case (2) */
484 c->username = url_decode_mem(cp, at - cp);
485 if (c->username && *c->username)
486 c->username_from_proto = 1;
487 host = at + 1;
488 } else {
489 /* Case (3) */
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));
494 host = at + 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 == '/')
503 slash++;
504 if (*slash) {
505 char *p;
506 c->path = url_decode(slash);
507 p = c->path + strlen(c->path) - 1;
508 while (p > c->path && *p == '/')
509 *p-- = '\0';
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)
517 return -1;
519 return 0;
522 static int credential_from_potentially_partial_url(struct credential *c,
523 const char *url)
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);