Git 2.2.1
[git/mingw.git] / credential-cache--daemon.c
blobc2f00498f6a171ecde3dd91a5cbdb30d88827495
1 #include "cache.h"
2 #include "credential.h"
3 #include "unix-socket.h"
4 #include "sigchain.h"
5 #include "parse-options.h"
7 static const char *socket_path;
9 static void cleanup_socket(void)
11 if (socket_path)
12 unlink(socket_path);
15 static void cleanup_socket_on_signal(int sig)
17 cleanup_socket();
18 sigchain_pop(sig);
19 raise(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)
45 int i;
46 for (i = 0; i < entries_nr; i++) {
47 struct credential *e = &entries[i].item;
48 if (credential_match(c, e))
49 return &entries[i];
51 return NULL;
54 static void remove_credential(const struct credential *c)
56 struct credential_cache_entry *e;
58 e = lookup_credential(c);
59 if (e)
60 e->expiration = 0;
63 static int check_expirations(void)
65 static unsigned long wait_for_entry_until;
66 int i = 0;
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) {
80 entries_nr--;
81 credential_clear(&entries[i].item);
82 if (i != entries_nr)
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;
91 else {
92 if (entries[i].expiration < next)
93 next = entries[i].expiration;
94 i++;
98 if (!entries_nr) {
99 if (wait_for_entry_until <= now)
100 return 0;
101 next = wait_for_entry_until;
104 return next - now;
107 static int read_request(FILE *fh, struct credential *c,
108 struct strbuf *action, int *timeout) {
109 static struct strbuf item = STRBUF_INIT;
110 const char *p;
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);
120 *timeout = atoi(p);
122 if (credential_read(c, fh) < 0)
123 return -1;
124 return 0;
127 static void serve_one_client(FILE *in, FILE *out)
129 struct credential c = CREDENTIAL_INIT;
130 struct strbuf action = STRBUF_INIT;
131 int timeout = -1;
133 if (read_request(in, &c, &action, &timeout) < 0)
134 /* ignore error */ ;
135 else if (!strcmp(action.buf, "get")) {
136 struct credential_cache_entry *e = lookup_credential(&c);
137 if (e) {
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"))
143 exit(0);
144 else if (!strcmp(action.buf, "erase"))
145 remove_credential(&c);
146 else if (!strcmp(action.buf, "store")) {
147 if (timeout < 0)
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");
151 else {
152 remove_credential(&c);
153 cache_credential(&c, timeout);
156 else
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)
165 struct pollfd pfd;
166 unsigned long wakeup;
168 wakeup = check_expirations();
169 if (!wakeup)
170 return 0;
172 pfd.fd = fd;
173 pfd.events = POLLIN;
174 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
175 if (errno != EINTR)
176 die_errno("poll failed");
177 return 1;
180 if (pfd.revents & POLLIN) {
181 int client, client2;
182 FILE *in, *out;
184 client = accept(fd, NULL, NULL);
185 if (client < 0) {
186 warning("accept failed: %s", strerror(errno));
187 return 1;
189 client2 = dup(client);
190 if (client2 < 0) {
191 warning("dup failed: %s", strerror(errno));
192 close(client);
193 return 1;
196 in = xfdopen(client, "r");
197 out = xfdopen(client2, "w");
198 serve_one_client(in, out);
199 fclose(in);
200 fclose(out);
202 return 1;
205 static void serve_cache(const char *socket_path, int debug)
207 int fd;
209 fd = unix_stream_listen(socket_path);
210 if (fd < 0)
211 die_errno("unable to bind to '%s'", socket_path);
213 printf("ok\n");
214 fclose(stdout);
215 if (!debug) {
216 if (!freopen("/dev/null", "w", stderr))
217 die_errno("unable to point stderr to /dev/null");
220 while (serve_cache_loop(fd))
221 ; /* nothing */
223 close(fd);
224 unlink(socket_path);
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"
230 "\n"
231 " chmod 0700 %s";
232 static void check_socket_directory(const char *path)
234 struct stat st;
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);
241 free(path_copy);
242 return;
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);
255 free(path_copy);
258 int main(int argc, const char **argv)
260 static const char *usage[] = {
261 "git-credential-cache--daemon [opts] <socket_path>",
262 NULL
264 int debug = 0;
265 const struct option options[] = {
266 OPT_BOOL(0, "debug", &debug,
267 N_("print debugging messages to stderr")),
268 OPT_END()
271 argc = parse_options(argc, argv, NULL, options, usage, 0);
272 socket_path = argv[0];
274 if (!socket_path)
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);
283 return 0;