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