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 key = skip_prefix(var, "credential.");
48 return config_error_nonbool(var);
50 dot = strrchr(key, '.');
52 struct credential want = CREDENTIAL_INIT;
53 char *url = xmemdupz(key, dot - key);
56 credential_from_url(&want, url);
57 matched = credential_match(&want, c);
59 credential_clear(&want);
67 if (!strcmp(key, "helper"))
68 string_list_append(&c->helpers, value);
69 else if (!strcmp(key, "username")) {
71 c->username = xstrdup(value);
73 else if (!strcmp(key, "usehttppath"))
74 c->use_http_path = git_config_bool(var, value);
79 static int proto_is_http(const char *s)
83 return !strcmp(s, "https") || !strcmp(s, "http");
86 static void credential_apply_config(struct credential *c)
90 git_config(credential_config_callback, c);
93 if (!c->use_http_path && proto_is_http(c->protocol)) {
99 static void credential_describe(struct credential *c, struct strbuf *out)
103 strbuf_addf(out, "%s://", c->protocol);
104 if (c->username && *c->username)
105 strbuf_addf(out, "%s@", c->username);
107 strbuf_addstr(out, c->host);
109 strbuf_addf(out, "/%s", c->path);
112 static char *credential_ask_one(const char *what, struct credential *c,
115 struct strbuf desc = STRBUF_INIT;
116 struct strbuf prompt = STRBUF_INIT;
119 credential_describe(c, &desc);
121 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
123 strbuf_addf(&prompt, "%s: ", what);
125 r = git_prompt(prompt.buf, flags);
127 strbuf_release(&desc);
128 strbuf_release(&prompt);
132 static void credential_getpass(struct credential *c)
135 c->username = credential_ask_one("Username", c,
136 PROMPT_ASKPASS|PROMPT_ECHO);
138 c->password = credential_ask_one("Password", c,
142 int credential_read(struct credential *c, FILE *fp)
144 struct strbuf line = STRBUF_INIT;
146 while (strbuf_getline(&line, fp, '\n') != EOF) {
147 char *key = line.buf;
148 char *value = strchr(key, '=');
154 warning("invalid credential line: %s", key);
155 strbuf_release(&line);
160 if (!strcmp(key, "username")) {
162 c->username = xstrdup(value);
163 } else if (!strcmp(key, "password")) {
165 c->password = xstrdup(value);
166 } else if (!strcmp(key, "protocol")) {
168 c->protocol = xstrdup(value);
169 } else if (!strcmp(key, "host")) {
171 c->host = xstrdup(value);
172 } else if (!strcmp(key, "path")) {
174 c->path = xstrdup(value);
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.
183 strbuf_release(&line);
187 static void credential_write_item(FILE *fp, const char *key, const char *value)
191 fprintf(fp, "%s=%s\n", key, value);
194 void credential_write(const struct credential *c, FILE *fp)
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);
203 static int run_credential_helper(struct credential *c,
207 struct child_process helper;
208 const char *argv[] = { NULL, NULL };
211 memset(&helper, 0, sizeof(helper));
214 helper.use_shell = 1;
219 helper.no_stdout = 1;
221 if (start_command(&helper) < 0)
224 fp = xfdopen(helper.in, "w");
225 credential_write(c, fp);
230 fp = xfdopen(helper.out, "r");
231 r = credential_read(c, fp);
234 finish_command(&helper);
239 if (finish_command(&helper))
244 static int credential_do(struct credential *c, const char *helper,
245 const char *operation)
247 struct strbuf cmd = STRBUF_INIT;
250 if (helper[0] == '!')
251 strbuf_addstr(&cmd, helper + 1);
252 else if (is_absolute_path(helper))
253 strbuf_addstr(&cmd, helper);
255 strbuf_addf(&cmd, "git credential-%s", helper);
257 strbuf_addf(&cmd, " %s", operation);
258 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
260 strbuf_release(&cmd);
264 void credential_fill(struct credential *c)
268 if (c->username && c->password)
271 credential_apply_config(c);
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)
279 credential_getpass(c);
280 if (!c->username && !c->password)
281 die("unable to get password from user");
284 void credential_approve(struct credential *c)
290 if (!c->username || !c->password)
293 credential_apply_config(c);
295 for (i = 0; i < c->helpers.nr; i++)
296 credential_do(c, c->helpers.items[i].string, "store");
300 void credential_reject(struct credential *c)
304 credential_apply_config(c);
306 for (i = 0; i < c->helpers.nr; i++)
307 credential_do(c, c->helpers.items[i].string, "erase");
316 void credential_from_url(struct credential *c, const char *url)
318 const char *at, *colon, *cp, *slash, *host, *proto_end;
324 * (1) proto://<host>/...
325 * (2) proto://<user>@<host>/...
326 * (3) proto://<user>:<pass>@<host>/...
328 proto_end = strstr(url, "://");
332 at = strchr(cp, '@');
333 colon = strchr(cp, ':');
334 slash = strchrnul(cp, '/');
336 if (!at || slash <= at) {
340 else if (!colon || at <= colon) {
342 c->username = url_decode_mem(cp, at - cp);
346 c->username = url_decode_mem(cp, colon - cp);
347 c->password = url_decode_mem(colon + 1, at - (colon + 1));
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 == '/')
360 c->path = url_decode(slash);
361 p = c->path + strlen(c->path) - 1;
362 while (p > c->path && *p == '/')