move git_getpass to its own source file
[git/git.git] / credential.c
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"
7
8 void credential_init(struct credential *c)
9 {
10 memset(c, 0, sizeof(*c));
11 c->helpers.strdup_strings = 1;
12 }
13
14 void credential_clear(struct credential *c)
15 {
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);
22
23 credential_init(c);
24 }
25
26 int credential_match(const struct credential *want,
27 const struct credential *have)
28 {
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
35 }
36
37 static int credential_config_callback(const char *var, const char *value,
38 void *data)
39 {
40 struct credential *c = data;
41 const char *key, *dot;
42
43 key = skip_prefix(var, "credential.");
44 if (!key)
45 return 0;
46
47 if (!value)
48 return config_error_nonbool(var);
49
50 dot = strrchr(key, '.');
51 if (dot) {
52 struct credential want = CREDENTIAL_INIT;
53 char *url = xmemdupz(key, dot - key);
54 int matched;
55
56 credential_from_url(&want, url);
57 matched = credential_match(&want, c);
58
59 credential_clear(&want);
60 free(url);
61
62 if (!matched)
63 return 0;
64 key = dot + 1;
65 }
66
67 if (!strcmp(key, "helper"))
68 string_list_append(&c->helpers, value);
69 else if (!strcmp(key, "username")) {
70 if (!c->username)
71 c->username = xstrdup(value);
72 }
73 else if (!strcmp(key, "usehttppath"))
74 c->use_http_path = git_config_bool(var, value);
75
76 return 0;
77 }
78
79 static int proto_is_http(const char *s)
80 {
81 if (!s)
82 return 0;
83 return !strcmp(s, "https") || !strcmp(s, "http");
84 }
85
86 static void credential_apply_config(struct credential *c)
87 {
88 if (c->configured)
89 return;
90 git_config(credential_config_callback, c);
91 c->configured = 1;
92
93 if (!c->use_http_path && proto_is_http(c->protocol)) {
94 free(c->path);
95 c->path = NULL;
96 }
97 }
98
99 static void credential_describe(struct credential *c, struct strbuf *out)
100 {
101 if (!c->protocol)
102 return;
103 strbuf_addf(out, "%s://", c->protocol);
104 if (c->username && *c->username)
105 strbuf_addf(out, "%s@", c->username);
106 if (c->host)
107 strbuf_addstr(out, c->host);
108 if (c->path)
109 strbuf_addf(out, "/%s", c->path);
110 }
111
112 static char *credential_ask_one(const char *what, struct credential *c)
113 {
114 struct strbuf desc = STRBUF_INIT;
115 struct strbuf prompt = STRBUF_INIT;
116 char *r;
117
118 credential_describe(c, &desc);
119 if (desc.len)
120 strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
121 else
122 strbuf_addf(&prompt, "%s: ", what);
123
124 /* FIXME: for usernames, we should do something less magical that
125 * actually echoes the characters. However, we need to read from
126 * /dev/tty and not stdio, which is not portable (but getpass will do
127 * it for us). http.c uses the same workaround. */
128 r = git_getpass(prompt.buf);
129
130 strbuf_release(&desc);
131 strbuf_release(&prompt);
132 return xstrdup(r);
133 }
134
135 static void credential_getpass(struct credential *c)
136 {
137 if (!c->username)
138 c->username = credential_ask_one("Username", c);
139 if (!c->password)
140 c->password = credential_ask_one("Password", c);
141 }
142
143 int credential_read(struct credential *c, FILE *fp)
144 {
145 struct strbuf line = STRBUF_INIT;
146
147 while (strbuf_getline(&line, fp, '\n') != EOF) {
148 char *key = line.buf;
149 char *value = strchr(key, '=');
150
151 if (!line.len)
152 break;
153
154 if (!value) {
155 warning("invalid credential line: %s", key);
156 strbuf_release(&line);
157 return -1;
158 }
159 *value++ = '\0';
160
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 }
177 /*
178 * Ignore other lines; we don't know what they mean, but
179 * this future-proofs us when later versions of git do
180 * learn new lines, and the helpers are updated to match.
181 */
182 }
183
184 strbuf_release(&line);
185 return 0;
186 }
187
188 static void credential_write_item(FILE *fp, const char *key, const char *value)
189 {
190 if (!value)
191 return;
192 fprintf(fp, "%s=%s\n", key, value);
193 }
194
195 static void credential_write(const struct credential *c, FILE *fp)
196 {
197 credential_write_item(fp, "protocol", c->protocol);
198 credential_write_item(fp, "host", c->host);
199 credential_write_item(fp, "path", c->path);
200 credential_write_item(fp, "username", c->username);
201 credential_write_item(fp, "password", c->password);
202 }
203
204 static int run_credential_helper(struct credential *c,
205 const char *cmd,
206 int want_output)
207 {
208 struct child_process helper;
209 const char *argv[] = { NULL, NULL };
210 FILE *fp;
211
212 memset(&helper, 0, sizeof(helper));
213 argv[0] = cmd;
214 helper.argv = argv;
215 helper.use_shell = 1;
216 helper.in = -1;
217 if (want_output)
218 helper.out = -1;
219 else
220 helper.no_stdout = 1;
221
222 if (start_command(&helper) < 0)
223 return -1;
224
225 fp = xfdopen(helper.in, "w");
226 credential_write(c, fp);
227 fclose(fp);
228
229 if (want_output) {
230 int r;
231 fp = xfdopen(helper.out, "r");
232 r = credential_read(c, fp);
233 fclose(fp);
234 if (r < 0) {
235 finish_command(&helper);
236 return -1;
237 }
238 }
239
240 if (finish_command(&helper))
241 return -1;
242 return 0;
243 }
244
245 static int credential_do(struct credential *c, const char *helper,
246 const char *operation)
247 {
248 struct strbuf cmd = STRBUF_INIT;
249 int r;
250
251 if (helper[0] == '!')
252 strbuf_addstr(&cmd, helper + 1);
253 else if (is_absolute_path(helper))
254 strbuf_addstr(&cmd, helper);
255 else
256 strbuf_addf(&cmd, "git credential-%s", helper);
257
258 strbuf_addf(&cmd, " %s", operation);
259 r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
260
261 strbuf_release(&cmd);
262 return r;
263 }
264
265 void credential_fill(struct credential *c)
266 {
267 int i;
268
269 if (c->username && c->password)
270 return;
271
272 credential_apply_config(c);
273
274 for (i = 0; i < c->helpers.nr; i++) {
275 credential_do(c, c->helpers.items[i].string, "get");
276 if (c->username && c->password)
277 return;
278 }
279
280 credential_getpass(c);
281 if (!c->username && !c->password)
282 die("unable to get password from user");
283 }
284
285 void credential_approve(struct credential *c)
286 {
287 int i;
288
289 if (c->approved)
290 return;
291 if (!c->username || !c->password)
292 return;
293
294 credential_apply_config(c);
295
296 for (i = 0; i < c->helpers.nr; i++)
297 credential_do(c, c->helpers.items[i].string, "store");
298 c->approved = 1;
299 }
300
301 void credential_reject(struct credential *c)
302 {
303 int i;
304
305 credential_apply_config(c);
306
307 for (i = 0; i < c->helpers.nr; i++)
308 credential_do(c, c->helpers.items[i].string, "erase");
309
310 free(c->username);
311 c->username = NULL;
312 free(c->password);
313 c->password = NULL;
314 c->approved = 0;
315 }
316
317 void credential_from_url(struct credential *c, const char *url)
318 {
319 const char *at, *colon, *cp, *slash, *host, *proto_end;
320
321 credential_clear(c);
322
323 /*
324 * Match one of:
325 * (1) proto://<host>/...
326 * (2) proto://<user>@<host>/...
327 * (3) proto://<user>:<pass>@<host>/...
328 */
329 proto_end = strstr(url, "://");
330 if (!proto_end)
331 return;
332 cp = proto_end + 3;
333 at = strchr(cp, '@');
334 colon = strchr(cp, ':');
335 slash = strchrnul(cp, '/');
336
337 if (!at || slash <= at) {
338 /* Case (1) */
339 host = cp;
340 }
341 else if (!colon || at <= colon) {
342 /* Case (2) */
343 c->username = url_decode_mem(cp, at - cp);
344 host = at + 1;
345 } else {
346 /* Case (3) */
347 c->username = url_decode_mem(cp, colon - cp);
348 c->password = url_decode_mem(colon + 1, at - (colon + 1));
349 host = at + 1;
350 }
351
352 if (proto_end - url > 0)
353 c->protocol = xmemdupz(url, proto_end - url);
354 if (slash - host > 0)
355 c->host = url_decode_mem(host, slash - host);
356 /* Trim leading and trailing slashes from path */
357 while (*slash == '/')
358 slash++;
359 if (*slash) {
360 char *p;
361 c->path = url_decode(slash);
362 p = c->path + strlen(c->path) - 1;
363 while (p > c->path && *p == '/')
364 *p-- = '\0';
365 }
366 }