reftable/stack: fix stale lock when dying
[git.git] / builtin / credential-store.c
blob4a492411bbf3d2a7f0a34ada0bd127ae97cf5552
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "lockfile.h"
5 #include "credential.h"
6 #include "path.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,
14 struct credential *c,
15 void (*match_cb)(struct credential *),
16 void (*other_cb)(struct strbuf *),
17 int match_password)
19 FILE *fh;
20 struct strbuf line = STRBUF_INIT;
21 struct credential entry = CREDENTIAL_INIT;
22 int found_credential = 0;
24 fh = fopen(fn, "r");
25 if (!fh) {
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)) {
35 found_credential = 1;
36 if (match_cb) {
37 match_cb(&entry);
38 break;
41 else if (other_cb)
42 other_cb(&line);
45 credential_clear(&entry);
46 strbuf_release(&line);
47 fclose(fh);
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);
71 if (extra)
72 print_line(extra);
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)
80 return isalnum(ch) ||
81 ch == '-' || ch == '_' || ch == '.' || ch == '~';
84 static int is_rfc3986_reserved_or_unreserved(char ch)
86 if (is_rfc3986_unreserved(ch))
87 return 1;
88 switch (ch) {
89 case '!': case '*': case '\'': case '(': case ')': case ';':
90 case ':': case '@': case '&': case '=': case '+': case '$':
91 case ',': case '/': case '?': case '#': case '[': case ']':
92 return 1;
94 return 0;
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, '@');
106 if (c->host)
107 strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
108 if (c->path) {
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)
130 return;
132 for_each_string_list_item(fn, fns)
133 if (!access(fn->string, F_OK)) {
134 store_credential_file(fn->string, c);
135 return;
138 * Write credential to the filename specified by fns->items[0], thus
139 * creating it
141 if (fns->nr)
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)
158 return;
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>",
177 NULL
179 const char *op;
180 struct credential c = CREDENTIAL_INIT;
181 struct string_list fns = STRING_LIST_INIT_DUP;
182 char *file = NULL;
183 struct option options[] = {
184 OPT_STRING(0, "file", &file, "path",
185 "fetch and store credentials in <path>"),
186 OPT_END()
189 umask(077);
191 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
192 if (argc != 1)
193 usage_with_options(usage, options);
194 op = argv[0];
196 if (file) {
197 string_list_append(&fns, file);
198 } else {
199 if ((file = interpolate_path("~/.git-credentials", 0)))
200 string_list_append_nodup(&fns, file);
201 file = xdg_config_home("credentials");
202 if (file)
203 string_list_append_nodup(&fns, file);
205 if (!fns.nr)
206 die("unable to set up default path; use --file");
208 if (credential_read(&c, stdin) < 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);
217 else
218 ; /* Ignore unknown operation. */
220 string_list_clear(&fns, 0);
221 return 0;