Documentation/stash: remove mention of git reset --hard
[git.git] / credential.c
blobaa996669fc40021e0c8cb01fa8021da7064e2781
1 #include "cache.h"
2 #include "credential.h"
3 #include "string-list.h"
4 #include "run-command.h"
5 #include "url.h"
6 #include "prompt.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)
16 free(c->protocol);
17 free(c->host);
18 free(c->path);
19 free(c->username);
20 free(c->password);
21 string_list_clear(&c->helpers, 0);
23 credential_init(c);
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) &&
31 CHECK(host) &&
32 CHECK(path) &&
33 CHECK(username);
34 #undef CHECK
37 static int credential_config_callback(const char *var, const char *value,
38 void *data)
40 struct credential *c = data;
41 const char *key, *dot;
43 if (!skip_prefix(var, "credential.", &key))
44 return 0;
46 if (!value)
47 return config_error_nonbool(var);
49 dot = strrchr(key, '.');
50 if (dot) {
51 struct credential want = CREDENTIAL_INIT;
52 char *url = xmemdupz(key, dot - key);
53 int matched;
55 credential_from_url(&want, url);
56 matched = credential_match(&want, c);
58 credential_clear(&want);
59 free(url);
61 if (!matched)
62 return 0;
63 key = dot + 1;
66 if (!strcmp(key, "helper")) {
67 if (*value)
68 string_list_append(&c->helpers, value);
69 else
70 string_list_clear(&c->helpers, 0);
71 } else if (!strcmp(key, "username")) {
72 if (!c->username)
73 c->username = xstrdup(value);
75 else if (!strcmp(key, "usehttppath"))
76 c->use_http_path = git_config_bool(var, value);
78 return 0;
81 static int proto_is_http(const char *s)
83 if (!s)
84 return 0;
85 return !strcmp(s, "https") || !strcmp(s, "http");
88 static void credential_apply_config(struct credential *c)
90 if (c->configured)
91 return;
92 git_config(credential_config_callback, c);
93 c->configured = 1;
95 if (!c->use_http_path && proto_is_http(c->protocol)) {
96 free(c->path);
97 c->path = NULL;
101 static void credential_describe(struct credential *c, struct strbuf *out)
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);
114 static char *credential_ask_one(const char *what, struct credential *c,
115 int flags)
117 struct strbuf desc = STRBUF_INIT;
118 struct strbuf prompt = STRBUF_INIT;
119 char *r;
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);
127 r = git_prompt(prompt.buf, flags);
129 strbuf_release(&desc);
130 strbuf_release(&prompt);
131 return xstrdup(r);
134 static void credential_getpass(struct credential *c)
136 if (!c->username)
137 c->username = credential_ask_one("Username", c,
138 PROMPT_ASKPASS|PROMPT_ECHO);
139 if (!c->password)
140 c->password = credential_ask_one("Password", c,
141 PROMPT_ASKPASS);
144 int credential_read(struct credential *c, FILE *fp)
146 struct strbuf line = STRBUF_INIT;
148 while (strbuf_getline_lf(&line, fp) != EOF) {
149 char *key = line.buf;
150 char *value = strchr(key, '=');
152 if (!line.len)
153 break;
155 if (!value) {
156 warning("invalid credential line: %s", key);
157 strbuf_release(&line);
158 return -1;
160 *value++ = '\0';
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);
177 } else if (!strcmp(key, "url")) {
178 credential_from_url(c, value);
179 } else if (!strcmp(key, "quit")) {
180 c->quit = !!git_config_bool("quit", value);
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.
189 strbuf_release(&line);
190 return 0;
193 static void credential_write_item(FILE *fp, const char *key, const char *value)
195 if (!value)
196 return;
197 fprintf(fp, "%s=%s\n", key, value);
200 void credential_write(const struct credential *c, FILE *fp)
202 credential_write_item(fp, "protocol", c->protocol);
203 credential_write_item(fp, "host", c->host);
204 credential_write_item(fp, "path", c->path);
205 credential_write_item(fp, "username", c->username);
206 credential_write_item(fp, "password", c->password);
209 static int run_credential_helper(struct credential *c,
210 const char *cmd,
211 int want_output)
213 struct child_process helper = CHILD_PROCESS_INIT;
214 const char *argv[] = { NULL, NULL };
215 FILE *fp;
217 argv[0] = cmd;
218 helper.argv = argv;
219 helper.use_shell = 1;
220 helper.in = -1;
221 if (want_output)
222 helper.out = -1;
223 else
224 helper.no_stdout = 1;
226 if (start_command(&helper) < 0)
227 return -1;
229 fp = xfdopen(helper.in, "w");
230 credential_write(c, fp);
231 fclose(fp);
233 if (want_output) {
234 int r;
235 fp = xfdopen(helper.out, "r");
236 r = credential_read(c, fp);
237 fclose(fp);
238 if (r < 0) {
239 finish_command(&helper);
240 return -1;
244 if (finish_command(&helper))
245 return -1;
246 return 0;
249 static int credential_do(struct credential *c, const char *helper,
250 const char *operation)
252 struct strbuf cmd = STRBUF_INIT;
253 int r;
255 if (helper[0] == '!')
256 strbuf_addstr(&cmd, helper + 1);
257 else if (is_absolute_path(helper))
258 strbuf_addstr(&cmd, helper);
259 else
260 strbuf_addf(&cmd, "git credential-%s", helper);
262 strbuf_addf(&cmd, " %s", operation);
263 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
265 strbuf_release(&cmd);
266 return r;
269 void credential_fill(struct credential *c)
271 int i;
273 if (c->username && c->password)
274 return;
276 credential_apply_config(c);
278 for (i = 0; i < c->helpers.nr; i++) {
279 credential_do(c, c->helpers.items[i].string, "get");
280 if (c->username && c->password)
281 return;
282 if (c->quit)
283 die("credential helper '%s' told us to quit",
284 c->helpers.items[i].string);
287 credential_getpass(c);
288 if (!c->username && !c->password)
289 die("unable to get password from user");
292 void credential_approve(struct credential *c)
294 int i;
296 if (c->approved)
297 return;
298 if (!c->username || !c->password)
299 return;
301 credential_apply_config(c);
303 for (i = 0; i < c->helpers.nr; i++)
304 credential_do(c, c->helpers.items[i].string, "store");
305 c->approved = 1;
308 void credential_reject(struct credential *c)
310 int i;
312 credential_apply_config(c);
314 for (i = 0; i < c->helpers.nr; i++)
315 credential_do(c, c->helpers.items[i].string, "erase");
317 free(c->username);
318 c->username = NULL;
319 free(c->password);
320 c->password = NULL;
321 c->approved = 0;
324 void credential_from_url(struct credential *c, const char *url)
326 const char *at, *colon, *cp, *slash, *host, *proto_end;
328 credential_clear(c);
331 * Match one of:
332 * (1) proto://<host>/...
333 * (2) proto://<user>@<host>/...
334 * (3) proto://<user>:<pass>@<host>/...
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, '/');
344 if (!at || slash <= at) {
345 /* Case (1) */
346 host = cp;
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;
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';