Merge branch 'mh/use-wincred-from-system'
[alt-git.git] / builtin / credential-cache--daemon.c
blob756c5f02aef56ef4f6601729154c16dddc358810
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "alloc.h"
4 #include "gettext.h"
5 #include "object-file.h"
6 #include "parse-options.h"
8 #ifndef NO_UNIX_SOCKETS
10 #include "config.h"
11 #include "tempfile.h"
12 #include "credential.h"
13 #include "unix-socket.h"
15 struct credential_cache_entry {
16 struct credential item;
17 timestamp_t expiration;
19 static struct credential_cache_entry *entries;
20 static int entries_nr;
21 static int entries_alloc;
23 static void cache_credential(struct credential *c, int timeout)
25 struct credential_cache_entry *e;
27 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
28 e = &entries[entries_nr++];
30 /* take ownership of pointers */
31 memcpy(&e->item, c, sizeof(*c));
32 memset(c, 0, sizeof(*c));
33 e->expiration = time(NULL) + timeout;
36 static struct credential_cache_entry *lookup_credential(const struct credential *c)
38 int i;
39 for (i = 0; i < entries_nr; i++) {
40 struct credential *e = &entries[i].item;
41 if (credential_match(c, e))
42 return &entries[i];
44 return NULL;
47 static void remove_credential(const struct credential *c)
49 struct credential_cache_entry *e;
51 e = lookup_credential(c);
52 if (e)
53 e->expiration = 0;
56 static timestamp_t check_expirations(void)
58 static timestamp_t wait_for_entry_until;
59 int i = 0;
60 timestamp_t now = time(NULL);
61 timestamp_t next = TIME_MAX;
64 * Initially give the client 30 seconds to actually contact us
65 * and store a credential before we decide there's no point in
66 * keeping the daemon around.
68 if (!wait_for_entry_until)
69 wait_for_entry_until = now + 30;
71 while (i < entries_nr) {
72 if (entries[i].expiration <= now) {
73 entries_nr--;
74 credential_clear(&entries[i].item);
75 if (i != entries_nr)
76 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
78 * Stick around 30 seconds in case a new credential
79 * shows up (e.g., because we just removed a failed
80 * one, and we will soon get the correct one).
82 wait_for_entry_until = now + 30;
84 else {
85 if (entries[i].expiration < next)
86 next = entries[i].expiration;
87 i++;
91 if (!entries_nr) {
92 if (wait_for_entry_until <= now)
93 return 0;
94 next = wait_for_entry_until;
97 return next - now;
100 static int read_request(FILE *fh, struct credential *c,
101 struct strbuf *action, int *timeout)
103 static struct strbuf item = STRBUF_INIT;
104 const char *p;
106 strbuf_getline_lf(&item, fh);
107 if (!skip_prefix(item.buf, "action=", &p))
108 return error("client sent bogus action line: %s", item.buf);
109 strbuf_addstr(action, p);
111 strbuf_getline_lf(&item, fh);
112 if (!skip_prefix(item.buf, "timeout=", &p))
113 return error("client sent bogus timeout line: %s", item.buf);
114 *timeout = atoi(p);
116 if (credential_read(c, fh) < 0)
117 return -1;
118 return 0;
121 static void serve_one_client(FILE *in, FILE *out)
123 struct credential c = CREDENTIAL_INIT;
124 struct strbuf action = STRBUF_INIT;
125 int timeout = -1;
127 if (read_request(in, &c, &action, &timeout) < 0)
128 /* ignore error */ ;
129 else if (!strcmp(action.buf, "get")) {
130 struct credential_cache_entry *e = lookup_credential(&c);
131 if (e) {
132 fprintf(out, "username=%s\n", e->item.username);
133 fprintf(out, "password=%s\n", e->item.password);
134 if (e->item.password_expiry_utc != TIME_MAX)
135 fprintf(out, "password_expiry_utc=%"PRItime"\n",
136 e->item.password_expiry_utc);
137 if (e->item.oauth_refresh_token)
138 fprintf(out, "oauth_refresh_token=%s\n",
139 e->item.oauth_refresh_token);
142 else if (!strcmp(action.buf, "exit")) {
144 * It's important that we clean up our socket first, and then
145 * signal the client only once we have finished the cleanup.
146 * Calling exit() directly does this, because we clean up in
147 * our atexit() handler, and then signal the client when our
148 * process actually ends, which closes the socket and gives
149 * them EOF.
151 exit(0);
153 else if (!strcmp(action.buf, "erase"))
154 remove_credential(&c);
155 else if (!strcmp(action.buf, "store")) {
156 if (timeout < 0)
157 warning("cache client didn't specify a timeout");
158 else if (!c.username || !c.password)
159 warning("cache client gave us a partial credential");
160 else {
161 remove_credential(&c);
162 cache_credential(&c, timeout);
165 else
166 warning("cache client sent unknown action: %s", action.buf);
168 credential_clear(&c);
169 strbuf_release(&action);
172 static int serve_cache_loop(int fd)
174 struct pollfd pfd;
175 timestamp_t wakeup;
177 wakeup = check_expirations();
178 if (!wakeup)
179 return 0;
181 pfd.fd = fd;
182 pfd.events = POLLIN;
183 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
184 if (errno != EINTR)
185 die_errno("poll failed");
186 return 1;
189 if (pfd.revents & POLLIN) {
190 int client, client2;
191 FILE *in, *out;
193 client = accept(fd, NULL, NULL);
194 if (client < 0) {
195 warning_errno("accept failed");
196 return 1;
198 client2 = dup(client);
199 if (client2 < 0) {
200 warning_errno("dup failed");
201 close(client);
202 return 1;
205 in = xfdopen(client, "r");
206 out = xfdopen(client2, "w");
207 serve_one_client(in, out);
208 fclose(in);
209 fclose(out);
211 return 1;
214 static void serve_cache(const char *socket_path, int debug)
216 struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
217 int fd;
219 fd = unix_stream_listen(socket_path, &opts);
220 if (fd < 0)
221 die_errno("unable to bind to '%s'", socket_path);
223 printf("ok\n");
224 fclose(stdout);
225 if (!debug) {
226 if (!freopen("/dev/null", "w", stderr))
227 die_errno("unable to point stderr to /dev/null");
230 while (serve_cache_loop(fd))
231 ; /* nothing */
233 close(fd);
236 static const char permissions_advice[] = N_(
237 "The permissions on your socket directory are too loose; other\n"
238 "users may be able to read your cached credentials. Consider running:\n"
239 "\n"
240 " chmod 0700 %s");
241 static void init_socket_directory(const char *path)
243 struct stat st;
244 char *path_copy = xstrdup(path);
245 char *dir = dirname(path_copy);
247 if (!stat(dir, &st)) {
248 if (st.st_mode & 077)
249 die(_(permissions_advice), dir);
250 } else {
252 * We must be sure to create the directory with the correct mode,
253 * not just chmod it after the fact; otherwise, there is a race
254 * condition in which somebody can chdir to it, sleep, then try to open
255 * our protected socket.
257 if (safe_create_leading_directories_const(dir) < 0)
258 die_errno("unable to create directories for '%s'", dir);
259 if (mkdir(dir, 0700) < 0)
260 die_errno("unable to mkdir '%s'", dir);
263 if (chdir(dir))
265 * We don't actually care what our cwd is; we chdir here just to
266 * be a friendly daemon and avoid tying up our original cwd.
267 * If this fails, it's OK to just continue without that benefit.
271 free(path_copy);
274 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
276 struct tempfile *socket_file;
277 const char *socket_path;
278 int ignore_sighup = 0;
279 static const char *usage[] = {
280 "git credential-cache--daemon [--debug] <socket-path>",
281 NULL
283 int debug = 0;
284 const struct option options[] = {
285 OPT_BOOL(0, "debug", &debug,
286 N_("print debugging messages to stderr")),
287 OPT_END()
290 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
292 argc = parse_options(argc, argv, prefix, options, usage, 0);
293 socket_path = argv[0];
295 if (!socket_path)
296 usage_with_options(usage, options);
298 if (!is_absolute_path(socket_path))
299 die("socket directory must be an absolute path");
301 init_socket_directory(socket_path);
302 socket_file = register_tempfile(socket_path);
304 if (ignore_sighup)
305 signal(SIGHUP, SIG_IGN);
307 serve_cache(socket_path, debug);
308 delete_tempfile(&socket_file);
310 return 0;
313 #else
315 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
317 const char * const usage[] = {
318 "git credential-cache--daemon [--debug] <socket-path>",
320 "credential-cache--daemon is disabled in this build of Git",
321 NULL
323 struct option options[] = { OPT_END() };
325 argc = parse_options(argc, argv, prefix, options, usage, 0);
326 die(_("credential-cache--daemon unavailable; no unix socket support"));
329 #endif /* NO_UNIX_SOCKET */