Merge branch 'aw/doc-smtp-ssl-cert-path'
[git] / credential-cache--daemon.c
1 #include "cache.h"
2 #include "credential.h"
3 #include "unix-socket.h"
4 #include "sigchain.h"
5 #include "parse-options.h"
6
7 static const char *socket_path;
8
9 static void cleanup_socket(void)
10 {
11         if (socket_path)
12                 unlink(socket_path);
13 }
14
15 static void cleanup_socket_on_signal(int sig)
16 {
17         cleanup_socket();
18         sigchain_pop(sig);
19         raise(sig);
20 }
21
22 struct credential_cache_entry {
23         struct credential item;
24         unsigned long expiration;
25 };
26 static struct credential_cache_entry *entries;
27 static int entries_nr;
28 static int entries_alloc;
29
30 static void cache_credential(struct credential *c, int timeout)
31 {
32         struct credential_cache_entry *e;
33
34         ALLOC_GROW(entries, entries_nr + 1, entries_alloc);
35         e = &entries[entries_nr++];
36
37         /* take ownership of pointers */
38         memcpy(&e->item, c, sizeof(*c));
39         memset(c, 0, sizeof(*c));
40         e->expiration = time(NULL) + timeout;
41 }
42
43 static struct credential_cache_entry *lookup_credential(const struct credential *c)
44 {
45         int i;
46         for (i = 0; i < entries_nr; i++) {
47                 struct credential *e = &entries[i].item;
48                 if (credential_match(c, e))
49                         return &entries[i];
50         }
51         return NULL;
52 }
53
54 static void remove_credential(const struct credential *c)
55 {
56         struct credential_cache_entry *e;
57
58         e = lookup_credential(c);
59         if (e)
60                 e->expiration = 0;
61 }
62
63 static int check_expirations(void)
64 {
65         static unsigned long wait_for_entry_until;
66         int i = 0;
67         unsigned long now = time(NULL);
68         unsigned long next = (unsigned long)-1;
69
70         /*
71          * Initially give the client 30 seconds to actually contact us
72          * and store a credential before we decide there's no point in
73          * keeping the daemon around.
74          */
75         if (!wait_for_entry_until)
76                 wait_for_entry_until = now + 30;
77
78         while (i < entries_nr) {
79                 if (entries[i].expiration <= now) {
80                         entries_nr--;
81                         credential_clear(&entries[i].item);
82                         if (i != entries_nr)
83                                 memcpy(&entries[i], &entries[entries_nr], sizeof(*entries));
84                         /*
85                          * Stick around 30 seconds in case a new credential
86                          * shows up (e.g., because we just removed a failed
87                          * one, and we will soon get the correct one).
88                          */
89                         wait_for_entry_until = now + 30;
90                 }
91                 else {
92                         if (entries[i].expiration < next)
93                                 next = entries[i].expiration;
94                         i++;
95                 }
96         }
97
98         if (!entries_nr) {
99                 if (wait_for_entry_until <= now)
100                         return 0;
101                 next = wait_for_entry_until;
102         }
103
104         return next - now;
105 }
106
107 static int read_request(FILE *fh, struct credential *c,
108                         struct strbuf *action, int *timeout) {
109         static struct strbuf item = STRBUF_INIT;
110         const char *p;
111
112         strbuf_getline(&item, fh, '\n');
113         if (!skip_prefix(item.buf, "action=", &p))
114                 return error("client sent bogus action line: %s", item.buf);
115         strbuf_addstr(action, p);
116
117         strbuf_getline(&item, fh, '\n');
118         if (!skip_prefix(item.buf, "timeout=", &p))
119                 return error("client sent bogus timeout line: %s", item.buf);
120         *timeout = atoi(p);
121
122         if (credential_read(c, fh) < 0)
123                 return -1;
124         return 0;
125 }
126
127 static void serve_one_client(FILE *in, FILE *out)
128 {
129         struct credential c = CREDENTIAL_INIT;
130         struct strbuf action = STRBUF_INIT;
131         int timeout = -1;
132
133         if (read_request(in, &c, &action, &timeout) < 0)
134                 /* ignore error */ ;
135         else if (!strcmp(action.buf, "get")) {
136                 struct credential_cache_entry *e = lookup_credential(&c);
137                 if (e) {
138                         fprintf(out, "username=%s\n", e->item.username);
139                         fprintf(out, "password=%s\n", e->item.password);
140                 }
141         }
142         else if (!strcmp(action.buf, "exit"))
143                 exit(0);
144         else if (!strcmp(action.buf, "erase"))
145                 remove_credential(&c);
146         else if (!strcmp(action.buf, "store")) {
147                 if (timeout < 0)
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");
151                 else {
152                         remove_credential(&c);
153                         cache_credential(&c, timeout);
154                 }
155         }
156         else
157                 warning("cache client sent unknown action: %s", action.buf);
158
159         credential_clear(&c);
160         strbuf_release(&action);
161 }
162
163 static int serve_cache_loop(int fd)
164 {
165         struct pollfd pfd;
166         unsigned long wakeup;
167
168         wakeup = check_expirations();
169         if (!wakeup)
170                 return 0;
171
172         pfd.fd = fd;
173         pfd.events = POLLIN;
174         if (poll(&pfd, 1, 1000 * wakeup) < 0) {
175                 if (errno != EINTR)
176                         die_errno("poll failed");
177                 return 1;
178         }
179
180         if (pfd.revents & POLLIN) {
181                 int client, client2;
182                 FILE *in, *out;
183
184                 client = accept(fd, NULL, NULL);
185                 if (client < 0) {
186                         warning("accept failed: %s", strerror(errno));
187                         return 1;
188                 }
189                 client2 = dup(client);
190                 if (client2 < 0) {
191                         warning("dup failed: %s", strerror(errno));
192                         close(client);
193                         return 1;
194                 }
195
196                 in = xfdopen(client, "r");
197                 out = xfdopen(client2, "w");
198                 serve_one_client(in, out);
199                 fclose(in);
200                 fclose(out);
201         }
202         return 1;
203 }
204
205 static void serve_cache(const char *socket_path, int debug)
206 {
207         int fd;
208
209         fd = unix_stream_listen(socket_path);
210         if (fd < 0)
211                 die_errno("unable to bind to '%s'", socket_path);
212
213         printf("ok\n");
214         fclose(stdout);
215         if (!debug) {
216                 if (!freopen("/dev/null", "w", stderr))
217                         die_errno("unable to point stderr to /dev/null");
218         }
219
220         while (serve_cache_loop(fd))
221                 ; /* nothing */
222
223         close(fd);
224         unlink(socket_path);
225 }
226
227 static const char permissions_advice[] =
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"
230 "\n"
231 "       chmod 0700 %s";
232 static void check_socket_directory(const char *path)
233 {
234         struct stat st;
235         char *path_copy = xstrdup(path);
236         char *dir = dirname(path_copy);
237
238         if (!stat(dir, &st)) {
239                 if (st.st_mode & 077)
240                         die(permissions_advice, dir);
241                 free(path_copy);
242                 return;
243         }
244
245         /*
246          * We must be sure to create the directory with the correct mode,
247          * not just chmod it after the fact; otherwise, there is a race
248          * condition in which somebody can chdir to it, sleep, then try to open
249          * our protected socket.
250          */
251         if (safe_create_leading_directories_const(dir) < 0)
252                 die_errno("unable to create directories for '%s'", dir);
253         if (mkdir(dir, 0700) < 0)
254                 die_errno("unable to mkdir '%s'", dir);
255         free(path_copy);
256 }
257
258 int main(int argc, const char **argv)
259 {
260         static const char *usage[] = {
261                 "git-credential-cache--daemon [opts] <socket_path>",
262                 NULL
263         };
264         int debug = 0;
265         const struct option options[] = {
266                 OPT_BOOL(0, "debug", &debug,
267                          N_("print debugging messages to stderr")),
268                 OPT_END()
269         };
270
271         argc = parse_options(argc, argv, NULL, options, usage, 0);
272         socket_path = argv[0];
273
274         if (!socket_path)
275                 usage_with_options(usage, options);
276         check_socket_directory(socket_path);
277
278         atexit(cleanup_socket);
279         sigchain_push_common(cleanup_socket_on_signal);
280
281         serve_cache(socket_path, debug);
282
283         return 0;
284 }