]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/credential-cache--daemon.c
mv: fix error for moving directory to another
[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))
42 return &entries[i];
43 }
44 return NULL;
45 }
46
47 static void remove_credential(const struct credential *c)
48 {
49 struct credential_cache_entry *e;
50
51 e = lookup_credential(c);
52 if (e)
53 e->expiration = 0;
54 }
55
56 static timestamp_t check_expirations(void)
57 {
58 static timestamp_t wait_for_entry_until;
59 int i = 0;
60 timestamp_t now = time(NULL);
61 timestamp_t next = TIME_MAX;
62
63 /*
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.
67 */
68 if (!wait_for_entry_until)
69 wait_for_entry_until = now + 30;
70
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));
77 /*
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).
81 */
82 wait_for_entry_until = now + 30;
83 }
84 else {
85 if (entries[i].expiration < next)
86 next = entries[i].expiration;
87 i++;
88 }
89 }
90
91 if (!entries_nr) {
92 if (wait_for_entry_until <= now)
93 return 0;
94 next = wait_for_entry_until;
95 }
96
97 return next - now;
98 }
99
100 static int read_request(FILE *fh, struct credential *c,
101 struct strbuf *action, int *timeout)
102 {
103 static struct strbuf item = STRBUF_INIT;
104 const char *p;
105
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);
110
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);
115
116 if (credential_read(c, fh) < 0)
117 return -1;
118 return 0;
119 }
120
121 static void serve_one_client(FILE *in, FILE *out)
122 {
123 struct credential c = CREDENTIAL_INIT;
124 struct strbuf action = STRBUF_INIT;
125 int timeout = -1;
126
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);
140 }
141 }
142 else if (!strcmp(action.buf, "exit")) {
143 /*
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.
150 */
151 exit(0);
152 }
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);
163 }
164 }
165 else
166 warning("cache client sent unknown action: %s", action.buf);
167
168 credential_clear(&c);
169 strbuf_release(&action);
170 }
171
172 static int serve_cache_loop(int fd)
173 {
174 struct pollfd pfd;
175 timestamp_t wakeup;
176
177 wakeup = check_expirations();
178 if (!wakeup)
179 return 0;
180
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;
187 }
188
189 if (pfd.revents & POLLIN) {
190 int client, client2;
191 FILE *in, *out;
192
193 client = accept(fd, NULL, NULL);
194 if (client < 0) {
195 warning_errno("accept failed");
196 return 1;
197 }
198 client2 = dup(client);
199 if (client2 < 0) {
200 warning_errno("dup failed");
201 close(client);
202 return 1;
203 }
204
205 in = xfdopen(client, "r");
206 out = xfdopen(client2, "w");
207 serve_one_client(in, out);
208 fclose(in);
209 fclose(out);
210 }
211 return 1;
212 }
213
214 static void serve_cache(const char *socket_path, int debug)
215 {
216 struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
217 int fd;
218
219 fd = unix_stream_listen(socket_path, &opts);
220 if (fd < 0)
221 die_errno("unable to bind to '%s'", socket_path);
222
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");
228 }
229
230 while (serve_cache_loop(fd))
231 ; /* nothing */
232
233 close(fd);
234 }
235
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)
242 {
243 struct stat st;
244 char *path_copy = xstrdup(path);
245 char *dir = dirname(path_copy);
246
247 if (!stat(dir, &st)) {
248 if (st.st_mode & 077)
249 die(_(permissions_advice), dir);
250 } else {
251 /*
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.
256 */
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);
261 }
262
263 if (chdir(dir))
264 /*
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.
268 */
269 ;
270
271 free(path_copy);
272 }
273
274 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
275 {
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
282 };
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()
288 };
289
290 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup);
291
292 argc = parse_options(argc, argv, prefix, options, usage, 0);
293 socket_path = argv[0];
294
295 if (!socket_path)
296 usage_with_options(usage, options);
297
298 if (!is_absolute_path(socket_path))
299 die("socket directory must be an absolute path");
300
301 init_socket_directory(socket_path);
302 socket_file = register_tempfile(socket_path);
303
304 if (ignore_sighup)
305 signal(SIGHUP, SIG_IGN);
306
307 serve_cache(socket_path, debug);
308 delete_tempfile(&socket_file);
309
310 return 0;
311 }
312
313 #else
314
315 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
316 {
317 const char * const usage[] = {
318 "git credential-cache--daemon [--debug] <socket-path>",
319 "",
320 "credential-cache--daemon is disabled in this build of Git",
321 NULL
322 };
323 struct option options[] = { OPT_END() };
324
325 argc = parse_options(argc, argv, prefix, options, usage, 0);
326 die(_("credential-cache--daemon unavailable; no unix socket support"));
327 }
328
329 #endif /* NO_UNIX_SOCKET */