1#include "cache.h"
2#include "tempfile.h"
3#include "credential.h"
4#include "unix-socket.h"
5#include "parse-options.h"
6
7static struct tempfile socket_file;
8
9struct credential_cache_entry {
10 struct credential item;
11 unsigned long expiration;
12};
13static struct credential_cache_entry *entries;
14static int entries_nr;
15static int entries_alloc;
16
17static void cache_credential(struct credential *c, int timeout)
18{
19 struct credential_cache_entry *e;
20
21 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
22 e = &entries[entries_nr++];
23
24 /* take ownership of pointers */
25 memcpy(&e->item, c, sizeof(*c));
26 memset(c, 0, sizeof(*c));
27 e->expiration = time(NULL) + timeout;
28}
29
30static struct credential_cache_entry *lookup_credential(const struct credential *c)
31{
32 int i;
33 for (i = 0; i < entries_nr; i++) {
34 struct credential *e = &entries[i].item;
35 if (credential_match(c, e))
36 return &entries[i];
37 }
38 return NULL;
39}
40
41static void remove_credential(const struct credential *c)
42{
43 struct credential_cache_entry *e;
44
45 e = lookup_credential(c);
46 if (e)
47 e->expiration = 0;
48}
49
50static int check_expirations(void)
51{
52 static unsigned long wait_for_entry_until;
53 int i = 0;
54 unsigned long now = time(NULL);
55 unsigned long next = (unsigned long)-1;
56
57 /*
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.
61 */
62 if (!wait_for_entry_until)
63 wait_for_entry_until = now + 30;
64
65 while (i < entries_nr) {
66 if (entries[i].expiration <= now) {
67 entries_nr--;
68 credential_clear(&entries[i].item);
69 if (i != entries_nr)
70 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
71 /*
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).
75 */
76 wait_for_entry_until = now + 30;
77 }
78 else {
79 if (entries[i].expiration < next)
80 next = entries[i].expiration;
81 i++;
82 }
83 }
84
85 if (!entries_nr) {
86 if (wait_for_entry_until <= now)
87 return 0;
88 next = wait_for_entry_until;
89 }
90
91 return next - now;
92}
93
94static int read_request(FILE *fh, struct credential *c,
95 struct strbuf *action, int *timeout) {
96 static struct strbuf item = STRBUF_INIT;
97 const char *p;
98
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);
103
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);
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 }
129 else if (!strcmp(action.buf, "exit"))
130 exit(0);
131 else if (!strcmp(action.buf, "erase"))
132 remove_credential(&c);
133 else if (!strcmp(action.buf, "store")) {
134 if (timeout < 0)
135 warning("cache client didn't specify a timeout");
136 else if (!c.username || !c.password)
137 warning("cache client gave us a partial credential");
138 else {
139 remove_credential(&c);
140 cache_credential(&c, timeout);
141 }
142 }
143 else
144 warning("cache client sent unknown action: %s", action.buf);
145
146 credential_clear(&c);
147 strbuf_release(&action);
148}
149
150static int serve_cache_loop(int fd)
151{
152 struct pollfd pfd;
153 unsigned long wakeup;
154
155 wakeup = check_expirations();
156 if (!wakeup)
157 return 0;
158
159 pfd.fd = fd;
160 pfd.events = POLLIN;
161 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
162 if (errno != EINTR)
163 die_errno("poll failed");
164 return 1;
165 }
166
167 if (pfd.revents & POLLIN) {
168 int client, client2;
169 FILE *in, *out;
170
171 client = accept(fd, NULL, NULL);
172 if (client < 0) {
173 warning("accept failed: %s", strerror(errno));
174 return 1;
175 }
176 client2 = dup(client);
177 if (client2 < 0) {
178 warning("dup failed: %s", strerror(errno));
179 close(client);
180 return 1;
181 }
182
183 in = xfdopen(client, "r");
184 out = xfdopen(client2, "w");
185 serve_one_client(in, out);
186 fclose(in);
187 fclose(out);
188 }
189 return 1;
190}
191
192static void serve_cache(const char *socket_path, int debug)
193{
194 int fd;
195
196 fd = unix_stream_listen(socket_path);
197 if (fd < 0)
198 die_errno("unable to bind to '%s'", socket_path);
199
200 printf("ok\n");
201 fclose(stdout);
202 if (!debug) {
203 if (!freopen("/dev/null", "w", stderr))
204 die_errno("unable to point stderr to /dev/null");
205 }
206
207 while (serve_cache_loop(fd))
208 ; /* nothing */
209
210 close(fd);
211}
212
213static const char permissions_advice[] =
214"The permissions on your socket directory are too loose; other\n"
215"users may be able to read your cached credentials. Consider running:\n"
216"\n"
217" chmod 0700 %s";
218static void init_socket_directory(const char *path)
219{
220 struct stat st;
221 char *path_copy = xstrdup(path);
222 char *dir = dirname(path_copy);
223
224 if (!stat(dir, &st)) {
225 if (st.st_mode & 077)
226 die(permissions_advice, dir);
227 } else {
228 /*
229 * We must be sure to create the directory with the correct mode,
230 * not just chmod it after the fact; otherwise, there is a race
231 * condition in which somebody can chdir to it, sleep, then try to open
232 * our protected socket.
233 */
234 if (safe_create_leading_directories_const(dir) < 0)
235 die_errno("unable to create directories for '%s'", dir);
236 if (mkdir(dir, 0700) < 0)
237 die_errno("unable to mkdir '%s'", dir);
238 }
239
240 if (chdir(dir))
241 /*
242 * We don't actually care what our cwd is; we chdir here just to
243 * be a friendly daemon and avoid tying up our original cwd.
244 * If this fails, it's OK to just continue without that benefit.
245 */
246 ;
247
248 free(path_copy);
249}
250
251int main(int argc, const char **argv)
252{
253 const char *socket_path;
254 int ignore_sighup = 0;
255 static const char *usage[] = {
256 "git-credential-cache--daemon [opts] <socket_path>",
257 NULL
258 };
259 int debug = 0;
260 const struct option options[] = {
261 OPT_BOOL(0, "debug", &debug,
262 N_("print debugging messages to stderr")),
263 OPT_END()
264 };
265
266 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
267
268 argc = parse_options(argc, argv, NULL, options, usage, 0);
269 socket_path = argv[0];
270
271 if (!socket_path)
272 usage_with_options(usage, options);
273
274 if (!is_absolute_path(socket_path))
275 die("socket directory must be an absolute path");
276
277 init_socket_directory(socket_path);
278 register_tempfile(&socket_file, socket_path);
279
280 if (ignore_sighup)
281 signal(SIGHUP, SIG_IGN);
282
283 serve_cache(socket_path, debug);
284 delete_tempfile(&socket_file);
285
286 return 0;
287}