4 #include "run-command.h"
6 #include "string-list.h"
9 #define HOST_NAME_MAX 256
17 #define initgroups(x, y) (0) /* nothing */
20 static int log_syslog;
24 static const char daemon_usage[] =
25 "git daemon [--verbose] [--syslog] [--export-all]\n"
26 " [--timeout=<n>] [--init-timeout=<n>] [--max-connections=<n>]\n"
27 " [--strict-paths] [--base-path=<path>] [--base-path-relaxed]\n"
28 " [--user-path | --user-path=<path>]\n"
29 " [--interpolated-path=<path>]\n"
30 " [--reuseaddr] [--pid-file=<file>]\n"
31 " [--(enable|disable|allow-override|forbid-override)=<service>]\n"
32 " [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
33 " [--detach] [--user=<user> [--group=<group>]]\n"
36 /* List of acceptable pathname prefixes */
37 static char **ok_paths;
38 static int strict_paths;
40 /* If this is set, git-daemon-export-ok is not required */
41 static int export_all_trees;
43 /* Take all paths relative to this one if non-NULL */
44 static char *base_path;
45 static char *interpolated_path;
46 static int base_path_relaxed;
48 /* Flag indicating client sent extra args. */
49 static int saw_extended_args;
51 /* If defined, ~user notation is allowed and the string is inserted
52 * after ~user/. E.g. a request to git://host/~alice/frotz would
53 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
55 static const char *user_path;
57 /* Timeout, and initial timeout */
58 static unsigned int timeout;
59 static unsigned int init_timeout;
61 static char *hostname;
62 static char *canon_hostname;
63 static char *ip_address;
64 static char *tcp_port;
66 static void logreport(int priority, const char *err, va_list params)
70 vsnprintf(buf, sizeof(buf), err, params);
71 syslog(priority, "%s", buf);
74 * Since stderr is set to buffered mode, the
75 * logging of different processes will not overlap
76 * unless they overflow the (rather big) buffers.
78 fprintf(stderr, "[%"PRIuMAX"] ", (uintmax_t)getpid());
79 vfprintf(stderr, err, params);
85 __attribute__((format (printf, 1, 2)))
86 static void logerror(const char *err, ...)
89 va_start(params, err);
90 logreport(LOG_ERR, err, params);
94 __attribute__((format (printf, 1, 2)))
95 static void loginfo(const char *err, ...)
100 va_start(params, err);
101 logreport(LOG_INFO, err, params);
105 static void NORETURN daemon_die(const char *err, va_list params)
107 logreport(LOG_ERR, err, params);
111 static char *path_ok(char *directory)
113 static char rpath[PATH_MAX];
114 static char interp_path[PATH_MAX];
120 if (daemon_avoid_alias(dir)) {
121 logerror("'%s': aliased", dir);
127 logerror("'%s': User-path not allowed", dir);
131 /* Got either "~alice" or "~alice/foo";
132 * rewrite them to "~alice/%s" or
135 int namlen, restlen = strlen(dir);
136 char *slash = strchr(dir, '/');
138 slash = dir + restlen;
139 namlen = slash - dir;
141 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
142 snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
143 namlen, dir, user_path, restlen, slash);
147 else if (interpolated_path && saw_extended_args) {
148 struct strbuf expanded_path = STRBUF_INIT;
149 struct strbuf_expand_dict_entry dict[6];
151 dict[0].placeholder = "H"; dict[0].value = hostname;
152 dict[1].placeholder = "CH"; dict[1].value = canon_hostname;
153 dict[2].placeholder = "IP"; dict[2].value = ip_address;
154 dict[3].placeholder = "P"; dict[3].value = tcp_port;
155 dict[4].placeholder = "D"; dict[4].value = directory;
156 dict[5].placeholder = NULL; dict[5].value = NULL;
158 /* Allow only absolute */
159 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
163 strbuf_expand(&expanded_path, interpolated_path,
164 strbuf_expand_dict_cb, &dict);
165 strlcpy(interp_path, expanded_path.buf, PATH_MAX);
166 strbuf_release(&expanded_path);
167 loginfo("Interpolated dir '%s'", interp_path);
171 else if (base_path) {
173 /* Allow only absolute */
174 logerror("'%s': Non-absolute path denied (base-path active)", dir);
177 snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
181 path = enter_repo(dir, strict_paths);
182 if (!path && base_path && base_path_relaxed) {
184 * if we fail and base_path_relaxed is enabled, try without
185 * prefixing the base path
188 path = enter_repo(dir, strict_paths);
192 logerror("'%s' does not appear to be a git repository", dir);
196 if ( ok_paths && *ok_paths ) {
198 int pathlen = strlen(path);
200 /* The validation is done on the paths after enter_repo
201 * appends optional {.git,.git/.git} and friends, but
202 * it does not use getcwd(). So if your /pub is
203 * a symlink to /mnt/pub, you can whitelist /pub and
204 * do not have to say /mnt/pub.
207 for ( pp = ok_paths ; *pp ; pp++ ) {
208 int len = strlen(*pp);
209 if (len <= pathlen &&
210 !memcmp(*pp, path, len) &&
211 (path[len] == '\0' ||
212 (!strict_paths && path[len] == '/')))
217 /* be backwards compatible */
222 logerror("'%s': not in whitelist", path);
223 return NULL; /* Fallthrough. Deny by default */
226 typedef int (*daemon_service_fn)(void);
227 struct daemon_service {
229 const char *config_name;
230 daemon_service_fn fn;
235 static struct daemon_service *service_looking_at;
236 static int service_enabled;
238 static int git_daemon_config(const char *var, const char *value, void *cb)
240 if (!prefixcmp(var, "daemon.") &&
241 !strcmp(var + 7, service_looking_at->config_name)) {
242 service_enabled = git_config_bool(var, value);
246 /* we are not interested in parsing any other configuration here */
250 static int run_service(char *dir, struct daemon_service *service)
253 int enabled = service->enabled;
255 loginfo("Request %s for '%s'", service->name, dir);
257 if (!enabled && !service->overridable) {
258 logerror("'%s': service not enabled.", service->name);
263 if (!(path = path_ok(dir)))
267 * Security on the cheap.
269 * We want a readable HEAD, usable "objects" directory, and
270 * a "git-daemon-export-ok" flag that says that the other side
271 * is ok with us doing this.
273 * path_ok() uses enter_repo() and does whitelist checking.
274 * We only need to make sure the repository is exported.
277 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
278 logerror("'%s': repository not exported.", path);
283 if (service->overridable) {
284 service_looking_at = service;
285 service_enabled = -1;
286 git_config(git_daemon_config, NULL);
287 if (0 <= service_enabled)
288 enabled = service_enabled;
291 logerror("'%s': service not enabled for '%s'",
292 service->name, path);
298 * We'll ignore SIGTERM from now on, we have a
301 signal(SIGTERM, SIG_IGN);
303 return service->fn();
306 packet_write(1, "ERR %s: access denied", dir);
310 static void copy_to_log(int fd)
312 struct strbuf line = STRBUF_INIT;
315 fp = fdopen(fd, "r");
317 logerror("fdopen of error channel failed");
322 while (strbuf_getline(&line, fp, '\n') != EOF) {
323 logerror("%s", line.buf);
324 strbuf_setlen(&line, 0);
327 strbuf_release(&line);
331 static int run_service_command(const char **argv)
333 struct child_process cld;
335 memset(&cld, 0, sizeof(cld));
339 if (start_command(&cld))
345 copy_to_log(cld.err);
347 return finish_command(&cld);
350 static int upload_pack(void)
352 /* Timeout as string */
353 char timeout_buf[64];
354 const char *argv[] = { "upload-pack", "--strict", NULL, ".", NULL };
356 argv[2] = timeout_buf;
358 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
359 return run_service_command(argv);
362 static int upload_archive(void)
364 static const char *argv[] = { "upload-archive", ".", NULL };
365 return run_service_command(argv);
368 static int receive_pack(void)
370 static const char *argv[] = { "receive-pack", ".", NULL };
371 return run_service_command(argv);
374 static struct daemon_service daemon_service[] = {
375 { "upload-archive", "uploadarch", upload_archive, 0, 1 },
376 { "upload-pack", "uploadpack", upload_pack, 1, 1 },
377 { "receive-pack", "receivepack", receive_pack, 0, 1 },
380 static void enable_service(const char *name, int ena)
383 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
384 if (!strcmp(daemon_service[i].name, name)) {
385 daemon_service[i].enabled = ena;
389 die("No such service %s", name);
392 static void make_service_overridable(const char *name, int ena)
395 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
396 if (!strcmp(daemon_service[i].name, name)) {
397 daemon_service[i].overridable = ena;
401 die("No such service %s", name);
404 static char *xstrdup_tolower(const char *str)
406 char *p, *dup = xstrdup(str);
407 for (p = dup; *p; p++)
412 static void parse_host_and_port(char *hostport, char **host,
415 if (*hostport == '[') {
418 end = strchr(hostport, ']');
420 die("Invalid request ('[' without ']')");
422 *host = hostport + 1;
425 else if (end[1] == ':')
428 die("Garbage after end of host part");
431 *port = strrchr(hostport, ':');
440 * Read the host as supplied by the client connection.
442 static void parse_host_arg(char *extra_args, int buflen)
446 char *end = extra_args + buflen;
448 if (extra_args < end && *extra_args) {
449 saw_extended_args = 1;
450 if (strncasecmp("host=", extra_args, 5) == 0) {
451 val = extra_args + 5;
452 vallen = strlen(val) + 1;
454 /* Split <host>:<port> at colon. */
457 parse_host_and_port(val, &host, &port);
460 tcp_port = xstrdup(port);
463 hostname = xstrdup_tolower(host);
466 /* On to the next one */
467 extra_args = val + vallen;
469 if (extra_args < end && *extra_args)
470 die("Invalid request");
474 * Locate canonical hostname and its IP address.
478 struct addrinfo hints;
481 static char addrbuf[HOST_NAME_MAX + 1];
483 memset(&hints, 0, sizeof(hints));
484 hints.ai_flags = AI_CANONNAME;
486 gai = getaddrinfo(hostname, NULL, &hints, &ai);
488 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
490 inet_ntop(AF_INET, &sin_addr->sin_addr,
491 addrbuf, sizeof(addrbuf));
493 ip_address = xstrdup(addrbuf);
495 free(canon_hostname);
496 canon_hostname = xstrdup(ai->ai_canonname ?
497 ai->ai_canonname : ip_address);
502 struct hostent *hent;
503 struct sockaddr_in sa;
505 static char addrbuf[HOST_NAME_MAX + 1];
507 hent = gethostbyname(hostname);
509 ap = hent->h_addr_list;
510 memset(&sa, 0, sizeof sa);
511 sa.sin_family = hent->h_addrtype;
512 sa.sin_port = htons(0);
513 memcpy(&sa.sin_addr, *ap, hent->h_length);
515 inet_ntop(hent->h_addrtype, &sa.sin_addr,
516 addrbuf, sizeof(addrbuf));
518 free(canon_hostname);
519 canon_hostname = xstrdup(hent->h_name);
521 ip_address = xstrdup(addrbuf);
527 static int execute(void)
529 static char line[1000];
531 char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
534 loginfo("Connection from %s:%s", addr, port);
536 alarm(init_timeout ? init_timeout : timeout);
537 pktlen = packet_read_line(0, line, sizeof(line));
542 loginfo("Extended attributes (%d bytes) exist <%.*s>",
544 (int) pktlen - len, line + len + 1);
545 if (len && line[len-1] == '\n') {
551 free(canon_hostname);
554 hostname = canon_hostname = ip_address = tcp_port = NULL;
557 parse_host_arg(line + len + 1, pktlen - len - 1);
559 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
560 struct daemon_service *s = &(daemon_service[i]);
561 int namelen = strlen(s->name);
562 if (!prefixcmp(line, "git-") &&
563 !strncmp(s->name, line + 4, namelen) &&
564 line[namelen + 4] == ' ') {
566 * Note: The directory here is probably context sensitive,
567 * and might depend on the actual service being performed.
569 return run_service(line + namelen + 5, s);
573 logerror("Protocol error: '%s'", line);
577 static int addrcmp(const struct sockaddr_storage *s1,
578 const struct sockaddr_storage *s2)
580 const struct sockaddr *sa1 = (const struct sockaddr*) s1;
581 const struct sockaddr *sa2 = (const struct sockaddr*) s2;
583 if (sa1->sa_family != sa2->sa_family)
584 return sa1->sa_family - sa2->sa_family;
585 if (sa1->sa_family == AF_INET)
586 return memcmp(&((struct sockaddr_in *)s1)->sin_addr,
587 &((struct sockaddr_in *)s2)->sin_addr,
588 sizeof(struct in_addr));
590 if (sa1->sa_family == AF_INET6)
591 return memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,
592 &((struct sockaddr_in6 *)s2)->sin6_addr,
593 sizeof(struct in6_addr));
598 static int max_connections = 32;
600 static unsigned int live_children;
602 static struct child {
604 struct child_process cld;
605 struct sockaddr_storage address;
608 static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen)
610 struct child *newborn, **cradle;
612 newborn = xcalloc(1, sizeof(*newborn));
614 memcpy(&newborn->cld, cld, sizeof(*cld));
615 memcpy(&newborn->address, addr, addrlen);
616 for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
617 if (!addrcmp(&(*cradle)->address, &newborn->address))
619 newborn->next = *cradle;
624 * This gets called if the number of connections grows
625 * past "max_connections".
627 * We kill the newest connection from a duplicate IP.
629 static void kill_some_child(void)
631 const struct child *blanket, *next;
633 if (!(blanket = firstborn))
636 for (; (next = blanket->next); blanket = next)
637 if (!addrcmp(&blanket->address, &next->address)) {
638 kill(blanket->cld.pid, SIGTERM);
643 static void check_dead_children(void)
648 struct child **cradle, *blanket;
649 for (cradle = &firstborn; (blanket = *cradle);)
650 if ((pid = waitpid(blanket->cld.pid, &status, WNOHANG)) > 1) {
651 const char *dead = "";
653 dead = " (with error)";
654 loginfo("[%"PRIuMAX"] Disconnected%s", (uintmax_t)pid, dead);
656 /* remove the child */
657 *cradle = blanket->next;
661 cradle = &blanket->next;
664 static char **cld_argv;
665 static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
667 struct child_process cld = { NULL };
668 char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
669 char *env[] = { addrbuf, portbuf, NULL };
671 if (max_connections && live_children >= max_connections) {
673 sleep(1); /* give it some time to die */
674 check_dead_children();
675 if (live_children >= max_connections) {
677 logerror("Too many children, dropping connection");
682 if (addr->sa_family == AF_INET) {
683 struct sockaddr_in *sin_addr = (void *) addr;
684 inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf + 12,
685 sizeof(addrbuf) - 12);
686 snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
687 ntohs(sin_addr->sin_port));
689 } else if (addr && addr->sa_family == AF_INET6) {
690 struct sockaddr_in6 *sin6_addr = (void *) addr;
692 char *buf = addrbuf + 12;
693 *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
694 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf,
695 sizeof(addrbuf) - 13);
698 snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
699 ntohs(sin6_addr->sin6_port));
703 cld.env = (const char **)env;
704 cld.argv = (const char **)cld_argv;
706 cld.out = dup(incoming);
708 if (start_command(&cld))
709 logerror("unable to fork");
711 add_child(&cld, addr, addrlen);
715 static void child_handler(int signo)
718 * Otherwise empty handler because systemcalls will get interrupted
719 * upon signal receipt
720 * SysV needs the handler to be rearmed
722 signal(SIGCHLD, child_handler);
725 static int set_reuse_addr(int sockfd)
731 return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
741 static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
744 static char ip[INET_ADDRSTRLEN];
746 static char ip[INET6_ADDRSTRLEN];
752 inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
756 inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
759 strcpy(ip, "<unknown>");
766 static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
770 char pbuf[NI_MAXSERV];
771 struct addrinfo hints, *ai0, *ai;
775 sprintf(pbuf, "%d", listen_port);
776 memset(&hints, 0, sizeof(hints));
777 hints.ai_family = AF_UNSPEC;
778 hints.ai_socktype = SOCK_STREAM;
779 hints.ai_protocol = IPPROTO_TCP;
780 hints.ai_flags = AI_PASSIVE;
782 gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
784 logerror("getaddrinfo() for %s failed: %s", listen_addr, gai_strerror(gai));
788 for (ai = ai0; ai; ai = ai->ai_next) {
791 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
794 if (sockfd >= FD_SETSIZE) {
795 logerror("Socket descriptor too large");
801 if (ai->ai_family == AF_INET6) {
803 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
805 /* Note: error is not fatal */
809 if (set_reuse_addr(sockfd)) {
810 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
815 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
816 logerror("Could not bind to %s: %s",
817 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
820 continue; /* not fatal */
822 if (listen(sockfd, 5) < 0) {
823 logerror("Could not listen to %s: %s",
824 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
827 continue; /* not fatal */
830 flags = fcntl(sockfd, F_GETFD, 0);
832 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
834 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
835 socklist->list[socklist->nr++] = sockfd;
849 static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
851 struct sockaddr_in sin;
855 memset(&sin, 0, sizeof sin);
856 sin.sin_family = AF_INET;
857 sin.sin_port = htons(listen_port);
860 /* Well, host better be an IP address here. */
861 if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
864 sin.sin_addr.s_addr = htonl(INADDR_ANY);
867 sockfd = socket(AF_INET, SOCK_STREAM, 0);
871 if (set_reuse_addr(sockfd)) {
872 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
877 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
878 logerror("Could not listen to %s: %s",
879 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
885 if (listen(sockfd, 5) < 0) {
886 logerror("Could not listen to %s: %s",
887 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
893 flags = fcntl(sockfd, F_GETFD, 0);
895 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
897 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
898 socklist->list[socklist->nr++] = sockfd;
904 static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
906 if (!listen_addr->nr)
907 setup_named_sock(NULL, listen_port, socklist);
910 for (i = 0; i < listen_addr->nr; i++) {
911 socknum = setup_named_sock(listen_addr->items[i].string,
912 listen_port, socklist);
915 logerror("unable to allocate any listen sockets for host %s on port %u",
916 listen_addr->items[i].string, listen_port);
921 static int service_loop(struct socketlist *socklist)
926 pfd = xcalloc(socklist->nr, sizeof(struct pollfd));
928 for (i = 0; i < socklist->nr; i++) {
929 pfd[i].fd = socklist->list[i];
930 pfd[i].events = POLLIN;
933 signal(SIGCHLD, child_handler);
938 check_dead_children();
940 if (poll(pfd, socklist->nr, -1) < 0) {
941 if (errno != EINTR) {
942 logerror("Poll failed, resuming: %s",
949 for (i = 0; i < socklist->nr; i++) {
950 if (pfd[i].revents & POLLIN) {
953 struct sockaddr_in sai;
955 struct sockaddr_in6 sai6;
958 socklen_t sslen = sizeof(ss);
959 int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
967 die_errno("accept returned");
970 handle(incoming, &ss.sa, sslen);
976 /* if any standard file descriptor is missing open it to /dev/null */
977 static void sanitize_stdfds(void)
979 int fd = open("/dev/null", O_RDWR, 0);
980 while (fd != -1 && fd < 2)
983 die_errno("open /dev/null or dup failed");
988 #ifdef NO_POSIX_GOODIES
992 static void drop_privileges(struct credentials *cred)
997 static void daemonize(void)
999 die("--detach not supported on this platform");
1002 static struct credentials *prepare_credentials(const char *user_name,
1003 const char *group_name)
1005 die("--user not supported on this platform");
1010 struct credentials {
1011 struct passwd *pass;
1015 static void drop_privileges(struct credentials *cred)
1017 if (cred && (initgroups(cred->pass->pw_name, cred->gid) ||
1018 setgid (cred->gid) || setuid(cred->pass->pw_uid)))
1019 die("cannot drop privileges");
1022 static struct credentials *prepare_credentials(const char *user_name,
1023 const char *group_name)
1025 static struct credentials c;
1027 c.pass = getpwnam(user_name);
1029 die("user not found - %s", user_name);
1032 c.gid = c.pass->pw_gid;
1034 struct group *group = getgrnam(group_name);
1036 die("group not found - %s", group_name);
1038 c.gid = group->gr_gid;
1044 static void daemonize(void)
1050 die_errno("fork failed");
1055 die_errno("setsid failed");
1063 static void store_pid(const char *path)
1065 FILE *f = fopen(path, "w");
1067 die_errno("cannot open pid file '%s'", path);
1068 if (fprintf(f, "%"PRIuMAX"\n", (uintmax_t) getpid()) < 0 || fclose(f) != 0)
1069 die_errno("failed to write pid file '%s'", path);
1072 static int serve(struct string_list *listen_addr, int listen_port,
1073 struct credentials *cred)
1075 struct socketlist socklist = { NULL, 0, 0 };
1077 socksetup(listen_addr, listen_port, &socklist);
1078 if (socklist.nr == 0)
1079 die("unable to allocate any listen sockets on port %u",
1082 drop_privileges(cred);
1084 return service_loop(&socklist);
1087 int main(int argc, char **argv)
1089 int listen_port = 0;
1090 struct string_list listen_addr = STRING_LIST_INIT_NODUP;
1091 int serve_mode = 0, inetd_mode = 0;
1092 const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
1094 struct credentials *cred = NULL;
1097 git_extract_argv0_path(argv[0]);
1099 for (i = 1; i < argc; i++) {
1100 char *arg = argv[i];
1102 if (!prefixcmp(arg, "--listen=")) {
1103 string_list_append(&listen_addr, xstrdup_tolower(arg + 9));
1106 if (!prefixcmp(arg, "--port=")) {
1109 n = strtoul(arg+7, &end, 0);
1110 if (arg[7] && !*end) {
1115 if (!strcmp(arg, "--serve")) {
1119 if (!strcmp(arg, "--inetd")) {
1124 if (!strcmp(arg, "--verbose")) {
1128 if (!strcmp(arg, "--syslog")) {
1132 if (!strcmp(arg, "--export-all")) {
1133 export_all_trees = 1;
1136 if (!prefixcmp(arg, "--timeout=")) {
1137 timeout = atoi(arg+10);
1140 if (!prefixcmp(arg, "--init-timeout=")) {
1141 init_timeout = atoi(arg+15);
1144 if (!prefixcmp(arg, "--max-connections=")) {
1145 max_connections = atoi(arg+18);
1146 if (max_connections < 0)
1147 max_connections = 0; /* unlimited */
1150 if (!strcmp(arg, "--strict-paths")) {
1154 if (!prefixcmp(arg, "--base-path=")) {
1158 if (!strcmp(arg, "--base-path-relaxed")) {
1159 base_path_relaxed = 1;
1162 if (!prefixcmp(arg, "--interpolated-path=")) {
1163 interpolated_path = arg+20;
1166 if (!strcmp(arg, "--reuseaddr")) {
1170 if (!strcmp(arg, "--user-path")) {
1174 if (!prefixcmp(arg, "--user-path=")) {
1175 user_path = arg + 12;
1178 if (!prefixcmp(arg, "--pid-file=")) {
1179 pid_file = arg + 11;
1182 if (!strcmp(arg, "--detach")) {
1187 if (!prefixcmp(arg, "--user=")) {
1188 user_name = arg + 7;
1191 if (!prefixcmp(arg, "--group=")) {
1192 group_name = arg + 8;
1195 if (!prefixcmp(arg, "--enable=")) {
1196 enable_service(arg + 9, 1);
1199 if (!prefixcmp(arg, "--disable=")) {
1200 enable_service(arg + 10, 0);
1203 if (!prefixcmp(arg, "--allow-override=")) {
1204 make_service_overridable(arg + 17, 1);
1207 if (!prefixcmp(arg, "--forbid-override=")) {
1208 make_service_overridable(arg + 18, 0);
1211 if (!strcmp(arg, "--")) {
1212 ok_paths = &argv[i+1];
1214 } else if (arg[0] != '-') {
1215 ok_paths = &argv[i];
1219 usage(daemon_usage);
1223 openlog("git-daemon", LOG_PID, LOG_DAEMON);
1224 set_die_routine(daemon_die);
1226 /* avoid splitting a message in the middle */
1227 setvbuf(stderr, NULL, _IOFBF, 4096);
1229 if (inetd_mode && (detach || group_name || user_name))
1230 die("--detach, --user and --group are incompatible with --inetd");
1232 if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
1233 die("--listen= and --port= are incompatible with --inetd");
1234 else if (listen_port == 0)
1235 listen_port = DEFAULT_GIT_PORT;
1237 if (group_name && !user_name)
1238 die("--group supplied without --user");
1241 cred = prepare_credentials(user_name, group_name);
1243 if (strict_paths && (!ok_paths || !*ok_paths))
1244 die("option --strict-paths requires a whitelist");
1246 if (base_path && !is_directory(base_path))
1247 die("base-path '%s' does not exist or is not a directory",
1251 if (!freopen("/dev/null", "w", stderr))
1252 die_errno("failed to redirect stderr to /dev/null");
1255 if (inetd_mode || serve_mode)
1260 loginfo("Ready to rumble");
1266 store_pid(pid_file);
1268 /* prepare argv for serving-processes */
1269 cld_argv = xmalloc(sizeof (char *) * (argc + 2));
1270 cld_argv[0] = argv[0]; /* git-daemon */
1271 cld_argv[1] = "--serve";
1272 for (i = 1; i < argc; ++i)
1273 cld_argv[i+1] = argv[i];
1274 cld_argv[argc+1] = NULL;
1276 return serve(&listen_addr, listen_port, cred);