]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/credential-store.c
Merge branch 'ab/pager-exit-log'
[thirdparty/git.git] / builtin / credential-store.c
CommitLineData
b5dd96b7 1#include "builtin.h"
df7f915f 2#include "config.h"
697cc8ef 3#include "lockfile.h"
71e1b4b6
JK
4#include "credential.h"
5#include "string-list.h"
6#include "parse-options.h"
7
8static struct lock_file credential_lock;
9
cb2c2796 10static int parse_credential_file(const char *fn,
71e1b4b6
JK
11 struct credential *c,
12 void (*match_cb)(struct credential *),
13 void (*other_cb)(struct strbuf *))
14{
15 FILE *fh;
16 struct strbuf line = STRBUF_INIT;
17 struct credential entry = CREDENTIAL_INIT;
cb2c2796 18 int found_credential = 0;
71e1b4b6
JK
19
20 fh = fopen(fn, "r");
21 if (!fh) {
cb2c2796 22 if (errno != ENOENT && errno != EACCES)
71e1b4b6 23 die_errno("unable to open %s", fn);
cb2c2796 24 return found_credential;
71e1b4b6
JK
25 }
26
8f309aeb 27 while (strbuf_getline_lf(&line, fh) != EOF) {
c03859a6
CMAB
28 if (!credential_from_url_gently(&entry, line.buf, 1) &&
29 entry.username && entry.password &&
71e1b4b6 30 credential_match(c, &entry)) {
cb2c2796 31 found_credential = 1;
71e1b4b6
JK
32 if (match_cb) {
33 match_cb(&entry);
34 break;
35 }
36 }
37 else if (other_cb)
38 other_cb(&line);
39 }
40
41 credential_clear(&entry);
42 strbuf_release(&line);
43 fclose(fh);
cb2c2796 44 return found_credential;
71e1b4b6
JK
45}
46
47static void print_entry(struct credential *c)
48{
49 printf("username=%s\n", c->username);
50 printf("password=%s\n", c->password);
51}
52
53static void print_line(struct strbuf *buf)
54{
55 strbuf_addch(buf, '\n');
c99a4c2d 56 write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len);
71e1b4b6
JK
57}
58
59static void rewrite_credential_file(const char *fn, struct credential *c,
60 struct strbuf *extra)
61{
df7f915f
SA
62 int timeout_ms = 1000;
63
64 git_config_get_int("credentialstore.locktimeoutms", &timeout_ms);
65 if (hold_lock_file_for_update_timeout(&credential_lock, fn, 0, timeout_ms) < 0)
66 die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms);
71e1b4b6
JK
67 if (extra)
68 print_line(extra);
69 parse_credential_file(fn, c, NULL, print_line);
70 if (commit_lock_file(&credential_lock) < 0)
87d01c85 71 die_errno("unable to write credential store");
71e1b4b6
JK
72}
73
cb2c2796 74static void store_credential_file(const char *fn, struct credential *c)
71e1b4b6
JK
75{
76 struct strbuf buf = STRBUF_INIT;
77
71e1b4b6 78 strbuf_addf(&buf, "%s://", c->protocol);
c2694952 79 strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
71e1b4b6 80 strbuf_addch(&buf, ':');
c2694952 81 strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
71e1b4b6
JK
82 strbuf_addch(&buf, '@');
83 if (c->host)
c2694952 84 strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
71e1b4b6
JK
85 if (c->path) {
86 strbuf_addch(&buf, '/');
c2694952
MD
87 strbuf_addstr_urlencode(&buf, c->path,
88 is_rfc3986_reserved_or_unreserved);
71e1b4b6
JK
89 }
90
91 rewrite_credential_file(fn, c, &buf);
92 strbuf_release(&buf);
93}
94
cb2c2796 95static void store_credential(const struct string_list *fns, struct credential *c)
71e1b4b6 96{
cb2c2796
PT
97 struct string_list_item *fn;
98
99 /*
100 * Sanity check that what we are storing is actually sensible.
101 * In particular, we can't make a URL without a protocol field.
102 * Without either a host or pathname (depending on the scheme),
103 * we have no primary key. And without a username and password,
104 * we are not actually storing a credential.
105 */
106 if (!c->protocol || !(c->host || c->path) || !c->username || !c->password)
107 return;
108
109 for_each_string_list_item(fn, fns)
110 if (!access(fn->string, F_OK)) {
111 store_credential_file(fn->string, c);
112 return;
113 }
114 /*
115 * Write credential to the filename specified by fns->items[0], thus
116 * creating it
117 */
118 if (fns->nr)
119 store_credential_file(fns->items[0].string, c);
120}
121
122static void remove_credential(const struct string_list *fns, struct credential *c)
71e1b4b6 123{
cb2c2796
PT
124 struct string_list_item *fn;
125
71e1b4b6
JK
126 /*
127 * Sanity check that we actually have something to match
128 * against. The input we get is a restrictive pattern,
129 * so technically a blank credential means "erase everything".
130 * But it is too easy to accidentally send this, since it is equivalent
131 * to empty input. So explicitly disallow it, and require that the
132 * pattern have some actual content to match.
133 */
cb2c2796
PT
134 if (!c->protocol && !c->host && !c->path && !c->username)
135 return;
136 for_each_string_list_item(fn, fns)
137 if (!access(fn->string, F_OK))
138 rewrite_credential_file(fn->string, c, NULL);
71e1b4b6
JK
139}
140
cb2c2796 141static void lookup_credential(const struct string_list *fns, struct credential *c)
71e1b4b6 142{
cb2c2796
PT
143 struct string_list_item *fn;
144
145 for_each_string_list_item(fn, fns)
146 if (parse_credential_file(fn->string, c, print_entry, NULL))
147 return; /* Found credential */
71e1b4b6
JK
148}
149
b5dd96b7 150int cmd_credential_store(int argc, const char **argv, const char *prefix)
71e1b4b6
JK
151{
152 const char * const usage[] = {
9c9b4f2f 153 "git credential-store [<options>] <action>",
71e1b4b6
JK
154 NULL
155 };
156 const char *op;
157 struct credential c = CREDENTIAL_INIT;
cb2c2796 158 struct string_list fns = STRING_LIST_INIT_DUP;
71e1b4b6
JK
159 char *file = NULL;
160 struct option options[] = {
161 OPT_STRING(0, "file", &file, "path",
162 "fetch and store credentials in <path>"),
163 OPT_END()
164 };
165
166 umask(077);
167
b5dd96b7 168 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
71e1b4b6
JK
169 if (argc != 1)
170 usage_with_options(usage, options);
171 op = argv[0];
172
44b22898 173 if (file) {
cb2c2796 174 string_list_append(&fns, file);
44b22898 175 } else {
4aad2f16 176 if ((file = expand_user_path("~/.git-credentials", 0)))
44b22898 177 string_list_append_nodup(&fns, file);
64ab71db 178 file = xdg_config_home("credentials");
44b22898
PT
179 if (file)
180 string_list_append_nodup(&fns, file);
181 }
182 if (!fns.nr)
71e1b4b6
JK
183 die("unable to set up default path; use --file");
184
185 if (credential_read(&c, stdin) < 0)
186 die("unable to read credential");
187
188 if (!strcmp(op, "get"))
cb2c2796 189 lookup_credential(&fns, &c);
71e1b4b6 190 else if (!strcmp(op, "erase"))
cb2c2796 191 remove_credential(&fns, &c);
71e1b4b6 192 else if (!strcmp(op, "store"))
cb2c2796 193 store_credential(&fns, &c);
71e1b4b6
JK
194 else
195 ; /* Ignore unknown operation. */
196
cb2c2796 197 string_list_clear(&fns, 0);
71e1b4b6
JK
198 return 0;
199}