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