5 #include <sys/socket.h>
8 #include <netinet/in.h>
12 static int log_syslog;
15 static const char daemon_usage[] = "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all] [directory...]";
17 /* List of acceptable pathname prefixes */
18 static char **ok_paths = NULL;
20 /* If this is set, git-daemon-export-ok is not required */
21 static int export_all_trees = 0;
24 static void logreport(int priority, const char *err, va_list params)
26 /* We should do a single write so that it is atomic and output
27 * of several processes do not get intermingled. */
32 /* sizeof(buf) should be big enough for "[pid] \n" */
33 buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid());
35 maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
36 msglen = vsnprintf(buf + buflen, maxlen, err, params);
39 syslog(priority, "%s", buf);
43 /* maxlen counted our own LF but also counts space given to
44 * vsnprintf for the terminating NUL. We want to make sure that
45 * we have space for our own LF and NUL after the "meat" of the
46 * message, so truncate it at maxlen - 1.
48 if (msglen > maxlen - 1)
51 msglen = 0; /* Protect against weird return values. */
57 write(2, buf, buflen);
60 void logerror(const char *err, ...)
63 va_start(params, err);
64 logreport(LOG_ERR, err, params);
68 void loginfo(const char *err, ...)
73 va_start(params, err);
74 logreport(LOG_INFO, err, params);
78 static int path_ok(const char *dir)
87 } else if ( *p == '/' || *p == '\0' ) {
88 if ( sl && ndot > 0 && ndot < 3 )
89 return 0; /* . or .. in path */
92 break; /* End of string and all is good */
99 if ( ok_paths && *ok_paths ) {
101 int dirlen = strlen(dir); /* read_packet_line can return embedded \0 */
103 for ( pp = ok_paths ; *pp ; pp++ ) {
104 int len = strlen(*pp);
105 if ( len <= dirlen &&
106 !strncmp(*pp, dir, len) &&
107 (dir[len] == '/' || dir[len] == '\0') ) {
114 return 0; /* Path not in whitelist */
117 return 1; /* Path acceptable */
120 static int upload(char *dir, int dirlen)
122 loginfo("Request for '%s'", dir);
125 logerror("Forbidden directory: %s\n", dir);
129 if (chdir(dir) < 0) {
130 logerror("Cannot chdir('%s'): %s", dir, strerror(errno));
135 * Security on the cheap.
137 * We want a readable HEAD, usable "objects" directory, and
138 * a "git-daemon-export-ok" flag that says that the other side
139 * is ok with us doing this.
141 if ((!export_all_trees && access("git-daemon-export-ok", F_OK)) ||
142 access("objects/00", X_OK) ||
143 access("HEAD", R_OK)) {
144 logerror("Not a valid git-daemon-enabled repository: '%s'", dir);
149 * We'll ignore SIGTERM from now on, we have a
152 signal(SIGTERM, SIG_IGN);
154 /* git-upload-pack only ever reads stuff, so this is safe */
155 execlp("git-upload-pack", "git-upload-pack", ".", NULL);
159 static int execute(void)
161 static char line[1000];
164 len = packet_read_line(0, line, sizeof(line));
166 if (len && line[len-1] == '\n')
169 if (!strncmp("git-upload-pack /", line, 17))
170 return upload(line + 16, len - 16);
172 logerror("Protocol error: '%s'", line);
178 * We count spawned/reaped separately, just to avoid any
179 * races when updating them from signals. The SIGCHLD handler
180 * will only update children_reaped, and the fork logic will
181 * only update children_spawned.
183 * MAX_CHILDREN should be a power-of-two to make the modulus
184 * operation cheap. It should also be at least twice
185 * the maximum number of connections we will ever allow.
187 #define MAX_CHILDREN 128
189 static int max_connections = 25;
191 /* These are updated by the signal handler */
192 static volatile unsigned int children_reaped = 0;
193 static pid_t dead_child[MAX_CHILDREN];
195 /* These are updated by the main loop */
196 static unsigned int children_spawned = 0;
197 static unsigned int children_deleted = 0;
199 static struct child {
202 struct sockaddr_storage address;
203 } live_child[MAX_CHILDREN];
205 static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen)
207 live_child[idx].pid = pid;
208 live_child[idx].addrlen = addrlen;
209 memcpy(&live_child[idx].address, addr, addrlen);
213 * Walk from "deleted" to "spawned", and remove child "pid".
215 * We move everything up by one, since the new "deleted" will
218 static void remove_child(pid_t pid, unsigned deleted, unsigned spawned)
222 deleted %= MAX_CHILDREN;
223 spawned %= MAX_CHILDREN;
224 if (live_child[deleted].pid == pid) {
225 live_child[deleted].pid = -1;
228 n = live_child[deleted];
231 deleted = (deleted + 1) % MAX_CHILDREN;
232 if (deleted == spawned)
233 die("could not find dead child %d\n", pid);
234 m = live_child[deleted];
235 live_child[deleted] = n;
243 * This gets called if the number of connections grows
244 * past "max_connections".
246 * We _should_ start off by searching for connections
247 * from the same IP, and if there is some address wth
248 * multiple connections, we should kill that first.
250 * As it is, we just "randomly" kill 25% of the connections,
251 * and our pseudo-random generator sucks too. I have no
254 * Really, this is just a place-holder for a _real_ algorithm.
256 static void kill_some_children(int signo, unsigned start, unsigned stop)
258 start %= MAX_CHILDREN;
259 stop %= MAX_CHILDREN;
260 while (start != stop) {
262 kill(live_child[start].pid, signo);
263 start = (start + 1) % MAX_CHILDREN;
267 static void check_max_connections(void)
271 unsigned spawned, reaped, deleted;
273 spawned = children_spawned;
274 reaped = children_reaped;
275 deleted = children_deleted;
277 while (deleted < reaped) {
278 pid_t pid = dead_child[deleted % MAX_CHILDREN];
279 remove_child(pid, deleted, spawned);
282 children_deleted = deleted;
284 active = spawned - deleted;
285 if (active <= max_connections)
288 /* Kill some unstarted connections with SIGTERM */
289 kill_some_children(SIGTERM, deleted, spawned);
290 if (active <= max_connections << 1)
293 /* If the SIGTERM thing isn't helping use SIGKILL */
294 kill_some_children(SIGKILL, deleted, spawned);
299 static void handle(int incoming, struct sockaddr *addr, int addrlen)
302 char addrbuf[256] = "";
312 idx = children_spawned % MAX_CHILDREN;
314 add_child(idx, pid, addr, addrlen);
316 check_max_connections();
324 if (addr->sa_family == AF_INET) {
325 struct sockaddr_in *sin_addr = (void *) addr;
326 inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
327 port = sin_addr->sin_port;
329 } else if (addr->sa_family == AF_INET6) {
330 struct sockaddr_in6 *sin6_addr = (void *) addr;
333 *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
334 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
337 port = sin6_addr->sin6_port;
339 loginfo("Connection from %s:%d", addrbuf, port);
344 static void child_handler(int signo)
348 pid_t pid = waitpid(-1, &status, WNOHANG);
351 unsigned reaped = children_reaped;
352 dead_child[reaped % MAX_CHILDREN] = pid;
353 children_reaped = reaped + 1;
354 /* XXX: Custom logging, since we don't wanna getpid() */
357 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
358 dead = " (with error)";
360 syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead);
362 fprintf(stderr, "[%d] Disconnected%s\n", pid, dead);
370 static int serve(int port)
372 struct addrinfo hints, *ai0, *ai;
374 int socknum = 0, *socklist = NULL;
376 fd_set fds_init, fds;
377 char pbuf[NI_MAXSERV];
379 signal(SIGCHLD, child_handler);
381 sprintf(pbuf, "%d", port);
382 memset(&hints, 0, sizeof(hints));
383 hints.ai_family = AF_UNSPEC;
384 hints.ai_socktype = SOCK_STREAM;
385 hints.ai_protocol = IPPROTO_TCP;
386 hints.ai_flags = AI_PASSIVE;
388 gai = getaddrinfo(NULL, pbuf, &hints, &ai0);
390 die("getaddrinfo() failed: %s\n", gai_strerror(gai));
394 for (ai = ai0; ai; ai = ai->ai_next) {
398 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
401 if (sockfd >= FD_SETSIZE) {
402 error("too large socket descriptor.");
408 if (ai->ai_family == AF_INET6) {
410 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
412 /* Note: error is not fatal */
416 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
418 continue; /* not fatal */
420 if (listen(sockfd, 5) < 0) {
422 continue; /* not fatal */
425 newlist = realloc(socklist, sizeof(int) * (socknum + 1));
427 die("memory allocation failed: %s", strerror(errno));
430 socklist[socknum++] = sockfd;
432 FD_SET(sockfd, &fds_init);
440 die("unable to allocate any listen sockets on port %u", port);
446 if (select(maxfd + 1, &fds, NULL, NULL, NULL) < 0) {
447 if (errno != EINTR) {
448 error("select failed, resuming: %s",
455 for (i = 0; i < socknum; i++) {
456 int sockfd = socklist[i];
458 if (FD_ISSET(sockfd, &fds)) {
459 struct sockaddr_storage ss;
460 int sslen = sizeof(ss);
461 int incoming = accept(sockfd, (struct sockaddr *)&ss, &sslen);
469 die("accept returned %s", strerror(errno));
472 handle(incoming, (struct sockaddr *)&ss, sslen);
478 int main(int argc, char **argv)
480 int port = DEFAULT_GIT_PORT;
484 for (i = 1; i < argc; i++) {
487 if (!strncmp(arg, "--port=", 7)) {
490 n = strtoul(arg+7, &end, 0);
491 if (arg[7] && !*end) {
496 if (!strcmp(arg, "--inetd")) {
500 if (!strcmp(arg, "--verbose")) {
504 if (!strcmp(arg, "--syslog")) {
506 openlog("git-daemon", 0, LOG_DAEMON);
509 if (!strcmp(arg, "--export-all")) {
510 export_all_trees = 1;
513 if (!strcmp(arg, "--")) {
514 ok_paths = &argv[i+1];
516 } else if (arg[0] != '-') {
525 fclose(stderr); //FIXME: workaround