2 #include "credential.h"
3 #include "string-list.h"
4 #include "run-command.h"
7 void credential_init(struct credential
*c
)
9 memset(c
, 0, sizeof(*c
));
10 c
->helpers
.strdup_strings
= 1;
13 void credential_clear(struct credential
*c
)
20 string_list_clear(&c
->helpers
, 0);
25 int credential_match(const struct credential
*want
,
26 const struct credential
*have
)
28 #define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
29 return CHECK(protocol
) &&
36 static int credential_config_callback(const char *var
, const char *value
,
39 struct credential
*c
= data
;
40 const char *key
, *dot
;
42 key
= skip_prefix(var
, "credential.");
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
);
72 static void credential_apply_config(struct credential
*c
)
76 git_config(credential_config_callback
, c
);
80 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
84 strbuf_addf(out
, "%s://", c
->protocol
);
85 if (c
->username
&& *c
->username
)
86 strbuf_addf(out
, "%s@", c
->username
);
88 strbuf_addstr(out
, c
->host
);
90 strbuf_addf(out
, "/%s", c
->path
);
93 static char *credential_ask_one(const char *what
, struct credential
*c
)
95 struct strbuf desc
= STRBUF_INIT
;
96 struct strbuf prompt
= STRBUF_INIT
;
99 credential_describe(c
, &desc
);
101 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
103 strbuf_addf(&prompt
, "%s: ", what
);
105 /* FIXME: for usernames, we should do something less magical that
106 * actually echoes the characters. However, we need to read from
107 * /dev/tty and not stdio, which is not portable (but getpass will do
108 * it for us). http.c uses the same workaround. */
109 r
= git_getpass(prompt
.buf
);
111 strbuf_release(&desc
);
112 strbuf_release(&prompt
);
116 static void credential_getpass(struct credential
*c
)
119 c
->username
= credential_ask_one("Username", c
);
121 c
->password
= credential_ask_one("Password", c
);
124 int credential_read(struct credential
*c
, FILE *fp
)
126 struct strbuf line
= STRBUF_INIT
;
128 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
129 char *key
= line
.buf
;
130 char *value
= strchr(key
, '=');
136 warning("invalid credential line: %s", key
);
137 strbuf_release(&line
);
142 if (!strcmp(key
, "username")) {
144 c
->username
= xstrdup(value
);
145 } else if (!strcmp(key
, "password")) {
147 c
->password
= xstrdup(value
);
148 } else if (!strcmp(key
, "protocol")) {
150 c
->protocol
= xstrdup(value
);
151 } else if (!strcmp(key
, "host")) {
153 c
->host
= xstrdup(value
);
154 } else if (!strcmp(key
, "path")) {
156 c
->path
= xstrdup(value
);
159 * Ignore other lines; we don't know what they mean, but
160 * this future-proofs us when later versions of git do
161 * learn new lines, and the helpers are updated to match.
165 strbuf_release(&line
);
169 static void credential_write_item(FILE *fp
, const char *key
, const char *value
)
173 fprintf(fp
, "%s=%s\n", key
, value
);
176 static void credential_write(const struct credential
*c
, FILE *fp
)
178 credential_write_item(fp
, "protocol", c
->protocol
);
179 credential_write_item(fp
, "host", c
->host
);
180 credential_write_item(fp
, "path", c
->path
);
181 credential_write_item(fp
, "username", c
->username
);
182 credential_write_item(fp
, "password", c
->password
);
185 static int run_credential_helper(struct credential
*c
,
189 struct child_process helper
;
190 const char *argv
[] = { NULL
, NULL
};
193 memset(&helper
, 0, sizeof(helper
));
196 helper
.use_shell
= 1;
201 helper
.no_stdout
= 1;
203 if (start_command(&helper
) < 0)
206 fp
= xfdopen(helper
.in
, "w");
207 credential_write(c
, fp
);
212 fp
= xfdopen(helper
.out
, "r");
213 r
= credential_read(c
, fp
);
216 finish_command(&helper
);
221 if (finish_command(&helper
))
226 static int credential_do(struct credential
*c
, const char *helper
,
227 const char *operation
)
229 struct strbuf cmd
= STRBUF_INIT
;
232 if (helper
[0] == '!')
233 strbuf_addstr(&cmd
, helper
+ 1);
234 else if (is_absolute_path(helper
))
235 strbuf_addstr(&cmd
, helper
);
237 strbuf_addf(&cmd
, "git credential-%s", helper
);
239 strbuf_addf(&cmd
, " %s", operation
);
240 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
242 strbuf_release(&cmd
);
246 void credential_fill(struct credential
*c
)
250 if (c
->username
&& c
->password
)
253 credential_apply_config(c
);
255 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
256 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
257 if (c
->username
&& c
->password
)
261 credential_getpass(c
);
262 if (!c
->username
&& !c
->password
)
263 die("unable to get password from user");
266 void credential_approve(struct credential
*c
)
272 if (!c
->username
|| !c
->password
)
275 credential_apply_config(c
);
277 for (i
= 0; i
< c
->helpers
.nr
; i
++)
278 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
282 void credential_reject(struct credential
*c
)
286 credential_apply_config(c
);
288 for (i
= 0; i
< c
->helpers
.nr
; i
++)
289 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
298 void credential_from_url(struct credential
*c
, const char *url
)
300 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
306 * (1) proto://<host>/...
307 * (2) proto://<user>@<host>/...
308 * (3) proto://<user>:<pass>@<host>/...
310 proto_end
= strstr(url
, "://");
314 at
= strchr(cp
, '@');
315 colon
= strchr(cp
, ':');
316 slash
= strchrnul(cp
, '/');
318 if (!at
|| slash
<= at
) {
322 else if (!colon
|| at
<= colon
) {
324 c
->username
= url_decode_mem(cp
, at
- cp
);
328 c
->username
= url_decode_mem(cp
, colon
- cp
);
329 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
333 if (proto_end
- url
> 0)
334 c
->protocol
= xmemdupz(url
, proto_end
- url
);
335 if (slash
- host
> 0)
336 c
->host
= url_decode_mem(host
, slash
- host
);
337 /* Trim leading and trailing slashes from path */
338 while (*slash
== '/')
342 c
->path
= url_decode(slash
);
343 p
= c
->path
+ strlen(c
->path
) - 1;
344 while (p
> c
->path
&& *p
== '/')