Skip to content

Commit cb2c279

Browse files
pyokagangitster
authored andcommitted
git-credential-store: support multiple credential files
Previously, git-credential-store only supported storing credentials in a single file: ~/.git-credentials. In order to support the XDG base directory specification[1], git-credential-store needs to be able to lookup and erase credentials from multiple files, as well as to pick the appropriate file to write to so that the credentials can be found on subsequent lookups. [1] http://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html Note that some credential storage files may not be owned, readable or writable by the user, as they may be system-wide files that are meant to apply to every user. Instead of a single file path, lookup_credential(), remove_credential() and store_credential() now take a precedence-ordered string_list of file paths. lookup_credential() expects both user-specific and system-wide credential files to be provided to support the use case of system administrators setting default credentials for users. remove_credential() and store_credential() expect only the user-specific credential files to be provided as usually the only config files that users are allowed to edit are their own user-specific ones. lookup_credential() will read these (user-specific and system-wide) file paths in order until it finds the 1st matching credential and print it. As some files may be private and thus unreadable, any file which cannot be read will be ignored silently. remove_credential() will erase credentials from all (user-specific) files in the list. This is because if credentials are only erased from the file with the highest precedence, a matching credential may still be found in a file further down the list. (Note that due to the lockfile code, this requires the directory to be writable, which should be so for user-specific config files) store_credential() will write the credentials to the first existing (user-specific) file in the list. If none of the files in the list exist, store_credential() will write to the filename specified by the first item of the filename list. For backwards compatibility, this filename should be "~/.git-credentials". Helped-by: Matthieu Moy <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Paul Tan <[email protected]> Reviewed-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9874fca commit cb2c279

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

credential-store.c

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,28 @@
66

77
static struct lock_file credential_lock;
88

