2 #include "credential.h"
3 #include "string-list.h"
4 #include "run-command.h"
7 void credential_init(struct credential *c)
9 memset(c, 0, sizeof(*c));
10 c->helpers.strdup_strings = 1;
13 void credential_clear(struct credential *c)
20 string_list_clear(&c->helpers, 0);
25 int credential_match(const struct credential *want,
26 const struct credential *have)
28 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
29 return CHECK(protocol) &&
36 static int credential_config_callback(const char *var, const char *value,
39 struct credential *c = data;
40 const char *key, *dot;
42 key = skip_prefix(var, "credential.");
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"))
67 string_list_append(&c->helpers, value);
68 else if (!strcmp(key, "username")) {
70 c->username = xstrdup(value);
76 static void credential_apply_config(struct credential *c)
80 git_config(credential_config_callback, c);
84 static void credential_describe(struct credential *c, struct strbuf *out)
88 strbuf_addf(out, "%s://", c->protocol);
89 if (c->username && *c->username)
90 strbuf_addf(out, "%s@", c->username);
92 strbuf_addstr(out, c->host);
94 strbuf_addf(out, "/%s", c->path);
97 static char *credential_ask_one(const char *what, struct credential *c)
99 struct strbuf desc = STRBUF_INIT;
100 struct strbuf prompt = STRBUF_INIT;
103 credential_describe(c, &desc);
105 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
107 strbuf_addf(&prompt, "%s: ", what);
109 /* FIXME: for usernames, we should do something less magical that
110 * actually echoes the characters. However, we need to read from
111 * /dev/tty and not stdio, which is not portable (but getpass will do
112 * it for us). http.c uses the same workaround. */
113 r = git_getpass(prompt.buf);
115 strbuf_release(&desc);
116 strbuf_release(&prompt);
120 static void credential_getpass(struct credential *c)
123 c->username = credential_ask_one("Username", c);
125 c->password = credential_ask_one("Password", c);
128 int credential_read(struct credential *c, FILE *fp)
130 struct strbuf line = STRBUF_INIT;
132 while (strbuf_getline(&line, fp, '\n') != EOF) {
133 char *key = line.buf;
134 char *value = strchr(key, '=');
140 warning("invalid credential line: %s", key);
141 strbuf_release(&line);
146 if (!strcmp(key, "username")) {
148 c->username = xstrdup(value);
149 } else if (!strcmp(key, "password")) {
151 c->password = xstrdup(value);
152 } else if (!strcmp(key, "protocol")) {
154 c->protocol = xstrdup(value);
155 } else if (!strcmp(key, "host")) {
157 c->host = xstrdup(value);
158 } else if (!strcmp(key, "path")) {
160 c->path = xstrdup(value);
163 * Ignore other lines; we don't know what they mean, but
164 * this future-proofs us when later versions of git do
165 * learn new lines, and the helpers are updated to match.
169 strbuf_release(&line);
173 static void credential_write_item(FILE *fp, const char *key, const char *value)
177 fprintf(fp, "%s=%s\n", key, value);
180 static void credential_write(const struct credential *c, FILE *fp)
182 credential_write_item(fp, "protocol", c->protocol);
183 credential_write_item(fp, "host", c->host);
184 credential_write_item(fp, "path", c->path);
185 credential_write_item(fp, "username", c->username);
186 credential_write_item(fp, "password", c->password);
189 static int run_credential_helper(struct credential *c,
193 struct child_process helper;
194 const char *argv[] = { NULL, NULL };
197 memset(&helper, 0, sizeof(helper));
200 helper.use_shell = 1;
205 helper.no_stdout = 1;
207 if (start_command(&helper) < 0)
210 fp = xfdopen(helper.in, "w");
211 credential_write(c, fp);
216 fp = xfdopen(helper.out, "r");
217 r = credential_read(c, fp);
220 finish_command(&helper);
225 if (finish_command(&helper))
230 static int credential_do(struct credential *c, const char *helper,
231 const char *operation)
233 struct strbuf cmd = STRBUF_INIT;
236 if (helper[0] == '!')
237 strbuf_addstr(&cmd, helper + 1);
238 else if (is_absolute_path(helper))
239 strbuf_addstr(&cmd, helper);
241 strbuf_addf(&cmd, "git credential-%s", helper);
243 strbuf_addf(&cmd, " %s", operation);
244 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
246 strbuf_release(&cmd);
250 void credential_fill(struct credential *c)
254 if (c->username && c->password)
257 credential_apply_config(c);
259 for (i = 0; i < c->helpers.nr; i++) {
260 credential_do(c, c->helpers.items[i].string, "get");
261 if (c->username && c->password)
265 credential_getpass(c);
266 if (!c->username && !c->password)
267 die("unable to get password from user");
270 void credential_approve(struct credential *c)
276 if (!c->username || !c->password)
279 credential_apply_config(c);
281 for (i = 0; i < c->helpers.nr; i++)
282 credential_do(c, c->helpers.items[i].string, "store");
286 void credential_reject(struct credential *c)
290 credential_apply_config(c);
292 for (i = 0; i < c->helpers.nr; i++)
293 credential_do(c, c->helpers.items[i].string, "erase");
302 void credential_from_url(struct credential *c, const char *url)
304 const char *at, *colon, *cp, *slash, *host, *proto_end;
310 * (1) proto://<host>/...
311 * (2) proto://<user>@<host>/...
312 * (3) proto://<user>:<pass>@<host>/...
314 proto_end = strstr(url, "://");
318 at = strchr(cp, '@');
319 colon = strchr(cp, ':');
320 slash = strchrnul(cp, '/');
322 if (!at || slash <= at) {
326 else if (!colon || at <= colon) {
328 c->username = url_decode_mem(cp, at - cp);
332 c->username = url_decode_mem(cp, colon - cp);
333 c->password = url_decode_mem(colon + 1, at - (colon + 1));
337 if (proto_end - url > 0)
338 c->protocol = xmemdupz(url, proto_end - url);
339 if (slash - host > 0)
340 c->host = url_decode_mem(host, slash - host);
341 /* Trim leading and trailing slashes from path */
342 while (*slash == '/')
346 c->path = url_decode(slash);
347 p = c->path + strlen(c->path) - 1;
348 while (p > c->path && *p == '/')