3 #include "credential.h"
4 #include "string-list.h"
5 #include "run-command.h"
10 void credential_init(struct credential
*c
)
12 memset(c
, 0, sizeof(*c
));
13 c
->helpers
.strdup_strings
= 1;
16 void credential_clear(struct credential
*c
)
23 string_list_clear(&c
->helpers
, 0);
28 int credential_match(const struct credential
*want
,
29 const struct credential
*have
)
31 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
32 return CHECK(protocol
) &&
39 static int credential_config_callback(const char *var
, const char *value
,
42 struct credential
*c
= data
;
43 const char *key
, *dot
;
45 if (!skip_prefix(var
, "credential.", &key
))
49 return config_error_nonbool(var
);
51 dot
= strrchr(key
, '.');
53 struct credential want
= CREDENTIAL_INIT
;
54 char *url
= xmemdupz(key
, dot
- key
);
57 credential_from_url(&want
, url
);
58 matched
= credential_match(&want
, c
);
60 credential_clear(&want
);
68 if (!strcmp(key
, "helper")) {
70 string_list_append(&c
->helpers
, value
);
72 string_list_clear(&c
->helpers
, 0);
73 } else if (!strcmp(key
, "username")) {
75 c
->username
= xstrdup(value
);
77 else if (!strcmp(key
, "usehttppath"))
78 c
->use_http_path
= git_config_bool(var
, value
);
83 static int proto_is_http(const char *s
)
87 return !strcmp(s
, "https") || !strcmp(s
, "http");
90 static void credential_apply_config(struct credential
*c
)
94 git_config(credential_config_callback
, c
);
97 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
98 FREE_AND_NULL(c
->path
);
102 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
106 strbuf_addf(out
, "%s://", c
->protocol
);
107 if (c
->username
&& *c
->username
)
108 strbuf_addf(out
, "%s@", c
->username
);
110 strbuf_addstr(out
, c
->host
);
112 strbuf_addf(out
, "/%s", c
->path
);
115 static char *credential_ask_one(const char *what
, struct credential
*c
,
118 struct strbuf desc
= STRBUF_INIT
;
119 struct strbuf prompt
= STRBUF_INIT
;
122 credential_describe(c
, &desc
);
124 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
126 strbuf_addf(&prompt
, "%s: ", what
);
128 r
= git_prompt(prompt
.buf
, flags
);
130 strbuf_release(&desc
);
131 strbuf_release(&prompt
);
135 static void credential_getpass(struct credential
*c
)
138 c
->username
= credential_ask_one("Username", c
,
139 PROMPT_ASKPASS
|PROMPT_ECHO
);
141 c
->password
= credential_ask_one("Password", c
,
145 int credential_read(struct credential
*c
, FILE *fp
)
147 struct strbuf line
= STRBUF_INIT
;
149 while (strbuf_getline_lf(&line
, fp
) != EOF
) {
150 char *key
= line
.buf
;
151 char *value
= strchr(key
, '=');
157 warning("invalid credential line: %s", key
);
158 strbuf_release(&line
);
163 if (!strcmp(key
, "username")) {
165 c
->username
= xstrdup(value
);
166 } else if (!strcmp(key
, "password")) {
168 c
->password
= xstrdup(value
);
169 } else if (!strcmp(key
, "protocol")) {
171 c
->protocol
= xstrdup(value
);
172 } else if (!strcmp(key
, "host")) {
174 c
->host
= xstrdup(value
);
175 } else if (!strcmp(key
, "path")) {
177 c
->path
= xstrdup(value
);
178 } else if (!strcmp(key
, "url")) {
179 credential_from_url(c
, value
);
180 } else if (!strcmp(key
, "quit")) {
181 c
->quit
= !!git_config_bool("quit", value
);
184 * Ignore other lines; we don't know what they mean, but
185 * this future-proofs us when later versions of git do
186 * learn new lines, and the helpers are updated to match.
190 strbuf_release(&line
);
194 static void credential_write_item(FILE *fp
, const char *key
, const char *value
)
198 if (strchr(value
, '\n'))
199 die("credential value for %s contains newline", key
);
200 fprintf(fp
, "%s=%s\n", key
, value
);
203 void credential_write(const struct credential
*c
, FILE *fp
)
205 credential_write_item(fp
, "protocol", c
->protocol
);
206 credential_write_item(fp
, "host", c
->host
);
207 credential_write_item(fp
, "path", c
->path
);
208 credential_write_item(fp
, "username", c
->username
);
209 credential_write_item(fp
, "password", c
->password
);
212 static int run_credential_helper(struct credential
*c
,
216 struct child_process helper
= CHILD_PROCESS_INIT
;
217 const char *argv
[] = { NULL
, NULL
};
222 helper
.use_shell
= 1;
227 helper
.no_stdout
= 1;
229 if (start_command(&helper
) < 0)
232 fp
= xfdopen(helper
.in
, "w");
233 sigchain_push(SIGPIPE
, SIG_IGN
);
234 credential_write(c
, fp
);
236 sigchain_pop(SIGPIPE
);
240 fp
= xfdopen(helper
.out
, "r");
241 r
= credential_read(c
, fp
);
244 finish_command(&helper
);
249 if (finish_command(&helper
))
254 static int credential_do(struct credential
*c
, const char *helper
,
255 const char *operation
)
257 struct strbuf cmd
= STRBUF_INIT
;
260 if (helper
[0] == '!')
261 strbuf_addstr(&cmd
, helper
+ 1);
262 else if (is_absolute_path(helper
))
263 strbuf_addstr(&cmd
, helper
);
265 strbuf_addf(&cmd
, "git credential-%s", helper
);
267 strbuf_addf(&cmd
, " %s", operation
);
268 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
270 strbuf_release(&cmd
);
274 void credential_fill(struct credential
*c
)
278 if (c
->username
&& c
->password
)
281 credential_apply_config(c
);
283 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
284 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
285 if (c
->username
&& c
->password
)
288 die("credential helper '%s' told us to quit",
289 c
->helpers
.items
[i
].string
);
292 credential_getpass(c
);
293 if (!c
->username
&& !c
->password
)
294 die("unable to get password from user");
297 void credential_approve(struct credential
*c
)
303 if (!c
->username
|| !c
->password
)
306 credential_apply_config(c
);
308 for (i
= 0; i
< c
->helpers
.nr
; i
++)
309 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
313 void credential_reject(struct credential
*c
)
317 credential_apply_config(c
);
319 for (i
= 0; i
< c
->helpers
.nr
; i
++)
320 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
322 FREE_AND_NULL(c
->username
);
323 FREE_AND_NULL(c
->password
);
327 static int check_url_component(const char *url
, int quiet
,
328 const char *name
, const char *value
)
332 if (!strchr(value
, '\n'))
336 warning(_("url contains a newline in its %s component: %s"),
341 int credential_from_url_gently(struct credential
*c
, const char *url
,
344 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
350 * (1) proto://<host>/...
351 * (2) proto://<user>@<host>/...
352 * (3) proto://<user>:<pass>@<host>/...
354 proto_end
= strstr(url
, "://");
358 at
= strchr(cp
, '@');
359 colon
= strchr(cp
, ':');
360 slash
= strchrnul(cp
, '/');
362 if (!at
|| slash
<= at
) {
366 else if (!colon
|| at
<= colon
) {
368 c
->username
= url_decode_mem(cp
, at
- cp
);
372 c
->username
= url_decode_mem(cp
, colon
- cp
);
373 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
377 if (proto_end
- url
> 0)
378 c
->protocol
= xmemdupz(url
, proto_end
- url
);
379 if (slash
- host
> 0)
380 c
->host
= url_decode_mem(host
, slash
- host
);
381 /* Trim leading and trailing slashes from path */
382 while (*slash
== '/')
386 c
->path
= url_decode(slash
);
387 p
= c
->path
+ strlen(c
->path
) - 1;
388 while (p
> c
->path
&& *p
== '/')
392 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
393 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
394 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
395 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
396 check_url_component(url
, quiet
, "path", c
->path
) < 0)
402 void credential_from_url(struct credential
*c
, const char *url
)
404 if (credential_from_url_gently(c
, url
, 0) < 0) {
405 warning(_("skipping credential lookup for url: %s"), url
);