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