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