msysGit-based Git for Windows 1.x was superseded by Git for Windows 2.x
[git/mingw/4msysgit.git] / credential-cache--daemon.c
blob3b370ca5e529c74e17e0f39da5c689ebc4ccfc25
1 #include "cache.h"
2 #include "credential.h"
3 #include "unix-socket.h"
4 #include "sigchain.h"
6 static const char *socket_path;
8 static void cleanup_socket(void)
10 if (socket_path)
11 unlink(socket_path);
14 static void cleanup_socket_on_signal(int sig)
16 cleanup_socket();
17 sigchain_pop(sig);
18 raise(sig);
21 struct credential_cache_entry {
22 struct credential item;
23 unsigned long expiration;
25 static struct credential_cache_entry *entries;
26 static int entries_nr;
27 static int entries_alloc;
29 static void cache_credential(struct credential *c, int timeout)
31 struct credential_cache_entry *e;
33 ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
34 e = &entries[entries_nr++];
36 /* take ownership of pointers */
37 memcpy(&e->item, c, sizeof(*c));
38 memset(c, 0, sizeof(*c));
39 e->expiration = time(NULL) + timeout;
42 static struct credential_cache_entry *lookup_credential(const struct credential *c)
44 int i;
45 for (i = 0; i < entries_nr; i++) {
46 struct credential *e = &entries[i].item;
47 if (credential_match(c, e))
48 return &entries[i];
50 return NULL;
53 static void remove_credential(const struct credential *c)
55 struct credential_cache_entry *e;
57 e = lookup_credential(c);
58 if (e)
59 e->expiration = 0;
62 static int check_expirations(void)
64 static unsigned long wait_for_entry_until;
65 int i = 0;
66 unsigned long now = time(NULL);
67 unsigned long next = (unsigned long)-1;
70 * Initially give the client 30 seconds to actually contact us
71 * and store a credential before we decide there's no point in
72 * keeping the daemon around.
74 if (!wait_for_entry_until)
75 wait_for_entry_until = now + 30;
77 while (i < entries_nr) {
78 if (entries[i].expiration <= now) {
79 entries_nr--;
80 credential_clear(&entries[i].item);
81 if (i != entries_nr)
82 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
84 * Stick around 30 seconds in case a new credential
85 * shows up (e.g., because we just removed a failed
86 * one, and we will soon get the correct one).
88 wait_for_entry_until = now + 30;
90 else {
91 if (entries[i].expiration < next)
92 next = entries[i].expiration;
93 i++;
97 if (!entries_nr) {
98 if (wait_for_entry_until <= now)
99 return 0;
100 next = wait_for_entry_until;
103 return next - now;
106 static int read_request(FILE *fh, struct credential *c,
107 struct strbuf *action, int *timeout) {
108 static struct strbuf item = STRBUF_INIT;
109 const char *p;
111 strbuf_getline(&item, fh, '\n');
112 if (!skip_prefix(item.buf, "action=", &p))
113 return error("client sent bogus action line: %s", item.buf);
114 strbuf_addstr(action, p);
116 strbuf_getline(&item, fh, '\n');
117 if (!skip_prefix(item.buf, "timeout=", &p))
118 return error("client sent bogus timeout line: %s", item.buf);
119 *timeout = atoi(p);
121 if (credential_read(c, fh) < 0)
122 return -1;
123 return 0;
126 static void serve_one_client(FILE *in, FILE *out)
128 struct credential c = CREDENTIAL_INIT;
129 struct strbuf action = STRBUF_INIT;
130 int timeout = -1;
132 if (read_request(in, &c, &action, &timeout) < 0)
133 /* ignore error */ ;
134 else if (!strcmp(action.buf, "get")) {
135 struct credential_cache_entry *e = lookup_credential(&c);
136 if (e) {
137 fprintf(out, "username=%s\n", e->item.username);
138 fprintf(out, "password=%s\n", e->item.password);
141 else if (!strcmp(action.buf, "exit"))
142 exit(0);
143 else if (!strcmp(action.buf, "erase"))
144 remove_credential(&c);
145 else if (!strcmp(action.buf, "store")) {
146 if (timeout < 0)
147 warning("cache client didn't specify a timeout");
148 else if (!c.username || !c.password)
149 warning("cache client gave us a partial credential");
150 else {
151 remove_credential(&c);
152 cache_credential(&c, timeout);
155 else
156 warning("cache client sent unknown action: %s", action.buf);
158 credential_clear(&c);
159 strbuf_release(&action);
162 static int serve_cache_loop(int fd)
164 struct pollfd pfd;
165 unsigned long wakeup;
167 wakeup = check_expirations();
168 if (!wakeup)
169 return 0;
171 pfd.fd = fd;
172 pfd.events = POLLIN;
173 if (poll(&pfd, 1, 1000 * wakeup) < 0) {
174 if (errno != EINTR)
175 die_errno("poll failed");
176 return 1;
179 if (pfd.revents & POLLIN) {
180 int client, client2;
181 FILE *in, *out;
183 client = accept(fd, NULL, NULL);
184 if (client < 0) {
185 warning("accept failed: %s", strerror(errno));
186 return 1;
188 client2 = dup(client);
189 if (client2 < 0) {
190 warning("dup failed: %s", strerror(errno));
191 close(client);
192 return 1;
195 in = xfdopen(client, "r");
196 out = xfdopen(client2, "w");
197 serve_one_client(in, out);
198 fclose(in);
199 fclose(out);
201 return 1;
204 static void serve_cache(const char *socket_path)
206 int fd;
208 fd = unix_stream_listen(socket_path);
209 if (fd < 0)
210 die_errno("unable to bind to '%s'", socket_path);
212 printf("ok\n");
213 fclose(stdout);
215 while (serve_cache_loop(fd))
216 ; /* nothing */
218 close(fd);
219 unlink(socket_path);
222 static const char permissions_advice[] =
223 "The permissions on your socket directory are too loose; other\n"
224 "users may be able to read your cached credentials. Consider running:\n"
225 "\n"
226 " chmod 0700 %s";
227 static void check_socket_directory(const char *path)
229 struct stat st;
230 char *path_copy = xstrdup(path);
231 char *dir = dirname(path_copy);
233 if (!stat(dir, &st)) {
234 if (st.st_mode & 077)
235 die(permissions_advice, dir);
236 free(path_copy);
237 return;
241 * We must be sure to create the directory with the correct mode,
242 * not just chmod it after the fact; otherwise, there is a race
243 * condition in which somebody can chdir to it, sleep, then try to open
244 * our protected socket.
246 if (safe_create_leading_directories_const(dir) < 0)
247 die_errno("unable to create directories for '%s'", dir);
248 if (mkdir(dir, 0700) < 0)
249 die_errno("unable to mkdir '%s'", dir);
250 free(path_copy);
253 int main(int argc, const char **argv)
255 socket_path = argv[1];
257 if (!socket_path)
258 die("usage: git-credential-cache--daemon <socket_path>");
259 check_socket_directory(socket_path);
261 atexit(cleanup_socket);
262 sigchain_push_common(cleanup_socket_on_signal);
264 serve_cache(socket_path);
266 return 0;