notes.c: use designated initializers for clarity
[git/debian.git] / builtin / credential-store.c
blob8977604eb9dc0b206474282e6e34afe36aeeb35f
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "lockfile.h"
5 #include "credential.h"
6 #include "string-list.h"
7 #include "parse-options.h"
8 #include "write-or-die.h"
10 static struct lock_file credential_lock;
12 static int parse_credential_file(const char *fn,
13 struct credential *c,
14 void (*match_cb)(struct credential *),
15 void (*other_cb)(struct strbuf *))
17 FILE *fh;
18 struct strbuf line = STRBUF_INIT;
19 struct credential entry = CREDENTIAL_INIT;
20 int found_credential = 0;
22 fh = fopen(fn, "r");
23 if (!fh) {
24 if (errno != ENOENT && errno != EACCES)
25 die_errno("unable to open %s", fn);
26 return found_credential;
29 while (strbuf_getline_lf(&line, fh) != EOF) {
30 if (!credential_from_url_gently(&entry, line.buf, 1) &&
31 entry.username && entry.password &&
32 credential_match(c, &entry)) {
33 found_credential = 1;
34 if (match_cb) {
35 match_cb(&entry);
36 break;
39 else if (other_cb)
40 other_cb(&line);
43 credential_clear(&entry);
44 strbuf_release(&line);
45 fclose(fh);
46 return found_credential;
49 static void print_entry(struct credential *c)
51 printf("username=%s\n", c->username);
52 printf("password=%s\n", c->password);
55 static void print_line(struct strbuf *buf)
57 strbuf_addch(buf, '\n');
58 write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len);
61 static void rewrite_credential_file(const char *fn, struct credential *c,
62 struct strbuf *extra)
64 int timeout_ms = 1000;
66 git_config_get_int("credentialstore.locktimeoutms", &timeout_ms);
67 if (hold_lock_file_for_update_timeout(&credential_lock, fn, 0, timeout_ms) < 0)
68 die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms);
69 if (extra)
70 print_line(extra);
71 parse_credential_file(fn, c, NULL, print_line);
72 if (commit_lock_file(&credential_lock) < 0)
73 die_errno("unable to write credential store");
76 static void store_credential_file(const char *fn, struct credential *c)
78 struct strbuf buf = STRBUF_INIT;
80 strbuf_addf(&buf, "%s://", c->protocol);
81 strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
82 strbuf_addch(&buf, ':');
83 strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
84 strbuf_addch(&buf, '@');
85 if (c->host)
86 strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
87 if (c->path) {
88 strbuf_addch(&buf, '/');
89 strbuf_addstr_urlencode(&buf, c->path,
90 is_rfc3986_reserved_or_unreserved);
93 rewrite_credential_file(fn, c, &buf);
94 strbuf_release(&buf);
97 static void store_credential(const struct string_list *fns, struct credential *c)
99 struct string_list_item *fn;
102 * Sanity check that what we are storing is actually sensible.
103 * In particular, we can't make a URL without a protocol field.
104 * Without either a host or pathname (depending on the scheme),
105 * we have no primary key. And without a username and password,
106 * we are not actually storing a credential.
108 if (!c->protocol || !(c->host || c->path) || !c->username || !c->password)
109 return;
111 for_each_string_list_item(fn, fns)
112 if (!access(fn->string, F_OK)) {
113 store_credential_file(fn->string, c);
114 return;
117 * Write credential to the filename specified by fns->items[0], thus
118 * creating it
120 if (fns->nr)
121 store_credential_file(fns->items[0].string, c);
124 static void remove_credential(const struct string_list *fns, struct credential *c)
126 struct string_list_item *fn;
129 * Sanity check that we actually have something to match
130 * against. The input we get is a restrictive pattern,
131 * so technically a blank credential means "erase everything".
132 * But it is too easy to accidentally send this, since it is equivalent
133 * to empty input. So explicitly disallow it, and require that the
134 * pattern have some actual content to match.
136 if (!c->protocol && !c->host && !c->path && !c->username)
137 return;
138 for_each_string_list_item(fn, fns)
139 if (!access(fn->string, F_OK))
140 rewrite_credential_file(fn->string, c, NULL);
143 static void lookup_credential(const struct string_list *fns, struct credential *c)
145 struct string_list_item *fn;
147 for_each_string_list_item(fn, fns)
148 if (parse_credential_file(fn->string, c, print_entry, NULL))
149 return; /* Found credential */
152 int cmd_credential_store(int argc, const char **argv, const char *prefix)
154 const char * const usage[] = {
155 "git credential-store [<options>] <action>",
156 NULL
158 const char *op;
159 struct credential c = CREDENTIAL_INIT;
160 struct string_list fns = STRING_LIST_INIT_DUP;
161 char *file = NULL;
162 struct option options[] = {
163 OPT_STRING(0, "file", &file, "path",
164 "fetch and store credentials in <path>"),
165 OPT_END()
168 umask(077);
170 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
171 if (argc != 1)
172 usage_with_options(usage, options);
173 op = argv[0];
175 if (file) {
176 string_list_append(&fns, file);
177 } else {
178 if ((file = interpolate_path("~/.git-credentials", 0)))
179 string_list_append_nodup(&fns, file);
180 file = xdg_config_home("credentials");
181 if (file)
182 string_list_append_nodup(&fns, file);
184 if (!fns.nr)
185 die("unable to set up default path; use --file");
187 if (credential_read(&c, stdin) < 0)
188 die("unable to read credential");
190 if (!strcmp(op, "get"))
191 lookup_credential(&fns, &c);
192 else if (!strcmp(op, "erase"))
193 remove_credential(&fns, &c);
194 else if (!strcmp(op, "store"))
195 store_credential(&fns, &c);
196 else
197 ; /* Ignore unknown operation. */
199 string_list_clear(&fns, 0);
200 return 0;