9-
static void parse_credential_file(const char *fn,
9+
static int parse_credential_file(const char *fn,
1010
struct credential *c,
1111
void (*match_cb)(struct credential *),
1212
void (*other_cb)(struct strbuf *))
1313
{
1414
FILE *fh;
1515
struct strbuf line = STRBUF_INIT;
1616
struct credential entry = CREDENTIAL_INIT;
17+
int found_credential = 0;
1718

1819
fh = fopen(fn, "r");
1920
if (!fh) {
20-
if (errno != ENOENT)
21+
if (errno != ENOENT && errno != EACCES)
2122
die_errno("unable to open %s", fn);
22-
return;
23+
return found_credential;
2324
}
2425

2526
while (strbuf_getline(&line, fh, '\n') != EOF) {
2627
credential_from_url(&entry, line.buf);
2728
if (entry.username && entry.password &&
2829
credential_match(c, &entry)) {
30+
found_credential = 1;
2931
if (match_cb) {
3032
match_cb(&entry);
3133
break;
@@ -38,6 +40,7 @@ static void parse_credential_file(const char *fn,
3840
credential_clear(&entry);
3941
strbuf_release(&line);
4042
fclose(fh);
43+
return found_credential;
4144
}
4245

4346
static void print_entry(struct credential *c)
@@ -64,21 +67,10 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
6467
die_errno("unable to commit credential store");
6568
}
6669

67-
static void store_credential(const char *fn, struct credential *c)
70+
static void store_credential_file(const char *fn, struct credential *c)
6871
{
6972
struct strbuf buf = STRBUF_INIT;
7073

71-
/*
72-
* Sanity check that what we are storing is actually sensible.
73-
* In particular, we can't make a URL without a protocol field.
74-
* Without either a host or pathname (depending on the scheme),
75-
* we have no primary key. And without a username and password,
76-
* we are not actually storing a credential.
77-
*/
78-
if (!c->protocol || !(c->host || c->path) ||
79-
!c->username || !c->password)
80-
return;
81-
8274
strbuf_addf(&buf, "%s://", c->protocol);
8375
strbuf_addstr_urlencode(&buf, c->username, 1);
8476
strbuf_addch(&buf, ':');
@@ -95,8 +87,37 @@ static void store_credential(const char *fn, struct credential *c)
9587
strbuf_release(&buf);
9688
}
9789

98-
static void remove_credential(const char *fn, struct credential *c)
90+
static void store_credential(const struct string_list *fns, struct credential *c)
9991
{
92+
struct string_list_item *fn;
93+
94+
/*
95+
* Sanity check that what we are storing is actually sensible.
96+
* In particular, we can't make a URL without a protocol field.
97+
* Without either a host or pathname (depending on the scheme),
98+
* we have no primary key. And without a username and password,
99+
* we are not actually storing a credential.
100+
*/
101+
if (!c->protocol || !(c->host || c->path) || !c->username || !c->password)
102+
return;
103+
104+
for_each_string_list_item(fn, fns)
105+
if (!access(fn->string, F_OK)) {
106+
store_credential_file(fn->string, c);
107+
return;
108+
}
109+
/*
110+
* Write credential to the filename specified by fns->items[0], thus
111+
* creating it
112+
*/
113+
if (fns->nr)
114+
store_credential_file(fns->items[0].string, c);
115+
}
116+
117+
static void remove_credential(const struct string_list *fns, struct credential *c)
118+
{
119+
struct string_list_item *fn;
120+
100121
/*
101122
* Sanity check that we actually have something to match
102123
* against. The input we get is a restrictive pattern,
@@ -105,14 +126,20 @@ static void remove_credential(const char *fn, struct credential *c)
105126
* to empty input. So explicitly disallow it, and require that the
106127
* pattern have some actual content to match.
107128
*/
108-
if (c->protocol || c->host || c->path || c->username)
109-
rewrite_credential_file(fn, c, NULL);
129+
if (!c->protocol && !c->host && !c->path && !c->username)
130+
return;
131+
for_each_string_list_item(fn, fns)
132+
if (!access(fn->string, F_OK))
133+
rewrite_credential_file(fn->string, c, NULL);
110134
}
111135

112-
static int lookup_credential(const char *fn, struct credential *c)
136+
static void lookup_credential(const struct string_list *fns, struct credential *c)
113137
{
114-
parse_credential_file(fn, c, print_entry, NULL);
115-
return c->username && c->password;
138+
struct string_list_item *fn;
139+
140+
for_each_string_list_item(fn, fns)
141+
if (parse_credential_file(fn->string, c, print_entry, NULL))
142+
return; /* Found credential */
116143
}
117144

118145
int main(int argc, char **argv)
@@ -123,6 +150,7 @@ int main(int argc, char **argv)
123150
};
124151
const char *op;
125152
struct credential c = CREDENTIAL_INIT;
153+
struct string_list fns = STRING_LIST_INIT_DUP;
126154
char *file = NULL;
127155
struct option options[] = {
128156
OPT_STRING(0, "file", &file, "path",
@@ -139,20 +167,23 @@ int main(int argc, char **argv)
139167

140168
if (!file)
141169
file = expand_user_path("~/.git-credentials");
142-
if (!file)
170+
if (file)
171+
string_list_append(&fns, file);
172+
else
143173
die("unable to set up default path; use --file");
144174

145175
if (credential_read(&c, stdin) < 0)
146176
die("unable to read credential");
147177

148178
if (!strcmp(op, "get"))
149-
lookup_credential(file, &c);
179+
lookup_credential(&fns, &c);
150180
else if (!strcmp(op, "erase"))
151-
remove_credential(file, &c);
181+
remove_credential(&fns, &c);
152182
else if (!strcmp(op, "store"))
153-
store_credential(file, &c);
183+
store_credential(&fns, &c);
154184
else
155185
; /* Ignore unknown operation. */
156186

187+
string_list_clear(&fns, 0);
157188
return 0;
158189
}

0 commit comments

Comments
 (0)