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 int hostname_lookup_done
;
66 static void lookup_hostname(void);
68 static const char *get_canon_hostname(void)
71 return canon_hostname
;
74 static const char *get_ip_address(void)
80 static void logreport(int priority
, const char *err
, va_list params
)
84 vsnprintf(buf
, sizeof(buf
), err
, params
);
85 syslog(priority
, "%s", buf
);
88 * Since stderr is set to buffered mode, the
89 * logging of different processes will not overlap
90 * unless they overflow the (rather big) buffers.
92 fprintf(stderr
, "[%"PRIuMAX
"] ", (uintmax_t)getpid());
93 vfprintf(stderr
, err
, params
);
99 __attribute__((format (printf
, 1, 2)))
100 static void logerror(const char *err
, ...)
103 va_start(params
, err
);
104 logreport(LOG_ERR
, err
, params
);
108 __attribute__((format (printf
, 1, 2)))
109 static void loginfo(const char *err
, ...)
114 va_start(params
, err
);
115 logreport(LOG_INFO
, err
, params
);
119 static void NORETURN
daemon_die(const char *err
, va_list params
)
121 logreport(LOG_ERR
, err
, params
);
125 static void strbuf_addstr_or_null(struct strbuf
*sb
, const char *s
)
128 strbuf_addstr(sb
, s
);
131 struct expand_path_context
{
132 const char *directory
;
135 static size_t expand_path(struct strbuf
*sb
, const char *placeholder
, void *ctx
)
137 struct expand_path_context
*context
= ctx
;
139 switch (placeholder
[0]) {
141 strbuf_addstr_or_null(sb
, hostname
);
144 if (placeholder
[1] == 'H') {
145 strbuf_addstr_or_null(sb
, get_canon_hostname());
150 if (placeholder
[1] == 'P') {
151 strbuf_addstr_or_null(sb
, get_ip_address());
156 strbuf_addstr_or_null(sb
, tcp_port
);
159 strbuf_addstr(sb
, context
->directory
);
165 static const char *path_ok(const char *directory
)
167 static char rpath
[PATH_MAX
];
168 static char interp_path
[PATH_MAX
];
174 if (daemon_avoid_alias(dir
)) {
175 logerror("'%s': aliased", dir
);
181 logerror("'%s': User-path not allowed", dir
);
185 /* Got either "~alice" or "~alice/foo";
186 * rewrite them to "~alice/%s" or
189 int namlen
, restlen
= strlen(dir
);
190 const char *slash
= strchr(dir
, '/');
192 slash
= dir
+ restlen
;
193 namlen
= slash
- dir
;
195 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path
, dir
, namlen
, restlen
, slash
);
196 snprintf(rpath
, PATH_MAX
, "%.*s/%s%.*s",
197 namlen
, dir
, user_path
, restlen
, slash
);
201 else if (interpolated_path
&& saw_extended_args
) {
202 struct strbuf expanded_path
= STRBUF_INIT
;
203 struct expand_path_context context
;
205 context
.directory
= directory
;
208 /* Allow only absolute */
209 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir
);
213 strbuf_expand(&expanded_path
, interpolated_path
,
214 expand_path
, &context
);
215 strlcpy(interp_path
, expanded_path
.buf
, PATH_MAX
);
216 strbuf_release(&expanded_path
);
217 loginfo("Interpolated dir '%s'", interp_path
);
221 else if (base_path
) {
223 /* Allow only absolute */
224 logerror("'%s': Non-absolute path denied (base-path active)", dir
);
227 snprintf(rpath
, PATH_MAX
, "%s%s", base_path
, dir
);
231 path
= enter_repo(dir
, strict_paths
);
232 if (!path
&& base_path
&& base_path_relaxed
) {
234 * if we fail and base_path_relaxed is enabled, try without
235 * prefixing the base path
238 path
= enter_repo(dir
, strict_paths
);
242 logerror("'%s' does not appear to be a git repository", dir
);
246 if ( ok_paths
&& *ok_paths
) {
248 int pathlen
= strlen(path
);
250 /* The validation is done on the paths after enter_repo
251 * appends optional {.git,.git/.git} and friends, but
252 * it does not use getcwd(). So if your /pub is
253 * a symlink to /mnt/pub, you can whitelist /pub and
254 * do not have to say /mnt/pub.
257 for ( pp
= ok_paths
; *pp
; pp
++ ) {
258 int len
= strlen(*pp
);
259 if (len
<= pathlen
&&
260 !memcmp(*pp
, path
, len
) &&
261 (path
[len
] == '\0' ||
262 (!strict_paths
&& path
[len
] == '/')))
267 /* be backwards compatible */
272 logerror("'%s': not in whitelist", path
);
273 return NULL
; /* Fallthrough. Deny by default */
276 typedef int (*daemon_service_fn
)(void);
277 struct daemon_service
{
279 const char *config_name
;
280 daemon_service_fn fn
;
285 static struct daemon_service
*service_looking_at
;
286 static int service_enabled
;
288 static int git_daemon_config(const char *var
, const char *value
, void *cb
)
292 if (skip_prefix(var
, "daemon.", &service
) &&
293 !strcmp(service
, service_looking_at
->config_name
)) {
294 service_enabled
= git_config_bool(var
, value
);
298 /* we are not interested in parsing any other configuration here */
302 static int daemon_error(const char *dir
, const char *msg
)
304 if (!informative_errors
)
305 msg
= "access denied or repository not exported";
306 packet_write(1, "ERR %s: %s", msg
, dir
);
310 static const char *access_hook
;
312 static int run_access_hook(struct daemon_service
*service
, const char *dir
, const char *path
)
314 struct child_process child
;
315 struct strbuf buf
= STRBUF_INIT
;
317 const char **arg
= argv
;
321 #define STRARG(x) ((x) ? (x) : "")
322 *arg
++ = access_hook
;
323 *arg
++ = service
->name
;
325 *arg
++ = STRARG(hostname
);
326 *arg
++ = STRARG(get_canon_hostname());
327 *arg
++ = STRARG(get_ip_address());
328 *arg
++ = STRARG(tcp_port
);
332 memset(&child
, 0, sizeof(child
));
338 if (start_command(&child
)) {
339 logerror("daemon access hook '%s' failed to start",
343 if (strbuf_read(&buf
, child
.out
, 0) < 0) {
344 logerror("failed to read from pipe to daemon access hook '%s'",
349 if (close(child
.out
) < 0) {
350 logerror("failed to close pipe to daemon access hook '%s'",
354 if (finish_command(&child
))
358 strbuf_release(&buf
);
365 strbuf_addstr(&buf
, "service rejected");
366 eol
= strchr(buf
.buf
, '\n');
370 daemon_error(dir
, buf
.buf
);
371 strbuf_release(&buf
);
375 static int run_service(const char *dir
, struct daemon_service
*service
)
378 int enabled
= service
->enabled
;
380 loginfo("Request %s for '%s'", service
->name
, dir
);
382 if (!enabled
&& !service
->overridable
) {
383 logerror("'%s': service not enabled.", service
->name
);
385 return daemon_error(dir
, "service not enabled");
388 if (!(path
= path_ok(dir
)))
389 return daemon_error(dir
, "no such repository");
392 * Security on the cheap.
394 * We want a readable HEAD, usable "objects" directory, and
395 * a "git-daemon-export-ok" flag that says that the other side
396 * is ok with us doing this.
398 * path_ok() uses enter_repo() and does whitelist checking.
399 * We only need to make sure the repository is exported.
402 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
403 logerror("'%s': repository not exported.", path
);
405 return daemon_error(dir
, "repository not exported");
408 if (service
->overridable
) {
409 service_looking_at
= service
;
410 service_enabled
= -1;
411 git_config(git_daemon_config
, NULL
);
412 if (0 <= service_enabled
)
413 enabled
= service_enabled
;
416 logerror("'%s': service not enabled for '%s'",
417 service
->name
, path
);
419 return daemon_error(dir
, "service not enabled");
423 * Optionally, a hook can choose to deny access to the
424 * repository depending on the phase of the moon.
426 if (access_hook
&& run_access_hook(service
, dir
, path
))
430 * We'll ignore SIGTERM from now on, we have a
433 signal(SIGTERM
, SIG_IGN
);
435 return service
->fn();
438 static void copy_to_log(int fd
)
440 struct strbuf line
= STRBUF_INIT
;
443 fp
= fdopen(fd
, "r");
445 logerror("fdopen of error channel failed");
450 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
451 logerror("%s", line
.buf
);
452 strbuf_setlen(&line
, 0);
455 strbuf_release(&line
);
459 static int run_service_command(const char **argv
)
461 struct child_process cld
;
463 memset(&cld
, 0, sizeof(cld
));
467 if (start_command(&cld
))
473 copy_to_log(cld
.err
);
475 return finish_command(&cld
);
478 static int upload_pack(void)
480 /* Timeout as string */
481 char timeout_buf
[64];
482 const char *argv
[] = { "upload-pack", "--strict", NULL
, ".", NULL
};
484 argv
[2] = timeout_buf
;
486 snprintf(timeout_buf
, sizeof timeout_buf
, "--timeout=%u", timeout
);
487 return run_service_command(argv
);
490 static int upload_archive(void)
492 static const char *argv
[] = { "upload-archive", ".", NULL
};
493 return run_service_command(argv
);
496 static int receive_pack(void)
498 static const char *argv
[] = { "receive-pack", ".", NULL
};
499 return run_service_command(argv
);
502 static struct daemon_service daemon_service
[] = {
503 { "upload-archive", "uploadarch", upload_archive
, 0, 1 },
504 { "upload-pack", "uploadpack", upload_pack
, 1, 1 },
505 { "receive-pack", "receivepack", receive_pack
, 0, 1 },
508 static void enable_service(const char *name
, int ena
)
511 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
512 if (!strcmp(daemon_service
[i
].name
, name
)) {
513 daemon_service
[i
].enabled
= ena
;
517 die("No such service %s", name
);
520 static void make_service_overridable(const char *name
, int ena
)
523 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
524 if (!strcmp(daemon_service
[i
].name
, name
)) {
525 daemon_service
[i
].overridable
= ena
;
529 die("No such service %s", name
);
532 static void parse_host_and_port(char *hostport
, char **host
,
535 if (*hostport
== '[') {
538 end
= strchr(hostport
, ']');
540 die("Invalid request ('[' without ']')");
542 *host
= hostport
+ 1;
545 else if (end
[1] == ':')
548 die("Garbage after end of host part");
551 *port
= strrchr(hostport
, ':');
560 * Read the host as supplied by the client connection.
562 static void parse_host_arg(char *extra_args
, int buflen
)
566 char *end
= extra_args
+ buflen
;
568 if (extra_args
< end
&& *extra_args
) {
569 saw_extended_args
= 1;
570 if (strncasecmp("host=", extra_args
, 5) == 0) {
571 val
= extra_args
+ 5;
572 vallen
= strlen(val
) + 1;
574 /* Split <host>:<port> at colon. */
577 parse_host_and_port(val
, &host
, &port
);
580 tcp_port
= xstrdup(port
);
583 hostname
= xstrdup_tolower(host
);
584 hostname_lookup_done
= 0;
587 /* On to the next one */
588 extra_args
= val
+ vallen
;
590 if (extra_args
< end
&& *extra_args
)
591 die("Invalid request");
596 * Locate canonical hostname and its IP address.
598 static void lookup_hostname(void)
600 if (!hostname_lookup_done
&& hostname
) {
602 struct addrinfo hints
;
605 static char addrbuf
[HOST_NAME_MAX
+ 1];
607 memset(&hints
, 0, sizeof(hints
));
608 hints
.ai_flags
= AI_CANONNAME
;
610 gai
= getaddrinfo(hostname
, NULL
, &hints
, &ai
);
612 struct sockaddr_in
*sin_addr
= (void *)ai
->ai_addr
;
614 inet_ntop(AF_INET
, &sin_addr
->sin_addr
,
615 addrbuf
, sizeof(addrbuf
));
617 ip_address
= xstrdup(addrbuf
);
619 free(canon_hostname
);
620 canon_hostname
= xstrdup(ai
->ai_canonname
?
621 ai
->ai_canonname
: ip_address
);
626 struct hostent
*hent
;
627 struct sockaddr_in sa
;
629 static char addrbuf
[HOST_NAME_MAX
+ 1];
631 hent
= gethostbyname(hostname
);
633 ap
= hent
->h_addr_list
;
634 memset(&sa
, 0, sizeof sa
);
635 sa
.sin_family
= hent
->h_addrtype
;
636 sa
.sin_port
= htons(0);
637 memcpy(&sa
.sin_addr
, *ap
, hent
->h_length
);
639 inet_ntop(hent
->h_addrtype
, &sa
.sin_addr
,
640 addrbuf
, sizeof(addrbuf
));
642 free(canon_hostname
);
643 canon_hostname
= xstrdup(hent
->h_name
);
645 ip_address
= xstrdup(addrbuf
);
648 hostname_lookup_done
= 1;
653 static int execute(void)
655 char *line
= packet_buffer
;
657 char *addr
= getenv("REMOTE_ADDR"), *port
= getenv("REMOTE_PORT");
660 loginfo("Connection from %s:%s", addr
, port
);
662 alarm(init_timeout
? init_timeout
: timeout
);
663 pktlen
= packet_read(0, NULL
, NULL
, packet_buffer
, sizeof(packet_buffer
), 0);
668 loginfo("Extended attributes (%d bytes) exist <%.*s>",
670 (int) pktlen
- len
, line
+ len
+ 1);
671 if (len
&& line
[len
-1] == '\n') {
677 free(canon_hostname
);
680 hostname
= canon_hostname
= ip_address
= tcp_port
= NULL
;
683 parse_host_arg(line
+ len
+ 1, pktlen
- len
- 1);
685 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
686 struct daemon_service
*s
= &(daemon_service
[i
]);
689 if (skip_prefix(line
, "git-", &arg
) &&
690 skip_prefix(arg
, s
->name
, &arg
) &&
693 * Note: The directory here is probably context sensitive,
694 * and might depend on the actual service being performed.
696 return run_service(arg
, s
);
700 logerror("Protocol error: '%s'", line
);
704 static int addrcmp(const struct sockaddr_storage
*s1
,
705 const struct sockaddr_storage
*s2
)
707 const struct sockaddr
*sa1
= (const struct sockaddr
*) s1
;
708 const struct sockaddr
*sa2
= (const struct sockaddr
*) s2
;
710 if (sa1
->sa_family
!= sa2
->sa_family
)
711 return sa1
->sa_family
- sa2
->sa_family
;
712 if (sa1
->sa_family
== AF_INET
)
713 return memcmp(&((struct sockaddr_in
*)s1
)->sin_addr
,
714 &((struct sockaddr_in
*)s2
)->sin_addr
,
715 sizeof(struct in_addr
));
717 if (sa1
->sa_family
== AF_INET6
)
718 return memcmp(&((struct sockaddr_in6
*)s1
)->sin6_addr
,
719 &((struct sockaddr_in6
*)s2
)->sin6_addr
,
720 sizeof(struct in6_addr
));
725 static int max_connections
= 32;
727 static unsigned int live_children
;
729 static struct child
{
731 struct child_process cld
;
732 struct sockaddr_storage address
;
735 static void add_child(struct child_process
*cld
, struct sockaddr
*addr
, socklen_t addrlen
)
737 struct child
*newborn
, **cradle
;
739 newborn
= xcalloc(1, sizeof(*newborn
));
741 memcpy(&newborn
->cld
, cld
, sizeof(*cld
));
742 memcpy(&newborn
->address
, addr
, addrlen
);
743 for (cradle
= &firstborn
; *cradle
; cradle
= &(*cradle
)->next
)
744 if (!addrcmp(&(*cradle
)->address
, &newborn
->address
))
746 newborn
->next
= *cradle
;
751 * This gets called if the number of connections grows
752 * past "max_connections".
754 * We kill the newest connection from a duplicate IP.
756 static void kill_some_child(void)
758 const struct child
*blanket
, *next
;
760 if (!(blanket
= firstborn
))
763 for (; (next
= blanket
->next
); blanket
= next
)
764 if (!addrcmp(&blanket
->address
, &next
->address
)) {
765 kill(blanket
->cld
.pid
, SIGTERM
);
770 static void check_dead_children(void)
775 struct child
**cradle
, *blanket
;
776 for (cradle
= &firstborn
; (blanket
= *cradle
);)
777 if ((pid
= waitpid(blanket
->cld
.pid
, &status
, WNOHANG
)) > 1) {
778 const char *dead
= "";
780 dead
= " (with error)";
781 loginfo("[%"PRIuMAX
"] Disconnected%s", (uintmax_t)pid
, dead
);
783 /* remove the child */
784 *cradle
= blanket
->next
;
788 cradle
= &blanket
->next
;
791 static char **cld_argv
;
792 static void handle(int incoming
, struct sockaddr
*addr
, socklen_t addrlen
)
794 struct child_process cld
= { NULL
};
795 char addrbuf
[300] = "REMOTE_ADDR=", portbuf
[300];
796 char *env
[] = { addrbuf
, portbuf
, NULL
};
798 if (max_connections
&& live_children
>= max_connections
) {
800 sleep(1); /* give it some time to die */
801 check_dead_children();
802 if (live_children
>= max_connections
) {
804 logerror("Too many children, dropping connection");
809 if (addr
->sa_family
== AF_INET
) {
810 struct sockaddr_in
*sin_addr
= (void *) addr
;
811 inet_ntop(addr
->sa_family
, &sin_addr
->sin_addr
, addrbuf
+ 12,
812 sizeof(addrbuf
) - 12);
813 snprintf(portbuf
, sizeof(portbuf
), "REMOTE_PORT=%d",
814 ntohs(sin_addr
->sin_port
));
816 } else if (addr
->sa_family
== AF_INET6
) {
817 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
819 char *buf
= addrbuf
+ 12;
820 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
821 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
,
822 sizeof(addrbuf
) - 13);
825 snprintf(portbuf
, sizeof(portbuf
), "REMOTE_PORT=%d",
826 ntohs(sin6_addr
->sin6_port
));
830 cld
.env
= (const char **)env
;
831 cld
.argv
= (const char **)cld_argv
;
833 cld
.out
= dup(incoming
);
835 if (start_command(&cld
))
836 logerror("unable to fork");
838 add_child(&cld
, addr
, addrlen
);
841 static void child_handler(int signo
)
844 * Otherwise empty handler because systemcalls will get interrupted
845 * upon signal receipt
846 * SysV needs the handler to be rearmed
848 signal(SIGCHLD
, child_handler
);
851 static int set_reuse_addr(int sockfd
)
857 return setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
867 static const char *ip2str(int family
, struct sockaddr
*sin
, socklen_t len
)
870 static char ip
[INET_ADDRSTRLEN
];
872 static char ip
[INET6_ADDRSTRLEN
];
878 inet_ntop(family
, &((struct sockaddr_in6
*)sin
)->sin6_addr
, ip
, len
);
882 inet_ntop(family
, &((struct sockaddr_in
*)sin
)->sin_addr
, ip
, len
);
885 strcpy(ip
, "<unknown>");
892 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
895 char pbuf
[NI_MAXSERV
];
896 struct addrinfo hints
, *ai0
, *ai
;
900 sprintf(pbuf
, "%d", listen_port
);
901 memset(&hints
, 0, sizeof(hints
));
902 hints
.ai_family
= AF_UNSPEC
;
903 hints
.ai_socktype
= SOCK_STREAM
;
904 hints
.ai_protocol
= IPPROTO_TCP
;
905 hints
.ai_flags
= AI_PASSIVE
;
907 gai
= getaddrinfo(listen_addr
, pbuf
, &hints
, &ai0
);
909 logerror("getaddrinfo() for %s failed: %s", listen_addr
, gai_strerror(gai
));
913 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
916 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
919 if (sockfd
>= FD_SETSIZE
) {
920 logerror("Socket descriptor too large");
926 if (ai
->ai_family
== AF_INET6
) {
928 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
930 /* Note: error is not fatal */
934 if (set_reuse_addr(sockfd
)) {
935 logerror("Could not set SO_REUSEADDR: %s", strerror(errno
));
940 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
941 logerror("Could not bind to %s: %s",
942 ip2str(ai
->ai_family
, ai
->ai_addr
, ai
->ai_addrlen
),
945 continue; /* not fatal */
947 if (listen(sockfd
, 5) < 0) {
948 logerror("Could not listen to %s: %s",
949 ip2str(ai
->ai_family
, ai
->ai_addr
, ai
->ai_addrlen
),
952 continue; /* not fatal */
955 flags
= fcntl(sockfd
, F_GETFD
, 0);
957 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
959 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
960 socklist
->list
[socklist
->nr
++] = sockfd
;
971 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
973 struct sockaddr_in sin
;
977 memset(&sin
, 0, sizeof sin
);
978 sin
.sin_family
= AF_INET
;
979 sin
.sin_port
= htons(listen_port
);
982 /* Well, host better be an IP address here. */
983 if (inet_pton(AF_INET
, listen_addr
, &sin
.sin_addr
.s_addr
) <= 0)
986 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
989 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
993 if (set_reuse_addr(sockfd
)) {
994 logerror("Could not set SO_REUSEADDR: %s", strerror(errno
));
999 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
1000 logerror("Could not bind to %s: %s",
1001 ip2str(AF_INET
, (struct sockaddr
*)&sin
, sizeof(sin
)),
1007 if (listen(sockfd
, 5) < 0) {
1008 logerror("Could not listen to %s: %s",
1009 ip2str(AF_INET
, (struct sockaddr
*)&sin
, sizeof(sin
)),
1015 flags
= fcntl(sockfd
, F_GETFD
, 0);
1017 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
1019 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
1020 socklist
->list
[socklist
->nr
++] = sockfd
;
1026 static void socksetup(struct string_list
*listen_addr
, int listen_port
, struct socketlist
*socklist
)
1028 if (!listen_addr
->nr
)
1029 setup_named_sock(NULL
, listen_port
, socklist
);
1032 for (i
= 0; i
< listen_addr
->nr
; i
++) {
1033 socknum
= setup_named_sock(listen_addr
->items
[i
].string
,
1034 listen_port
, socklist
);
1037 logerror("unable to allocate any listen sockets for host %s on port %u",
1038 listen_addr
->items
[i
].string
, listen_port
);
1043 static int service_loop(struct socketlist
*socklist
)
1048 pfd
= xcalloc(socklist
->nr
, sizeof(struct pollfd
));
1050 for (i
= 0; i
< socklist
->nr
; i
++) {
1051 pfd
[i
].fd
= socklist
->list
[i
];
1052 pfd
[i
].events
= POLLIN
;
1055 signal(SIGCHLD
, child_handler
);
1060 check_dead_children();
1062 if (poll(pfd
, socklist
->nr
, -1) < 0) {
1063 if (errno
!= EINTR
) {
1064 logerror("Poll failed, resuming: %s",
1071 for (i
= 0; i
< socklist
->nr
; i
++) {
1072 if (pfd
[i
].revents
& POLLIN
) {
1075 struct sockaddr_in sai
;
1077 struct sockaddr_in6 sai6
;
1080 socklen_t sslen
= sizeof(ss
);
1081 int incoming
= accept(pfd
[i
].fd
, &ss
.sa
, &sslen
);
1089 die_errno("accept returned");
1092 handle(incoming
, &ss
.sa
, sslen
);
1098 #ifdef NO_POSIX_GOODIES
1102 static void drop_privileges(struct credentials
*cred
)
1107 static struct credentials
*prepare_credentials(const char *user_name
,
1108 const char *group_name
)
1110 die("--user not supported on this platform");
1115 struct credentials
{
1116 struct passwd
*pass
;
1120 static void drop_privileges(struct credentials
*cred
)
1122 if (cred
&& (initgroups(cred
->pass
->pw_name
, cred
->gid
) ||
1123 setgid (cred
->gid
) || setuid(cred
->pass
->pw_uid
)))
1124 die("cannot drop privileges");
1127 static struct credentials
*prepare_credentials(const char *user_name
,
1128 const char *group_name
)
1130 static struct credentials c
;
1132 c
.pass
= getpwnam(user_name
);
1134 die("user not found - %s", user_name
);
1137 c
.gid
= c
.pass
->pw_gid
;
1139 struct group
*group
= getgrnam(group_name
);
1141 die("group not found - %s", group_name
);
1143 c
.gid
= group
->gr_gid
;
1150 static void store_pid(const char *path
)
1152 FILE *f
= fopen(path
, "w");
1154 die_errno("cannot open pid file '%s'", path
);
1155 if (fprintf(f
, "%"PRIuMAX
"\n", (uintmax_t) getpid()) < 0 || fclose(f
) != 0)
1156 die_errno("failed to write pid file '%s'", path
);
1159 static int serve(struct string_list
*listen_addr
, int listen_port
,
1160 struct credentials
*cred
)
1162 struct socketlist socklist
= { NULL
, 0, 0 };
1164 socksetup(listen_addr
, listen_port
, &socklist
);
1165 if (socklist
.nr
== 0)
1166 die("unable to allocate any listen sockets on port %u",
1169 drop_privileges(cred
);
1171 loginfo("Ready to rumble");
1173 return service_loop(&socklist
);
1176 int main(int argc
, char **argv
)
1178 int listen_port
= 0;
1179 struct string_list listen_addr
= STRING_LIST_INIT_NODUP
;
1180 int serve_mode
= 0, inetd_mode
= 0;
1181 const char *pid_file
= NULL
, *user_name
= NULL
, *group_name
= NULL
;
1183 struct credentials
*cred
= NULL
;
1186 git_setup_gettext();
1188 git_extract_argv0_path(argv
[0]);
1190 for (i
= 1; i
< argc
; i
++) {
1191 char *arg
= argv
[i
];
1194 if (skip_prefix(arg
, "--listen=", &v
)) {
1195 string_list_append(&listen_addr
, xstrdup_tolower(v
));
1198 if (skip_prefix(arg
, "--port=", &v
)) {
1201 n
= strtoul(v
, &end
, 0);
1207 if (!strcmp(arg
, "--serve")) {
1211 if (!strcmp(arg
, "--inetd")) {
1216 if (!strcmp(arg
, "--verbose")) {
1220 if (!strcmp(arg
, "--syslog")) {
1224 if (!strcmp(arg
, "--export-all")) {
1225 export_all_trees
= 1;
1228 if (skip_prefix(arg
, "--access-hook=", &v
)) {
1232 if (skip_prefix(arg
, "--timeout=", &v
)) {
1236 if (skip_prefix(arg
, "--init-timeout=", &v
)) {
1237 init_timeout
= atoi(v
);
1240 if (skip_prefix(arg
, "--max-connections=", &v
)) {
1241 max_connections
= atoi(v
);
1242 if (max_connections
< 0)
1243 max_connections
= 0; /* unlimited */
1246 if (!strcmp(arg
, "--strict-paths")) {
1250 if (skip_prefix(arg
, "--base-path=", &v
)) {
1254 if (!strcmp(arg
, "--base-path-relaxed")) {
1255 base_path_relaxed
= 1;
1258 if (skip_prefix(arg
, "--interpolated-path=", &v
)) {
1259 interpolated_path
= v
;
1262 if (!strcmp(arg
, "--reuseaddr")) {
1266 if (!strcmp(arg
, "--user-path")) {
1270 if (skip_prefix(arg
, "--user-path=", &v
)) {
1274 if (skip_prefix(arg
, "--pid-file=", &v
)) {
1278 if (!strcmp(arg
, "--detach")) {
1283 if (skip_prefix(arg
, "--user=", &v
)) {
1287 if (skip_prefix(arg
, "--group=", &v
)) {
1291 if (skip_prefix(arg
, "--enable=", &v
)) {
1292 enable_service(v
, 1);
1295 if (skip_prefix(arg
, "--disable=", &v
)) {
1296 enable_service(v
, 0);
1299 if (skip_prefix(arg
, "--allow-override=", &v
)) {
1300 make_service_overridable(v
, 1);
1303 if (skip_prefix(arg
, "--forbid-override=", &v
)) {
1304 make_service_overridable(v
, 0);
1307 if (!strcmp(arg
, "--informative-errors")) {
1308 informative_errors
= 1;
1311 if (!strcmp(arg
, "--no-informative-errors")) {
1312 informative_errors
= 0;
1315 if (!strcmp(arg
, "--")) {
1316 ok_paths
= &argv
[i
+1];
1318 } else if (arg
[0] != '-') {
1319 ok_paths
= &argv
[i
];
1323 usage(daemon_usage
);
1327 openlog("git-daemon", LOG_PID
, LOG_DAEMON
);
1328 set_die_routine(daemon_die
);
1330 /* avoid splitting a message in the middle */
1331 setvbuf(stderr
, NULL
, _IOFBF
, 4096);
1333 if (inetd_mode
&& (detach
|| group_name
|| user_name
))
1334 die("--detach, --user and --group are incompatible with --inetd");
1336 if (inetd_mode
&& (listen_port
|| (listen_addr
.nr
> 0)))
1337 die("--listen= and --port= are incompatible with --inetd");
1338 else if (listen_port
== 0)
1339 listen_port
= DEFAULT_GIT_PORT
;
1341 if (group_name
&& !user_name
)
1342 die("--group supplied without --user");
1345 cred
= prepare_credentials(user_name
, group_name
);
1347 if (strict_paths
&& (!ok_paths
|| !*ok_paths
))
1348 die("option --strict-paths requires a whitelist");
1350 if (base_path
&& !is_directory(base_path
))
1351 die("base-path '%s' does not exist or is not a directory",
1355 if (!freopen("/dev/null", "w", stderr
))
1356 die_errno("failed to redirect stderr to /dev/null");
1359 if (inetd_mode
|| serve_mode
)
1364 die("--detach not supported on this platform");
1369 store_pid(pid_file
);
1371 /* prepare argv for serving-processes */
1372 cld_argv
= xmalloc(sizeof (char *) * (argc
+ 2));
1373 cld_argv
[0] = argv
[0]; /* git-daemon */
1374 cld_argv
[1] = "--serve";
1375 for (i
= 1; i
< argc
; ++i
)
1376 cld_argv
[i
+1] = argv
[i
];
1377 cld_argv
[argc
+1] = NULL
;
1379 return serve(&listen_addr
, listen_port
, cred
);