2 #include "credential.h"
3 #include "string-list.h"
4 #include "run-command.h"
8 void credential_init(struct credential *c)
10 memset(c, 0, sizeof(*c));
11 c->helpers.strdup_strings = 1;
14 void credential_clear(struct credential *c)
21 string_list_clear(&c->helpers, 0);
26 int credential_match(const struct credential *want,
27 const struct credential *have)
29 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
30 return CHECK(protocol) &&
37 static int credential_config_callback(const char *var, const char *value,
40 struct credential *c = data;
41 const char *key, *dot;
43 if (!skip_prefix(var, "credential.", &key))
47 return config_error_nonbool(var);
49 dot = strrchr(key, '.');
51 struct credential want = CREDENTIAL_INIT;
52 char *url = xmemdupz(key, dot - key);
55 credential_from_url(&want, url);
56 matched = credential_match(&want, c);
58 credential_clear(&want);
66 if (!strcmp(key, "helper")) {
68 string_list_append(&c->helpers, value);
70 string_list_clear(&c->helpers, 0);
71 } else if (!strcmp(key, "username")) {
73 c->username = xstrdup(value);
75 else if (!strcmp(key, "usehttppath"))
76 c->use_http_path = git_config_bool(var, value);
81 static int proto_is_http(const char *s)
85 return !strcmp(s, "https") || !strcmp(s, "http");
88 static void credential_apply_config(struct credential *c)
92 git_config(credential_config_callback, c);
95 if (!c->use_http_path && proto_is_http(c->protocol)) {
96 FREE_AND_NULL(c->path);
100 static void credential_describe(struct credential *c, struct strbuf *out)
104 strbuf_addf(out, "%s://", c->protocol);
105 if (c->username && *c->username)
106 strbuf_addf(out, "%s@", c->username);
108 strbuf_addstr(out, c->host);
110 strbuf_addf(out, "/%s", c->path);
113 static char *credential_ask_one(const char *what, struct credential *c,
116 struct strbuf desc = STRBUF_INIT;
117 struct strbuf prompt = STRBUF_INIT;
120 credential_describe(c, &desc);
122 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
124 strbuf_addf(&prompt, "%s: ", what);
126 r = git_prompt(prompt.buf, flags);
128 strbuf_release(&desc);
129 strbuf_release(&prompt);
133 static void credential_getpass(struct credential *c)
136 c->username = credential_ask_one("Username", c,
137 PROMPT_ASKPASS|PROMPT_ECHO);
139 c->password = credential_ask_one("Password", c,
143 int credential_read(struct credential *c, FILE *fp)
145 struct strbuf line = STRBUF_INIT;
147 while (strbuf_getline_lf(&line, fp) != EOF) {
148 char *key = line.buf;
149 char *value = strchr(key, '=');
155 warning("invalid credential line: %s", key);
156 strbuf_release(&line);
161 if (!strcmp(key, "username")) {
163 c->username = xstrdup(value);
164 } else if (!strcmp(key, "password")) {
166 c->password = xstrdup(value);
167 } else if (!strcmp(key, "protocol")) {
169 c->protocol = xstrdup(value);
170 } else if (!strcmp(key, "host")) {
172 c->host = xstrdup(value);
173 } else if (!strcmp(key, "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);
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.
188 strbuf_release(&line);
192 static void credential_write_item(FILE *fp, const char *key, const char *value)
196 fprintf(fp, "%s=%s\n", key, value);
199 void credential_write(const struct credential *c, FILE *fp)
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);
208 static int run_credential_helper(struct credential *c,
212 struct child_process helper = CHILD_PROCESS_INIT;
213 const char *argv[] = { NULL, NULL };
218 helper.use_shell = 1;
223 helper.no_stdout = 1;
225 if (start_command(&helper) < 0)
228 fp = xfdopen(helper.in, "w");
229 credential_write(c, fp);
234 fp = xfdopen(helper.out, "r");
235 r = credential_read(c, fp);
238 finish_command(&helper);
243 if (finish_command(&helper))
248 static int credential_do(struct credential *c, const char *helper,
249 const char *operation)
251 struct strbuf cmd = STRBUF_INIT;
254 if (helper[0] == '!')
255 strbuf_addstr(&cmd, helper + 1);
256 else if (is_absolute_path(helper))
257 strbuf_addstr(&cmd, helper);
259 strbuf_addf(&cmd, "git credential-%s", helper);
261 strbuf_addf(&cmd, " %s", operation);
262 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
264 strbuf_release(&cmd);
268 void credential_fill(struct credential *c)
272 if (c->username && c->password)
275 credential_apply_config(c);
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)
282 die("credential helper '%s' told us to quit",
283 c->helpers.items[i].string);
286 credential_getpass(c);
287 if (!c->username && !c->password)
288 die("unable to get password from user");
291 void credential_approve(struct credential *c)
297 if (!c->username || !c->password)
300 credential_apply_config(c);
302 for (i = 0; i < c->helpers.nr; i++)
303 credential_do(c, c->helpers.items[i].string, "store");
307 void credential_reject(struct credential *c)
311 credential_apply_config(c);
313 for (i = 0; i < c->helpers.nr; i++)
314 credential_do(c, c->helpers.items[i].string, "erase");
323 void credential_from_url(struct credential *c, const char *url)
325 const char *at, *colon, *cp, *slash, *host, *proto_end;
331 * (1) proto://<host>/...
332 * (2) proto://<user>@<host>/...
333 * (3) proto://<user>:<pass>@<host>/...
335 proto_end = strstr(url, "://");
339 at = strchr(cp, '@');
340 colon = strchr(cp, ':');
341 slash = strchrnul(cp, '/');
343 if (!at || slash <= at) {
347 else if (!colon || at <= colon) {
349 c->username = url_decode_mem(cp, at - cp);
353 c->username = url_decode_mem(cp, colon - cp);
354 c->password = url_decode_mem(colon + 1, at - (colon + 1));
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 == '/')
367 c->path = url_decode(slash);
368 p = c->path + strlen(c->path) - 1;
369 while (p > c->path && *p == '/')