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
)
93 die(_("refusing to work with credential missing host field"));
95 die(_("refusing to work with credential missing protocol field"));
99 git_config(credential_config_callback
, c
);
102 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
103 FREE_AND_NULL(c
->path
);
107 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
111 strbuf_addf(out
, "%s://", c
->protocol
);
112 if (c
->username
&& *c
->username
)
113 strbuf_addf(out
, "%s@", c
->username
);
115 strbuf_addstr(out
, c
->host
);
117 strbuf_addf(out
, "/%s", c
->path
);
120 static char *credential_ask_one(const char *what
, struct credential
*c
,
123 struct strbuf desc
= STRBUF_INIT
;
124 struct strbuf prompt
= STRBUF_INIT
;
127 credential_describe(c
, &desc
);
129 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
131 strbuf_addf(&prompt
, "%s: ", what
);
133 r
= git_prompt(prompt
.buf
, flags
);
135 strbuf_release(&desc
);
136 strbuf_release(&prompt
);
140 static void credential_getpass(struct credential
*c
)
143 c
->username
= credential_ask_one("Username", c
,
144 PROMPT_ASKPASS
|PROMPT_ECHO
);
146 c
->password
= credential_ask_one("Password", c
,
150 int credential_read(struct credential
*c
, FILE *fp
)
152 struct strbuf line
= STRBUF_INIT
;
154 while (strbuf_getline_lf(&line
, fp
) != EOF
) {
155 char *key
= line
.buf
;
156 char *value
= strchr(key
, '=');
162 warning("invalid credential line: %s", key
);
163 strbuf_release(&line
);
168 if (!strcmp(key
, "username")) {
170 c
->username
= xstrdup(value
);
171 } else if (!strcmp(key
, "password")) {
173 c
->password
= xstrdup(value
);
174 } else if (!strcmp(key
, "protocol")) {
176 c
->protocol
= xstrdup(value
);
177 } else if (!strcmp(key
, "host")) {
179 c
->host
= xstrdup(value
);
180 } else if (!strcmp(key
, "path")) {
182 c
->path
= xstrdup(value
);
183 } else if (!strcmp(key
, "url")) {
184 credential_from_url(c
, value
);
185 } else if (!strcmp(key
, "quit")) {
186 c
->quit
= !!git_config_bool("quit", value
);
189 * Ignore other lines; we don't know what they mean, but
190 * this future-proofs us when later versions of git do
191 * learn new lines, and the helpers are updated to match.
195 strbuf_release(&line
);
199 static void credential_write_item(FILE *fp
, const char *key
, const char *value
,
202 if (!value
&& required
)
203 BUG("credential value for %s is missing", key
);
206 if (strchr(value
, '\n'))
207 die("credential value for %s contains newline", key
);
208 fprintf(fp
, "%s=%s\n", key
, value
);
211 void credential_write(const struct credential
*c
, FILE *fp
)
213 credential_write_item(fp
, "protocol", c
->protocol
, 1);
214 credential_write_item(fp
, "host", c
->host
, 1);
215 credential_write_item(fp
, "path", c
->path
, 0);
216 credential_write_item(fp
, "username", c
->username
, 0);
217 credential_write_item(fp
, "password", c
->password
, 0);
220 static int run_credential_helper(struct credential
*c
,
224 struct child_process helper
= CHILD_PROCESS_INIT
;
225 const char *argv
[] = { NULL
, NULL
};
230 helper
.use_shell
= 1;
235 helper
.no_stdout
= 1;
237 if (start_command(&helper
) < 0)
240 fp
= xfdopen(helper
.in
, "w");
241 sigchain_push(SIGPIPE
, SIG_IGN
);
242 credential_write(c
, fp
);
244 sigchain_pop(SIGPIPE
);
248 fp
= xfdopen(helper
.out
, "r");
249 r
= credential_read(c
, fp
);
252 finish_command(&helper
);
257 if (finish_command(&helper
))
262 static int credential_do(struct credential
*c
, const char *helper
,
263 const char *operation
)
265 struct strbuf cmd
= STRBUF_INIT
;
268 if (helper
[0] == '!')
269 strbuf_addstr(&cmd
, helper
+ 1);
270 else if (is_absolute_path(helper
))
271 strbuf_addstr(&cmd
, helper
);
273 strbuf_addf(&cmd
, "git credential-%s", helper
);
275 strbuf_addf(&cmd
, " %s", operation
);
276 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
278 strbuf_release(&cmd
);
282 void credential_fill(struct credential
*c
)
286 if (c
->username
&& c
->password
)
289 credential_apply_config(c
);
291 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
292 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
293 if (c
->username
&& c
->password
)
296 die("credential helper '%s' told us to quit",
297 c
->helpers
.items
[i
].string
);
300 credential_getpass(c
);
301 if (!c
->username
&& !c
->password
)
302 die("unable to get password from user");
305 void credential_approve(struct credential
*c
)
311 if (!c
->username
|| !c
->password
)
314 credential_apply_config(c
);
316 for (i
= 0; i
< c
->helpers
.nr
; i
++)
317 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
321 void credential_reject(struct credential
*c
)
325 credential_apply_config(c
);
327 for (i
= 0; i
< c
->helpers
.nr
; i
++)
328 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
330 FREE_AND_NULL(c
->username
);
331 FREE_AND_NULL(c
->password
);
335 static int check_url_component(const char *url
, int quiet
,
336 const char *name
, const char *value
)
340 if (!strchr(value
, '\n'))
344 warning(_("url contains a newline in its %s component: %s"),
349 int credential_from_url_gently(struct credential
*c
, const char *url
,
352 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
358 * (1) proto://<host>/...
359 * (2) proto://<user>@<host>/...
360 * (3) proto://<user>:<pass>@<host>/...
362 proto_end
= strstr(url
, "://");
363 if (!proto_end
|| proto_end
== url
) {
365 warning(_("url has no scheme: %s"), url
);
369 at
= strchr(cp
, '@');
370 colon
= strchr(cp
, ':');
371 slash
= strchrnul(cp
, '/');
373 if (!at
|| slash
<= at
) {
377 else if (!colon
|| at
<= colon
) {
379 c
->username
= url_decode_mem(cp
, at
- cp
);
383 c
->username
= url_decode_mem(cp
, colon
- cp
);
384 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
388 c
->protocol
= xmemdupz(url
, proto_end
- url
);
389 c
->host
= url_decode_mem(host
, slash
- host
);
390 /* Trim leading and trailing slashes from path */
391 while (*slash
== '/')
395 c
->path
= url_decode(slash
);
396 p
= c
->path
+ strlen(c
->path
) - 1;
397 while (p
> c
->path
&& *p
== '/')
401 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
402 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
403 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
404 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
405 check_url_component(url
, quiet
, "path", c
->path
) < 0)
411 void credential_from_url(struct credential
*c
, const char *url
)
413 if (credential_from_url_gently(c
, url
, 0) < 0)
414 die(_("credential url cannot be parsed: %s"), url
);