Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 3 | #include "credential.h" |
| 4 | #include "string-list.h" |
| 5 | #include "run-command.h" |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 6 | #include "url.h" |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 7 | #include "prompt.h" |
Erik E Brady | a0d51e8 | 2018-03-29 11:00:56 -0700 | [diff] [blame] | 8 | #include "sigchain.h" |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 9 | #include "urlmatch.h" |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 10 | |
| 11 | void credential_init(struct credential *c) |
| 12 | { |
| 13 | memset(c, 0, sizeof(*c)); |
| 14 | c->helpers.strdup_strings = 1; |
| 15 | } |
| 16 | |
| 17 | void credential_clear(struct credential *c) |
| 18 | { |
| 19 | free(c->protocol); |
| 20 | free(c->host); |
| 21 | free(c->path); |
| 22 | free(c->username); |
| 23 | free(c->password); |
| 24 | string_list_clear(&c->helpers, 0); |
| 25 | |
| 26 | credential_init(c); |
| 27 | } |
| 28 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 29 | int credential_match(const struct credential *want, |
| 30 | const struct credential *have) |
| 31 | { |
| 32 | #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x))) |
| 33 | return CHECK(protocol) && |
| 34 | CHECK(host) && |
| 35 | CHECK(path) && |
| 36 | CHECK(username); |
| 37 | #undef CHECK |
| 38 | } |
| 39 | |
Johannes Schindelin | 9a121b0 | 2020-04-24 11:49:52 +0000 | [diff] [blame] | 40 | |
| 41 | static int credential_from_potentially_partial_url(struct credential *c, |
| 42 | const char *url); |
| 43 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 44 | static int credential_config_callback(const char *var, const char *value, |
| 45 | void *data) |
| 46 | { |
| 47 | struct credential *c = data; |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 48 | const char *key; |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 49 | |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 50 | if (!skip_prefix(var, "credential.", &key)) |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 51 | return 0; |
| 52 | |
| 53 | if (!value) |
| 54 | return config_error_nonbool(var); |
| 55 | |
Jeff King | 2432137 | 2016-02-26 05:51:35 -0500 | [diff] [blame] | 56 | if (!strcmp(key, "helper")) { |
| 57 | if (*value) |
| 58 | string_list_append(&c->helpers, value); |
| 59 | else |
| 60 | string_list_clear(&c->helpers, 0); |
| 61 | } else if (!strcmp(key, "username")) { |
brian m. carlson | 82eb249 | 2020-02-20 02:24:12 +0000 | [diff] [blame] | 62 | if (!c->username_from_proto) { |
| 63 | free(c->username); |
Jeff King | d574242 | 2011-12-10 05:31:30 -0500 | [diff] [blame] | 64 | c->username = xstrdup(value); |
brian m. carlson | 82eb249 | 2020-02-20 02:24:12 +0000 | [diff] [blame] | 65 | } |
Jeff King | d574242 | 2011-12-10 05:31:30 -0500 | [diff] [blame] | 66 | } |
Jeff King | a78fbb4 | 2011-12-10 05:31:34 -0500 | [diff] [blame] | 67 | else if (!strcmp(key, "usehttppath")) |
| 68 | c->use_http_path = git_config_bool(var, value); |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Jeff King | a78fbb4 | 2011-12-10 05:31:34 -0500 | [diff] [blame] | 73 | static int proto_is_http(const char *s) |
| 74 | { |
| 75 | if (!s) |
| 76 | return 0; |
| 77 | return !strcmp(s, "https") || !strcmp(s, "http"); |
| 78 | } |
| 79 | |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 80 | static void credential_describe(struct credential *c, struct strbuf *out); |
| 81 | static void credential_format(struct credential *c, struct strbuf *out); |
| 82 | |
| 83 | static int select_all(const struct urlmatch_item *a, |
| 84 | const struct urlmatch_item *b) |
| 85 | { |
| 86 | return 0; |
| 87 | } |
| 88 | |
Johannes Schindelin | 1229499 | 2020-04-24 22:35:49 +0000 | [diff] [blame] | 89 | static int match_partial_url(const char *url, void *cb) |
| 90 | { |
| 91 | struct credential *c = cb; |
| 92 | struct credential want = CREDENTIAL_INIT; |
| 93 | int matches = 0; |
| 94 | |
| 95 | if (credential_from_potentially_partial_url(&want, url) < 0) |
| 96 | warning(_("skipping credential lookup for key: credential.%s"), |
| 97 | url); |
| 98 | else |
| 99 | matches = credential_match(&want, c); |
| 100 | credential_clear(&want); |
| 101 | |
| 102 | return matches; |
| 103 | } |
| 104 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 105 | static void credential_apply_config(struct credential *c) |
| 106 | { |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 107 | char *normalized_url; |
| 108 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; |
| 109 | struct strbuf url = STRBUF_INIT; |
| 110 | |
Jeff King | 8ba8ed5 | 2020-04-18 20:50:48 -0700 | [diff] [blame] | 111 | if (!c->host) |
| 112 | die(_("refusing to work with credential missing host field")); |
| 113 | if (!c->protocol) |
| 114 | die(_("refusing to work with credential missing protocol field")); |
| 115 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 116 | if (c->configured) |
| 117 | return; |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 118 | |
| 119 | config.section = "credential"; |
| 120 | config.key = NULL; |
| 121 | config.collect_fn = credential_config_callback; |
| 122 | config.cascade_fn = NULL; |
| 123 | config.select_fn = select_all; |
Johannes Schindelin | 1229499 | 2020-04-24 22:35:49 +0000 | [diff] [blame] | 124 | config.fallback_match_fn = match_partial_url; |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 125 | config.cb = c; |
| 126 | |
| 127 | credential_format(c, &url); |
| 128 | normalized_url = url_normalize(url.buf, &config.url); |
| 129 | |
| 130 | git_config(urlmatch_config_entry, &config); |
| 131 | free(normalized_url); |
| 132 | strbuf_release(&url); |
| 133 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 134 | c->configured = 1; |
Jeff King | a78fbb4 | 2011-12-10 05:31:34 -0500 | [diff] [blame] | 135 | |
| 136 | if (!c->use_http_path && proto_is_http(c->protocol)) { |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 137 | FREE_AND_NULL(c->path); |
Jeff King | a78fbb4 | 2011-12-10 05:31:34 -0500 | [diff] [blame] | 138 | } |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 141 | static void credential_describe(struct credential *c, struct strbuf *out) |
| 142 | { |
| 143 | if (!c->protocol) |
| 144 | return; |
| 145 | strbuf_addf(out, "%s://", c->protocol); |
| 146 | if (c->username && *c->username) |
| 147 | strbuf_addf(out, "%s@", c->username); |
| 148 | if (c->host) |
| 149 | strbuf_addstr(out, c->host); |
| 150 | if (c->path) |
| 151 | strbuf_addf(out, "/%s", c->path); |
| 152 | } |
| 153 | |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 154 | static void credential_format(struct credential *c, struct strbuf *out) |
| 155 | { |
| 156 | if (!c->protocol) |
| 157 | return; |
| 158 | strbuf_addf(out, "%s://", c->protocol); |
| 159 | if (c->username && *c->username) { |
brian m. carlson | b44d011 | 2020-04-27 01:18:08 +0000 | [diff] [blame] | 160 | strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH); |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 161 | strbuf_addch(out, '@'); |
| 162 | } |
| 163 | if (c->host) |
| 164 | strbuf_addstr(out, c->host); |
| 165 | if (c->path) { |
| 166 | strbuf_addch(out, '/'); |
brian m. carlson | b44d011 | 2020-04-27 01:18:08 +0000 | [diff] [blame] | 167 | strbuf_add_percentencode(out, c->path, 0); |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Jeff King | ce77aa4 | 2011-12-10 05:41:23 -0500 | [diff] [blame] | 171 | static char *credential_ask_one(const char *what, struct credential *c, |
| 172 | int flags) |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 173 | { |
| 174 | struct strbuf desc = STRBUF_INIT; |
| 175 | struct strbuf prompt = STRBUF_INIT; |
| 176 | char *r; |
| 177 | |
| 178 | credential_describe(c, &desc); |
| 179 | if (desc.len) |
| 180 | strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf); |
| 181 | else |
| 182 | strbuf_addf(&prompt, "%s: ", what); |
| 183 | |
Jeff King | ce77aa4 | 2011-12-10 05:41:23 -0500 | [diff] [blame] | 184 | r = git_prompt(prompt.buf, flags); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 185 | |
| 186 | strbuf_release(&desc); |
| 187 | strbuf_release(&prompt); |
| 188 | return xstrdup(r); |
| 189 | } |
| 190 | |
| 191 | static void credential_getpass(struct credential *c) |
| 192 | { |
| 193 | if (!c->username) |
Jeff King | ce77aa4 | 2011-12-10 05:41:23 -0500 | [diff] [blame] | 194 | c->username = credential_ask_one("Username", c, |
| 195 | PROMPT_ASKPASS|PROMPT_ECHO); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 196 | if (!c->password) |
Jeff King | ce77aa4 | 2011-12-10 05:41:23 -0500 | [diff] [blame] | 197 | c->password = credential_ask_one("Password", c, |
| 198 | PROMPT_ASKPASS); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | int credential_read(struct credential *c, FILE *fp) |
| 202 | { |
| 203 | struct strbuf line = STRBUF_INIT; |
| 204 | |
Nikita Leonov | 356c473 | 2020-10-03 13:29:12 +0000 | [diff] [blame] | 205 | while (strbuf_getline(&line, fp) != EOF) { |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 206 | char *key = line.buf; |
| 207 | char *value = strchr(key, '='); |
| 208 | |
| 209 | if (!line.len) |
| 210 | break; |
| 211 | |
| 212 | if (!value) { |
| 213 | warning("invalid credential line: %s", key); |
| 214 | strbuf_release(&line); |
| 215 | return -1; |
| 216 | } |
| 217 | *value++ = '\0'; |
| 218 | |
| 219 | if (!strcmp(key, "username")) { |
| 220 | free(c->username); |
| 221 | c->username = xstrdup(value); |
brian m. carlson | 82eb249 | 2020-02-20 02:24:12 +0000 | [diff] [blame] | 222 | c->username_from_proto = 1; |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 223 | } else if (!strcmp(key, "password")) { |
| 224 | free(c->password); |
| 225 | c->password = xstrdup(value); |
| 226 | } else if (!strcmp(key, "protocol")) { |
| 227 | free(c->protocol); |
| 228 | c->protocol = xstrdup(value); |
| 229 | } else if (!strcmp(key, "host")) { |
| 230 | free(c->host); |
| 231 | c->host = xstrdup(value); |
| 232 | } else if (!strcmp(key, "path")) { |
| 233 | free(c->path); |
| 234 | c->path = xstrdup(value); |
Jeff King | 9c183a7 | 2012-07-18 08:06:26 -0400 | [diff] [blame] | 235 | } else if (!strcmp(key, "url")) { |
| 236 | credential_from_url(c, value); |
Jeff King | 59b3865 | 2014-12-03 22:46:48 -0500 | [diff] [blame] | 237 | } else if (!strcmp(key, "quit")) { |
| 238 | c->quit = !!git_config_bool("quit", value); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 239 | } |
| 240 | /* |
| 241 | * Ignore other lines; we don't know what they mean, but |
| 242 | * this future-proofs us when later versions of git do |
| 243 | * learn new lines, and the helpers are updated to match. |
| 244 | */ |
| 245 | } |
| 246 | |
| 247 | strbuf_release(&line); |
| 248 | return 0; |
| 249 | } |
| 250 | |
Jeff King | 8ba8ed5 | 2020-04-18 20:50:48 -0700 | [diff] [blame] | 251 | static void credential_write_item(FILE *fp, const char *key, const char *value, |
| 252 | int required) |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 253 | { |
Jeff King | 8ba8ed5 | 2020-04-18 20:50:48 -0700 | [diff] [blame] | 254 | if (!value && required) |
| 255 | BUG("credential value for %s is missing", key); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 256 | if (!value) |
| 257 | return; |
Jeff King | 9a6bbee | 2020-03-11 17:53:41 -0400 | [diff] [blame] | 258 | if (strchr(value, '\n')) |
| 259 | die("credential value for %s contains newline", key); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 260 | fprintf(fp, "%s=%s\n", key, value); |
| 261 | } |
| 262 | |
Matthieu Moy | 2d6dc18 | 2012-06-24 13:40:00 +0200 | [diff] [blame] | 263 | void credential_write(const struct credential *c, FILE *fp) |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 264 | { |
Jeff King | 8ba8ed5 | 2020-04-18 20:50:48 -0700 | [diff] [blame] | 265 | credential_write_item(fp, "protocol", c->protocol, 1); |
| 266 | credential_write_item(fp, "host", c->host, 1); |
| 267 | credential_write_item(fp, "path", c->path, 0); |
| 268 | credential_write_item(fp, "username", c->username, 0); |
| 269 | credential_write_item(fp, "password", c->password, 0); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int run_credential_helper(struct credential *c, |
| 273 | const char *cmd, |
| 274 | int want_output) |
| 275 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 276 | struct child_process helper = CHILD_PROCESS_INIT; |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 277 | FILE *fp; |
| 278 | |
Junio C Hamano | afbdba3 | 2020-08-26 15:25:03 -0700 | [diff] [blame] | 279 | strvec_push(&helper.args, cmd); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 280 | helper.use_shell = 1; |
| 281 | helper.in = -1; |
| 282 | if (want_output) |
| 283 | helper.out = -1; |
| 284 | else |
| 285 | helper.no_stdout = 1; |
| 286 | |
| 287 | if (start_command(&helper) < 0) |
| 288 | return -1; |
| 289 | |
| 290 | fp = xfdopen(helper.in, "w"); |
Erik E Brady | a0d51e8 | 2018-03-29 11:00:56 -0700 | [diff] [blame] | 291 | sigchain_push(SIGPIPE, SIG_IGN); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 292 | credential_write(c, fp); |
| 293 | fclose(fp); |
Erik E Brady | a0d51e8 | 2018-03-29 11:00:56 -0700 | [diff] [blame] | 294 | sigchain_pop(SIGPIPE); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 295 | |
| 296 | if (want_output) { |
| 297 | int r; |
| 298 | fp = xfdopen(helper.out, "r"); |
| 299 | r = credential_read(c, fp); |
| 300 | fclose(fp); |
| 301 | if (r < 0) { |
| 302 | finish_command(&helper); |
| 303 | return -1; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (finish_command(&helper)) |
| 308 | return -1; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int credential_do(struct credential *c, const char *helper, |
| 313 | const char *operation) |
| 314 | { |
| 315 | struct strbuf cmd = STRBUF_INIT; |
| 316 | int r; |
| 317 | |
| 318 | if (helper[0] == '!') |
| 319 | strbuf_addstr(&cmd, helper + 1); |
| 320 | else if (is_absolute_path(helper)) |
| 321 | strbuf_addstr(&cmd, helper); |
| 322 | else |
| 323 | strbuf_addf(&cmd, "git credential-%s", helper); |
| 324 | |
| 325 | strbuf_addf(&cmd, " %s", operation); |
| 326 | r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get")); |
| 327 | |
| 328 | strbuf_release(&cmd); |
| 329 | return r; |
| 330 | } |
| 331 | |
| 332 | void credential_fill(struct credential *c) |
| 333 | { |
| 334 | int i; |
| 335 | |
| 336 | if (c->username && c->password) |
| 337 | return; |
| 338 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 339 | credential_apply_config(c); |
| 340 | |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 341 | for (i = 0; i < c->helpers.nr; i++) { |
| 342 | credential_do(c, c->helpers.items[i].string, "get"); |
| 343 | if (c->username && c->password) |
| 344 | return; |
Jeff King | 59b3865 | 2014-12-03 22:46:48 -0500 | [diff] [blame] | 345 | if (c->quit) |
| 346 | die("credential helper '%s' told us to quit", |
| 347 | c->helpers.items[i].string); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | credential_getpass(c); |
| 351 | if (!c->username && !c->password) |
| 352 | die("unable to get password from user"); |
| 353 | } |
| 354 | |
| 355 | void credential_approve(struct credential *c) |
| 356 | { |
| 357 | int i; |
| 358 | |
| 359 | if (c->approved) |
| 360 | return; |
| 361 | if (!c->username || !c->password) |
| 362 | return; |
| 363 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 364 | credential_apply_config(c); |
| 365 | |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 366 | for (i = 0; i < c->helpers.nr; i++) |
| 367 | credential_do(c, c->helpers.items[i].string, "store"); |
| 368 | c->approved = 1; |
| 369 | } |
| 370 | |
| 371 | void credential_reject(struct credential *c) |
| 372 | { |
| 373 | int i; |
| 374 | |
Jeff King | 1182507 | 2011-12-10 05:31:24 -0500 | [diff] [blame] | 375 | credential_apply_config(c); |
| 376 | |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 377 | for (i = 0; i < c->helpers.nr; i++) |
| 378 | credential_do(c, c->helpers.items[i].string, "erase"); |
| 379 | |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 380 | FREE_AND_NULL(c->username); |
| 381 | FREE_AND_NULL(c->password); |
Jeff King | abca927 | 2011-12-10 05:31:11 -0500 | [diff] [blame] | 382 | c->approved = 0; |
| 383 | } |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 384 | |
Jeff King | c716fe4 | 2020-03-12 01:31:11 -0400 | [diff] [blame] | 385 | static int check_url_component(const char *url, int quiet, |
| 386 | const char *name, const char *value) |
| 387 | { |
| 388 | if (!value) |
| 389 | return 0; |
| 390 | if (!strchr(value, '\n')) |
| 391 | return 0; |
| 392 | |
| 393 | if (!quiet) |
| 394 | warning(_("url contains a newline in its %s component: %s"), |
| 395 | name, url); |
| 396 | return -1; |
| 397 | } |
| 398 | |
Johannes Schindelin | 6828e59 | 2020-04-24 11:49:51 +0000 | [diff] [blame] | 399 | /* |
| 400 | * Potentially-partial URLs can, but do not have to, contain |
| 401 | * |
| 402 | * - a protocol (or scheme) of the form "<protocol>://" |
| 403 | * |
| 404 | * - a host name (the part after the protocol and before the first slash after |
| 405 | * that, if any) |
| 406 | * |
| 407 | * - a user name and potentially a password (as "<user>[:<password>]@" part of |
| 408 | * the host name) |
| 409 | * |
| 410 | * - a path (the part after the host name, if any, starting with the slash) |
| 411 | * |
| 412 | * Missing parts will be left unset in `struct credential`. Thus, `https://` |
| 413 | * will have only the `protocol` set, `example.com` only the host name, and |
| 414 | * `/git` only the path. |
| 415 | * |
| 416 | * Note that an empty host name in an otherwise fully-qualified URL (e.g. |
| 417 | * `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to |
| 418 | * be potentially partial, and only then (otherwise, the empty string is used). |
| 419 | * |
| 420 | * The credential_from_url() function does not allow partial URLs. |
| 421 | */ |
| 422 | static int credential_from_url_1(struct credential *c, const char *url, |
| 423 | int allow_partial_url, int quiet) |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 424 | { |
| 425 | const char *at, *colon, *cp, *slash, *host, *proto_end; |
| 426 | |
| 427 | credential_clear(c); |
| 428 | |
| 429 | /* |
| 430 | * Match one of: |
| 431 | * (1) proto://<host>/... |
| 432 | * (2) proto://<user>@<host>/... |
| 433 | * (3) proto://<user>:<pass>@<host>/... |
| 434 | */ |
| 435 | proto_end = strstr(url, "://"); |
Johannes Schindelin | 6828e59 | 2020-04-24 11:49:51 +0000 | [diff] [blame] | 436 | if (!allow_partial_url && (!proto_end || proto_end == url)) { |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 437 | if (!quiet) |
| 438 | warning(_("url has no scheme: %s"), url); |
| 439 | return -1; |
| 440 | } |
Johannes Schindelin | 6828e59 | 2020-04-24 11:49:51 +0000 | [diff] [blame] | 441 | cp = proto_end ? proto_end + 3 : url; |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 442 | at = strchr(cp, '@'); |
| 443 | colon = strchr(cp, ':'); |
Jeff King | 4c5971e | 2020-04-14 17:43:04 -0400 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * A query or fragment marker before the slash ends the host portion. |
| 447 | * We'll just continue to call this "slash" for simplicity. Notably our |
| 448 | * "trim leading slashes" part won't skip over this part of the path, |
| 449 | * but that's what we'd want. |
| 450 | */ |
| 451 | slash = cp + strcspn(cp, "/?#"); |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 452 | |
| 453 | if (!at || slash <= at) { |
| 454 | /* Case (1) */ |
| 455 | host = cp; |
| 456 | } |
| 457 | else if (!colon || at <= colon) { |
| 458 | /* Case (2) */ |
| 459 | c->username = url_decode_mem(cp, at - cp); |
brian m. carlson | 82eb249 | 2020-02-20 02:24:12 +0000 | [diff] [blame] | 460 | if (c->username && *c->username) |
| 461 | c->username_from_proto = 1; |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 462 | host = at + 1; |
| 463 | } else { |
| 464 | /* Case (3) */ |
| 465 | c->username = url_decode_mem(cp, colon - cp); |
brian m. carlson | 82eb249 | 2020-02-20 02:24:12 +0000 | [diff] [blame] | 466 | if (c->username && *c->username) |
| 467 | c->username_from_proto = 1; |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 468 | c->password = url_decode_mem(colon + 1, at - (colon + 1)); |
| 469 | host = at + 1; |
| 470 | } |
| 471 | |
Johannes Schindelin | 6828e59 | 2020-04-24 11:49:51 +0000 | [diff] [blame] | 472 | if (proto_end && proto_end - url > 0) |
| 473 | c->protocol = xmemdupz(url, proto_end - url); |
| 474 | if (!allow_partial_url || slash - host > 0) |
| 475 | c->host = url_decode_mem(host, slash - host); |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 476 | /* Trim leading and trailing slashes from path */ |
| 477 | while (*slash == '/') |
| 478 | slash++; |
| 479 | if (*slash) { |
| 480 | char *p; |
| 481 | c->path = url_decode(slash); |
| 482 | p = c->path + strlen(c->path) - 1; |
| 483 | while (p > c->path && *p == '/') |
| 484 | *p-- = '\0'; |
| 485 | } |
Jeff King | c716fe4 | 2020-03-12 01:31:11 -0400 | [diff] [blame] | 486 | |
| 487 | if (check_url_component(url, quiet, "username", c->username) < 0 || |
| 488 | check_url_component(url, quiet, "password", c->password) < 0 || |
| 489 | check_url_component(url, quiet, "protocol", c->protocol) < 0 || |
| 490 | check_url_component(url, quiet, "host", c->host) < 0 || |
| 491 | check_url_component(url, quiet, "path", c->path) < 0) |
| 492 | return -1; |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Johannes Schindelin | 9a121b0 | 2020-04-24 11:49:52 +0000 | [diff] [blame] | 497 | static int credential_from_potentially_partial_url(struct credential *c, |
| 498 | const char *url) |
| 499 | { |
| 500 | return credential_from_url_1(c, url, 1, 0); |
| 501 | } |
| 502 | |
Johannes Schindelin | 6828e59 | 2020-04-24 11:49:51 +0000 | [diff] [blame] | 503 | int credential_from_url_gently(struct credential *c, const char *url, int quiet) |
| 504 | { |
| 505 | return credential_from_url_1(c, url, 0, quiet); |
| 506 | } |
| 507 | |
Jeff King | c716fe4 | 2020-03-12 01:31:11 -0400 | [diff] [blame] | 508 | void credential_from_url(struct credential *c, const char *url) |
| 509 | { |
Jeff King | fe29a9b | 2020-04-18 20:53:09 -0700 | [diff] [blame] | 510 | if (credential_from_url_gently(c, url, 0) < 0) |
| 511 | die(_("credential url cannot be parsed: %s"), url); |
Jeff King | d3e847c | 2011-12-10 05:31:17 -0500 | [diff] [blame] | 512 | } |