| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 1 | #include "cache.h" |
| Brandon Williams | b2141fc | 2017-06-14 18:07:36 | [diff] [blame] | 2 | #include "config.h" |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 3 | #include "credential.h" |
| 4 | #include "string-list.h" |
| 5 | #include "run-command.h" |
| Jeff King | d3e847c | 2011-12-10 10:31:17 | [diff] [blame] | 6 | #include "url.h" |
| Jeff King | d3c58b8 | 2011-12-10 10:40:54 | [diff] [blame] | 7 | #include "prompt.h" |
| Erik E Brady | a0d51e8 | 2018-03-29 18:00:56 | [diff] [blame] | 8 | #include "sigchain.h" |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 9 | |
| 10 | void credential_init(struct credential *c) |
| 11 | { |
| 12 | memset(c, 0, sizeof(*c)); |
| 13 | c->helpers.strdup_strings = 1; |
| 14 | } |
| 15 | |
| 16 | void credential_clear(struct credential *c) |
| 17 | { |
| 18 | free(c->protocol); |
| 19 | free(c->host); |
| 20 | free(c->path); |
| 21 | free(c->username); |
| 22 | free(c->password); |
| 23 | string_list_clear(&c->helpers, 0); |
| 24 | |
| 25 | credential_init(c); |
| 26 | } |
| 27 | |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 28 | int credential_match(const struct credential *want, |
| 29 | const struct credential *have) |
| 30 | { |
| 31 | #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x))) |
| 32 | return CHECK(protocol) && |
| 33 | CHECK(host) && |
| 34 | CHECK(path) && |
| 35 | CHECK(username); |
| 36 | #undef CHECK |
| 37 | } |
| 38 | |
| 39 | static int credential_config_callback(const char *var, const char *value, |
| 40 | void *data) |
| 41 | { |
| 42 | struct credential *c = data; |
| 43 | const char *key, *dot; |
| 44 | |
| Jeff King | cf4fff5 | 2014-06-18 19:44:19 | [diff] [blame] | 45 | if (!skip_prefix(var, "credential.", &key)) |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 46 | return 0; |
| 47 | |
| 48 | if (!value) |
| 49 | return config_error_nonbool(var); |
| 50 | |
| 51 | dot = strrchr(key, '.'); |
| 52 | if (dot) { |
| 53 | struct credential want = CREDENTIAL_INIT; |
| 54 | char *url = xmemdupz(key, dot - key); |
| 55 | int matched; |
| 56 | |
| 57 | credential_from_url(&want, url); |
| 58 | matched = credential_match(&want, c); |
| 59 | |
| 60 | credential_clear(&want); |
| 61 | free(url); |
| 62 | |
| 63 | if (!matched) |
| 64 | return 0; |
| 65 | key = dot + 1; |
| 66 | } |
| 67 | |
| Jeff King | 2432137 | 2016-02-26 10:51:35 | [diff] [blame] | 68 | if (!strcmp(key, "helper")) { |
| 69 | if (*value) |
| 70 | string_list_append(&c->helpers, value); |
| 71 | else |
| 72 | string_list_clear(&c->helpers, 0); |
| 73 | } else if (!strcmp(key, "username")) { |
| Jeff King | d574242 | 2011-12-10 10:31:30 | [diff] [blame] | 74 | if (!c->username) |
| 75 | c->username = xstrdup(value); |
| 76 | } |
| Jeff King | a78fbb4 | 2011-12-10 10:31:34 | [diff] [blame] | 77 | else if (!strcmp(key, "usehttppath")) |
| 78 | c->use_http_path = git_config_bool(var, value); |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| Jeff King | a78fbb4 | 2011-12-10 10:31:34 | [diff] [blame] | 83 | static int proto_is_http(const char *s) |
| 84 | { |
| 85 | if (!s) |
| 86 | return 0; |
| 87 | return !strcmp(s, "https") || !strcmp(s, "http"); |
| 88 | } |
| 89 | |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 90 | static void credential_apply_config(struct credential *c) |
| 91 | { |
| Jeff King | 8ba8ed5 | 2020-04-19 03:50:48 | [diff] [blame] | 92 | if (!c->host) |
| 93 | die(_("refusing to work with credential missing host field")); |
| 94 | if (!c->protocol) |
| 95 | die(_("refusing to work with credential missing protocol field")); |
| 96 | |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 97 | if (c->configured) |
| 98 | return; |
| 99 | git_config(credential_config_callback, c); |
| 100 | c->configured = 1; |
| Jeff King | a78fbb4 | 2011-12-10 10:31:34 | [diff] [blame] | 101 | |
| 102 | if (!c->use_http_path && proto_is_http(c->protocol)) { |
| Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 | [diff] [blame] | 103 | FREE_AND_NULL(c->path); |
| Jeff King | a78fbb4 | 2011-12-10 10:31:34 | [diff] [blame] | 104 | } |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 105 | } |
| 106 | |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 107 | static void credential_describe(struct credential *c, struct strbuf *out) |
| 108 | { |
| 109 | if (!c->protocol) |
| 110 | return; |
| 111 | strbuf_addf(out, "%s://", c->protocol); |
| 112 | if (c->username && *c->username) |
| 113 | strbuf_addf(out, "%s@", c->username); |
| 114 | if (c->host) |
| 115 | strbuf_addstr(out, c->host); |
| 116 | if (c->path) |
| 117 | strbuf_addf(out, "/%s", c->path); |
| 118 | } |
| 119 | |
| Jeff King | ce77aa4 | 2011-12-10 10:41:23 | [diff] [blame] | 120 | static char *credential_ask_one(const char *what, struct credential *c, |
| 121 | int flags) |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 122 | { |
| 123 | struct strbuf desc = STRBUF_INIT; |
| 124 | struct strbuf prompt = STRBUF_INIT; |
| 125 | char *r; |
| 126 | |
| 127 | credential_describe(c, &desc); |
| 128 | if (desc.len) |
| 129 | strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf); |
| 130 | else |
| 131 | strbuf_addf(&prompt, "%s: ", what); |
| 132 | |
| Jeff King | ce77aa4 | 2011-12-10 10:41:23 | [diff] [blame] | 133 | r = git_prompt(prompt.buf, flags); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 134 | |
| 135 | strbuf_release(&desc); |
| 136 | strbuf_release(&prompt); |
| 137 | return xstrdup(r); |
| 138 | } |
| 139 | |
| 140 | static void credential_getpass(struct credential *c) |
| 141 | { |
| 142 | if (!c->username) |
| Jeff King | ce77aa4 | 2011-12-10 10:41:23 | [diff] [blame] | 143 | c->username = credential_ask_one("Username", c, |
| 144 | PROMPT_ASKPASS|PROMPT_ECHO); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 145 | if (!c->password) |
| Jeff King | ce77aa4 | 2011-12-10 10:41:23 | [diff] [blame] | 146 | c->password = credential_ask_one("Password", c, |
| 147 | PROMPT_ASKPASS); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int credential_read(struct credential *c, FILE *fp) |
| 151 | { |
| 152 | struct strbuf line = STRBUF_INIT; |
| 153 | |
| Junio C Hamano | 8f309ae | 2016-01-13 23:31:17 | [diff] [blame] | 154 | while (strbuf_getline_lf(&line, fp) != EOF) { |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 155 | char *key = line.buf; |
| 156 | char *value = strchr(key, '='); |
| 157 | |
| 158 | if (!line.len) |
| 159 | break; |
| 160 | |
| 161 | if (!value) { |
| 162 | warning("invalid credential line: %s", key); |
| 163 | strbuf_release(&line); |
| 164 | return -1; |
| 165 | } |
| 166 | *value++ = '\0'; |
| 167 | |
| 168 | if (!strcmp(key, "username")) { |
| 169 | free(c->username); |
| 170 | c->username = xstrdup(value); |
| 171 | } else if (!strcmp(key, "password")) { |
| 172 | free(c->password); |
| 173 | c->password = xstrdup(value); |
| 174 | } else if (!strcmp(key, "protocol")) { |
| 175 | free(c->protocol); |
| 176 | c->protocol = xstrdup(value); |
| 177 | } else if (!strcmp(key, "host")) { |
| 178 | free(c->host); |
| 179 | c->host = xstrdup(value); |
| 180 | } else if (!strcmp(key, "path")) { |
| 181 | free(c->path); |
| 182 | c->path = xstrdup(value); |
| Jeff King | 9c183a7 | 2012-07-18 12:06:26 | [diff] [blame] | 183 | } else if (!strcmp(key, "url")) { |
| 184 | credential_from_url(c, value); |
| Jeff King | 59b3865 | 2014-12-04 03:46:48 | [diff] [blame] | 185 | } else if (!strcmp(key, "quit")) { |
| 186 | c->quit = !!git_config_bool("quit", value); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 187 | } |
| 188 | /* |
| 189 | * Ignore other lines; we don't know what they mean, but |
| 190 | * this future-proofs us when later versions of git do |
| 191 | * learn new lines, and the helpers are updated to match. |
| 192 | */ |
| 193 | } |
| 194 | |
| 195 | strbuf_release(&line); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| Jeff King | 8ba8ed5 | 2020-04-19 03:50:48 | [diff] [blame] | 199 | static void credential_write_item(FILE *fp, const char *key, const char *value, |
| 200 | int required) |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 201 | { |
| Jeff King | 8ba8ed5 | 2020-04-19 03:50:48 | [diff] [blame] | 202 | if (!value && required) |
| 203 | BUG("credential value for %s is missing", key); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 204 | if (!value) |
| 205 | return; |
| Jeff King | 9a6bbee | 2020-03-11 21:53:41 | [diff] [blame] | 206 | if (strchr(value, '\n')) |
| 207 | die("credential value for %s contains newline", key); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 208 | fprintf(fp, "%s=%s\n", key, value); |
| 209 | } |
| 210 | |
| Matthieu Moy | 2d6dc18 | 2012-06-24 11:40:00 | [diff] [blame] | 211 | void credential_write(const struct credential *c, FILE *fp) |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 212 | { |
| Jeff King | 8ba8ed5 | 2020-04-19 03:50:48 | [diff] [blame] | 213 | credential_write_item(fp, "protocol", c->protocol, 1); |
| 214 | credential_write_item(fp, "host", c->host, 1); |
| 215 | credential_write_item(fp, "path", c->path, 0); |
| 216 | credential_write_item(fp, "username", c->username, 0); |
| 217 | credential_write_item(fp, "password", c->password, 0); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int run_credential_helper(struct credential *c, |
| 221 | const char *cmd, |
| 222 | int want_output) |
| 223 | { |
| René Scharfe | d318027 | 2014-08-19 19:09:35 | [diff] [blame] | 224 | struct child_process helper = CHILD_PROCESS_INIT; |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 225 | const char *argv[] = { NULL, NULL }; |
| 226 | FILE *fp; |
| 227 | |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 228 | argv[0] = cmd; |
| 229 | helper.argv = argv; |
| 230 | helper.use_shell = 1; |
| 231 | helper.in = -1; |
| 232 | if (want_output) |
| 233 | helper.out = -1; |
| 234 | else |
| 235 | helper.no_stdout = 1; |
| 236 | |
| 237 | if (start_command(&helper) < 0) |
| 238 | return -1; |
| 239 | |
| 240 | fp = xfdopen(helper.in, "w"); |
| Erik E Brady | a0d51e8 | 2018-03-29 18:00:56 | [diff] [blame] | 241 | sigchain_push(SIGPIPE, SIG_IGN); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 242 | credential_write(c, fp); |
| 243 | fclose(fp); |
| Erik E Brady | a0d51e8 | 2018-03-29 18:00:56 | [diff] [blame] | 244 | sigchain_pop(SIGPIPE); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 245 | |
| 246 | if (want_output) { |
| 247 | int r; |
| 248 | fp = xfdopen(helper.out, "r"); |
| 249 | r = credential_read(c, fp); |
| 250 | fclose(fp); |
| 251 | if (r < 0) { |
| 252 | finish_command(&helper); |
| 253 | return -1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (finish_command(&helper)) |
| 258 | return -1; |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int credential_do(struct credential *c, const char *helper, |
| 263 | const char *operation) |
| 264 | { |
| 265 | struct strbuf cmd = STRBUF_INIT; |
| 266 | int r; |
| 267 | |
| 268 | if (helper[0] == '!') |
| 269 | strbuf_addstr(&cmd, helper + 1); |
| 270 | else if (is_absolute_path(helper)) |
| 271 | strbuf_addstr(&cmd, helper); |
| 272 | else |
| 273 | strbuf_addf(&cmd, "git credential-%s", helper); |
| 274 | |
| 275 | strbuf_addf(&cmd, " %s", operation); |
| 276 | r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get")); |
| 277 | |
| 278 | strbuf_release(&cmd); |
| 279 | return r; |
| 280 | } |
| 281 | |
| 282 | void credential_fill(struct credential *c) |
| 283 | { |
| 284 | int i; |
| 285 | |
| 286 | if (c->username && c->password) |
| 287 | return; |
| 288 | |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 289 | credential_apply_config(c); |
| 290 | |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 291 | for (i = 0; i < c->helpers.nr; i++) { |
| 292 | credential_do(c, c->helpers.items[i].string, "get"); |
| 293 | if (c->username && c->password) |
| 294 | return; |
| Jeff King | 59b3865 | 2014-12-04 03:46:48 | [diff] [blame] | 295 | if (c->quit) |
| 296 | die("credential helper '%s' told us to quit", |
| 297 | c->helpers.items[i].string); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | credential_getpass(c); |
| 301 | if (!c->username && !c->password) |
| 302 | die("unable to get password from user"); |
| 303 | } |
| 304 | |
| 305 | void credential_approve(struct credential *c) |
| 306 | { |
| 307 | int i; |
| 308 | |
| 309 | if (c->approved) |
| 310 | return; |
| 311 | if (!c->username || !c->password) |
| 312 | return; |
| 313 | |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 314 | credential_apply_config(c); |
| 315 | |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 316 | for (i = 0; i < c->helpers.nr; i++) |
| 317 | credential_do(c, c->helpers.items[i].string, "store"); |
| 318 | c->approved = 1; |
| 319 | } |
| 320 | |
| 321 | void credential_reject(struct credential *c) |
| 322 | { |
| 323 | int i; |
| 324 | |
| Jeff King | 1182507 | 2011-12-10 10:31:24 | [diff] [blame] | 325 | credential_apply_config(c); |
| 326 | |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 327 | for (i = 0; i < c->helpers.nr; i++) |
| 328 | credential_do(c, c->helpers.items[i].string, "erase"); |
| 329 | |
| Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 | [diff] [blame] | 330 | FREE_AND_NULL(c->username); |
| 331 | FREE_AND_NULL(c->password); |
| Jeff King | abca927 | 2011-12-10 10:31:11 | [diff] [blame] | 332 | c->approved = 0; |
| 333 | } |
| Jeff King | d3e847c | 2011-12-10 10:31:17 | [diff] [blame] | 334 | |
| Jeff King | c716fe4 | 2020-03-12 05:31:11 | [diff] [blame] | 335 | static int check_url_component(const char *url, int quiet, |
| 336 | const char *name, const char *value) |
| 337 | { |
| 338 | if (!value) |
| 339 | return 0; |
| 340 | if (!strchr(value, '\n')) |
| 341 | return 0; |
| 342 | |
| 343 | if (!quiet) |
| 344 | warning(_("url contains a newline in its %s component: %s"), |
| 345 | name, url); |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | int credential_from_url_gently(struct credential *c, const char *url, |
| 350 | int quiet) |
| Jeff King | d3e847c | 2011-12-10 10:31:17 | [diff] [blame] | 351 | { |
| 352 | const char *at, *colon, *cp, *slash, *host, *proto_end; |
| 353 | |
| 354 | credential_clear(c); |
| 355 | |
| 356 | /* |
| 357 | * Match one of: |
| 358 | * (1) proto://<host>/... |
| 359 | * (2) proto://<user>@<host>/... |
| 360 | * (3) proto://<user>:<pass>@<host>/... |
| 361 | */ |
| 362 | proto_end = strstr(url, "://"); |
| Jonathan Nieder | e7fab62 | 2020-04-19 03:54:57 | [diff] [blame] | 363 | if (!proto_end || proto_end == url) { |
| Jonathan Nieder | c44088e | 2020-04-19 03:54:13 | [diff] [blame] | 364 | if (!quiet) |
| 365 | warning(_("url has no scheme: %s"), url); |
| 366 | return -1; |
| 367 | } |
| Jeff King | d3e847c | 2011-12-10 10:31:17 | [diff] [blame] | 368 | cp = proto_end + 3; |
| 369 | at = strchr(cp, '@'); |
| 370 | colon = strchr(cp, ':'); |
| 371 | slash = strchrnul(cp, '/'); |
| 372 | |
| 373 | if (!at || slash <= at) { |
| 374 | /* Case (1) */ |
| 375 | host = cp; |
| 376 | } |
| 377 | else if (!colon || at <= colon) { |
| 378 | /* Case (2) */ |
| 379 | c->username = url_decode_mem(cp, at - cp); |
| 380 | host = at + 1; |
| 381 | } else { |
| 382 | /* Case (3) */ |
| 383 | c->username = url_decode_mem(cp, colon - cp); |
| 384 | c->password = url_decode_mem(colon + 1, at - (colon + 1)); |
| 385 | host = at + 1; |
| 386 | } |
| 387 | |
| Jonathan Nieder | e7fab62 | 2020-04-19 03:54:57 | [diff] [blame] | 388 | c->protocol = xmemdupz(url, proto_end - url); |
| Jeff King | 2403668 | 2020-04-19 03:48:05 | [diff] [blame] | 389 | c->host = url_decode_mem(host, slash - host); |
| Jeff King | d3e847c | 2011-12-10 10:31:17 | [diff] [blame] | 390 | /* Trim leading and trailing slashes from path */ |
| 391 | while (*slash == '/') |
| 392 | slash++; |
| 393 | if (*slash) { |
| 394 | char *p; |
| 395 | c->path = url_decode(slash); |
| 396 | p = c->path + strlen(c->path) - 1; |
| 397 | while (p > c->path && *p == '/') |
| 398 | *p-- = '\0'; |
| 399 | } |
| Jeff King | c716fe4 | 2020-03-12 05:31:11 | [diff] [blame] | 400 | |
| 401 | if (check_url_component(url, quiet, "username", c->username) < 0 || |
| 402 | check_url_component(url, quiet, "password", c->password) < 0 || |
| 403 | check_url_component(url, quiet, "protocol", c->protocol) < 0 || |
| 404 | check_url_component(url, quiet, "host", c->host) < 0 || |
| 405 | check_url_component(url, quiet, "path", c->path) < 0) |
| 406 | return -1; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | void credential_from_url(struct credential *c, const char *url) |
| 412 | { |
| Jeff King | fe29a9b | 2020-04-19 03:53:09 | [diff] [blame] | 413 | if (credential_from_url_gently(c, url, 0) < 0) |
| 414 | die(_("credential url cannot be parsed: %s"), url); |
| Jeff King | d3e847c | 2011-12-10 10:31:17 | [diff] [blame] | 415 | } |