rebase -i: remove patch file after conflict resolution
[git.git] / builtin / credential-cache--daemon.c
blob62c09a271d6eb4578ef0360910575c5611f61ffd
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "alloc.h"
4 #include "gettext.h"
5 #include "parse-options.h"
7 #ifndef NO_UNIX_SOCKETS
9 #include "config.h"
10 #include "tempfile.h"
11 #include "credential.h"
12 #include "unix-socket.h"
14 struct credential_cache_entry {
15 struct credential item;
16 timestamp_t expiration;
18 static struct credential_cache_entry *entries;
19 static int entries_nr;
20 static int entries_alloc;
22 static void cache_credential(struct credential *c, int timeout)
24 struct credential_cache_entry *e;
26 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
27 e = &entries[entries_nr++];
29 /* take ownership of pointers */
30 memcpy(&e->item, c, sizeof(*c));
31 memset(c, 0, sizeof(*c));
32 e->expiration = time(NULL) + timeout;
35 static struct credential_cache_entry *lookup_credential(const struct credential *c)
37 int i;
38 for (i = 0; i < entries_nr; i++) {
39 struct credential *e = &entries[i].item;
40 if (credential_match(c, e))
41 return &entries[i];
43 return NULL;
46 static void remove_credential(const struct credential *c)
48 struct credential_cache_entry *e;
50 e = lookup_credential(c);
51 if (e)
52 e->expiration = 0;
55 static timestamp_t check_expirations(void)
57 static timestamp_t wait_for_entry_until;
58 int i = 0;
59 timestamp_t now = time(NULL);
60 timestamp_t next = TIME_MAX;
63 * Initially give the client 30 seconds to actually contact us
64 * and store a credential before we decide there's no point in
65 * keeping the daemon around.
67 if (!wait_for_entry_until)
68 wait_for_entry_until = now + 30;
70 while (i < entries_nr) {
71 if (entries[i].expiration <= now) {
72 entries_nr--;
73 credential_clear(&entries[i].item);
74 if (i != entries_nr)
75 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
77 * Stick around 30 seconds in case a new credential
78 * shows up (e.g., because we just removed a failed
79 * one, and we will soon get the correct one).
81 wait_for_entry_until = now + 30;
83 else {
84 if (entries[i].expiration < next)
85 next = entries[i].expiration;
86 i++;
90 if (!entries_nr) {
91 if (wait_for_entry_until <= now)
92 return 0;
93 next = wait_for_entry_until;
96 return next - now;
99 static int read_request(FILE *fh, struct credential *c,
100 struct strbuf *action, int *timeout)
102 static struct strbuf item = STRBUF_INIT;
103 const char *p;
105 strbuf_getline_lf(&item, fh);
106 if (!skip_prefix(item.buf, "action=", &p))
107 return error("client sent bogus action line: %s", item.buf);
108 strbuf_addstr(action, p);
110 strbuf_getline_lf(&item, fh);
111 if (!skip_prefix(item.buf, "timeout=", &p))
112 return error("client sent bogus timeout line: %s", item.buf);
113 *timeout = atoi(p);
115 if (credential_read(c, fh) < 0)
116 return -1;
117 return 0;
120 static void serve_one_client(FILE *in, FILE *out)
122 struct credential c = CREDENTIAL_INIT;
123 struct strbuf action = STRBUF_INIT;
124 int timeout = -1;
126 if (read_request(in, &c, &action, &timeout) < 0)
127 /* ignore error */ ;
128 else if (!strcmp(action.buf, "get")) {
129 struct credential_cache_entry *e = lookup_credential(&c);
130 if (e) {
131 fprintf(out, "username=%s\n", e->item.username);
132 fprintf(out, "password=%s\n", e->item.password);
133 if (e->item.password_expiry_utc != TIME_MAX)
134 fprintf(out, "password_expiry_utc=%"PRItime"\n",
135 e->item.password_expiry_utc);
138 else if (!strcmp(action.buf, "exit")) {
140 * It's important that we clean up our socket first, and then
141 * signal the client only once we have finished the cleanup.
142 * Calling exit() directly does this, because we clean up in
143 * our atexit() handler, and then signal the client when our
144 * process actually ends, which closes the socket and gives
145 * them EOF.
147 exit(0);
149 else if (!strcmp(action.buf, "erase"))
150 remove_credential(&c);
151 else if (!strcmp(action.buf, "store")) {
152 if (timeout < 0)
153 warning("cache client didn't specify a timeout");
154 else if (!c.username || !c.password)
155 warning("cache client gave us a partial credential");
156 else {
157 remove_credential(&c);
158 cache_credential(&c, timeout);
161 else
162 warning("cache client sent unknown action: %s", action.buf);
164 credential_clear(&c);
165 strbuf_release(&action);
168 static int serve_cache_loop(int fd)
170 struct pollfd pfd;
171 timestamp_t wakeup;
173 wakeup = check_expirations();
174 if (!wakeup)
175 return 0;
177 pfd.fd = fd;
178 pfd.events = POLLIN;
179 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
180 if (errno != EINTR)
181 die_errno("poll failed");
182 return 1;
185 if (pfd.revents & POLLIN) {
186 int client, client2;
187 FILE *in, *out;
189 client = accept(fd, NULL, NULL);
190 if (client < 0) {
191 warning_errno("accept failed");
192 return 1;
194 client2 = dup(client);
195 if (client2 < 0) {
196 warning_errno("dup failed");
197 close(client);
198 return 1;
201 in = xfdopen(client, "r");
202 out = xfdopen(client2, "w");
203 serve_one_client(in, out);
204 fclose(in);
205 fclose(out);
207 return 1;
210 static void serve_cache(const char *socket_path, int debug)
212 struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
213 int fd;
215 fd = unix_stream_listen(socket_path, &opts);
216 if (fd < 0)
217 die_errno("unable to bind to '%s'", socket_path);
219 printf("ok\n");
220 fclose(stdout);
221 if (!debug) {
222 if (!freopen("/dev/null", "w", stderr))
223 die_errno("unable to point stderr to /dev/null");
226 while (serve_cache_loop(fd))
227 ; /* nothing */
229 close(fd);
232 static const char permissions_advice[] = N_(
233 "The permissions on your socket directory are too loose; other\n"
234 "users may be able to read your cached credentials. Consider running:\n"
235 "\n"
236 " chmod 0700 %s");
237 static void init_socket_directory(const char *path)
239 struct stat st;
240 char *path_copy = xstrdup(path);
241 char *dir = dirname(path_copy);
243 if (!stat(dir, &st)) {
244 if (st.st_mode & 077)
245 die(_(permissions_advice), dir);
246 } else {
248 * We must be sure to create the directory with the correct mode,
249 * not just chmod it after the fact; otherwise, there is a race
250 * condition in which somebody can chdir to it, sleep, then try to open
251 * our protected socket.
253 if (safe_create_leading_directories_const(dir) < 0)
254 die_errno("unable to create directories for '%s'", dir);
255 if (mkdir(dir, 0700) < 0)
256 die_errno("unable to mkdir '%s'", dir);
259 if (chdir(dir))
261 * We don't actually care what our cwd is; we chdir here just to
262 * be a friendly daemon and avoid tying up our original cwd.
263 * If this fails, it's OK to just continue without that benefit.
267 free(path_copy);
270 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
272 struct tempfile *socket_file;
273 const char *socket_path;
274 int ignore_sighup = 0;
275 static const char *usage[] = {
276 "git credential-cache--daemon [--debug] <socket-path>",
277 NULL
279 int debug = 0;
280 const struct option options[] = {
281 OPT_BOOL(0, "debug", &debug,
282 N_("print debugging messages to stderr")),
283 OPT_END()
286 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
288 argc = parse_options(argc, argv, prefix, options, usage, 0);
289 socket_path = argv[0];
291 if (!socket_path)
292 usage_with_options(usage, options);
294 if (!is_absolute_path(socket_path))
295 die("socket directory must be an absolute path");
297 init_socket_directory(socket_path);
298 socket_file = register_tempfile(socket_path);
300 if (ignore_sighup)
301 signal(SIGHUP, SIG_IGN);
303 serve_cache(socket_path, debug);
304 delete_tempfile(&socket_file);
306 return 0;
309 #else
311 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
313 const char * const usage[] = {
314 "git credential-cache--daemon [--debug] <socket-path>",
316 "credential-cache--daemon is disabled in this build of Git",
317 NULL
319 struct option options[] = { OPT_END() };
321 argc = parse_options(argc, argv, prefix, options, usage, 0);
322 die(_("credential-cache--daemon unavailable; no unix socket support"));
325 #endif /* NO_UNIX_SOCKET */