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