4 #include "run-command.h"
6 #include "string-list.h"
9 #define HOST_NAME_MAX 256
17 #define initgroups(x, y) (0) /* nothing */
20 static int log_syslog
;
23 static int informative_errors
;
25 static const char daemon_usage
[] =
26 "git daemon [--verbose] [--syslog] [--export-all]\n"
27 " [--timeout=<n>] [--init-timeout=<n>] [--max-connections=<n>]\n"
28 " [--strict-paths] [--base-path=<path>] [--base-path-relaxed]\n"
29 " [--user-path | --user-path=<path>]\n"
30 " [--interpolated-path=<path>]\n"
31 " [--reuseaddr] [--pid-file=<file>]\n"
32 " [--(enable|disable|allow-override|forbid-override)=<service>]\n"
33 " [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
34 " [--detach] [--user=<user> [--group=<group>]]\n"
37 /* List of acceptable pathname prefixes */
38 static char **ok_paths
;
39 static int strict_paths
;
41 /* If this is set, git-daemon-export-ok is not required */
42 static int export_all_trees
;
44 /* Take all paths relative to this one if non-NULL */
45 static char *base_path
;
46 static char *interpolated_path
;
47 static int base_path_relaxed
;
49 /* Flag indicating client sent extra args. */
50 static int saw_extended_args
;
52 /* If defined, ~user notation is allowed and the string is inserted
53 * after ~user/. E.g. a request to git://host/~alice/frotz would
54 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
56 static const char *user_path
;
58 /* Timeout, and initial timeout */
59 static unsigned int timeout
;
60 static unsigned int init_timeout
;
62 static char *hostname
;
63 static char *canon_hostname
;
64 static char *ip_address
;
65 static char *tcp_port
;
67 static void logreport(int priority
, const char *err
, va_list params
)
71 vsnprintf(buf
, sizeof(buf
), err
, params
);
72 syslog(priority
, "%s", buf
);
75 * Since stderr is set to buffered mode, the
76 * logging of different processes will not overlap
77 * unless they overflow the (rather big) buffers.
79 fprintf(stderr
, "[%"PRIuMAX
"] ", (uintmax_t)getpid());
80 vfprintf(stderr
, err
, params
);
86 __attribute__((format (printf
, 1, 2)))
87 static void logerror(const char *err
, ...)
90 va_start(params
, err
);
91 logreport(LOG_ERR
, err
, params
);
95 __attribute__((format (printf
, 1, 2)))
96 static void loginfo(const char *err
, ...)
101 va_start(params
, err
);
102 logreport(LOG_INFO
, err
, params
);
106 static void NORETURN
daemon_die(const char *err
, va_list params
)
108 logreport(LOG_ERR
, err
, params
);
112 static const char *path_ok(char *directory
)
114 static char rpath
[PATH_MAX
];
115 static char interp_path
[PATH_MAX
];
121 if (daemon_avoid_alias(dir
)) {
122 logerror("'%s': aliased", dir
);
128 logerror("'%s': User-path not allowed", dir
);
132 /* Got either "~alice" or "~alice/foo";
133 * rewrite them to "~alice/%s" or
136 int namlen
, restlen
= strlen(dir
);
137 char *slash
= strchr(dir
, '/');
139 slash
= dir
+ restlen
;
140 namlen
= slash
- dir
;
142 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path
, dir
, namlen
, restlen
, slash
);
143 snprintf(rpath
, PATH_MAX
, "%.*s/%s%.*s",
144 namlen
, dir
, user_path
, restlen
, slash
);
148 else if (interpolated_path
&& saw_extended_args
) {
149 struct strbuf expanded_path
= STRBUF_INIT
;
150 struct strbuf_expand_dict_entry dict
[6];
152 dict
[0].placeholder
= "H"; dict
[0].value
= hostname
;
153 dict
[1].placeholder
= "CH"; dict
[1].value
= canon_hostname
;
154 dict
[2].placeholder
= "IP"; dict
[2].value
= ip_address
;
155 dict
[3].placeholder
= "P"; dict
[3].value
= tcp_port
;
156 dict
[4].placeholder
= "D"; dict
[4].value
= directory
;
157 dict
[5].placeholder
= NULL
; dict
[5].value
= NULL
;
159 /* Allow only absolute */
160 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir
);
164 strbuf_expand(&expanded_path
, interpolated_path
,
165 strbuf_expand_dict_cb
, &dict
);
166 strlcpy(interp_path
, expanded_path
.buf
, PATH_MAX
);
167 strbuf_release(&expanded_path
);
168 loginfo("Interpolated dir '%s'", interp_path
);
172 else if (base_path
) {
174 /* Allow only absolute */
175 logerror("'%s': Non-absolute path denied (base-path active)", dir
);
178 snprintf(rpath
, PATH_MAX
, "%s%s", base_path
, dir
);
182 path
= enter_repo(dir
, strict_paths
);
183 if (!path
&& base_path
&& base_path_relaxed
) {
185 * if we fail and base_path_relaxed is enabled, try without
186 * prefixing the base path
189 path
= enter_repo(dir
, strict_paths
);
193 logerror("'%s' does not appear to be a git repository", dir
);
197 if ( ok_paths
&& *ok_paths
) {
199 int pathlen
= strlen(path
);
201 /* The validation is done on the paths after enter_repo
202 * appends optional {.git,.git/.git} and friends, but
203 * it does not use getcwd(). So if your /pub is
204 * a symlink to /mnt/pub, you can whitelist /pub and
205 * do not have to say /mnt/pub.
208 for ( pp
= ok_paths
; *pp
; pp
++ ) {
209 int len
= strlen(*pp
);
210 if (len
<= pathlen
&&
211 !memcmp(*pp
, path
, len
) &&
212 (path
[len
] == '\0' ||
213 (!strict_paths
&& path
[len
] == '/')))
218 /* be backwards compatible */
223 logerror("'%s': not in whitelist", path
);
224 return NULL
; /* Fallthrough. Deny by default */
227 typedef int (*daemon_service_fn
)(void);
228 struct daemon_service
{
230 const char *config_name
;
231 daemon_service_fn fn
;
236 static struct daemon_service
*service_looking_at
;
237 static int service_enabled
;
239 static int git_daemon_config(const char *var
, const char *value
, void *cb
)
241 if (!prefixcmp(var
, "daemon.") &&
242 !strcmp(var
+ 7, service_looking_at
->config_name
)) {
243 service_enabled
= git_config_bool(var
, value
);
247 /* we are not interested in parsing any other configuration here */
251 static int daemon_error(const char *dir
, const char *msg
)
253 if (!informative_errors
)
254 msg
= "access denied or repository not exported";
255 packet_write(1, "ERR %s: %s", msg
, dir
);
259 static int run_service(char *dir
, struct daemon_service
*service
)
262 int enabled
= service
->enabled
;
264 loginfo("Request %s for '%s'", service
->name
, dir
);
266 if (!enabled
&& !service
->overridable
) {
267 logerror("'%s': service not enabled.", service
->name
);
269 return daemon_error(dir
, "service not enabled");
272 if (!(path
= path_ok(dir
)))
273 return daemon_error(dir
, "no such repository");
276 * Security on the cheap.
278 * We want a readable HEAD, usable "objects" directory, and
279 * a "git-daemon-export-ok" flag that says that the other side
280 * is ok with us doing this.
282 * path_ok() uses enter_repo() and does whitelist checking.
283 * We only need to make sure the repository is exported.
286 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
287 logerror("'%s': repository not exported.", path
);
289 return daemon_error(dir
, "repository not exported");
292 if (service
->overridable
) {
293 service_looking_at
= service
;
294 service_enabled
= -1;
295 git_config(git_daemon_config
, NULL
);
296 if (0 <= service_enabled
)
297 enabled
= service_enabled
;
300 logerror("'%s': service not enabled for '%s'",
301 service
->name
, path
);
303 return daemon_error(dir
, "service not enabled");
307 * We'll ignore SIGTERM from now on, we have a
310 signal(SIGTERM
, SIG_IGN
);
312 return service
->fn();
315 static void copy_to_log(int fd
)
317 struct strbuf line
= STRBUF_INIT
;
320 fp
= fdopen(fd
, "r");
322 logerror("fdopen of error channel failed");
327 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
328 logerror("%s", line
.buf
);
329 strbuf_setlen(&line
, 0);
332 strbuf_release(&line
);
336 static int run_service_command(const char **argv
)
338 struct child_process cld
;
340 memset(&cld
, 0, sizeof(cld
));
344 if (start_command(&cld
))
350 copy_to_log(cld
.err
);
352 return finish_command(&cld
);
355 static int upload_pack(void)
357 /* Timeout as string */
358 char timeout_buf
[64];
359 const char *argv
[] = { "upload-pack", "--strict", NULL
, ".", NULL
};
361 argv
[2] = timeout_buf
;
363 snprintf(timeout_buf
, sizeof timeout_buf
, "--timeout=%u", timeout
);
364 return run_service_command(argv
);
367 static int upload_archive(void)
369 static const char *argv
[] = { "upload-archive", ".", NULL
};
370 return run_service_command(argv
);
373 static int receive_pack(void)
375 static const char *argv
[] = { "receive-pack", ".", NULL
};
376 return run_service_command(argv
);
379 static struct daemon_service daemon_service
[] = {
380 { "upload-archive", "uploadarch", upload_archive
, 0, 1 },
381 { "upload-pack", "uploadpack", upload_pack
, 1, 1 },
382 { "receive-pack", "receivepack", receive_pack
, 0, 1 },
385 static void enable_service(const char *name
, int ena
)
388 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
389 if (!strcmp(daemon_service
[i
].name
, name
)) {
390 daemon_service
[i
].enabled
= ena
;
394 die("No such service %s", name
);
397 static void make_service_overridable(const char *name
, int ena
)
400 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
401 if (!strcmp(daemon_service
[i
].name
, name
)) {
402 daemon_service
[i
].overridable
= ena
;
406 die("No such service %s", name
);
409 static char *xstrdup_tolower(const char *str
)
411 char *p
, *dup
= xstrdup(str
);
412 for (p
= dup
; *p
; p
++)
417 static void parse_host_and_port(char *hostport
, char **host
,
420 if (*hostport
== '[') {
423 end
= strchr(hostport
, ']');
425 die("Invalid request ('[' without ']')");
427 *host
= hostport
+ 1;
430 else if (end
[1] == ':')
433 die("Garbage after end of host part");
436 *port
= strrchr(hostport
, ':');
445 * Read the host as supplied by the client connection.
447 static void parse_host_arg(char *extra_args
, int buflen
)
451 char *end
= extra_args
+ buflen
;
453 if (extra_args
< end
&& *extra_args
) {
454 saw_extended_args
= 1;
455 if (strncasecmp("host=", extra_args
, 5) == 0) {
456 val
= extra_args
+ 5;
457 vallen
= strlen(val
) + 1;
459 /* Split <host>:<port> at colon. */
462 parse_host_and_port(val
, &host
, &port
);
465 tcp_port
= xstrdup(port
);
468 hostname
= xstrdup_tolower(host
);
471 /* On to the next one */
472 extra_args
= val
+ vallen
;
474 if (extra_args
< end
&& *extra_args
)
475 die("Invalid request");
479 * Locate canonical hostname and its IP address.
483 struct addrinfo hints
;
486 static char addrbuf
[HOST_NAME_MAX
+ 1];
488 memset(&hints
, 0, sizeof(hints
));
489 hints
.ai_flags
= AI_CANONNAME
;
491 gai
= getaddrinfo(hostname
, NULL
, &hints
, &ai
);
493 struct sockaddr_in
*sin_addr
= (void *)ai
->ai_addr
;
495 inet_ntop(AF_INET
, &sin_addr
->sin_addr
,
496 addrbuf
, sizeof(addrbuf
));
498 ip_address
= xstrdup(addrbuf
);
500 free(canon_hostname
);
501 canon_hostname
= xstrdup(ai
->ai_canonname
?
502 ai
->ai_canonname
: ip_address
);
507 struct hostent
*hent
;
508 struct sockaddr_in sa
;
510 static char addrbuf
[HOST_NAME_MAX
+ 1];
512 hent
= gethostbyname(hostname
);
514 ap
= hent
->h_addr_list
;
515 memset(&sa
, 0, sizeof sa
);
516 sa
.sin_family
= hent
->h_addrtype
;
517 sa
.sin_port
= htons(0);
518 memcpy(&sa
.sin_addr
, *ap
, hent
->h_length
);
520 inet_ntop(hent
->h_addrtype
, &sa
.sin_addr
,
521 addrbuf
, sizeof(addrbuf
));
523 free(canon_hostname
);
524 canon_hostname
= xstrdup(hent
->h_name
);
526 ip_address
= xstrdup(addrbuf
);
532 static int execute(void)
534 static char line
[1000];
536 char *addr
= getenv("REMOTE_ADDR"), *port
= getenv("REMOTE_PORT");
539 loginfo("Connection from %s:%s", addr
, port
);
541 alarm(init_timeout
? init_timeout
: timeout
);
542 pktlen
= packet_read_line(0, line
, sizeof(line
));
547 loginfo("Extended attributes (%d bytes) exist <%.*s>",
549 (int) pktlen
- len
, line
+ len
+ 1);
550 if (len
&& line
[len
-1] == '\n') {
556 free(canon_hostname
);
559 hostname
= canon_hostname
= ip_address
= tcp_port
= NULL
;
562 parse_host_arg(line
+ len
+ 1, pktlen
- len
- 1);
564 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
565 struct daemon_service
*s
= &(daemon_service
[i
]);
566 int namelen
= strlen(s
->name
);
567 if (!prefixcmp(line
, "git-") &&
568 !strncmp(s
->name
, line
+ 4, namelen
) &&
569 line
[namelen
+ 4] == ' ') {
571 * Note: The directory here is probably context sensitive,
572 * and might depend on the actual service being performed.
574 return run_service(line
+ namelen
+ 5, s
);
578 logerror("Protocol error: '%s'", line
);
582 static int addrcmp(const struct sockaddr_storage
*s1
,
583 const struct sockaddr_storage
*s2
)
585 const struct sockaddr
*sa1
= (const struct sockaddr
*) s1
;
586 const struct sockaddr
*sa2
= (const struct sockaddr
*) s2
;
588 if (sa1
->sa_family
!= sa2
->sa_family
)
589 return sa1
->sa_family
- sa2
->sa_family
;
590 if (sa1
->sa_family
== AF_INET
)
591 return memcmp(&((struct sockaddr_in
*)s1
)->sin_addr
,
592 &((struct sockaddr_in
*)s2
)->sin_addr
,
593 sizeof(struct in_addr
));
595 if (sa1
->sa_family
== AF_INET6
)
596 return memcmp(&((struct sockaddr_in6
*)s1
)->sin6_addr
,
597 &((struct sockaddr_in6
*)s2
)->sin6_addr
,
598 sizeof(struct in6_addr
));
603 static int max_connections
= 32;
605 static unsigned int live_children
;
607 static struct child
{
609 struct child_process cld
;
610 struct sockaddr_storage address
;
613 static void add_child(struct child_process
*cld
, struct sockaddr
*addr
, socklen_t addrlen
)
615 struct child
*newborn
, **cradle
;
617 newborn
= xcalloc(1, sizeof(*newborn
));
619 memcpy(&newborn
->cld
, cld
, sizeof(*cld
));
620 memcpy(&newborn
->address
, addr
, addrlen
);
621 for (cradle
= &firstborn
; *cradle
; cradle
= &(*cradle
)->next
)
622 if (!addrcmp(&(*cradle
)->address
, &newborn
->address
))
624 newborn
->next
= *cradle
;
629 * This gets called if the number of connections grows
630 * past "max_connections".
632 * We kill the newest connection from a duplicate IP.
634 static void kill_some_child(void)
636 const struct child
*blanket
, *next
;
638 if (!(blanket
= firstborn
))
641 for (; (next
= blanket
->next
); blanket
= next
)
642 if (!addrcmp(&blanket
->address
, &next
->address
)) {
643 kill(blanket
->cld
.pid
, SIGTERM
);
648 static void check_dead_children(void)
653 struct child
**cradle
, *blanket
;
654 for (cradle
= &firstborn
; (blanket
= *cradle
);)
655 if ((pid
= waitpid(blanket
->cld
.pid
, &status
, WNOHANG
)) > 1) {
656 const char *dead
= "";
658 dead
= " (with error)";
659 loginfo("[%"PRIuMAX
"] Disconnected%s", (uintmax_t)pid
, dead
);
661 /* remove the child */
662 *cradle
= blanket
->next
;
666 cradle
= &blanket
->next
;
669 static char **cld_argv
;
670 static void handle(int incoming
, struct sockaddr
*addr
, socklen_t addrlen
)
672 struct child_process cld
= { NULL
};
673 char addrbuf
[300] = "REMOTE_ADDR=", portbuf
[300];
674 char *env
[] = { addrbuf
, portbuf
, NULL
};
676 if (max_connections
&& live_children
>= max_connections
) {
678 sleep(1); /* give it some time to die */
679 check_dead_children();
680 if (live_children
>= max_connections
) {
682 logerror("Too many children, dropping connection");
687 if (addr
->sa_family
== AF_INET
) {
688 struct sockaddr_in
*sin_addr
= (void *) addr
;
689 inet_ntop(addr
->sa_family
, &sin_addr
->sin_addr
, addrbuf
+ 12,
690 sizeof(addrbuf
) - 12);
691 snprintf(portbuf
, sizeof(portbuf
), "REMOTE_PORT=%d",
692 ntohs(sin_addr
->sin_port
));
694 } else if (addr
&& addr
->sa_family
== AF_INET6
) {
695 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
697 char *buf
= addrbuf
+ 12;
698 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
699 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
,
700 sizeof(addrbuf
) - 13);
703 snprintf(portbuf
, sizeof(portbuf
), "REMOTE_PORT=%d",
704 ntohs(sin6_addr
->sin6_port
));
708 cld
.env
= (const char **)env
;
709 cld
.argv
= (const char **)cld_argv
;
711 cld
.out
= dup(incoming
);
713 if (start_command(&cld
))
714 logerror("unable to fork");
716 add_child(&cld
, addr
, addrlen
);
720 static void child_handler(int signo
)
723 * Otherwise empty handler because systemcalls will get interrupted
724 * upon signal receipt
725 * SysV needs the handler to be rearmed
727 signal(SIGCHLD
, child_handler
);
730 static int set_reuse_addr(int sockfd
)
736 return setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
746 static const char *ip2str(int family
, struct sockaddr
*sin
, socklen_t len
)
749 static char ip
[INET_ADDRSTRLEN
];
751 static char ip
[INET6_ADDRSTRLEN
];
757 inet_ntop(family
, &((struct sockaddr_in6
*)sin
)->sin6_addr
, ip
, len
);
761 inet_ntop(family
, &((struct sockaddr_in
*)sin
)->sin_addr
, ip
, len
);
764 strcpy(ip
, "<unknown>");
771 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
775 char pbuf
[NI_MAXSERV
];
776 struct addrinfo hints
, *ai0
, *ai
;
780 sprintf(pbuf
, "%d", listen_port
);
781 memset(&hints
, 0, sizeof(hints
));
782 hints
.ai_family
= AF_UNSPEC
;
783 hints
.ai_socktype
= SOCK_STREAM
;
784 hints
.ai_protocol
= IPPROTO_TCP
;
785 hints
.ai_flags
= AI_PASSIVE
;
787 gai
= getaddrinfo(listen_addr
, pbuf
, &hints
, &ai0
);
789 logerror("getaddrinfo() for %s failed: %s", listen_addr
, gai_strerror(gai
));
793 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
796 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
799 if (sockfd
>= FD_SETSIZE
) {
800 logerror("Socket descriptor too large");
806 if (ai
->ai_family
== AF_INET6
) {
808 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
810 /* Note: error is not fatal */
814 if (set_reuse_addr(sockfd
)) {
815 logerror("Could not set SO_REUSEADDR: %s", strerror(errno
));
820 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
821 logerror("Could not bind to %s: %s",
822 ip2str(ai
->ai_family
, ai
->ai_addr
, ai
->ai_addrlen
),
825 continue; /* not fatal */
827 if (listen(sockfd
, 5) < 0) {
828 logerror("Could not listen to %s: %s",
829 ip2str(ai
->ai_family
, ai
->ai_addr
, ai
->ai_addrlen
),
832 continue; /* not fatal */
835 flags
= fcntl(sockfd
, F_GETFD
, 0);
837 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
839 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
840 socklist
->list
[socklist
->nr
++] = sockfd
;
854 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
856 struct sockaddr_in sin
;
860 memset(&sin
, 0, sizeof sin
);
861 sin
.sin_family
= AF_INET
;
862 sin
.sin_port
= htons(listen_port
);
865 /* Well, host better be an IP address here. */
866 if (inet_pton(AF_INET
, listen_addr
, &sin
.sin_addr
.s_addr
) <= 0)
869 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
872 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
876 if (set_reuse_addr(sockfd
)) {
877 logerror("Could not set SO_REUSEADDR: %s", strerror(errno
));
882 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
883 logerror("Could not listen to %s: %s",
884 ip2str(AF_INET
, (struct sockaddr
*)&sin
, sizeof(sin
)),
890 if (listen(sockfd
, 5) < 0) {
891 logerror("Could not listen to %s: %s",
892 ip2str(AF_INET
, (struct sockaddr
*)&sin
, sizeof(sin
)),
898 flags
= fcntl(sockfd
, F_GETFD
, 0);
900 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
902 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
903 socklist
->list
[socklist
->nr
++] = sockfd
;
909 static void socksetup(struct string_list
*listen_addr
, int listen_port
, struct socketlist
*socklist
)
911 if (!listen_addr
->nr
)
912 setup_named_sock(NULL
, listen_port
, socklist
);
915 for (i
= 0; i
< listen_addr
->nr
; i
++) {
916 socknum
= setup_named_sock(listen_addr
->items
[i
].string
,
917 listen_port
, socklist
);
920 logerror("unable to allocate any listen sockets for host %s on port %u",
921 listen_addr
->items
[i
].string
, listen_port
);
926 static int service_loop(struct socketlist
*socklist
)
931 pfd
= xcalloc(socklist
->nr
, sizeof(struct pollfd
));
933 for (i
= 0; i
< socklist
->nr
; i
++) {
934 pfd
[i
].fd
= socklist
->list
[i
];
935 pfd
[i
].events
= POLLIN
;
938 signal(SIGCHLD
, child_handler
);
943 check_dead_children();
945 if (poll(pfd
, socklist
->nr
, -1) < 0) {
946 if (errno
!= EINTR
) {
947 logerror("Poll failed, resuming: %s",
954 for (i
= 0; i
< socklist
->nr
; i
++) {
955 if (pfd
[i
].revents
& POLLIN
) {
958 struct sockaddr_in sai
;
960 struct sockaddr_in6 sai6
;
963 socklen_t sslen
= sizeof(ss
);
964 int incoming
= accept(pfd
[i
].fd
, &ss
.sa
, &sslen
);
972 die_errno("accept returned");
975 handle(incoming
, &ss
.sa
, sslen
);
981 /* if any standard file descriptor is missing open it to /dev/null */
982 static void sanitize_stdfds(void)
984 int fd
= open("/dev/null", O_RDWR
, 0);
985 while (fd
!= -1 && fd
< 2)
988 die_errno("open /dev/null or dup failed");
993 #ifdef NO_POSIX_GOODIES
997 static void drop_privileges(struct credentials
*cred
)
1002 static void daemonize(void)
1004 die("--detach not supported on this platform");
1007 static struct credentials
*prepare_credentials(const char *user_name
,
1008 const char *group_name
)
1010 die("--user not supported on this platform");
1015 struct credentials
{
1016 struct passwd
*pass
;
1020 static void drop_privileges(struct credentials
*cred
)
1022 if (cred
&& (initgroups(cred
->pass
->pw_name
, cred
->gid
) ||
1023 setgid (cred
->gid
) || setuid(cred
->pass
->pw_uid
)))
1024 die("cannot drop privileges");
1027 static struct credentials
*prepare_credentials(const char *user_name
,
1028 const char *group_name
)
1030 static struct credentials c
;
1032 c
.pass
= getpwnam(user_name
);
1034 die("user not found - %s", user_name
);
1037 c
.gid
= c
.pass
->pw_gid
;
1039 struct group
*group
= getgrnam(group_name
);
1041 die("group not found - %s", group_name
);
1043 c
.gid
= group
->gr_gid
;
1049 static void daemonize(void)
1055 die_errno("fork failed");
1060 die_errno("setsid failed");
1068 static void store_pid(const char *path
)
1070 FILE *f
= fopen(path
, "w");
1072 die_errno("cannot open pid file '%s'", path
);
1073 if (fprintf(f
, "%"PRIuMAX
"\n", (uintmax_t) getpid()) < 0 || fclose(f
) != 0)
1074 die_errno("failed to write pid file '%s'", path
);
1077 static int serve(struct string_list
*listen_addr
, int listen_port
,
1078 struct credentials
*cred
)
1080 struct socketlist socklist
= { NULL
, 0, 0 };
1082 socksetup(listen_addr
, listen_port
, &socklist
);
1083 if (socklist
.nr
== 0)
1084 die("unable to allocate any listen sockets on port %u",
1087 drop_privileges(cred
);
1089 loginfo("Ready to rumble");
1091 return service_loop(&socklist
);
1094 int main(int argc
, char **argv
)
1096 int listen_port
= 0;
1097 struct string_list listen_addr
= STRING_LIST_INIT_NODUP
;
1098 int serve_mode
= 0, inetd_mode
= 0;
1099 const char *pid_file
= NULL
, *user_name
= NULL
, *group_name
= NULL
;
1101 struct credentials
*cred
= NULL
;
1104 git_setup_gettext();
1106 git_extract_argv0_path(argv
[0]);
1108 for (i
= 1; i
< argc
; i
++) {
1109 char *arg
= argv
[i
];
1111 if (!prefixcmp(arg
, "--listen=")) {
1112 string_list_append(&listen_addr
, xstrdup_tolower(arg
+ 9));
1115 if (!prefixcmp(arg
, "--port=")) {
1118 n
= strtoul(arg
+7, &end
, 0);
1119 if (arg
[7] && !*end
) {
1124 if (!strcmp(arg
, "--serve")) {
1128 if (!strcmp(arg
, "--inetd")) {
1133 if (!strcmp(arg
, "--verbose")) {
1137 if (!strcmp(arg
, "--syslog")) {
1141 if (!strcmp(arg
, "--export-all")) {
1142 export_all_trees
= 1;
1145 if (!prefixcmp(arg
, "--timeout=")) {
1146 timeout
= atoi(arg
+10);
1149 if (!prefixcmp(arg
, "--init-timeout=")) {
1150 init_timeout
= atoi(arg
+15);
1153 if (!prefixcmp(arg
, "--max-connections=")) {
1154 max_connections
= atoi(arg
+18);
1155 if (max_connections
< 0)
1156 max_connections
= 0; /* unlimited */
1159 if (!strcmp(arg
, "--strict-paths")) {
1163 if (!prefixcmp(arg
, "--base-path=")) {
1167 if (!strcmp(arg
, "--base-path-relaxed")) {
1168 base_path_relaxed
= 1;
1171 if (!prefixcmp(arg
, "--interpolated-path=")) {
1172 interpolated_path
= arg
+20;
1175 if (!strcmp(arg
, "--reuseaddr")) {
1179 if (!strcmp(arg
, "--user-path")) {
1183 if (!prefixcmp(arg
, "--user-path=")) {
1184 user_path
= arg
+ 12;
1187 if (!prefixcmp(arg
, "--pid-file=")) {
1188 pid_file
= arg
+ 11;
1191 if (!strcmp(arg
, "--detach")) {
1196 if (!prefixcmp(arg
, "--user=")) {
1197 user_name
= arg
+ 7;
1200 if (!prefixcmp(arg
, "--group=")) {
1201 group_name
= arg
+ 8;
1204 if (!prefixcmp(arg
, "--enable=")) {
1205 enable_service(arg
+ 9, 1);
1208 if (!prefixcmp(arg
, "--disable=")) {
1209 enable_service(arg
+ 10, 0);
1212 if (!prefixcmp(arg
, "--allow-override=")) {
1213 make_service_overridable(arg
+ 17, 1);
1216 if (!prefixcmp(arg
, "--forbid-override=")) {
1217 make_service_overridable(arg
+ 18, 0);
1220 if (!prefixcmp(arg
, "--informative-errors")) {
1221 informative_errors
= 1;
1224 if (!prefixcmp(arg
, "--no-informative-errors")) {
1225 informative_errors
= 0;
1228 if (!strcmp(arg
, "--")) {
1229 ok_paths
= &argv
[i
+1];
1231 } else if (arg
[0] != '-') {
1232 ok_paths
= &argv
[i
];
1236 usage(daemon_usage
);
1240 openlog("git-daemon", LOG_PID
, LOG_DAEMON
);
1241 set_die_routine(daemon_die
);
1243 /* avoid splitting a message in the middle */
1244 setvbuf(stderr
, NULL
, _IOFBF
, 4096);
1246 if (inetd_mode
&& (detach
|| group_name
|| user_name
))
1247 die("--detach, --user and --group are incompatible with --inetd");
1249 if (inetd_mode
&& (listen_port
|| (listen_addr
.nr
> 0)))
1250 die("--listen= and --port= are incompatible with --inetd");
1251 else if (listen_port
== 0)
1252 listen_port
= DEFAULT_GIT_PORT
;
1254 if (group_name
&& !user_name
)
1255 die("--group supplied without --user");
1258 cred
= prepare_credentials(user_name
, group_name
);
1260 if (strict_paths
&& (!ok_paths
|| !*ok_paths
))
1261 die("option --strict-paths requires a whitelist");
1263 if (base_path
&& !is_directory(base_path
))
1264 die("base-path '%s' does not exist or is not a directory",
1268 if (!freopen("/dev/null", "w", stderr
))
1269 die_errno("failed to redirect stderr to /dev/null");
1272 if (inetd_mode
|| serve_mode
)
1281 store_pid(pid_file
);
1283 /* prepare argv for serving-processes */
1284 cld_argv
= xmalloc(sizeof (char *) * (argc
+ 2));
1285 cld_argv
[0] = argv
[0]; /* git-daemon */
1286 cld_argv
[1] = "--serve";
1287 for (i
= 1; i
< argc
; ++i
)
1288 cld_argv
[i
+1] = argv
[i
];
1289 cld_argv
[argc
+1] = NULL
;
1291 return serve(&listen_addr
, listen_port
, cred
);