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")) {
74 if (!c
->username_from_proto
) {
76 c
->username
= xstrdup(value
);
79 else if (!strcmp(key
, "usehttppath"))
80 c
->use_http_path
= git_config_bool(var
, value
);
85 static int proto_is_http(const char *s
)
89 return !strcmp(s
, "https") || !strcmp(s
, "http");
92 static void credential_apply_config(struct credential
*c
)
96 git_config(credential_config_callback
, c
);
99 if (!c
->use_http_path
&& proto_is_http(c
->protocol
)) {
100 FREE_AND_NULL(c
->path
);
104 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
108 strbuf_addf(out
, "%s://", c
->protocol
);
109 if (c
->username
&& *c
->username
)
110 strbuf_addf(out
, "%s@", c
->username
);
112 strbuf_addstr(out
, c
->host
);
114 strbuf_addf(out
, "/%s", c
->path
);
117 static char *credential_ask_one(const char *what
, struct credential
*c
,
120 struct strbuf desc
= STRBUF_INIT
;
121 struct strbuf prompt
= STRBUF_INIT
;
124 credential_describe(c
, &desc
);
126 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
128 strbuf_addf(&prompt
, "%s: ", what
);
130 r
= git_prompt(prompt
.buf
, flags
);
132 strbuf_release(&desc
);
133 strbuf_release(&prompt
);
137 static void credential_getpass(struct credential
*c
)
140 c
->username
= credential_ask_one("Username", c
,
141 PROMPT_ASKPASS
|PROMPT_ECHO
);
143 c
->password
= credential_ask_one("Password", c
,
147 int credential_read(struct credential
*c
, FILE *fp
)
149 struct strbuf line
= STRBUF_INIT
;
151 while (strbuf_getline_lf(&line
, fp
) != EOF
) {
152 char *key
= line
.buf
;
153 char *value
= strchr(key
, '=');
159 warning("invalid credential line: %s", key
);
160 strbuf_release(&line
);
165 if (!strcmp(key
, "username")) {
167 c
->username
= xstrdup(value
);
168 c
->username_from_proto
= 1;
169 } else if (!strcmp(key
, "password")) {
171 c
->password
= xstrdup(value
);
172 } else if (!strcmp(key
, "protocol")) {
174 c
->protocol
= xstrdup(value
);
175 } else if (!strcmp(key
, "host")) {
177 c
->host
= xstrdup(value
);
178 } else if (!strcmp(key
, "path")) {
180 c
->path
= xstrdup(value
);
181 } else if (!strcmp(key
, "url")) {
182 credential_from_url(c
, value
);
183 } else if (!strcmp(key
, "quit")) {
184 c
->quit
= !!git_config_bool("quit", value
);
187 * Ignore other lines; we don't know what they mean, but
188 * this future-proofs us when later versions of git do
189 * learn new lines, and the helpers are updated to match.
193 strbuf_release(&line
);
197 static void credential_write_item(FILE *fp
, const char *key
, const char *value
)
201 fprintf(fp
, "%s=%s\n", key
, value
);
204 void credential_write(const struct credential
*c
, FILE *fp
)
206 credential_write_item(fp
, "protocol", c
->protocol
);
207 credential_write_item(fp
, "host", c
->host
);
208 credential_write_item(fp
, "path", c
->path
);
209 credential_write_item(fp
, "username", c
->username
);
210 credential_write_item(fp
, "password", c
->password
);
213 static int run_credential_helper(struct credential
*c
,
217 struct child_process helper
= CHILD_PROCESS_INIT
;
218 const char *argv
[] = { NULL
, NULL
};
223 helper
.use_shell
= 1;
228 helper
.no_stdout
= 1;
230 if (start_command(&helper
) < 0)
233 fp
= xfdopen(helper
.in
, "w");
234 sigchain_push(SIGPIPE
, SIG_IGN
);
235 credential_write(c
, fp
);
237 sigchain_pop(SIGPIPE
);
241 fp
= xfdopen(helper
.out
, "r");
242 r
= credential_read(c
, fp
);
245 finish_command(&helper
);
250 if (finish_command(&helper
))
255 static int credential_do(struct credential
*c
, const char *helper
,
256 const char *operation
)
258 struct strbuf cmd
= STRBUF_INIT
;
261 if (helper
[0] == '!')
262 strbuf_addstr(&cmd
, helper
+ 1);
263 else if (is_absolute_path(helper
))
264 strbuf_addstr(&cmd
, helper
);
266 strbuf_addf(&cmd
, "git credential-%s", helper
);
268 strbuf_addf(&cmd
, " %s", operation
);
269 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
271 strbuf_release(&cmd
);
275 void credential_fill(struct credential
*c
)
279 if (c
->username
&& c
->password
)
282 credential_apply_config(c
);
284 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
285 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
286 if (c
->username
&& c
->password
)
289 die("credential helper '%s' told us to quit",
290 c
->helpers
.items
[i
].string
);
293 credential_getpass(c
);
294 if (!c
->username
&& !c
->password
)
295 die("unable to get password from user");
298 void credential_approve(struct credential
*c
)
304 if (!c
->username
|| !c
->password
)
307 credential_apply_config(c
);
309 for (i
= 0; i
< c
->helpers
.nr
; i
++)
310 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
314 void credential_reject(struct credential
*c
)
318 credential_apply_config(c
);
320 for (i
= 0; i
< c
->helpers
.nr
; i
++)
321 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
323 FREE_AND_NULL(c
->username
);
324 FREE_AND_NULL(c
->password
);
328 void credential_from_url(struct credential
*c
, const char *url
)
330 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
336 * (1) proto://<host>/...
337 * (2) proto://<user>@<host>/...
338 * (3) proto://<user>:<pass>@<host>/...
340 proto_end
= strstr(url
, "://");
344 at
= strchr(cp
, '@');
345 colon
= strchr(cp
, ':');
346 slash
= strchrnul(cp
, '/');
348 if (!at
|| slash
<= at
) {
352 else if (!colon
|| at
<= colon
) {
354 c
->username
= url_decode_mem(cp
, at
- cp
);
355 if (c
->username
&& *c
->username
)
356 c
->username_from_proto
= 1;
360 c
->username
= url_decode_mem(cp
, colon
- cp
);
361 if (c
->username
&& *c
->username
)
362 c
->username_from_proto
= 1;
363 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
367 if (proto_end
- url
> 0)
368 c
->protocol
= xmemdupz(url
, proto_end
- url
);
369 if (slash
- host
> 0)
370 c
->host
= url_decode_mem(host
, slash
- host
);
371 /* Trim leading and trailing slashes from path */
372 while (*slash
== '/')
376 c
->path
= url_decode(slash
);
377 p
= c
->path
+ strlen(c
->path
) - 1;
378 while (p
> c
->path
&& *p
== '/')