]>
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 | |
d819e4e6 JH |
235 | typedef int (*daemon_service_fn)(void); |
236 | struct daemon_service { | |
237 | const char *name; | |
238 | const char *config_name; | |
239 | daemon_service_fn fn; | |
240 | int enabled; | |
241 | int overridable; | |
242 | }; | |
243 | ||
244 | static struct daemon_service *service_looking_at; | |
245 | static int service_enabled; | |
246 | ||
247 | static int git_daemon_config(const char *var, const char *value) | |
248 | { | |
249 | if (!strncmp(var, "daemon.", 7) && | |
250 | !strcmp(var + 7, service_looking_at->config_name)) { | |
251 | service_enabled = git_config_bool(var, value); | |
252 | return 0; | |
253 | } | |
254 | ||
255 | /* we are not interested in parsing any other configuration here */ | |
256 | return 0; | |
257 | } | |
258 | ||
259 | static int run_service(char *dir, struct daemon_service *service) | |
a87e8be2 | 260 | { |
4dbd1352 | 261 | const char *path; |
d819e4e6 JH |
262 | int enabled = service->enabled; |
263 | ||
264 | loginfo("Request %s for '%s'", service->name, dir); | |
4dbd1352 | 265 | |
d819e4e6 JH |
266 | if (!enabled && !service->overridable) { |
267 | logerror("'%s': service not enabled.", service->name); | |
268 | errno = EACCES; | |
269 | return -1; | |
270 | } | |
4ae95682 | 271 | |
4dbd1352 | 272 | if (!(path = path_ok(dir))) |
a87e8be2 | 273 | return -1; |
47888f0f | 274 | |
a87e8be2 LT |
275 | /* |
276 | * Security on the cheap. | |
277 | * | |
a935c397 | 278 | * We want a readable HEAD, usable "objects" directory, and |
a87e8be2 LT |
279 | * a "git-daemon-export-ok" flag that says that the other side |
280 | * is ok with us doing this. | |
4dbd1352 AE |
281 | * |
282 | * path_ok() uses enter_repo() and does whitelist checking. | |
283 | * We only need to make sure the repository is exported. | |
a87e8be2 | 284 | */ |
4dbd1352 | 285 | |
3e04c62d | 286 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { |
4dbd1352 | 287 | logerror("'%s': repository not exported.", path); |
3e04c62d PA |
288 | errno = EACCES; |
289 | return -1; | |
290 | } | |
291 | ||
d819e4e6 JH |
292 | if (service->overridable) { |
293 | service_looking_at = service; | |
294 | service_enabled = -1; | |
295 | git_config(git_daemon_config); | |
296 | if (0 <= service_enabled) | |
297 | enabled = service_enabled; | |
298 | } | |
299 | if (!enabled) { | |
300 | logerror("'%s': service not enabled for '%s'", | |
301 | service->name, path); | |
302 | errno = EACCES; | |
303 | return -1; | |
304 | } | |
305 | ||
02d57da4 LT |
306 | /* |
307 | * We'll ignore SIGTERM from now on, we have a | |
308 | * good client. | |
309 | */ | |
310 | signal(SIGTERM, SIG_IGN); | |
311 | ||
d819e4e6 JH |
312 | return service->fn(); |
313 | } | |
314 | ||
315 | static int upload_pack(void) | |
316 | { | |
317 | /* Timeout as string */ | |
318 | char timeout_buf[64]; | |
319 | ||
960deccb PA |
320 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); |
321 | ||
a87e8be2 | 322 | /* git-upload-pack only ever reads stuff, so this is safe */ |
77cb17e9 | 323 | execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL); |
a87e8be2 LT |
324 | return -1; |
325 | } | |
326 | ||
d819e4e6 JH |
327 | static struct daemon_service daemon_service[] = { |
328 | { "upload-pack", "uploadpack", upload_pack, 1, 1 }, | |
329 | }; | |
330 | ||
331 | static void enable_service(const char *name, int ena) { | |
332 | int i; | |
333 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { | |
334 | if (!strcmp(daemon_service[i].name, name)) { | |
335 | daemon_service[i].enabled = ena; | |
336 | return; | |
337 | } | |
338 | } | |
339 | die("No such service %s", name); | |
340 | } | |
341 | ||
342 | static void make_service_overridable(const char *name, int ena) { | |
343 | int i; | |
344 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { | |
345 | if (!strcmp(daemon_service[i].name, name)) { | |
346 | daemon_service[i].overridable = ena; | |
347 | return; | |
348 | } | |
349 | } | |
350 | die("No such service %s", name); | |
351 | } | |
352 | ||
5b276ee4 | 353 | static int execute(struct sockaddr *addr) |
a87e8be2 | 354 | { |
7d80694a | 355 | static char line[1000]; |
d819e4e6 | 356 | int pktlen, len, i; |
7d80694a | 357 | |
5b276ee4 DW |
358 | if (addr) { |
359 | char addrbuf[256] = ""; | |
360 | int port = -1; | |
361 | ||
362 | if (addr->sa_family == AF_INET) { | |
363 | struct sockaddr_in *sin_addr = (void *) addr; | |
364 | inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); | |
365 | port = sin_addr->sin_port; | |
366 | #ifndef NO_IPV6 | |
367 | } else if (addr && addr->sa_family == AF_INET6) { | |
368 | struct sockaddr_in6 *sin6_addr = (void *) addr; | |
369 | ||
370 | char *buf = addrbuf; | |
371 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ | |
372 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); | |
373 | strcat(buf, "]"); | |
374 | ||
375 | port = sin6_addr->sin6_port; | |
376 | #endif | |
377 | } | |
378 | loginfo("Connection from %s:%d", addrbuf, port); | |
379 | } | |
380 | ||
960deccb | 381 | alarm(init_timeout ? init_timeout : timeout); |
5ad312be | 382 | pktlen = packet_read_line(0, line, sizeof(line)); |
960deccb | 383 | alarm(0); |
7d80694a | 384 | |
5ad312be JL |
385 | len = strlen(line); |
386 | if (pktlen != len) | |
387 | loginfo("Extended attributes (%d bytes) exist <%.*s>", | |
388 | (int) pktlen - len, | |
389 | (int) pktlen - len, line + len + 1); | |
7d80694a LT |
390 | if (len && line[len-1] == '\n') |
391 | line[--len] = 0; | |
392 | ||
d819e4e6 JH |
393 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { |
394 | struct daemon_service *s = &(daemon_service[i]); | |
395 | int namelen = strlen(s->name); | |
396 | if (!strncmp("git-", line, 4) && | |
397 | !strncmp(s->name, line + 4, namelen) && | |
398 | line[namelen + 4] == ' ') | |
399 | return run_service(line + namelen + 5, s); | |
400 | } | |
a87e8be2 | 401 | |
f8ff0c06 | 402 | logerror("Protocol error: '%s'", line); |
a87e8be2 LT |
403 | return -1; |
404 | } | |
405 | ||
66e631de LT |
406 | |
407 | /* | |
408 | * We count spawned/reaped separately, just to avoid any | |
409 | * races when updating them from signals. The SIGCHLD handler | |
410 | * will only update children_reaped, and the fork logic will | |
411 | * only update children_spawned. | |
412 | * | |
413 | * MAX_CHILDREN should be a power-of-two to make the modulus | |
414 | * operation cheap. It should also be at least twice | |
415 | * the maximum number of connections we will ever allow. | |
416 | */ | |
417 | #define MAX_CHILDREN 128 | |
418 | ||
419 | static int max_connections = 25; | |
420 | ||
421 | /* These are updated by the signal handler */ | |
96f1e58f | 422 | static volatile unsigned int children_reaped; |
4d8fa916 | 423 | static pid_t dead_child[MAX_CHILDREN]; |
66e631de LT |
424 | |
425 | /* These are updated by the main loop */ | |
96f1e58f DR |
426 | static unsigned int children_spawned; |
427 | static unsigned int children_deleted; | |
66e631de | 428 | |
4d8fa916 | 429 | static struct child { |
66e631de | 430 | pid_t pid; |
7fa09084 | 431 | int addrlen; |
df076bdb | 432 | struct sockaddr_storage address; |
66e631de LT |
433 | } live_child[MAX_CHILDREN]; |
434 | ||
7fa09084 | 435 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) |
66e631de LT |
436 | { |
437 | live_child[idx].pid = pid; | |
438 | live_child[idx].addrlen = addrlen; | |
df076bdb | 439 | memcpy(&live_child[idx].address, addr, addrlen); |
66e631de LT |
440 | } |
441 | ||
442 | /* | |
443 | * Walk from "deleted" to "spawned", and remove child "pid". | |
444 | * | |
445 | * We move everything up by one, since the new "deleted" will | |
446 | * be one higher. | |
447 | */ | |
448 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) | |
449 | { | |
450 | struct child n; | |
451 | ||
452 | deleted %= MAX_CHILDREN; | |
453 | spawned %= MAX_CHILDREN; | |
454 | if (live_child[deleted].pid == pid) { | |
455 | live_child[deleted].pid = -1; | |
456 | return; | |
457 | } | |
458 | n = live_child[deleted]; | |
459 | for (;;) { | |
460 | struct child m; | |
461 | deleted = (deleted + 1) % MAX_CHILDREN; | |
462 | if (deleted == spawned) | |
463 | die("could not find dead child %d\n", pid); | |
464 | m = live_child[deleted]; | |
465 | live_child[deleted] = n; | |
466 | if (m.pid == pid) | |
467 | return; | |
468 | n = m; | |
469 | } | |
470 | } | |
471 | ||
472 | /* | |
473 | * This gets called if the number of connections grows | |
474 | * past "max_connections". | |
475 | * | |
476 | * We _should_ start off by searching for connections | |
477 | * from the same IP, and if there is some address wth | |
478 | * multiple connections, we should kill that first. | |
479 | * | |
480 | * As it is, we just "randomly" kill 25% of the connections, | |
481 | * and our pseudo-random generator sucks too. I have no | |
482 | * shame. | |
483 | * | |
484 | * Really, this is just a place-holder for a _real_ algorithm. | |
485 | */ | |
02d57da4 | 486 | static void kill_some_children(int signo, unsigned start, unsigned stop) |
66e631de LT |
487 | { |
488 | start %= MAX_CHILDREN; | |
489 | stop %= MAX_CHILDREN; | |
490 | while (start != stop) { | |
491 | if (!(start & 3)) | |
02d57da4 | 492 | kill(live_child[start].pid, signo); |
66e631de LT |
493 | start = (start + 1) % MAX_CHILDREN; |
494 | } | |
495 | } | |
496 | ||
02d57da4 | 497 | static void check_max_connections(void) |
a87e8be2 | 498 | { |
02d57da4 | 499 | for (;;) { |
eaa94919 | 500 | int active; |
66e631de | 501 | unsigned spawned, reaped, deleted; |
eaa94919 | 502 | |
66e631de | 503 | spawned = children_spawned; |
66e631de LT |
504 | reaped = children_reaped; |
505 | deleted = children_deleted; | |
506 | ||
507 | while (deleted < reaped) { | |
508 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; | |
509 | remove_child(pid, deleted, spawned); | |
510 | deleted++; | |
511 | } | |
512 | children_deleted = deleted; | |
513 | ||
514 | active = spawned - deleted; | |
02d57da4 LT |
515 | if (active <= max_connections) |
516 | break; | |
66e631de | 517 | |
02d57da4 LT |
518 | /* Kill some unstarted connections with SIGTERM */ |
519 | kill_some_children(SIGTERM, deleted, spawned); | |
520 | if (active <= max_connections << 1) | |
521 | break; | |
522 | ||
523 | /* If the SIGTERM thing isn't helping use SIGKILL */ | |
524 | kill_some_children(SIGKILL, deleted, spawned); | |
525 | sleep(1); | |
526 | } | |
527 | } | |
528 | ||
7fa09084 | 529 | static void handle(int incoming, struct sockaddr *addr, int addrlen) |
02d57da4 LT |
530 | { |
531 | pid_t pid = fork(); | |
532 | ||
533 | if (pid) { | |
534 | unsigned idx; | |
535 | ||
536 | close(incoming); | |
537 | if (pid < 0) | |
538 | return; | |
539 | ||
540 | idx = children_spawned % MAX_CHILDREN; | |
541 | children_spawned++; | |
542 | add_child(idx, pid, addr, addrlen); | |
66e631de | 543 | |
02d57da4 | 544 | check_max_connections(); |
a87e8be2 LT |
545 | return; |
546 | } | |
547 | ||
548 | dup2(incoming, 0); | |
549 | dup2(incoming, 1); | |
550 | close(incoming); | |
f8ff0c06 | 551 | |
5b276ee4 | 552 | exit(execute(addr)); |
a87e8be2 LT |
553 | } |
554 | ||
eaa94919 LT |
555 | static void child_handler(int signo) |
556 | { | |
557 | for (;;) { | |
9048fe1c PB |
558 | int status; |
559 | pid_t pid = waitpid(-1, &status, WNOHANG); | |
66e631de LT |
560 | |
561 | if (pid > 0) { | |
562 | unsigned reaped = children_reaped; | |
563 | dead_child[reaped % MAX_CHILDREN] = pid; | |
564 | children_reaped = reaped + 1; | |
f8ff0c06 | 565 | /* XXX: Custom logging, since we don't wanna getpid() */ |
9048fe1c | 566 | if (verbose) { |
554fe20d | 567 | const char *dead = ""; |
9048fe1c PB |
568 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) |
569 | dead = " (with error)"; | |
570 | if (log_syslog) | |
571 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); | |
572 | else | |
573 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); | |
574 | } | |
eaa94919 LT |
575 | continue; |
576 | } | |
577 | break; | |
578 | } | |
579 | } | |
580 | ||
1955fabf MW |
581 | static int set_reuse_addr(int sockfd) |
582 | { | |
583 | int on = 1; | |
584 | ||
585 | if (!reuseaddr) | |
586 | return 0; | |
587 | return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, | |
588 | &on, sizeof(on)); | |
589 | } | |
590 | ||
6573faff PA |
591 | #ifndef NO_IPV6 |
592 | ||
593 | static int socksetup(int port, int **socklist_p) | |
a87e8be2 | 594 | { |
df076bdb YH |
595 | int socknum = 0, *socklist = NULL; |
596 | int maxfd = -1; | |
df076bdb | 597 | char pbuf[NI_MAXSERV]; |
a87e8be2 | 598 | |
6573faff PA |
599 | struct addrinfo hints, *ai0, *ai; |
600 | int gai; | |
df076bdb YH |
601 | |
602 | sprintf(pbuf, "%d", port); | |
603 | memset(&hints, 0, sizeof(hints)); | |
604 | hints.ai_family = AF_UNSPEC; | |
605 | hints.ai_socktype = SOCK_STREAM; | |
606 | hints.ai_protocol = IPPROTO_TCP; | |
607 | hints.ai_flags = AI_PASSIVE; | |
608 | ||
609 | gai = getaddrinfo(NULL, pbuf, &hints, &ai0); | |
610 | if (gai) | |
611 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); | |
612 | ||
df076bdb YH |
613 | for (ai = ai0; ai; ai = ai->ai_next) { |
614 | int sockfd; | |
df076bdb YH |
615 | |
616 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | |
617 | if (sockfd < 0) | |
618 | continue; | |
619 | if (sockfd >= FD_SETSIZE) { | |
620 | error("too large socket descriptor."); | |
621 | close(sockfd); | |
622 | continue; | |
623 | } | |
624 | ||
625 | #ifdef IPV6_V6ONLY | |
626 | if (ai->ai_family == AF_INET6) { | |
627 | int on = 1; | |
628 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, | |
629 | &on, sizeof(on)); | |
630 | /* Note: error is not fatal */ | |
631 | } | |
632 | #endif | |
633 | ||
1955fabf MW |
634 | if (set_reuse_addr(sockfd)) { |
635 | close(sockfd); | |
0032d548 | 636 | continue; |
1955fabf MW |
637 | } |
638 | ||
df076bdb YH |
639 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
640 | close(sockfd); | |
641 | continue; /* not fatal */ | |
642 | } | |
643 | if (listen(sockfd, 5) < 0) { | |
644 | close(sockfd); | |
645 | continue; /* not fatal */ | |
646 | } | |
647 | ||
83572c1a | 648 | socklist = xrealloc(socklist, sizeof(int) * (socknum + 1)); |
df076bdb YH |
649 | socklist[socknum++] = sockfd; |
650 | ||
df076bdb YH |
651 | if (maxfd < sockfd) |
652 | maxfd = sockfd; | |
653 | } | |
654 | ||
655 | freeaddrinfo(ai0); | |
656 | ||
6573faff PA |
657 | *socklist_p = socklist; |
658 | return socknum; | |
659 | } | |
660 | ||
661 | #else /* NO_IPV6 */ | |
662 | ||
663 | static int socksetup(int port, int **socklist_p) | |
664 | { | |
665 | struct sockaddr_in sin; | |
666 | int sockfd; | |
667 | ||
668 | sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
669 | if (sockfd < 0) | |
670 | return 0; | |
671 | ||
672 | memset(&sin, 0, sizeof sin); | |
673 | sin.sin_family = AF_INET; | |
674 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
675 | sin.sin_port = htons(port); | |
676 | ||
1955fabf MW |
677 | if (set_reuse_addr(sockfd)) { |
678 | close(sockfd); | |
679 | return 0; | |
680 | } | |
681 | ||
6573faff PA |
682 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { |
683 | close(sockfd); | |
684 | return 0; | |
685 | } | |
a87e8be2 | 686 | |
f35230fb PS |
687 | if (listen(sockfd, 5) < 0) { |
688 | close(sockfd); | |
689 | return 0; | |
690 | } | |
691 | ||
1b4713fb | 692 | *socklist_p = xmalloc(sizeof(int)); |
6573faff | 693 | **socklist_p = sockfd; |
f35230fb | 694 | return 1; |
6573faff PA |
695 | } |
696 | ||
697 | #endif | |
698 | ||
699 | static int service_loop(int socknum, int *socklist) | |
700 | { | |
701 | struct pollfd *pfd; | |
702 | int i; | |
703 | ||
1b4713fb | 704 | pfd = xcalloc(socknum, sizeof(struct pollfd)); |
6573faff PA |
705 | |
706 | for (i = 0; i < socknum; i++) { | |
707 | pfd[i].fd = socklist[i]; | |
708 | pfd[i].events = POLLIN; | |
709 | } | |
9220282a PA |
710 | |
711 | signal(SIGCHLD, child_handler); | |
a87e8be2 LT |
712 | |
713 | for (;;) { | |
df076bdb | 714 | int i; |
6573faff | 715 | |
7872e055 | 716 | if (poll(pfd, socknum, -1) < 0) { |
1eef0b33 | 717 | if (errno != EINTR) { |
6573faff | 718 | error("poll failed, resuming: %s", |
1eef0b33 JH |
719 | strerror(errno)); |
720 | sleep(1); | |
721 | } | |
df076bdb YH |
722 | continue; |
723 | } | |
724 | ||
725 | for (i = 0; i < socknum; i++) { | |
6573faff | 726 | if (pfd[i].revents & POLLIN) { |
df076bdb | 727 | struct sockaddr_storage ss; |
7626e49e | 728 | unsigned int sslen = sizeof(ss); |
6573faff | 729 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); |
df076bdb YH |
730 | if (incoming < 0) { |
731 | switch (errno) { | |
732 | case EAGAIN: | |
733 | case EINTR: | |
734 | case ECONNABORTED: | |
735 | continue; | |
736 | default: | |
737 | die("accept returned %s", strerror(errno)); | |
738 | } | |
739 | } | |
740 | handle(incoming, (struct sockaddr *)&ss, sslen); | |
a87e8be2 LT |
741 | } |
742 | } | |
a87e8be2 LT |
743 | } |
744 | } | |
745 | ||
258e93a1 ML |
746 | /* if any standard file descriptor is missing open it to /dev/null */ |
747 | static void sanitize_stdfds(void) | |
748 | { | |
749 | int fd = open("/dev/null", O_RDWR, 0); | |
750 | while (fd != -1 && fd < 2) | |
751 | fd = dup(fd); | |
752 | if (fd == -1) | |
753 | die("open /dev/null or dup failed: %s", strerror(errno)); | |
754 | if (fd > 2) | |
755 | close(fd); | |
756 | } | |
757 | ||
a5262768 ML |
758 | static void daemonize(void) |
759 | { | |
760 | switch (fork()) { | |
761 | case 0: | |
762 | break; | |
763 | case -1: | |
764 | die("fork failed: %s", strerror(errno)); | |
765 | default: | |
766 | exit(0); | |
767 | } | |
768 | if (setsid() == -1) | |
769 | die("setsid failed: %s", strerror(errno)); | |
770 | close(0); | |
771 | close(1); | |
772 | close(2); | |
773 | sanitize_stdfds(); | |
774 | } | |
775 | ||
45ed5d7f ML |
776 | static void store_pid(const char *path) |
777 | { | |
778 | FILE *f = fopen(path, "w"); | |
779 | if (!f) | |
780 | die("cannot open pid file %s: %s", path, strerror(errno)); | |
781 | fprintf(f, "%d\n", getpid()); | |
782 | fclose(f); | |
783 | } | |
784 | ||
678dac6b | 785 | static int serve(int port, struct passwd *pass, gid_t gid) |
6573faff PA |
786 | { |
787 | int socknum, *socklist; | |
4ae22d96 | 788 | |
6573faff PA |
789 | socknum = socksetup(port, &socklist); |
790 | if (socknum == 0) | |
791 | die("unable to allocate any listen sockets on port %u", port); | |
4ae22d96 | 792 | |
678dac6b TS |
793 | if (pass && gid && |
794 | (initgroups(pass->pw_name, gid) || setgid (gid) || | |
795 | setuid(pass->pw_uid))) | |
796 | die("cannot drop privileges"); | |
797 | ||
6573faff | 798 | return service_loop(socknum, socklist); |
4ae22d96 | 799 | } |
6573faff | 800 | |
a87e8be2 LT |
801 | int main(int argc, char **argv) |
802 | { | |
803 | int port = DEFAULT_GIT_PORT; | |
e64e1b79 | 804 | int inetd_mode = 0; |
678dac6b | 805 | const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; |
a5262768 | 806 | int detach = 0; |
678dac6b TS |
807 | struct passwd *pass = NULL; |
808 | struct group *group; | |
809 | gid_t gid = 0; | |
a87e8be2 LT |
810 | int i; |
811 | ||
f0b7367c JH |
812 | /* Without this we cannot rely on waitpid() to tell |
813 | * what happened to our children. | |
814 | */ | |
815 | signal(SIGCHLD, SIG_DFL); | |
816 | ||
a87e8be2 LT |
817 | for (i = 1; i < argc; i++) { |
818 | char *arg = argv[i]; | |
819 | ||
820 | if (!strncmp(arg, "--port=", 7)) { | |
821 | char *end; | |
822 | unsigned long n; | |
823 | n = strtoul(arg+7, &end, 0); | |
824 | if (arg[7] && !*end) { | |
825 | port = n; | |
826 | continue; | |
827 | } | |
828 | } | |
e64e1b79 LT |
829 | if (!strcmp(arg, "--inetd")) { |
830 | inetd_mode = 1; | |
a8883288 | 831 | log_syslog = 1; |
e64e1b79 LT |
832 | continue; |
833 | } | |
f8ff0c06 PB |
834 | if (!strcmp(arg, "--verbose")) { |
835 | verbose = 1; | |
836 | continue; | |
837 | } | |
9048fe1c PB |
838 | if (!strcmp(arg, "--syslog")) { |
839 | log_syslog = 1; | |
9048fe1c PB |
840 | continue; |
841 | } | |
4ae95682 PA |
842 | if (!strcmp(arg, "--export-all")) { |
843 | export_all_trees = 1; | |
844 | continue; | |
845 | } | |
960deccb PA |
846 | if (!strncmp(arg, "--timeout=", 10)) { |
847 | timeout = atoi(arg+10); | |
a8883288 | 848 | continue; |
960deccb | 849 | } |
54e31a20 | 850 | if (!strncmp(arg, "--init-timeout=", 15)) { |
960deccb | 851 | init_timeout = atoi(arg+15); |
a8883288 | 852 | continue; |
960deccb | 853 | } |
4dbd1352 AE |
854 | if (!strcmp(arg, "--strict-paths")) { |
855 | strict_paths = 1; | |
856 | continue; | |
857 | } | |
b21c31c9 PB |
858 | if (!strncmp(arg, "--base-path=", 12)) { |
859 | base_path = arg+12; | |
860 | continue; | |
861 | } | |
1955fabf MW |
862 | if (!strcmp(arg, "--reuseaddr")) { |
863 | reuseaddr = 1; | |
864 | continue; | |
865 | } | |
603968d2 JH |
866 | if (!strcmp(arg, "--user-path")) { |
867 | user_path = ""; | |
868 | continue; | |
869 | } | |
870 | if (!strncmp(arg, "--user-path=", 12)) { | |
871 | user_path = arg + 12; | |
872 | continue; | |
873 | } | |
45ed5d7f ML |
874 | if (!strncmp(arg, "--pid-file=", 11)) { |
875 | pid_file = arg + 11; | |
876 | continue; | |
877 | } | |
a5262768 ML |
878 | if (!strcmp(arg, "--detach")) { |
879 | detach = 1; | |
880 | log_syslog = 1; | |
881 | continue; | |
882 | } | |
678dac6b TS |
883 | if (!strncmp(arg, "--user=", 7)) { |
884 | user_name = arg + 7; | |
885 | continue; | |
886 | } | |
887 | if (!strncmp(arg, "--group=", 8)) { | |
888 | group_name = arg + 8; | |
889 | continue; | |
890 | } | |
d819e4e6 JH |
891 | if (!strncmp(arg, "--enable=", 9)) { |
892 | enable_service(arg + 9, 1); | |
893 | continue; | |
894 | } | |
895 | if (!strncmp(arg, "--disable=", 10)) { | |
896 | enable_service(arg + 10, 0); | |
897 | continue; | |
898 | } | |
899 | if (!strncmp(arg, "--enable-override=", 18)) { | |
900 | make_service_overridable(arg + 18, 1); | |
901 | continue; | |
902 | } | |
903 | if (!strncmp(arg, "--disable-override=", 19)) { | |
904 | make_service_overridable(arg + 19, 0); | |
905 | continue; | |
906 | } | |
4ae95682 PA |
907 | if (!strcmp(arg, "--")) { |
908 | ok_paths = &argv[i+1]; | |
909 | break; | |
910 | } else if (arg[0] != '-') { | |
911 | ok_paths = &argv[i]; | |
912 | break; | |
913 | } | |
e64e1b79 | 914 | |
a87e8be2 LT |
915 | usage(daemon_usage); |
916 | } | |
917 | ||
678dac6b TS |
918 | if (inetd_mode && (group_name || user_name)) |
919 | die("--user and --group are incompatible with --inetd"); | |
920 | ||
921 | if (group_name && !user_name) | |
922 | die("--group supplied without --user"); | |
923 | ||
924 | if (user_name) { | |
925 | pass = getpwnam(user_name); | |
926 | if (!pass) | |
927 | die("user not found - %s", user_name); | |
928 | ||
929 | if (!group_name) | |
930 | gid = pass->pw_gid; | |
931 | else { | |
932 | group = getgrnam(group_name); | |
933 | if (!group) | |
934 | die("group not found - %s", group_name); | |
935 | ||
936 | gid = group->gr_gid; | |
937 | } | |
938 | } | |
939 | ||
ad8b4f56 | 940 | if (log_syslog) { |
a8883288 | 941 | openlog("git-daemon", 0, LOG_DAEMON); |
ad8b4f56 | 942 | set_die_routine(daemon_die); |
4dbd1352 AE |
943 | } |
944 | ||
ad8b4f56 ML |
945 | if (strict_paths && (!ok_paths || !*ok_paths)) |
946 | die("option --strict-paths requires a whitelist"); | |
947 | ||
7c3693f1 | 948 | if (inetd_mode) { |
5b276ee4 DW |
949 | struct sockaddr_storage ss; |
950 | struct sockaddr *peer = (struct sockaddr *)&ss; | |
951 | socklen_t slen = sizeof(ss); | |
952 | ||
ba0012c3 | 953 | freopen("/dev/null", "w", stderr); |
5b276ee4 DW |
954 | |
955 | if (getpeername(0, peer, &slen)) | |
956 | peer = NULL; | |
957 | ||
958 | return execute(peer); | |
7c3693f1 | 959 | } |
bce8230d | 960 | |
a5262768 ML |
961 | if (detach) |
962 | daemonize(); | |
963 | else | |
964 | sanitize_stdfds(); | |
258e93a1 | 965 | |
45ed5d7f ML |
966 | if (pid_file) |
967 | store_pid(pid_file); | |
968 | ||
678dac6b | 969 | return serve(port, pass, gid); |
a87e8be2 | 970 | } |