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