]> git.ipfire.org Git - thirdparty/git.git/blame - daemon.c
git-repack: Properly abort in corrupt repository
[thirdparty/git.git] / daemon.c
CommitLineData
eaa94919
LT
1#include <signal.h>
2#include <sys/wait.h>
a87e8be2 3#include <sys/socket.h>
3cd6ecda 4#include <sys/time.h>
6573faff 5#include <sys/poll.h>
df076bdb 6#include <netdb.h>
a87e8be2 7#include <netinet/in.h>
f8ff0c06 8#include <arpa/inet.h>
9048fe1c 9#include <syslog.h>
979e32fa
RS
10#include "pkt-line.h"
11#include "cache.h"
f8ff0c06 12
9048fe1c 13static int log_syslog;
f8ff0c06
PB
14static int verbose;
15
960deccb
PA
16static const char daemon_usage[] =
17"git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
4dbd1352 18" [--timeout=n] [--init-timeout=n] [--strict-paths] [directory...]";
4ae95682
PA
19
20/* List of acceptable pathname prefixes */
21static char **ok_paths = NULL;
4dbd1352 22static int strict_paths = 0;
4ae95682
PA
23
24/* If this is set, git-daemon-export-ok is not required */
25static int export_all_trees = 0;
f8ff0c06 26
960deccb
PA
27/* Timeout, and initial timeout */
28static unsigned int timeout = 0;
29static unsigned int init_timeout = 0;
f8ff0c06 30
9048fe1c 31static void logreport(int priority, const char *err, va_list params)
f8ff0c06
PB
32{
33 /* We should do a single write so that it is atomic and output
34 * of several processes do not get intermingled. */
35 char buf[1024];
36 int buflen;
37 int maxlen, msglen;
38
39 /* sizeof(buf) should be big enough for "[pid] \n" */
1bedd4ca 40 buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
f8ff0c06
PB
41
42 maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
43 msglen = vsnprintf(buf + buflen, maxlen, err, params);
44
9048fe1c
PB
45 if (log_syslog) {
46 syslog(priority, "%s", buf);
47 return;
48 }
49
f8ff0c06
PB
50 /* maxlen counted our own LF but also counts space given to
51 * vsnprintf for the terminating NUL. We want to make sure that
52 * we have space for our own LF and NUL after the "meat" of the
53 * message, so truncate it at maxlen - 1.
54 */
55 if (msglen > maxlen - 1)
56 msglen = maxlen - 1;
57 else if (msglen < 0)
58 msglen = 0; /* Protect against weird return values. */
59 buflen += msglen;
60
61 buf[buflen++] = '\n';
62 buf[buflen] = '\0';
63
64 write(2, buf, buflen);
65}
66
cdda4745 67static void logerror(const char *err, ...)
f8ff0c06
PB
68{
69 va_list params;
70 va_start(params, err);
9048fe1c 71 logreport(LOG_ERR, err, params);
f8ff0c06
PB
72 va_end(params);
73}
74
cdda4745 75static void loginfo(const char *err, ...)
f8ff0c06
PB
76{
77 va_list params;
78 if (!verbose)
79 return;
80 va_start(params, err);
9048fe1c 81 logreport(LOG_INFO, err, params);
f8ff0c06
PB
82 va_end(params);
83}
a87e8be2 84
4dbd1352 85static char *path_ok(char *dir)
4ae95682 86{
4dbd1352 87 char *path = enter_repo(dir, strict_paths);
3e04c62d 88
4dbd1352
AE
89 if (!path) {
90 logerror("'%s': unable to chdir or not a git archive", dir);
91 return NULL;
4ae95682
PA
92 }
93
94 if ( ok_paths && *ok_paths ) {
ce335fe0 95 char **pp;
4dbd1352 96 int pathlen = strlen(path);
4ae95682 97
ce335fe0
JH
98 /* The validation is done on the paths after enter_repo
99 * canonicalization, so whitelist should be written in
100 * terms of real pathnames (i.e. after ~user is expanded
101 * and symlinks resolved).
102 */
4ae95682
PA
103 for ( pp = ok_paths ; *pp ; pp++ ) {
104 int len = strlen(*pp);
ce335fe0
JH
105 if (len <= pathlen &&
106 !memcmp(*pp, path, len) &&
107 (path[len] == '\0' ||
108 (!strict_paths && path[len] == '/')))
109 return path;
4ae95682 110 }
4dbd1352
AE
111 }
112 else {
113 /* be backwards compatible */
114 if (!strict_paths)
115 return path;
4ae95682
PA
116 }
117
4dbd1352
AE
118 logerror("'%s': not in whitelist", path);
119 return NULL; /* Fallthrough. Deny by default */
4ae95682 120}
a87e8be2 121
4dbd1352 122static int upload(char *dir)
a87e8be2 123{
4dbd1352
AE
124 /* Timeout as string */
125 char timeout_buf[64];
126 const char *path;
127
128 loginfo("Request for '%s'", dir);
4ae95682 129
4dbd1352 130 if (!(path = path_ok(dir)))
a87e8be2 131 return -1;
47888f0f 132
a87e8be2
LT
133 /*
134 * Security on the cheap.
135 *
a935c397 136 * We want a readable HEAD, usable "objects" directory, and
a87e8be2
LT
137 * a "git-daemon-export-ok" flag that says that the other side
138 * is ok with us doing this.
4dbd1352
AE
139 *
140 * path_ok() uses enter_repo() and does whitelist checking.
141 * We only need to make sure the repository is exported.
a87e8be2 142 */
4dbd1352 143
3e04c62d 144 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
4dbd1352 145 logerror("'%s': repository not exported.", path);
3e04c62d
PA
146 errno = EACCES;
147 return -1;
148 }
149
02d57da4
LT
150 /*
151 * We'll ignore SIGTERM from now on, we have a
152 * good client.
153 */
154 signal(SIGTERM, SIG_IGN);
155
960deccb
PA
156 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
157
a87e8be2 158 /* git-upload-pack only ever reads stuff, so this is safe */
ce335fe0 159 execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL);
a87e8be2
LT
160 return -1;
161}
162
7d80694a 163static int execute(void)
a87e8be2 164{
7d80694a
LT
165 static char line[1000];
166 int len;
167
960deccb 168 alarm(init_timeout ? init_timeout : timeout);
7d80694a 169 len = packet_read_line(0, line, sizeof(line));
960deccb 170 alarm(0);
7d80694a
LT
171
172 if (len && line[len-1] == '\n')
173 line[--len] = 0;
174
4dbd1352 175 if (!strncmp("git-upload-pack ", line, 16))
3e04c62d 176 return upload(line+16);
a87e8be2 177
f8ff0c06 178 logerror("Protocol error: '%s'", line);
a87e8be2
LT
179 return -1;
180}
181
66e631de
LT
182
183/*
184 * We count spawned/reaped separately, just to avoid any
185 * races when updating them from signals. The SIGCHLD handler
186 * will only update children_reaped, and the fork logic will
187 * only update children_spawned.
188 *
189 * MAX_CHILDREN should be a power-of-two to make the modulus
190 * operation cheap. It should also be at least twice
191 * the maximum number of connections we will ever allow.
192 */
193#define MAX_CHILDREN 128
194
195static int max_connections = 25;
196
197/* These are updated by the signal handler */
198static volatile unsigned int children_reaped = 0;
4d8fa916 199static pid_t dead_child[MAX_CHILDREN];
66e631de
LT
200
201/* These are updated by the main loop */
202static unsigned int children_spawned = 0;
203static unsigned int children_deleted = 0;
204
4d8fa916 205static struct child {
66e631de 206 pid_t pid;
7fa09084 207 int addrlen;
df076bdb 208 struct sockaddr_storage address;
66e631de
LT
209} live_child[MAX_CHILDREN];
210
7fa09084 211static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen)
66e631de
LT
212{
213 live_child[idx].pid = pid;
214 live_child[idx].addrlen = addrlen;
df076bdb 215 memcpy(&live_child[idx].address, addr, addrlen);
66e631de
LT
216}
217
218/*
219 * Walk from "deleted" to "spawned", and remove child "pid".
220 *
221 * We move everything up by one, since the new "deleted" will
222 * be one higher.
223 */
224static void remove_child(pid_t pid, unsigned deleted, unsigned spawned)
225{
226 struct child n;
227
228 deleted %= MAX_CHILDREN;
229 spawned %= MAX_CHILDREN;
230 if (live_child[deleted].pid == pid) {
231 live_child[deleted].pid = -1;
232 return;
233 }
234 n = live_child[deleted];
235 for (;;) {
236 struct child m;
237 deleted = (deleted + 1) % MAX_CHILDREN;
238 if (deleted == spawned)
239 die("could not find dead child %d\n", pid);
240 m = live_child[deleted];
241 live_child[deleted] = n;
242 if (m.pid == pid)
243 return;
244 n = m;
245 }
246}
247
248/*
249 * This gets called if the number of connections grows
250 * past "max_connections".
251 *
252 * We _should_ start off by searching for connections
253 * from the same IP, and if there is some address wth
254 * multiple connections, we should kill that first.
255 *
256 * As it is, we just "randomly" kill 25% of the connections,
257 * and our pseudo-random generator sucks too. I have no
258 * shame.
259 *
260 * Really, this is just a place-holder for a _real_ algorithm.
261 */
02d57da4 262static void kill_some_children(int signo, unsigned start, unsigned stop)
66e631de
LT
263{
264 start %= MAX_CHILDREN;
265 stop %= MAX_CHILDREN;
266 while (start != stop) {
267 if (!(start & 3))
02d57da4 268 kill(live_child[start].pid, signo);
66e631de
LT
269 start = (start + 1) % MAX_CHILDREN;
270 }
271}
272
02d57da4 273static void check_max_connections(void)
a87e8be2 274{
02d57da4 275 for (;;) {
eaa94919 276 int active;
66e631de 277 unsigned spawned, reaped, deleted;
eaa94919 278
66e631de 279 spawned = children_spawned;
66e631de
LT
280 reaped = children_reaped;
281 deleted = children_deleted;
282
283 while (deleted < reaped) {
284 pid_t pid = dead_child[deleted % MAX_CHILDREN];
285 remove_child(pid, deleted, spawned);
286 deleted++;
287 }
288 children_deleted = deleted;
289
290 active = spawned - deleted;
02d57da4
LT
291 if (active <= max_connections)
292 break;
66e631de 293
02d57da4
LT
294 /* Kill some unstarted connections with SIGTERM */
295 kill_some_children(SIGTERM, deleted, spawned);
296 if (active <= max_connections << 1)
297 break;
298
299 /* If the SIGTERM thing isn't helping use SIGKILL */
300 kill_some_children(SIGKILL, deleted, spawned);
301 sleep(1);
302 }
303}
304
7fa09084 305static void handle(int incoming, struct sockaddr *addr, int addrlen)
02d57da4
LT
306{
307 pid_t pid = fork();
f8ff0c06
PB
308 char addrbuf[256] = "";
309 int port = -1;
02d57da4
LT
310
311 if (pid) {
312 unsigned idx;
313
314 close(incoming);
315 if (pid < 0)
316 return;
317
318 idx = children_spawned % MAX_CHILDREN;
319 children_spawned++;
320 add_child(idx, pid, addr, addrlen);
66e631de 321
02d57da4 322 check_max_connections();
a87e8be2
LT
323 return;
324 }
325
326 dup2(incoming, 0);
327 dup2(incoming, 1);
328 close(incoming);
f8ff0c06
PB
329
330 if (addr->sa_family == AF_INET) {
331 struct sockaddr_in *sin_addr = (void *) addr;
332 inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
333 port = sin_addr->sin_port;
334
6573faff 335#ifndef NO_IPV6
f8ff0c06
PB
336 } else if (addr->sa_family == AF_INET6) {
337 struct sockaddr_in6 *sin6_addr = (void *) addr;
338
339 char *buf = addrbuf;
340 *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
341 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
342 strcat(buf, "]");
343
344 port = sin6_addr->sin6_port;
6573faff 345#endif
f8ff0c06 346 }
da38641d 347 loginfo("Connection from %s:%d", addrbuf, port);
f8ff0c06 348
7d80694a 349 exit(execute());
a87e8be2
LT
350}
351
eaa94919
LT
352static void child_handler(int signo)
353{
354 for (;;) {
9048fe1c
PB
355 int status;
356 pid_t pid = waitpid(-1, &status, WNOHANG);
66e631de
LT
357
358 if (pid > 0) {
359 unsigned reaped = children_reaped;
360 dead_child[reaped % MAX_CHILDREN] = pid;
361 children_reaped = reaped + 1;
f8ff0c06 362 /* XXX: Custom logging, since we don't wanna getpid() */
9048fe1c
PB
363 if (verbose) {
364 char *dead = "";
365 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
366 dead = " (with error)";
367 if (log_syslog)
368 syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead);
369 else
370 fprintf(stderr, "[%d] Disconnected%s\n", pid, dead);
371 }
eaa94919
LT
372 continue;
373 }
374 break;
375 }
376}
377
6573faff
PA
378#ifndef NO_IPV6
379
380static int socksetup(int port, int **socklist_p)
a87e8be2 381{
df076bdb
YH
382 int socknum = 0, *socklist = NULL;
383 int maxfd = -1;
df076bdb 384 char pbuf[NI_MAXSERV];
a87e8be2 385
6573faff
PA
386 struct addrinfo hints, *ai0, *ai;
387 int gai;
df076bdb
YH
388
389 sprintf(pbuf, "%d", port);
390 memset(&hints, 0, sizeof(hints));
391 hints.ai_family = AF_UNSPEC;
392 hints.ai_socktype = SOCK_STREAM;
393 hints.ai_protocol = IPPROTO_TCP;
394 hints.ai_flags = AI_PASSIVE;
395
396 gai = getaddrinfo(NULL, pbuf, &hints, &ai0);
397 if (gai)
398 die("getaddrinfo() failed: %s\n", gai_strerror(gai));
399
df076bdb
YH
400 for (ai = ai0; ai; ai = ai->ai_next) {
401 int sockfd;
402 int *newlist;
403
404 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
405 if (sockfd < 0)
406 continue;
407 if (sockfd >= FD_SETSIZE) {
408 error("too large socket descriptor.");
409 close(sockfd);
410 continue;
411 }
412
413#ifdef IPV6_V6ONLY
414 if (ai->ai_family == AF_INET6) {
415 int on = 1;
416 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
417 &on, sizeof(on));
418 /* Note: error is not fatal */
419 }
420#endif
421
422 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
423 close(sockfd);
424 continue; /* not fatal */
425 }
426 if (listen(sockfd, 5) < 0) {
427 close(sockfd);
428 continue; /* not fatal */
429 }
430
431 newlist = realloc(socklist, sizeof(int) * (socknum + 1));
432 if (!newlist)
433 die("memory allocation failed: %s", strerror(errno));
434
435 socklist = newlist;
436 socklist[socknum++] = sockfd;
437
df076bdb
YH
438 if (maxfd < sockfd)
439 maxfd = sockfd;
440 }
441
442 freeaddrinfo(ai0);
443
6573faff
PA
444 *socklist_p = socklist;
445 return socknum;
446}
447
448#else /* NO_IPV6 */
449
450static int socksetup(int port, int **socklist_p)
451{
452 struct sockaddr_in sin;
453 int sockfd;
454
455 sockfd = socket(AF_INET, SOCK_STREAM, 0);
456 if (sockfd < 0)
457 return 0;
458
459 memset(&sin, 0, sizeof sin);
460 sin.sin_family = AF_INET;
461 sin.sin_addr.s_addr = htonl(INADDR_ANY);
462 sin.sin_port = htons(port);
463
464 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
465 close(sockfd);
466 return 0;
467 }
a87e8be2 468
1b4713fb 469 *socklist_p = xmalloc(sizeof(int));
6573faff
PA
470 **socklist_p = sockfd;
471}
472
473#endif
474
475static int service_loop(int socknum, int *socklist)
476{
477 struct pollfd *pfd;
478 int i;
479
1b4713fb 480 pfd = xcalloc(socknum, sizeof(struct pollfd));
6573faff
PA
481
482 for (i = 0; i < socknum; i++) {
483 pfd[i].fd = socklist[i];
484 pfd[i].events = POLLIN;
485 }
9220282a
PA
486
487 signal(SIGCHLD, child_handler);
a87e8be2
LT
488
489 for (;;) {
df076bdb 490 int i;
6573faff 491
7872e055 492 if (poll(pfd, socknum, -1) < 0) {
1eef0b33 493 if (errno != EINTR) {
6573faff 494 error("poll failed, resuming: %s",
1eef0b33
JH
495 strerror(errno));
496 sleep(1);
497 }
df076bdb
YH
498 continue;
499 }
500
501 for (i = 0; i < socknum; i++) {
6573faff 502 if (pfd[i].revents & POLLIN) {
df076bdb 503 struct sockaddr_storage ss;
7626e49e 504 unsigned int sslen = sizeof(ss);
6573faff 505 int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
df076bdb
YH
506 if (incoming < 0) {
507 switch (errno) {
508 case EAGAIN:
509 case EINTR:
510 case ECONNABORTED:
511 continue;
512 default:
513 die("accept returned %s", strerror(errno));
514 }
515 }
516 handle(incoming, (struct sockaddr *)&ss, sslen);
a87e8be2
LT
517 }
518 }
a87e8be2
LT
519 }
520}
521
6573faff
PA
522static int serve(int port)
523{
524 int socknum, *socklist;
4ae22d96 525
6573faff
PA
526 socknum = socksetup(port, &socklist);
527 if (socknum == 0)
528 die("unable to allocate any listen sockets on port %u", port);
4ae22d96 529
6573faff 530 return service_loop(socknum, socklist);
4ae22d96 531}
6573faff 532
a87e8be2
LT
533int main(int argc, char **argv)
534{
535 int port = DEFAULT_GIT_PORT;
e64e1b79 536 int inetd_mode = 0;
a87e8be2
LT
537 int i;
538
539 for (i = 1; i < argc; i++) {
540 char *arg = argv[i];
541
542 if (!strncmp(arg, "--port=", 7)) {
543 char *end;
544 unsigned long n;
545 n = strtoul(arg+7, &end, 0);
546 if (arg[7] && !*end) {
547 port = n;
548 continue;
549 }
550 }
e64e1b79
LT
551 if (!strcmp(arg, "--inetd")) {
552 inetd_mode = 1;
a8883288 553 log_syslog = 1;
e64e1b79
LT
554 continue;
555 }
f8ff0c06
PB
556 if (!strcmp(arg, "--verbose")) {
557 verbose = 1;
558 continue;
559 }
9048fe1c
PB
560 if (!strcmp(arg, "--syslog")) {
561 log_syslog = 1;
9048fe1c
PB
562 continue;
563 }
4ae95682
PA
564 if (!strcmp(arg, "--export-all")) {
565 export_all_trees = 1;
566 continue;
567 }
960deccb
PA
568 if (!strncmp(arg, "--timeout=", 10)) {
569 timeout = atoi(arg+10);
a8883288 570 continue;
960deccb 571 }
54e31a20 572 if (!strncmp(arg, "--init-timeout=", 15)) {
960deccb 573 init_timeout = atoi(arg+15);
a8883288 574 continue;
960deccb 575 }
4dbd1352
AE
576 if (!strcmp(arg, "--strict-paths")) {
577 strict_paths = 1;
578 continue;
579 }
4ae95682
PA
580 if (!strcmp(arg, "--")) {
581 ok_paths = &argv[i+1];
582 break;
583 } else if (arg[0] != '-') {
584 ok_paths = &argv[i];
585 break;
586 }
e64e1b79 587
a87e8be2
LT
588 usage(daemon_usage);
589 }
590
a8883288
AE
591 if (log_syslog)
592 openlog("git-daemon", 0, LOG_DAEMON);
593
4dbd1352
AE
594 if (strict_paths && (!ok_paths || !*ok_paths)) {
595 if (!inetd_mode)
596 die("git-daemon: option --strict-paths requires a whitelist");
597
598 logerror("option --strict-paths requires a whitelist");
599 exit (1);
600 }
601
7c3693f1
LD
602 if (inetd_mode) {
603 fclose(stderr); //FIXME: workaround
e64e1b79 604 return execute();
7c3693f1 605 }
bce8230d
AE
606
607 return serve(port);
a87e8be2 608}