Code

5bf5c82627beb8d8a5af71bce55c7e850ea54c46
[git.git] / daemon.c
1 #include <signal.h>
2 #include <sys/wait.h>
3 #include <sys/socket.h>
4 #include <sys/time.h>
5 #include <sys/poll.h>
6 #include <netdb.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <syslog.h>
10 #include "pkt-line.h"
11 #include "cache.h"
12 #include "exec_cmd.h"
14 static int log_syslog;
15 static int verbose;
16 static int reuseaddr;
18 static const char daemon_usage[] =
19 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
20 "           [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
21 "           [--base-path=path] [--user-path | --user-path=path]\n"
22 "           [--reuseaddr] [--detach] [--pid-file=file] [directory...]";
24 /* List of acceptable pathname prefixes */
25 static char **ok_paths;
26 static int strict_paths;
28 /* If this is set, git-daemon-export-ok is not required */
29 static int export_all_trees;
31 /* Take all paths relative to this one if non-NULL */
32 static char *base_path;
34 /* If defined, ~user notation is allowed and the string is inserted
35  * after ~user/.  E.g. a request to git://host/~alice/frotz would
36  * go to /home/alice/pub_git/frotz with --user-path=pub_git.
37  */
38 static const char *user_path;
40 /* Timeout, and initial timeout */
41 static unsigned int timeout;
42 static unsigned int init_timeout;
44 static void logreport(int priority, const char *err, va_list params)
45 {
46         /* We should do a single write so that it is atomic and output
47          * of several processes do not get intermingled. */
48         char buf[1024];
49         int buflen;
50         int maxlen, msglen;
52         /* sizeof(buf) should be big enough for "[pid] \n" */
53         buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
55         maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
56         msglen = vsnprintf(buf + buflen, maxlen, err, params);
58         if (log_syslog) {
59                 syslog(priority, "%s", buf);
60                 return;
61         }
63         /* maxlen counted our own LF but also counts space given to
64          * vsnprintf for the terminating NUL.  We want to make sure that
65          * we have space for our own LF and NUL after the "meat" of the
66          * message, so truncate it at maxlen - 1.
67          */
68         if (msglen > maxlen - 1)
69                 msglen = maxlen - 1;
70         else if (msglen < 0)
71                 msglen = 0; /* Protect against weird return values. */
72         buflen += msglen;
74         buf[buflen++] = '\n';
75         buf[buflen] = '\0';
77         write(2, buf, buflen);
78 }
80 static void logerror(const char *err, ...)
81 {
82         va_list params;
83         va_start(params, err);
84         logreport(LOG_ERR, err, params);
85         va_end(params);
86 }
88 static void loginfo(const char *err, ...)
89 {
90         va_list params;
91         if (!verbose)
92                 return;
93         va_start(params, err);
94         logreport(LOG_INFO, err, params);
95         va_end(params);
96 }
98 static void NORETURN daemon_die(const char *err, va_list params)
99 {
100         logreport(LOG_ERR, err, params);
101         exit(1);
104 static int avoid_alias(char *p)
106         int sl, ndot;
108         /* 
109          * This resurrects the belts and suspenders paranoia check by HPA
110          * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
111          * does not do getcwd() based path canonicalizations.
112          *
113          * sl becomes true immediately after seeing '/' and continues to
114          * be true as long as dots continue after that without intervening
115          * non-dot character.
116          */
117         if (!p || (*p != '/' && *p != '~'))
118                 return -1;
119         sl = 1; ndot = 0;
120         p++;
122         while (1) {
123                 char ch = *p++;
124                 if (sl) {
125                         if (ch == '.')
126                                 ndot++;
127                         else if (ch == '/') {
128                                 if (ndot < 3)
129                                         /* reject //, /./ and /../ */
130                                         return -1;
131                                 ndot = 0;
132                         }
133                         else if (ch == 0) {
134                                 if (0 < ndot && ndot < 3)
135                                         /* reject /.$ and /..$ */
136                                         return -1;
137                                 return 0;
138                         }
139                         else
140                                 sl = ndot = 0;
141                 }
142                 else if (ch == 0)
143                         return 0;
144                 else if (ch == '/') {
145                         sl = 1;
146                         ndot = 0;
147                 }
148         }
151 static char *path_ok(char *dir)
153         static char rpath[PATH_MAX];
154         char *path;
156         if (avoid_alias(dir)) {
157                 logerror("'%s': aliased", dir);
158                 return NULL;
159         }
161         if (*dir == '~') {
162                 if (!user_path) {
163                         logerror("'%s': User-path not allowed", dir);
164                         return NULL;
165                 }
166                 if (*user_path) {
167                         /* Got either "~alice" or "~alice/foo";
168                          * rewrite them to "~alice/%s" or
169                          * "~alice/%s/foo".
170                          */
171                         int namlen, restlen = strlen(dir);
172                         char *slash = strchr(dir, '/');
173                         if (!slash)
174                                 slash = dir + restlen;
175                         namlen = slash - dir;
176                         restlen -= namlen;
177                         loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
178                         snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
179                                  namlen, dir, user_path, restlen, slash);
180                         dir = rpath;
181                 }
182         }
183         else if (base_path) {
184                 if (*dir != '/') {
185                         /* Allow only absolute */
186                         logerror("'%s': Non-absolute path denied (base-path active)", dir);
187                         return NULL;
188                 }
189                 else {
190                         snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
191                         dir = rpath;
192                 }
193         }
195         path = enter_repo(dir, strict_paths);
197         if (!path) {
198                 logerror("'%s': unable to chdir or not a git archive", dir);
199                 return NULL;
200         }
202         if ( ok_paths && *ok_paths ) {
203                 char **pp;
204                 int pathlen = strlen(path);
206                 /* The validation is done on the paths after enter_repo
207                  * appends optional {.git,.git/.git} and friends, but 
208                  * it does not use getcwd().  So if your /pub is
209                  * a symlink to /mnt/pub, you can whitelist /pub and
210                  * do not have to say /mnt/pub.
211                  * Do not say /pub/.
212                  */
213                 for ( pp = ok_paths ; *pp ; pp++ ) {
214                         int len = strlen(*pp);
215                         if (len <= pathlen &&
216                             !memcmp(*pp, path, len) &&
217                             (path[len] == '\0' ||
218                              (!strict_paths && path[len] == '/')))
219                                 return path;
220                 }
221         }
222         else {
223                 /* be backwards compatible */
224                 if (!strict_paths)
225                         return path;
226         }
228         logerror("'%s': not in whitelist", path);
229         return NULL;            /* Fallthrough. Deny by default */
232 static int upload(char *dir)
234         /* Timeout as string */
235         char timeout_buf[64];
236         const char *path;
238         loginfo("Request for '%s'", dir);
240         if (!(path = path_ok(dir)))
241                 return -1;
243         /*
244          * Security on the cheap.
245          *
246          * We want a readable HEAD, usable "objects" directory, and
247          * a "git-daemon-export-ok" flag that says that the other side
248          * is ok with us doing this.
249          *
250          * path_ok() uses enter_repo() and does whitelist checking.
251          * We only need to make sure the repository is exported.
252          */
254         if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
255                 logerror("'%s': repository not exported.", path);
256                 errno = EACCES;
257                 return -1;
258         }
260         /*
261          * We'll ignore SIGTERM from now on, we have a
262          * good client.
263          */
264         signal(SIGTERM, SIG_IGN);
266         snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
268         /* git-upload-pack only ever reads stuff, so this is safe */
269         execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL);
270         return -1;
273 static int execute(struct sockaddr *addr)
275         static char line[1000];
276         int pktlen, len;
278         if (addr) {
279                 char addrbuf[256] = "";
280                 int port = -1;
282                 if (addr->sa_family == AF_INET) {
283                         struct sockaddr_in *sin_addr = (void *) addr;
284                         inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
285                         port = sin_addr->sin_port;
286 #ifndef NO_IPV6
287                 } else if (addr && addr->sa_family == AF_INET6) {
288                         struct sockaddr_in6 *sin6_addr = (void *) addr;
290                         char *buf = addrbuf;
291                         *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
292                         inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
293                         strcat(buf, "]");
295                         port = sin6_addr->sin6_port;
296 #endif
297                 }
298                 loginfo("Connection from %s:%d", addrbuf, port);
299         }
301         alarm(init_timeout ? init_timeout : timeout);
302         pktlen = packet_read_line(0, line, sizeof(line));
303         alarm(0);
305         len = strlen(line);
306         if (pktlen != len)
307                 loginfo("Extended attributes (%d bytes) exist <%.*s>",
308                         (int) pktlen - len,
309                         (int) pktlen - len, line + len + 1);
310         if (len && line[len-1] == '\n')
311                 line[--len] = 0;
313         if (!strncmp("git-upload-pack ", line, 16))
314                 return upload(line+16);
316         logerror("Protocol error: '%s'", line);
317         return -1;
321 /*
322  * We count spawned/reaped separately, just to avoid any
323  * races when updating them from signals. The SIGCHLD handler
324  * will only update children_reaped, and the fork logic will
325  * only update children_spawned.
326  *
327  * MAX_CHILDREN should be a power-of-two to make the modulus
328  * operation cheap. It should also be at least twice
329  * the maximum number of connections we will ever allow.
330  */
331 #define MAX_CHILDREN 128
333 static int max_connections = 25;
335 /* These are updated by the signal handler */
336 static volatile unsigned int children_reaped;
337 static pid_t dead_child[MAX_CHILDREN];
339 /* These are updated by the main loop */
340 static unsigned int children_spawned;
341 static unsigned int children_deleted;
343 static struct child {
344         pid_t pid;
345         int addrlen;
346         struct sockaddr_storage address;
347 } live_child[MAX_CHILDREN];
349 static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen)
351         live_child[idx].pid = pid;
352         live_child[idx].addrlen = addrlen;
353         memcpy(&live_child[idx].address, addr, addrlen);
356 /*
357  * Walk from "deleted" to "spawned", and remove child "pid".
358  *
359  * We move everything up by one, since the new "deleted" will
360  * be one higher.
361  */
362 static void remove_child(pid_t pid, unsigned deleted, unsigned spawned)
364         struct child n;
366         deleted %= MAX_CHILDREN;
367         spawned %= MAX_CHILDREN;
368         if (live_child[deleted].pid == pid) {
369                 live_child[deleted].pid = -1;
370                 return;
371         }
372         n = live_child[deleted];
373         for (;;) {
374                 struct child m;
375                 deleted = (deleted + 1) % MAX_CHILDREN;
376                 if (deleted == spawned)
377                         die("could not find dead child %d\n", pid);
378                 m = live_child[deleted];
379                 live_child[deleted] = n;
380                 if (m.pid == pid)
381                         return;
382                 n = m;
383         }
386 /*
387  * This gets called if the number of connections grows
388  * past "max_connections".
389  *
390  * We _should_ start off by searching for connections
391  * from the same IP, and if there is some address wth
392  * multiple connections, we should kill that first.
393  *
394  * As it is, we just "randomly" kill 25% of the connections,
395  * and our pseudo-random generator sucks too. I have no
396  * shame.
397  *
398  * Really, this is just a place-holder for a _real_ algorithm.
399  */
400 static void kill_some_children(int signo, unsigned start, unsigned stop)
402         start %= MAX_CHILDREN;
403         stop %= MAX_CHILDREN;
404         while (start != stop) {
405                 if (!(start & 3))
406                         kill(live_child[start].pid, signo);
407                 start = (start + 1) % MAX_CHILDREN;
408         }
411 static void check_max_connections(void)
413         for (;;) {
414                 int active;
415                 unsigned spawned, reaped, deleted;
417                 spawned = children_spawned;
418                 reaped = children_reaped;
419                 deleted = children_deleted;
421                 while (deleted < reaped) {
422                         pid_t pid = dead_child[deleted % MAX_CHILDREN];
423                         remove_child(pid, deleted, spawned);
424                         deleted++;
425                 }
426                 children_deleted = deleted;
428                 active = spawned - deleted;
429                 if (active <= max_connections)
430                         break;
432                 /* Kill some unstarted connections with SIGTERM */
433                 kill_some_children(SIGTERM, deleted, spawned);
434                 if (active <= max_connections << 1)
435                         break;
437                 /* If the SIGTERM thing isn't helping use SIGKILL */
438                 kill_some_children(SIGKILL, deleted, spawned);
439                 sleep(1);
440         }
443 static void handle(int incoming, struct sockaddr *addr, int addrlen)
445         pid_t pid = fork();
447         if (pid) {
448                 unsigned idx;
450                 close(incoming);
451                 if (pid < 0)
452                         return;
454                 idx = children_spawned % MAX_CHILDREN;
455                 children_spawned++;
456                 add_child(idx, pid, addr, addrlen);
458                 check_max_connections();
459                 return;
460         }
462         dup2(incoming, 0);
463         dup2(incoming, 1);
464         close(incoming);
466         exit(execute(addr));
469 static void child_handler(int signo)
471         for (;;) {
472                 int status;
473                 pid_t pid = waitpid(-1, &status, WNOHANG);
475                 if (pid > 0) {
476                         unsigned reaped = children_reaped;
477                         dead_child[reaped % MAX_CHILDREN] = pid;
478                         children_reaped = reaped + 1;
479                         /* XXX: Custom logging, since we don't wanna getpid() */
480                         if (verbose) {
481                                 const char *dead = "";
482                                 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
483                                         dead = " (with error)";
484                                 if (log_syslog)
485                                         syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead);
486                                 else
487                                         fprintf(stderr, "[%d] Disconnected%s\n", pid, dead);
488                         }
489                         continue;
490                 }
491                 break;
492         }
495 static int set_reuse_addr(int sockfd)
497         int on = 1;
499         if (!reuseaddr)
500                 return 0;
501         return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
502                           &on, sizeof(on));
505 #ifndef NO_IPV6
507 static int socksetup(int port, int **socklist_p)
509         int socknum = 0, *socklist = NULL;
510         int maxfd = -1;
511         char pbuf[NI_MAXSERV];
513         struct addrinfo hints, *ai0, *ai;
514         int gai;
516         sprintf(pbuf, "%d", port);
517         memset(&hints, 0, sizeof(hints));
518         hints.ai_family = AF_UNSPEC;
519         hints.ai_socktype = SOCK_STREAM;
520         hints.ai_protocol = IPPROTO_TCP;
521         hints.ai_flags = AI_PASSIVE;
523         gai = getaddrinfo(NULL, pbuf, &hints, &ai0);
524         if (gai)
525                 die("getaddrinfo() failed: %s\n", gai_strerror(gai));
527         for (ai = ai0; ai; ai = ai->ai_next) {
528                 int sockfd;
530                 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
531                 if (sockfd < 0)
532                         continue;
533                 if (sockfd >= FD_SETSIZE) {
534                         error("too large socket descriptor.");
535                         close(sockfd);
536                         continue;
537                 }
539 #ifdef IPV6_V6ONLY
540                 if (ai->ai_family == AF_INET6) {
541                         int on = 1;
542                         setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
543                                    &on, sizeof(on));
544                         /* Note: error is not fatal */
545                 }
546 #endif
548                 if (set_reuse_addr(sockfd)) {
549                         close(sockfd);
550                         continue;
551                 }
553                 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
554                         close(sockfd);
555                         continue;       /* not fatal */
556                 }
557                 if (listen(sockfd, 5) < 0) {
558                         close(sockfd);
559                         continue;       /* not fatal */
560                 }
562                 socklist = xrealloc(socklist, sizeof(int) * (socknum + 1));
563                 socklist[socknum++] = sockfd;
565                 if (maxfd < sockfd)
566                         maxfd = sockfd;
567         }
569         freeaddrinfo(ai0);
571         *socklist_p = socklist;
572         return socknum;
575 #else /* NO_IPV6 */
577 static int socksetup(int port, int **socklist_p)
579         struct sockaddr_in sin;
580         int sockfd;
582         sockfd = socket(AF_INET, SOCK_STREAM, 0);
583         if (sockfd < 0)
584                 return 0;
586         memset(&sin, 0, sizeof sin);
587         sin.sin_family = AF_INET;
588         sin.sin_addr.s_addr = htonl(INADDR_ANY);
589         sin.sin_port = htons(port);
591         if (set_reuse_addr(sockfd)) {
592                 close(sockfd);
593                 return 0;
594         }
596         if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
597                 close(sockfd);
598                 return 0;
599         }
601         if (listen(sockfd, 5) < 0) {
602                 close(sockfd);
603                 return 0;
604         }
606         *socklist_p = xmalloc(sizeof(int));
607         **socklist_p = sockfd;
608         return 1;
611 #endif
613 static int service_loop(int socknum, int *socklist)
615         struct pollfd *pfd;
616         int i;
618         pfd = xcalloc(socknum, sizeof(struct pollfd));
620         for (i = 0; i < socknum; i++) {
621                 pfd[i].fd = socklist[i];
622                 pfd[i].events = POLLIN;
623         }
625         signal(SIGCHLD, child_handler);
627         for (;;) {
628                 int i;
630                 if (poll(pfd, socknum, -1) < 0) {
631                         if (errno != EINTR) {
632                                 error("poll failed, resuming: %s",
633                                       strerror(errno));
634                                 sleep(1);
635                         }
636                         continue;
637                 }
639                 for (i = 0; i < socknum; i++) {
640                         if (pfd[i].revents & POLLIN) {
641                                 struct sockaddr_storage ss;
642                                 unsigned int sslen = sizeof(ss);
643                                 int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
644                                 if (incoming < 0) {
645                                         switch (errno) {
646                                         case EAGAIN:
647                                         case EINTR:
648                                         case ECONNABORTED:
649                                                 continue;
650                                         default:
651                                                 die("accept returned %s", strerror(errno));
652                                         }
653                                 }
654                                 handle(incoming, (struct sockaddr *)&ss, sslen);
655                         }
656                 }
657         }
660 /* if any standard file descriptor is missing open it to /dev/null */
661 static void sanitize_stdfds(void)
663         int fd = open("/dev/null", O_RDWR, 0);
664         while (fd != -1 && fd < 2)
665                 fd = dup(fd);
666         if (fd == -1)
667                 die("open /dev/null or dup failed: %s", strerror(errno));
668         if (fd > 2)
669                 close(fd);
672 static void daemonize(void)
674         switch (fork()) {
675                 case 0:
676                         break;
677                 case -1:
678                         die("fork failed: %s", strerror(errno));
679                 default:
680                         exit(0);
681         }
682         if (setsid() == -1)
683                 die("setsid failed: %s", strerror(errno));
684         close(0);
685         close(1);
686         close(2);
687         sanitize_stdfds();
690 static void store_pid(const char *path)
692         FILE *f = fopen(path, "w");
693         if (!f)
694                 die("cannot open pid file %s: %s", path, strerror(errno));
695         fprintf(f, "%d\n", getpid());
696         fclose(f);
699 static int serve(int port)
701         int socknum, *socklist;
703         socknum = socksetup(port, &socklist);
704         if (socknum == 0)
705                 die("unable to allocate any listen sockets on port %u", port);
707         return service_loop(socknum, socklist);
710 int main(int argc, char **argv)
712         int port = DEFAULT_GIT_PORT;
713         int inetd_mode = 0;
714         const char *pid_file = NULL;
715         int detach = 0;
716         int i;
718         /* Without this we cannot rely on waitpid() to tell
719          * what happened to our children.
720          */
721         signal(SIGCHLD, SIG_DFL);
723         for (i = 1; i < argc; i++) {
724                 char *arg = argv[i];
726                 if (!strncmp(arg, "--port=", 7)) {
727                         char *end;
728                         unsigned long n;
729                         n = strtoul(arg+7, &end, 0);
730                         if (arg[7] && !*end) {
731                                 port = n;
732                                 continue;
733                         }
734                 }
735                 if (!strcmp(arg, "--inetd")) {
736                         inetd_mode = 1;
737                         log_syslog = 1;
738                         continue;
739                 }
740                 if (!strcmp(arg, "--verbose")) {
741                         verbose = 1;
742                         continue;
743                 }
744                 if (!strcmp(arg, "--syslog")) {
745                         log_syslog = 1;
746                         continue;
747                 }
748                 if (!strcmp(arg, "--export-all")) {
749                         export_all_trees = 1;
750                         continue;
751                 }
752                 if (!strncmp(arg, "--timeout=", 10)) {
753                         timeout = atoi(arg+10);
754                         continue;
755                 }
756                 if (!strncmp(arg, "--init-timeout=", 15)) {
757                         init_timeout = atoi(arg+15);
758                         continue;
759                 }
760                 if (!strcmp(arg, "--strict-paths")) {
761                         strict_paths = 1;
762                         continue;
763                 }
764                 if (!strncmp(arg, "--base-path=", 12)) {
765                         base_path = arg+12;
766                         continue;
767                 }
768                 if (!strcmp(arg, "--reuseaddr")) {
769                         reuseaddr = 1;
770                         continue;
771                 }
772                 if (!strcmp(arg, "--user-path")) {
773                         user_path = "";
774                         continue;
775                 }
776                 if (!strncmp(arg, "--user-path=", 12)) {
777                         user_path = arg + 12;
778                         continue;
779                 }
780                 if (!strncmp(arg, "--pid-file=", 11)) {
781                         pid_file = arg + 11;
782                         continue;
783                 }
784                 if (!strcmp(arg, "--detach")) {
785                         detach = 1;
786                         log_syslog = 1;
787                         continue;
788                 }
789                 if (!strcmp(arg, "--")) {
790                         ok_paths = &argv[i+1];
791                         break;
792                 } else if (arg[0] != '-') {
793                         ok_paths = &argv[i];
794                         break;
795                 }
797                 usage(daemon_usage);
798         }
800         if (log_syslog) {
801                 openlog("git-daemon", 0, LOG_DAEMON);
802                 set_die_routine(daemon_die);
803         }
805         if (strict_paths && (!ok_paths || !*ok_paths))
806                 die("option --strict-paths requires a whitelist");
808         if (inetd_mode) {
809                 struct sockaddr_storage ss;
810                 struct sockaddr *peer = (struct sockaddr *)&ss;
811                 socklen_t slen = sizeof(ss);
813                 freopen("/dev/null", "w", stderr);
815                 if (getpeername(0, peer, &slen))
816                         peer = NULL;
818                 return execute(peer);
819         }
821         if (detach)
822                 daemonize();
823         else
824                 sanitize_stdfds();
826         if (pid_file)
827                 store_pid(pid_file);
829         return serve(port);