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