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
))
47 static void remove_credential(const struct credential
*c
)
49 struct credential_cache_entry
*e
;
51 e
= lookup_credential(c
);
56 static timestamp_t
check_expirations(void)
58 static timestamp_t wait_for_entry_until
;
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
) {
74 credential_clear(&entries
[i
].item
);
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;
85 if (entries
[i
].expiration
< next
)
86 next
= entries
[i
].expiration
;
92 if (wait_for_entry_until
<= now
)
94 next
= wait_for_entry_until
;
100 static int read_request(FILE *fh
, struct credential
*c
,
101 struct strbuf
*action
, int *timeout
)
103 static struct strbuf item
= STRBUF_INIT
;
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
);
116 if (credential_read(c
, fh
) < 0)
121 static void serve_one_client(FILE *in
, FILE *out
)
123 struct credential c
= CREDENTIAL_INIT
;
124 struct strbuf action
= STRBUF_INIT
;
127 if (read_request(in
, &c
, &action
, &timeout
) < 0)
129 else if (!strcmp(action
.buf
, "get")) {
130 struct credential_cache_entry
*e
= lookup_credential(&c
);
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
153 else if (!strcmp(action
.buf
, "erase"))
154 remove_credential(&c
);
155 else if (!strcmp(action
.buf
, "store")) {
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");
161 remove_credential(&c
);
162 cache_credential(&c
, timeout
);
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
)
177 wakeup
= check_expirations();
183 if (poll(&pfd
, 1, 1000 * wakeup
) < 0) {
185 die_errno("poll failed");
189 if (pfd
.revents
& POLLIN
) {
193 client
= accept(fd
, NULL
, NULL
);
195 warning_errno("accept failed");
198 client2
= dup(client
);
200 warning_errno("dup failed");
205 in
= xfdopen(client
, "r");
206 out
= xfdopen(client2
, "w");
207 serve_one_client(in
, out
);
214 static void serve_cache(const char *socket_path
, int debug
)
216 struct unix_stream_listen_opts opts
= UNIX_STREAM_LISTEN_OPTS_INIT
;
219 fd
= unix_stream_listen(socket_path
, &opts
);
221 die_errno("unable to bind to '%s'", socket_path
);
226 if (!freopen("/dev/null", "w", stderr
))
227 die_errno("unable to point stderr to /dev/null");
230 while (serve_cache_loop(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"
241 static void init_socket_directory(const char *path
)
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
);
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
);
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.
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>",
284 const struct option options
[] = {
285 OPT_BOOL(0, "debug", &debug
,
286 N_("print debugging messages to stderr")),
290 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup
);
292 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
293 socket_path
= argv
[0];
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
);
305 signal(SIGHUP
, SIG_IGN
);
307 serve_cache(socket_path
, debug
);
308 delete_tempfile(&socket_file
);
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",
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 */