*.[ch] refactoring: make use of the FREE_AND_NULL() macro
[git.git] / credential.c
blob924ab58538accd8815d8a7c39d5c42ba9441f28e
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_AND_NULL(c->path);
100 static void credential_describe(struct credential *c, struct strbuf *out)
102 if (!c->protocol)
103 return;
104 strbuf_addf(out, "%s://", c->protocol);
105 if (c->username && *c->username)
106 strbuf_addf(out, "%s@", c->username);
107 if (c->host)
108 strbuf_addstr(out, c->host);
109 if (c->path)
110 strbuf_addf(out, "/%s", c->path);
113 static char *credential_ask_one(const char *what, struct credential *c,
114 int flags)
116 struct strbuf desc = STRBUF_INIT;
117 struct strbuf prompt = STRBUF_INIT;
118 char *r;
120 credential_describe(c, &desc);
121 if (desc.len)
122 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
123 else
124 strbuf_addf(&prompt, "%s: ", what);
126 r = git_prompt(prompt.buf, flags);
128 strbuf_release(&desc);
129 strbuf_release(&prompt);
130 return xstrdup(r);
133 static void credential_getpass(struct credential *c)
135 if (!c->username)
136 c->username = credential_ask_one("Username", c,
137 PROMPT_ASKPASS|PROMPT_ECHO);
138 if (!c->password)
139 c->password = credential_ask_one("Password", c,
140 PROMPT_ASKPASS);
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, '=');
151 if (!line.len)
152 break;
154 if (!value) {
155 warning("invalid credential line: %s", key);
156 strbuf_release(&line);
157 return -1;
159 *value++ = '\0';
161 if (!strcmp(key, "username")) {
162 free(c->username);
163 c->username = xstrdup(value);
164 } else if (!strcmp(key, "password")) {
165 free(c->password);
166 c->password = xstrdup(value);
167 } else if (!strcmp(key, "protocol")) {
168 free(c->protocol);
169 c->protocol = xstrdup(value);
170 } else if (!strcmp(key, "host")) {
171 free(c->host);
172 c->host = xstrdup(value);
173 } else if (!strcmp(key, "path")) {
174 free(c->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);
189 return 0;
192 static void credential_write_item(FILE *fp, const char *key, const char *value)
194 if (!value)
195 return;
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,
209 const char *cmd,
210 int want_output)
212 struct child_process helper = CHILD_PROCESS_INIT;
213 const char *argv[] = { NULL, NULL };
214 FILE *fp;
216 argv[0] = cmd;
217 helper.argv = argv;
218 helper.use_shell = 1;
219 helper.in = -1;
220 if (want_output)
221 helper.out = -1;
222 else
223 helper.no_stdout = 1;
225 if (start_command(&helper) < 0)
226 return -1;
228 fp = xfdopen(helper.in, "w");
229 credential_write(c, fp);
230 fclose(fp);
232 if (want_output) {
233 int r;
234 fp = xfdopen(helper.out, "r");
235 r = credential_read(c, fp);
236 fclose(fp);
237 if (r < 0) {
238 finish_command(&helper);
239 return -1;
243 if (finish_command(&helper))
244 return -1;
245 return 0;
248 static int credential_do(struct credential *c, const char *helper,
249 const char *operation)
251 struct strbuf cmd = STRBUF_INIT;
252 int r;
254 if (helper[0] == '!')
255 strbuf_addstr(&cmd, helper + 1);
256 else if (is_absolute_path(helper))
257 strbuf_addstr(&cmd, helper);
258 else
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);
265 return r;
268 void credential_fill(struct credential *c)
270 int i;
272 if (c->username && c->password)
273 return;
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)
280 return;
281 if (c->quit)
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)
293 int i;
295 if (c->approved)
296 return;
297 if (!c->username || !c->password)
298 return;
300 credential_apply_config(c);
302 for (i = 0; i < c->helpers.nr; i++)
303 credential_do(c, c->helpers.items[i].string, "store");
304 c->approved = 1;
307 void credential_reject(struct credential *c)
309 int i;
311 credential_apply_config(c);
313 for (i = 0; i < c->helpers.nr; i++)
314 credential_do(c, c->helpers.items[i].string, "erase");
316 FREE_AND_NULL(c->username);
317 FREE_AND_NULL(c->password);
318 c->approved = 0;
321 void credential_from_url(struct credential *c, const char *url)
323 const char *at, *colon, *cp, *slash, *host, *proto_end;
325 credential_clear(c);
328 * Match one of:
329 * (1) proto://<host>/...
330 * (2) proto://<user>@<host>/...
331 * (3) proto://<user>:<pass>@<host>/...
333 proto_end = strstr(url, "://");
334 if (!proto_end)
335 return;
336 cp = proto_end + 3;
337 at = strchr(cp, '@');
338 colon = strchr(cp, ':');
339 slash = strchrnul(cp, '/');
341 if (!at || slash <= at) {
342 /* Case (1) */
343 host = cp;
345 else if (!colon || at <= colon) {
346 /* Case (2) */
347 c->username = url_decode_mem(cp, at - cp);
348 host = at + 1;
349 } else {
350 /* Case (3) */
351 c->username = url_decode_mem(cp, colon - cp);
352 c->password = url_decode_mem(colon + 1, at - (colon + 1));
353 host = at + 1;
356 if (proto_end - url > 0)
357 c->protocol = xmemdupz(url, proto_end - url);
358 if (slash - host > 0)
359 c->host = url_decode_mem(host, slash - host);
360 /* Trim leading and trailing slashes from path */
361 while (*slash == '/')
362 slash++;
363 if (*slash) {
364 char *p;
365 c->path = url_decode(slash);
366 p = c->path + strlen(c->path) - 1;
367 while (p > c->path && *p == '/')
368 *p-- = '\0';