]>
Commit | Line | Data |
---|---|---|
e2770979 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
9e903316 | 3 | #include "tempfile.h" |
e2770979 JK |
4 | #include "credential.h" |
5 | #include "unix-socket.h" | |
f5e3c0b9 | 6 | #include "parse-options.h" |
e2770979 | 7 | |
9e903316 | 8 | static struct tempfile socket_file; |
e2770979 JK |
9 | |
10 | struct credential_cache_entry { | |
11 | struct credential item; | |
dddbad72 | 12 | timestamp_t expiration; |
e2770979 JK |
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 | ||
dddbad72 | 51 | static timestamp_t check_expirations(void) |
e2770979 | 52 | { |
dddbad72 | 53 | static timestamp_t wait_for_entry_until; |
e2770979 | 54 | int i = 0; |
dddbad72 JS |
55 | timestamp_t now = time(NULL); |
56 | timestamp_t next = TIME_MAX; | |
e2770979 JK |
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 | ||
8f309aeb | 100 | strbuf_getline_lf(&item, fh); |
cf4fff57 | 101 | if (!skip_prefix(item.buf, "action=", &p)) |
e2770979 JK |
102 | return error("client sent bogus action line: %s", item.buf); |
103 | strbuf_addstr(action, p); | |
104 | ||
8f309aeb | 105 | strbuf_getline_lf(&item, fh); |
cf4fff57 | 106 | if (!skip_prefix(item.buf, "timeout=", &p)) |
e2770979 JK |
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 | } | |
7d5e9c98 JK |
130 | else if (!strcmp(action.buf, "exit")) { |
131 | /* | |
132 | * It's important that we clean up our socket first, and then | |
133 | * signal the client only once we have finished the cleanup. | |
134 | * Calling exit() directly does this, because we clean up in | |
135 | * our atexit() handler, and then signal the client when our | |
136 | * process actually ends, which closes the socket and gives | |
137 | * them EOF. | |
138 | */ | |
e2770979 | 139 | exit(0); |
7d5e9c98 | 140 | } |
e2770979 JK |
141 | else if (!strcmp(action.buf, "erase")) |
142 | remove_credential(&c); | |
143 | else if (!strcmp(action.buf, "store")) { | |
144 | if (timeout < 0) | |
145 | warning("cache client didn't specify a timeout"); | |
146 | else if (!c.username || !c.password) | |
147 | warning("cache client gave us a partial credential"); | |
148 | else { | |
149 | remove_credential(&c); | |
150 | cache_credential(&c, timeout); | |
151 | } | |
152 | } | |
153 | else | |
154 | warning("cache client sent unknown action: %s", action.buf); | |
155 | ||
156 | credential_clear(&c); | |
157 | strbuf_release(&action); | |
158 | } | |
159 | ||
160 | static int serve_cache_loop(int fd) | |
161 | { | |
162 | struct pollfd pfd; | |
dddbad72 | 163 | timestamp_t wakeup; |
e2770979 JK |
164 | |
165 | wakeup = check_expirations(); | |
166 | if (!wakeup) | |
167 | return 0; | |
168 | ||
169 | pfd.fd = fd; | |
170 | pfd.events = POLLIN; | |
171 | if (poll(&pfd, 1, 1000 * wakeup) < 0) { | |
172 | if (errno != EINTR) | |
173 | die_errno("poll failed"); | |
174 | return 1; | |
175 | } | |
176 | ||
177 | if (pfd.revents & POLLIN) { | |
178 | int client, client2; | |
179 | FILE *in, *out; | |
180 | ||
181 | client = accept(fd, NULL, NULL); | |
182 | if (client < 0) { | |
26604f9f | 183 | warning_errno("accept failed"); |
e2770979 JK |
184 | return 1; |
185 | } | |
186 | client2 = dup(client); | |
187 | if (client2 < 0) { | |
26604f9f | 188 | warning_errno("dup failed"); |
e2770979 JK |
189 | close(client); |
190 | return 1; | |
191 | } | |
192 | ||
193 | in = xfdopen(client, "r"); | |
194 | out = xfdopen(client2, "w"); | |
195 | serve_one_client(in, out); | |
196 | fclose(in); | |
197 | fclose(out); | |
198 | } | |
199 | return 1; | |
200 | } | |
201 | ||
f5e3c0b9 | 202 | static void serve_cache(const char *socket_path, int debug) |
e2770979 JK |
203 | { |
204 | int fd; | |
205 | ||
206 | fd = unix_stream_listen(socket_path); | |
207 | if (fd < 0) | |
208 | die_errno("unable to bind to '%s'", socket_path); | |
209 | ||
210 | printf("ok\n"); | |
211 | fclose(stdout); | |
f5e3c0b9 JK |
212 | if (!debug) { |
213 | if (!freopen("/dev/null", "w", stderr)) | |
214 | die_errno("unable to point stderr to /dev/null"); | |
215 | } | |
e2770979 JK |
216 | |
217 | while (serve_cache_loop(fd)) | |
218 | ; /* nothing */ | |
219 | ||
220 | close(fd); | |
e2770979 JK |
221 | } |
222 | ||
af64f20b | 223 | static const char permissions_advice[] = N_( |
e2770979 JK |
224 | "The permissions on your socket directory are too loose; other\n" |
225 | "users may be able to read your cached credentials. Consider running:\n" | |
226 | "\n" | |
af64f20b | 227 | " chmod 0700 %s"); |
a6e5e286 | 228 | static void init_socket_directory(const char *path) |
e2770979 JK |
229 | { |
230 | struct stat st; | |
231 | char *path_copy = xstrdup(path); | |
232 | char *dir = dirname(path_copy); | |
233 | ||
234 | if (!stat(dir, &st)) { | |
235 | if (st.st_mode & 077) | |
af64f20b | 236 | die(_(permissions_advice), dir); |
a6e5e286 JG |
237 | } else { |
238 | /* | |
239 | * We must be sure to create the directory with the correct mode, | |
240 | * not just chmod it after the fact; otherwise, there is a race | |
241 | * condition in which somebody can chdir to it, sleep, then try to open | |
242 | * our protected socket. | |
243 | */ | |
244 | if (safe_create_leading_directories_const(dir) < 0) | |
245 | die_errno("unable to create directories for '%s'", dir); | |
246 | if (mkdir(dir, 0700) < 0) | |
247 | die_errno("unable to mkdir '%s'", dir); | |
e2770979 JK |
248 | } |
249 | ||
6e614490 JG |
250 | if (chdir(dir)) |
251 | /* | |
252 | * We don't actually care what our cwd is; we chdir here just to | |
253 | * be a friendly daemon and avoid tying up our original cwd. | |
254 | * If this fails, it's OK to just continue without that benefit. | |
255 | */ | |
256 | ; | |
257 | ||
e2770979 JK |
258 | free(path_copy); |
259 | } | |
260 | ||
3f2e2297 | 261 | int cmd_main(int argc, const char **argv) |
e2770979 | 262 | { |
9e903316 | 263 | const char *socket_path; |
7f4d4746 | 264 | int ignore_sighup = 0; |
f5e3c0b9 JK |
265 | static const char *usage[] = { |
266 | "git-credential-cache--daemon [opts] <socket_path>", | |
267 | NULL | |
268 | }; | |
269 | int debug = 0; | |
270 | const struct option options[] = { | |
271 | OPT_BOOL(0, "debug", &debug, | |
272 | N_("print debugging messages to stderr")), | |
273 | OPT_END() | |
274 | }; | |
275 | ||
7f4d4746 NP |
276 | git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup); |
277 | ||
f5e3c0b9 JK |
278 | argc = parse_options(argc, argv, NULL, options, usage, 0); |
279 | socket_path = argv[0]; | |
e2770979 JK |
280 | |
281 | if (!socket_path) | |
f5e3c0b9 | 282 | usage_with_options(usage, options); |
e2770979 | 283 | |
bd93b8d9 JG |
284 | if (!is_absolute_path(socket_path)) |
285 | die("socket directory must be an absolute path"); | |
286 | ||
a6e5e286 | 287 | init_socket_directory(socket_path); |
9e903316 | 288 | register_tempfile(&socket_file, socket_path); |
7f4d4746 NP |
289 | |
290 | if (ignore_sighup) | |
291 | signal(SIGHUP, SIG_IGN); | |
292 | ||
f5e3c0b9 | 293 | serve_cache(socket_path, debug); |
9e903316 | 294 | delete_tempfile(&socket_file); |
18a3de42 | 295 | |
e2770979 JK |
296 | return 0; |
297 | } |