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> | |
dd467629 | 12 | #include <limits.h> |
979e32fa RS |
13 | #include "pkt-line.h" |
14 | #include "cache.h" | |
77cb17e9 | 15 | #include "exec_cmd.h" |
49ba83fb | 16 | #include "interpolate.h" |
f8ff0c06 | 17 | |
9048fe1c | 18 | static int log_syslog; |
f8ff0c06 | 19 | static int verbose; |
1955fabf | 20 | static int reuseaddr; |
f8ff0c06 | 21 | |
960deccb | 22 | static const char daemon_usage[] = |
dd467629 | 23 | "git-daemon [--verbose] [--syslog] [--export-all]\n" |
b21c31c9 | 24 | " [--timeout=n] [--init-timeout=n] [--strict-paths]\n" |
603968d2 | 25 | " [--base-path=path] [--user-path | --user-path=path]\n" |
49ba83fb | 26 | " [--interpolated-path=path]\n" |
678dac6b | 27 | " [--reuseaddr] [--detach] [--pid-file=file]\n" |
d9edcbd6 | 28 | " [--[enable|disable|allow-override|forbid-override]=service]\n" |
dd467629 JL |
29 | " [--inetd | [--listen=host_or_ipaddr] [--port=n]\n" |
30 | " [--user=user [--group=group]]\n" | |
31 | " [directory...]"; | |
4ae95682 PA |
32 | |
33 | /* List of acceptable pathname prefixes */ | |
96f1e58f DR |
34 | static char **ok_paths; |
35 | static int strict_paths; | |
4ae95682 PA |
36 | |
37 | /* If this is set, git-daemon-export-ok is not required */ | |
96f1e58f | 38 | static int export_all_trees; |
f8ff0c06 | 39 | |
b21c31c9 | 40 | /* Take all paths relative to this one if non-NULL */ |
96f1e58f | 41 | static char *base_path; |
49ba83fb JL |
42 | static char *interpolated_path; |
43 | ||
44 | /* Flag indicating client sent extra args. */ | |
45 | static int saw_extended_args; | |
b21c31c9 | 46 | |
603968d2 JH |
47 | /* If defined, ~user notation is allowed and the string is inserted |
48 | * after ~user/. E.g. a request to git://host/~alice/frotz would | |
49 | * go to /home/alice/pub_git/frotz with --user-path=pub_git. | |
50 | */ | |
96f1e58f | 51 | static const char *user_path; |
603968d2 | 52 | |
960deccb | 53 | /* Timeout, and initial timeout */ |
96f1e58f DR |
54 | static unsigned int timeout; |
55 | static unsigned int init_timeout; | |
f8ff0c06 | 56 | |
49ba83fb JL |
57 | /* |
58 | * Static table for now. Ugh. | |
59 | * Feel free to make dynamic as needed. | |
60 | */ | |
61 | #define INTERP_SLOT_HOST (0) | |
dd467629 JL |
62 | #define INTERP_SLOT_CANON_HOST (1) |
63 | #define INTERP_SLOT_IP (2) | |
64 | #define INTERP_SLOT_PORT (3) | |
65 | #define INTERP_SLOT_DIR (4) | |
66 | #define INTERP_SLOT_PERCENT (5) | |
49ba83fb JL |
67 | |
68 | static struct interp interp_table[] = { | |
69 | { "%H", 0}, | |
dd467629 JL |
70 | { "%CH", 0}, |
71 | { "%IP", 0}, | |
72 | { "%P", 0}, | |
49ba83fb | 73 | { "%D", 0}, |
eb30aed7 | 74 | { "%%", 0}, |
49ba83fb JL |
75 | }; |
76 | ||
77 | ||
9048fe1c | 78 | static void logreport(int priority, const char *err, va_list params) |
f8ff0c06 PB |
79 | { |
80 | /* We should do a single write so that it is atomic and output | |
81 | * of several processes do not get intermingled. */ | |
82 | char buf[1024]; | |
83 | int buflen; | |
84 | int maxlen, msglen; | |
85 | ||
86 | /* sizeof(buf) should be big enough for "[pid] \n" */ | |
1bedd4ca | 87 | buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid()); |
f8ff0c06 PB |
88 | |
89 | maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */ | |
90 | msglen = vsnprintf(buf + buflen, maxlen, err, params); | |
91 | ||
9048fe1c PB |
92 | if (log_syslog) { |
93 | syslog(priority, "%s", buf); | |
94 | return; | |
95 | } | |
96 | ||
f8ff0c06 PB |
97 | /* maxlen counted our own LF but also counts space given to |
98 | * vsnprintf for the terminating NUL. We want to make sure that | |
99 | * we have space for our own LF and NUL after the "meat" of the | |
100 | * message, so truncate it at maxlen - 1. | |
101 | */ | |
102 | if (msglen > maxlen - 1) | |
103 | msglen = maxlen - 1; | |
104 | else if (msglen < 0) | |
105 | msglen = 0; /* Protect against weird return values. */ | |
106 | buflen += msglen; | |
107 | ||
108 | buf[buflen++] = '\n'; | |
109 | buf[buflen] = '\0'; | |
110 | ||
111 | write(2, buf, buflen); | |
112 | } | |
113 | ||
cdda4745 | 114 | static void logerror(const char *err, ...) |
f8ff0c06 PB |
115 | { |
116 | va_list params; | |
117 | va_start(params, err); | |
9048fe1c | 118 | logreport(LOG_ERR, err, params); |
f8ff0c06 PB |
119 | va_end(params); |
120 | } | |
121 | ||
cdda4745 | 122 | static void loginfo(const char *err, ...) |
f8ff0c06 PB |
123 | { |
124 | va_list params; | |
125 | if (!verbose) | |
126 | return; | |
127 | va_start(params, err); | |
9048fe1c | 128 | logreport(LOG_INFO, err, params); |
f8ff0c06 PB |
129 | va_end(params); |
130 | } | |
a87e8be2 | 131 | |
ad8b4f56 ML |
132 | static void NORETURN daemon_die(const char *err, va_list params) |
133 | { | |
134 | logreport(LOG_ERR, err, params); | |
135 | exit(1); | |
136 | } | |
137 | ||
d79374c7 JH |
138 | static int avoid_alias(char *p) |
139 | { | |
140 | int sl, ndot; | |
141 | ||
142 | /* | |
143 | * This resurrects the belts and suspenders paranoia check by HPA | |
144 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() | |
145 | * does not do getcwd() based path canonicalizations. | |
146 | * | |
147 | * sl becomes true immediately after seeing '/' and continues to | |
148 | * be true as long as dots continue after that without intervening | |
149 | * non-dot character. | |
150 | */ | |
151 | if (!p || (*p != '/' && *p != '~')) | |
152 | return -1; | |
153 | sl = 1; ndot = 0; | |
154 | p++; | |
155 | ||
156 | while (1) { | |
157 | char ch = *p++; | |
158 | if (sl) { | |
159 | if (ch == '.') | |
160 | ndot++; | |
161 | else if (ch == '/') { | |
162 | if (ndot < 3) | |
163 | /* reject //, /./ and /../ */ | |
164 | return -1; | |
165 | ndot = 0; | |
166 | } | |
167 | else if (ch == 0) { | |
168 | if (0 < ndot && ndot < 3) | |
169 | /* reject /.$ and /..$ */ | |
170 | return -1; | |
171 | return 0; | |
172 | } | |
173 | else | |
174 | sl = ndot = 0; | |
175 | } | |
176 | else if (ch == 0) | |
177 | return 0; | |
178 | else if (ch == '/') { | |
179 | sl = 1; | |
180 | ndot = 0; | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
49ba83fb | 185 | static char *path_ok(struct interp *itable) |
4ae95682 | 186 | { |
603968d2 | 187 | static char rpath[PATH_MAX]; |
49ba83fb | 188 | static char interp_path[PATH_MAX]; |
d79374c7 | 189 | char *path; |
49ba83fb JL |
190 | char *dir; |
191 | ||
192 | dir = itable[INTERP_SLOT_DIR].value; | |
d79374c7 JH |
193 | |
194 | if (avoid_alias(dir)) { | |
195 | logerror("'%s': aliased", dir); | |
196 | return NULL; | |
197 | } | |
198 | ||
603968d2 JH |
199 | if (*dir == '~') { |
200 | if (!user_path) { | |
201 | logerror("'%s': User-path not allowed", dir); | |
202 | return NULL; | |
203 | } | |
204 | if (*user_path) { | |
205 | /* Got either "~alice" or "~alice/foo"; | |
206 | * rewrite them to "~alice/%s" or | |
207 | * "~alice/%s/foo". | |
208 | */ | |
209 | int namlen, restlen = strlen(dir); | |
210 | char *slash = strchr(dir, '/'); | |
211 | if (!slash) | |
212 | slash = dir + restlen; | |
213 | namlen = slash - dir; | |
214 | restlen -= namlen; | |
215 | loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash); | |
216 | snprintf(rpath, PATH_MAX, "%.*s/%s%.*s", | |
217 | namlen, dir, user_path, restlen, slash); | |
218 | dir = rpath; | |
219 | } | |
220 | } | |
49ba83fb JL |
221 | else if (interpolated_path && saw_extended_args) { |
222 | if (*dir != '/') { | |
223 | /* Allow only absolute */ | |
224 | logerror("'%s': Non-absolute path denied (interpolated-path active)", dir); | |
225 | return NULL; | |
226 | } | |
227 | ||
228 | interpolate(interp_path, PATH_MAX, interpolated_path, | |
229 | interp_table, ARRAY_SIZE(interp_table)); | |
230 | loginfo("Interpolated dir '%s'", interp_path); | |
231 | ||
232 | dir = interp_path; | |
233 | } | |
603968d2 JH |
234 | else if (base_path) { |
235 | if (*dir != '/') { | |
236 | /* Allow only absolute */ | |
1fda3d55 | 237 | logerror("'%s': Non-absolute path denied (base-path active)", dir); |
b21c31c9 PB |
238 | return NULL; |
239 | } | |
49ba83fb JL |
240 | snprintf(rpath, PATH_MAX, "%s%s", base_path, dir); |
241 | dir = rpath; | |
b21c31c9 PB |
242 | } |
243 | ||
d79374c7 | 244 | path = enter_repo(dir, strict_paths); |
3e04c62d | 245 | |
4dbd1352 AE |
246 | if (!path) { |
247 | logerror("'%s': unable to chdir or not a git archive", dir); | |
248 | return NULL; | |
4ae95682 PA |
249 | } |
250 | ||
251 | if ( ok_paths && *ok_paths ) { | |
ce335fe0 | 252 | char **pp; |
4dbd1352 | 253 | int pathlen = strlen(path); |
4ae95682 | 254 | |
ce335fe0 | 255 | /* The validation is done on the paths after enter_repo |
d79374c7 JH |
256 | * appends optional {.git,.git/.git} and friends, but |
257 | * it does not use getcwd(). So if your /pub is | |
258 | * a symlink to /mnt/pub, you can whitelist /pub and | |
259 | * do not have to say /mnt/pub. | |
260 | * Do not say /pub/. | |
ce335fe0 | 261 | */ |
4ae95682 PA |
262 | for ( pp = ok_paths ; *pp ; pp++ ) { |
263 | int len = strlen(*pp); | |
ce335fe0 JH |
264 | if (len <= pathlen && |
265 | !memcmp(*pp, path, len) && | |
266 | (path[len] == '\0' || | |
267 | (!strict_paths && path[len] == '/'))) | |
268 | return path; | |
4ae95682 | 269 | } |
4dbd1352 AE |
270 | } |
271 | else { | |
272 | /* be backwards compatible */ | |
273 | if (!strict_paths) | |
274 | return path; | |
4ae95682 PA |
275 | } |
276 | ||
4dbd1352 AE |
277 | logerror("'%s': not in whitelist", path); |
278 | return NULL; /* Fallthrough. Deny by default */ | |
4ae95682 | 279 | } |
a87e8be2 | 280 | |
d819e4e6 JH |
281 | typedef int (*daemon_service_fn)(void); |
282 | struct daemon_service { | |
283 | const char *name; | |
284 | const char *config_name; | |
285 | daemon_service_fn fn; | |
286 | int enabled; | |
287 | int overridable; | |
288 | }; | |
289 | ||
290 | static struct daemon_service *service_looking_at; | |
291 | static int service_enabled; | |
292 | ||
293 | static int git_daemon_config(const char *var, const char *value) | |
294 | { | |
295 | if (!strncmp(var, "daemon.", 7) && | |
296 | !strcmp(var + 7, service_looking_at->config_name)) { | |
297 | service_enabled = git_config_bool(var, value); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | /* we are not interested in parsing any other configuration here */ | |
302 | return 0; | |
303 | } | |
304 | ||
49ba83fb | 305 | static int run_service(struct interp *itable, struct daemon_service *service) |
a87e8be2 | 306 | { |
4dbd1352 | 307 | const char *path; |
d819e4e6 JH |
308 | int enabled = service->enabled; |
309 | ||
49ba83fb JL |
310 | loginfo("Request %s for '%s'", |
311 | service->name, | |
312 | itable[INTERP_SLOT_DIR].value); | |
4dbd1352 | 313 | |
d819e4e6 JH |
314 | if (!enabled && !service->overridable) { |
315 | logerror("'%s': service not enabled.", service->name); | |
316 | errno = EACCES; | |
317 | return -1; | |
318 | } | |
4ae95682 | 319 | |
49ba83fb | 320 | if (!(path = path_ok(itable))) |
a87e8be2 | 321 | return -1; |
47888f0f | 322 | |
a87e8be2 LT |
323 | /* |
324 | * Security on the cheap. | |
325 | * | |
a935c397 | 326 | * We want a readable HEAD, usable "objects" directory, and |
a87e8be2 LT |
327 | * a "git-daemon-export-ok" flag that says that the other side |
328 | * is ok with us doing this. | |
4dbd1352 AE |
329 | * |
330 | * path_ok() uses enter_repo() and does whitelist checking. | |
331 | * We only need to make sure the repository is exported. | |
a87e8be2 | 332 | */ |
4dbd1352 | 333 | |
3e04c62d | 334 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { |
4dbd1352 | 335 | logerror("'%s': repository not exported.", path); |
3e04c62d PA |
336 | errno = EACCES; |
337 | return -1; | |
338 | } | |
339 | ||
d819e4e6 JH |
340 | if (service->overridable) { |
341 | service_looking_at = service; | |
342 | service_enabled = -1; | |
343 | git_config(git_daemon_config); | |
344 | if (0 <= service_enabled) | |
345 | enabled = service_enabled; | |
346 | } | |
347 | if (!enabled) { | |
348 | logerror("'%s': service not enabled for '%s'", | |
349 | service->name, path); | |
350 | errno = EACCES; | |
351 | return -1; | |
352 | } | |
353 | ||
02d57da4 LT |
354 | /* |
355 | * We'll ignore SIGTERM from now on, we have a | |
356 | * good client. | |
357 | */ | |
358 | signal(SIGTERM, SIG_IGN); | |
359 | ||
d819e4e6 JH |
360 | return service->fn(); |
361 | } | |
362 | ||
363 | static int upload_pack(void) | |
364 | { | |
365 | /* Timeout as string */ | |
366 | char timeout_buf[64]; | |
367 | ||
960deccb PA |
368 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); |
369 | ||
a87e8be2 | 370 | /* git-upload-pack only ever reads stuff, so this is safe */ |
77cb17e9 | 371 | execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL); |
a87e8be2 LT |
372 | return -1; |
373 | } | |
374 | ||
39345a21 FBH |
375 | static int upload_archive(void) |
376 | { | |
377 | execl_git_cmd("upload-archive", ".", NULL); | |
378 | return -1; | |
379 | } | |
380 | ||
d819e4e6 | 381 | static struct daemon_service daemon_service[] = { |
39345a21 | 382 | { "upload-archive", "uploadarch", upload_archive, 0, 1 }, |
d819e4e6 JH |
383 | { "upload-pack", "uploadpack", upload_pack, 1, 1 }, |
384 | }; | |
385 | ||
386 | static void enable_service(const char *name, int ena) { | |
387 | int i; | |
388 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { | |
389 | if (!strcmp(daemon_service[i].name, name)) { | |
390 | daemon_service[i].enabled = ena; | |
391 | return; | |
392 | } | |
393 | } | |
394 | die("No such service %s", name); | |
395 | } | |
396 | ||
397 | static void make_service_overridable(const char *name, int ena) { | |
398 | int i; | |
399 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { | |
400 | if (!strcmp(daemon_service[i].name, name)) { | |
401 | daemon_service[i].overridable = ena; | |
402 | return; | |
403 | } | |
404 | } | |
405 | die("No such service %s", name); | |
406 | } | |
407 | ||
eb30aed7 JL |
408 | /* |
409 | * Separate the "extra args" information as supplied by the client connection. | |
410 | * Any resulting data is squirrelled away in the given interpolation table. | |
411 | */ | |
412 | static void parse_extra_args(struct interp *table, char *extra_args, int buflen) | |
49ba83fb JL |
413 | { |
414 | char *val; | |
415 | int vallen; | |
416 | char *end = extra_args + buflen; | |
417 | ||
418 | while (extra_args < end && *extra_args) { | |
419 | saw_extended_args = 1; | |
420 | if (strncasecmp("host=", extra_args, 5) == 0) { | |
421 | val = extra_args + 5; | |
422 | vallen = strlen(val) + 1; | |
423 | if (*val) { | |
eb30aed7 JL |
424 | /* Split <host>:<port> at colon. */ |
425 | char *host = val; | |
426 | char *port = strrchr(host, ':'); | |
dd467629 JL |
427 | if (port) { |
428 | *port = 0; | |
429 | port++; | |
eb30aed7 | 430 | interp_set_entry(table, INTERP_SLOT_PORT, port); |
dd467629 | 431 | } |
eb30aed7 | 432 | interp_set_entry(table, INTERP_SLOT_HOST, host); |
49ba83fb | 433 | } |
eb30aed7 | 434 | |
49ba83fb JL |
435 | /* On to the next one */ |
436 | extra_args = val + vallen; | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
dd467629 JL |
441 | void fill_in_extra_table_entries(struct interp *itable) |
442 | { | |
443 | char *hp; | |
dd467629 JL |
444 | |
445 | /* | |
446 | * Replace literal host with lowercase-ized hostname. | |
447 | */ | |
448 | hp = interp_table[INTERP_SLOT_HOST].value; | |
449 | for ( ; *hp; hp++) | |
450 | *hp = tolower(*hp); | |
451 | ||
452 | /* | |
453 | * Locate canonical hostname and its IP address. | |
454 | */ | |
455 | #ifndef NO_IPV6 | |
456 | { | |
457 | struct addrinfo hints; | |
458 | struct addrinfo *ai, *ai0; | |
459 | int gai; | |
460 | static char addrbuf[HOST_NAME_MAX + 1]; | |
461 | ||
462 | memset(&hints, 0, sizeof(hints)); | |
463 | hints.ai_flags = AI_CANONNAME; | |
464 | ||
465 | gai = getaddrinfo(interp_table[INTERP_SLOT_HOST].value, 0, &hints, &ai0); | |
466 | if (!gai) { | |
467 | for (ai = ai0; ai; ai = ai->ai_next) { | |
468 | struct sockaddr_in *sin_addr = (void *)ai->ai_addr; | |
469 | ||
dd467629 JL |
470 | inet_ntop(AF_INET, &sin_addr->sin_addr, |
471 | addrbuf, sizeof(addrbuf)); | |
eb30aed7 JL |
472 | interp_set_entry(interp_table, |
473 | INTERP_SLOT_CANON_HOST, ai->ai_canonname); | |
474 | interp_set_entry(interp_table, | |
475 | INTERP_SLOT_IP, addrbuf); | |
dd467629 JL |
476 | break; |
477 | } | |
478 | freeaddrinfo(ai0); | |
479 | } | |
480 | } | |
481 | #else | |
482 | { | |
483 | struct hostent *hent; | |
484 | struct sockaddr_in sa; | |
485 | char **ap; | |
486 | static char addrbuf[HOST_NAME_MAX + 1]; | |
487 | ||
488 | hent = gethostbyname(interp_table[INTERP_SLOT_HOST].value); | |
dd467629 JL |
489 | |
490 | ap = hent->h_addr_list; | |
491 | memset(&sa, 0, sizeof sa); | |
492 | sa.sin_family = hent->h_addrtype; | |
493 | sa.sin_port = htons(0); | |
494 | memcpy(&sa.sin_addr, *ap, hent->h_length); | |
495 | ||
496 | inet_ntop(hent->h_addrtype, &sa.sin_addr, | |
497 | addrbuf, sizeof(addrbuf)); | |
eb30aed7 JL |
498 | |
499 | interp_set_entry(interp_table, INTERP_SLOT_CANON_HOST, hent->h_name); | |
500 | interp_set_entry(interp_table, INTERP_SLOT_IP, addrbuf); | |
dd467629 JL |
501 | } |
502 | #endif | |
dd467629 JL |
503 | } |
504 | ||
505 | ||
5b276ee4 | 506 | static int execute(struct sockaddr *addr) |
a87e8be2 | 507 | { |
7d80694a | 508 | static char line[1000]; |
d819e4e6 | 509 | int pktlen, len, i; |
7d80694a | 510 | |
5b276ee4 DW |
511 | if (addr) { |
512 | char addrbuf[256] = ""; | |
513 | int port = -1; | |
514 | ||
515 | if (addr->sa_family == AF_INET) { | |
516 | struct sockaddr_in *sin_addr = (void *) addr; | |
517 | inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); | |
518 | port = sin_addr->sin_port; | |
519 | #ifndef NO_IPV6 | |
520 | } else if (addr && addr->sa_family == AF_INET6) { | |
521 | struct sockaddr_in6 *sin6_addr = (void *) addr; | |
522 | ||
523 | char *buf = addrbuf; | |
524 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ | |
525 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); | |
526 | strcat(buf, "]"); | |
527 | ||
528 | port = sin6_addr->sin6_port; | |
529 | #endif | |
530 | } | |
531 | loginfo("Connection from %s:%d", addrbuf, port); | |
532 | } | |
533 | ||
960deccb | 534 | alarm(init_timeout ? init_timeout : timeout); |
5ad312be | 535 | pktlen = packet_read_line(0, line, sizeof(line)); |
960deccb | 536 | alarm(0); |
7d80694a | 537 | |
5ad312be JL |
538 | len = strlen(line); |
539 | if (pktlen != len) | |
540 | loginfo("Extended attributes (%d bytes) exist <%.*s>", | |
541 | (int) pktlen - len, | |
542 | (int) pktlen - len, line + len + 1); | |
7d80694a LT |
543 | if (len && line[len-1] == '\n') |
544 | line[--len] = 0; | |
545 | ||
eb30aed7 JL |
546 | /* |
547 | * Initialize the path interpolation table for this connection. | |
548 | */ | |
549 | interp_clear_table(interp_table, ARRAY_SIZE(interp_table)); | |
550 | interp_set_entry(interp_table, INTERP_SLOT_PERCENT, "%"); | |
551 | ||
dd467629 | 552 | if (len != pktlen) { |
eb30aed7 | 553 | parse_extra_args(interp_table, line + len + 1, pktlen - len - 1); |
dd467629 JL |
554 | fill_in_extra_table_entries(interp_table); |
555 | } | |
49ba83fb | 556 | |
d819e4e6 JH |
557 | for (i = 0; i < ARRAY_SIZE(daemon_service); i++) { |
558 | struct daemon_service *s = &(daemon_service[i]); | |
559 | int namelen = strlen(s->name); | |
560 | if (!strncmp("git-", line, 4) && | |
561 | !strncmp(s->name, line + 4, namelen) && | |
49ba83fb | 562 | line[namelen + 4] == ' ') { |
eb30aed7 JL |
563 | /* |
564 | * Note: The directory here is probably context sensitive, | |
565 | * and might depend on the actual service being performed. | |
566 | */ | |
567 | interp_set_entry(interp_table, | |
568 | INTERP_SLOT_DIR, line + namelen + 5); | |
49ba83fb JL |
569 | return run_service(interp_table, s); |
570 | } | |
d819e4e6 | 571 | } |
a87e8be2 | 572 | |
f8ff0c06 | 573 | logerror("Protocol error: '%s'", line); |
a87e8be2 LT |
574 | return -1; |
575 | } | |
576 | ||
66e631de LT |
577 | |
578 | /* | |
579 | * We count spawned/reaped separately, just to avoid any | |
580 | * races when updating them from signals. The SIGCHLD handler | |
581 | * will only update children_reaped, and the fork logic will | |
582 | * only update children_spawned. | |
583 | * | |
584 | * MAX_CHILDREN should be a power-of-two to make the modulus | |
585 | * operation cheap. It should also be at least twice | |
586 | * the maximum number of connections we will ever allow. | |
587 | */ | |
588 | #define MAX_CHILDREN 128 | |
589 | ||
590 | static int max_connections = 25; | |
591 | ||
592 | /* These are updated by the signal handler */ | |
96f1e58f | 593 | static volatile unsigned int children_reaped; |
4d8fa916 | 594 | static pid_t dead_child[MAX_CHILDREN]; |
66e631de LT |
595 | |
596 | /* These are updated by the main loop */ | |
96f1e58f DR |
597 | static unsigned int children_spawned; |
598 | static unsigned int children_deleted; | |
66e631de | 599 | |
4d8fa916 | 600 | static struct child { |
66e631de | 601 | pid_t pid; |
7fa09084 | 602 | int addrlen; |
df076bdb | 603 | struct sockaddr_storage address; |
66e631de LT |
604 | } live_child[MAX_CHILDREN]; |
605 | ||
7fa09084 | 606 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) |
66e631de LT |
607 | { |
608 | live_child[idx].pid = pid; | |
609 | live_child[idx].addrlen = addrlen; | |
df076bdb | 610 | memcpy(&live_child[idx].address, addr, addrlen); |
66e631de LT |
611 | } |
612 | ||
613 | /* | |
614 | * Walk from "deleted" to "spawned", and remove child "pid". | |
615 | * | |
616 | * We move everything up by one, since the new "deleted" will | |
617 | * be one higher. | |
618 | */ | |
619 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) | |
620 | { | |
621 | struct child n; | |
622 | ||
623 | deleted %= MAX_CHILDREN; | |
624 | spawned %= MAX_CHILDREN; | |
625 | if (live_child[deleted].pid == pid) { | |
626 | live_child[deleted].pid = -1; | |
627 | return; | |
628 | } | |
629 | n = live_child[deleted]; | |
630 | for (;;) { | |
631 | struct child m; | |
632 | deleted = (deleted + 1) % MAX_CHILDREN; | |
633 | if (deleted == spawned) | |
634 | die("could not find dead child %d\n", pid); | |
635 | m = live_child[deleted]; | |
636 | live_child[deleted] = n; | |
637 | if (m.pid == pid) | |
638 | return; | |
639 | n = m; | |
640 | } | |
641 | } | |
642 | ||
643 | /* | |
644 | * This gets called if the number of connections grows | |
645 | * past "max_connections". | |
646 | * | |
647 | * We _should_ start off by searching for connections | |
648 | * from the same IP, and if there is some address wth | |
649 | * multiple connections, we should kill that first. | |
650 | * | |
651 | * As it is, we just "randomly" kill 25% of the connections, | |
652 | * and our pseudo-random generator sucks too. I have no | |
653 | * shame. | |
654 | * | |
655 | * Really, this is just a place-holder for a _real_ algorithm. | |
656 | */ | |
02d57da4 | 657 | static void kill_some_children(int signo, unsigned start, unsigned stop) |
66e631de LT |
658 | { |
659 | start %= MAX_CHILDREN; | |
660 | stop %= MAX_CHILDREN; | |
661 | while (start != stop) { | |
662 | if (!(start & 3)) | |
02d57da4 | 663 | kill(live_child[start].pid, signo); |
66e631de LT |
664 | start = (start + 1) % MAX_CHILDREN; |
665 | } | |
666 | } | |
667 | ||
02d57da4 | 668 | static void check_max_connections(void) |
a87e8be2 | 669 | { |
02d57da4 | 670 | for (;;) { |
eaa94919 | 671 | int active; |
66e631de | 672 | unsigned spawned, reaped, deleted; |
eaa94919 | 673 | |
66e631de | 674 | spawned = children_spawned; |
66e631de LT |
675 | reaped = children_reaped; |
676 | deleted = children_deleted; | |
677 | ||
678 | while (deleted < reaped) { | |
679 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; | |
680 | remove_child(pid, deleted, spawned); | |
681 | deleted++; | |
682 | } | |
683 | children_deleted = deleted; | |
684 | ||
685 | active = spawned - deleted; | |
02d57da4 LT |
686 | if (active <= max_connections) |
687 | break; | |
66e631de | 688 | |
02d57da4 LT |
689 | /* Kill some unstarted connections with SIGTERM */ |
690 | kill_some_children(SIGTERM, deleted, spawned); | |
691 | if (active <= max_connections << 1) | |
692 | break; | |
693 | ||
694 | /* If the SIGTERM thing isn't helping use SIGKILL */ | |
695 | kill_some_children(SIGKILL, deleted, spawned); | |
696 | sleep(1); | |
697 | } | |
698 | } | |
699 | ||
7fa09084 | 700 | static void handle(int incoming, struct sockaddr *addr, int addrlen) |
02d57da4 LT |
701 | { |
702 | pid_t pid = fork(); | |
703 | ||
704 | if (pid) { | |
705 | unsigned idx; | |
706 | ||
707 | close(incoming); | |
708 | if (pid < 0) | |
709 | return; | |
710 | ||
711 | idx = children_spawned % MAX_CHILDREN; | |
712 | children_spawned++; | |
713 | add_child(idx, pid, addr, addrlen); | |
66e631de | 714 | |
02d57da4 | 715 | check_max_connections(); |
a87e8be2 LT |
716 | return; |
717 | } | |
718 | ||
719 | dup2(incoming, 0); | |
720 | dup2(incoming, 1); | |
721 | close(incoming); | |
f8ff0c06 | 722 | |
5b276ee4 | 723 | exit(execute(addr)); |
a87e8be2 LT |
724 | } |
725 | ||
eaa94919 LT |
726 | static void child_handler(int signo) |
727 | { | |
728 | for (;;) { | |
9048fe1c PB |
729 | int status; |
730 | pid_t pid = waitpid(-1, &status, WNOHANG); | |
66e631de LT |
731 | |
732 | if (pid > 0) { | |
733 | unsigned reaped = children_reaped; | |
734 | dead_child[reaped % MAX_CHILDREN] = pid; | |
735 | children_reaped = reaped + 1; | |
f8ff0c06 | 736 | /* XXX: Custom logging, since we don't wanna getpid() */ |
9048fe1c | 737 | if (verbose) { |
554fe20d | 738 | const char *dead = ""; |
9048fe1c PB |
739 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) |
740 | dead = " (with error)"; | |
741 | if (log_syslog) | |
742 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); | |
743 | else | |
744 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); | |
745 | } | |
eaa94919 LT |
746 | continue; |
747 | } | |
748 | break; | |
749 | } | |
750 | } | |
751 | ||
1955fabf MW |
752 | static int set_reuse_addr(int sockfd) |
753 | { | |
754 | int on = 1; | |
755 | ||
756 | if (!reuseaddr) | |
757 | return 0; | |
758 | return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, | |
759 | &on, sizeof(on)); | |
760 | } | |
761 | ||
6573faff PA |
762 | #ifndef NO_IPV6 |
763 | ||
dd467629 | 764 | static int socksetup(char *listen_addr, int listen_port, int **socklist_p) |
a87e8be2 | 765 | { |
df076bdb YH |
766 | int socknum = 0, *socklist = NULL; |
767 | int maxfd = -1; | |
df076bdb | 768 | char pbuf[NI_MAXSERV]; |
6573faff PA |
769 | struct addrinfo hints, *ai0, *ai; |
770 | int gai; | |
df076bdb | 771 | |
dd467629 | 772 | sprintf(pbuf, "%d", listen_port); |
df076bdb YH |
773 | memset(&hints, 0, sizeof(hints)); |
774 | hints.ai_family = AF_UNSPEC; | |
775 | hints.ai_socktype = SOCK_STREAM; | |
776 | hints.ai_protocol = IPPROTO_TCP; | |
777 | hints.ai_flags = AI_PASSIVE; | |
778 | ||
dd467629 | 779 | gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0); |
df076bdb YH |
780 | if (gai) |
781 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); | |
782 | ||
df076bdb YH |
783 | for (ai = ai0; ai; ai = ai->ai_next) { |
784 | int sockfd; | |
df076bdb YH |
785 | |
786 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | |
787 | if (sockfd < 0) | |
788 | continue; | |
789 | if (sockfd >= FD_SETSIZE) { | |
790 | error("too large socket descriptor."); | |
791 | close(sockfd); | |
792 | continue; | |
793 | } | |
794 | ||
795 | #ifdef IPV6_V6ONLY | |
796 | if (ai->ai_family == AF_INET6) { | |
797 | int on = 1; | |
798 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, | |
799 | &on, sizeof(on)); | |
800 | /* Note: error is not fatal */ | |
801 | } | |
802 | #endif | |
803 | ||
1955fabf MW |
804 | if (set_reuse_addr(sockfd)) { |
805 | close(sockfd); | |
0032d548 | 806 | continue; |
1955fabf MW |
807 | } |
808 | ||
df076bdb YH |
809 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
810 | close(sockfd); | |
811 | continue; /* not fatal */ | |
812 | } | |
813 | if (listen(sockfd, 5) < 0) { | |
814 | close(sockfd); | |
815 | continue; /* not fatal */ | |
816 | } | |
817 | ||
83572c1a | 818 | socklist = xrealloc(socklist, sizeof(int) * (socknum + 1)); |
df076bdb YH |
819 | socklist[socknum++] = sockfd; |
820 | ||
df076bdb YH |
821 | if (maxfd < sockfd) |
822 | maxfd = sockfd; | |
823 | } | |
824 | ||
825 | freeaddrinfo(ai0); | |
826 | ||
6573faff PA |
827 | *socklist_p = socklist; |
828 | return socknum; | |
829 | } | |
830 | ||
831 | #else /* NO_IPV6 */ | |
832 | ||
dd467629 | 833 | static int socksetup(char *lisen_addr, int listen_port, int **socklist_p) |
6573faff PA |
834 | { |
835 | struct sockaddr_in sin; | |
836 | int sockfd; | |
837 | ||
dd467629 JL |
838 | memset(&sin, 0, sizeof sin); |
839 | sin.sin_family = AF_INET; | |
840 | sin.sin_port = htons(listen_port); | |
841 | ||
842 | if (listen_addr) { | |
843 | /* Well, host better be an IP address here. */ | |
844 | if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0) | |
845 | return 0; | |
846 | } else { | |
847 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
848 | } | |
849 | ||
6573faff PA |
850 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
851 | if (sockfd < 0) | |
852 | return 0; | |
853 | ||
1955fabf MW |
854 | if (set_reuse_addr(sockfd)) { |
855 | close(sockfd); | |
856 | return 0; | |
857 | } | |
858 | ||
6573faff PA |
859 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { |
860 | close(sockfd); | |
861 | return 0; | |
862 | } | |
a87e8be2 | 863 | |
f35230fb PS |
864 | if (listen(sockfd, 5) < 0) { |
865 | close(sockfd); | |
866 | return 0; | |
867 | } | |
868 | ||
1b4713fb | 869 | *socklist_p = xmalloc(sizeof(int)); |
6573faff | 870 | **socklist_p = sockfd; |
f35230fb | 871 | return 1; |
6573faff PA |
872 | } |
873 | ||
874 | #endif | |
875 | ||
876 | static int service_loop(int socknum, int *socklist) | |
877 | { | |
878 | struct pollfd *pfd; | |
879 | int i; | |
880 | ||
1b4713fb | 881 | pfd = xcalloc(socknum, sizeof(struct pollfd)); |
6573faff PA |
882 | |
883 | for (i = 0; i < socknum; i++) { | |
884 | pfd[i].fd = socklist[i]; | |
885 | pfd[i].events = POLLIN; | |
886 | } | |
9220282a PA |
887 | |
888 | signal(SIGCHLD, child_handler); | |
a87e8be2 LT |
889 | |
890 | for (;;) { | |
df076bdb | 891 | int i; |
6573faff | 892 | |
7872e055 | 893 | if (poll(pfd, socknum, -1) < 0) { |
1eef0b33 | 894 | if (errno != EINTR) { |
6573faff | 895 | error("poll failed, resuming: %s", |
1eef0b33 JH |
896 | strerror(errno)); |
897 | sleep(1); | |
898 | } | |
df076bdb YH |
899 | continue; |
900 | } | |
901 | ||
902 | for (i = 0; i < socknum; i++) { | |
6573faff | 903 | if (pfd[i].revents & POLLIN) { |
df076bdb | 904 | struct sockaddr_storage ss; |
7626e49e | 905 | unsigned int sslen = sizeof(ss); |
6573faff | 906 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); |
df076bdb YH |
907 | if (incoming < 0) { |
908 | switch (errno) { | |
909 | case EAGAIN: | |
910 | case EINTR: | |
911 | case ECONNABORTED: | |
912 | continue; | |
913 | default: | |
914 | die("accept returned %s", strerror(errno)); | |
915 | } | |
916 | } | |
917 | handle(incoming, (struct sockaddr *)&ss, sslen); | |
a87e8be2 LT |
918 | } |
919 | } | |
a87e8be2 LT |
920 | } |
921 | } | |
922 | ||
258e93a1 ML |
923 | /* if any standard file descriptor is missing open it to /dev/null */ |
924 | static void sanitize_stdfds(void) | |
925 | { | |
926 | int fd = open("/dev/null", O_RDWR, 0); | |
927 | while (fd != -1 && fd < 2) | |
928 | fd = dup(fd); | |
929 | if (fd == -1) | |
930 | die("open /dev/null or dup failed: %s", strerror(errno)); | |
931 | if (fd > 2) | |
932 | close(fd); | |
933 | } | |
934 | ||
a5262768 ML |
935 | static void daemonize(void) |
936 | { | |
937 | switch (fork()) { | |
938 | case 0: | |
939 | break; | |
940 | case -1: | |
941 | die("fork failed: %s", strerror(errno)); | |
942 | default: | |
943 | exit(0); | |
944 | } | |
945 | if (setsid() == -1) | |
946 | die("setsid failed: %s", strerror(errno)); | |
947 | close(0); | |
948 | close(1); | |
949 | close(2); | |
950 | sanitize_stdfds(); | |
951 | } | |
952 | ||
45ed5d7f ML |
953 | static void store_pid(const char *path) |
954 | { | |
955 | FILE *f = fopen(path, "w"); | |
956 | if (!f) | |
957 | die("cannot open pid file %s: %s", path, strerror(errno)); | |
958 | fprintf(f, "%d\n", getpid()); | |
959 | fclose(f); | |
960 | } | |
961 | ||
dd467629 | 962 | static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid) |
6573faff PA |
963 | { |
964 | int socknum, *socklist; | |
4ae22d96 | 965 | |
dd467629 | 966 | socknum = socksetup(listen_addr, listen_port, &socklist); |
6573faff | 967 | if (socknum == 0) |
dd467629 JL |
968 | die("unable to allocate any listen sockets on host %s port %u", |
969 | listen_addr, listen_port); | |
4ae22d96 | 970 | |
678dac6b TS |
971 | if (pass && gid && |
972 | (initgroups(pass->pw_name, gid) || setgid (gid) || | |
973 | setuid(pass->pw_uid))) | |
974 | die("cannot drop privileges"); | |
975 | ||
6573faff | 976 | return service_loop(socknum, socklist); |
4ae22d96 | 977 | } |
6573faff | 978 | |
a87e8be2 LT |
979 | int main(int argc, char **argv) |
980 | { | |
dd467629 JL |
981 | int listen_port = 0; |
982 | char *listen_addr = NULL; | |
e64e1b79 | 983 | int inetd_mode = 0; |
678dac6b | 984 | const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; |
a5262768 | 985 | int detach = 0; |
678dac6b TS |
986 | struct passwd *pass = NULL; |
987 | struct group *group; | |
988 | gid_t gid = 0; | |
a87e8be2 LT |
989 | int i; |
990 | ||
f0b7367c JH |
991 | /* Without this we cannot rely on waitpid() to tell |
992 | * what happened to our children. | |
993 | */ | |
994 | signal(SIGCHLD, SIG_DFL); | |
995 | ||
a87e8be2 LT |
996 | for (i = 1; i < argc; i++) { |
997 | char *arg = argv[i]; | |
998 | ||
dd467629 JL |
999 | if (!strncmp(arg, "--listen=", 9)) { |
1000 | char *p = arg + 9; | |
1001 | char *ph = listen_addr = xmalloc(strlen(arg + 9) + 1); | |
1002 | while (*p) | |
1003 | *ph++ = tolower(*p++); | |
1004 | *ph = 0; | |
1005 | continue; | |
1006 | } | |
a87e8be2 LT |
1007 | if (!strncmp(arg, "--port=", 7)) { |
1008 | char *end; | |
1009 | unsigned long n; | |
1010 | n = strtoul(arg+7, &end, 0); | |
1011 | if (arg[7] && !*end) { | |
dd467629 | 1012 | listen_port = n; |
a87e8be2 LT |
1013 | continue; |
1014 | } | |
1015 | } | |
e64e1b79 LT |
1016 | if (!strcmp(arg, "--inetd")) { |
1017 | inetd_mode = 1; | |
a8883288 | 1018 | log_syslog = 1; |
e64e1b79 LT |
1019 | continue; |
1020 | } | |
f8ff0c06 PB |
1021 | if (!strcmp(arg, "--verbose")) { |
1022 | verbose = 1; | |
1023 | continue; | |
1024 | } | |
9048fe1c PB |
1025 | if (!strcmp(arg, "--syslog")) { |
1026 | log_syslog = 1; | |
9048fe1c PB |
1027 | continue; |
1028 | } | |
4ae95682 PA |
1029 | if (!strcmp(arg, "--export-all")) { |
1030 | export_all_trees = 1; | |
1031 | continue; | |
1032 | } | |
960deccb PA |
1033 | if (!strncmp(arg, "--timeout=", 10)) { |
1034 | timeout = atoi(arg+10); | |
a8883288 | 1035 | continue; |
960deccb | 1036 | } |
54e31a20 | 1037 | if (!strncmp(arg, "--init-timeout=", 15)) { |
960deccb | 1038 | init_timeout = atoi(arg+15); |
a8883288 | 1039 | continue; |
960deccb | 1040 | } |
4dbd1352 AE |
1041 | if (!strcmp(arg, "--strict-paths")) { |
1042 | strict_paths = 1; | |
1043 | continue; | |
1044 | } | |
b21c31c9 PB |
1045 | if (!strncmp(arg, "--base-path=", 12)) { |
1046 | base_path = arg+12; | |
1047 | continue; | |
1048 | } | |
49ba83fb JL |
1049 | if (!strncmp(arg, "--interpolated-path=", 20)) { |
1050 | interpolated_path = arg+20; | |
1051 | continue; | |
1052 | } | |
1955fabf MW |
1053 | if (!strcmp(arg, "--reuseaddr")) { |
1054 | reuseaddr = 1; | |
1055 | continue; | |
1056 | } | |
603968d2 JH |
1057 | if (!strcmp(arg, "--user-path")) { |
1058 | user_path = ""; | |
1059 | continue; | |
1060 | } | |
1061 | if (!strncmp(arg, "--user-path=", 12)) { | |
1062 | user_path = arg + 12; | |
1063 | continue; | |
1064 | } | |
45ed5d7f ML |
1065 | if (!strncmp(arg, "--pid-file=", 11)) { |
1066 | pid_file = arg + 11; | |
1067 | continue; | |
1068 | } | |
a5262768 ML |
1069 | if (!strcmp(arg, "--detach")) { |
1070 | detach = 1; | |
1071 | log_syslog = 1; | |
1072 | continue; | |
1073 | } | |
678dac6b TS |
1074 | if (!strncmp(arg, "--user=", 7)) { |
1075 | user_name = arg + 7; | |
1076 | continue; | |
1077 | } | |
1078 | if (!strncmp(arg, "--group=", 8)) { | |
1079 | group_name = arg + 8; | |
1080 | continue; | |
1081 | } | |
d819e4e6 JH |
1082 | if (!strncmp(arg, "--enable=", 9)) { |
1083 | enable_service(arg + 9, 1); | |
1084 | continue; | |
1085 | } | |
1086 | if (!strncmp(arg, "--disable=", 10)) { | |
1087 | enable_service(arg + 10, 0); | |
1088 | continue; | |
1089 | } | |
74c0cc21 JH |
1090 | if (!strncmp(arg, "--allow-override=", 17)) { |
1091 | make_service_overridable(arg + 17, 1); | |
d819e4e6 JH |
1092 | continue; |
1093 | } | |
74c0cc21 JH |
1094 | if (!strncmp(arg, "--forbid-override=", 18)) { |
1095 | make_service_overridable(arg + 18, 0); | |
d819e4e6 JH |
1096 | continue; |
1097 | } | |
4ae95682 PA |
1098 | if (!strcmp(arg, "--")) { |
1099 | ok_paths = &argv[i+1]; | |
1100 | break; | |
1101 | } else if (arg[0] != '-') { | |
1102 | ok_paths = &argv[i]; | |
1103 | break; | |
1104 | } | |
e64e1b79 | 1105 | |
a87e8be2 LT |
1106 | usage(daemon_usage); |
1107 | } | |
1108 | ||
678dac6b TS |
1109 | if (inetd_mode && (group_name || user_name)) |
1110 | die("--user and --group are incompatible with --inetd"); | |
1111 | ||
dd467629 JL |
1112 | if (inetd_mode && (listen_port || listen_addr)) |
1113 | die("--listen= and --port= are incompatible with --inetd"); | |
1114 | else if (listen_port == 0) | |
1115 | listen_port = DEFAULT_GIT_PORT; | |
1116 | ||
678dac6b TS |
1117 | if (group_name && !user_name) |
1118 | die("--group supplied without --user"); | |
1119 | ||
1120 | if (user_name) { | |
1121 | pass = getpwnam(user_name); | |
1122 | if (!pass) | |
1123 | die("user not found - %s", user_name); | |
1124 | ||
1125 | if (!group_name) | |
1126 | gid = pass->pw_gid; | |
1127 | else { | |
1128 | group = getgrnam(group_name); | |
1129 | if (!group) | |
1130 | die("group not found - %s", group_name); | |
1131 | ||
1132 | gid = group->gr_gid; | |
1133 | } | |
1134 | } | |
1135 | ||
ad8b4f56 | 1136 | if (log_syslog) { |
a8883288 | 1137 | openlog("git-daemon", 0, LOG_DAEMON); |
ad8b4f56 | 1138 | set_die_routine(daemon_die); |
4dbd1352 AE |
1139 | } |
1140 | ||
ad8b4f56 ML |
1141 | if (strict_paths && (!ok_paths || !*ok_paths)) |
1142 | die("option --strict-paths requires a whitelist"); | |
1143 | ||
7c3693f1 | 1144 | if (inetd_mode) { |
5b276ee4 DW |
1145 | struct sockaddr_storage ss; |
1146 | struct sockaddr *peer = (struct sockaddr *)&ss; | |
1147 | socklen_t slen = sizeof(ss); | |
1148 | ||
ba0012c3 | 1149 | freopen("/dev/null", "w", stderr); |
5b276ee4 DW |
1150 | |
1151 | if (getpeername(0, peer, &slen)) | |
1152 | peer = NULL; | |
1153 | ||
1154 | return execute(peer); | |
7c3693f1 | 1155 | } |
bce8230d | 1156 | |
a5262768 ML |
1157 | if (detach) |
1158 | daemonize(); | |
1159 | else | |
1160 | sanitize_stdfds(); | |
258e93a1 | 1161 | |
45ed5d7f ML |
1162 | if (pid_file) |
1163 | store_pid(pid_file); | |
1164 | ||
dd467629 | 1165 | return serve(listen_addr, listen_port, pass, gid); |
a87e8be2 | 1166 | } |