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 char *base_path
;
43 static 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(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 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 struct daemon_service
*service_looking_at
;
234 static int service_enabled
;
236 static int git_daemon_config(const char *var
, const char *value
, void *cb
)
238 if (!prefixcmp(var
, "daemon.") &&
239 !strcmp(var
+ 7, service_looking_at
->config_name
)) {
240 service_enabled
= git_config_bool(var
, value
);
244 /* we are not interested in parsing any other configuration here */
248 static int daemon_error(const char *dir
, const char *msg
)
250 if (!informative_errors
)
251 msg
= "access denied or repository not exported";
252 packet_write(1, "ERR %s: %s", msg
, dir
);
256 static char *access_hook
;
258 static int run_access_hook(struct daemon_service
*service
, const char *dir
, const char *path
)
260 struct child_process child
;
261 struct strbuf buf
= STRBUF_INIT
;
263 const char **arg
= argv
;
267 #define STRARG(x) ((x) ? (x) : "")
268 *arg
++ = access_hook
;
269 *arg
++ = service
->name
;
271 *arg
++ = STRARG(hostname
);
272 *arg
++ = STRARG(canon_hostname
);
273 *arg
++ = STRARG(ip_address
);
274 *arg
++ = STRARG(tcp_port
);
278 memset(&child
, 0, sizeof(child
));
284 if (start_command(&child
)) {
285 logerror("daemon access hook '%s' failed to start",
289 if (strbuf_read(&buf
, child
.out
, 0) < 0) {
290 logerror("failed to read from pipe to daemon access hook '%s'",
295 if (close(child
.out
) < 0) {
296 logerror("failed to close pipe to daemon access hook '%s'",
300 if (finish_command(&child
))
304 strbuf_release(&buf
);
311 strbuf_addstr(&buf
, "service rejected");
312 eol
= strchr(buf
.buf
, '\n');
316 daemon_error(dir
, buf
.buf
);
317 strbuf_release(&buf
);
321 static int run_service(char *dir
, struct daemon_service
*service
)
324 int enabled
= service
->enabled
;
326 loginfo("Request %s for '%s'", service
->name
, dir
);
328 if (!enabled
&& !service
->overridable
) {
329 logerror("'%s': service not enabled.", service
->name
);
331 return daemon_error(dir
, "service not enabled");
334 if (!(path
= path_ok(dir
)))
335 return daemon_error(dir
, "no such repository");
338 * Security on the cheap.
340 * We want a readable HEAD, usable "objects" directory, and
341 * a "git-daemon-export-ok" flag that says that the other side
342 * is ok with us doing this.
344 * path_ok() uses enter_repo() and does whitelist checking.
345 * We only need to make sure the repository is exported.
348 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
349 logerror("'%s': repository not exported.", path
);
351 return daemon_error(dir
, "repository not exported");
354 if (service
->overridable
) {
355 service_looking_at
= service
;
356 service_enabled
= -1;
357 git_config(git_daemon_config
, NULL
);
358 if (0 <= service_enabled
)
359 enabled
= service_enabled
;
362 logerror("'%s': service not enabled for '%s'",
363 service
->name
, path
);
365 return daemon_error(dir
, "service not enabled");
369 * Optionally, a hook can choose to deny access to the
370 * repository depending on the phase of the moon.
372 if (access_hook
&& run_access_hook(service
, dir
, path
))
376 * We'll ignore SIGTERM from now on, we have a
379 signal(SIGTERM
, SIG_IGN
);
381 return service
->fn();
384 static void copy_to_log(int fd
)
386 struct strbuf line
= STRBUF_INIT
;
389 fp
= fdopen(fd
, "r");
391 logerror("fdopen of error channel failed");
396 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
397 logerror("%s", line
.buf
);
398 strbuf_setlen(&line
, 0);
401 strbuf_release(&line
);
405 static int run_service_command(const char **argv
)
407 struct child_process cld
;
409 memset(&cld
, 0, sizeof(cld
));
413 if (start_command(&cld
))
419 copy_to_log(cld
.err
);
421 return finish_command(&cld
);
424 static int upload_pack(void)
426 /* Timeout as string */
427 char timeout_buf
[64];
428 const char *argv
[] = { "upload-pack", "--strict", NULL
, ".", NULL
};
430 argv
[2] = timeout_buf
;
432 snprintf(timeout_buf
, sizeof timeout_buf
, "--timeout=%u", timeout
);
433 return run_service_command(argv
);
436 static int upload_archive(void)
438 static const char *argv
[] = { "upload-archive", ".", NULL
};
439 return run_service_command(argv
);
442 static int receive_pack(void)
444 static const char *argv
[] = { "receive-pack", ".", NULL
};
445 return run_service_command(argv
);
448 static struct daemon_service daemon_service
[] = {
449 { "upload-archive", "uploadarch", upload_archive
, 0, 1 },
450 { "upload-pack", "uploadpack", upload_pack
, 1, 1 },
451 { "receive-pack", "receivepack", receive_pack
, 0, 1 },
454 static void enable_service(const char *name
, int ena
)
457 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
458 if (!strcmp(daemon_service
[i
].name
, name
)) {
459 daemon_service
[i
].enabled
= ena
;
463 die("No such service %s", name
);
466 static void make_service_overridable(const char *name
, int ena
)
469 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
470 if (!strcmp(daemon_service
[i
].name
, name
)) {
471 daemon_service
[i
].overridable
= ena
;
475 die("No such service %s", name
);
478 static char *xstrdup_tolower(const char *str
)
480 char *p
, *dup
= xstrdup(str
);
481 for (p
= dup
; *p
; p
++)
486 static void parse_host_and_port(char *hostport
, char **host
,
489 if (*hostport
== '[') {
492 end
= strchr(hostport
, ']');
494 die("Invalid request ('[' without ']')");
496 *host
= hostport
+ 1;
499 else if (end
[1] == ':')
502 die("Garbage after end of host part");
505 *port
= strrchr(hostport
, ':');
514 * Read the host as supplied by the client connection.
516 static void parse_host_arg(char *extra_args
, int buflen
)
520 char *end
= extra_args
+ buflen
;
522 if (extra_args
< end
&& *extra_args
) {
523 saw_extended_args
= 1;
524 if (strncasecmp("host=", extra_args
, 5) == 0) {
525 val
= extra_args
+ 5;
526 vallen
= strlen(val
) + 1;
528 /* Split <host>:<port> at colon. */
531 parse_host_and_port(val
, &host
, &port
);
534 tcp_port
= xstrdup(port
);
537 hostname
= xstrdup_tolower(host
);
540 /* On to the next one */
541 extra_args
= val
+ vallen
;
543 if (extra_args
< end
&& *extra_args
)
544 die("Invalid request");
548 * Locate canonical hostname and its IP address.
552 struct addrinfo hints
;
555 static char addrbuf
[HOST_NAME_MAX
+ 1];
557 memset(&hints
, 0, sizeof(hints
));
558 hints
.ai_flags
= AI_CANONNAME
;
560 gai
= getaddrinfo(hostname
, NULL
, &hints
, &ai
);
562 struct sockaddr_in
*sin_addr
= (void *)ai
->ai_addr
;
564 inet_ntop(AF_INET
, &sin_addr
->sin_addr
,
565 addrbuf
, sizeof(addrbuf
));
567 ip_address
= xstrdup(addrbuf
);
569 free(canon_hostname
);
570 canon_hostname
= xstrdup(ai
->ai_canonname
?
571 ai
->ai_canonname
: ip_address
);
576 struct hostent
*hent
;
577 struct sockaddr_in sa
;
579 static char addrbuf
[HOST_NAME_MAX
+ 1];
581 hent
= gethostbyname(hostname
);
583 ap
= hent
->h_addr_list
;
584 memset(&sa
, 0, sizeof sa
);
585 sa
.sin_family
= hent
->h_addrtype
;
586 sa
.sin_port
= htons(0);
587 memcpy(&sa
.sin_addr
, *ap
, hent
->h_length
);
589 inet_ntop(hent
->h_addrtype
, &sa
.sin_addr
,
590 addrbuf
, sizeof(addrbuf
));
592 free(canon_hostname
);
593 canon_hostname
= xstrdup(hent
->h_name
);
595 ip_address
= xstrdup(addrbuf
);
601 static int execute(void)
603 static char line
[1000];
605 char *addr
= getenv("REMOTE_ADDR"), *port
= getenv("REMOTE_PORT");
608 loginfo("Connection from %s:%s", addr
, port
);
610 alarm(init_timeout
? init_timeout
: timeout
);
611 pktlen
= packet_read_line(0, line
, sizeof(line
));
616 loginfo("Extended attributes (%d bytes) exist <%.*s>",
618 (int) pktlen
- len
, line
+ len
+ 1);
619 if (len
&& line
[len
-1] == '\n') {
625 free(canon_hostname
);
628 hostname
= canon_hostname
= ip_address
= tcp_port
= NULL
;
631 parse_host_arg(line
+ len
+ 1, pktlen
- len
- 1);
633 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
634 struct daemon_service
*s
= &(daemon_service
[i
]);
635 int namelen
= strlen(s
->name
);
636 if (!prefixcmp(line
, "git-") &&
637 !strncmp(s
->name
, line
+ 4, namelen
) &&
638 line
[namelen
+ 4] == ' ') {
640 * Note: The directory here is probably context sensitive,
641 * and might depend on the actual service being performed.
643 return run_service(line
+ namelen
+ 5, s
);
647 logerror("Protocol error: '%s'", line
);
651 static int addrcmp(const struct sockaddr_storage
*s1
,
652 const struct sockaddr_storage
*s2
)
654 const struct sockaddr
*sa1
= (const struct sockaddr
*) s1
;
655 const struct sockaddr
*sa2
= (const struct sockaddr
*) s2
;
657 if (sa1
->sa_family
!= sa2
->sa_family
)
658 return sa1
->sa_family
- sa2
->sa_family
;
659 if (sa1
->sa_family
== AF_INET
)
660 return memcmp(&((struct sockaddr_in
*)s1
)->sin_addr
,
661 &((struct sockaddr_in
*)s2
)->sin_addr
,
662 sizeof(struct in_addr
));
664 if (sa1
->sa_family
== AF_INET6
)
665 return memcmp(&((struct sockaddr_in6
*)s1
)->sin6_addr
,
666 &((struct sockaddr_in6
*)s2
)->sin6_addr
,
667 sizeof(struct in6_addr
));
672 static int max_connections
= 32;
674 static unsigned int live_children
;
676 static struct child
{
678 struct child_process cld
;
679 struct sockaddr_storage address
;
682 static void add_child(struct child_process
*cld
, struct sockaddr
*addr
, socklen_t addrlen
)
684 struct child
*newborn
, **cradle
;
686 newborn
= xcalloc(1, sizeof(*newborn
));
688 memcpy(&newborn
->cld
, cld
, sizeof(*cld
));
689 memcpy(&newborn
->address
, addr
, addrlen
);
690 for (cradle
= &firstborn
; *cradle
; cradle
= &(*cradle
)->next
)
691 if (!addrcmp(&(*cradle
)->address
, &newborn
->address
))
693 newborn
->next
= *cradle
;
698 * This gets called if the number of connections grows
699 * past "max_connections".
701 * We kill the newest connection from a duplicate IP.
703 static void kill_some_child(void)
705 const struct child
*blanket
, *next
;
707 if (!(blanket
= firstborn
))
710 for (; (next
= blanket
->next
); blanket
= next
)
711 if (!addrcmp(&blanket
->address
, &next
->address
)) {
712 kill(blanket
->cld
.pid
, SIGTERM
);
717 static void check_dead_children(void)
722 struct child
**cradle
, *blanket
;
723 for (cradle
= &firstborn
; (blanket
= *cradle
);)
724 if ((pid
= waitpid(blanket
->cld
.pid
, &status
, WNOHANG
)) > 1) {
725 const char *dead
= "";
727 dead
= " (with error)";
728 loginfo("[%"PRIuMAX
"] Disconnected%s", (uintmax_t)pid
, dead
);
730 /* remove the child */
731 *cradle
= blanket
->next
;
735 cradle
= &blanket
->next
;
738 static char **cld_argv
;
739 static void handle(int incoming
, struct sockaddr
*addr
, socklen_t addrlen
)
741 struct child_process cld
= { NULL
};
742 char addrbuf
[300] = "REMOTE_ADDR=", portbuf
[300];
743 char *env
[] = { addrbuf
, portbuf
, NULL
};
745 if (max_connections
&& live_children
>= max_connections
) {
747 sleep(1); /* give it some time to die */
748 check_dead_children();
749 if (live_children
>= max_connections
) {
751 logerror("Too many children, dropping connection");
756 if (addr
->sa_family
== AF_INET
) {
757 struct sockaddr_in
*sin_addr
= (void *) addr
;
758 inet_ntop(addr
->sa_family
, &sin_addr
->sin_addr
, addrbuf
+ 12,
759 sizeof(addrbuf
) - 12);
760 snprintf(portbuf
, sizeof(portbuf
), "REMOTE_PORT=%d",
761 ntohs(sin_addr
->sin_port
));
763 } else if (addr
&& addr
->sa_family
== AF_INET6
) {
764 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
766 char *buf
= addrbuf
+ 12;
767 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
768 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
,
769 sizeof(addrbuf
) - 13);
772 snprintf(portbuf
, sizeof(portbuf
), "REMOTE_PORT=%d",
773 ntohs(sin6_addr
->sin6_port
));
777 cld
.env
= (const char **)env
;
778 cld
.argv
= (const char **)cld_argv
;
780 cld
.out
= dup(incoming
);
782 if (start_command(&cld
))
783 logerror("unable to fork");
785 add_child(&cld
, addr
, addrlen
);
789 static void child_handler(int signo
)
792 * Otherwise empty handler because systemcalls will get interrupted
793 * upon signal receipt
794 * SysV needs the handler to be rearmed
796 signal(SIGCHLD
, child_handler
);
799 static int set_reuse_addr(int sockfd
)
805 return setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
815 static const char *ip2str(int family
, struct sockaddr
*sin
, socklen_t len
)
818 static char ip
[INET_ADDRSTRLEN
];
820 static char ip
[INET6_ADDRSTRLEN
];
826 inet_ntop(family
, &((struct sockaddr_in6
*)sin
)->sin6_addr
, ip
, len
);
830 inet_ntop(family
, &((struct sockaddr_in
*)sin
)->sin_addr
, ip
, len
);
833 strcpy(ip
, "<unknown>");
840 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
844 char pbuf
[NI_MAXSERV
];
845 struct addrinfo hints
, *ai0
, *ai
;
849 sprintf(pbuf
, "%d", listen_port
);
850 memset(&hints
, 0, sizeof(hints
));
851 hints
.ai_family
= AF_UNSPEC
;
852 hints
.ai_socktype
= SOCK_STREAM
;
853 hints
.ai_protocol
= IPPROTO_TCP
;
854 hints
.ai_flags
= AI_PASSIVE
;
856 gai
= getaddrinfo(listen_addr
, pbuf
, &hints
, &ai0
);
858 logerror("getaddrinfo() for %s failed: %s", listen_addr
, gai_strerror(gai
));
862 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
865 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
868 if (sockfd
>= FD_SETSIZE
) {
869 logerror("Socket descriptor too large");
875 if (ai
->ai_family
== AF_INET6
) {
877 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
879 /* Note: error is not fatal */
883 if (set_reuse_addr(sockfd
)) {
884 logerror("Could not set SO_REUSEADDR: %s", strerror(errno
));
889 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
890 logerror("Could not bind to %s: %s",
891 ip2str(ai
->ai_family
, ai
->ai_addr
, ai
->ai_addrlen
),
894 continue; /* not fatal */
896 if (listen(sockfd
, 5) < 0) {
897 logerror("Could not listen to %s: %s",
898 ip2str(ai
->ai_family
, ai
->ai_addr
, ai
->ai_addrlen
),
901 continue; /* not fatal */
904 flags
= fcntl(sockfd
, F_GETFD
, 0);
906 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
908 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
909 socklist
->list
[socklist
->nr
++] = sockfd
;
923 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
925 struct sockaddr_in sin
;
929 memset(&sin
, 0, sizeof sin
);
930 sin
.sin_family
= AF_INET
;
931 sin
.sin_port
= htons(listen_port
);
934 /* Well, host better be an IP address here. */
935 if (inet_pton(AF_INET
, listen_addr
, &sin
.sin_addr
.s_addr
) <= 0)
938 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
941 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
945 if (set_reuse_addr(sockfd
)) {
946 logerror("Could not set SO_REUSEADDR: %s", strerror(errno
));
951 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
952 logerror("Could not listen to %s: %s",
953 ip2str(AF_INET
, (struct sockaddr
*)&sin
, sizeof(sin
)),
959 if (listen(sockfd
, 5) < 0) {
960 logerror("Could not listen to %s: %s",
961 ip2str(AF_INET
, (struct sockaddr
*)&sin
, sizeof(sin
)),
967 flags
= fcntl(sockfd
, F_GETFD
, 0);
969 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
971 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
972 socklist
->list
[socklist
->nr
++] = sockfd
;
978 static void socksetup(struct string_list
*listen_addr
, int listen_port
, struct socketlist
*socklist
)
980 if (!listen_addr
->nr
)
981 setup_named_sock(NULL
, listen_port
, socklist
);
984 for (i
= 0; i
< listen_addr
->nr
; i
++) {
985 socknum
= setup_named_sock(listen_addr
->items
[i
].string
,
986 listen_port
, socklist
);
989 logerror("unable to allocate any listen sockets for host %s on port %u",
990 listen_addr
->items
[i
].string
, listen_port
);
995 static int service_loop(struct socketlist
*socklist
)
1000 pfd
= xcalloc(socklist
->nr
, sizeof(struct pollfd
));
1002 for (i
= 0; i
< socklist
->nr
; i
++) {
1003 pfd
[i
].fd
= socklist
->list
[i
];
1004 pfd
[i
].events
= POLLIN
;
1007 signal(SIGCHLD
, child_handler
);
1012 check_dead_children();
1014 if (poll(pfd
, socklist
->nr
, -1) < 0) {
1015 if (errno
!= EINTR
) {
1016 logerror("Poll failed, resuming: %s",
1023 for (i
= 0; i
< socklist
->nr
; i
++) {
1024 if (pfd
[i
].revents
& POLLIN
) {
1027 struct sockaddr_in sai
;
1029 struct sockaddr_in6 sai6
;
1032 socklen_t sslen
= sizeof(ss
);
1033 int incoming
= accept(pfd
[i
].fd
, &ss
.sa
, &sslen
);
1041 die_errno("accept returned");
1044 handle(incoming
, &ss
.sa
, sslen
);
1050 #ifdef NO_POSIX_GOODIES
1054 static void drop_privileges(struct credentials
*cred
)
1059 static void daemonize(void)
1061 die("--detach not supported on this platform");
1064 static struct credentials
*prepare_credentials(const char *user_name
,
1065 const char *group_name
)
1067 die("--user not supported on this platform");
1072 struct credentials
{
1073 struct passwd
*pass
;
1077 static void drop_privileges(struct credentials
*cred
)
1079 if (cred
&& (initgroups(cred
->pass
->pw_name
, cred
->gid
) ||
1080 setgid (cred
->gid
) || setuid(cred
->pass
->pw_uid
)))
1081 die("cannot drop privileges");
1084 static struct credentials
*prepare_credentials(const char *user_name
,
1085 const char *group_name
)
1087 static struct credentials c
;
1089 c
.pass
= getpwnam(user_name
);
1091 die("user not found - %s", user_name
);
1094 c
.gid
= c
.pass
->pw_gid
;
1096 struct group
*group
= getgrnam(group_name
);
1098 die("group not found - %s", group_name
);
1100 c
.gid
= group
->gr_gid
;
1106 static void daemonize(void)
1112 die_errno("fork failed");
1117 die_errno("setsid failed");
1125 static void store_pid(const char *path
)
1127 FILE *f
= fopen(path
, "w");
1129 die_errno("cannot open pid file '%s'", path
);
1130 if (fprintf(f
, "%"PRIuMAX
"\n", (uintmax_t) getpid()) < 0 || fclose(f
) != 0)
1131 die_errno("failed to write pid file '%s'", path
);
1134 static int serve(struct string_list
*listen_addr
, int listen_port
,
1135 struct credentials
*cred
)
1137 struct socketlist socklist
= { NULL
, 0, 0 };
1139 socksetup(listen_addr
, listen_port
, &socklist
);
1140 if (socklist
.nr
== 0)
1141 die("unable to allocate any listen sockets on port %u",
1144 drop_privileges(cred
);
1146 loginfo("Ready to rumble");
1148 return service_loop(&socklist
);
1151 int main(int argc
, char **argv
)
1153 int listen_port
= 0;
1154 struct string_list listen_addr
= STRING_LIST_INIT_NODUP
;
1155 int serve_mode
= 0, inetd_mode
= 0;
1156 const char *pid_file
= NULL
, *user_name
= NULL
, *group_name
= NULL
;
1158 struct credentials
*cred
= NULL
;
1161 git_setup_gettext();
1163 git_extract_argv0_path(argv
[0]);
1165 for (i
= 1; i
< argc
; i
++) {
1166 char *arg
= argv
[i
];
1168 if (!prefixcmp(arg
, "--listen=")) {
1169 string_list_append(&listen_addr
, xstrdup_tolower(arg
+ 9));
1172 if (!prefixcmp(arg
, "--port=")) {
1175 n
= strtoul(arg
+7, &end
, 0);
1176 if (arg
[7] && !*end
) {
1181 if (!strcmp(arg
, "--serve")) {
1185 if (!strcmp(arg
, "--inetd")) {
1190 if (!strcmp(arg
, "--verbose")) {
1194 if (!strcmp(arg
, "--syslog")) {
1198 if (!strcmp(arg
, "--export-all")) {
1199 export_all_trees
= 1;
1202 if (!prefixcmp(arg
, "--access-hook=")) {
1203 access_hook
= arg
+ 14;
1206 if (!prefixcmp(arg
, "--timeout=")) {
1207 timeout
= atoi(arg
+10);
1210 if (!prefixcmp(arg
, "--init-timeout=")) {
1211 init_timeout
= atoi(arg
+15);
1214 if (!prefixcmp(arg
, "--max-connections=")) {
1215 max_connections
= atoi(arg
+18);
1216 if (max_connections
< 0)
1217 max_connections
= 0; /* unlimited */
1220 if (!strcmp(arg
, "--strict-paths")) {
1224 if (!prefixcmp(arg
, "--base-path=")) {
1228 if (!strcmp(arg
, "--base-path-relaxed")) {
1229 base_path_relaxed
= 1;
1232 if (!prefixcmp(arg
, "--interpolated-path=")) {
1233 interpolated_path
= arg
+20;
1236 if (!strcmp(arg
, "--reuseaddr")) {
1240 if (!strcmp(arg
, "--user-path")) {
1244 if (!prefixcmp(arg
, "--user-path=")) {
1245 user_path
= arg
+ 12;
1248 if (!prefixcmp(arg
, "--pid-file=")) {
1249 pid_file
= arg
+ 11;
1252 if (!strcmp(arg
, "--detach")) {
1257 if (!prefixcmp(arg
, "--user=")) {
1258 user_name
= arg
+ 7;
1261 if (!prefixcmp(arg
, "--group=")) {
1262 group_name
= arg
+ 8;
1265 if (!prefixcmp(arg
, "--enable=")) {
1266 enable_service(arg
+ 9, 1);
1269 if (!prefixcmp(arg
, "--disable=")) {
1270 enable_service(arg
+ 10, 0);
1273 if (!prefixcmp(arg
, "--allow-override=")) {
1274 make_service_overridable(arg
+ 17, 1);
1277 if (!prefixcmp(arg
, "--forbid-override=")) {
1278 make_service_overridable(arg
+ 18, 0);
1281 if (!prefixcmp(arg
, "--informative-errors")) {
1282 informative_errors
= 1;
1285 if (!prefixcmp(arg
, "--no-informative-errors")) {
1286 informative_errors
= 0;
1289 if (!strcmp(arg
, "--")) {
1290 ok_paths
= &argv
[i
+1];
1292 } else if (arg
[0] != '-') {
1293 ok_paths
= &argv
[i
];
1297 usage(daemon_usage
);
1301 openlog("git-daemon", LOG_PID
, LOG_DAEMON
);
1302 set_die_routine(daemon_die
);
1304 /* avoid splitting a message in the middle */
1305 setvbuf(stderr
, NULL
, _IOFBF
, 4096);
1307 if (inetd_mode
&& (detach
|| group_name
|| user_name
))
1308 die("--detach, --user and --group are incompatible with --inetd");
1310 if (inetd_mode
&& (listen_port
|| (listen_addr
.nr
> 0)))
1311 die("--listen= and --port= are incompatible with --inetd");
1312 else if (listen_port
== 0)
1313 listen_port
= DEFAULT_GIT_PORT
;
1315 if (group_name
&& !user_name
)
1316 die("--group supplied without --user");
1319 cred
= prepare_credentials(user_name
, group_name
);
1321 if (strict_paths
&& (!ok_paths
|| !*ok_paths
))
1322 die("option --strict-paths requires a whitelist");
1324 if (base_path
&& !is_directory(base_path
))
1325 die("base-path '%s' does not exist or is not a directory",
1329 if (!freopen("/dev/null", "w", stderr
))
1330 die_errno("failed to redirect stderr to /dev/null");
1333 if (inetd_mode
|| serve_mode
)
1342 store_pid(pid_file
);
1344 /* prepare argv for serving-processes */
1345 cld_argv
= xmalloc(sizeof (char *) * (argc
+ 2));
1346 cld_argv
[0] = argv
[0]; /* git-daemon */
1347 cld_argv
[1] = "--serve";
1348 for (i
= 1; i
< argc
; ++i
)
1349 cld_argv
[i
+1] = argv
[i
];
1350 cld_argv
[argc
+1] = NULL
;
1352 return serve(&listen_addr
, listen_port
, cred
);