2 #include "credential.h"
3 #include "string-list.h"
4 #include "run-command.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
)
21 string_list_clear(&c
->helpers
, 0);
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
) &&
37 static int credential_config_callback(const char *var
, const char *value
,
40 struct credential
*c
= data
;
41 const char *key
, *dot
;
43 if (!skip_prefix(var
, "credential.", &key
))
47 return config_error_nonbool(var
);
49 dot
= strrchr(key
, '.');
51 struct credential want
= CREDENTIAL_INIT
;
52 char *url
= xmemdupz(key
, dot
- key
);
55 credential_from_url(&want
, url
);
56 matched
= credential_match(&want
, c
);
58 credential_clear(&want
);
66 if (!strcmp(key
, "helper"))
67 string_list_append(&c
->helpers
, value
);
68 else if (!strcmp(key
, "username")) {
70 c
->username
= xstrdup(value
);
72 else if (!strcmp(key
, "usehttppath"))
73 c
->use_http_path
= git_config_bool(var
, value
);
78 static int proto_is_http(const char *s
)
82 return !strcmp(s
, "https") || !strcmp(s
, "http");
85 static void credential_apply_config(struct credential
*c
)
89 git_config(credential_config_callback
, c
);
92 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
98 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
102 strbuf_addf(out
, "%s://", c
->protocol
);
103 if (c
->username
&& *c
->username
)
104 strbuf_addf(out
, "%s@", c
->username
);
106 strbuf_addstr(out
, c
->host
);
108 strbuf_addf(out
, "/%s", c
->path
);
111 static char *credential_ask_one(const char *what
, struct credential
*c
,
114 struct strbuf desc
= STRBUF_INIT
;
115 struct strbuf prompt
= STRBUF_INIT
;
118 credential_describe(c
, &desc
);
120 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
122 strbuf_addf(&prompt
, "%s: ", what
);
124 r
= git_prompt(prompt
.buf
, flags
);
126 strbuf_release(&desc
);
127 strbuf_release(&prompt
);
131 static void credential_getpass(struct credential
*c
)
134 c
->username
= credential_ask_one("Username", c
,
135 PROMPT_ASKPASS
|PROMPT_ECHO
);
137 c
->password
= credential_ask_one("Password", c
,
141 int credential_read(struct credential
*c
, FILE *fp
)
143 struct strbuf line
= STRBUF_INIT
;
145 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
146 char *key
= line
.buf
;
147 char *value
= strchr(key
, '=');
153 warning("invalid credential line: %s", key
);
154 strbuf_release(&line
);
159 if (!strcmp(key
, "username")) {
161 c
->username
= xstrdup(value
);
162 } else if (!strcmp(key
, "password")) {
164 c
->password
= xstrdup(value
);
165 } else if (!strcmp(key
, "protocol")) {
167 c
->protocol
= xstrdup(value
);
168 } else if (!strcmp(key
, "host")) {
170 c
->host
= xstrdup(value
);
171 } else if (!strcmp(key
, "path")) {
173 c
->path
= xstrdup(value
);
174 } else if (!strcmp(key
, "url")) {
175 credential_from_url(c
, value
);
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.
184 strbuf_release(&line
);
188 static void credential_write_item(FILE *fp
, const char *key
, const char *value
)
192 fprintf(fp
, "%s=%s\n", key
, value
);
195 void credential_write(const struct credential
*c
, FILE *fp
)
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
);
204 static int run_credential_helper(struct credential
*c
,
208 struct child_process helper
;
209 const char *argv
[] = { NULL
, NULL
};
212 memset(&helper
, 0, sizeof(helper
));
215 helper
.use_shell
= 1;
220 helper
.no_stdout
= 1;
222 if (start_command(&helper
) < 0)
225 fp
= xfdopen(helper
.in
, "w");
226 credential_write(c
, fp
);
231 fp
= xfdopen(helper
.out
, "r");
232 r
= credential_read(c
, fp
);
235 finish_command(&helper
);
240 if (finish_command(&helper
))
245 static int credential_do(struct credential
*c
, const char *helper
,
246 const char *operation
)
248 struct strbuf cmd
= STRBUF_INIT
;
251 if (helper
[0] == '!')
252 strbuf_addstr(&cmd
, helper
+ 1);
253 else if (is_absolute_path(helper
))
254 strbuf_addstr(&cmd
, helper
);
256 strbuf_addf(&cmd
, "git credential-%s", helper
);
258 strbuf_addf(&cmd
, " %s", operation
);
259 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
261 strbuf_release(&cmd
);
265 void credential_fill(struct credential
*c
)
269 if (c
->username
&& c
->password
)
272 credential_apply_config(c
);
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
)
280 credential_getpass(c
);
281 if (!c
->username
&& !c
->password
)
282 die("unable to get password from user");
285 void credential_approve(struct credential
*c
)
291 if (!c
->username
|| !c
->password
)
294 credential_apply_config(c
);
296 for (i
= 0; i
< c
->helpers
.nr
; i
++)
297 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
301 void credential_reject(struct credential
*c
)
305 credential_apply_config(c
);
307 for (i
= 0; i
< c
->helpers
.nr
; i
++)
308 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
317 void credential_from_url(struct credential
*c
, const char *url
)
319 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
325 * (1) proto://<host>/...
326 * (2) proto://<user>@<host>/...
327 * (3) proto://<user>:<pass>@<host>/...
329 proto_end
= strstr(url
, "://");
333 at
= strchr(cp
, '@');
334 colon
= strchr(cp
, ':');
335 slash
= strchrnul(cp
, '/');
337 if (!at
|| slash
<= at
) {
341 else if (!colon
|| at
<= colon
) {
343 c
->username
= url_decode_mem(cp
, at
- cp
);
347 c
->username
= url_decode_mem(cp
, colon
- cp
);
348 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
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
== '/')
361 c
->path
= url_decode(slash
);
362 p
= c
->path
+ strlen(c
->path
) - 1;
363 while (p
> c
->path
&& *p
== '/')