3 #include "credential.h"
4 #include "string-list.h"
5 #include "run-command.h"
9 void credential_init(struct credential
*c
)
11 memset(c
, 0, sizeof(*c
));
12 c
->helpers
.strdup_strings
= 1;
15 void credential_clear(struct credential
*c
)
22 string_list_clear(&c
->helpers
, 0);
27 int credential_match(const struct credential
*want
,
28 const struct credential
*have
)
30 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
31 return CHECK(protocol
) &&
38 static int credential_config_callback(const char *var
, const char *value
,
41 struct credential
*c
= data
;
42 const char *key
, *dot
;
44 if (!skip_prefix(var
, "credential.", &key
))
48 return config_error_nonbool(var
);
50 dot
= strrchr(key
, '.');
52 struct credential want
= CREDENTIAL_INIT
;
53 char *url
= xmemdupz(key
, dot
- key
);
56 credential_from_url(&want
, url
);
57 matched
= credential_match(&want
, c
);
59 credential_clear(&want
);
67 if (!strcmp(key
, "helper")) {
69 string_list_append(&c
->helpers
, value
);
71 string_list_clear(&c
->helpers
, 0);
72 } else if (!strcmp(key
, "username")) {
74 c
->username
= xstrdup(value
);
76 else if (!strcmp(key
, "usehttppath"))
77 c
->use_http_path
= git_config_bool(var
, value
);
82 static int proto_is_http(const char *s
)
86 return !strcmp(s
, "https") || !strcmp(s
, "http");
89 static void credential_apply_config(struct credential
*c
)
93 git_config(credential_config_callback
, c
);
96 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
97 FREE_AND_NULL(c
->path
);
101 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
105 strbuf_addf(out
, "%s://", c
->protocol
);
106 if (c
->username
&& *c
->username
)
107 strbuf_addf(out
, "%s@", c
->username
);
109 strbuf_addstr(out
, c
->host
);
111 strbuf_addf(out
, "/%s", c
->path
);
114 static char *credential_ask_one(const char *what
, struct credential
*c
,
117 struct strbuf desc
= STRBUF_INIT
;
118 struct strbuf prompt
= STRBUF_INIT
;
121 credential_describe(c
, &desc
);
123 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
125 strbuf_addf(&prompt
, "%s: ", what
);
127 r
= git_prompt(prompt
.buf
, flags
);
129 strbuf_release(&desc
);
130 strbuf_release(&prompt
);
134 static void credential_getpass(struct credential
*c
)
137 c
->username
= credential_ask_one("Username", c
,
138 PROMPT_ASKPASS
|PROMPT_ECHO
);
140 c
->password
= credential_ask_one("Password", c
,
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
, '=');
156 warning("invalid credential line: %s", key
);
157 strbuf_release(&line
);
162 if (!strcmp(key
, "username")) {
164 c
->username
= xstrdup(value
);
165 } else if (!strcmp(key
, "password")) {
167 c
->password
= xstrdup(value
);
168 } else if (!strcmp(key
, "protocol")) {
170 c
->protocol
= xstrdup(value
);
171 } else if (!strcmp(key
, "host")) {
173 c
->host
= xstrdup(value
);
174 } else if (!strcmp(key
, "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
);
193 static void credential_write_item(FILE *fp
, const char *key
, const char *value
)
197 if (strchr(value
, '\n'))
198 die("credential value for %s contains newline", key
);
199 fprintf(fp
, "%s=%s\n", key
, value
);
202 void credential_write(const struct credential
*c
, FILE *fp
)
204 credential_write_item(fp
, "protocol", c
->protocol
);
205 credential_write_item(fp
, "host", c
->host
);
206 credential_write_item(fp
, "path", c
->path
);
207 credential_write_item(fp
, "username", c
->username
);
208 credential_write_item(fp
, "password", c
->password
);
211 static int run_credential_helper(struct credential
*c
,
215 struct child_process helper
= CHILD_PROCESS_INIT
;
216 const char *argv
[] = { NULL
, NULL
};
221 helper
.use_shell
= 1;
226 helper
.no_stdout
= 1;
228 if (start_command(&helper
) < 0)
231 fp
= xfdopen(helper
.in
, "w");
232 credential_write(c
, fp
);
237 fp
= xfdopen(helper
.out
, "r");
238 r
= credential_read(c
, fp
);
241 finish_command(&helper
);
246 if (finish_command(&helper
))
251 static int credential_do(struct credential
*c
, const char *helper
,
252 const char *operation
)
254 struct strbuf cmd
= STRBUF_INIT
;
257 if (helper
[0] == '!')
258 strbuf_addstr(&cmd
, helper
+ 1);
259 else if (is_absolute_path(helper
))
260 strbuf_addstr(&cmd
, helper
);
262 strbuf_addf(&cmd
, "git credential-%s", helper
);
264 strbuf_addf(&cmd
, " %s", operation
);
265 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
267 strbuf_release(&cmd
);
271 void credential_fill(struct credential
*c
)
275 if (c
->username
&& c
->password
)
278 credential_apply_config(c
);
280 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
281 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
282 if (c
->username
&& c
->password
)
285 die("credential helper '%s' told us to quit",
286 c
->helpers
.items
[i
].string
);
289 credential_getpass(c
);
290 if (!c
->username
&& !c
->password
)
291 die("unable to get password from user");
294 void credential_approve(struct credential
*c
)
300 if (!c
->username
|| !c
->password
)
303 credential_apply_config(c
);
305 for (i
= 0; i
< c
->helpers
.nr
; i
++)
306 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
310 void credential_reject(struct credential
*c
)
314 credential_apply_config(c
);
316 for (i
= 0; i
< c
->helpers
.nr
; i
++)
317 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
319 FREE_AND_NULL(c
->username
);
320 FREE_AND_NULL(c
->password
);
324 static int check_url_component(const char *url
, int quiet
,
325 const char *name
, const char *value
)
329 if (!strchr(value
, '\n'))
333 warning(_("url contains a newline in its %s component: %s"),
338 int credential_from_url_gently(struct credential
*c
, const char *url
,
341 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
347 * (1) proto://<host>/...
348 * (2) proto://<user>@<host>/...
349 * (3) proto://<user>:<pass>@<host>/...
351 proto_end
= strstr(url
, "://");
355 at
= strchr(cp
, '@');
356 colon
= strchr(cp
, ':');
357 slash
= strchrnul(cp
, '/');
359 if (!at
|| slash
<= at
) {
363 else if (!colon
|| at
<= colon
) {
365 c
->username
= url_decode_mem(cp
, at
- cp
);
369 c
->username
= url_decode_mem(cp
, colon
- cp
);
370 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
374 if (proto_end
- url
> 0)
375 c
->protocol
= xmemdupz(url
, proto_end
- url
);
376 c
->host
= url_decode_mem(host
, slash
- host
);
377 /* Trim leading and trailing slashes from path */
378 while (*slash
== '/')
382 c
->path
= url_decode(slash
);
383 p
= c
->path
+ strlen(c
->path
) - 1;
384 while (p
> c
->path
&& *p
== '/')
388 if (check_url_component(url
, quiet
, "username", c
->username
) < 0 ||
389 check_url_component(url
, quiet
, "password", c
->password
) < 0 ||
390 check_url_component(url
, quiet
, "protocol", c
->protocol
) < 0 ||
391 check_url_component(url
, quiet
, "host", c
->host
) < 0 ||
392 check_url_component(url
, quiet
, "path", c
->path
) < 0)
398 void credential_from_url(struct credential
*c
, const char *url
)
400 if (credential_from_url_gently(c
, url
, 0) < 0) {
401 warning(_("skipping credential lookup for url: %s"), url
);