]> git.ipfire.org Git - thirdparty/git.git/blame - daemon.c
daemon: enable SO_KEEPALIVE for all sockets
[thirdparty/git.git] / daemon.c
CommitLineData
979e32fa 1#include "cache.h"
85023577 2#include "pkt-line.h"
77cb17e9 3#include "exec_cmd.h"
5d87dd4f
JS
4#include "run-command.h"
5#include "strbuf.h"
3a3a29c1 6#include "string-list.h"
f8ff0c06 7
695dffe2
JS
8#ifndef HOST_NAME_MAX
9#define HOST_NAME_MAX 256
10#endif
11
2844923d
MD
12#ifdef NO_INITGROUPS
13#define initgroups(x, y) (0) /* nothing */
14#endif
15
9048fe1c 16static int log_syslog;
f8ff0c06 17static int verbose;
1955fabf 18static int reuseaddr;
d5570f4d 19static int informative_errors;
f8ff0c06 20
960deccb 21static const char daemon_usage[] =
1b1dd23f 22"git daemon [--verbose] [--syslog] [--export-all]\n"
62b4698e
ŠN
23" [--timeout=<n>] [--init-timeout=<n>] [--max-connections=<n>]\n"
24" [--strict-paths] [--base-path=<path>] [--base-path-relaxed]\n"
25" [--user-path | --user-path=<path>]\n"
26" [--interpolated-path=<path>]\n"
9cddf56e 27" [--reuseaddr] [--pid-file=<file>]\n"
62b4698e 28" [--(enable|disable|allow-override|forbid-override)=<service>]\n"
93741e4a 29" [--access-hook=<path>]\n"
62b4698e 30" [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
9cddf56e 31" [--detach] [--user=<user> [--group=<group>]]\n"
62b4698e 32" [<directory>...]";
4ae95682
PA
33
34/* List of acceptable pathname prefixes */
96f1e58f
DR
35static char **ok_paths;
36static int strict_paths;
4ae95682
PA
37
38/* If this is set, git-daemon-export-ok is not required */
96f1e58f 39static int export_all_trees;
f8ff0c06 40
b21c31c9 41/* Take all paths relative to this one if non-NULL */
1055a890
JK
42static const char *base_path;
43static const char *interpolated_path;
73a7a656 44static int base_path_relaxed;
49ba83fb 45
603968d2
JH
46/* If defined, ~user notation is allowed and the string is inserted
47 * after ~user/. E.g. a request to git://host/~alice/frotz would
48 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
49 */
96f1e58f 50static const char *user_path;
603968d2 51
960deccb 52/* Timeout, and initial timeout */
96f1e58f
DR
53static unsigned int timeout;
54static unsigned int init_timeout;
f8ff0c06 55
01cec54e
RS
56struct hostinfo {
57 struct strbuf hostname;
58 struct strbuf canon_hostname;
59 struct strbuf ip_address;
60 struct strbuf tcp_port;
61 unsigned int hostname_lookup_done:1;
62 unsigned int saw_extended_args:1;
63};
edef953e 64
01cec54e 65static void lookup_hostname(struct hostinfo *hi);
edef953e 66
01cec54e 67static const char *get_canon_hostname(struct hostinfo *hi)
edef953e 68{
01cec54e
RS
69 lookup_hostname(hi);
70 return hi->canon_hostname.buf;
edef953e
RS
71}
72
01cec54e 73static const char *get_ip_address(struct hostinfo *hi)
edef953e 74{
01cec54e
RS
75 lookup_hostname(hi);
76 return hi->ip_address.buf;
edef953e
RS
77}
78
9048fe1c 79static void logreport(int priority, const char *err, va_list params)
f8ff0c06 80{
9048fe1c 81 if (log_syslog) {
6a992e9e
SB
82 char buf[1024];
83 vsnprintf(buf, sizeof(buf), err, params);
9048fe1c 84 syslog(priority, "%s", buf);
460c2010
JH
85 } else {
86 /*
48cfaea1 87 * Since stderr is set to buffered mode, the
6a992e9e 88 * logging of different processes will not overlap
48cfaea1 89 * unless they overflow the (rather big) buffers.
6a992e9e 90 */
85e72830 91 fprintf(stderr, "[%"PRIuMAX"] ", (uintmax_t)getpid());
6a992e9e
SB
92 vfprintf(stderr, err, params);
93 fputc('\n', stderr);
48cfaea1 94 fflush(stderr);
6a992e9e 95 }
f8ff0c06
PB
96}
97
28bea9e5 98__attribute__((format (printf, 1, 2)))
cdda4745 99static void logerror(const char *err, ...)
f8ff0c06
PB
100{
101 va_list params;
102 va_start(params, err);
9048fe1c 103 logreport(LOG_ERR, err, params);
f8ff0c06
PB
104 va_end(params);
105}
106
28bea9e5 107__attribute__((format (printf, 1, 2)))
cdda4745 108static void loginfo(const char *err, ...)
f8ff0c06
PB
109{
110 va_list params;
111 if (!verbose)
112 return;
113 va_start(params, err);
9048fe1c 114 logreport(LOG_INFO, err, params);
f8ff0c06
PB
115 va_end(params);
116}
a87e8be2 117
ad8b4f56
ML
118static void NORETURN daemon_die(const char *err, va_list params)
119{
120 logreport(LOG_ERR, err, params);
121 exit(1);
122}
123
dc8edc8f
RS
124struct expand_path_context {
125 const char *directory;
01cec54e 126 struct hostinfo *hostinfo;
dc8edc8f
RS
127};
128
129static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
130{
131 struct expand_path_context *context = ctx;
01cec54e 132 struct hostinfo *hi = context->hostinfo;
dc8edc8f
RS
133
134 switch (placeholder[0]) {
135 case 'H':
01cec54e 136 strbuf_addbuf(sb, &hi->hostname);
dc8edc8f
RS
137 return 1;
138 case 'C':
139 if (placeholder[1] == 'H') {
01cec54e 140 strbuf_addstr(sb, get_canon_hostname(hi));
dc8edc8f
RS
141 return 2;
142 }
143 break;
144 case 'I':
145 if (placeholder[1] == 'P') {
01cec54e 146 strbuf_addstr(sb, get_ip_address(hi));
dc8edc8f
RS
147 return 2;
148 }
149 break;
150 case 'P':
01cec54e 151 strbuf_addbuf(sb, &hi->tcp_port);
dc8edc8f
RS
152 return 1;
153 case 'D':
154 strbuf_addstr(sb, context->directory);
155 return 1;
156 }
157 return 0;
158}
159
01cec54e 160static const char *path_ok(const char *directory, struct hostinfo *hi)
4ae95682 161{
603968d2 162 static char rpath[PATH_MAX];
49ba83fb 163 static char interp_path[PATH_MAX];
1c64b48e 164 const char *path;
1055a890 165 const char *dir;
49ba83fb 166
9d7ca667 167 dir = directory;
d79374c7 168
34b6cb8b 169 if (daemon_avoid_alias(dir)) {
d79374c7
JH
170 logerror("'%s': aliased", dir);
171 return NULL;
172 }
173
603968d2
JH
174 if (*dir == '~') {
175 if (!user_path) {
176 logerror("'%s': User-path not allowed", dir);
177 return NULL;
178 }
179 if (*user_path) {
180 /* Got either "~alice" or "~alice/foo";
181 * rewrite them to "~alice/%s" or
182 * "~alice/%s/foo".
183 */
184 int namlen, restlen = strlen(dir);
1055a890 185 const char *slash = strchr(dir, '/');
603968d2
JH
186 if (!slash)
187 slash = dir + restlen;
188 namlen = slash - dir;
189 restlen -= namlen;
190 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
191 snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
192 namlen, dir, user_path, restlen, slash);
193 dir = rpath;
194 }
195 }
01cec54e 196 else if (interpolated_path && hi->saw_extended_args) {
9d7ca667 197 struct strbuf expanded_path = STRBUF_INIT;
dc8edc8f
RS
198 struct expand_path_context context;
199
200 context.directory = directory;
01cec54e 201 context.hostinfo = hi;
dc8edc8f 202
49ba83fb
JL
203 if (*dir != '/') {
204 /* Allow only absolute */
205 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
206 return NULL;
207 }
208
9d7ca667 209 strbuf_expand(&expanded_path, interpolated_path,
dc8edc8f 210 expand_path, &context);
9d7ca667
RS
211 strlcpy(interp_path, expanded_path.buf, PATH_MAX);
212 strbuf_release(&expanded_path);
49ba83fb
JL
213 loginfo("Interpolated dir '%s'", interp_path);
214
215 dir = interp_path;
216 }
603968d2
JH
217 else if (base_path) {
218 if (*dir != '/') {
219 /* Allow only absolute */
1fda3d55 220 logerror("'%s': Non-absolute path denied (base-path active)", dir);
b21c31c9
PB
221 return NULL;
222 }
49ba83fb
JL
223 snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
224 dir = rpath;
b21c31c9
PB
225 }
226
a583971f
RS
227 path = enter_repo(dir, strict_paths);
228 if (!path && base_path && base_path_relaxed) {
73a7a656
JA
229 /*
230 * if we fail and base_path_relaxed is enabled, try without
231 * prefixing the base path
232 */
a583971f
RS
233 dir = directory;
234 path = enter_repo(dir, strict_paths);
235 }
3e04c62d 236
4dbd1352 237 if (!path) {
05ac6b34 238 logerror("'%s' does not appear to be a git repository", dir);
4dbd1352 239 return NULL;
4ae95682
PA
240 }
241
242 if ( ok_paths && *ok_paths ) {
ce335fe0 243 char **pp;
4dbd1352 244 int pathlen = strlen(path);
4ae95682 245
ce335fe0 246 /* The validation is done on the paths after enter_repo
a6080a0a 247 * appends optional {.git,.git/.git} and friends, but
d79374c7
JH
248 * it does not use getcwd(). So if your /pub is
249 * a symlink to /mnt/pub, you can whitelist /pub and
250 * do not have to say /mnt/pub.
251 * Do not say /pub/.
ce335fe0 252 */
4ae95682
PA
253 for ( pp = ok_paths ; *pp ; pp++ ) {
254 int len = strlen(*pp);
ce335fe0
JH
255 if (len <= pathlen &&
256 !memcmp(*pp, path, len) &&
257 (path[len] == '\0' ||
258 (!strict_paths && path[len] == '/')))
259 return path;
4ae95682 260 }
4dbd1352
AE
261 }
262 else {
263 /* be backwards compatible */
264 if (!strict_paths)
265 return path;
4ae95682
PA
266 }
267
4dbd1352
AE
268 logerror("'%s': not in whitelist", path);
269 return NULL; /* Fallthrough. Deny by default */
4ae95682 270}
a87e8be2 271
d819e4e6
JH
272typedef int (*daemon_service_fn)(void);
273struct daemon_service {
274 const char *name;
275 const char *config_name;
276 daemon_service_fn fn;
277 int enabled;
278 int overridable;
279};
280
d5570f4d
JK
281static int daemon_error(const char *dir, const char *msg)
282{
283 if (!informative_errors)
284 msg = "access denied or repository not exported";
285 packet_write(1, "ERR %s: %s", msg, dir);
286 return -1;
287}
288
1055a890 289static const char *access_hook;
93741e4a 290
01cec54e
RS
291static int run_access_hook(struct daemon_service *service, const char *dir,
292 const char *path, struct hostinfo *hi)
93741e4a 293{
d3180279 294 struct child_process child = CHILD_PROCESS_INIT;
93741e4a
JH
295 struct strbuf buf = STRBUF_INIT;
296 const char *argv[8];
297 const char **arg = argv;
298 char *eol;
299 int seen_errors = 0;
300
93741e4a
JH
301 *arg++ = access_hook;
302 *arg++ = service->name;
303 *arg++ = path;
01cec54e
RS
304 *arg++ = hi->hostname.buf;
305 *arg++ = get_canon_hostname(hi);
306 *arg++ = get_ip_address(hi);
307 *arg++ = hi->tcp_port.buf;
93741e4a 308 *arg = NULL;
93741e4a 309
93741e4a
JH
310 child.use_shell = 1;
311 child.argv = argv;
312 child.no_stdin = 1;
313 child.no_stderr = 1;
314 child.out = -1;
315 if (start_command(&child)) {
316 logerror("daemon access hook '%s' failed to start",
317 access_hook);
318 goto error_return;
319 }
320 if (strbuf_read(&buf, child.out, 0) < 0) {
321 logerror("failed to read from pipe to daemon access hook '%s'",
322 access_hook);
323 strbuf_reset(&buf);
324 seen_errors = 1;
325 }
326 if (close(child.out) < 0) {
327 logerror("failed to close pipe to daemon access hook '%s'",
328 access_hook);
329 seen_errors = 1;
330 }
331 if (finish_command(&child))
332 seen_errors = 1;
333
334 if (!seen_errors) {
335 strbuf_release(&buf);
336 return 0;
337 }
338
339error_return:
340 strbuf_ltrim(&buf);
341 if (!buf.len)
342 strbuf_addstr(&buf, "service rejected");
343 eol = strchr(buf.buf, '\n');
344 if (eol)
345 *eol = '\0';
346 errno = EACCES;
347 daemon_error(dir, buf.buf);
348 strbuf_release(&buf);
349 return -1;
350}
351
01cec54e
RS
352static int run_service(const char *dir, struct daemon_service *service,
353 struct hostinfo *hi)
a87e8be2 354{
4dbd1352 355 const char *path;
d819e4e6 356 int enabled = service->enabled;
8939d32d 357 struct strbuf var = STRBUF_INIT;
d819e4e6 358
a47551c3 359 loginfo("Request %s for '%s'", service->name, dir);
4dbd1352 360
d819e4e6
JH
361 if (!enabled && !service->overridable) {
362 logerror("'%s': service not enabled.", service->name);
363 errno = EACCES;
d5570f4d 364 return daemon_error(dir, "service not enabled");
d819e4e6 365 }
4ae95682 366
01cec54e 367 if (!(path = path_ok(dir, hi)))
d5570f4d 368 return daemon_error(dir, "no such repository");
47888f0f 369
a87e8be2
LT
370 /*
371 * Security on the cheap.
372 *
a935c397 373 * We want a readable HEAD, usable "objects" directory, and
a87e8be2
LT
374 * a "git-daemon-export-ok" flag that says that the other side
375 * is ok with us doing this.
4dbd1352
AE
376 *
377 * path_ok() uses enter_repo() and does whitelist checking.
378 * We only need to make sure the repository is exported.
a87e8be2 379 */
4dbd1352 380
3e04c62d 381 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
4dbd1352 382 logerror("'%s': repository not exported.", path);
3e04c62d 383 errno = EACCES;
d5570f4d 384 return daemon_error(dir, "repository not exported");
3e04c62d
PA
385 }
386
d819e4e6 387 if (service->overridable) {
8939d32d
TA
388 strbuf_addf(&var, "daemon.%s", service->config_name);
389 git_config_get_bool(var.buf, &enabled);
390 strbuf_release(&var);
d819e4e6
JH
391 }
392 if (!enabled) {
393 logerror("'%s': service not enabled for '%s'",
394 service->name, path);
395 errno = EACCES;
d5570f4d 396 return daemon_error(dir, "service not enabled");
d819e4e6
JH
397 }
398
93741e4a
JH
399 /*
400 * Optionally, a hook can choose to deny access to the
401 * repository depending on the phase of the moon.
402 */
01cec54e 403 if (access_hook && run_access_hook(service, dir, path, hi))
93741e4a
JH
404 return -1;
405
02d57da4
LT
406 /*
407 * We'll ignore SIGTERM from now on, we have a
408 * good client.
409 */
410 signal(SIGTERM, SIG_IGN);
411
d819e4e6
JH
412 return service->fn();
413}
414
5d87dd4f
JS
415static void copy_to_log(int fd)
416{
417 struct strbuf line = STRBUF_INIT;
418 FILE *fp;
419
420 fp = fdopen(fd, "r");
421 if (fp == NULL) {
422 logerror("fdopen of error channel failed");
423 close(fd);
424 return;
425 }
426
8f309aeb 427 while (strbuf_getline_lf(&line, fp) != EOF) {
5d87dd4f
JS
428 logerror("%s", line.buf);
429 strbuf_setlen(&line, 0);
430 }
431
432 strbuf_release(&line);
433 fclose(fp);
434}
435
436static int run_service_command(const char **argv)
437{
d3180279 438 struct child_process cld = CHILD_PROCESS_INIT;
5d87dd4f 439
5d87dd4f
JS
440 cld.argv = argv;
441 cld.git_cmd = 1;
442 cld.err = -1;
443 if (start_command(&cld))
444 return -1;
445
446 close(0);
447 close(1);
448
449 copy_to_log(cld.err);
450
451 return finish_command(&cld);
452}
453
d819e4e6
JH
454static int upload_pack(void)
455{
456 /* Timeout as string */
457 char timeout_buf[64];
66dbfd55
GV
458 const char *argv[] = { "upload-pack", "--strict", NULL, ".", NULL };
459
460 argv[2] = timeout_buf;
d819e4e6 461
960deccb 462 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
5d87dd4f 463 return run_service_command(argv);
a87e8be2
LT
464}
465
39345a21
FBH
466static int upload_archive(void)
467{
5d87dd4f
JS
468 static const char *argv[] = { "upload-archive", ".", NULL };
469 return run_service_command(argv);
39345a21
FBH
470}
471
4b3b1e1e
LT
472static int receive_pack(void)
473{
5d87dd4f
JS
474 static const char *argv[] = { "receive-pack", ".", NULL };
475 return run_service_command(argv);
4b3b1e1e
LT
476}
477
d819e4e6 478static struct daemon_service daemon_service[] = {
39345a21 479 { "upload-archive", "uploadarch", upload_archive, 0, 1 },
d819e4e6 480 { "upload-pack", "uploadpack", upload_pack, 1, 1 },
4b3b1e1e 481 { "receive-pack", "receivepack", receive_pack, 0, 1 },
d819e4e6
JH
482};
483
f3fa1838
JH
484static void enable_service(const char *name, int ena)
485{
d819e4e6
JH
486 int i;
487 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
488 if (!strcmp(daemon_service[i].name, name)) {
489 daemon_service[i].enabled = ena;
490 return;
491 }
492 }
493 die("No such service %s", name);
494}
495
f3fa1838
JH
496static void make_service_overridable(const char *name, int ena)
497{
d819e4e6
JH
498 int i;
499 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
500 if (!strcmp(daemon_service[i].name, name)) {
501 daemon_service[i].overridable = ena;
502 return;
503 }
504 }
505 die("No such service %s", name);
506}
507
e8dbd76d
IL
508static void parse_host_and_port(char *hostport, char **host,
509 char **port)
510{
511 if (*hostport == '[') {
512 char *end;
513
514 end = strchr(hostport, ']');
515 if (!end)
9517e6b8 516 die("Invalid request ('[' without ']')");
e8dbd76d
IL
517 *end = '\0';
518 *host = hostport + 1;
519 if (!end[1])
520 *port = NULL;
521 else if (end[1] == ':')
522 *port = end + 2;
523 else
524 die("Garbage after end of host part");
525 } else {
526 *host = hostport;
527 *port = strrchr(hostport, ':');
528 if (*port) {
e9bd3235 529 **port = '\0';
e8dbd76d
IL
530 ++*port;
531 }
532 }
533}
534
b4853730
JK
535/*
536 * Sanitize a string from the client so that it's OK to be inserted into a
537 * filesystem path. Specifically, we disallow slashes, runs of "..", and
538 * trailing and leading dots, which means that the client cannot escape
539 * our base path via ".." traversal.
540 */
7a646cec 541static void sanitize_client(struct strbuf *out, const char *in)
b4853730
JK
542{
543 for (; *in; in++) {
544 if (*in == '/')
545 continue;
546 if (*in == '.' && (!out->len || out->buf[out->len - 1] == '.'))
547 continue;
548 strbuf_addch(out, *in);
549 }
550
551 while (out->len && out->buf[out->len - 1] == '.')
552 strbuf_setlen(out, out->len - 1);
553}
554
b4853730
JK
555/*
556 * Like sanitize_client, but we also perform any canonicalization
557 * to make life easier on the admin.
558 */
7a646cec 559static void canonicalize_client(struct strbuf *out, const char *in)
b4853730 560{
7a646cec
RS
561 sanitize_client(out, in);
562 strbuf_tolower(out);
b4853730
JK
563}
564
eb30aed7 565/*
73bb33a9 566 * Read the host as supplied by the client connection.
eb30aed7 567 */
01cec54e 568static void parse_host_arg(struct hostinfo *hi, char *extra_args, int buflen)
49ba83fb
JL
569{
570 char *val;
571 int vallen;
572 char *end = extra_args + buflen;
573
73bb33a9 574 if (extra_args < end && *extra_args) {
01cec54e 575 hi->saw_extended_args = 1;
49ba83fb
JL
576 if (strncasecmp("host=", extra_args, 5) == 0) {
577 val = extra_args + 5;
578 vallen = strlen(val) + 1;
579 if (*val) {
eb30aed7 580 /* Split <host>:<port> at colon. */
e8dbd76d
IL
581 char *host;
582 char *port;
583 parse_host_and_port(val, &host, &port);
01cec54e
RS
584 if (port)
585 sanitize_client(&hi->tcp_port, port);
586 canonicalize_client(&hi->hostname, host);
587 hi->hostname_lookup_done = 0;
49ba83fb 588 }
eb30aed7 589
49ba83fb
JL
590 /* On to the next one */
591 extra_args = val + vallen;
592 }
73bb33a9
SP
593 if (extra_args < end && *extra_args)
594 die("Invalid request");
49ba83fb 595 }
edef953e 596}
dd467629 597
edef953e
RS
598/*
599 * Locate canonical hostname and its IP address.
600 */
01cec54e 601static void lookup_hostname(struct hostinfo *hi)
edef953e 602{
01cec54e 603 if (!hi->hostname_lookup_done && hi->hostname.len) {
dd467629 604#ifndef NO_IPV6
dd467629 605 struct addrinfo hints;
3e8a00ae 606 struct addrinfo *ai;
dd467629
JL
607 int gai;
608 static char addrbuf[HOST_NAME_MAX + 1];
609
610 memset(&hints, 0, sizeof(hints));
611 hints.ai_flags = AI_CANONNAME;
612
01cec54e 613 gai = getaddrinfo(hi->hostname.buf, NULL, &hints, &ai);
dd467629 614 if (!gai) {
3e8a00ae
BK
615 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
616
617 inet_ntop(AF_INET, &sin_addr->sin_addr,
618 addrbuf, sizeof(addrbuf));
01cec54e 619 strbuf_addstr(&hi->ip_address, addrbuf);
3e8a00ae 620
7a646cec 621 if (ai->ai_canonname)
01cec54e 622 sanitize_client(&hi->canon_hostname,
7a646cec
RS
623 ai->ai_canonname);
624 else
01cec54e
RS
625 strbuf_addbuf(&hi->canon_hostname,
626 &hi->ip_address);
3e8a00ae
BK
627
628 freeaddrinfo(ai);
dd467629 629 }
dd467629 630#else
dd467629
JL
631 struct hostent *hent;
632 struct sockaddr_in sa;
633 char **ap;
634 static char addrbuf[HOST_NAME_MAX + 1];
635
d358f771 636 hent = gethostbyname(hi->hostname.buf);
eb6c4035
RS
637 if (hent) {
638 ap = hent->h_addr_list;
639 memset(&sa, 0, sizeof sa);
640 sa.sin_family = hent->h_addrtype;
641 sa.sin_port = htons(0);
642 memcpy(&sa.sin_addr, *ap, hent->h_length);
643
644 inet_ntop(hent->h_addrtype, &sa.sin_addr,
645 addrbuf, sizeof(addrbuf));
dd467629 646
01cec54e
RS
647 sanitize_client(&hi->canon_hostname, hent->h_name);
648 strbuf_addstr(&hi->ip_address, addrbuf);
eb6c4035 649 }
dd467629 650#endif
01cec54e 651 hi->hostname_lookup_done = 1;
6720e95b 652 }
dd467629
JL
653}
654
01cec54e
RS
655static void hostinfo_init(struct hostinfo *hi)
656{
657 memset(hi, 0, sizeof(*hi));
658 strbuf_init(&hi->hostname, 0);
659 strbuf_init(&hi->canon_hostname, 0);
660 strbuf_init(&hi->ip_address, 0);
661 strbuf_init(&hi->tcp_port, 0);
662}
663
664static void hostinfo_clear(struct hostinfo *hi)
665{
666 strbuf_release(&hi->hostname);
667 strbuf_release(&hi->canon_hostname);
668 strbuf_release(&hi->ip_address);
669 strbuf_release(&hi->tcp_port);
670}
dd467629 671
a43b68a1
EW
672static void set_keep_alive(int sockfd)
673{
674 int ka = 1;
675
676 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
677 logerror("unable to set SO_KEEPALIVE on socket: %s",
678 strerror(errno));
679}
680
f9c87be6 681static int execute(void)
a87e8be2 682{
74543a04 683 char *line = packet_buffer;
d819e4e6 684 int pktlen, len, i;
f9c87be6 685 char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
01cec54e
RS
686 struct hostinfo hi;
687
688 hostinfo_init(&hi);
7d80694a 689
f9c87be6
EFL
690 if (addr)
691 loginfo("Connection from %s:%s", addr, port);
5b276ee4 692
a43b68a1 693 set_keep_alive(0);
960deccb 694 alarm(init_timeout ? init_timeout : timeout);
4981fe75 695 pktlen = packet_read(0, NULL, NULL, packet_buffer, sizeof(packet_buffer), 0);
960deccb 696 alarm(0);
7d80694a 697
5ad312be
JL
698 len = strlen(line);
699 if (pktlen != len)
700 loginfo("Extended attributes (%d bytes) exist <%.*s>",
701 (int) pktlen - len,
702 (int) pktlen - len, line + len + 1);
83543a24 703 if (len && line[len-1] == '\n') {
7d80694a 704 line[--len] = 0;
83543a24
JH
705 pktlen--;
706 }
7d80694a 707
d433ed0b 708 if (len != pktlen)
01cec54e 709 parse_host_arg(&hi, line + len + 1, pktlen - len - 1);
49ba83fb 710
d819e4e6
JH
711 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
712 struct daemon_service *s = &(daemon_service[i]);
d12c24d2
JK
713 const char *arg;
714
715 if (skip_prefix(line, "git-", &arg) &&
716 skip_prefix(arg, s->name, &arg) &&
717 *arg++ == ' ') {
eb30aed7
JL
718 /*
719 * Note: The directory here is probably context sensitive,
720 * and might depend on the actual service being performed.
721 */
01cec54e
RS
722 int rc = run_service(arg, s, &hi);
723 hostinfo_clear(&hi);
724 return rc;
49ba83fb 725 }
d819e4e6 726 }
a87e8be2 727
01cec54e 728 hostinfo_clear(&hi);
f8ff0c06 729 logerror("Protocol error: '%s'", line);
a87e8be2
LT
730 return -1;
731}
732
15515b73
EFL
733static int addrcmp(const struct sockaddr_storage *s1,
734 const struct sockaddr_storage *s2)
735{
3aff874a
BC
736 const struct sockaddr *sa1 = (const struct sockaddr*) s1;
737 const struct sockaddr *sa2 = (const struct sockaddr*) s2;
738
739 if (sa1->sa_family != sa2->sa_family)
740 return sa1->sa_family - sa2->sa_family;
741 if (sa1->sa_family == AF_INET)
15515b73
EFL
742 return memcmp(&((struct sockaddr_in *)s1)->sin_addr,
743 &((struct sockaddr_in *)s2)->sin_addr,
744 sizeof(struct in_addr));
745#ifndef NO_IPV6
3aff874a 746 if (sa1->sa_family == AF_INET6)
15515b73
EFL
747 return memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,
748 &((struct sockaddr_in6 *)s2)->sin6_addr,
749 sizeof(struct in6_addr));
750#endif
751 return 0;
752}
753
3bd62c21 754static int max_connections = 32;
66e631de 755
3bd62c21 756static unsigned int live_children;
66e631de 757
4d8fa916 758static struct child {
3bd62c21 759 struct child *next;
30e15602 760 struct child_process cld;
df076bdb 761 struct sockaddr_storage address;
3bd62c21 762} *firstborn;
66e631de 763
c295cf06 764static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen)
66e631de 765{
460c2010
JH
766 struct child *newborn, **cradle;
767
460c2010
JH
768 newborn = xcalloc(1, sizeof(*newborn));
769 live_children++;
30e15602 770 memcpy(&newborn->cld, cld, sizeof(*cld));
460c2010
JH
771 memcpy(&newborn->address, addr, addrlen);
772 for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
15515b73 773 if (!addrcmp(&(*cradle)->address, &newborn->address))
460c2010
JH
774 break;
775 newborn->next = *cradle;
776 *cradle = newborn;
66e631de
LT
777}
778
66e631de
LT
779/*
780 * This gets called if the number of connections grows
781 * past "max_connections".
782 *
3bd62c21 783 * We kill the newest connection from a duplicate IP.
66e631de 784 */
3bd62c21 785static void kill_some_child(void)
66e631de 786{
460c2010 787 const struct child *blanket, *next;
66e631de 788
460c2010
JH
789 if (!(blanket = firstborn))
790 return;
695605b5 791
460c2010 792 for (; (next = blanket->next); blanket = next)
15515b73 793 if (!addrcmp(&blanket->address, &next->address)) {
30e15602 794 kill(blanket->cld.pid, SIGTERM);
460c2010
JH
795 break;
796 }
a5a9126b
JS
797}
798
3bd62c21 799static void check_dead_children(void)
a87e8be2 800{
3bd62c21
SB
801 int status;
802 pid_t pid;
02d57da4 803
30e15602
EFL
804 struct child **cradle, *blanket;
805 for (cradle = &firstborn; (blanket = *cradle);)
806 if ((pid = waitpid(blanket->cld.pid, &status, WNOHANG)) > 1) {
807 const char *dead = "";
808 if (status)
809 dead = " (with error)";
810 loginfo("[%"PRIuMAX"] Disconnected%s", (uintmax_t)pid, dead);
811
812 /* remove the child */
813 *cradle = blanket->next;
814 live_children--;
b1b49ff8 815 child_process_clear(&blanket->cld);
30e15602
EFL
816 free(blanket);
817 } else
818 cradle = &blanket->next;
02d57da4
LT
819}
820
850d2fec 821static struct argv_array cld_argv = ARGV_ARRAY_INIT;
c295cf06 822static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
02d57da4 823{
d3180279 824 struct child_process cld = CHILD_PROCESS_INIT;
02d57da4 825
3bd62c21
SB
826 if (max_connections && live_children >= max_connections) {
827 kill_some_child();
460c2010 828 sleep(1); /* give it some time to die */
3bd62c21
SB
829 check_dead_children();
830 if (live_children >= max_connections) {
831 close(incoming);
832 logerror("Too many children, dropping connection");
833 return;
834 }
835 }
02d57da4 836
f9c87be6 837 if (addr->sa_family == AF_INET) {
f063d38b 838 char buf[128] = "";
f9c87be6 839 struct sockaddr_in *sin_addr = (void *) addr;
f063d38b
JK
840 inet_ntop(addr->sa_family, &sin_addr->sin_addr, buf, sizeof(buf));
841 argv_array_pushf(&cld.env_array, "REMOTE_ADDR=%s", buf);
842 argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
843 ntohs(sin_addr->sin_port));
f9c87be6 844#ifndef NO_IPV6
5d9cfa29 845 } else if (addr->sa_family == AF_INET6) {
f063d38b 846 char buf[128] = "";
f9c87be6 847 struct sockaddr_in6 *sin6_addr = (void *) addr;
f063d38b
JK
848 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(buf));
849 argv_array_pushf(&cld.env_array, "REMOTE_ADDR=[%s]", buf);
850 argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
851 ntohs(sin6_addr->sin6_port));
f9c87be6
EFL
852#endif
853 }
854
850d2fec 855 cld.argv = cld_argv.argv;
30e15602
EFL
856 cld.in = incoming;
857 cld.out = dup(incoming);
a87e8be2 858
30e15602
EFL
859 if (start_command(&cld))
860 logerror("unable to fork");
861 else
862 add_child(&cld, addr, addrlen);
a87e8be2
LT
863}
864
eaa94919
LT
865static void child_handler(int signo)
866{
460c2010
JH
867 /*
868 * Otherwise empty handler because systemcalls will get interrupted
695605b5
SB
869 * upon signal receipt
870 * SysV needs the handler to be rearmed
871 */
bd7b371e 872 signal(SIGCHLD, child_handler);
eaa94919
LT
873}
874
1955fabf
MW
875static int set_reuse_addr(int sockfd)
876{
877 int on = 1;
878
879 if (!reuseaddr)
880 return 0;
881 return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
882 &on, sizeof(on));
883}
884
2caa3215
AS
885struct socketlist {
886 int *list;
887 size_t nr;
888 size_t alloc;
889};
890
089d82e8
NTND
891static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
892{
893#ifdef NO_IPV6
894 static char ip[INET_ADDRSTRLEN];
895#else
896 static char ip[INET6_ADDRSTRLEN];
897#endif
898
899 switch (family) {
900#ifndef NO_IPV6
901 case AF_INET6:
902 inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
903 break;
904#endif
905 case AF_INET:
906 inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
907 break;
908 default:
5096d490 909 xsnprintf(ip, sizeof(ip), "<unknown>");
089d82e8
NTND
910 }
911 return ip;
912}
913
6573faff
PA
914#ifndef NO_IPV6
915
2caa3215 916static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
a87e8be2 917{
2caa3215 918 int socknum = 0;
df076bdb 919 char pbuf[NI_MAXSERV];
6573faff
PA
920 struct addrinfo hints, *ai0, *ai;
921 int gai;
20276889 922 long flags;
df076bdb 923
5096d490 924 xsnprintf(pbuf, sizeof(pbuf), "%d", listen_port);
df076bdb
YH
925 memset(&hints, 0, sizeof(hints));
926 hints.ai_family = AF_UNSPEC;
927 hints.ai_socktype = SOCK_STREAM;
928 hints.ai_protocol = IPPROTO_TCP;
929 hints.ai_flags = AI_PASSIVE;
930
dd467629 931 gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
2caa3215
AS
932 if (gai) {
933 logerror("getaddrinfo() for %s failed: %s", listen_addr, gai_strerror(gai));
934 return 0;
935 }
df076bdb 936
df076bdb
YH
937 for (ai = ai0; ai; ai = ai->ai_next) {
938 int sockfd;
df076bdb
YH
939
940 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
941 if (sockfd < 0)
942 continue;
943 if (sockfd >= FD_SETSIZE) {
df0daf8a 944 logerror("Socket descriptor too large");
df076bdb
YH
945 close(sockfd);
946 continue;
947 }
948
949#ifdef IPV6_V6ONLY
950 if (ai->ai_family == AF_INET6) {
951 int on = 1;
952 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
953 &on, sizeof(on));
954 /* Note: error is not fatal */
955 }
956#endif
957
1955fabf 958 if (set_reuse_addr(sockfd)) {
089d82e8 959 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
1955fabf 960 close(sockfd);
0032d548 961 continue;
1955fabf
MW
962 }
963
a43b68a1
EW
964 set_keep_alive(sockfd);
965
df076bdb 966 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
089d82e8
NTND
967 logerror("Could not bind to %s: %s",
968 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
969 strerror(errno));
df076bdb
YH
970 close(sockfd);
971 continue; /* not fatal */
972 }
973 if (listen(sockfd, 5) < 0) {
089d82e8
NTND
974 logerror("Could not listen to %s: %s",
975 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
976 strerror(errno));
df076bdb
YH
977 close(sockfd);
978 continue; /* not fatal */
979 }
980
20276889
AJ
981 flags = fcntl(sockfd, F_GETFD, 0);
982 if (flags >= 0)
983 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
984
2caa3215
AS
985 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
986 socklist->list[socklist->nr++] = sockfd;
987 socknum++;
df076bdb
YH
988 }
989
990 freeaddrinfo(ai0);
991
6573faff
PA
992 return socknum;
993}
994
995#else /* NO_IPV6 */
996
2caa3215 997static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
6573faff
PA
998{
999 struct sockaddr_in sin;
1000 int sockfd;
20276889 1001 long flags;
6573faff 1002
dd467629
JL
1003 memset(&sin, 0, sizeof sin);
1004 sin.sin_family = AF_INET;
1005 sin.sin_port = htons(listen_port);
1006
1007 if (listen_addr) {
1008 /* Well, host better be an IP address here. */
1009 if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
1010 return 0;
1011 } else {
1012 sin.sin_addr.s_addr = htonl(INADDR_ANY);
1013 }
1014
6573faff
PA
1015 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1016 if (sockfd < 0)
1017 return 0;
1018
1955fabf 1019 if (set_reuse_addr(sockfd)) {
089d82e8 1020 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
1955fabf
MW
1021 close(sockfd);
1022 return 0;
1023 }
1024
a43b68a1
EW
1025 set_keep_alive(sockfd);
1026
6573faff 1027 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
9d1b9aa9 1028 logerror("Could not bind to %s: %s",
089d82e8
NTND
1029 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1030 strerror(errno));
6573faff
PA
1031 close(sockfd);
1032 return 0;
1033 }
a87e8be2 1034
f35230fb 1035 if (listen(sockfd, 5) < 0) {
089d82e8
NTND
1036 logerror("Could not listen to %s: %s",
1037 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
1038 strerror(errno));
f35230fb
PS
1039 close(sockfd);
1040 return 0;
1041 }
1042
20276889
AJ
1043 flags = fcntl(sockfd, F_GETFD, 0);
1044 if (flags >= 0)
1045 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
1046
2caa3215
AS
1047 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
1048 socklist->list[socklist->nr++] = sockfd;
f35230fb 1049 return 1;
6573faff
PA
1050}
1051
1052#endif
1053
3a3a29c1 1054static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
2caa3215 1055{
3a3a29c1
AS
1056 if (!listen_addr->nr)
1057 setup_named_sock(NULL, listen_port, socklist);
1058 else {
1059 int i, socknum;
1060 for (i = 0; i < listen_addr->nr; i++) {
1061 socknum = setup_named_sock(listen_addr->items[i].string,
1062 listen_port, socklist);
1063
1064 if (socknum == 0)
1065 logerror("unable to allocate any listen sockets for host %s on port %u",
1066 listen_addr->items[i].string, listen_port);
1067 }
1068 }
2caa3215
AS
1069}
1070
1071static int service_loop(struct socketlist *socklist)
6573faff
PA
1072{
1073 struct pollfd *pfd;
1074 int i;
1075
2caa3215 1076 pfd = xcalloc(socklist->nr, sizeof(struct pollfd));
6573faff 1077
2caa3215
AS
1078 for (i = 0; i < socklist->nr; i++) {
1079 pfd[i].fd = socklist->list[i];
6573faff
PA
1080 pfd[i].events = POLLIN;
1081 }
9220282a
PA
1082
1083 signal(SIGCHLD, child_handler);
a87e8be2
LT
1084
1085 for (;;) {
df076bdb 1086 int i;
6573faff 1087
695605b5
SB
1088 check_dead_children();
1089
2caa3215 1090 if (poll(pfd, socklist->nr, -1) < 0) {
1eef0b33 1091 if (errno != EINTR) {
df0daf8a 1092 logerror("Poll failed, resuming: %s",
1eef0b33
JH
1093 strerror(errno));
1094 sleep(1);
1095 }
df076bdb
YH
1096 continue;
1097 }
1098
2caa3215 1099 for (i = 0; i < socklist->nr; i++) {
6573faff 1100 if (pfd[i].revents & POLLIN) {
f9c87be6
EFL
1101 union {
1102 struct sockaddr sa;
1103 struct sockaddr_in sai;
1104#ifndef NO_IPV6
1105 struct sockaddr_in6 sai6;
1106#endif
1107 } ss;
c295cf06 1108 socklen_t sslen = sizeof(ss);
f9c87be6 1109 int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
df076bdb
YH
1110 if (incoming < 0) {
1111 switch (errno) {
1112 case EAGAIN:
1113 case EINTR:
1114 case ECONNABORTED:
1115 continue;
1116 default:
d824cbba 1117 die_errno("accept returned");
df076bdb
YH
1118 }
1119 }
f9c87be6 1120 handle(incoming, &ss.sa, sslen);
a87e8be2
LT
1121 }
1122 }
a87e8be2
LT
1123 }
1124}
1125
a666b472
EFL
1126#ifdef NO_POSIX_GOODIES
1127
1128struct credentials;
1129
1130static void drop_privileges(struct credentials *cred)
1131{
1132 /* nothing */
1133}
1134
a666b472
EFL
1135static struct credentials *prepare_credentials(const char *user_name,
1136 const char *group_name)
1137{
1138 die("--user not supported on this platform");
1139}
1140
1141#else
1142
1143struct credentials {
1144 struct passwd *pass;
1145 gid_t gid;
1146};
1147
1148static void drop_privileges(struct credentials *cred)
1149{
1150 if (cred && (initgroups(cred->pass->pw_name, cred->gid) ||
1151 setgid (cred->gid) || setuid(cred->pass->pw_uid)))
1152 die("cannot drop privileges");
1153}
1154
1155static struct credentials *prepare_credentials(const char *user_name,
1156 const char *group_name)
1157{
1158 static struct credentials c;
1159
1160 c.pass = getpwnam(user_name);
1161 if (!c.pass)
1162 die("user not found - %s", user_name);
1163
1164 if (!group_name)
1165 c.gid = c.pass->pw_gid;
1166 else {
1167 struct group *group = getgrnam(group_name);
1168 if (!group)
1169 die("group not found - %s", group_name);
1170
1171 c.gid = group->gr_gid;
1172 }
1173
1174 return &c;
1175}
a666b472 1176#endif
a5262768 1177
a666b472
EFL
1178static int serve(struct string_list *listen_addr, int listen_port,
1179 struct credentials *cred)
6573faff 1180{
2caa3215 1181 struct socketlist socklist = { NULL, 0, 0 };
4ae22d96 1182
2caa3215
AS
1183 socksetup(listen_addr, listen_port, &socklist);
1184 if (socklist.nr == 0)
3a3a29c1
AS
1185 die("unable to allocate any listen sockets on port %u",
1186 listen_port);
4ae22d96 1187
a666b472 1188 drop_privileges(cred);
678dac6b 1189
f6a34cfb
CB
1190 loginfo("Ready to rumble");
1191
2caa3215 1192 return service_loop(&socklist);
4ae22d96 1193}
6573faff 1194
a87e8be2
LT
1195int main(int argc, char **argv)
1196{
dd467629 1197 int listen_port = 0;
3a3a29c1 1198 struct string_list listen_addr = STRING_LIST_INIT_NODUP;
30e15602 1199 int serve_mode = 0, inetd_mode = 0;
678dac6b 1200 const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
a5262768 1201 int detach = 0;
a666b472 1202 struct credentials *cred = NULL;
a87e8be2
LT
1203 int i;
1204
5e9637c6
ÆAB
1205 git_setup_gettext();
1206
2fb3f6db
SP
1207 git_extract_argv0_path(argv[0]);
1208
a87e8be2
LT
1209 for (i = 1; i < argc; i++) {
1210 char *arg = argv[i];
ae021d87 1211 const char *v;
a87e8be2 1212
ae021d87
JK
1213 if (skip_prefix(arg, "--listen=", &v)) {
1214 string_list_append(&listen_addr, xstrdup_tolower(v));
6720e95b 1215 continue;
dd467629 1216 }
ae021d87 1217 if (skip_prefix(arg, "--port=", &v)) {
a87e8be2
LT
1218 char *end;
1219 unsigned long n;
ae021d87
JK
1220 n = strtoul(v, &end, 0);
1221 if (*v && !*end) {
dd467629 1222 listen_port = n;
a87e8be2
LT
1223 continue;
1224 }
1225 }
30e15602
EFL
1226 if (!strcmp(arg, "--serve")) {
1227 serve_mode = 1;
1228 continue;
1229 }
e64e1b79
LT
1230 if (!strcmp(arg, "--inetd")) {
1231 inetd_mode = 1;
a8883288 1232 log_syslog = 1;
e64e1b79
LT
1233 continue;
1234 }
f8ff0c06
PB
1235 if (!strcmp(arg, "--verbose")) {
1236 verbose = 1;
1237 continue;
1238 }
9048fe1c
PB
1239 if (!strcmp(arg, "--syslog")) {
1240 log_syslog = 1;
9048fe1c
PB
1241 continue;
1242 }
4ae95682
PA
1243 if (!strcmp(arg, "--export-all")) {
1244 export_all_trees = 1;
1245 continue;
1246 }
ae021d87
JK
1247 if (skip_prefix(arg, "--access-hook=", &v)) {
1248 access_hook = v;
93741e4a
JH
1249 continue;
1250 }
ae021d87
JK
1251 if (skip_prefix(arg, "--timeout=", &v)) {
1252 timeout = atoi(v);
a8883288 1253 continue;
960deccb 1254 }
ae021d87
JK
1255 if (skip_prefix(arg, "--init-timeout=", &v)) {
1256 init_timeout = atoi(v);
a8883288 1257 continue;
960deccb 1258 }
ae021d87
JK
1259 if (skip_prefix(arg, "--max-connections=", &v)) {
1260 max_connections = atoi(v);
3bd62c21
SB
1261 if (max_connections < 0)
1262 max_connections = 0; /* unlimited */
1263 continue;
1264 }
4dbd1352
AE
1265 if (!strcmp(arg, "--strict-paths")) {
1266 strict_paths = 1;
1267 continue;
1268 }
ae021d87
JK
1269 if (skip_prefix(arg, "--base-path=", &v)) {
1270 base_path = v;
b21c31c9
PB
1271 continue;
1272 }
73a7a656
JA
1273 if (!strcmp(arg, "--base-path-relaxed")) {
1274 base_path_relaxed = 1;
1275 continue;
1276 }
ae021d87
JK
1277 if (skip_prefix(arg, "--interpolated-path=", &v)) {
1278 interpolated_path = v;
49ba83fb
JL
1279 continue;
1280 }
1955fabf
MW
1281 if (!strcmp(arg, "--reuseaddr")) {
1282 reuseaddr = 1;
1283 continue;
1284 }
603968d2
JH
1285 if (!strcmp(arg, "--user-path")) {
1286 user_path = "";
1287 continue;
1288 }
ae021d87
JK
1289 if (skip_prefix(arg, "--user-path=", &v)) {
1290 user_path = v;
603968d2
JH
1291 continue;
1292 }
ae021d87
JK
1293 if (skip_prefix(arg, "--pid-file=", &v)) {
1294 pid_file = v;
45ed5d7f
ML
1295 continue;
1296 }
a5262768
ML
1297 if (!strcmp(arg, "--detach")) {
1298 detach = 1;
1299 log_syslog = 1;
1300 continue;
1301 }
ae021d87
JK
1302 if (skip_prefix(arg, "--user=", &v)) {
1303 user_name = v;
678dac6b
TS
1304 continue;
1305 }
ae021d87
JK
1306 if (skip_prefix(arg, "--group=", &v)) {
1307 group_name = v;
678dac6b
TS
1308 continue;
1309 }
ae021d87
JK
1310 if (skip_prefix(arg, "--enable=", &v)) {
1311 enable_service(v, 1);
d819e4e6
JH
1312 continue;
1313 }
ae021d87
JK
1314 if (skip_prefix(arg, "--disable=", &v)) {
1315 enable_service(v, 0);
d819e4e6
JH
1316 continue;
1317 }
ae021d87
JK
1318 if (skip_prefix(arg, "--allow-override=", &v)) {
1319 make_service_overridable(v, 1);
d819e4e6
JH
1320 continue;
1321 }
ae021d87
JK
1322 if (skip_prefix(arg, "--forbid-override=", &v)) {
1323 make_service_overridable(v, 0);
d819e4e6
JH
1324 continue;
1325 }
82246b76 1326 if (!strcmp(arg, "--informative-errors")) {
d5570f4d
JK
1327 informative_errors = 1;
1328 continue;
1329 }
82246b76 1330 if (!strcmp(arg, "--no-informative-errors")) {
d5570f4d
JK
1331 informative_errors = 0;
1332 continue;
1333 }
4ae95682
PA
1334 if (!strcmp(arg, "--")) {
1335 ok_paths = &argv[i+1];
1336 break;
1337 } else if (arg[0] != '-') {
1338 ok_paths = &argv[i];
1339 break;
1340 }
e64e1b79 1341
a87e8be2
LT
1342 usage(daemon_usage);
1343 }
1344
22665bba 1345 if (log_syslog) {
6a992e9e 1346 openlog("git-daemon", LOG_PID, LOG_DAEMON);
22665bba 1347 set_die_routine(daemon_die);
460c2010 1348 } else
48196afd 1349 /* avoid splitting a message in the middle */
48cfaea1 1350 setvbuf(stderr, NULL, _IOFBF, 4096);
22665bba 1351
9cddf56e
EFL
1352 if (inetd_mode && (detach || group_name || user_name))
1353 die("--detach, --user and --group are incompatible with --inetd");
678dac6b 1354
3a3a29c1 1355 if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
dd467629
JL
1356 die("--listen= and --port= are incompatible with --inetd");
1357 else if (listen_port == 0)
1358 listen_port = DEFAULT_GIT_PORT;
1359
678dac6b
TS
1360 if (group_name && !user_name)
1361 die("--group supplied without --user");
1362
a666b472
EFL
1363 if (user_name)
1364 cred = prepare_credentials(user_name, group_name);
678dac6b 1365
ad8b4f56
ML
1366 if (strict_paths && (!ok_paths || !*ok_paths))
1367 die("option --strict-paths requires a whitelist");
1368
90b4a71c
JH
1369 if (base_path && !is_directory(base_path))
1370 die("base-path '%s' does not exist or is not a directory",
1371 base_path);
20632071 1372
7c3693f1 1373 if (inetd_mode) {
30e15602
EFL
1374 if (!freopen("/dev/null", "w", stderr))
1375 die_errno("failed to redirect stderr to /dev/null");
1376 }
1377
f9c87be6
EFL
1378 if (inetd_mode || serve_mode)
1379 return execute();
bce8230d 1380
de0957ce
NTND
1381 if (detach) {
1382 if (daemonize())
1383 die("--detach not supported on this platform");
1384 } else
a5262768 1385 sanitize_stdfds();
258e93a1 1386
45ed5d7f 1387 if (pid_file)
1f76a10b 1388 write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());
45ed5d7f 1389
30e15602 1390 /* prepare argv for serving-processes */
850d2fec
JK
1391 argv_array_push(&cld_argv, argv[0]); /* git-daemon */
1392 argv_array_push(&cld_argv, "--serve");
081f84ee 1393 for (i = 1; i < argc; ++i)
850d2fec 1394 argv_array_push(&cld_argv, argv[i]);
30e15602 1395
a666b472 1396 return serve(&listen_addr, listen_port, cred);
a87e8be2 1397}