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 static void credential_describe(struct credential
*c
, struct strbuf
*out
)
29 strbuf_addf(out
, "%s://", c
->protocol
);
30 if (c
->username
&& *c
->username
)
31 strbuf_addf(out
, "%s@", c
->username
);
33 strbuf_addstr(out
, c
->host
);
35 strbuf_addf(out
, "/%s", c
->path
);
38 static char *credential_ask_one(const char *what
, struct credential
*c
)
40 struct strbuf desc
= STRBUF_INIT
;
41 struct strbuf prompt
= STRBUF_INIT
;
44 credential_describe(c
, &desc
);
46 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
.buf
);
48 strbuf_addf(&prompt
, "%s: ", what
);
50 /* FIXME: for usernames, we should do something less magical that
51 * actually echoes the characters. However, we need to read from
52 * /dev/tty and not stdio, which is not portable (but getpass will do
53 * it for us). http.c uses the same workaround. */
54 r
= git_getpass(prompt
.buf
);
56 strbuf_release(&desc
);
57 strbuf_release(&prompt
);
61 static void credential_getpass(struct credential
*c
)
64 c
->username
= credential_ask_one("Username", c
);
66 c
->password
= credential_ask_one("Password", c
);
69 int credential_read(struct credential
*c
, FILE *fp
)
71 struct strbuf line
= STRBUF_INIT
;
73 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
75 char *value
= strchr(key
, '=');
81 warning("invalid credential line: %s", key
);
82 strbuf_release(&line
);
87 if (!strcmp(key
, "username")) {
89 c
->username
= xstrdup(value
);
90 } else if (!strcmp(key
, "password")) {
92 c
->password
= xstrdup(value
);
93 } else if (!strcmp(key
, "protocol")) {
95 c
->protocol
= xstrdup(value
);
96 } else if (!strcmp(key
, "host")) {
98 c
->host
= xstrdup(value
);
99 } else if (!strcmp(key
, "path")) {
101 c
->path
= xstrdup(value
);
104 * Ignore other lines; we don't know what they mean, but
105 * this future-proofs us when later versions of git do
106 * learn new lines, and the helpers are updated to match.
110 strbuf_release(&line
);
114 static void credential_write_item(FILE *fp
, const char *key
, const char *value
)
118 fprintf(fp
, "%s=%s\n", key
, value
);
121 static void credential_write(const struct credential
*c
, FILE *fp
)
123 credential_write_item(fp
, "protocol", c
->protocol
);
124 credential_write_item(fp
, "host", c
->host
);
125 credential_write_item(fp
, "path", c
->path
);
126 credential_write_item(fp
, "username", c
->username
);
127 credential_write_item(fp
, "password", c
->password
);
130 static int run_credential_helper(struct credential
*c
,
134 struct child_process helper
;
135 const char *argv
[] = { NULL
, NULL
};
138 memset(&helper
, 0, sizeof(helper
));
141 helper
.use_shell
= 1;
146 helper
.no_stdout
= 1;
148 if (start_command(&helper
) < 0)
151 fp
= xfdopen(helper
.in
, "w");
152 credential_write(c
, fp
);
157 fp
= xfdopen(helper
.out
, "r");
158 r
= credential_read(c
, fp
);
161 finish_command(&helper
);
166 if (finish_command(&helper
))
171 static int credential_do(struct credential
*c
, const char *helper
,
172 const char *operation
)
174 struct strbuf cmd
= STRBUF_INIT
;
177 if (helper
[0] == '!')
178 strbuf_addstr(&cmd
, helper
+ 1);
179 else if (is_absolute_path(helper
))
180 strbuf_addstr(&cmd
, helper
);
182 strbuf_addf(&cmd
, "git credential-%s", helper
);
184 strbuf_addf(&cmd
, " %s", operation
);
185 r
= run_credential_helper(c
, cmd
.buf
, !strcmp(operation
, "get"));
187 strbuf_release(&cmd
);
191 void credential_fill(struct credential
*c
)
195 if (c
->username
&& c
->password
)
198 for (i
= 0; i
< c
->helpers
.nr
; i
++) {
199 credential_do(c
, c
->helpers
.items
[i
].string
, "get");
200 if (c
->username
&& c
->password
)
204 credential_getpass(c
);
205 if (!c
->username
&& !c
->password
)
206 die("unable to get password from user");
209 void credential_approve(struct credential
*c
)
215 if (!c
->username
|| !c
->password
)
218 for (i
= 0; i
< c
->helpers
.nr
; i
++)
219 credential_do(c
, c
->helpers
.items
[i
].string
, "store");
223 void credential_reject(struct credential
*c
)
227 for (i
= 0; i
< c
->helpers
.nr
; i
++)
228 credential_do(c
, c
->helpers
.items
[i
].string
, "erase");
237 void credential_from_url(struct credential
*c
, const char *url
)
239 const char *at
, *colon
, *cp
, *slash
, *host
, *proto_end
;
245 * (1) proto://<host>/...
246 * (2) proto://<user>@<host>/...
247 * (3) proto://<user>:<pass>@<host>/...
249 proto_end
= strstr(url
, "://");
253 at
= strchr(cp
, '@');
254 colon
= strchr(cp
, ':');
255 slash
= strchrnul(cp
, '/');
257 if (!at
|| slash
<= at
) {
261 else if (!colon
|| at
<= colon
) {
263 c
->username
= url_decode_mem(cp
, at
- cp
);
267 c
->username
= url_decode_mem(cp
, colon
- cp
);
268 c
->password
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
272 if (proto_end
- url
> 0)
273 c
->protocol
= xmemdupz(url
, proto_end
- url
);
274 if (slash
- host
> 0)
275 c
->host
= url_decode_mem(host
, slash
- host
);
276 /* Trim leading and trailing slashes from path */
277 while (*slash
== '/')
281 c
->path
= url_decode(slash
);
282 p
= c
->path
+ strlen(c
->path
) - 1;
283 while (p
> c
->path
&& *p
== '/')