2 #include "credential.h"
3 #include "unix-socket.h"
5 #include "parse-options.h"
7 static const char *socket_path;
9 static void cleanup_socket(void)
15 static void cleanup_socket_on_signal(int sig)
22 struct credential_cache_entry {
23 struct credential item;
24 unsigned long expiration;
26 static struct credential_cache_entry *entries;
27 static int entries_nr;
28 static int entries_alloc;
30 static void cache_credential(struct credential *c, int timeout)
32 struct credential_cache_entry *e;
34 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
35 e = &entries[entries_nr++];
37 /* take ownership of pointers */
38 memcpy(&e->item, c, sizeof(*c));
39 memset(c, 0, sizeof(*c));
40 e->expiration = time(NULL) + timeout;
43 static struct credential_cache_entry *lookup_credential(const struct credential *c)
46 for (i = 0; i < entries_nr; i++) {
47 struct credential *e = &entries[i].item;
48 if (credential_match(c, e))
54 static void remove_credential(const struct credential *c)
56 struct credential_cache_entry *e;
58 e = lookup_credential(c);
63 static int check_expirations(void)
65 static unsigned long wait_for_entry_until;
67 unsigned long now = time(NULL);
68 unsigned long next = (unsigned long)-1;
71 * Initially give the client 30 seconds to actually contact us
72 * and store a credential before we decide there's no point in
73 * keeping the daemon around.
75 if (!wait_for_entry_until)
76 wait_for_entry_until = now + 30;
78 while (i < entries_nr) {
79 if (entries[i].expiration <= now) {
81 credential_clear(&entries[i].item);
83 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
85 * Stick around 30 seconds in case a new credential
86 * shows up (e.g., because we just removed a failed
87 * one, and we will soon get the correct one).
89 wait_for_entry_until = now + 30;
92 if (entries[i].expiration < next)
93 next = entries[i].expiration;
99 if (wait_for_entry_until <= now)
101 next = wait_for_entry_until;
107 static int read_request(FILE *fh, struct credential *c,
108 struct strbuf *action, int *timeout) {
109 static struct strbuf item = STRBUF_INIT;
112 strbuf_getline(&item, fh, '\n');
113 if (!skip_prefix(item.buf, "action=", &p))
114 return error("client sent bogus action line: %s", item.buf);
115 strbuf_addstr(action, p);
117 strbuf_getline(&item, fh, '\n');
118 if (!skip_prefix(item.buf, "timeout=", &p))
119 return error("client sent bogus timeout line: %s", item.buf);
122 if (credential_read(c, fh) < 0)
127 static void serve_one_client(FILE *in, FILE *out)
129 struct credential c = CREDENTIAL_INIT;
130 struct strbuf action = STRBUF_INIT;
133 if (read_request(in, &c, &action, &timeout) < 0)
135 else if (!strcmp(action.buf, "get")) {
136 struct credential_cache_entry *e = lookup_credential(&c);
138 fprintf(out, "username=%s\n", e->item.username);
139 fprintf(out, "password=%s\n", e->item.password);
142 else if (!strcmp(action.buf, "exit"))
144 else if (!strcmp(action.buf, "erase"))
145 remove_credential(&c);
146 else if (!strcmp(action.buf, "store")) {
148 warning("cache client didn't specify a timeout");
149 else if (!c.username || !c.password)
150 warning("cache client gave us a partial credential");
152 remove_credential(&c);
153 cache_credential(&c, timeout);
157 warning("cache client sent unknown action: %s", action.buf);
159 credential_clear(&c);
160 strbuf_release(&action);
163 static int serve_cache_loop(int fd)
166 unsigned long wakeup;
168 wakeup = check_expirations();
174 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
176 die_errno("poll failed");
180 if (pfd.revents & POLLIN) {
184 client = accept(fd, NULL, NULL);
186 warning("accept failed: %s", strerror(errno));
189 client2 = dup(client);
191 warning("dup failed: %s", strerror(errno));
196 in = xfdopen(client, "r");
197 out = xfdopen(client2, "w");
198 serve_one_client(in, out);
205 static void serve_cache(const char *socket_path, int debug)
209 fd = unix_stream_listen(socket_path);
211 die_errno("unable to bind to '%s'", socket_path);
216 if (!freopen("/dev/null", "w", stderr))
217 die_errno("unable to point stderr to /dev/null");
220 while (serve_cache_loop(fd))
227 static const char permissions_advice[] =
228 "The permissions on your socket directory are too loose; other\n"
229 "users may be able to read your cached credentials. Consider running:\n"
232 static void check_socket_directory(const char *path)
235 char *path_copy = xstrdup(path);
236 char *dir = dirname(path_copy);
238 if (!stat(dir, &st)) {
239 if (st.st_mode & 077)
240 die(permissions_advice, dir);
246 * We must be sure to create the directory with the correct mode,
247 * not just chmod it after the fact; otherwise, there is a race
248 * condition in which somebody can chdir to it, sleep, then try to open
249 * our protected socket.
251 if (safe_create_leading_directories_const(dir) < 0)
252 die_errno("unable to create directories for '%s'", dir);
253 if (mkdir(dir, 0700) < 0)
254 die_errno("unable to mkdir '%s'", dir);
258 int main(int argc, const char **argv)
260 static const char *usage[] = {
261 "git-credential-cache--daemon [opts] <socket_path>",
265 const struct option options[] = {
266 OPT_BOOL(0, "debug", &debug,
267 N_("print debugging messages to stderr")),
271 argc = parse_options(argc, argv, NULL, options, usage, 0);
272 socket_path = argv[0];
275 usage_with_options(usage, options);
276 check_socket_directory(socket_path);
278 atexit(cleanup_socket);
279 sigchain_push_common(cleanup_socket_on_signal);
281 serve_cache(socket_path, debug);