3 #include "parse-options.h"
5 #ifndef NO_UNIX_SOCKETS
9 #include "credential.h"
10 #include "unix-socket.h"
12 struct credential_cache_entry
{
13 struct credential item
;
14 timestamp_t expiration
;
16 static struct credential_cache_entry
*entries
;
17 static int entries_nr
;
18 static int entries_alloc
;
20 static void cache_credential(struct credential
*c
, int timeout
)
22 struct credential_cache_entry
*e
;
24 ALLOC_GROW(entries
, entries_nr
+ 1, entries_alloc
);
25 e
= &entries
[entries_nr
++];
27 /* take ownership of pointers */
28 memcpy(&e
->item
, c
, sizeof(*c
));
29 memset(c
, 0, sizeof(*c
));
30 e
->expiration
= time(NULL
) + timeout
;
33 static struct credential_cache_entry
*lookup_credential(const struct credential
*c
)
36 for (i
= 0; i
< entries_nr
; i
++) {
37 struct credential
*e
= &entries
[i
].item
;
38 if (credential_match(c
, e
))
44 static void remove_credential(const struct credential
*c
)
46 struct credential_cache_entry
*e
;
48 e
= lookup_credential(c
);
53 static timestamp_t
check_expirations(void)
55 static timestamp_t wait_for_entry_until
;
57 timestamp_t now
= time(NULL
);
58 timestamp_t next
= TIME_MAX
;
61 * Initially give the client 30 seconds to actually contact us
62 * and store a credential before we decide there's no point in
63 * keeping the daemon around.
65 if (!wait_for_entry_until
)
66 wait_for_entry_until
= now
+ 30;
68 while (i
< entries_nr
) {
69 if (entries
[i
].expiration
<= now
) {
71 credential_clear(&entries
[i
].item
);
73 memcpy(&entries
[i
], &entries
[entries_nr
], sizeof(*entries
));
75 * Stick around 30 seconds in case a new credential
76 * shows up (e.g., because we just removed a failed
77 * one, and we will soon get the correct one).
79 wait_for_entry_until
= now
+ 30;
82 if (entries
[i
].expiration
< next
)
83 next
= entries
[i
].expiration
;
89 if (wait_for_entry_until
<= now
)
91 next
= wait_for_entry_until
;
97 static int read_request(FILE *fh
, struct credential
*c
,
98 struct strbuf
*action
, int *timeout
)
100 static struct strbuf item
= STRBUF_INIT
;
103 strbuf_getline_lf(&item
, fh
);
104 if (!skip_prefix(item
.buf
, "action=", &p
))
105 return error("client sent bogus action line: %s", item
.buf
);
106 strbuf_addstr(action
, p
);
108 strbuf_getline_lf(&item
, fh
);
109 if (!skip_prefix(item
.buf
, "timeout=", &p
))
110 return error("client sent bogus timeout line: %s", item
.buf
);
113 if (credential_read(c
, fh
) < 0)
118 static void serve_one_client(FILE *in
, FILE *out
)
120 struct credential c
= CREDENTIAL_INIT
;
121 struct strbuf action
= STRBUF_INIT
;
124 if (read_request(in
, &c
, &action
, &timeout
) < 0)
126 else if (!strcmp(action
.buf
, "get")) {
127 struct credential_cache_entry
*e
= lookup_credential(&c
);
129 fprintf(out
, "username=%s\n", e
->item
.username
);
130 fprintf(out
, "password=%s\n", e
->item
.password
);
133 else if (!strcmp(action
.buf
, "exit")) {
135 * It's important that we clean up our socket first, and then
136 * signal the client only once we have finished the cleanup.
137 * Calling exit() directly does this, because we clean up in
138 * our atexit() handler, and then signal the client when our
139 * process actually ends, which closes the socket and gives
144 else if (!strcmp(action
.buf
, "erase"))
145 remove_credential(&c
);
146 else if (!strcmp(action
.buf
, "store")) {
148 warning("cache client didn't specify a timeout");
149 else if (!c
.username
|| !c
.password
)
150 warning("cache client gave us a partial credential");
152 remove_credential(&c
);
153 cache_credential(&c
, timeout
);
157 warning("cache client sent unknown action: %s", action
.buf
);
159 credential_clear(&c
);
160 strbuf_release(&action
);
163 static int serve_cache_loop(int fd
)
168 wakeup
= check_expirations();
174 if (poll(&pfd
, 1, 1000 * wakeup
) < 0) {
176 die_errno("poll failed");
180 if (pfd
.revents
& POLLIN
) {
184 client
= accept(fd
, NULL
, NULL
);
186 warning_errno("accept failed");
189 client2
= dup(client
);
191 warning_errno("dup failed");
196 in
= xfdopen(client
, "r");
197 out
= xfdopen(client2
, "w");
198 serve_one_client(in
, out
);
205 static void serve_cache(const char *socket_path
, int debug
)
207 struct unix_stream_listen_opts opts
= UNIX_STREAM_LISTEN_OPTS_INIT
;
210 fd
= unix_stream_listen(socket_path
, &opts
);
212 die_errno("unable to bind to '%s'", socket_path
);
217 if (!freopen("/dev/null", "w", stderr
))
218 die_errno("unable to point stderr to /dev/null");
221 while (serve_cache_loop(fd
))
227 static const char permissions_advice
[] = N_(
228 "The permissions on your socket directory are too loose; other\n"
229 "users may be able to read your cached credentials. Consider running:\n"
232 static void init_socket_directory(const char *path
)
235 char *path_copy
= xstrdup(path
);
236 char *dir
= dirname(path_copy
);
238 if (!stat(dir
, &st
)) {
239 if (st
.st_mode
& 077)
240 die(_(permissions_advice
), dir
);
243 * We must be sure to create the directory with the correct mode,
244 * not just chmod it after the fact; otherwise, there is a race
245 * condition in which somebody can chdir to it, sleep, then try to open
246 * our protected socket.
248 if (safe_create_leading_directories_const(dir
) < 0)
249 die_errno("unable to create directories for '%s'", dir
);
250 if (mkdir(dir
, 0700) < 0)
251 die_errno("unable to mkdir '%s'", dir
);
256 * We don't actually care what our cwd is; we chdir here just to
257 * be a friendly daemon and avoid tying up our original cwd.
258 * If this fails, it's OK to just continue without that benefit.
265 int cmd_credential_cache_daemon(int argc
, const char **argv
, const char *prefix
)
267 struct tempfile
*socket_file
;
268 const char *socket_path
;
269 int ignore_sighup
= 0;
270 static const char *usage
[] = {
271 "git credential-cache--daemon [--debug] <socket-path>",
275 const struct option options
[] = {
276 OPT_BOOL(0, "debug", &debug
,
277 N_("print debugging messages to stderr")),
281 git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup
);
283 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
284 socket_path
= argv
[0];
287 usage_with_options(usage
, options
);
289 if (!is_absolute_path(socket_path
))
290 die("socket directory must be an absolute path");
292 init_socket_directory(socket_path
);
293 socket_file
= register_tempfile(socket_path
);
296 signal(SIGHUP
, SIG_IGN
);
298 serve_cache(socket_path
, debug
);
299 delete_tempfile(&socket_file
);
306 int cmd_credential_cache_daemon(int argc
, const char **argv
, const char *prefix
)
308 const char * const usage
[] = {
309 "git credential-cache--daemon [--debug] <socket-path>",
311 "credential-cache--daemon is disabled in this build of Git",
314 struct option options
[] = { OPT_END() };
316 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
317 die(_("credential-cache--daemon unavailable; no unix socket support"));
320 #endif /* NO_UNIX_SOCKET */