credential-cache--daemon: delete socket from main()
[git/git.git] / credential-cache--daemon.c
CommitLineData
e2770979
JK
1#include "cache.h"
2#include "credential.h"
3#include "unix-socket.h"
4#include "sigchain.h"
f5e3c0b9 5#include "parse-options.h"
e2770979
JK
6
7static const char *socket_path;
8
9static void cleanup_socket(void)
10{
11 if (socket_path)
12 unlink(socket_path);
13}
14
15static void cleanup_socket_on_signal(int sig)
16{
17 cleanup_socket();
18 sigchain_pop(sig);
19 raise(sig);
20}
21
22struct credential_cache_entry {
23 struct credential item;
24 unsigned long expiration;
25};
26static struct credential_cache_entry *entries;
27static int entries_nr;
28static int entries_alloc;
29
30static void cache_credential(struct credential *c, int timeout)
31{
32 struct credential_cache_entry *e;
33
34 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
35 e = &entries[entries_nr++];
36
37 /* take ownership of pointers */
38 memcpy(&e->item, c, sizeof(*c));
39 memset(c, 0, sizeof(*c));
40 e->expiration = time(NULL) + timeout;
41}
42
43static struct credential_cache_entry *lookup_credential(const struct credential *c)
44{
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];
50 }
51 return NULL;
52}
53
54static void remove_credential(const struct credential *c)
55{
56 struct credential_cache_entry *e;
57
58 e = lookup_credential(c);
59 if (e)
60 e->expiration = 0;
61}
62
63static int check_expirations(void)
64{
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;
69
70 /*
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.
74 */
75 if (!wait_for_entry_until)
76 wait_for_entry_until = now + 30;
77
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));
84 /*
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).
88 */
89 wait_for_entry_until = now + 30;
90 }
91 else {
92 if (entries[i].expiration < next)
93 next = entries[i].expiration;
94 i++;
95 }
96 }
97
98 if (!entries_nr) {
99 if (wait_for_entry_until <= now)
100 return 0;
101 next = wait_for_entry_until;
102 }
103
104 return next - now;
105}
106
107static 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;
111
112 strbuf_getline(&item, fh, '\n');
cf4fff57 113 if (!skip_prefix(item.buf, "action=", &p))
e2770979
JK
114 return error("client sent bogus action line: %s", item.buf);
115 strbuf_addstr(action, p);
116
117 strbuf_getline(&item, fh, '\n');
cf4fff57 118 if (!skip_prefix(item.buf, "timeout=", &p))
e2770979
JK
119 return error("client sent bogus timeout line: %s", item.buf);
120 *timeout = atoi(p);
121
122 if (credential_read(c, fh) < 0)
123 return -1;
124 return 0;
125}
126
127static void serve_one_client(FILE *in, FILE *out)
128{
129 struct credential c = CREDENTIAL_INIT;
130 struct strbuf action = STRBUF_INIT;
131 int timeout = -1;
132
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);
140 }
141 }
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);
154 }
155 }
156 else
157 warning("cache client sent unknown action: %s", action.buf);
158
159 credential_clear(&c);
160 strbuf_release(&action);
161}
162
163static int serve_cache_loop(int fd)
164{
165 struct pollfd pfd;
166 unsigned long wakeup;
167
168 wakeup = check_expirations();
169 if (!wakeup)
170 return 0;
171
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;
178 }
179
180 if (pfd.revents & POLLIN) {
181 int client, client2;
182 FILE *in, *out;
183
184 client = accept(fd, NULL, NULL);
185 if (client < 0) {
186 warning("accept failed: %s", strerror(errno));
187 return 1;
188 }
189 client2 = dup(client);
190 if (client2 < 0) {
191 warning("dup failed: %s", strerror(errno));
192 close(client);
193 return 1;
194 }
195
196 in = xfdopen(client, "r");
197 out = xfdopen(client2, "w");
198 serve_one_client(in, out);
199 fclose(in);
200 fclose(out);
201 }
202 return 1;
203}
204
f5e3c0b9 205static void serve_cache(const char *socket_path, int debug)
e2770979
JK
206{
207 int fd;
208
209 fd = unix_stream_listen(socket_path);
210 if (fd < 0)
211 die_errno("unable to bind to '%s'", socket_path);
212
213 printf("ok\n");
214 fclose(stdout);
f5e3c0b9
JK
215 if (!debug) {
216 if (!freopen("/dev/null", "w", stderr))
217 die_errno("unable to point stderr to /dev/null");
218 }
e2770979
JK
219
220 while (serve_cache_loop(fd))
221 ; /* nothing */
222
223 close(fd);
e2770979
JK
224}
225
226static const char permissions_advice[] =
227"The permissions on your socket directory are too loose; other\n"
228"users may be able to read your cached credentials. Consider running:\n"
229"\n"
230" chmod 0700 %s";
231static void check_socket_directory(const char *path)
232{
233 struct stat st;
234 char *path_copy = xstrdup(path);
235 char *dir = dirname(path_copy);
236
237 if (!stat(dir, &st)) {
238 if (st.st_mode & 077)
239 die(permissions_advice, dir);
240 free(path_copy);
241 return;
242 }
243
244 /*
245 * We must be sure to create the directory with the correct mode,
246 * not just chmod it after the fact; otherwise, there is a race
247 * condition in which somebody can chdir to it, sleep, then try to open
248 * our protected socket.
249 */
250 if (safe_create_leading_directories_const(dir) < 0)
251 die_errno("unable to create directories for '%s'", dir);
252 if (mkdir(dir, 0700) < 0)
253 die_errno("unable to mkdir '%s'", dir);
254 free(path_copy);
255}
256
257int main(int argc, const char **argv)
258{
f5e3c0b9
JK
259 static const char *usage[] = {
260 "git-credential-cache--daemon [opts] <socket_path>",
261 NULL
262 };
263 int debug = 0;
264 const struct option options[] = {
265 OPT_BOOL(0, "debug", &debug,
266 N_("print debugging messages to stderr")),
267 OPT_END()
268 };
269
270 argc = parse_options(argc, argv, NULL, options, usage, 0);
271 socket_path = argv[0];
e2770979
JK
272
273 if (!socket_path)
f5e3c0b9 274 usage_with_options(usage, options);
e2770979
JK
275 check_socket_directory(socket_path);
276
277 atexit(cleanup_socket);
278 sigchain_push_common(cleanup_socket_on_signal);
279
f5e3c0b9 280 serve_cache(socket_path, debug);
e2770979 281
18a3de42
MH
282 unlink(socket_path);
283
e2770979
JK
284 return 0;
285}