t5510: use `main` as initial branch name
[git] / builtin / credential-store.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "lockfile.h"
4 #include "credential.h"
5 #include "string-list.h"
6 #include "parse-options.h"
7
8 static struct lock_file credential_lock;
9
10 static int parse_credential_file(const char *fn,
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;
18         int found_credential = 0;
19
20         fh = fopen(fn, "r");
21         if (!fh) {
22                 if (errno != ENOENT && errno != EACCES)
23                         die_errno("unable to open %s", fn);
24                 return found_credential;
25         }
26
27         while (strbuf_getline_lf(&line, fh) != EOF) {
28                 if (!credential_from_url_gently(&entry, line.buf, 1) &&
29                     entry.username && entry.password &&
30                     credential_match(c, &entry)) {
31                         found_credential = 1;
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);
44         return found_credential;
45 }
46
47 static void print_entry(struct credential *c)
48 {
49         printf("username=%s\n", c->username);
50         printf("password=%s\n", c->password);
51 }
52
53 static void print_line(struct strbuf *buf)
54 {
55         strbuf_addch(buf, '\n');
56         write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len);
57 }
58
59 static void rewrite_credential_file(const char *fn, struct credential *c,
60                                     struct strbuf *extra)
61 {
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);
67         if (extra)
68                 print_line(extra);
69         parse_credential_file(fn, c, NULL, print_line);
70         if (commit_lock_file(&credential_lock) < 0)
71                 die_errno("unable to write credential store");
72 }
73
74 static void store_credential_file(const char *fn, struct credential *c)
75 {
76         struct strbuf buf = STRBUF_INIT;
77
78         strbuf_addf(&buf, "%s://", c->protocol);
79         strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved);
80         strbuf_addch(&buf, ':');
81         strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved);
82         strbuf_addch(&buf, '@');
83         if (c->host)
84                 strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved);
85         if (c->path) {
86                 strbuf_addch(&buf, '/');
87                 strbuf_addstr_urlencode(&buf, c->path,
88                                         is_rfc3986_reserved_or_unreserved);
89         }
90
91         rewrite_credential_file(fn, c, &buf);
92         strbuf_release(&buf);
93 }
94
95 static void store_credential(const struct string_list *fns, struct credential *c)
96 {
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
122 static void remove_credential(const struct string_list *fns, struct credential *c)
123 {
124         struct string_list_item *fn;
125
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          */
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);
139 }
140
141 static void lookup_credential(const struct string_list *fns, struct credential *c)
142 {
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 */
148 }
149
150 int cmd_credential_store(int argc, const char **argv, const char *prefix)
151 {
152         const char * const usage[] = {
153                 "git credential-store [<options>] <action>",
154                 NULL
155         };
156         const char *op;
157         struct credential c = CREDENTIAL_INIT;
158         struct string_list fns = STRING_LIST_INIT_DUP;
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
168         argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
169         if (argc != 1)
170                 usage_with_options(usage, options);
171         op = argv[0];
172
173         if (file) {
174                 string_list_append(&fns, file);
175         } else {
176                 if ((file = expand_user_path("~/.git-credentials", 0)))
177                         string_list_append_nodup(&fns, file);
178                 file = xdg_config_home("credentials");
179                 if (file)
180                         string_list_append_nodup(&fns, file);
181         }
182         if (!fns.nr)
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"))
189                 lookup_credential(&fns, &c);
190         else if (!strcmp(op, "erase"))
191                 remove_credential(&fns, &c);
192         else if (!strcmp(op, "store"))
193                 store_credential(&fns, &c);
194         else
195                 ; /* Ignore unknown operation. */
196
197         string_list_clear(&fns, 0);
198         return 0;
199 }