]>
Commit | Line | Data |
---|---|---|
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" | |
13 | ||
14 | static int log_syslog; | |
15 | static int verbose; | |
16 | static int reuseaddr; | |
17 | ||
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] [directory...]"; | |
23 | ||
24 | /* List of acceptable pathname prefixes */ | |
25 | static char **ok_paths = NULL; | |
26 | static int strict_paths = 0; | |
27 | ||
28 | /* If this is set, git-daemon-export-ok is not required */ | |
29 | static int export_all_trees = 0; | |
30 | ||
31 | /* Take all paths relative to this one if non-NULL */ | |
32 | static char *base_path = NULL; | |
33 | ||
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 = NULL; | |
39 | ||
40 | /* Timeout, and initial timeout */ | |
41 | static unsigned int timeout = 0; | |
42 | static unsigned int init_timeout = 0; | |
43 | ||
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; | |
51 | ||
52 | /* sizeof(buf) should be big enough for "[pid] \n" */ | |
53 | buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid()); | |
54 | ||
55 | maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */ | |
56 | msglen = vsnprintf(buf + buflen, maxlen, err, params); | |
57 | ||
58 | if (log_syslog) { | |
59 | syslog(priority, "%s", buf); | |
60 | return; | |
61 | } | |
62 | ||
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; | |
73 | ||
74 | buf[buflen++] = '\n'; | |
75 | buf[buflen] = '\0'; | |
76 | ||
77 | write(2, buf, buflen); | |
78 | } | |
79 | ||
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 | } | |
87 | ||
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 | } | |
97 | ||
98 | static int avoid_alias(char *p) | |
99 | { | |
100 | int sl, ndot; | |
101 | ||
102 | /* | |
103 | * This resurrects the belts and suspenders paranoia check by HPA | |
104 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() | |
105 | * does not do getcwd() based path canonicalizations. | |
106 | * | |
107 | * sl becomes true immediately after seeing '/' and continues to | |
108 | * be true as long as dots continue after that without intervening | |
109 | * non-dot character. | |
110 | */ | |
111 | if (!p || (*p != '/' && *p != '~')) | |
112 | return -1; | |
113 | sl = 1; ndot = 0; | |
114 | p++; | |
115 | ||
116 | while (1) { | |
117 | char ch = *p++; | |
118 | if (sl) { | |
119 | if (ch == '.') | |
120 | ndot++; | |
121 | else if (ch == '/') { | |
122 | if (ndot < 3) | |
123 | /* reject //, /./ and /../ */ | |
124 | return -1; | |
125 | ndot = 0; | |
126 | } | |
127 | else if (ch == 0) { | |
128 | if (0 < ndot && ndot < 3) | |
129 | /* reject /.$ and /..$ */ | |
130 | return -1; | |
131 | return 0; | |
132 | } | |
133 | else | |
134 | sl = ndot = 0; | |
135 | } | |
136 | else if (ch == 0) | |
137 | return 0; | |
138 | else if (ch == '/') { | |
139 | sl = 1; | |
140 | ndot = 0; | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | static char *path_ok(char *dir) | |
146 | { | |
147 | static char rpath[PATH_MAX]; | |
148 | char *path; | |
149 | ||
150 | if (avoid_alias(dir)) { | |
151 | logerror("'%s': aliased", dir); | |
152 | return NULL; | |
153 | } | |
154 | ||
155 | if (*dir == '~') { | |
156 | if (!user_path) { | |
157 | logerror("'%s': User-path not allowed", dir); | |
158 | return NULL; | |
159 | } | |
160 | if (*user_path) { | |
161 | /* Got either "~alice" or "~alice/foo"; | |
162 | * rewrite them to "~alice/%s" or | |
163 | * "~alice/%s/foo". | |
164 | */ | |
165 | int namlen, restlen = strlen(dir); | |
166 | char *slash = strchr(dir, '/'); | |
167 | if (!slash) | |
168 | slash = dir + restlen; | |
169 | namlen = slash - dir; | |
170 | restlen -= namlen; | |
171 | loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash); | |
172 | snprintf(rpath, PATH_MAX, "%.*s/%s%.*s", | |
173 | namlen, dir, user_path, restlen, slash); | |
174 | dir = rpath; | |
175 | } | |
176 | } | |
177 | else if (base_path) { | |
178 | if (*dir != '/') { | |
179 | /* Allow only absolute */ | |
180 | logerror("'%s': Non-absolute path denied (base-path active)", dir); | |
181 | return NULL; | |
182 | } | |
183 | else { | |
184 | snprintf(rpath, PATH_MAX, "%s%s", base_path, dir); | |
185 | dir = rpath; | |
186 | } | |
187 | } | |
188 | ||
189 | path = enter_repo(dir, strict_paths); | |
190 | ||
191 | if (!path) { | |
192 | logerror("'%s': unable to chdir or not a git archive", dir); | |
193 | return NULL; | |
194 | } | |
195 | ||
196 | if ( ok_paths && *ok_paths ) { | |
197 | char **pp; | |
198 | int pathlen = strlen(path); | |
199 | ||
200 | /* The validation is done on the paths after enter_repo | |
201 | * appends optional {.git,.git/.git} and friends, but | |
202 | * it does not use getcwd(). So if your /pub is | |
203 | * a symlink to /mnt/pub, you can whitelist /pub and | |
204 | * do not have to say /mnt/pub. | |
205 | * Do not say /pub/. | |
206 | */ | |
207 | for ( pp = ok_paths ; *pp ; pp++ ) { | |
208 | int len = strlen(*pp); | |
209 | if (len <= pathlen && | |
210 | !memcmp(*pp, path, len) && | |
211 | (path[len] == '\0' || | |
212 | (!strict_paths && path[len] == '/'))) | |
213 | return path; | |
214 | } | |
215 | } | |
216 | else { | |
217 | /* be backwards compatible */ | |
218 | if (!strict_paths) | |
219 | return path; | |
220 | } | |
221 | ||
222 | logerror("'%s': not in whitelist", path); | |
223 | return NULL; /* Fallthrough. Deny by default */ | |
224 | } | |
225 | ||
226 | static int upload(char *dir) | |
227 | { | |
228 | /* Timeout as string */ | |
229 | char timeout_buf[64]; | |
230 | const char *path; | |
231 | ||
232 | loginfo("Request for '%s'", dir); | |
233 | ||
234 | if (!(path = path_ok(dir))) | |
235 | return -1; | |
236 | ||
237 | /* | |
238 | * Security on the cheap. | |
239 | * | |
240 | * We want a readable HEAD, usable "objects" directory, and | |
241 | * a "git-daemon-export-ok" flag that says that the other side | |
242 | * is ok with us doing this. | |
243 | * | |
244 | * path_ok() uses enter_repo() and does whitelist checking. | |
245 | * We only need to make sure the repository is exported. | |
246 | */ | |
247 | ||
248 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { | |
249 | logerror("'%s': repository not exported.", path); | |
250 | errno = EACCES; | |
251 | return -1; | |
252 | } | |
253 | ||
254 | /* | |
255 | * We'll ignore SIGTERM from now on, we have a | |
256 | * good client. | |
257 | */ | |
258 | signal(SIGTERM, SIG_IGN); | |
259 | ||
260 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); | |
261 | ||
262 | /* git-upload-pack only ever reads stuff, so this is safe */ | |
263 | execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL); | |
264 | return -1; | |
265 | } | |
266 | ||
267 | static int execute(struct sockaddr *addr) | |
268 | { | |
269 | static char line[1000]; | |
270 | int pktlen, len; | |
271 | ||
272 | if (addr) { | |
273 | char addrbuf[256] = ""; | |
274 | int port = -1; | |
275 | ||
276 | if (addr->sa_family == AF_INET) { | |
277 | struct sockaddr_in *sin_addr = (void *) addr; | |
278 | inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); | |
279 | port = sin_addr->sin_port; | |
280 | #ifndef NO_IPV6 | |
281 | } else if (addr && addr->sa_family == AF_INET6) { | |
282 | struct sockaddr_in6 *sin6_addr = (void *) addr; | |
283 | ||
284 | char *buf = addrbuf; | |
285 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ | |
286 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); | |
287 | strcat(buf, "]"); | |
288 | ||
289 | port = sin6_addr->sin6_port; | |
290 | #endif | |
291 | } | |
292 | loginfo("Connection from %s:%d", addrbuf, port); | |
293 | } | |
294 | ||
295 | alarm(init_timeout ? init_timeout : timeout); | |
296 | pktlen = packet_read_line(0, line, sizeof(line)); | |
297 | alarm(0); | |
298 | ||
299 | len = strlen(line); | |
300 | if (pktlen != len) | |
301 | loginfo("Extended attributes (%d bytes) exist <%.*s>", | |
302 | (int) pktlen - len, | |
303 | (int) pktlen - len, line + len + 1); | |
304 | if (len && line[len-1] == '\n') | |
305 | line[--len] = 0; | |
306 | ||
307 | if (!strncmp("git-upload-pack ", line, 16)) | |
308 | return upload(line+16); | |
309 | ||
310 | logerror("Protocol error: '%s'", line); | |
311 | return -1; | |
312 | } | |
313 | ||
314 | ||
315 | /* | |
316 | * We count spawned/reaped separately, just to avoid any | |
317 | * races when updating them from signals. The SIGCHLD handler | |
318 | * will only update children_reaped, and the fork logic will | |
319 | * only update children_spawned. | |
320 | * | |
321 | * MAX_CHILDREN should be a power-of-two to make the modulus | |
322 | * operation cheap. It should also be at least twice | |
323 | * the maximum number of connections we will ever allow. | |
324 | */ | |
325 | #define MAX_CHILDREN 128 | |
326 | ||
327 | static int max_connections = 25; | |
328 | ||
329 | /* These are updated by the signal handler */ | |
330 | static volatile unsigned int children_reaped = 0; | |
331 | static pid_t dead_child[MAX_CHILDREN]; | |
332 | ||
333 | /* These are updated by the main loop */ | |
334 | static unsigned int children_spawned = 0; | |
335 | static unsigned int children_deleted = 0; | |
336 | ||
337 | static struct child { | |
338 | pid_t pid; | |
339 | int addrlen; | |
340 | struct sockaddr_storage address; | |
341 | } live_child[MAX_CHILDREN]; | |
342 | ||
343 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) | |
344 | { | |
345 | live_child[idx].pid = pid; | |
346 | live_child[idx].addrlen = addrlen; | |
347 | memcpy(&live_child[idx].address, addr, addrlen); | |
348 | } | |
349 | ||
350 | /* | |
351 | * Walk from "deleted" to "spawned", and remove child "pid". | |
352 | * | |
353 | * We move everything up by one, since the new "deleted" will | |
354 | * be one higher. | |
355 | */ | |
356 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) | |
357 | { | |
358 | struct child n; | |
359 | ||
360 | deleted %= MAX_CHILDREN; | |
361 | spawned %= MAX_CHILDREN; | |
362 | if (live_child[deleted].pid == pid) { | |
363 | live_child[deleted].pid = -1; | |
364 | return; | |
365 | } | |
366 | n = live_child[deleted]; | |
367 | for (;;) { | |
368 | struct child m; | |
369 | deleted = (deleted + 1) % MAX_CHILDREN; | |
370 | if (deleted == spawned) | |
371 | die("could not find dead child %d\n", pid); | |
372 | m = live_child[deleted]; | |
373 | live_child[deleted] = n; | |
374 | if (m.pid == pid) | |
375 | return; | |
376 | n = m; | |
377 | } | |
378 | } | |
379 | ||
380 | /* | |
381 | * This gets called if the number of connections grows | |
382 | * past "max_connections". | |
383 | * | |
384 | * We _should_ start off by searching for connections | |
385 | * from the same IP, and if there is some address wth | |
386 | * multiple connections, we should kill that first. | |
387 | * | |
388 | * As it is, we just "randomly" kill 25% of the connections, | |
389 | * and our pseudo-random generator sucks too. I have no | |
390 | * shame. | |
391 | * | |
392 | * Really, this is just a place-holder for a _real_ algorithm. | |
393 | */ | |
394 | static void kill_some_children(int signo, unsigned start, unsigned stop) | |
395 | { | |
396 | start %= MAX_CHILDREN; | |
397 | stop %= MAX_CHILDREN; | |
398 | while (start != stop) { | |
399 | if (!(start & 3)) | |
400 | kill(live_child[start].pid, signo); | |
401 | start = (start + 1) % MAX_CHILDREN; | |
402 | } | |
403 | } | |
404 | ||
405 | static void check_max_connections(void) | |
406 | { | |
407 | for (;;) { | |
408 | int active; | |
409 | unsigned spawned, reaped, deleted; | |
410 | ||
411 | spawned = children_spawned; | |
412 | reaped = children_reaped; | |
413 | deleted = children_deleted; | |
414 | ||
415 | while (deleted < reaped) { | |
416 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; | |
417 | remove_child(pid, deleted, spawned); | |
418 | deleted++; | |
419 | } | |
420 | children_deleted = deleted; | |
421 | ||
422 | active = spawned - deleted; | |
423 | if (active <= max_connections) | |
424 | break; | |
425 | ||
426 | /* Kill some unstarted connections with SIGTERM */ | |
427 | kill_some_children(SIGTERM, deleted, spawned); | |
428 | if (active <= max_connections << 1) | |
429 | break; | |
430 | ||
431 | /* If the SIGTERM thing isn't helping use SIGKILL */ | |
432 | kill_some_children(SIGKILL, deleted, spawned); | |
433 | sleep(1); | |
434 | } | |
435 | } | |
436 | ||
437 | static void handle(int incoming, struct sockaddr *addr, int addrlen) | |
438 | { | |
439 | pid_t pid = fork(); | |
440 | ||
441 | if (pid) { | |
442 | unsigned idx; | |
443 | ||
444 | close(incoming); | |
445 | if (pid < 0) | |
446 | return; | |
447 | ||
448 | idx = children_spawned % MAX_CHILDREN; | |
449 | children_spawned++; | |
450 | add_child(idx, pid, addr, addrlen); | |
451 | ||
452 | check_max_connections(); | |
453 | return; | |
454 | } | |
455 | ||
456 | dup2(incoming, 0); | |
457 | dup2(incoming, 1); | |
458 | close(incoming); | |
459 | ||
460 | exit(execute(addr)); | |
461 | } | |
462 | ||
463 | static void child_handler(int signo) | |
464 | { | |
465 | for (;;) { | |
466 | int status; | |
467 | pid_t pid = waitpid(-1, &status, WNOHANG); | |
468 | ||
469 | if (pid > 0) { | |
470 | unsigned reaped = children_reaped; | |
471 | dead_child[reaped % MAX_CHILDREN] = pid; | |
472 | children_reaped = reaped + 1; | |
473 | /* XXX: Custom logging, since we don't wanna getpid() */ | |
474 | if (verbose) { | |
475 | const char *dead = ""; | |
476 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) | |
477 | dead = " (with error)"; | |
478 | if (log_syslog) | |
479 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); | |
480 | else | |
481 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); | |
482 | } | |
483 | continue; | |
484 | } | |
485 | break; | |
486 | } | |
487 | } | |
488 | ||
489 | static int set_reuse_addr(int sockfd) | |
490 | { | |
491 | int on = 1; | |
492 | ||
493 | if (!reuseaddr) | |
494 | return 0; | |
495 | return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, | |
496 | &on, sizeof(on)); | |
497 | } | |
498 | ||
499 | #ifndef NO_IPV6 | |
500 | ||
501 | static int socksetup(int port, int **socklist_p) | |
502 | { | |
503 | int socknum = 0, *socklist = NULL; | |
504 | int maxfd = -1; | |
505 | char pbuf[NI_MAXSERV]; | |
506 | ||
507 | struct addrinfo hints, *ai0, *ai; | |
508 | int gai; | |
509 | ||
510 | sprintf(pbuf, "%d", port); | |
511 | memset(&hints, 0, sizeof(hints)); | |
512 | hints.ai_family = AF_UNSPEC; | |
513 | hints.ai_socktype = SOCK_STREAM; | |
514 | hints.ai_protocol = IPPROTO_TCP; | |
515 | hints.ai_flags = AI_PASSIVE; | |
516 | ||
517 | gai = getaddrinfo(NULL, pbuf, &hints, &ai0); | |
518 | if (gai) | |
519 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); | |
520 | ||
521 | for (ai = ai0; ai; ai = ai->ai_next) { | |
522 | int sockfd; | |
523 | int *newlist; | |
524 | ||
525 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | |
526 | if (sockfd < 0) | |
527 | continue; | |
528 | if (sockfd >= FD_SETSIZE) { | |
529 | error("too large socket descriptor."); | |
530 | close(sockfd); | |
531 | continue; | |
532 | } | |
533 | ||
534 | #ifdef IPV6_V6ONLY | |
535 | if (ai->ai_family == AF_INET6) { | |
536 | int on = 1; | |
537 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, | |
538 | &on, sizeof(on)); | |
539 | /* Note: error is not fatal */ | |
540 | } | |
541 | #endif | |
542 | ||
543 | if (set_reuse_addr(sockfd)) { | |
544 | close(sockfd); | |
545 | continue; | |
546 | } | |
547 | ||
548 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { | |
549 | close(sockfd); | |
550 | continue; /* not fatal */ | |
551 | } | |
552 | if (listen(sockfd, 5) < 0) { | |
553 | close(sockfd); | |
554 | continue; /* not fatal */ | |
555 | } | |
556 | ||
557 | newlist = realloc(socklist, sizeof(int) * (socknum + 1)); | |
558 | if (!newlist) | |
559 | die("memory allocation failed: %s", strerror(errno)); | |
560 | ||
561 | socklist = newlist; | |
562 | socklist[socknum++] = sockfd; | |
563 | ||
564 | if (maxfd < sockfd) | |
565 | maxfd = sockfd; | |
566 | } | |
567 | ||
568 | freeaddrinfo(ai0); | |
569 | ||
570 | *socklist_p = socklist; | |
571 | return socknum; | |
572 | } | |
573 | ||
574 | #else /* NO_IPV6 */ | |
575 | ||
576 | static int socksetup(int port, int **socklist_p) | |
577 | { | |
578 | struct sockaddr_in sin; | |
579 | int sockfd; | |
580 | ||
581 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
582 | if (sockfd < 0) | |
583 | return 0; | |
584 | ||
585 | memset(&sin, 0, sizeof sin); | |
586 | sin.sin_family = AF_INET; | |
587 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
588 | sin.sin_port = htons(port); | |
589 | ||
590 | if (set_reuse_addr(sockfd)) { | |
591 | close(sockfd); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { | |
596 | close(sockfd); | |
597 | return 0; | |
598 | } | |
599 | ||
600 | if (listen(sockfd, 5) < 0) { | |
601 | close(sockfd); | |
602 | return 0; | |
603 | } | |
604 | ||
605 | *socklist_p = xmalloc(sizeof(int)); | |
606 | **socklist_p = sockfd; | |
607 | return 1; | |
608 | } | |
609 | ||
610 | #endif | |
611 | ||
612 | static int service_loop(int socknum, int *socklist) | |
613 | { | |
614 | struct pollfd *pfd; | |
615 | int i; | |
616 | ||
617 | pfd = xcalloc(socknum, sizeof(struct pollfd)); | |
618 | ||
619 | for (i = 0; i < socknum; i++) { | |
620 | pfd[i].fd = socklist[i]; | |
621 | pfd[i].events = POLLIN; | |
622 | } | |
623 | ||
624 | signal(SIGCHLD, child_handler); | |
625 | ||
626 | for (;;) { | |
627 | int i; | |
628 | ||
629 | if (poll(pfd, socknum, -1) < 0) { | |
630 | if (errno != EINTR) { | |
631 | error("poll failed, resuming: %s", | |
632 | strerror(errno)); | |
633 | sleep(1); | |
634 | } | |
635 | continue; | |
636 | } | |
637 | ||
638 | for (i = 0; i < socknum; i++) { | |
639 | if (pfd[i].revents & POLLIN) { | |
640 | struct sockaddr_storage ss; | |
641 | unsigned int sslen = sizeof(ss); | |
642 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); | |
643 | if (incoming < 0) { | |
644 | switch (errno) { | |
645 | case EAGAIN: | |
646 | case EINTR: | |
647 | case ECONNABORTED: | |
648 | continue; | |
649 | default: | |
650 | die("accept returned %s", strerror(errno)); | |
651 | } | |
652 | } | |
653 | handle(incoming, (struct sockaddr *)&ss, sslen); | |
654 | } | |
655 | } | |
656 | } | |
657 | } | |
658 | ||
659 | static int serve(int port) | |
660 | { | |
661 | int socknum, *socklist; | |
662 | ||
663 | socknum = socksetup(port, &socklist); | |
664 | if (socknum == 0) | |
665 | die("unable to allocate any listen sockets on port %u", port); | |
666 | ||
667 | return service_loop(socknum, socklist); | |
668 | } | |
669 | ||
670 | int main(int argc, char **argv) | |
671 | { | |
672 | int port = DEFAULT_GIT_PORT; | |
673 | int inetd_mode = 0; | |
674 | int i; | |
675 | ||
676 | /* Without this we cannot rely on waitpid() to tell | |
677 | * what happened to our children. | |
678 | */ | |
679 | signal(SIGCHLD, SIG_DFL); | |
680 | ||
681 | for (i = 1; i < argc; i++) { | |
682 | char *arg = argv[i]; | |
683 | ||
684 | if (!strncmp(arg, "--port=", 7)) { | |
685 | char *end; | |
686 | unsigned long n; | |
687 | n = strtoul(arg+7, &end, 0); | |
688 | if (arg[7] && !*end) { | |
689 | port = n; | |
690 | continue; | |
691 | } | |
692 | } | |
693 | if (!strcmp(arg, "--inetd")) { | |
694 | inetd_mode = 1; | |
695 | log_syslog = 1; | |
696 | continue; | |
697 | } | |
698 | if (!strcmp(arg, "--verbose")) { | |
699 | verbose = 1; | |
700 | continue; | |
701 | } | |
702 | if (!strcmp(arg, "--syslog")) { | |
703 | log_syslog = 1; | |
704 | continue; | |
705 | } | |
706 | if (!strcmp(arg, "--export-all")) { | |
707 | export_all_trees = 1; | |
708 | continue; | |
709 | } | |
710 | if (!strncmp(arg, "--timeout=", 10)) { | |
711 | timeout = atoi(arg+10); | |
712 | continue; | |
713 | } | |
714 | if (!strncmp(arg, "--init-timeout=", 15)) { | |
715 | init_timeout = atoi(arg+15); | |
716 | continue; | |
717 | } | |
718 | if (!strcmp(arg, "--strict-paths")) { | |
719 | strict_paths = 1; | |
720 | continue; | |
721 | } | |
722 | if (!strncmp(arg, "--base-path=", 12)) { | |
723 | base_path = arg+12; | |
724 | continue; | |
725 | } | |
726 | if (!strcmp(arg, "--reuseaddr")) { | |
727 | reuseaddr = 1; | |
728 | continue; | |
729 | } | |
730 | if (!strcmp(arg, "--user-path")) { | |
731 | user_path = ""; | |
732 | continue; | |
733 | } | |
734 | if (!strncmp(arg, "--user-path=", 12)) { | |
735 | user_path = arg + 12; | |
736 | continue; | |
737 | } | |
738 | if (!strcmp(arg, "--")) { | |
739 | ok_paths = &argv[i+1]; | |
740 | break; | |
741 | } else if (arg[0] != '-') { | |
742 | ok_paths = &argv[i]; | |
743 | break; | |
744 | } | |
745 | ||
746 | usage(daemon_usage); | |
747 | } | |
748 | ||
749 | if (log_syslog) | |
750 | openlog("git-daemon", 0, LOG_DAEMON); | |
751 | ||
752 | if (strict_paths && (!ok_paths || !*ok_paths)) { | |
753 | if (!inetd_mode) | |
754 | die("git-daemon: option --strict-paths requires a whitelist"); | |
755 | ||
756 | logerror("option --strict-paths requires a whitelist"); | |
757 | exit (1); | |
758 | } | |
759 | ||
760 | if (inetd_mode) { | |
761 | struct sockaddr_storage ss; | |
762 | struct sockaddr *peer = (struct sockaddr *)&ss; | |
763 | socklen_t slen = sizeof(ss); | |
764 | ||
765 | freopen("/dev/null", "w", stderr); | |
766 | ||
767 | if (getpeername(0, peer, &slen)) | |
768 | peer = NULL; | |
769 | ||
770 | return execute(peer); | |
771 | } | |
772 | ||
773 | return serve(port); | |
774 | } |