4 #include "interpolate.h"
9 #define HOST_NAME_MAX 256
16 static int log_syslog;
19 static int child_handler_pipe[2];
21 static const char daemon_usage[] =
22 "git daemon [--verbose] [--syslog] [--export-all]\n"
23 " [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
24 " [--base-path=path] [--base-path-relaxed]\n"
25 " [--user-path | --user-path=path]\n"
26 " [--interpolated-path=path]\n"
27 " [--reuseaddr] [--detach] [--pid-file=file]\n"
28 " [--[enable|disable|allow-override|forbid-override]=service]\n"
29 " [--inetd | [--listen=host_or_ipaddr] [--port=n]\n"
30 " [--user=user [--group=group]]\n"
33 /* List of acceptable pathname prefixes */
34 static char **ok_paths;
35 static int strict_paths;
37 /* If this is set, git-daemon-export-ok is not required */
38 static int export_all_trees;
40 /* Take all paths relative to this one if non-NULL */
41 static char *base_path;
42 static char *interpolated_path;
43 static int base_path_relaxed;
45 /* Flag indicating client sent extra args. */
46 static int saw_extended_args;
48 /* If defined, ~user notation is allowed and the string is inserted
49 * after ~user/. E.g. a request to git://host/~alice/frotz would
50 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
52 static const char *user_path;
54 /* Timeout, and initial timeout */
55 static unsigned int timeout;
56 static unsigned int init_timeout;
59 * Static table for now. Ugh.
60 * Feel free to make dynamic as needed.
62 #define INTERP_SLOT_HOST (0)
63 #define INTERP_SLOT_CANON_HOST (1)
64 #define INTERP_SLOT_IP (2)
65 #define INTERP_SLOT_PORT (3)
66 #define INTERP_SLOT_DIR (4)
67 #define INTERP_SLOT_PERCENT (5)
69 static struct interp interp_table[] = {
79 static void logreport(int priority, const char *err, va_list params)
83 vsnprintf(buf, sizeof(buf), err, params);
84 syslog(priority, "%s", buf);
87 /* Since stderr is set to linebuffered mode, the
88 * logging of different processes will not overlap
90 fprintf(stderr, "[%d] ", (int)getpid());
91 vfprintf(stderr, err, params);
96 static void logerror(const char *err, ...)
99 va_start(params, err);
100 logreport(LOG_ERR, err, params);
104 static void loginfo(const char *err, ...)
109 va_start(params, err);
110 logreport(LOG_INFO, err, params);
114 static void NORETURN daemon_die(const char *err, va_list params)
116 logreport(LOG_ERR, err, params);
120 static int avoid_alias(char *p)
125 * This resurrects the belts and suspenders paranoia check by HPA
126 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
127 * does not do getcwd() based path canonicalizations.
129 * sl becomes true immediately after seeing '/' and continues to
130 * be true as long as dots continue after that without intervening
133 if (!p || (*p != '/' && *p != '~'))
143 else if (ch == '/') {
145 /* reject //, /./ and /../ */
150 if (0 < ndot && ndot < 3)
151 /* reject /.$ and /..$ */
160 else if (ch == '/') {
167 static char *path_ok(struct interp *itable)
169 static char rpath[PATH_MAX];
170 static char interp_path[PATH_MAX];
171 int retried_path = 0;
175 dir = itable[INTERP_SLOT_DIR].value;
177 if (avoid_alias(dir)) {
178 logerror("'%s': aliased", dir);
184 logerror("'%s': User-path not allowed", dir);
188 /* Got either "~alice" or "~alice/foo";
189 * rewrite them to "~alice/%s" or
192 int namlen, restlen = strlen(dir);
193 char *slash = strchr(dir, '/');
195 slash = dir + restlen;
196 namlen = slash - dir;
198 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path, dir, namlen, restlen, slash);
199 snprintf(rpath, PATH_MAX, "%.*s/%s%.*s",
200 namlen, dir, user_path, restlen, slash);
204 else if (interpolated_path && saw_extended_args) {
206 /* Allow only absolute */
207 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
211 interpolate(interp_path, PATH_MAX, interpolated_path,
212 interp_table, ARRAY_SIZE(interp_table));
213 loginfo("Interpolated dir '%s'", interp_path);
217 else if (base_path) {
219 /* Allow only absolute */
220 logerror("'%s': Non-absolute path denied (base-path active)", dir);
223 snprintf(rpath, PATH_MAX, "%s%s", base_path, dir);
228 path = enter_repo(dir, strict_paths);
233 * if we fail and base_path_relaxed is enabled, try without
234 * prefixing the base path
236 if (base_path && base_path_relaxed && !retried_path) {
237 dir = itable[INTERP_SLOT_DIR].value;
245 logerror("'%s': unable to chdir or not a git archive", dir);
249 if ( ok_paths && *ok_paths ) {
251 int pathlen = strlen(path);
253 /* The validation is done on the paths after enter_repo
254 * appends optional {.git,.git/.git} and friends, but
255 * it does not use getcwd(). So if your /pub is
256 * a symlink to /mnt/pub, you can whitelist /pub and
257 * do not have to say /mnt/pub.
260 for ( pp = ok_paths ; *pp ; pp++ ) {
261 int len = strlen(*pp);
262 if (len <= pathlen &&
263 !memcmp(*pp, path, len) &&
264 (path[len] == '\0' ||
265 (!strict_paths && path[len] == '/')))
270 /* be backwards compatible */
275 logerror("'%s': not in whitelist", path);
276 return NULL; /* Fallthrough. Deny by default */
279 typedef int (*daemon_service_fn)(void);
280 struct daemon_service {
282 const char *config_name;
283 daemon_service_fn fn;
288 static struct daemon_service *service_looking_at;
289 static int service_enabled;
291 static int git_daemon_config(const char *var, const char *value, void *cb)
293 if (!prefixcmp(var, "daemon.") &&
294 !strcmp(var + 7, service_looking_at->config_name)) {
295 service_enabled = git_config_bool(var, value);
299 /* we are not interested in parsing any other configuration here */
303 static int run_service(struct interp *itable, struct daemon_service *service)
306 int enabled = service->enabled;
308 loginfo("Request %s for '%s'",
310 itable[INTERP_SLOT_DIR].value);
312 if (!enabled && !service->overridable) {
313 logerror("'%s': service not enabled.", service->name);
318 if (!(path = path_ok(itable)))
322 * Security on the cheap.
324 * We want a readable HEAD, usable "objects" directory, and
325 * a "git-daemon-export-ok" flag that says that the other side
326 * is ok with us doing this.
328 * path_ok() uses enter_repo() and does whitelist checking.
329 * We only need to make sure the repository is exported.
332 if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
333 logerror("'%s': repository not exported.", path);
338 if (service->overridable) {
339 service_looking_at = service;
340 service_enabled = -1;
341 git_config(git_daemon_config, NULL);
342 if (0 <= service_enabled)
343 enabled = service_enabled;
346 logerror("'%s': service not enabled for '%s'",
347 service->name, path);
353 * We'll ignore SIGTERM from now on, we have a
356 signal(SIGTERM, SIG_IGN);
358 return service->fn();
361 static int upload_pack(void)
363 /* Timeout as string */
364 char timeout_buf[64];
366 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
368 /* git-upload-pack only ever reads stuff, so this is safe */
369 execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL);
373 static int upload_archive(void)
375 execl_git_cmd("upload-archive", ".", NULL);
379 static int receive_pack(void)
381 execl_git_cmd("receive-pack", ".", NULL);
385 static struct daemon_service daemon_service[] = {
386 { "upload-archive", "uploadarch", upload_archive, 0, 1 },
387 { "upload-pack", "uploadpack", upload_pack, 1, 1 },
388 { "receive-pack", "receivepack", receive_pack, 0, 1 },
391 static void enable_service(const char *name, int ena)
394 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
395 if (!strcmp(daemon_service[i].name, name)) {
396 daemon_service[i].enabled = ena;
400 die("No such service %s", name);
403 static void make_service_overridable(const char *name, int ena)
406 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
407 if (!strcmp(daemon_service[i].name, name)) {
408 daemon_service[i].overridable = ena;
412 die("No such service %s", name);
416 * Separate the "extra args" information as supplied by the client connection.
417 * Any resulting data is squirreled away in the given interpolation table.
419 static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
423 char *end = extra_args + buflen;
425 while (extra_args < end && *extra_args) {
426 saw_extended_args = 1;
427 if (strncasecmp("host=", extra_args, 5) == 0) {
428 val = extra_args + 5;
429 vallen = strlen(val) + 1;
431 /* Split <host>:<port> at colon. */
433 char *port = strrchr(host, ':');
437 interp_set_entry(table, INTERP_SLOT_PORT, port);
439 interp_set_entry(table, INTERP_SLOT_HOST, host);
442 /* On to the next one */
443 extra_args = val + vallen;
448 static void fill_in_extra_table_entries(struct interp *itable)
453 * Replace literal host with lowercase-ized hostname.
455 hp = interp_table[INTERP_SLOT_HOST].value;
462 * Locate canonical hostname and its IP address.
466 struct addrinfo hints;
467 struct addrinfo *ai, *ai0;
469 static char addrbuf[HOST_NAME_MAX + 1];
471 memset(&hints, 0, sizeof(hints));
472 hints.ai_flags = AI_CANONNAME;
474 gai = getaddrinfo(interp_table[INTERP_SLOT_HOST].value, 0, &hints, &ai0);
476 for (ai = ai0; ai; ai = ai->ai_next) {
477 struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
479 inet_ntop(AF_INET, &sin_addr->sin_addr,
480 addrbuf, sizeof(addrbuf));
481 interp_set_entry(interp_table,
482 INTERP_SLOT_CANON_HOST, ai->ai_canonname);
483 interp_set_entry(interp_table,
484 INTERP_SLOT_IP, addrbuf);
492 struct hostent *hent;
493 struct sockaddr_in sa;
495 static char addrbuf[HOST_NAME_MAX + 1];
497 hent = gethostbyname(interp_table[INTERP_SLOT_HOST].value);
499 ap = hent->h_addr_list;
500 memset(&sa, 0, sizeof sa);
501 sa.sin_family = hent->h_addrtype;
502 sa.sin_port = htons(0);
503 memcpy(&sa.sin_addr, *ap, hent->h_length);
505 inet_ntop(hent->h_addrtype, &sa.sin_addr,
506 addrbuf, sizeof(addrbuf));
508 interp_set_entry(interp_table, INTERP_SLOT_CANON_HOST, hent->h_name);
509 interp_set_entry(interp_table, INTERP_SLOT_IP, addrbuf);
515 static int execute(struct sockaddr *addr)
517 static char line[1000];
521 char addrbuf[256] = "";
524 if (addr->sa_family == AF_INET) {
525 struct sockaddr_in *sin_addr = (void *) addr;
526 inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
527 port = ntohs(sin_addr->sin_port);
529 } else if (addr && addr->sa_family == AF_INET6) {
530 struct sockaddr_in6 *sin6_addr = (void *) addr;
533 *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
534 inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
537 port = ntohs(sin6_addr->sin6_port);
540 loginfo("Connection from %s:%d", addrbuf, port);
543 alarm(init_timeout ? init_timeout : timeout);
544 pktlen = packet_read_line(0, line, sizeof(line));
549 loginfo("Extended attributes (%d bytes) exist <%.*s>",
551 (int) pktlen - len, line + len + 1);
552 if (len && line[len-1] == '\n') {
558 * Initialize the path interpolation table for this connection.
560 interp_clear_table(interp_table, ARRAY_SIZE(interp_table));
561 interp_set_entry(interp_table, INTERP_SLOT_PERCENT, "%");
564 parse_extra_args(interp_table, line + len + 1, pktlen - len - 1);
565 fill_in_extra_table_entries(interp_table);
568 for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
569 struct daemon_service *s = &(daemon_service[i]);
570 int namelen = strlen(s->name);
571 if (!prefixcmp(line, "git-") &&
572 !strncmp(s->name, line + 4, namelen) &&
573 line[namelen + 4] == ' ') {
575 * Note: The directory here is probably context sensitive,
576 * and might depend on the actual service being performed.
578 interp_set_entry(interp_table,
579 INTERP_SLOT_DIR, line + namelen + 5);
580 return run_service(interp_table, s);
584 logerror("Protocol error: '%s'", line);
590 * We count spawned/reaped separately, just to avoid any
591 * races when updating them from signals. The SIGCHLD handler
592 * will only update children_reaped, and the fork logic will
593 * only update children_spawned.
595 * MAX_CHILDREN should be a power-of-two to make the modulus
596 * operation cheap. It should also be at least twice
597 * the maximum number of connections we will ever allow.
599 #define MAX_CHILDREN 128
601 static int max_connections = 25;
603 /* These are updated by the signal handler */
604 static volatile unsigned int children_reaped;
605 static pid_t dead_child[MAX_CHILDREN];
607 /* These are updated by the main loop */
608 static unsigned int children_spawned;
609 static unsigned int children_deleted;
611 static struct child {
614 struct sockaddr_storage address;
615 } live_child[MAX_CHILDREN];
617 static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen)
619 live_child[idx].pid = pid;
620 live_child[idx].addrlen = addrlen;
621 memcpy(&live_child[idx].address, addr, addrlen);
625 * Walk from "deleted" to "spawned", and remove child "pid".
627 * We move everything up by one, since the new "deleted" will
630 static void remove_child(pid_t pid, unsigned deleted, unsigned spawned)
634 deleted %= MAX_CHILDREN;
635 spawned %= MAX_CHILDREN;
636 if (live_child[deleted].pid == pid) {
637 live_child[deleted].pid = -1;
640 n = live_child[deleted];
643 deleted = (deleted + 1) % MAX_CHILDREN;
644 if (deleted == spawned)
645 die("could not find dead child %d\n", pid);
646 m = live_child[deleted];
647 live_child[deleted] = n;
655 * This gets called if the number of connections grows
656 * past "max_connections".
658 * We _should_ start off by searching for connections
659 * from the same IP, and if there is some address wth
660 * multiple connections, we should kill that first.
662 * As it is, we just "randomly" kill 25% of the connections,
663 * and our pseudo-random generator sucks too. I have no
666 * Really, this is just a place-holder for a _real_ algorithm.
668 static void kill_some_children(int signo, unsigned start, unsigned stop)
670 start %= MAX_CHILDREN;
671 stop %= MAX_CHILDREN;
672 while (start != stop) {
674 kill(live_child[start].pid, signo);
675 start = (start + 1) % MAX_CHILDREN;
679 static void check_dead_children(void)
681 unsigned spawned, reaped, deleted;
683 spawned = children_spawned;
684 reaped = children_reaped;
685 deleted = children_deleted;
687 while (deleted < reaped) {
688 pid_t pid = dead_child[deleted % MAX_CHILDREN];
689 const char *dead = pid < 0 ? " (with error)" : "";
694 /* XXX: Custom logging, since we don't wanna getpid() */
697 syslog(LOG_INFO, "[%d] Disconnected%s",
700 fprintf(stderr, "[%d] Disconnected%s\n",
703 remove_child(pid, deleted, spawned);
706 children_deleted = deleted;
709 static void check_max_connections(void)
713 unsigned spawned, deleted;
715 check_dead_children();
717 spawned = children_spawned;
718 deleted = children_deleted;
720 active = spawned - deleted;
721 if (active <= max_connections)
724 /* Kill some unstarted connections with SIGTERM */
725 kill_some_children(SIGTERM, deleted, spawned);
726 if (active <= max_connections << 1)
729 /* If the SIGTERM thing isn't helping use SIGKILL */
730 kill_some_children(SIGKILL, deleted, spawned);
735 static void handle(int incoming, struct sockaddr *addr, int addrlen)
746 idx = children_spawned % MAX_CHILDREN;
748 add_child(idx, pid, addr, addrlen);
750 check_max_connections();
761 static void child_handler(int signo)
765 pid_t pid = waitpid(-1, &status, WNOHANG);
768 unsigned reaped = children_reaped;
769 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
771 dead_child[reaped % MAX_CHILDREN] = pid;
772 children_reaped = reaped + 1;
773 write(child_handler_pipe[1], &status, 1);
778 signal(SIGCHLD, child_handler);
781 static int set_reuse_addr(int sockfd)
787 return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
793 static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
795 int socknum = 0, *socklist = NULL;
797 char pbuf[NI_MAXSERV];
798 struct addrinfo hints, *ai0, *ai;
802 sprintf(pbuf, "%d", listen_port);
803 memset(&hints, 0, sizeof(hints));
804 hints.ai_family = AF_UNSPEC;
805 hints.ai_socktype = SOCK_STREAM;
806 hints.ai_protocol = IPPROTO_TCP;
807 hints.ai_flags = AI_PASSIVE;
809 gai = getaddrinfo(listen_addr, pbuf, &hints, &ai0);
811 die("getaddrinfo() failed: %s\n", gai_strerror(gai));
813 for (ai = ai0; ai; ai = ai->ai_next) {
816 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
819 if (sockfd >= FD_SETSIZE) {
820 logerror("Socket descriptor too large");
826 if (ai->ai_family == AF_INET6) {
828 setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
830 /* Note: error is not fatal */
834 if (set_reuse_addr(sockfd)) {
839 if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
841 continue; /* not fatal */
843 if (listen(sockfd, 5) < 0) {
845 continue; /* not fatal */
848 flags = fcntl(sockfd, F_GETFD, 0);
850 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
852 socklist = xrealloc(socklist, sizeof(int) * (socknum + 1));
853 socklist[socknum++] = sockfd;
861 *socklist_p = socklist;
867 static int socksetup(char *listen_addr, int listen_port, int **socklist_p)
869 struct sockaddr_in sin;
873 memset(&sin, 0, sizeof sin);
874 sin.sin_family = AF_INET;
875 sin.sin_port = htons(listen_port);
878 /* Well, host better be an IP address here. */
879 if (inet_pton(AF_INET, listen_addr, &sin.sin_addr.s_addr) <= 0)
882 sin.sin_addr.s_addr = htonl(INADDR_ANY);
885 sockfd = socket(AF_INET, SOCK_STREAM, 0);
889 if (set_reuse_addr(sockfd)) {
894 if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) {
899 if (listen(sockfd, 5) < 0) {
904 flags = fcntl(sockfd, F_GETFD, 0);
906 fcntl(sockfd, F_SETFD, flags | FD_CLOEXEC);
908 *socklist_p = xmalloc(sizeof(int));
909 **socklist_p = sockfd;
915 static int service_loop(int socknum, int *socklist)
920 if (pipe(child_handler_pipe) < 0)
921 die ("Could not set up pipe for child handler");
923 pfd = xcalloc(socknum + 1, sizeof(struct pollfd));
925 for (i = 0; i < socknum; i++) {
926 pfd[i].fd = socklist[i];
927 pfd[i].events = POLLIN;
929 pfd[socknum].fd = child_handler_pipe[0];
930 pfd[socknum].events = POLLIN;
932 signal(SIGCHLD, child_handler);
937 if (poll(pfd, socknum + 1, -1) < 0) {
938 if (errno != EINTR) {
939 logerror("Poll failed, resuming: %s",
945 if (pfd[socknum].revents & POLLIN) {
946 read(child_handler_pipe[0], &i, 1);
947 check_dead_children();
950 for (i = 0; i < socknum; i++) {
951 if (pfd[i].revents & POLLIN) {
952 struct sockaddr_storage ss;
953 unsigned int sslen = sizeof(ss);
954 int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
962 die("accept returned %s", strerror(errno));
965 handle(incoming, (struct sockaddr *)&ss, sslen);
971 /* if any standard file descriptor is missing open it to /dev/null */
972 static void sanitize_stdfds(void)
974 int fd = open("/dev/null", O_RDWR, 0);
975 while (fd != -1 && fd < 2)
978 die("open /dev/null or dup failed: %s", strerror(errno));
983 static void daemonize(void)
989 die("fork failed: %s", strerror(errno));
994 die("setsid failed: %s", strerror(errno));
1001 static void store_pid(const char *path)
1003 FILE *f = fopen(path, "w");
1005 die("cannot open pid file %s: %s", path, strerror(errno));
1006 if (fprintf(f, "%d\n", getpid()) < 0 || fclose(f) != 0)
1007 die("failed to write pid file %s: %s", path, strerror(errno));
1010 static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
1012 int socknum, *socklist;
1014 socknum = socksetup(listen_addr, listen_port, &socklist);
1016 die("unable to allocate any listen sockets on host %s port %u",
1017 listen_addr, listen_port);
1020 (initgroups(pass->pw_name, gid) || setgid (gid) ||
1021 setuid(pass->pw_uid)))
1022 die("cannot drop privileges");
1024 return service_loop(socknum, socklist);
1027 int main(int argc, char **argv)
1029 int listen_port = 0;
1030 char *listen_addr = NULL;
1032 const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
1034 struct passwd *pass = NULL;
1035 struct group *group;
1039 /* Without this we cannot rely on waitpid() to tell
1040 * what happened to our children.
1042 signal(SIGCHLD, SIG_DFL);
1044 for (i = 1; i < argc; i++) {
1045 char *arg = argv[i];
1047 if (!prefixcmp(arg, "--listen=")) {
1049 char *ph = listen_addr = xmalloc(strlen(arg + 9) + 1);
1051 *ph++ = tolower(*p++);
1055 if (!prefixcmp(arg, "--port=")) {
1058 n = strtoul(arg+7, &end, 0);
1059 if (arg[7] && !*end) {
1064 if (!strcmp(arg, "--inetd")) {
1069 if (!strcmp(arg, "--verbose")) {
1073 if (!strcmp(arg, "--syslog")) {
1077 if (!strcmp(arg, "--export-all")) {
1078 export_all_trees = 1;
1081 if (!prefixcmp(arg, "--timeout=")) {
1082 timeout = atoi(arg+10);
1085 if (!prefixcmp(arg, "--init-timeout=")) {
1086 init_timeout = atoi(arg+15);
1089 if (!strcmp(arg, "--strict-paths")) {
1093 if (!prefixcmp(arg, "--base-path=")) {
1097 if (!strcmp(arg, "--base-path-relaxed")) {
1098 base_path_relaxed = 1;
1101 if (!prefixcmp(arg, "--interpolated-path=")) {
1102 interpolated_path = arg+20;
1105 if (!strcmp(arg, "--reuseaddr")) {
1109 if (!strcmp(arg, "--user-path")) {
1113 if (!prefixcmp(arg, "--user-path=")) {
1114 user_path = arg + 12;
1117 if (!prefixcmp(arg, "--pid-file=")) {
1118 pid_file = arg + 11;
1121 if (!strcmp(arg, "--detach")) {
1126 if (!prefixcmp(arg, "--user=")) {
1127 user_name = arg + 7;
1130 if (!prefixcmp(arg, "--group=")) {
1131 group_name = arg + 8;
1134 if (!prefixcmp(arg, "--enable=")) {
1135 enable_service(arg + 9, 1);
1138 if (!prefixcmp(arg, "--disable=")) {
1139 enable_service(arg + 10, 0);
1142 if (!prefixcmp(arg, "--allow-override=")) {
1143 make_service_overridable(arg + 17, 1);
1146 if (!prefixcmp(arg, "--forbid-override=")) {
1147 make_service_overridable(arg + 18, 0);
1150 if (!strcmp(arg, "--")) {
1151 ok_paths = &argv[i+1];
1153 } else if (arg[0] != '-') {
1154 ok_paths = &argv[i];
1158 usage(daemon_usage);
1162 openlog("git-daemon", LOG_PID, LOG_DAEMON);
1163 set_die_routine(daemon_die);
1166 setlinebuf(stderr); /* avoid splitting a message in the middle */
1168 if (inetd_mode && (group_name || user_name))
1169 die("--user and --group are incompatible with --inetd");
1171 if (inetd_mode && (listen_port || listen_addr))
1172 die("--listen= and --port= are incompatible with --inetd");
1173 else if (listen_port == 0)
1174 listen_port = DEFAULT_GIT_PORT;
1176 if (group_name && !user_name)
1177 die("--group supplied without --user");
1180 pass = getpwnam(user_name);
1182 die("user not found - %s", user_name);
1187 group = getgrnam(group_name);
1189 die("group not found - %s", group_name);
1191 gid = group->gr_gid;
1195 if (strict_paths && (!ok_paths || !*ok_paths))
1196 die("option --strict-paths requires a whitelist");
1201 if (stat(base_path, &st) || !S_ISDIR(st.st_mode))
1202 die("base-path '%s' does not exist or "
1203 "is not a directory", base_path);
1207 struct sockaddr_storage ss;
1208 struct sockaddr *peer = (struct sockaddr *)&ss;
1209 socklen_t slen = sizeof(ss);
1211 freopen("/dev/null", "w", stderr);
1213 if (getpeername(0, peer, &slen))
1216 return execute(peer);
1221 loginfo("Ready to rumble");
1227 store_pid(pid_file);
1229 return serve(listen_addr, listen_port, pass, gid);