]>
Commit | Line | Data |
---|---|---|
eaa94919 LT |
1 | #include <signal.h> |
2 | #include <sys/wait.h> | |
a87e8be2 | 3 | #include <sys/socket.h> |
3cd6ecda | 4 | #include <sys/time.h> |
6573faff | 5 | #include <sys/poll.h> |
df076bdb | 6 | #include <netdb.h> |
a87e8be2 | 7 | #include <netinet/in.h> |
f8ff0c06 | 8 | #include <arpa/inet.h> |
9048fe1c | 9 | #include <syslog.h> |
979e32fa RS |
10 | #include "pkt-line.h" |
11 | #include "cache.h" | |
77cb17e9 | 12 | #include "exec_cmd.h" |
f8ff0c06 | 13 | |
9048fe1c | 14 | static int log_syslog; |
f8ff0c06 | 15 | static int verbose; |
1955fabf | 16 | static int reuseaddr; |
f8ff0c06 | 17 | |
960deccb PA |
18 | static const char daemon_usage[] = |
19 | "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n" | |
b21c31c9 | 20 | " [--timeout=n] [--init-timeout=n] [--strict-paths]\n" |
603968d2 JH |
21 | " [--base-path=path] [--user-path | --user-path=path]\n" |
22 | " [--reuseaddr] [directory...]"; | |
4ae95682 PA |
23 | |
24 | /* List of acceptable pathname prefixes */ | |
25 | static char **ok_paths = NULL; | |
4dbd1352 | 26 | static int strict_paths = 0; |
4ae95682 PA |
27 | |
28 | /* If this is set, git-daemon-export-ok is not required */ | |
29 | static int export_all_trees = 0; | |
f8ff0c06 | 30 | |
b21c31c9 PB |
31 | /* Take all paths relative to this one if non-NULL */ |
32 | static char *base_path = NULL; | |
33 | ||
603968d2 JH |
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 char *user_path = NULL; | |
39 | ||
960deccb PA |
40 | /* Timeout, and initial timeout */ |
41 | static unsigned int timeout = 0; | |
42 | static unsigned int init_timeout = 0; | |
f8ff0c06 | 43 | |
9048fe1c | 44 | static void logreport(int priority, const char *err, va_list params) |
f8ff0c06 PB |
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" */ | |
1bedd4ca | 53 | buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid()); |
f8ff0c06 PB |
54 | |
55 | maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */ | |
56 | msglen = vsnprintf(buf + buflen, maxlen, err, params); | |
57 | ||
9048fe1c PB |
58 | if (log_syslog) { |
59 | syslog(priority, "%s", buf); | |
60 | return; | |
61 | } | |
62 | ||
f8ff0c06 PB |
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 | ||
cdda4745 | 80 | static void logerror(const char *err, ...) |
f8ff0c06 PB |
81 | { |
82 | va_list params; | |
83 | va_start(params, err); | |
9048fe1c | 84 | logreport(LOG_ERR, err, params); |
f8ff0c06 PB |
85 | va_end(params); |
86 | } | |
87 | ||
cdda4745 | 88 | static void loginfo(const char *err, ...) |
f8ff0c06 PB |
89 | { |
90 | va_list params; | |
91 | if (!verbose) | |
92 | return; | |
93 | va_start(params, err); | |
9048fe1c | 94 | logreport(LOG_INFO, err, params); |
f8ff0c06 PB |
95 | va_end(params); |
96 | } | |
a87e8be2 | 97 | |
d79374c7 JH |
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 | ||
4dbd1352 | 145 | static char *path_ok(char *dir) |
4ae95682 | 146 | { |
603968d2 | 147 | static char rpath[PATH_MAX]; |
d79374c7 JH |
148 | char *path; |
149 | ||
150 | if (avoid_alias(dir)) { | |
151 | logerror("'%s': aliased", dir); | |
152 | return NULL; | |
153 | } | |
154 | ||
603968d2 JH |
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 */ | |
1fda3d55 | 180 | logerror("'%s': Non-absolute path denied (base-path active)", dir); |
b21c31c9 PB |
181 | return NULL; |
182 | } | |
363f24c9 JH |
183 | else { |
184 | snprintf(rpath, PATH_MAX, "%s%s", base_path, dir); | |
185 | dir = rpath; | |
186 | } | |
b21c31c9 PB |
187 | } |
188 | ||
d79374c7 | 189 | path = enter_repo(dir, strict_paths); |
3e04c62d | 190 | |
4dbd1352 AE |
191 | if (!path) { |
192 | logerror("'%s': unable to chdir or not a git archive", dir); | |
193 | return NULL; | |
4ae95682 PA |
194 | } |
195 | ||
196 | if ( ok_paths && *ok_paths ) { | |
ce335fe0 | 197 | char **pp; |
4dbd1352 | 198 | int pathlen = strlen(path); |
4ae95682 | 199 | |
ce335fe0 | 200 | /* The validation is done on the paths after enter_repo |
d79374c7 JH |
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/. | |
ce335fe0 | 206 | */ |
4ae95682 PA |
207 | for ( pp = ok_paths ; *pp ; pp++ ) { |
208 | int len = strlen(*pp); | |
ce335fe0 JH |
209 | if (len <= pathlen && |
210 | !memcmp(*pp, path, len) && | |
211 | (path[len] == '\0' || | |
212 | (!strict_paths && path[len] == '/'))) | |
213 | return path; | |
4ae95682 | 214 | } |
4dbd1352 AE |
215 | } |
216 | else { | |
217 | /* be backwards compatible */ | |
218 | if (!strict_paths) | |
219 | return path; | |
4ae95682 PA |
220 | } |
221 | ||
4dbd1352 AE |
222 | logerror("'%s': not in whitelist", path); |
223 | return NULL; /* Fallthrough. Deny by default */ | |
4ae95682 | 224 | } |
a87e8be2 | 225 | |
4dbd1352 | 226 | static int upload(char *dir) |
a87e8be2 | 227 | { |
4dbd1352 AE |
228 | /* Timeout as string */ |
229 | char timeout_buf[64]; | |
230 | const char *path; | |
231 | ||
232 | loginfo("Request for '%s'", dir); | |
4ae95682 | 233 | |
4dbd1352 | 234 | if (!(path = path_ok(dir))) |
a87e8be2 | 235 | return -1; |
47888f0f | 236 | |
a87e8be2 LT |
237 | /* |
238 | * Security on the cheap. | |
239 | * | |
a935c397 | 240 | * We want a readable HEAD, usable "objects" directory, and |
a87e8be2 LT |
241 | * a "git-daemon-export-ok" flag that says that the other side |
242 | * is ok with us doing this. | |
4dbd1352 AE |
243 | * |
244 | * path_ok() uses enter_repo() and does whitelist checking. | |
245 | * We only need to make sure the repository is exported. | |
a87e8be2 | 246 | */ |
4dbd1352 | 247 | |
3e04c62d | 248 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { |
4dbd1352 | 249 | logerror("'%s': repository not exported.", path); |
3e04c62d PA |
250 | errno = EACCES; |
251 | return -1; | |
252 | } | |
253 | ||
02d57da4 LT |
254 | /* |
255 | * We'll ignore SIGTERM from now on, we have a | |
256 | * good client. | |
257 | */ | |
258 | signal(SIGTERM, SIG_IGN); | |
259 | ||
960deccb PA |
260 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); |
261 | ||
a87e8be2 | 262 | /* git-upload-pack only ever reads stuff, so this is safe */ |
77cb17e9 | 263 | execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL); |
a87e8be2 LT |
264 | return -1; |
265 | } | |
266 | ||
7d80694a | 267 | static int execute(void) |
a87e8be2 | 268 | { |
7d80694a | 269 | static char line[1000]; |
5ad312be | 270 | int pktlen, len; |
7d80694a | 271 | |
960deccb | 272 | alarm(init_timeout ? init_timeout : timeout); |
5ad312be | 273 | pktlen = packet_read_line(0, line, sizeof(line)); |
960deccb | 274 | alarm(0); |
7d80694a | 275 | |
5ad312be JL |
276 | len = strlen(line); |
277 | if (pktlen != len) | |
278 | loginfo("Extended attributes (%d bytes) exist <%.*s>", | |
279 | (int) pktlen - len, | |
280 | (int) pktlen - len, line + len + 1); | |
7d80694a LT |
281 | if (len && line[len-1] == '\n') |
282 | line[--len] = 0; | |
283 | ||
4dbd1352 | 284 | if (!strncmp("git-upload-pack ", line, 16)) |
3e04c62d | 285 | return upload(line+16); |
a87e8be2 | 286 | |
f8ff0c06 | 287 | logerror("Protocol error: '%s'", line); |
a87e8be2 LT |
288 | return -1; |
289 | } | |
290 | ||
66e631de LT |
291 | |
292 | /* | |
293 | * We count spawned/reaped separately, just to avoid any | |
294 | * races when updating them from signals. The SIGCHLD handler | |
295 | * will only update children_reaped, and the fork logic will | |
296 | * only update children_spawned. | |
297 | * | |
298 | * MAX_CHILDREN should be a power-of-two to make the modulus | |
299 | * operation cheap. It should also be at least twice | |
300 | * the maximum number of connections we will ever allow. | |
301 | */ | |
302 | #define MAX_CHILDREN 128 | |
303 | ||
304 | static int max_connections = 25; | |
305 | ||
306 | /* These are updated by the signal handler */ | |
307 | static volatile unsigned int children_reaped = 0; | |
4d8fa916 | 308 | static pid_t dead_child[MAX_CHILDREN]; |
66e631de LT |
309 | |
310 | /* These are updated by the main loop */ | |
311 | static unsigned int children_spawned = 0; | |
312 | static unsigned int children_deleted = 0; | |
313 | ||
4d8fa916 | 314 | static struct child { |
66e631de | 315 | pid_t pid; |
7fa09084 | 316 | int addrlen; |
df076bdb | 317 | struct sockaddr_storage address; |
66e631de LT |
318 | } live_child[MAX_CHILDREN]; |
319 | ||
7fa09084 | 320 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) |
66e631de LT |
321 | { |
322 | live_child[idx].pid = pid; | |
323 | live_child[idx].addrlen = addrlen; | |
df076bdb | 324 | memcpy(&live_child[idx].address, addr, addrlen); |
66e631de LT |
325 | } |
326 | ||
327 | /* | |
328 | * Walk from "deleted" to "spawned", and remove child "pid". | |
329 | * | |
330 | * We move everything up by one, since the new "deleted" will | |
331 | * be one higher. | |
332 | */ | |
333 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) | |
334 | { | |
335 | struct child n; | |
336 | ||
337 | deleted %= MAX_CHILDREN; | |
338 | spawned %= MAX_CHILDREN; | |
339 | if (live_child[deleted].pid == pid) { | |
340 | live_child[deleted].pid = -1; | |
341 | return; | |
342 | } | |
343 | n = live_child[deleted]; | |
344 | for (;;) { | |
345 | struct child m; | |
346 | deleted = (deleted + 1) % MAX_CHILDREN; | |
347 | if (deleted == spawned) | |
348 | die("could not find dead child %d\n", pid); | |
349 | m = live_child[deleted]; | |
350 | live_child[deleted] = n; | |
351 | if (m.pid == pid) | |
352 | return; | |
353 | n = m; | |
354 | } | |
355 | } | |
356 | ||
357 | /* | |
358 | * This gets called if the number of connections grows | |
359 | * past "max_connections". | |
360 | * | |
361 | * We _should_ start off by searching for connections | |
362 | * from the same IP, and if there is some address wth | |
363 | * multiple connections, we should kill that first. | |
364 | * | |
365 | * As it is, we just "randomly" kill 25% of the connections, | |
366 | * and our pseudo-random generator sucks too. I have no | |
367 | * shame. | |
368 | * | |
369 | * Really, this is just a place-holder for a _real_ algorithm. | |
370 | */ | |
02d57da4 | 371 | static void kill_some_children(int signo, unsigned start, unsigned stop) |
66e631de LT |
372 | { |
373 | start %= MAX_CHILDREN; | |
374 | stop %= MAX_CHILDREN; | |
375 | while (start != stop) { | |
376 | if (!(start & 3)) | |
02d57da4 | 377 | kill(live_child[start].pid, signo); |
66e631de LT |
378 | start = (start + 1) % MAX_CHILDREN; |
379 | } | |
380 | } | |
381 | ||
02d57da4 | 382 | static void check_max_connections(void) |
a87e8be2 | 383 | { |
02d57da4 | 384 | for (;;) { |
eaa94919 | 385 | int active; |
66e631de | 386 | unsigned spawned, reaped, deleted; |
eaa94919 | 387 | |
66e631de | 388 | spawned = children_spawned; |
66e631de LT |
389 | reaped = children_reaped; |
390 | deleted = children_deleted; | |
391 | ||
392 | while (deleted < reaped) { | |
393 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; | |
394 | remove_child(pid, deleted, spawned); | |
395 | deleted++; | |
396 | } | |
397 | children_deleted = deleted; | |
398 | ||
399 | active = spawned - deleted; | |
02d57da4 LT |
400 | if (active <= max_connections) |
401 | break; | |
66e631de | 402 | |
02d57da4 LT |
403 | /* Kill some unstarted connections with SIGTERM */ |
404 | kill_some_children(SIGTERM, deleted, spawned); | |
405 | if (active <= max_connections << 1) | |
406 | break; | |
407 | ||
408 | /* If the SIGTERM thing isn't helping use SIGKILL */ | |
409 | kill_some_children(SIGKILL, deleted, spawned); | |
410 | sleep(1); | |
411 | } | |
412 | } | |
413 | ||
7fa09084 | 414 | static void handle(int incoming, struct sockaddr *addr, int addrlen) |
02d57da4 LT |
415 | { |
416 | pid_t pid = fork(); | |
f8ff0c06 PB |
417 | char addrbuf[256] = ""; |
418 | int port = -1; | |
02d57da4 LT |
419 | |
420 | if (pid) { | |
421 | unsigned idx; | |
422 | ||
423 | close(incoming); | |
424 | if (pid < 0) | |
425 | return; | |
426 | ||
427 | idx = children_spawned % MAX_CHILDREN; | |
428 | children_spawned++; | |
429 | add_child(idx, pid, addr, addrlen); | |
66e631de | 430 | |
02d57da4 | 431 | check_max_connections(); |
a87e8be2 LT |
432 | return; |
433 | } | |
434 | ||
435 | dup2(incoming, 0); | |
436 | dup2(incoming, 1); | |
437 | close(incoming); | |
f8ff0c06 PB |
438 | |
439 | if (addr->sa_family == AF_INET) { | |
440 | struct sockaddr_in *sin_addr = (void *) addr; | |
441 | inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); | |
442 | port = sin_addr->sin_port; | |
443 | ||
6573faff | 444 | #ifndef NO_IPV6 |
f8ff0c06 PB |
445 | } else if (addr->sa_family == AF_INET6) { |
446 | struct sockaddr_in6 *sin6_addr = (void *) addr; | |
447 | ||
448 | char *buf = addrbuf; | |
449 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ | |
450 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); | |
451 | strcat(buf, "]"); | |
452 | ||
453 | port = sin6_addr->sin6_port; | |
6573faff | 454 | #endif |
f8ff0c06 | 455 | } |
da38641d | 456 | loginfo("Connection from %s:%d", addrbuf, port); |
f8ff0c06 | 457 | |
7d80694a | 458 | exit(execute()); |
a87e8be2 LT |
459 | } |
460 | ||
eaa94919 LT |
461 | static void child_handler(int signo) |
462 | { | |
463 | for (;;) { | |
9048fe1c PB |
464 | int status; |
465 | pid_t pid = waitpid(-1, &status, WNOHANG); | |
66e631de LT |
466 | |
467 | if (pid > 0) { | |
468 | unsigned reaped = children_reaped; | |
469 | dead_child[reaped % MAX_CHILDREN] = pid; | |
470 | children_reaped = reaped + 1; | |
f8ff0c06 | 471 | /* XXX: Custom logging, since we don't wanna getpid() */ |
9048fe1c PB |
472 | if (verbose) { |
473 | char *dead = ""; | |
474 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) | |
475 | dead = " (with error)"; | |
476 | if (log_syslog) | |
477 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); | |
478 | else | |
479 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); | |
480 | } | |
eaa94919 LT |
481 | continue; |
482 | } | |
483 | break; | |
484 | } | |
485 | } | |
486 | ||
1955fabf MW |
487 | static int set_reuse_addr(int sockfd) |
488 | { | |
489 | int on = 1; | |
490 | ||
491 | if (!reuseaddr) | |
492 | return 0; | |
493 | return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, | |
494 | &on, sizeof(on)); | |
495 | } | |
496 | ||
6573faff PA |
497 | #ifndef NO_IPV6 |
498 | ||
499 | static int socksetup(int port, int **socklist_p) | |
a87e8be2 | 500 | { |
df076bdb YH |
501 | int socknum = 0, *socklist = NULL; |
502 | int maxfd = -1; | |
df076bdb | 503 | char pbuf[NI_MAXSERV]; |
a87e8be2 | 504 | |
6573faff PA |
505 | struct addrinfo hints, *ai0, *ai; |
506 | int gai; | |
df076bdb YH |
507 | |
508 | sprintf(pbuf, "%d", port); | |
509 | memset(&hints, 0, sizeof(hints)); | |
510 | hints.ai_family = AF_UNSPEC; | |
511 | hints.ai_socktype = SOCK_STREAM; | |
512 | hints.ai_protocol = IPPROTO_TCP; | |
513 | hints.ai_flags = AI_PASSIVE; | |
514 | ||
515 | gai = getaddrinfo(NULL, pbuf, &hints, &ai0); | |
516 | if (gai) | |
517 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); | |
518 | ||
df076bdb YH |
519 | for (ai = ai0; ai; ai = ai->ai_next) { |
520 | int sockfd; | |
521 | int *newlist; | |
522 | ||
523 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | |
524 | if (sockfd < 0) | |
525 | continue; | |
526 | if (sockfd >= FD_SETSIZE) { | |
527 | error("too large socket descriptor."); | |
528 | close(sockfd); | |
529 | continue; | |
530 | } | |
531 | ||
532 | #ifdef IPV6_V6ONLY | |
533 | if (ai->ai_family == AF_INET6) { | |
534 | int on = 1; | |
535 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, | |
536 | &on, sizeof(on)); | |
537 | /* Note: error is not fatal */ | |
538 | } | |
539 | #endif | |
540 | ||
1955fabf MW |
541 | if (set_reuse_addr(sockfd)) { |
542 | close(sockfd); | |
0032d548 | 543 | continue; |
1955fabf MW |
544 | } |
545 | ||
df076bdb YH |
546 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
547 | close(sockfd); | |
548 | continue; /* not fatal */ | |
549 | } | |
550 | if (listen(sockfd, 5) < 0) { | |
551 | close(sockfd); | |
552 | continue; /* not fatal */ | |
553 | } | |
554 | ||
555 | newlist = realloc(socklist, sizeof(int) * (socknum + 1)); | |
556 | if (!newlist) | |
557 | die("memory allocation failed: %s", strerror(errno)); | |
558 | ||
559 | socklist = newlist; | |
560 | socklist[socknum++] = sockfd; | |
561 | ||
df076bdb YH |
562 | if (maxfd < sockfd) |
563 | maxfd = sockfd; | |
564 | } | |
565 | ||
566 | freeaddrinfo(ai0); | |
567 | ||
6573faff PA |
568 | *socklist_p = socklist; |
569 | return socknum; | |
570 | } | |
571 | ||
572 | #else /* NO_IPV6 */ | |
573 | ||
574 | static int socksetup(int port, int **socklist_p) | |
575 | { | |
576 | struct sockaddr_in sin; | |
577 | int sockfd; | |
578 | ||
579 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
580 | if (sockfd < 0) | |
581 | return 0; | |
582 | ||
583 | memset(&sin, 0, sizeof sin); | |
584 | sin.sin_family = AF_INET; | |
585 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
586 | sin.sin_port = htons(port); | |
587 | ||
1955fabf MW |
588 | if (set_reuse_addr(sockfd)) { |
589 | close(sockfd); | |
590 | return 0; | |
591 | } | |
592 | ||
6573faff PA |
593 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { |
594 | close(sockfd); | |
595 | return 0; | |
596 | } | |
a87e8be2 | 597 | |
f35230fb PS |
598 | if (listen(sockfd, 5) < 0) { |
599 | close(sockfd); | |
600 | return 0; | |
601 | } | |
602 | ||
1b4713fb | 603 | *socklist_p = xmalloc(sizeof(int)); |
6573faff | 604 | **socklist_p = sockfd; |
f35230fb | 605 | return 1; |
6573faff PA |
606 | } |
607 | ||
608 | #endif | |
609 | ||
610 | static int service_loop(int socknum, int *socklist) | |
611 | { | |
612 | struct pollfd *pfd; | |
613 | int i; | |
614 | ||
1b4713fb | 615 | pfd = xcalloc(socknum, sizeof(struct pollfd)); |
6573faff PA |
616 | |
617 | for (i = 0; i < socknum; i++) { | |
618 | pfd[i].fd = socklist[i]; | |
619 | pfd[i].events = POLLIN; | |
620 | } | |
9220282a PA |
621 | |
622 | signal(SIGCHLD, child_handler); | |
a87e8be2 LT |
623 | |
624 | for (;;) { | |
df076bdb | 625 | int i; |
6573faff | 626 | |
7872e055 | 627 | if (poll(pfd, socknum, -1) < 0) { |
1eef0b33 | 628 | if (errno != EINTR) { |
6573faff | 629 | error("poll failed, resuming: %s", |
1eef0b33 JH |
630 | strerror(errno)); |
631 | sleep(1); | |
632 | } | |
df076bdb YH |
633 | continue; |
634 | } | |
635 | ||
636 | for (i = 0; i < socknum; i++) { | |
6573faff | 637 | if (pfd[i].revents & POLLIN) { |
df076bdb | 638 | struct sockaddr_storage ss; |
7626e49e | 639 | unsigned int sslen = sizeof(ss); |
6573faff | 640 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); |
df076bdb YH |
641 | if (incoming < 0) { |
642 | switch (errno) { | |
643 | case EAGAIN: | |
644 | case EINTR: | |
645 | case ECONNABORTED: | |
646 | continue; | |
647 | default: | |
648 | die("accept returned %s", strerror(errno)); | |
649 | } | |
650 | } | |
651 | handle(incoming, (struct sockaddr *)&ss, sslen); | |
a87e8be2 LT |
652 | } |
653 | } | |
a87e8be2 LT |
654 | } |
655 | } | |
656 | ||
6573faff PA |
657 | static int serve(int port) |
658 | { | |
659 | int socknum, *socklist; | |
4ae22d96 | 660 | |
6573faff PA |
661 | socknum = socksetup(port, &socklist); |
662 | if (socknum == 0) | |
663 | die("unable to allocate any listen sockets on port %u", port); | |
4ae22d96 | 664 | |
6573faff | 665 | return service_loop(socknum, socklist); |
4ae22d96 | 666 | } |
6573faff | 667 | |
a87e8be2 LT |
668 | int main(int argc, char **argv) |
669 | { | |
670 | int port = DEFAULT_GIT_PORT; | |
e64e1b79 | 671 | int inetd_mode = 0; |
a87e8be2 LT |
672 | int i; |
673 | ||
674 | for (i = 1; i < argc; i++) { | |
675 | char *arg = argv[i]; | |
676 | ||
677 | if (!strncmp(arg, "--port=", 7)) { | |
678 | char *end; | |
679 | unsigned long n; | |
680 | n = strtoul(arg+7, &end, 0); | |
681 | if (arg[7] && !*end) { | |
682 | port = n; | |
683 | continue; | |
684 | } | |
685 | } | |
e64e1b79 LT |
686 | if (!strcmp(arg, "--inetd")) { |
687 | inetd_mode = 1; | |
a8883288 | 688 | log_syslog = 1; |
e64e1b79 LT |
689 | continue; |
690 | } | |
f8ff0c06 PB |
691 | if (!strcmp(arg, "--verbose")) { |
692 | verbose = 1; | |
693 | continue; | |
694 | } | |
9048fe1c PB |
695 | if (!strcmp(arg, "--syslog")) { |
696 | log_syslog = 1; | |
9048fe1c PB |
697 | continue; |
698 | } | |
4ae95682 PA |
699 | if (!strcmp(arg, "--export-all")) { |
700 | export_all_trees = 1; | |
701 | continue; | |
702 | } | |
960deccb PA |
703 | if (!strncmp(arg, "--timeout=", 10)) { |
704 | timeout = atoi(arg+10); | |
a8883288 | 705 | continue; |
960deccb | 706 | } |
54e31a20 | 707 | if (!strncmp(arg, "--init-timeout=", 15)) { |
960deccb | 708 | init_timeout = atoi(arg+15); |
a8883288 | 709 | continue; |
960deccb | 710 | } |
4dbd1352 AE |
711 | if (!strcmp(arg, "--strict-paths")) { |
712 | strict_paths = 1; | |
713 | continue; | |
714 | } | |
b21c31c9 PB |
715 | if (!strncmp(arg, "--base-path=", 12)) { |
716 | base_path = arg+12; | |
717 | continue; | |
718 | } | |
1955fabf MW |
719 | if (!strcmp(arg, "--reuseaddr")) { |
720 | reuseaddr = 1; | |
721 | continue; | |
722 | } | |
603968d2 JH |
723 | if (!strcmp(arg, "--user-path")) { |
724 | user_path = ""; | |
725 | continue; | |
726 | } | |
727 | if (!strncmp(arg, "--user-path=", 12)) { | |
728 | user_path = arg + 12; | |
729 | continue; | |
730 | } | |
4ae95682 PA |
731 | if (!strcmp(arg, "--")) { |
732 | ok_paths = &argv[i+1]; | |
733 | break; | |
734 | } else if (arg[0] != '-') { | |
735 | ok_paths = &argv[i]; | |
736 | break; | |
737 | } | |
e64e1b79 | 738 | |
a87e8be2 LT |
739 | usage(daemon_usage); |
740 | } | |
741 | ||
a8883288 AE |
742 | if (log_syslog) |
743 | openlog("git-daemon", 0, LOG_DAEMON); | |
744 | ||
4dbd1352 AE |
745 | if (strict_paths && (!ok_paths || !*ok_paths)) { |
746 | if (!inetd_mode) | |
747 | die("git-daemon: option --strict-paths requires a whitelist"); | |
748 | ||
749 | logerror("option --strict-paths requires a whitelist"); | |
750 | exit (1); | |
751 | } | |
752 | ||
7c3693f1 LD |
753 | if (inetd_mode) { |
754 | fclose(stderr); //FIXME: workaround | |
e64e1b79 | 755 | return execute(); |
7c3693f1 | 756 | } |
bce8230d AE |
757 | |
758 | return serve(port); | |
a87e8be2 | 759 | } |