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