diff --no-index: fix -R with stdin
[git/debian.git] / builtin / credential-store.c
blob0937230bced2cbeaaafc92e93f5847a64d0e7db6
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 void store_credential_file(const char *fn, struct credential *c)
80 struct strbuf buf = STRBUF_INIT;
82 strbuf_addf(&buf, "%s://", c->protocol);
83 strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
84 strbuf_addch(&buf, ':');
85 strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
86 strbuf_addch(&buf, '@');
87 if (c->host)
88 strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
89 if (c->path) {
90 strbuf_addch(&buf, '/');
91 strbuf_addstr_urlencode(&buf, c->path,
92 is_rfc3986_reserved_or_unreserved);
95 rewrite_credential_file(fn, c, &buf, 0);
96 strbuf_release(&buf);
99 static void store_credential(const struct string_list *fns, struct credential *c)
101 struct string_list_item *fn;
104 * Sanity check that what we are storing is actually sensible.
105 * In particular, we can't make a URL without a protocol field.
106 * Without either a host or pathname (depending on the scheme),
107 * we have no primary key. And without a username and password,
108 * we are not actually storing a credential.
110 if (!c->protocol || !(c->host || c->path) || !c->username || !c->password)
111 return;
113 for_each_string_list_item(fn, fns)
114 if (!access(fn->string, F_OK)) {
115 store_credential_file(fn->string, c);
116 return;
119 * Write credential to the filename specified by fns->items[0], thus
120 * creating it
122 if (fns->nr)
123 store_credential_file(fns->items[0].string, c);
126 static void remove_credential(const struct string_list *fns, struct credential *c)
128 struct string_list_item *fn;
131 * Sanity check that we actually have something to match
132 * against. The input we get is a restrictive pattern,
133 * so technically a blank credential means "erase everything".
134 * But it is too easy to accidentally send this, since it is equivalent
135 * to empty input. So explicitly disallow it, and require that the
136 * pattern have some actual content to match.
138 if (!c->protocol && !c->host && !c->path && !c->username)
139 return;
140 for_each_string_list_item(fn, fns)
141 if (!access(fn->string, F_OK))
142 rewrite_credential_file(fn->string, c, NULL, 1);
145 static void lookup_credential(const struct string_list *fns, struct credential *c)
147 struct string_list_item *fn;
149 for_each_string_list_item(fn, fns)
150 if (parse_credential_file(fn->string, c, print_entry, NULL, 0))
151 return; /* Found credential */
154 int cmd_credential_store(int argc, const char **argv, const char *prefix)
156 const char * const usage[] = {
157 "git credential-store [<options>] <action>",
158 NULL
160 const char *op;
161 struct credential c = CREDENTIAL_INIT;
162 struct string_list fns = STRING_LIST_INIT_DUP;
163 char *file = NULL;
164 struct option options[] = {
165 OPT_STRING(0, "file", &file, "path",
166 "fetch and store credentials in <path>"),
167 OPT_END()
170 umask(077);
172 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
173 if (argc != 1)
174 usage_with_options(usage, options);
175 op = argv[0];
177 if (file) {
178 string_list_append(&fns, file);
179 } else {
180 if ((file = interpolate_path("~/.git-credentials", 0)))
181 string_list_append_nodup(&fns, file);
182 file = xdg_config_home("credentials");
183 if (file)
184 string_list_append_nodup(&fns, file);
186 if (!fns.nr)
187 die("unable to set up default path; use --file");
189 if (credential_read(&c, stdin) < 0)
190 die("unable to read credential");
192 if (!strcmp(op, "get"))
193 lookup_credential(&fns, &c);
194 else if (!strcmp(op, "erase"))
195 remove_credential(&fns, &c);
196 else if (!strcmp(op, "store"))
197 store_credential(&fns, &c);
198 else
199 ; /* Ignore unknown operation. */
201 string_list_clear(&fns, 0);
202 return 0;