blob: ec1271f89ce08b3b117fe09715f61730c7943bea [file] [log] [blame]
Jeff Kinge2770972011-12-10 05:34:14 -05001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Michael Haggerty9e903312015-08-10 11:47:51 +02003#include "tempfile.h"
Jeff Kinge2770972011-12-10 05:34:14 -05004#include "credential.h"
5#include "unix-socket.h"
Jeff Kingf5e3c0b2014-09-14 03:35:06 -04006#include "parse-options.h"
Jeff Kinge2770972011-12-10 05:34:14 -05007
Jeff Kinge2770972011-12-10 05:34:14 -05008struct credential_cache_entry {
9 struct credential item;
Johannes Schindelindddbad72017-04-26 21:29:31 +020010 timestamp_t expiration;
Jeff Kinge2770972011-12-10 05:34:14 -050011};
12static struct credential_cache_entry *entries;
13static int entries_nr;
14static int entries_alloc;
15
16static void cache_credential(struct credential *c, int timeout)
17{
18 struct credential_cache_entry *e;
19
20 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
21 e = &entries[entries_nr++];
22
23 /* take ownership of pointers */
24 memcpy(&e->item, c, sizeof(*c));
25 memset(c, 0, sizeof(*c));
26 e->expiration = time(NULL) + timeout;
27}
28
29static struct credential_cache_entry *lookup_credential(const struct credential *c)
30{
31 int i;
32 for (i = 0; i < entries_nr; i++) {
33 struct credential *e = &entries[i].item;
34 if (credential_match(c, e))
35 return &entries[i];
36 }
37 return NULL;
38}
39
40static void remove_credential(const struct credential *c)
41{
42 struct credential_cache_entry *e;
43
44 e = lookup_credential(c);
45 if (e)
46 e->expiration = 0;
47}
48
Johannes Schindelindddbad72017-04-26 21:29:31 +020049static timestamp_t check_expirations(void)
Jeff Kinge2770972011-12-10 05:34:14 -050050{
Johannes Schindelindddbad72017-04-26 21:29:31 +020051 static timestamp_t wait_for_entry_until;
Jeff Kinge2770972011-12-10 05:34:14 -050052 int i = 0;
Johannes Schindelindddbad72017-04-26 21:29:31 +020053 timestamp_t now = time(NULL);
54 timestamp_t next = TIME_MAX;
Jeff Kinge2770972011-12-10 05:34:14 -050055
56 /*
57 * Initially give the client 30 seconds to actually contact us
58 * and store a credential before we decide there's no point in
59 * keeping the daemon around.
60 */
61 if (!wait_for_entry_until)
62 wait_for_entry_until = now + 30;
63
64 while (i < entries_nr) {
65 if (entries[i].expiration <= now) {
66 entries_nr--;
67 credential_clear(&entries[i].item);
68 if (i != entries_nr)
69 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
70 /*
71 * Stick around 30 seconds in case a new credential
72 * shows up (e.g., because we just removed a failed
73 * one, and we will soon get the correct one).
74 */
75 wait_for_entry_until = now + 30;
76 }
77 else {
78 if (entries[i].expiration < next)
79 next = entries[i].expiration;
80 i++;
81 }
82 }
83
84 if (!entries_nr) {
85 if (wait_for_entry_until <= now)
86 return 0;
87 next = wait_for_entry_until;
88 }
89
90 return next - now;
91}
92
93static int read_request(FILE *fh, struct credential *c,
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +010094 struct strbuf *action, int *timeout)
95{
Jeff Kinge2770972011-12-10 05:34:14 -050096 static struct strbuf item = STRBUF_INIT;
97 const char *p;
98
Junio C Hamano8f309ae2016-01-13 15:31:17 -080099 strbuf_getline_lf(&item, fh);
Jeff Kingcf4fff52014-06-18 15:44:19 -0400100 if (!skip_prefix(item.buf, "action=", &p))
Jeff Kinge2770972011-12-10 05:34:14 -0500101 return error("client sent bogus action line: %s", item.buf);
102 strbuf_addstr(action, p);
103
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800104 strbuf_getline_lf(&item, fh);
Jeff Kingcf4fff52014-06-18 15:44:19 -0400105 if (!skip_prefix(item.buf, "timeout=", &p))
Jeff Kinge2770972011-12-10 05:34:14 -0500106 return error("client sent bogus timeout line: %s", item.buf);
107 *timeout = atoi(p);
108
109 if (credential_read(c, fh) < 0)
110 return -1;
111 return 0;
112}
113
114static void serve_one_client(FILE *in, FILE *out)
115{
116 struct credential c = CREDENTIAL_INIT;
117 struct strbuf action = STRBUF_INIT;
118 int timeout = -1;
119
120 if (read_request(in, &c, &action, &timeout) < 0)
121 /* ignore error */ ;
122 else if (!strcmp(action.buf, "get")) {
123 struct credential_cache_entry *e = lookup_credential(&c);
124 if (e) {
125 fprintf(out, "username=%s\n", e->item.username);
126 fprintf(out, "password=%s\n", e->item.password);
127 }
128 }
Jeff King7d5e9c92016-03-18 02:12:01 -0400129 else if (!strcmp(action.buf, "exit")) {
130 /*
131 * It's important that we clean up our socket first, and then
132 * signal the client only once we have finished the cleanup.
133 * Calling exit() directly does this, because we clean up in
134 * our atexit() handler, and then signal the client when our
135 * process actually ends, which closes the socket and gives
136 * them EOF.
137 */
Jeff Kinge2770972011-12-10 05:34:14 -0500138 exit(0);
Jeff King7d5e9c92016-03-18 02:12:01 -0400139 }
Jeff Kinge2770972011-12-10 05:34:14 -0500140 else if (!strcmp(action.buf, "erase"))
141 remove_credential(&c);
142 else if (!strcmp(action.buf, "store")) {
143 if (timeout < 0)
144 warning("cache client didn't specify a timeout");
145 else if (!c.username || !c.password)
146 warning("cache client gave us a partial credential");
147 else {
148 remove_credential(&c);
149 cache_credential(&c, timeout);
150 }
151 }
152 else
153 warning("cache client sent unknown action: %s", action.buf);
154
155 credential_clear(&c);
156 strbuf_release(&action);
157}
158
159static int serve_cache_loop(int fd)
160{
161 struct pollfd pfd;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200162 timestamp_t wakeup;
Jeff Kinge2770972011-12-10 05:34:14 -0500163
164 wakeup = check_expirations();
165 if (!wakeup)
166 return 0;
167
168 pfd.fd = fd;
169 pfd.events = POLLIN;
170 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
171 if (errno != EINTR)
172 die_errno("poll failed");
173 return 1;
174 }
175
176 if (pfd.revents & POLLIN) {
177 int client, client2;
178 FILE *in, *out;
179
180 client = accept(fd, NULL, NULL);
181 if (client < 0) {
Nguyễn Thái Ngọc Duy26604f92016-05-08 16:47:41 +0700182 warning_errno("accept failed");
Jeff Kinge2770972011-12-10 05:34:14 -0500183 return 1;
184 }
185 client2 = dup(client);
186 if (client2 < 0) {
Nguyễn Thái Ngọc Duy26604f92016-05-08 16:47:41 +0700187 warning_errno("dup failed");
Jeff Kinge2770972011-12-10 05:34:14 -0500188 close(client);
189 return 1;
190 }
191
192 in = xfdopen(client, "r");
193 out = xfdopen(client2, "w");
194 serve_one_client(in, out);
195 fclose(in);
196 fclose(out);
197 }
198 return 1;
199}
200
Jeff Kingf5e3c0b2014-09-14 03:35:06 -0400201static void serve_cache(const char *socket_path, int debug)
Jeff Kinge2770972011-12-10 05:34:14 -0500202{
203 int fd;
204
205 fd = unix_stream_listen(socket_path);
206 if (fd < 0)
207 die_errno("unable to bind to '%s'", socket_path);
208
209 printf("ok\n");
210 fclose(stdout);
Jeff Kingf5e3c0b2014-09-14 03:35:06 -0400211 if (!debug) {
212 if (!freopen("/dev/null", "w", stderr))
213 die_errno("unable to point stderr to /dev/null");
214 }
Jeff Kinge2770972011-12-10 05:34:14 -0500215
216 while (serve_cache_loop(fd))
217 ; /* nothing */
218
219 close(fd);
Jeff Kinge2770972011-12-10 05:34:14 -0500220}
221
Vasco Almeidaaf64f202016-10-17 13:15:28 +0000222static const char permissions_advice[] = N_(
Jeff Kinge2770972011-12-10 05:34:14 -0500223"The permissions on your socket directory are too loose; other\n"
224"users may be able to read your cached credentials. Consider running:\n"
225"\n"
Vasco Almeidaaf64f202016-10-17 13:15:28 +0000226" chmod 0700 %s");
Jon Griffithsa6e5e282016-02-23 02:15:15 -0500227static void init_socket_directory(const char *path)
Jeff Kinge2770972011-12-10 05:34:14 -0500228{
229 struct stat st;
230 char *path_copy = xstrdup(path);
231 char *dir = dirname(path_copy);
232
233 if (!stat(dir, &st)) {
234 if (st.st_mode & 077)
Vasco Almeidaaf64f202016-10-17 13:15:28 +0000235 die(_(permissions_advice), dir);
Jon Griffithsa6e5e282016-02-23 02:15:15 -0500236 } else {
237 /*
238 * We must be sure to create the directory with the correct mode,
239 * not just chmod it after the fact; otherwise, there is a race
240 * condition in which somebody can chdir to it, sleep, then try to open
241 * our protected socket.
242 */
243 if (safe_create_leading_directories_const(dir) < 0)
244 die_errno("unable to create directories for '%s'", dir);
245 if (mkdir(dir, 0700) < 0)
246 die_errno("unable to mkdir '%s'", dir);
Jeff Kinge2770972011-12-10 05:34:14 -0500247 }
248
Jon Griffiths6e614492016-02-23 02:16:04 -0500249 if (chdir(dir))
250 /*
251 * We don't actually care what our cwd is; we chdir here just to
252 * be a friendly daemon and avoid tying up our original cwd.
253 * If this fails, it's OK to just continue without that benefit.
254 */
255 ;
256
Jeff Kinge2770972011-12-10 05:34:14 -0500257 free(path_copy);
258}
259
Jeff King3f2e2292016-07-01 01:58:58 -0400260int cmd_main(int argc, const char **argv)
Jeff Kinge2770972011-12-10 05:34:14 -0500261{
Jeff King076aa2c2017-09-05 08:15:08 -0400262 struct tempfile *socket_file;
Michael Haggerty9e903312015-08-10 11:47:51 +0200263 const char *socket_path;
Noam Postavsky7f4d4742015-11-09 19:26:29 -0500264 int ignore_sighup = 0;
Jeff Kingf5e3c0b2014-09-14 03:35:06 -0400265 static const char *usage[] = {
266 "git-credential-cache--daemon [opts] <socket_path>",
267 NULL
268 };
269 int debug = 0;
270 const struct option options[] = {
271 OPT_BOOL(0, "debug", &debug,
272 N_("print debugging messages to stderr")),
273 OPT_END()
274 };
275
Noam Postavsky7f4d4742015-11-09 19:26:29 -0500276 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
277
Jeff Kingf5e3c0b2014-09-14 03:35:06 -0400278 argc = parse_options(argc, argv, NULL, options, usage, 0);
279 socket_path = argv[0];
Jeff Kinge2770972011-12-10 05:34:14 -0500280
281 if (!socket_path)
Jeff Kingf5e3c0b2014-09-14 03:35:06 -0400282 usage_with_options(usage, options);
Michael Haggerty9e903312015-08-10 11:47:51 +0200283
Jon Griffithsbd93b8d2016-02-23 02:15:41 -0500284 if (!is_absolute_path(socket_path))
285 die("socket directory must be an absolute path");
286
Jon Griffithsa6e5e282016-02-23 02:15:15 -0500287 init_socket_directory(socket_path);
Jeff King076aa2c2017-09-05 08:15:08 -0400288 socket_file = register_tempfile(socket_path);
Noam Postavsky7f4d4742015-11-09 19:26:29 -0500289
290 if (ignore_sighup)
291 signal(SIGHUP, SIG_IGN);
292
Jeff Kingf5e3c0b2014-09-14 03:35:06 -0400293 serve_cache(socket_path, debug);
Michael Haggerty9e903312015-08-10 11:47:51 +0200294 delete_tempfile(&socket_file);
Michael Haggerty18a3de42015-08-10 11:47:50 +0200295
Jeff Kinge2770972011-12-10 05:34:14 -0500296 return 0;
297}