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