hash.h: move some oid-related declarations from cache.h
[git/debian.git] / builtin / credential-cache--daemon.c
blob590aefc6ead2893d018c4a724768bdfd79a1518a
1 #include "builtin.h"
2 #include "alloc.h"
3 #include "parse-options.h"
5 #ifndef NO_UNIX_SOCKETS
7 #include "config.h"
8 #include "tempfile.h"
9 #include "credential.h"
10 #include "unix-socket.h"
12 struct credential_cache_entry {
13 struct credential item;
14 timestamp_t expiration;
16 static struct credential_cache_entry *entries;
17 static int entries_nr;
18 static int entries_alloc;
20 static void cache_credential(struct credential *c, int timeout)
22 struct credential_cache_entry *e;
24 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
25 e = &entries[entries_nr++];
27 /* take ownership of pointers */
28 memcpy(&e->item, c, sizeof(*c));
29 memset(c, 0, sizeof(*c));
30 e->expiration = time(NULL) + timeout;
33 static struct credential_cache_entry *lookup_credential(const struct credential *c)
35 int i;
36 for (i = 0; i < entries_nr; i++) {
37 struct credential *e = &entries[i].item;
38 if (credential_match(c, e))
39 return &entries[i];
41 return NULL;
44 static void remove_credential(const struct credential *c)
46 struct credential_cache_entry *e;
48 e = lookup_credential(c);
49 if (e)
50 e->expiration = 0;
53 static timestamp_t check_expirations(void)
55 static timestamp_t wait_for_entry_until;
56 int i = 0;
57 timestamp_t now = time(NULL);
58 timestamp_t next = TIME_MAX;
61 * Initially give the client 30 seconds to actually contact us
62 * and store a credential before we decide there's no point in
63 * keeping the daemon around.
65 if (!wait_for_entry_until)
66 wait_for_entry_until = now + 30;
68 while (i < entries_nr) {
69 if (entries[i].expiration <= now) {
70 entries_nr--;
71 credential_clear(&entries[i].item);
72 if (i != entries_nr)
73 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
75 * Stick around 30 seconds in case a new credential
76 * shows up (e.g., because we just removed a failed
77 * one, and we will soon get the correct one).
79 wait_for_entry_until = now + 30;
81 else {
82 if (entries[i].expiration < next)
83 next = entries[i].expiration;
84 i++;
88 if (!entries_nr) {
89 if (wait_for_entry_until <= now)
90 return 0;
91 next = wait_for_entry_until;
94 return next - now;
97 static int read_request(FILE *fh, struct credential *c,
98 struct strbuf *action, int *timeout)
100 static struct strbuf item = STRBUF_INIT;
101 const char *p;
103 strbuf_getline_lf(&item, fh);
104 if (!skip_prefix(item.buf, "action=", &p))
105 return error("client sent bogus action line: %s", item.buf);
106 strbuf_addstr(action, p);
108 strbuf_getline_lf(&item, fh);
109 if (!skip_prefix(item.buf, "timeout=", &p))
110 return error("client sent bogus timeout line: %s", item.buf);
111 *timeout = atoi(p);
113 if (credential_read(c, fh) < 0)
114 return -1;
115 return 0;
118 static void serve_one_client(FILE *in, FILE *out)
120 struct credential c = CREDENTIAL_INIT;
121 struct strbuf action = STRBUF_INIT;
122 int timeout = -1;
124 if (read_request(in, &c, &action, &timeout) < 0)
125 /* ignore error */ ;
126 else if (!strcmp(action.buf, "get")) {
127 struct credential_cache_entry *e = lookup_credential(&c);
128 if (e) {
129 fprintf(out, "username=%s\n", e->item.username);
130 fprintf(out, "password=%s\n", e->item.password);
133 else if (!strcmp(action.buf, "exit")) {
135 * It's important that we clean up our socket first, and then
136 * signal the client only once we have finished the cleanup.
137 * Calling exit() directly does this, because we clean up in
138 * our atexit() handler, and then signal the client when our
139 * process actually ends, which closes the socket and gives
140 * them EOF.
142 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 timestamp_t 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_errno("accept failed");
187 return 1;
189 client2 = dup(client);
190 if (client2 < 0) {
191 warning_errno("dup failed");
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 struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
208 int fd;
210 fd = unix_stream_listen(socket_path, &opts);
211 if (fd < 0)
212 die_errno("unable to bind to '%s'", socket_path);
214 printf("ok\n");
215 fclose(stdout);
216 if (!debug) {
217 if (!freopen("/dev/null", "w", stderr))
218 die_errno("unable to point stderr to /dev/null");
221 while (serve_cache_loop(fd))
222 ; /* nothing */
224 close(fd);
227 static const char permissions_advice[] = N_(
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 init_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 } else {
243 * We must be sure to create the directory with the correct mode,
244 * not just chmod it after the fact; otherwise, there is a race
245 * condition in which somebody can chdir to it, sleep, then try to open
246 * our protected socket.
248 if (safe_create_leading_directories_const(dir) < 0)
249 die_errno("unable to create directories for '%s'", dir);
250 if (mkdir(dir, 0700) < 0)
251 die_errno("unable to mkdir '%s'", dir);
254 if (chdir(dir))
256 * We don't actually care what our cwd is; we chdir here just to
257 * be a friendly daemon and avoid tying up our original cwd.
258 * If this fails, it's OK to just continue without that benefit.
262 free(path_copy);
265 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
267 struct tempfile *socket_file;
268 const char *socket_path;
269 int ignore_sighup = 0;
270 static const char *usage[] = {
271 "git credential-cache--daemon [--debug] <socket-path>",
272 NULL
274 int debug = 0;
275 const struct option options[] = {
276 OPT_BOOL(0, "debug", &debug,
277 N_("print debugging messages to stderr")),
278 OPT_END()
281 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
283 argc = parse_options(argc, argv, prefix, options, usage, 0);
284 socket_path = argv[0];
286 if (!socket_path)
287 usage_with_options(usage, options);
289 if (!is_absolute_path(socket_path))
290 die("socket directory must be an absolute path");
292 init_socket_directory(socket_path);
293 socket_file = register_tempfile(socket_path);
295 if (ignore_sighup)
296 signal(SIGHUP, SIG_IGN);
298 serve_cache(socket_path, debug);
299 delete_tempfile(&socket_file);
301 return 0;
304 #else
306 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
308 const char * const usage[] = {
309 "git credential-cache--daemon [--debug] <socket-path>",
311 "credential-cache--daemon is disabled in this build of Git",
312 NULL
314 struct option options[] = { OPT_END() };
316 argc = parse_options(argc, argv, prefix, options, usage, 0);
317 die(_("credential-cache--daemon unavailable; no unix socket support"));
320 #endif /* NO_UNIX_SOCKET */