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