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