1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "object-file.h"
6 #include "parse-options.h"
8 #ifndef NO_UNIX_SOCKETS
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
)
39 for (i
= 0; i
< entries_nr
; i
++) {
40 struct credential
*e
= &entries
[i
].item
;
41 if (credential_match(c
, e
, 0))
47 static void remove_credential(const struct credential
*c
, int match_password
)
49 struct credential_cache_entry
*e
;
52 for (i
= 0; i
< entries_nr
; i
++) {
54 if (credential_match(c
, &e
->item
, match_password
))
59 static timestamp_t
check_expirations(void)
61 static timestamp_t wait_for_entry_until
;
63 timestamp_t now
= time(NULL
);
64 timestamp_t next
= TIME_MAX
;
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.
71 if (!wait_for_entry_until
)
72 wait_for_entry_until
= now
+ 30;
74 while (i
< entries_nr
) {
75 if (entries
[i
].expiration
<= now
) {
77 credential_clear(&entries
[i
].item
);
79 memcpy(&entries
[i
], &entries
[entries_nr
], sizeof(*entries
));
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).
85 wait_for_entry_until
= now
+ 30;
88 if (entries
[i
].expiration
< next
)
89 next
= entries
[i
].expiration
;
95 if (wait_for_entry_until
<= now
)
97 next
= wait_for_entry_until
;
103 static int read_request(FILE *fh
, struct credential
*c
,
104 struct strbuf
*action
, int *timeout
)
106 static struct strbuf item
= STRBUF_INIT
;
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
);
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
);
119 credential_set_all_capabilities(c
, CREDENTIAL_OP_INITIAL
);
121 if (credential_read(c
, fh
, CREDENTIAL_OP_HELPER
) < 0)
126 static void serve_one_client(FILE *in
, FILE *out
)
128 struct credential c
= CREDENTIAL_INIT
;
129 struct strbuf action
= STRBUF_INIT
;
132 if (read_request(in
, &c
, &action
, &timeout
) < 0)
134 else if (!strcmp(action
.buf
, "get")) {
135 struct credential_cache_entry
*e
= lookup_credential(&c
);
137 e
->item
.capa_authtype
.request_initial
= 1;
138 e
->item
.capa_authtype
.request_helper
= 1;
140 fprintf(out
, "capability[]=authtype\n");
141 if (e
->item
.username
)
142 fprintf(out
, "username=%s\n", e
->item
.username
);
143 if (e
->item
.password
)
144 fprintf(out
, "password=%s\n", e
->item
.password
);
145 if (credential_has_capability(&c
.capa_authtype
, CREDENTIAL_OP_HELPER
) && e
->item
.authtype
)
146 fprintf(out
, "authtype=%s\n", e
->item
.authtype
);
147 if (credential_has_capability(&c
.capa_authtype
, CREDENTIAL_OP_HELPER
) && e
->item
.credential
)
148 fprintf(out
, "credential=%s\n", e
->item
.credential
);
149 if (e
->item
.password_expiry_utc
!= TIME_MAX
)
150 fprintf(out
, "password_expiry_utc=%"PRItime
"\n",
151 e
->item
.password_expiry_utc
);
152 if (e
->item
.oauth_refresh_token
)
153 fprintf(out
, "oauth_refresh_token=%s\n",
154 e
->item
.oauth_refresh_token
);
157 else if (!strcmp(action
.buf
, "exit")) {
159 * It's important that we clean up our socket first, and then
160 * signal the client only once we have finished the cleanup.
161 * Calling exit() directly does this, because we clean up in
162 * our atexit() handler, and then signal the client when our
163 * process actually ends, which closes the socket and gives
168 else if (!strcmp(action
.buf
, "erase"))
169 remove_credential(&c
, 1);
170 else if (!strcmp(action
.buf
, "store")) {
172 warning("cache client didn't specify a timeout");
173 else if ((!c
.username
|| !c
.password
) && (!c
.authtype
&& !c
.credential
))
174 warning("cache client gave us a partial credential");
175 else if (c
.ephemeral
)
176 warning("not storing ephemeral credential");
178 remove_credential(&c
, 0);
179 cache_credential(&c
, timeout
);
183 warning("cache client sent unknown action: %s", action
.buf
);
185 credential_clear(&c
);
186 strbuf_release(&action
);
189 static int serve_cache_loop(int fd
)
194 wakeup
= check_expirations();
200 if (poll(&pfd
, 1, 1000 * wakeup
) < 0) {
202 die_errno("poll failed");
206 if (pfd
.revents
& POLLIN
) {
210 client
= accept(fd
, NULL
, NULL
);
212 warning_errno("accept failed");
215 client2
= dup(client
);
217 warning_errno("dup failed");
222 in
= xfdopen(client
, "r");
223 out
= xfdopen(client2
, "w");
224 serve_one_client(in
, out
);
231 static void serve_cache(const char *socket_path
, int debug
)
233 struct unix_stream_listen_opts opts
= UNIX_STREAM_LISTEN_OPTS_INIT
;
236 fd
= unix_stream_listen(socket_path
, &opts
);
238 die_errno("unable to bind to '%s'", socket_path
);
243 if (!freopen("/dev/null", "w", stderr
))
244 die_errno("unable to point stderr to /dev/null");
247 while (serve_cache_loop(fd
))
253 static const char permissions_advice
[] = N_(
254 "The permissions on your socket directory are too loose; other\n"
255 "users may be able to read your cached credentials. Consider running:\n"
258 static void init_socket_directory(const char *path
)
261 char *path_copy
= xstrdup(path
);
262 char *dir
= dirname(path_copy
);
264 if (!stat(dir
, &st
)) {
265 if (st
.st_mode
& 077)
266 die(_(permissions_advice
), dir
);
269 * We must be sure to create the directory with the correct mode,
270 * not just chmod it after the fact; otherwise, there is a race
271 * condition in which somebody can chdir to it, sleep, then try to open
272 * our protected socket.
274 if (safe_create_leading_directories_const(dir
) < 0)
275 die_errno("unable to create directories for '%s'", dir
);
276 if (mkdir(dir
, 0700) < 0)
277 die_errno("unable to mkdir '%s'", dir
);
282 * We don't actually care what our cwd is; we chdir here just to
283 * be a friendly daemon and avoid tying up our original cwd.
284 * If this fails, it's OK to just continue without that benefit.
291 int cmd_credential_cache_daemon(int argc
,
294 struct repository
*repo UNUSED
)
296 struct tempfile
*socket_file
;
297 const char *socket_path
;
298 int ignore_sighup
= 0;
299 static const char *usage
[] = {
300 "git credential-cache--daemon [--debug] <socket-path>",
304 const struct option options
[] = {
305 OPT_BOOL(0, "debug", &debug
,
306 N_("print debugging messages to stderr")),
310 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup
);
312 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
313 socket_path
= argv
[0];
315 if (!have_unix_sockets())
316 die(_("credential-cache--daemon unavailable; no unix socket support"));
318 usage_with_options(usage
, options
);
320 if (!is_absolute_path(socket_path
))
321 die("socket directory must be an absolute path");
323 init_socket_directory(socket_path
);
324 socket_file
= register_tempfile(socket_path
);
327 signal(SIGHUP
, SIG_IGN
);
329 serve_cache(socket_path
, debug
);
330 delete_tempfile(&socket_file
);
337 int cmd_credential_cache_daemon(int argc
,
340 struct repository
*repo UNUSED
)
342 const char * const usage
[] = {
343 "git credential-cache--daemon [--debug] <socket-path>",
345 "credential-cache--daemon is disabled in this build of Git",
348 struct option options
[] = { OPT_END() };
350 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
351 die(_("credential-cache--daemon unavailable; no unix socket support"));
354 #endif /* NO_UNIX_SOCKET */