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