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