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