3 #include "credential.h"
4 #include "unix-socket.h"
5 #include "parse-options.h"
7 static struct tempfile socket_file;
9 struct credential_cache_entry {
10 struct credential item;
11 timestamp_t expiration;
13 static struct credential_cache_entry *entries;
14 static int entries_nr;
15 static int entries_alloc;
17 static void cache_credential(struct credential *c, int timeout)
19 struct credential_cache_entry *e;
21 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
22 e = &entries[entries_nr++];
24 /* take ownership of pointers */
25 memcpy(&e->item, c, sizeof(*c));
26 memset(c, 0, sizeof(*c));
27 e->expiration = time(NULL) + timeout;
30 static struct credential_cache_entry *lookup_credential(const struct credential *c)
33 for (i = 0; i < entries_nr; i++) {
34 struct credential *e = &entries[i].item;
35 if (credential_match(c, e))
41 static void remove_credential(const struct credential *c)
43 struct credential_cache_entry *e;
45 e = lookup_credential(c);
50 static timestamp_t check_expirations(void)
52 static timestamp_t wait_for_entry_until;
54 timestamp_t now = time(NULL);
55 timestamp_t next = TIME_MAX;
58 * Initially give the client 30 seconds to actually contact us
59 * and store a credential before we decide there's no point in
60 * keeping the daemon around.
62 if (!wait_for_entry_until)
63 wait_for_entry_until = now + 30;
65 while (i < entries_nr) {
66 if (entries[i].expiration <= now) {
68 credential_clear(&entries[i].item);
70 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
72 * Stick around 30 seconds in case a new credential
73 * shows up (e.g., because we just removed a failed
74 * one, and we will soon get the correct one).
76 wait_for_entry_until = now + 30;
79 if (entries[i].expiration < next)
80 next = entries[i].expiration;
86 if (wait_for_entry_until <= now)
88 next = wait_for_entry_until;
94 static int read_request(FILE *fh, struct credential *c,
95 struct strbuf *action, int *timeout) {
96 static struct strbuf item = STRBUF_INIT;
99 strbuf_getline_lf(&item, fh);
100 if (!skip_prefix(item.buf, "action=", &p))
101 return error("client sent bogus action line: %s", item.buf);
102 strbuf_addstr(action, p);
104 strbuf_getline_lf(&item, fh);
105 if (!skip_prefix(item.buf, "timeout=", &p))
106 return error("client sent bogus timeout line: %s", item.buf);
109 if (credential_read(c, fh) < 0)
114 static void serve_one_client(FILE *in, FILE *out)
116 struct credential c = CREDENTIAL_INIT;
117 struct strbuf action = STRBUF_INIT;
120 if (read_request(in, &c, &action, &timeout) < 0)
122 else if (!strcmp(action.buf, "get")) {
123 struct credential_cache_entry *e = lookup_credential(&c);
125 fprintf(out, "username=%s\n", e->item.username);
126 fprintf(out, "password=%s\n", e->item.password);
129 else if (!strcmp(action.buf, "exit")) {
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
140 else if (!strcmp(action.buf, "erase"))
141 remove_credential(&c);
142 else if (!strcmp(action.buf, "store")) {
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");
148 remove_credential(&c);
149 cache_credential(&c, timeout);
153 warning("cache client sent unknown action: %s", action.buf);
155 credential_clear(&c);
156 strbuf_release(&action);
159 static int serve_cache_loop(int fd)
164 wakeup = check_expirations();
170 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
172 die_errno("poll failed");
176 if (pfd.revents & POLLIN) {
180 client = accept(fd, NULL, NULL);
182 warning_errno("accept failed");
185 client2 = dup(client);
187 warning_errno("dup failed");
192 in = xfdopen(client, "r");
193 out = xfdopen(client2, "w");
194 serve_one_client(in, out);
201 static void serve_cache(const char *socket_path, int debug)
205 fd = unix_stream_listen(socket_path);
207 die_errno("unable to bind to '%s'", socket_path);
212 if (!freopen("/dev/null", "w", stderr))
213 die_errno("unable to point stderr to /dev/null");
216 while (serve_cache_loop(fd))
222 static const char permissions_advice[] = N_(
223 "The permissions on your socket directory are too loose; other\n"
224 "users may be able to read your cached credentials. Consider running:\n"
227 static void init_socket_directory(const char *path)
230 char *path_copy = xstrdup(path);
231 char *dir = dirname(path_copy);
233 if (!stat(dir, &st)) {
234 if (st.st_mode & 077)
235 die(_(permissions_advice), dir);
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.
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);
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.
260 int cmd_main(int argc, const char **argv)
262 const char *socket_path;
263 int ignore_sighup = 0;
264 static const char *usage[] = {
265 "git-credential-cache--daemon [opts] <socket_path>",
269 const struct option options[] = {
270 OPT_BOOL(0, "debug", &debug,
271 N_("print debugging messages to stderr")),
275 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
277 argc = parse_options(argc, argv, NULL, options, usage, 0);
278 socket_path = argv[0];
281 usage_with_options(usage, options);
283 if (!is_absolute_path(socket_path))
284 die("socket directory must be an absolute path");
286 init_socket_directory(socket_path);
287 register_tempfile(&socket_file, socket_path);
290 signal(SIGHUP, SIG_IGN);
292 serve_cache(socket_path, debug);
293 delete_tempfile(&socket_file);