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