3 #include <sys/socket.h>
7 #include <netinet/in.h>
13 static int log_syslog
;
16 static const char daemon_usage
[] =
17 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
18 " [--timeout=n] [--init-timeout=n] [--strict-paths] [directory...]";
20 /* List of acceptable pathname prefixes */
21 static char **ok_paths
= NULL
;
22 static int strict_paths
= 0;
24 /* If this is set, git-daemon-export-ok is not required */
25 static int export_all_trees
= 0;
27 /* Timeout, and initial timeout */
28 static unsigned int timeout
= 0;
29 static unsigned int init_timeout
= 0;
31 static void logreport(int priority
, const char *err
, va_list params
)
33 /* We should do a single write so that it is atomic and output
34 * of several processes do not get intermingled. */
39 /* sizeof(buf) should be big enough for "[pid] \n" */
40 buflen
= snprintf(buf
, sizeof(buf
), "[%ld] ", (long) getpid());
42 maxlen
= sizeof(buf
) - buflen
- 1; /* -1 for our own LF */
43 msglen
= vsnprintf(buf
+ buflen
, maxlen
, err
, params
);
46 syslog(priority
, "%s", buf
);
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.
55 if (msglen
> maxlen
- 1)
58 msglen
= 0; /* Protect against weird return values. */
64 write(2, buf
, buflen
);
67 static void logerror(const char *err
, ...)
70 va_start(params
, err
);
71 logreport(LOG_ERR
, err
, params
);
75 static void loginfo(const char *err
, ...)
80 va_start(params
, err
);
81 logreport(LOG_INFO
, err
, params
);
85 static char *path_ok(char *dir
)
87 char *path
= enter_repo(dir
, strict_paths
);
90 logerror("'%s': unable to chdir or not a git archive", dir
);
94 if ( ok_paths
&& *ok_paths
) {
96 int pathlen
= strlen(path
);
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).
103 for ( pp
= ok_paths
; *pp
; pp
++ ) {
104 int len
= strlen(*pp
);
105 if (len
<= pathlen
&&
106 !memcmp(*pp
, path
, len
) &&
107 (path
[len
] == '\0' ||
108 (!strict_paths
&& path
[len
] == '/')))
113 /* be backwards compatible */
118 logerror("'%s': not in whitelist", path
);
119 return NULL
; /* Fallthrough. Deny by default */
122 static int upload(char *dir
)
124 /* Timeout as string */
125 char timeout_buf
[64];
128 loginfo("Request for '%s'", dir
);
130 if (!(path
= path_ok(dir
)))
134 * Security on the cheap.
136 * We want a readable HEAD, usable "objects" directory, and
137 * a "git-daemon-export-ok" flag that says that the other side
138 * is ok with us doing this.
140 * path_ok() uses enter_repo() and does whitelist checking.
141 * We only need to make sure the repository is exported.
144 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
145 logerror("'%s': repository not exported.", path
);
151 * We'll ignore SIGTERM from now on, we have a
154 signal(SIGTERM
, SIG_IGN
);
156 snprintf(timeout_buf
, sizeof timeout_buf
, "--timeout=%u", timeout
);
158 /* git-upload-pack only ever reads stuff, so this is safe */
159 execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf
, ".", NULL
);
163 static int execute(void)
165 static char line
[1000];
168 alarm(init_timeout
? init_timeout
: timeout
);
169 len
= packet_read_line(0, line
, sizeof(line
));
172 if (len
&& line
[len
-1] == '\n')
175 if (!strncmp("git-upload-pack ", line
, 16))
176 return upload(line
+16);
178 logerror("Protocol error: '%s'", line
);
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.
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.
193 #define MAX_CHILDREN 128
195 static int max_connections
= 25;
197 /* These are updated by the signal handler */
198 static volatile unsigned int children_reaped
= 0;
199 static pid_t dead_child
[MAX_CHILDREN
];
201 /* These are updated by the main loop */
202 static unsigned int children_spawned
= 0;
203 static unsigned int children_deleted
= 0;
205 static struct child
{
208 struct sockaddr_storage address
;
209 } live_child
[MAX_CHILDREN
];
211 static void add_child(int idx
, pid_t pid
, struct sockaddr
*addr
, int addrlen
)
213 live_child
[idx
].pid
= pid
;
214 live_child
[idx
].addrlen
= addrlen
;
215 memcpy(&live_child
[idx
].address
, addr
, addrlen
);
219 * Walk from "deleted" to "spawned", and remove child "pid".
221 * We move everything up by one, since the new "deleted" will
224 static void remove_child(pid_t pid
, unsigned deleted
, unsigned spawned
)
228 deleted
%= MAX_CHILDREN
;
229 spawned
%= MAX_CHILDREN
;
230 if (live_child
[deleted
].pid
== pid
) {
231 live_child
[deleted
].pid
= -1;
234 n
= live_child
[deleted
];
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
;
249 * This gets called if the number of connections grows
250 * past "max_connections".
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.
256 * As it is, we just "randomly" kill 25% of the connections,
257 * and our pseudo-random generator sucks too. I have no
260 * Really, this is just a place-holder for a _real_ algorithm.
262 static void kill_some_children(int signo
, unsigned start
, unsigned stop
)
264 start
%= MAX_CHILDREN
;
265 stop
%= MAX_CHILDREN
;
266 while (start
!= stop
) {
268 kill(live_child
[start
].pid
, signo
);
269 start
= (start
+ 1) % MAX_CHILDREN
;
273 static void check_max_connections(void)
277 unsigned spawned
, reaped
, deleted
;
279 spawned
= children_spawned
;
280 reaped
= children_reaped
;
281 deleted
= children_deleted
;
283 while (deleted
< reaped
) {
284 pid_t pid
= dead_child
[deleted
% MAX_CHILDREN
];
285 remove_child(pid
, deleted
, spawned
);
288 children_deleted
= deleted
;
290 active
= spawned
- deleted
;
291 if (active
<= max_connections
)
294 /* Kill some unstarted connections with SIGTERM */
295 kill_some_children(SIGTERM
, deleted
, spawned
);
296 if (active
<= max_connections
<< 1)
299 /* If the SIGTERM thing isn't helping use SIGKILL */
300 kill_some_children(SIGKILL
, deleted
, spawned
);
305 static void handle(int incoming
, struct sockaddr
*addr
, int addrlen
)
308 char addrbuf
[256] = "";
318 idx
= children_spawned
% MAX_CHILDREN
;
320 add_child(idx
, pid
, addr
, addrlen
);
322 check_max_connections();
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
;
336 } else if (addr
->sa_family
== AF_INET6
) {
337 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
340 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
341 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
, sizeof(addrbuf
) - 1);
344 port
= sin6_addr
->sin6_port
;
347 loginfo("Connection from %s:%d", addrbuf
, port
);
352 static void child_handler(int signo
)
356 pid_t pid
= waitpid(-1, &status
, WNOHANG
);
359 unsigned reaped
= children_reaped
;
360 dead_child
[reaped
% MAX_CHILDREN
] = pid
;
361 children_reaped
= reaped
+ 1;
362 /* XXX: Custom logging, since we don't wanna getpid() */
365 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0)
366 dead
= " (with error)";
368 syslog(LOG_INFO
, "[%d] Disconnected%s", pid
, dead
);
370 fprintf(stderr
, "[%d] Disconnected%s\n", pid
, dead
);
380 static int socksetup(int port
, int **socklist_p
)
382 int socknum
= 0, *socklist
= NULL
;
384 char pbuf
[NI_MAXSERV
];
386 struct addrinfo hints
, *ai0
, *ai
;
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
;
396 gai
= getaddrinfo(NULL
, pbuf
, &hints
, &ai0
);
398 die("getaddrinfo() failed: %s\n", gai_strerror(gai
));
400 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
404 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
407 if (sockfd
>= FD_SETSIZE
) {
408 error("too large socket descriptor.");
414 if (ai
->ai_family
== AF_INET6
) {
416 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
418 /* Note: error is not fatal */
422 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
424 continue; /* not fatal */
426 if (listen(sockfd
, 5) < 0) {
428 continue; /* not fatal */
431 newlist
= realloc(socklist
, sizeof(int) * (socknum
+ 1));
433 die("memory allocation failed: %s", strerror(errno
));
436 socklist
[socknum
++] = sockfd
;
444 *socklist_p
= socklist
;
450 static int socksetup(int port
, int **socklist_p
)
452 struct sockaddr_in sin
;
455 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
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
);
464 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
469 if (listen(sockfd
, 5) < 0) {
474 *socklist_p
= xmalloc(sizeof(int));
475 **socklist_p
= sockfd
;
481 static int service_loop(int socknum
, int *socklist
)
486 pfd
= xcalloc(socknum
, sizeof(struct pollfd
));
488 for (i
= 0; i
< socknum
; i
++) {
489 pfd
[i
].fd
= socklist
[i
];
490 pfd
[i
].events
= POLLIN
;
493 signal(SIGCHLD
, child_handler
);
498 if (poll(pfd
, socknum
, -1) < 0) {
499 if (errno
!= EINTR
) {
500 error("poll failed, resuming: %s",
507 for (i
= 0; i
< socknum
; i
++) {
508 if (pfd
[i
].revents
& POLLIN
) {
509 struct sockaddr_storage ss
;
510 unsigned int sslen
= sizeof(ss
);
511 int incoming
= accept(pfd
[i
].fd
, (struct sockaddr
*)&ss
, &sslen
);
519 die("accept returned %s", strerror(errno
));
522 handle(incoming
, (struct sockaddr
*)&ss
, sslen
);
528 static int serve(int port
)
530 int socknum
, *socklist
;
532 socknum
= socksetup(port
, &socklist
);
534 die("unable to allocate any listen sockets on port %u", port
);
536 return service_loop(socknum
, socklist
);
539 int main(int argc
, char **argv
)
541 int port
= DEFAULT_GIT_PORT
;
545 for (i
= 1; i
< argc
; i
++) {
548 if (!strncmp(arg
, "--port=", 7)) {
551 n
= strtoul(arg
+7, &end
, 0);
552 if (arg
[7] && !*end
) {
557 if (!strcmp(arg
, "--inetd")) {
562 if (!strcmp(arg
, "--verbose")) {
566 if (!strcmp(arg
, "--syslog")) {
570 if (!strcmp(arg
, "--export-all")) {
571 export_all_trees
= 1;
574 if (!strncmp(arg
, "--timeout=", 10)) {
575 timeout
= atoi(arg
+10);
578 if (!strncmp(arg
, "--init-timeout=", 15)) {
579 init_timeout
= atoi(arg
+15);
582 if (!strcmp(arg
, "--strict-paths")) {
586 if (!strcmp(arg
, "--")) {
587 ok_paths
= &argv
[i
+1];
589 } else if (arg
[0] != '-') {
598 openlog("git-daemon", 0, LOG_DAEMON
);
600 if (strict_paths
&& (!ok_paths
|| !*ok_paths
)) {
602 die("git-daemon: option --strict-paths requires a whitelist");
604 logerror("option --strict-paths requires a whitelist");
609 fclose(stderr
); //FIXME: workaround