surround %s with quotes when failed to lookup commit
[git/debian.git] / builtin / credential-store.c
blob30c6ccf56c05df3ae6e14497a5c6b719f933fecb
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 *))
18 FILE *fh;
19 struct strbuf line = STRBUF_INIT;
20 struct credential entry = CREDENTIAL_INIT;
21 int found_credential = 0;
23 fh = fopen(fn, "r");
24 if (!fh) {
25 if (errno != ENOENT && errno != EACCES)
26 die_errno("unable to open %s", fn);
27 return found_credential;
30 while (strbuf_getline_lf(&line, fh) != EOF) {
31 if (!credential_from_url_gently(&entry, line.buf, 1) &&
32 entry.username && entry.password &&
33 credential_match(c, &entry)) {
34 found_credential = 1;
35 if (match_cb) {
36 match_cb(&entry);
37 break;
40 else if (other_cb)
41 other_cb(&line);
44 credential_clear(&entry);
45 strbuf_release(&line);
46 fclose(fh);
47 return found_credential;
50 static void print_entry(struct credential *c)
52 printf("username=%s\n", c->username);
53 printf("password=%s\n", c->password);
56 static void print_line(struct strbuf *buf)
58 strbuf_addch(buf, '\n');
59 write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len);
62 static void rewrite_credential_file(const char *fn, struct credential *c,
63 struct strbuf *extra)
65 int timeout_ms = 1000;
67 git_config_get_int("credentialstore.locktimeoutms", &timeout_ms);
68 if (hold_lock_file_for_update_timeout(&credential_lock, fn, 0, timeout_ms) < 0)
69 die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms);
70 if (extra)
71 print_line(extra);
72 parse_credential_file(fn, c, NULL, print_line);
73 if (commit_lock_file(&credential_lock) < 0)
74 die_errno("unable to write credential store");
77 static void store_credential_file(const char *fn, struct credential *c)
79 struct strbuf buf = STRBUF_INIT;
81 strbuf_addf(&buf, "%s://", c->protocol);
82 strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
83 strbuf_addch(&buf, ':');
84 strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
85 strbuf_addch(&buf, '@');
86 if (c->host)
87 strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
88 if (c->path) {
89 strbuf_addch(&buf, '/');
90 strbuf_addstr_urlencode(&buf, c->path,
91 is_rfc3986_reserved_or_unreserved);
94 rewrite_credential_file(fn, c, &buf);
95 strbuf_release(&buf);
98 static void store_credential(const struct string_list *fns, struct credential *c)
100 struct string_list_item *fn;
103 * Sanity check that what we are storing is actually sensible.
104 * In particular, we can't make a URL without a protocol field.
105 * Without either a host or pathname (depending on the scheme),
106 * we have no primary key. And without a username and password,
107 * we are not actually storing a credential.
109 if (!c->protocol || !(c->host || c->path) || !c->username || !c->password)
110 return;
112 for_each_string_list_item(fn, fns)
113 if (!access(fn->string, F_OK)) {
114 store_credential_file(fn->string, c);
115 return;
118 * Write credential to the filename specified by fns->items[0], thus
119 * creating it
121 if (fns->nr)
122 store_credential_file(fns->items[0].string, c);
125 static void remove_credential(const struct string_list *fns, struct credential *c)
127 struct string_list_item *fn;
130 * Sanity check that we actually have something to match
131 * against. The input we get is a restrictive pattern,
132 * so technically a blank credential means "erase everything".
133 * But it is too easy to accidentally send this, since it is equivalent
134 * to empty input. So explicitly disallow it, and require that the
135 * pattern have some actual content to match.
137 if (!c->protocol && !c->host && !c->path && !c->username)
138 return;
139 for_each_string_list_item(fn, fns)
140 if (!access(fn->string, F_OK))
141 rewrite_credential_file(fn->string, c, NULL);
144 static void lookup_credential(const struct string_list *fns, struct credential *c)
146 struct string_list_item *fn;
148 for_each_string_list_item(fn, fns)
149 if (parse_credential_file(fn->string, c, print_entry, NULL))
150 return; /* Found credential */
153 int cmd_credential_store(int argc, const char **argv, const char *prefix)
155 const char * const usage[] = {
156 "git credential-store [<options>] <action>",
157 NULL
159 const char *op;
160 struct credential c = CREDENTIAL_INIT;
161 struct string_list fns = STRING_LIST_INIT_DUP;
162 char *file = NULL;
163 struct option options[] = {
164 OPT_STRING(0, "file", &file, "path",
165 "fetch and store credentials in <path>"),
166 OPT_END()
169 umask(077);
171 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
172 if (argc != 1)
173 usage_with_options(usage, options);
174 op = argv[0];
176 if (file) {
177 string_list_append(&fns, file);
178 } else {
179 if ((file = interpolate_path("~/.git-credentials", 0)))
180 string_list_append_nodup(&fns, file);
181 file = xdg_config_home("credentials");
182 if (file)
183 string_list_append_nodup(&fns, file);
185 if (!fns.nr)
186 die("unable to set up default path; use --file");
188 if (credential_read(&c, stdin) < 0)
189 die("unable to read credential");
191 if (!strcmp(op, "get"))
192 lookup_credential(&fns, &c);
193 else if (!strcmp(op, "erase"))
194 remove_credential(&fns, &c);
195 else if (!strcmp(op, "store"))
196 store_credential(&fns, &c);
197 else
198 ; /* Ignore unknown operation. */
200 string_list_clear(&fns, 0);
201 return 0;