3 #include <sys/socket.h>
7 #include <netinet/in.h>
16 static int log_syslog
;
20 static const char daemon_usage
[] =
21 "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
22 " [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
23 " [--base-path=path] [--user-path | --user-path=path]\n"
24 " [--reuseaddr] [--detach] [--pid-file=file]\n"
25 " [--user=user [[--group=group]] [directory...]";
27 /* List of acceptable pathname prefixes */
28 static char **ok_paths
;
29 static int strict_paths
;
31 /* If this is set, git-daemon-export-ok is not required */
32 static int export_all_trees
;
34 /* Take all paths relative to this one if non-NULL */
35 static char *base_path
;
37 /* If defined, ~user notation is allowed and the string is inserted
38 * after ~user/. E.g. a request to git://host/~alice/frotz would
39 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
41 static const char *user_path
;
43 /* Timeout, and initial timeout */
44 static unsigned int timeout
;
45 static unsigned int init_timeout
;
47 static void logreport(int priority
, const char *err
, va_list params
)
49 /* We should do a single write so that it is atomic and output
50 * of several processes do not get intermingled. */
55 /* sizeof(buf) should be big enough for "[pid] \n" */
56 buflen
= snprintf(buf
, sizeof(buf
), "[%ld] ", (long) getpid());
58 maxlen
= sizeof(buf
) - buflen
- 1; /* -1 for our own LF */
59 msglen
= vsnprintf(buf
+ buflen
, maxlen
, err
, params
);
62 syslog(priority
, "%s", buf
);
66 /* maxlen counted our own LF but also counts space given to
67 * vsnprintf for the terminating NUL. We want to make sure that
68 * we have space for our own LF and NUL after the "meat" of the
69 * message, so truncate it at maxlen - 1.
71 if (msglen
> maxlen
- 1)
74 msglen
= 0; /* Protect against weird return values. */
80 write(2, buf
, buflen
);
83 static void logerror(const char *err
, ...)
86 va_start(params
, err
);
87 logreport(LOG_ERR
, err
, params
);
91 static void loginfo(const char *err
, ...)
96 va_start(params
, err
);
97 logreport(LOG_INFO
, err
, params
);
101 static void NORETURN
daemon_die(const char *err
, va_list params
)
103 logreport(LOG_ERR
, err
, params
);
107 static int avoid_alias(char *p
)
112 * This resurrects the belts and suspenders paranoia check by HPA
113 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
114 * does not do getcwd() based path canonicalizations.
116 * sl becomes true immediately after seeing '/' and continues to
117 * be true as long as dots continue after that without intervening
120 if (!p
|| (*p
!= '/' && *p
!= '~'))
130 else if (ch
== '/') {
132 /* reject //, /./ and /../ */
137 if (0 < ndot
&& ndot
< 3)
138 /* reject /.$ and /..$ */
147 else if (ch
== '/') {
154 static char *path_ok(char *dir
)
156 static char rpath
[PATH_MAX
];
159 if (avoid_alias(dir
)) {
160 logerror("'%s': aliased", dir
);
166 logerror("'%s': User-path not allowed", dir
);
170 /* Got either "~alice" or "~alice/foo";
171 * rewrite them to "~alice/%s" or
174 int namlen
, restlen
= strlen(dir
);
175 char *slash
= strchr(dir
, '/');
177 slash
= dir
+ restlen
;
178 namlen
= slash
- dir
;
180 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path
, dir
, namlen
, restlen
, slash
);
181 snprintf(rpath
, PATH_MAX
, "%.*s/%s%.*s",
182 namlen
, dir
, user_path
, restlen
, slash
);
186 else if (base_path
) {
188 /* Allow only absolute */
189 logerror("'%s': Non-absolute path denied (base-path active)", dir
);
193 snprintf(rpath
, PATH_MAX
, "%s%s", base_path
, dir
);
198 path
= enter_repo(dir
, strict_paths
);
201 logerror("'%s': unable to chdir or not a git archive", dir
);
205 if ( ok_paths
&& *ok_paths
) {
207 int pathlen
= strlen(path
);
209 /* The validation is done on the paths after enter_repo
210 * appends optional {.git,.git/.git} and friends, but
211 * it does not use getcwd(). So if your /pub is
212 * a symlink to /mnt/pub, you can whitelist /pub and
213 * do not have to say /mnt/pub.
216 for ( pp
= ok_paths
; *pp
; pp
++ ) {
217 int len
= strlen(*pp
);
218 if (len
<= pathlen
&&
219 !memcmp(*pp
, path
, len
) &&
220 (path
[len
] == '\0' ||
221 (!strict_paths
&& path
[len
] == '/')))
226 /* be backwards compatible */
231 logerror("'%s': not in whitelist", path
);
232 return NULL
; /* Fallthrough. Deny by default */
235 static int upload(char *dir
)
237 /* Timeout as string */
238 char timeout_buf
[64];
241 loginfo("Request for '%s'", dir
);
243 if (!(path
= path_ok(dir
)))
247 * Security on the cheap.
249 * We want a readable HEAD, usable "objects" directory, and
250 * a "git-daemon-export-ok" flag that says that the other side
251 * is ok with us doing this.
253 * path_ok() uses enter_repo() and does whitelist checking.
254 * We only need to make sure the repository is exported.
257 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
258 logerror("'%s': repository not exported.", path
);
264 * We'll ignore SIGTERM from now on, we have a
267 signal(SIGTERM
, SIG_IGN
);
269 snprintf(timeout_buf
, sizeof timeout_buf
, "--timeout=%u", timeout
);
271 /* git-upload-pack only ever reads stuff, so this is safe */
272 execl_git_cmd("upload-pack", "--strict", timeout_buf
, ".", NULL
);
276 static int execute(struct sockaddr
*addr
)
278 static char line
[1000];
282 char addrbuf
[256] = "";
285 if (addr
->sa_family
== AF_INET
) {
286 struct sockaddr_in
*sin_addr
= (void *) addr
;
287 inet_ntop(addr
->sa_family
, &sin_addr
->sin_addr
, addrbuf
, sizeof(addrbuf
));
288 port
= sin_addr
->sin_port
;
290 } else if (addr
&& addr
->sa_family
== AF_INET6
) {
291 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
294 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
295 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
, sizeof(addrbuf
) - 1);
298 port
= sin6_addr
->sin6_port
;
301 loginfo("Connection from %s:%d", addrbuf
, port
);
304 alarm(init_timeout
? init_timeout
: timeout
);
305 pktlen
= packet_read_line(0, line
, sizeof(line
));
310 loginfo("Extended attributes (%d bytes) exist <%.*s>",
312 (int) pktlen
- len
, line
+ len
+ 1);
313 if (len
&& line
[len
-1] == '\n')
316 if (!strncmp("git-upload-pack ", line
, 16))
317 return upload(line
+16);
319 logerror("Protocol error: '%s'", line
);
325 * We count spawned/reaped separately, just to avoid any
326 * races when updating them from signals. The SIGCHLD handler
327 * will only update children_reaped, and the fork logic will
328 * only update children_spawned.
330 * MAX_CHILDREN should be a power-of-two to make the modulus
331 * operation cheap. It should also be at least twice
332 * the maximum number of connections we will ever allow.
334 #define MAX_CHILDREN 128
336 static int max_connections
= 25;
338 /* These are updated by the signal handler */
339 static volatile unsigned int children_reaped
;
340 static pid_t dead_child
[MAX_CHILDREN
];
342 /* These are updated by the main loop */
343 static unsigned int children_spawned
;
344 static unsigned int children_deleted
;
346 static struct child
{
349 struct sockaddr_storage address
;
350 } live_child
[MAX_CHILDREN
];
352 static void add_child(int idx
, pid_t pid
, struct sockaddr
*addr
, int addrlen
)
354 live_child
[idx
].pid
= pid
;
355 live_child
[idx
].addrlen
= addrlen
;
356 memcpy(&live_child
[idx
].address
, addr
, addrlen
);
360 * Walk from "deleted" to "spawned", and remove child "pid".
362 * We move everything up by one, since the new "deleted" will
365 static void remove_child(pid_t pid
, unsigned deleted
, unsigned spawned
)
369 deleted
%= MAX_CHILDREN
;
370 spawned
%= MAX_CHILDREN
;
371 if (live_child
[deleted
].pid
== pid
) {
372 live_child
[deleted
].pid
= -1;
375 n
= live_child
[deleted
];
378 deleted
= (deleted
+ 1) % MAX_CHILDREN
;
379 if (deleted
== spawned
)
380 die("could not find dead child %d\n", pid
);
381 m
= live_child
[deleted
];
382 live_child
[deleted
] = n
;
390 * This gets called if the number of connections grows
391 * past "max_connections".
393 * We _should_ start off by searching for connections
394 * from the same IP, and if there is some address wth
395 * multiple connections, we should kill that first.
397 * As it is, we just "randomly" kill 25% of the connections,
398 * and our pseudo-random generator sucks too. I have no
401 * Really, this is just a place-holder for a _real_ algorithm.
403 static void kill_some_children(int signo
, unsigned start
, unsigned stop
)
405 start
%= MAX_CHILDREN
;
406 stop
%= MAX_CHILDREN
;
407 while (start
!= stop
) {
409 kill(live_child
[start
].pid
, signo
);
410 start
= (start
+ 1) % MAX_CHILDREN
;
414 static void check_max_connections(void)
418 unsigned spawned
, reaped
, deleted
;
420 spawned
= children_spawned
;
421 reaped
= children_reaped
;
422 deleted
= children_deleted
;
424 while (deleted
< reaped
) {
425 pid_t pid
= dead_child
[deleted
% MAX_CHILDREN
];
426 remove_child(pid
, deleted
, spawned
);
429 children_deleted
= deleted
;
431 active
= spawned
- deleted
;
432 if (active
<= max_connections
)
435 /* Kill some unstarted connections with SIGTERM */
436 kill_some_children(SIGTERM
, deleted
, spawned
);
437 if (active
<= max_connections
<< 1)
440 /* If the SIGTERM thing isn't helping use SIGKILL */
441 kill_some_children(SIGKILL
, deleted
, spawned
);
446 static void handle(int incoming
, struct sockaddr
*addr
, int addrlen
)
457 idx
= children_spawned
% MAX_CHILDREN
;
459 add_child(idx
, pid
, addr
, addrlen
);
461 check_max_connections();
472 static void child_handler(int signo
)
476 pid_t pid
= waitpid(-1, &status
, WNOHANG
);
479 unsigned reaped
= children_reaped
;
480 dead_child
[reaped
% MAX_CHILDREN
] = pid
;
481 children_reaped
= reaped
+ 1;
482 /* XXX: Custom logging, since we don't wanna getpid() */
484 const char *dead
= "";
485 if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0)
486 dead
= " (with error)";
488 syslog(LOG_INFO
, "[%d] Disconnected%s", pid
, dead
);
490 fprintf(stderr
, "[%d] Disconnected%s\n", pid
, dead
);
498 static int set_reuse_addr(int sockfd
)
504 return setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
510 static int socksetup(int port
, int **socklist_p
)
512 int socknum
= 0, *socklist
= NULL
;
514 char pbuf
[NI_MAXSERV
];
516 struct addrinfo hints
, *ai0
, *ai
;
519 sprintf(pbuf
, "%d", port
);
520 memset(&hints
, 0, sizeof(hints
));
521 hints
.ai_family
= AF_UNSPEC
;
522 hints
.ai_socktype
= SOCK_STREAM
;
523 hints
.ai_protocol
= IPPROTO_TCP
;
524 hints
.ai_flags
= AI_PASSIVE
;
526 gai
= getaddrinfo(NULL
, pbuf
, &hints
, &ai0
);
528 die("getaddrinfo() failed: %s\n", gai_strerror(gai
));
530 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
533 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
536 if (sockfd
>= FD_SETSIZE
) {
537 error("too large socket descriptor.");
543 if (ai
->ai_family
== AF_INET6
) {
545 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
547 /* Note: error is not fatal */
551 if (set_reuse_addr(sockfd
)) {
556 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
558 continue; /* not fatal */
560 if (listen(sockfd
, 5) < 0) {
562 continue; /* not fatal */
565 socklist
= xrealloc(socklist
, sizeof(int) * (socknum
+ 1));
566 socklist
[socknum
++] = sockfd
;
574 *socklist_p
= socklist
;
580 static int socksetup(int port
, int **socklist_p
)
582 struct sockaddr_in sin
;
585 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
589 memset(&sin
, 0, sizeof sin
);
590 sin
.sin_family
= AF_INET
;
591 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
592 sin
.sin_port
= htons(port
);
594 if (set_reuse_addr(sockfd
)) {
599 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
604 if (listen(sockfd
, 5) < 0) {
609 *socklist_p
= xmalloc(sizeof(int));
610 **socklist_p
= sockfd
;
616 static int service_loop(int socknum
, int *socklist
)
621 pfd
= xcalloc(socknum
, sizeof(struct pollfd
));
623 for (i
= 0; i
< socknum
; i
++) {
624 pfd
[i
].fd
= socklist
[i
];
625 pfd
[i
].events
= POLLIN
;
628 signal(SIGCHLD
, child_handler
);
633 if (poll(pfd
, socknum
, -1) < 0) {
634 if (errno
!= EINTR
) {
635 error("poll failed, resuming: %s",
642 for (i
= 0; i
< socknum
; i
++) {
643 if (pfd
[i
].revents
& POLLIN
) {
644 struct sockaddr_storage ss
;
645 unsigned int sslen
= sizeof(ss
);
646 int incoming
= accept(pfd
[i
].fd
, (struct sockaddr
*)&ss
, &sslen
);
654 die("accept returned %s", strerror(errno
));
657 handle(incoming
, (struct sockaddr
*)&ss
, sslen
);
663 /* if any standard file descriptor is missing open it to /dev/null */
664 static void sanitize_stdfds(void)
666 int fd
= open("/dev/null", O_RDWR
, 0);
667 while (fd
!= -1 && fd
< 2)
670 die("open /dev/null or dup failed: %s", strerror(errno
));
675 static void daemonize(void)
681 die("fork failed: %s", strerror(errno
));
686 die("setsid failed: %s", strerror(errno
));
693 static void store_pid(const char *path
)
695 FILE *f
= fopen(path
, "w");
697 die("cannot open pid file %s: %s", path
, strerror(errno
));
698 fprintf(f
, "%d\n", getpid());
702 static int serve(int port
, struct passwd
*pass
, gid_t gid
)
704 int socknum
, *socklist
;
706 socknum
= socksetup(port
, &socklist
);
708 die("unable to allocate any listen sockets on port %u", port
);
711 (initgroups(pass
->pw_name
, gid
) || setgid (gid
) ||
712 setuid(pass
->pw_uid
)))
713 die("cannot drop privileges");
715 return service_loop(socknum
, socklist
);
718 int main(int argc
, char **argv
)
720 int port
= DEFAULT_GIT_PORT
;
722 const char *pid_file
= NULL
, *user_name
= NULL
, *group_name
= NULL
;
724 struct passwd
*pass
= NULL
;
729 /* Without this we cannot rely on waitpid() to tell
730 * what happened to our children.
732 signal(SIGCHLD
, SIG_DFL
);
734 for (i
= 1; i
< argc
; i
++) {
737 if (!strncmp(arg
, "--port=", 7)) {
740 n
= strtoul(arg
+7, &end
, 0);
741 if (arg
[7] && !*end
) {
746 if (!strcmp(arg
, "--inetd")) {
751 if (!strcmp(arg
, "--verbose")) {
755 if (!strcmp(arg
, "--syslog")) {
759 if (!strcmp(arg
, "--export-all")) {
760 export_all_trees
= 1;
763 if (!strncmp(arg
, "--timeout=", 10)) {
764 timeout
= atoi(arg
+10);
767 if (!strncmp(arg
, "--init-timeout=", 15)) {
768 init_timeout
= atoi(arg
+15);
771 if (!strcmp(arg
, "--strict-paths")) {
775 if (!strncmp(arg
, "--base-path=", 12)) {
779 if (!strcmp(arg
, "--reuseaddr")) {
783 if (!strcmp(arg
, "--user-path")) {
787 if (!strncmp(arg
, "--user-path=", 12)) {
788 user_path
= arg
+ 12;
791 if (!strncmp(arg
, "--pid-file=", 11)) {
795 if (!strcmp(arg
, "--detach")) {
800 if (!strncmp(arg
, "--user=", 7)) {
804 if (!strncmp(arg
, "--group=", 8)) {
805 group_name
= arg
+ 8;
808 if (!strcmp(arg
, "--")) {
809 ok_paths
= &argv
[i
+1];
811 } else if (arg
[0] != '-') {
819 if (inetd_mode
&& (group_name
|| user_name
))
820 die("--user and --group are incompatible with --inetd");
822 if (group_name
&& !user_name
)
823 die("--group supplied without --user");
826 pass
= getpwnam(user_name
);
828 die("user not found - %s", user_name
);
833 group
= getgrnam(group_name
);
835 die("group not found - %s", group_name
);
842 openlog("git-daemon", 0, LOG_DAEMON
);
843 set_die_routine(daemon_die
);
846 if (strict_paths
&& (!ok_paths
|| !*ok_paths
))
847 die("option --strict-paths requires a whitelist");
850 struct sockaddr_storage ss
;
851 struct sockaddr
*peer
= (struct sockaddr
*)&ss
;
852 socklen_t slen
= sizeof(ss
);
854 freopen("/dev/null", "w", stderr
);
856 if (getpeername(0, peer
, &slen
))
859 return execute(peer
);
870 return serve(port
, pass
, gid
);