5 #include "credential.h"
7 #include "string-list.h"
8 #include "parse-options.h"
9 #include "write-or-die.h"
11 static struct lock_file credential_lock
;
13 static int parse_credential_file(const char *fn
,
15 void (*match_cb
)(struct credential
*),
16 void (*other_cb
)(struct strbuf
*),
20 struct strbuf line
= STRBUF_INIT
;
21 struct credential entry
= CREDENTIAL_INIT
;
22 int found_credential
= 0;
26 if (errno
!= ENOENT
&& errno
!= EACCES
)
27 die_errno("unable to open %s", fn
);
28 return found_credential
;
31 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
32 if (!credential_from_url_gently(&entry
, line
.buf
, 1) &&
33 entry
.username
&& entry
.password
&&
34 credential_match(c
, &entry
, match_password
)) {
45 credential_clear(&entry
);
46 strbuf_release(&line
);
48 return found_credential
;
51 static void print_entry(struct credential
*c
)
53 printf("username=%s\n", c
->username
);
54 printf("password=%s\n", c
->password
);
57 static void print_line(struct strbuf
*buf
)
59 strbuf_addch(buf
, '\n');
60 write_or_die(get_lock_file_fd(&credential_lock
), buf
->buf
, buf
->len
);
63 static void rewrite_credential_file(const char *fn
, struct credential
*c
,
64 struct strbuf
*extra
, int match_password
)
66 int timeout_ms
= 1000;
68 git_config_get_int("credentialstore.locktimeoutms", &timeout_ms
);
69 if (hold_lock_file_for_update_timeout(&credential_lock
, fn
, 0, timeout_ms
) < 0)
70 die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms
);
73 parse_credential_file(fn
, c
, NULL
, print_line
, match_password
);
74 if (commit_lock_file(&credential_lock
) < 0)
75 die_errno("unable to write credential store");
78 static int is_rfc3986_unreserved(char ch
)
81 ch
== '-' || ch
== '_' || ch
== '.' || ch
== '~';
84 static int is_rfc3986_reserved_or_unreserved(char ch
)
86 if (is_rfc3986_unreserved(ch
))
89 case '!': case '*': case '\'': case '(': case ')': case ';':
90 case ':': case '@': case '&': case '=': case '+': case '$':
91 case ',': case '/': case '?': case '#': case '[': case ']':
97 static void store_credential_file(const char *fn
, struct credential
*c
)
99 struct strbuf buf
= STRBUF_INIT
;
101 strbuf_addf(&buf
, "%s://", c
->protocol
);
102 strbuf_addstr_urlencode(&buf
, c
->username
, is_rfc3986_unreserved
);
103 strbuf_addch(&buf
, ':');
104 strbuf_addstr_urlencode(&buf
, c
->password
, is_rfc3986_unreserved
);
105 strbuf_addch(&buf
, '@');
107 strbuf_addstr_urlencode(&buf
, c
->host
, is_rfc3986_unreserved
);
109 strbuf_addch(&buf
, '/');
110 strbuf_addstr_urlencode(&buf
, c
->path
,
111 is_rfc3986_reserved_or_unreserved
);
114 rewrite_credential_file(fn
, c
, &buf
, 0);
115 strbuf_release(&buf
);
118 static void store_credential(const struct string_list
*fns
, struct credential
*c
)
120 struct string_list_item
*fn
;
123 * Sanity check that what we are storing is actually sensible.
124 * In particular, we can't make a URL without a protocol field.
125 * Without either a host or pathname (depending on the scheme),
126 * we have no primary key. And without a username and password,
127 * we are not actually storing a credential.
129 if (!c
->protocol
|| !(c
->host
|| c
->path
) || !c
->username
|| !c
->password
)
132 for_each_string_list_item(fn
, fns
)
133 if (!access(fn
->string
, F_OK
)) {
134 store_credential_file(fn
->string
, c
);
138 * Write credential to the filename specified by fns->items[0], thus
142 store_credential_file(fns
->items
[0].string
, c
);
145 static void remove_credential(const struct string_list
*fns
, struct credential
*c
)
147 struct string_list_item
*fn
;
150 * Sanity check that we actually have something to match
151 * against. The input we get is a restrictive pattern,
152 * so technically a blank credential means "erase everything".
153 * But it is too easy to accidentally send this, since it is equivalent
154 * to empty input. So explicitly disallow it, and require that the
155 * pattern have some actual content to match.
157 if (!c
->protocol
&& !c
->host
&& !c
->path
&& !c
->username
)
159 for_each_string_list_item(fn
, fns
)
160 if (!access(fn
->string
, F_OK
))
161 rewrite_credential_file(fn
->string
, c
, NULL
, 1);
164 static void lookup_credential(const struct string_list
*fns
, struct credential
*c
)
166 struct string_list_item
*fn
;
168 for_each_string_list_item(fn
, fns
)
169 if (parse_credential_file(fn
->string
, c
, print_entry
, NULL
, 0))
170 return; /* Found credential */
173 int cmd_credential_store(int argc
, const char **argv
, const char *prefix
)
175 const char * const usage
[] = {
176 "git credential-store [<options>] <action>",
180 struct credential c
= CREDENTIAL_INIT
;
181 struct string_list fns
= STRING_LIST_INIT_DUP
;
183 struct option options
[] = {
184 OPT_STRING(0, "file", &file
, "path",
185 "fetch and store credentials in <path>"),
191 argc
= parse_options(argc
, (const char **)argv
, prefix
, options
, usage
, 0);
193 usage_with_options(usage
, options
);
197 string_list_append(&fns
, file
);
199 if ((file
= interpolate_path("~/.git-credentials", 0)))
200 string_list_append_nodup(&fns
, file
);
201 file
= xdg_config_home("credentials");
203 string_list_append_nodup(&fns
, file
);
206 die("unable to set up default path; use --file");
208 if (credential_read(&c
, stdin
, CREDENTIAL_OP_HELPER
) < 0)
209 die("unable to read credential");
211 if (!strcmp(op
, "get"))
212 lookup_credential(&fns
, &c
);
213 else if (!strcmp(op
, "erase"))
214 remove_credential(&fns
, &c
);
215 else if (!strcmp(op
, "store"))
216 store_credential(&fns
, &c
);
218 ; /* Ignore unknown operation. */
220 string_list_clear(&fns
, 0);
221 credential_clear(&c
);