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