4 #include "run-command.h"
6 #include "string-list.h"
9 #define HOST_NAME_MAX 256
13 #define initgroups(x, y) (0) /* nothing */
16 static int log_syslog;
19 static int informative_errors;
21 static const char daemon_usage[] =
22 "git daemon [--verbose] [--syslog] [--export-all]\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"
27 " [--reuseaddr] [--pid-file=<file>]\n"
28 " [--(enable|disable|allow-override|forbid-override)=<service>]\n"
29 " [--access-hook=<path>]\n"
30 " [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
31 " [--detach] [--user=<user> [--group=<group>]]\n"
34 /* List of acceptable pathname prefixes */
35 static char **ok_paths;
36 static int strict_paths;
38 /* If this is set, git-daemon-export-ok is not required */
39 static int export_all_trees;
41 /* Take all paths relative to this one if non-NULL */
42 static const char *base_path;
43 static const char *interpolated_path;
44 static int base_path_relaxed;
46 /* Flag indicating client sent extra args. */
47 static int saw_extended_args;
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.
53 static const char *user_path;
55 /* Timeout, and initial timeout */
56 static unsigned int timeout;
57 static unsigned int init_timeout;
59 static char *hostname;
60 static char *canon_hostname;
61 static char *ip_address;
62 static char *tcp_port;
64 static void logreport(int priority, const char *err, va_list params)
68 vsnprintf(buf, sizeof(buf), err, params);
69 syslog(priority, "%s", buf);
72 * Since stderr is set to buffered mode, the
73 * logging of different processes will not overlap
74 * unless they overflow the (rather big) buffers.
76 fprintf(stderr, "[%"PRIuMAX"] ", (uintmax_t)getpid());
77 vfprintf(stderr, err, params);
83 __attribute__((format (printf, 1, 2)))
84 static void logerror(const char *err, ...)
87 va_start(params, err);
88 logreport(LOG_ERR, err, params);
92 __attribute__((format (printf, 1, 2)))
93 static void loginfo(const char *err, ...)
98 va_start(params, err);
99 logreport(LOG_INFO, err, params);
103 static void NORETURN daemon_die(const char *err, va_list params)
105 logreport(LOG_ERR, err, params);
109 static const char *path_ok(const char *directory)
111 static char rpath[PATH_MAX];
112 static char interp_path[PATH_MAX];
118 if (daemon_avoid_alias(dir)) {
119 logerror("'%s': aliased", dir);
125 logerror("'%s': User-path not allowed", dir);
129 /* Got either "~alice" or "~alice/foo";
130 * rewrite them to "~alice/%s" or
133 int namlen, restlen = strlen(dir);
134 const char *slash = strchr(dir, '/');
136 slash = dir + restlen;
137 namlen = slash - dir;
139 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
140 snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
141 namlen, dir, user_path, restlen, slash);
145 else if (interpolated_path && saw_extended_args) {
146 struct strbuf expanded_path = STRBUF_INIT;
147 struct strbuf_expand_dict_entry dict[6];
149 dict[0].placeholder = "H"; dict[0].value = hostname;
150 dict[1].placeholder = "CH"; dict[1].value = canon_hostname;
151 dict[2].placeholder = "IP"; dict[2].value = ip_address;
152 dict[3].placeholder = "P"; dict[3].value = tcp_port;
153 dict[4].placeholder = "D"; dict[4].value = directory;
154 dict[5].placeholder = NULL; dict[5].value = NULL;
156 /* Allow only absolute */
157 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
161 strbuf_expand(&expanded_path, interpolated_path,
162 strbuf_expand_dict_cb, &dict);
163 strlcpy(interp_path, expanded_path.buf, PATH_MAX);
164 strbuf_release(&expanded_path);
165 loginfo("Interpolated dir '%s'", interp_path);
169 else if (base_path) {
171 /* Allow only absolute */
172 logerror("'%s': Non-absolute path denied (base-path active)", dir);
175 snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
179 path = enter_repo(dir, strict_paths);
180 if (!path && base_path && base_path_relaxed) {
182 * if we fail and base_path_relaxed is enabled, try without
183 * prefixing the base path
186 path = enter_repo(dir, strict_paths);
190 logerror("'%s' does not appear to be a git repository", dir);
194 if ( ok_paths && *ok_paths ) {
196 int pathlen = strlen(path);
198 /* The validation is done on the paths after enter_repo
199 * appends optional {.git,.git/.git} and friends, but
200 * it does not use getcwd(). So if your /pub is
201 * a symlink to /mnt/pub, you can whitelist /pub and
202 * do not have to say /mnt/pub.
205 for ( pp = ok_paths ; *pp ; pp++ ) {
206 int len = strlen(*pp);
207 if (len <= pathlen &&
208 !memcmp(*pp, path, len) &&
209 (path[len] == '\0' ||
210 (!strict_paths && path[len] == '/')))
215 /* be backwards compatible */
220 logerror("'%s': not in whitelist", path);
221 return NULL; /* Fallthrough. Deny by default */
224 typedef int (*daemon_service_fn)(void);
225 struct daemon_service {
227 const char *config_name;
228 daemon_service_fn fn;
233 static int daemon_error(const char *dir, const char *msg)
235 if (!informative_errors)
236 msg = "access denied or repository not exported";
237 packet_write(1, "ERR %s: %s", msg, dir);
241 static const char *access_hook;
243 static int run_access_hook(struct daemon_service *service, const char *dir, const char *path)
245 struct child_process child = CHILD_PROCESS_INIT;
246 struct strbuf buf = STRBUF_INIT;
248 const char **arg = argv;
252 #define STRARG(x) ((x) ? (x) : "")
253 *arg++ = access_hook;
254 *arg++ = service->name;
256 *arg++ = STRARG(hostname);
257 *arg++ = STRARG(canon_hostname);
258 *arg++ = STRARG(ip_address);
259 *arg++ = STRARG(tcp_port);
268 if (start_command(&child)) {
269 logerror("daemon access hook '%s' failed to start",
273 if (strbuf_read(&buf, child.out, 0) < 0) {
274 logerror("failed to read from pipe to daemon access hook '%s'",
279 if (close(child.out) < 0) {
280 logerror("failed to close pipe to daemon access hook '%s'",
284 if (finish_command(&child))
288 strbuf_release(&buf);
295 strbuf_addstr(&buf, "service rejected");
296 eol = strchr(buf.buf, '\n');
300 daemon_error(dir, buf.buf);
301 strbuf_release(&buf);
305 static int run_service(const char *dir, struct daemon_service *service)
308 int enabled = service->enabled;
309 struct strbuf var = STRBUF_INIT;
311 loginfo("Request %s for '%s'", service->name, dir);
313 if (!enabled && !service->overridable) {
314 logerror("'%s': service not enabled.", service->name);
316 return daemon_error(dir, "service not enabled");
319 if (!(path = path_ok(dir)))
320 return daemon_error(dir, "no such repository");
323 * Security on the cheap.
325 * We want a readable HEAD, usable "objects" directory, and
326 * a "git-daemon-export-ok" flag that says that the other side
327 * is ok with us doing this.
329 * path_ok() uses enter_repo() and does whitelist checking.
330 * We only need to make sure the repository is exported.
333 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
334 logerror("'%s': repository not exported.", path);
336 return daemon_error(dir, "repository not exported");
339 if (service->overridable) {
340 strbuf_addf(&var, "daemon.%s", service->config_name);
341 git_config_get_bool(var.buf, &enabled);
342 strbuf_release(&var);
345 logerror("'%s': service not enabled for '%s'",
346 service->name, path);
348 return daemon_error(dir, "service not enabled");
352 * Optionally, a hook can choose to deny access to the
353 * repository depending on the phase of the moon.
355 if (access_hook && run_access_hook(service, dir, path))
359 * We'll ignore SIGTERM from now on, we have a
362 signal(SIGTERM, SIG_IGN);
364 return service->fn();
367 static void copy_to_log(int fd)
369 struct strbuf line = STRBUF_INIT;
372 fp = fdopen(fd, "r");
374 logerror("fdopen of error channel failed");
379 while (strbuf_getline(&line, fp, '\n') != EOF) {
380 logerror("%s", line.buf);
381 strbuf_setlen(&line, 0);
384 strbuf_release(&line);
388 static int run_service_command(const char **argv)
390 struct child_process cld = CHILD_PROCESS_INIT;
395 if (start_command(&cld))
401 copy_to_log(cld.err);
403 return finish_command(&cld);
406 static int upload_pack(void)
408 /* Timeout as string */
409 char timeout_buf[64];
410 const char *argv[] = { "upload-pack", "--strict", NULL, ".", NULL };
412 argv[2] = timeout_buf;
414 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
415 return run_service_command(argv);
418 static int upload_archive(void)
420 static const char *argv[] = { "upload-archive", ".", NULL };
421 return run_service_command(argv);
424 static int receive_pack(void)
426 static const char *argv[] = { "receive-pack", ".", NULL };
427 return run_service_command(argv);
430 static struct daemon_service daemon_service[] = {
431 { "upload-archive", "uploadarch", upload_archive, 0, 1 },
432 { "upload-pack", "uploadpack", upload_pack, 1, 1 },
433 { "receive-pack", "receivepack", receive_pack, 0, 1 },
436 static void enable_service(const char *name, int ena)
439 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
440 if (!strcmp(daemon_service[i].name, name)) {
441 daemon_service[i].enabled = ena;
445 die("No such service %s", name);
448 static void make_service_overridable(const char *name, int ena)
451 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
452 if (!strcmp(daemon_service[i].name, name)) {
453 daemon_service[i].overridable = ena;
457 die("No such service %s", name);
460 static void parse_host_and_port(char *hostport, char **host,
463 if (*hostport == '[') {
466 end = strchr(hostport, ']');
468 die("Invalid request ('[' without ']')");
470 *host = hostport + 1;
473 else if (end[1] == ':')
476 die("Garbage after end of host part");
479 *port = strrchr(hostport, ':');
488 * Read the host as supplied by the client connection.
490 static void parse_host_arg(char *extra_args, int buflen)
494 char *end = extra_args + buflen;
496 if (extra_args < end && *extra_args) {
497 saw_extended_args = 1;
498 if (strncasecmp("host=", extra_args, 5) == 0) {
499 val = extra_args + 5;
500 vallen = strlen(val) + 1;
502 /* Split <host>:<port> at colon. */
505 parse_host_and_port(val, &host, &port);
508 tcp_port = xstrdup(port);
511 hostname = xstrdup_tolower(host);
514 /* On to the next one */
515 extra_args = val + vallen;
517 if (extra_args < end && *extra_args)
518 die("Invalid request");
522 * Locate canonical hostname and its IP address.
526 struct addrinfo hints;
529 static char addrbuf[HOST_NAME_MAX + 1];
531 memset(&hints, 0, sizeof(hints));
532 hints.ai_flags = AI_CANONNAME;
534 gai = getaddrinfo(hostname, NULL, &hints, &ai);
536 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
538 inet_ntop(AF_INET, &sin_addr->sin_addr,
539 addrbuf, sizeof(addrbuf));
541 ip_address = xstrdup(addrbuf);
543 free(canon_hostname);
544 canon_hostname = xstrdup(ai->ai_canonname ?
545 ai->ai_canonname : ip_address);
550 struct hostent *hent;
551 struct sockaddr_in sa;
553 static char addrbuf[HOST_NAME_MAX + 1];
555 hent = gethostbyname(hostname);
557 ap = hent->h_addr_list;
558 memset(&sa, 0, sizeof sa);
559 sa.sin_family = hent->h_addrtype;
560 sa.sin_port = htons(0);
561 memcpy(&sa.sin_addr, *ap, hent->h_length);
563 inet_ntop(hent->h_addrtype, &sa.sin_addr,
564 addrbuf, sizeof(addrbuf));
566 free(canon_hostname);
567 canon_hostname = xstrdup(hent->h_name);
569 ip_address = xstrdup(addrbuf);
576 static int execute(void)
578 char *line = packet_buffer;
580 char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
583 loginfo("Connection from %s:%s", addr, port);
585 alarm(init_timeout ? init_timeout : timeout);
586 pktlen = packet_read(0, NULL, NULL, packet_buffer, sizeof(packet_buffer), 0);
591 loginfo("Extended attributes (%d bytes) exist <%.*s>",
593 (int) pktlen - len, line + len + 1);
594 if (len && line[len-1] == '\n') {
600 free(canon_hostname);
603 hostname = canon_hostname = ip_address = tcp_port = NULL;
606 parse_host_arg(line + len + 1, pktlen - len - 1);
608 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
609 struct daemon_service *s = &(daemon_service[i]);
612 if (skip_prefix(line, "git-", &arg) &&
613 skip_prefix(arg, s->name, &arg) &&
616 * Note: The directory here is probably context sensitive,
617 * and might depend on the actual service being performed.
619 return run_service(arg, s);
623 logerror("Protocol error: '%s'", line);
627 static int addrcmp(const struct sockaddr_storage *s1,
628 const struct sockaddr_storage *s2)
630 const struct sockaddr *sa1 = (const struct sockaddr*) s1;
631 const struct sockaddr *sa2 = (const struct sockaddr*) s2;
633 if (sa1->sa_family != sa2->sa_family)
634 return sa1->sa_family - sa2->sa_family;
635 if (sa1->sa_family == AF_INET)
636 return memcmp(&((struct sockaddr_in *)s1)->sin_addr,
637 &((struct sockaddr_in *)s2)->sin_addr,
638 sizeof(struct in_addr));
640 if (sa1->sa_family == AF_INET6)
641 return memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,
642 &((struct sockaddr_in6 *)s2)->sin6_addr,
643 sizeof(struct in6_addr));
648 static int max_connections = 32;
650 static unsigned int live_children;
652 static struct child {
654 struct child_process cld;
655 struct sockaddr_storage address;
658 static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen)
660 struct child *newborn, **cradle;
662 newborn = xcalloc(1, sizeof(*newborn));
664 memcpy(&newborn->cld, cld, sizeof(*cld));
665 memcpy(&newborn->address, addr, addrlen);
666 for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
667 if (!addrcmp(&(*cradle)->address, &newborn->address))
669 newborn->next = *cradle;
674 * This gets called if the number of connections grows
675 * past "max_connections".
677 * We kill the newest connection from a duplicate IP.
679 static void kill_some_child(void)
681 const struct child *blanket, *next;
683 if (!(blanket = firstborn))
686 for (; (next = blanket->next); blanket = next)
687 if (!addrcmp(&blanket->address, &next->address)) {
688 kill(blanket->cld.pid, SIGTERM);
693 static void check_dead_children(void)
698 struct child **cradle, *blanket;
699 for (cradle = &firstborn; (blanket = *cradle);)
700 if ((pid = waitpid(blanket->cld.pid, &status, WNOHANG)) > 1) {
701 const char *dead = "";
703 dead = " (with error)";
704 loginfo("[%"PRIuMAX"] Disconnected%s", (uintmax_t)pid, dead);
706 /* remove the child */
707 *cradle = blanket->next;
711 cradle = &blanket->next;
714 static char **cld_argv;
715 static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
717 struct child_process cld = CHILD_PROCESS_INIT;
718 char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
719 char *env[] = { addrbuf, portbuf, NULL };
721 if (max_connections && live_children >= max_connections) {
723 sleep(1); /* give it some time to die */
724 check_dead_children();
725 if (live_children >= max_connections) {
727 logerror("Too many children, dropping connection");
732 if (addr->sa_family == AF_INET) {
733 struct sockaddr_in *sin_addr = (void *) addr;
734 inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf + 12,
735 sizeof(addrbuf) - 12);
736 snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
737 ntohs(sin_addr->sin_port));
739 } else if (addr->sa_family == AF_INET6) {
740 struct sockaddr_in6 *sin6_addr = (void *) addr;
742 char *buf = addrbuf + 12;
743 *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
744 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf,
745 sizeof(addrbuf) - 13);
748 snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
749 ntohs(sin6_addr->sin6_port));
753 cld.env = (const char **)env;
754 cld.argv = (const char **)cld_argv;
756 cld.out = dup(incoming);
758 if (start_command(&cld))
759 logerror("unable to fork");
761 add_child(&cld, addr, addrlen);
764 static void child_handler(int signo)
767 * Otherwise empty handler because systemcalls will get interrupted
768 * upon signal receipt
769 * SysV needs the handler to be rearmed
771 signal(SIGCHLD, child_handler);
774 static int set_reuse_addr(int sockfd)
780 return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
790 static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
793 static char ip[INET_ADDRSTRLEN];
795 static char ip[INET6_ADDRSTRLEN];
801 inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len);
805 inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
808 strcpy(ip, "<unknown>");
815 static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
818 char pbuf[NI_MAXSERV];
819 struct addrinfo hints, *ai0, *ai;
823 sprintf(pbuf, "%d", listen_port);
824 memset(&hints, 0, sizeof(hints));
825 hints.ai_family = AF_UNSPEC;
826 hints.ai_socktype = SOCK_STREAM;
827 hints.ai_protocol = IPPROTO_TCP;
828 hints.ai_flags = AI_PASSIVE;
830 gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
832 logerror("getaddrinfo() for %s failed: %s", listen_addr, gai_strerror(gai));
836 for (ai = ai0; ai; ai = ai->ai_next) {
839 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
842 if (sockfd >= FD_SETSIZE) {
843 logerror("Socket descriptor too large");
849 if (ai->ai_family == AF_INET6) {
851 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
853 /* Note: error is not fatal */
857 if (set_reuse_addr(sockfd)) {
858 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
863 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
864 logerror("Could not bind to %s: %s",
865 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
868 continue; /* not fatal */
870 if (listen(sockfd, 5) < 0) {
871 logerror("Could not listen to %s: %s",
872 ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen),
875 continue; /* not fatal */
878 flags = fcntl(sockfd, F_GETFD, 0);
880 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
882 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
883 socklist->list[socklist->nr++] = sockfd;
894 static int setup_named_sock(char *listen_addr, int listen_port, struct socketlist *socklist)
896 struct sockaddr_in sin;
900 memset(&sin, 0, sizeof sin);
901 sin.sin_family = AF_INET;
902 sin.sin_port = htons(listen_port);
905 /* Well, host better be an IP address here. */
906 if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
909 sin.sin_addr.s_addr = htonl(INADDR_ANY);
912 sockfd = socket(AF_INET, SOCK_STREAM, 0);
916 if (set_reuse_addr(sockfd)) {
917 logerror("Could not set SO_REUSEADDR: %s", strerror(errno));
922 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
923 logerror("Could not bind to %s: %s",
924 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
930 if (listen(sockfd, 5) < 0) {
931 logerror("Could not listen to %s: %s",
932 ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)),
938 flags = fcntl(sockfd, F_GETFD, 0);
940 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
942 ALLOC_GROW(socklist->list, socklist->nr + 1, socklist->alloc);
943 socklist->list[socklist->nr++] = sockfd;
949 static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
951 if (!listen_addr->nr)
952 setup_named_sock(NULL, listen_port, socklist);
955 for (i = 0; i < listen_addr->nr; i++) {
956 socknum = setup_named_sock(listen_addr->items[i].string,
957 listen_port, socklist);
960 logerror("unable to allocate any listen sockets for host %s on port %u",
961 listen_addr->items[i].string, listen_port);
966 static int service_loop(struct socketlist *socklist)
971 pfd = xcalloc(socklist->nr, sizeof(struct pollfd));
973 for (i = 0; i < socklist->nr; i++) {
974 pfd[i].fd = socklist->list[i];
975 pfd[i].events = POLLIN;
978 signal(SIGCHLD, child_handler);
983 check_dead_children();
985 if (poll(pfd, socklist->nr, -1) < 0) {
986 if (errno != EINTR) {
987 logerror("Poll failed, resuming: %s",
994 for (i = 0; i < socklist->nr; i++) {
995 if (pfd[i].revents & POLLIN) {
998 struct sockaddr_in sai;
1000 struct sockaddr_in6 sai6;
1003 socklen_t sslen = sizeof(ss);
1004 int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
1012 die_errno("accept returned");
1015 handle(incoming, &ss.sa, sslen);
1021 #ifdef NO_POSIX_GOODIES
1025 static void drop_privileges(struct credentials *cred)
1030 static struct credentials *prepare_credentials(const char *user_name,
1031 const char *group_name)
1033 die("--user not supported on this platform");
1038 struct credentials {
1039 struct passwd *pass;
1043 static void drop_privileges(struct credentials *cred)
1045 if (cred && (initgroups(cred->pass->pw_name, cred->gid) ||
1046 setgid (cred->gid) || setuid(cred->pass->pw_uid)))
1047 die("cannot drop privileges");
1050 static struct credentials *prepare_credentials(const char *user_name,
1051 const char *group_name)
1053 static struct credentials c;
1055 c.pass = getpwnam(user_name);
1057 die("user not found - %s", user_name);
1060 c.gid = c.pass->pw_gid;
1062 struct group *group = getgrnam(group_name);
1064 die("group not found - %s", group_name);
1066 c.gid = group->gr_gid;
1073 static void store_pid(const char *path)
1075 FILE *f = fopen(path, "w");
1077 die_errno("cannot open pid file '%s'", path);
1078 if (fprintf(f, "%"PRIuMAX"\n", (uintmax_t) getpid()) < 0 || fclose(f) != 0)
1079 die_errno("failed to write pid file '%s'", path);
1082 static int serve(struct string_list *listen_addr, int listen_port,
1083 struct credentials *cred)
1085 struct socketlist socklist = { NULL, 0, 0 };
1087 socksetup(listen_addr, listen_port, &socklist);
1088 if (socklist.nr == 0)
1089 die("unable to allocate any listen sockets on port %u",
1092 drop_privileges(cred);
1094 loginfo("Ready to rumble");
1096 return service_loop(&socklist);
1099 int main(int argc, char **argv)
1101 int listen_port = 0;
1102 struct string_list listen_addr = STRING_LIST_INIT_NODUP;
1103 int serve_mode = 0, inetd_mode = 0;
1104 const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
1106 struct credentials *cred = NULL;
1109 git_setup_gettext();
1111 git_extract_argv0_path(argv[0]);
1113 for (i = 1; i < argc; i++) {
1114 char *arg = argv[i];
1117 if (skip_prefix(arg, "--listen=", &v)) {
1118 string_list_append(&listen_addr, xstrdup_tolower(v));
1121 if (skip_prefix(arg, "--port=", &v)) {
1124 n = strtoul(v, &end, 0);
1130 if (!strcmp(arg, "--serve")) {
1134 if (!strcmp(arg, "--inetd")) {
1139 if (!strcmp(arg, "--verbose")) {
1143 if (!strcmp(arg, "--syslog")) {
1147 if (!strcmp(arg, "--export-all")) {
1148 export_all_trees = 1;
1151 if (skip_prefix(arg, "--access-hook=", &v)) {
1155 if (skip_prefix(arg, "--timeout=", &v)) {
1159 if (skip_prefix(arg, "--init-timeout=", &v)) {
1160 init_timeout = atoi(v);
1163 if (skip_prefix(arg, "--max-connections=", &v)) {
1164 max_connections = atoi(v);
1165 if (max_connections < 0)
1166 max_connections = 0; /* unlimited */
1169 if (!strcmp(arg, "--strict-paths")) {
1173 if (skip_prefix(arg, "--base-path=", &v)) {
1177 if (!strcmp(arg, "--base-path-relaxed")) {
1178 base_path_relaxed = 1;
1181 if (skip_prefix(arg, "--interpolated-path=", &v)) {
1182 interpolated_path = v;
1185 if (!strcmp(arg, "--reuseaddr")) {
1189 if (!strcmp(arg, "--user-path")) {
1193 if (skip_prefix(arg, "--user-path=", &v)) {
1197 if (skip_prefix(arg, "--pid-file=", &v)) {
1201 if (!strcmp(arg, "--detach")) {
1206 if (skip_prefix(arg, "--user=", &v)) {
1210 if (skip_prefix(arg, "--group=", &v)) {
1214 if (skip_prefix(arg, "--enable=", &v)) {
1215 enable_service(v, 1);
1218 if (skip_prefix(arg, "--disable=", &v)) {
1219 enable_service(v, 0);
1222 if (skip_prefix(arg, "--allow-override=", &v)) {
1223 make_service_overridable(v, 1);
1226 if (skip_prefix(arg, "--forbid-override=", &v)) {
1227 make_service_overridable(v, 0);
1230 if (!strcmp(arg, "--informative-errors")) {
1231 informative_errors = 1;
1234 if (!strcmp(arg, "--no-informative-errors")) {
1235 informative_errors = 0;
1238 if (!strcmp(arg, "--")) {
1239 ok_paths = &argv[i+1];
1241 } else if (arg[0] != '-') {
1242 ok_paths = &argv[i];
1246 usage(daemon_usage);
1250 openlog("git-daemon", LOG_PID, LOG_DAEMON);
1251 set_die_routine(daemon_die);
1253 /* avoid splitting a message in the middle */
1254 setvbuf(stderr, NULL, _IOFBF, 4096);
1256 if (inetd_mode && (detach || group_name || user_name))
1257 die("--detach, --user and --group are incompatible with --inetd");
1259 if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
1260 die("--listen= and --port= are incompatible with --inetd");
1261 else if (listen_port == 0)
1262 listen_port = DEFAULT_GIT_PORT;
1264 if (group_name && !user_name)
1265 die("--group supplied without --user");
1268 cred = prepare_credentials(user_name, group_name);
1270 if (strict_paths && (!ok_paths || !*ok_paths))
1271 die("option --strict-paths requires a whitelist");
1273 if (base_path && !is_directory(base_path))
1274 die("base-path '%s' does not exist or is not a directory",
1278 if (!freopen("/dev/null", "w", stderr))
1279 die_errno("failed to redirect stderr to /dev/null");
1282 if (inetd_mode || serve_mode)
1287 die("--detach not supported on this platform");
1292 store_pid(pid_file);
1294 /* prepare argv for serving-processes */
1295 cld_argv = xmalloc(sizeof (char *) * (argc + 2));
1296 cld_argv[0] = argv[0]; /* git-daemon */
1297 cld_argv[1] = "--serve";
1298 for (i = 1; i < argc; ++i)
1299 cld_argv[i+1] = argv[i];
1300 cld_argv[argc+1] = NULL;
1302 return serve(&listen_addr, listen_port, cred);