4 #include "run-command.h"
6 #include "string-list.h"
9 #define HOST_NAME_MAX 256
16 static int log_syslog
;
20 static const char daemon_usage
[] =
21 "git daemon [--verbose] [--syslog] [--export-all]\n"
22 " [--timeout=<n>] [--init-timeout=<n>] [--max-connections=<n>]\n"
23 " [--strict-paths] [--base-path=<path>] [--base-path-relaxed]\n"
24 " [--user-path | --user-path=<path>]\n"
25 " [--interpolated-path=<path>]\n"
26 " [--reuseaddr] [--detach] [--pid-file=<file>]\n"
27 " [--(enable|disable|allow-override|forbid-override)=<service>]\n"
28 " [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
29 " [--user=<user> [--group=<group>]]\n"
32 /* List of acceptable pathname prefixes */
33 static char **ok_paths
;
34 static int strict_paths
;
36 /* If this is set, git-daemon-export-ok is not required */
37 static int export_all_trees
;
39 /* Take all paths relative to this one if non-NULL */
40 static char *base_path
;
41 static char *interpolated_path
;
42 static int base_path_relaxed
;
44 /* Flag indicating client sent extra args. */
45 static int saw_extended_args
;
47 /* If defined, ~user notation is allowed and the string is inserted
48 * after ~user/. E.g. a request to git://host/~alice/frotz would
49 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
51 static const char *user_path
;
53 /* Timeout, and initial timeout */
54 static unsigned int timeout
;
55 static unsigned int init_timeout
;
57 static char *hostname
;
58 static char *canon_hostname
;
59 static char *ip_address
;
60 static char *tcp_port
;
62 static void logreport(int priority
, const char *err
, va_list params
)
66 vsnprintf(buf
, sizeof(buf
), err
, params
);
67 syslog(priority
, "%s", buf
);
70 * Since stderr is set to linebuffered mode, the
71 * logging of different processes will not overlap
73 fprintf(stderr
, "[%"PRIuMAX
"] ", (uintmax_t)getpid());
74 vfprintf(stderr
, err
, params
);
79 __attribute__((format (printf
, 1, 2)))
80 static void logerror(const char *err
, ...)
83 va_start(params
, err
);
84 logreport(LOG_ERR
, err
, params
);
88 __attribute__((format (printf
, 1, 2)))
89 static void loginfo(const char *err
, ...)
94 va_start(params
, err
);
95 logreport(LOG_INFO
, err
, params
);
99 static void NORETURN
daemon_die(const char *err
, va_list params
)
101 logreport(LOG_ERR
, err
, params
);
105 static char *path_ok(char *directory
)
107 static char rpath
[PATH_MAX
];
108 static char interp_path
[PATH_MAX
];
114 if (daemon_avoid_alias(dir
)) {
115 logerror("'%s': aliased", dir
);
121 logerror("'%s': User-path not allowed", dir
);
125 /* Got either "~alice" or "~alice/foo";
126 * rewrite them to "~alice/%s" or
129 int namlen
, restlen
= strlen(dir
);
130 char *slash
= strchr(dir
, '/');
132 slash
= dir
+ restlen
;
133 namlen
= slash
- dir
;
135 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path
, dir
, namlen
, restlen
, slash
);
136 snprintf(rpath
, PATH_MAX
, "%.*s/%s%.*s",
137 namlen
, dir
, user_path
, restlen
, slash
);
141 else if (interpolated_path
&& saw_extended_args
) {
142 struct strbuf expanded_path
= STRBUF_INIT
;
143 struct strbuf_expand_dict_entry dict
[6];
145 dict
[0].placeholder
= "H"; dict
[0].value
= hostname
;
146 dict
[1].placeholder
= "CH"; dict
[1].value
= canon_hostname
;
147 dict
[2].placeholder
= "IP"; dict
[2].value
= ip_address
;
148 dict
[3].placeholder
= "P"; dict
[3].value
= tcp_port
;
149 dict
[4].placeholder
= "D"; dict
[4].value
= directory
;
150 dict
[5].placeholder
= NULL
; dict
[5].value
= NULL
;
152 /* Allow only absolute */
153 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir
);
157 strbuf_expand(&expanded_path
, interpolated_path
,
158 strbuf_expand_dict_cb
, &dict
);
159 strlcpy(interp_path
, expanded_path
.buf
, PATH_MAX
);
160 strbuf_release(&expanded_path
);
161 loginfo("Interpolated dir '%s'", interp_path
);
165 else if (base_path
) {
167 /* Allow only absolute */
168 logerror("'%s': Non-absolute path denied (base-path active)", dir
);
171 snprintf(rpath
, PATH_MAX
, "%s%s", base_path
, dir
);
175 path
= enter_repo(dir
, strict_paths
);
176 if (!path
&& base_path
&& base_path_relaxed
) {
178 * if we fail and base_path_relaxed is enabled, try without
179 * prefixing the base path
182 path
= enter_repo(dir
, strict_paths
);
186 logerror("'%s' does not appear to be a git repository", dir
);
190 if ( ok_paths
&& *ok_paths
) {
192 int pathlen
= strlen(path
);
194 /* The validation is done on the paths after enter_repo
195 * appends optional {.git,.git/.git} and friends, but
196 * it does not use getcwd(). So if your /pub is
197 * a symlink to /mnt/pub, you can whitelist /pub and
198 * do not have to say /mnt/pub.
201 for ( pp
= ok_paths
; *pp
; pp
++ ) {
202 int len
= strlen(*pp
);
203 if (len
<= pathlen
&&
204 !memcmp(*pp
, path
, len
) &&
205 (path
[len
] == '\0' ||
206 (!strict_paths
&& path
[len
] == '/')))
211 /* be backwards compatible */
216 logerror("'%s': not in whitelist", path
);
217 return NULL
; /* Fallthrough. Deny by default */
220 typedef int (*daemon_service_fn
)(void);
221 struct daemon_service
{
223 const char *config_name
;
224 daemon_service_fn fn
;
229 static struct daemon_service
*service_looking_at
;
230 static int service_enabled
;
232 static int git_daemon_config(const char *var
, const char *value
, void *cb
)
234 if (!prefixcmp(var
, "daemon.") &&
235 !strcmp(var
+ 7, service_looking_at
->config_name
)) {
236 service_enabled
= git_config_bool(var
, value
);
240 /* we are not interested in parsing any other configuration here */
244 static int run_service(char *dir
, struct daemon_service
*service
)
247 int enabled
= service
->enabled
;
249 loginfo("Request %s for '%s'", service
->name
, dir
);
251 if (!enabled
&& !service
->overridable
) {
252 logerror("'%s': service not enabled.", service
->name
);
257 if (!(path
= path_ok(dir
)))
261 * Security on the cheap.
263 * We want a readable HEAD, usable "objects" directory, and
264 * a "git-daemon-export-ok" flag that says that the other side
265 * is ok with us doing this.
267 * path_ok() uses enter_repo() and does whitelist checking.
268 * We only need to make sure the repository is exported.
271 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
272 logerror("'%s': repository not exported.", path
);
277 if (service
->overridable
) {
278 service_looking_at
= service
;
279 service_enabled
= -1;
280 git_config(git_daemon_config
, NULL
);
281 if (0 <= service_enabled
)
282 enabled
= service_enabled
;
285 logerror("'%s': service not enabled for '%s'",
286 service
->name
, path
);
292 * We'll ignore SIGTERM from now on, we have a
295 signal(SIGTERM
, SIG_IGN
);
297 return service
->fn();
300 static void copy_to_log(int fd
)
302 struct strbuf line
= STRBUF_INIT
;
305 fp
= fdopen(fd
, "r");
307 logerror("fdopen of error channel failed");
312 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
313 logerror("%s", line
.buf
);
314 strbuf_setlen(&line
, 0);
317 strbuf_release(&line
);
321 static int run_service_command(const char **argv
)
323 struct child_process cld
;
325 memset(&cld
, 0, sizeof(cld
));
329 if (start_command(&cld
))
335 copy_to_log(cld
.err
);
337 return finish_command(&cld
);
340 static int upload_pack(void)
342 /* Timeout as string */
343 char timeout_buf
[64];
344 const char *argv
[] = { "upload-pack", "--strict", NULL
, ".", NULL
};
346 argv
[2] = timeout_buf
;
348 snprintf(timeout_buf
, sizeof timeout_buf
, "--timeout=%u", timeout
);
349 return run_service_command(argv
);
352 static int upload_archive(void)
354 static const char *argv
[] = { "upload-archive", ".", NULL
};
355 return run_service_command(argv
);
358 static int receive_pack(void)
360 static const char *argv
[] = { "receive-pack", ".", NULL
};
361 return run_service_command(argv
);
364 static struct daemon_service daemon_service
[] = {
365 { "upload-archive", "uploadarch", upload_archive
, 0, 1 },
366 { "upload-pack", "uploadpack", upload_pack
, 1, 1 },
367 { "receive-pack", "receivepack", receive_pack
, 0, 1 },
370 static void enable_service(const char *name
, int ena
)
373 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
374 if (!strcmp(daemon_service
[i
].name
, name
)) {
375 daemon_service
[i
].enabled
= ena
;
379 die("No such service %s", name
);
382 static void make_service_overridable(const char *name
, int ena
)
385 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
386 if (!strcmp(daemon_service
[i
].name
, name
)) {
387 daemon_service
[i
].overridable
= ena
;
391 die("No such service %s", name
);
394 static char *xstrdup_tolower(const char *str
)
396 char *p
, *dup
= xstrdup(str
);
397 for (p
= dup
; *p
; p
++)
402 static void parse_host_and_port(char *hostport
, char **host
,
405 if (*hostport
== '[') {
408 end
= strchr(hostport
, ']');
410 die("Invalid request ('[' without ']')");
412 *host
= hostport
+ 1;
415 else if (end
[1] == ':')
418 die("Garbage after end of host part");
421 *port
= strrchr(hostport
, ':');
430 * Read the host as supplied by the client connection.
432 static void parse_host_arg(char *extra_args
, int buflen
)
436 char *end
= extra_args
+ buflen
;
438 if (extra_args
< end
&& *extra_args
) {
439 saw_extended_args
= 1;
440 if (strncasecmp("host=", extra_args
, 5) == 0) {
441 val
= extra_args
+ 5;
442 vallen
= strlen(val
) + 1;
444 /* Split <host>:<port> at colon. */
447 parse_host_and_port(val
, &host
, &port
);
450 tcp_port
= xstrdup(port
);
453 hostname
= xstrdup_tolower(host
);
456 /* On to the next one */
457 extra_args
= val
+ vallen
;
459 if (extra_args
< end
&& *extra_args
)
460 die("Invalid request");
464 * Locate canonical hostname and its IP address.
468 struct addrinfo hints
;
471 static char addrbuf
[HOST_NAME_MAX
+ 1];
473 memset(&hints
, 0, sizeof(hints
));
474 hints
.ai_flags
= AI_CANONNAME
;
476 gai
= getaddrinfo(hostname
, NULL
, &hints
, &ai
);
478 struct sockaddr_in
*sin_addr
= (void *)ai
->ai_addr
;
480 inet_ntop(AF_INET
, &sin_addr
->sin_addr
,
481 addrbuf
, sizeof(addrbuf
));
483 ip_address
= xstrdup(addrbuf
);
485 free(canon_hostname
);
486 canon_hostname
= xstrdup(ai
->ai_canonname
?
487 ai
->ai_canonname
: ip_address
);
492 struct hostent
*hent
;
493 struct sockaddr_in sa
;
495 static char addrbuf
[HOST_NAME_MAX
+ 1];
497 hent
= gethostbyname(hostname
);
499 ap
= hent
->h_addr_list
;
500 memset(&sa
, 0, sizeof sa
);
501 sa
.sin_family
= hent
->h_addrtype
;
502 sa
.sin_port
= htons(0);
503 memcpy(&sa
.sin_addr
, *ap
, hent
->h_length
);
505 inet_ntop(hent
->h_addrtype
, &sa
.sin_addr
,
506 addrbuf
, sizeof(addrbuf
));
508 free(canon_hostname
);
509 canon_hostname
= xstrdup(hent
->h_name
);
511 ip_address
= xstrdup(addrbuf
);
517 static int execute(struct sockaddr
*addr
)
519 static char line
[1000];
523 char addrbuf
[256] = "";
526 if (addr
->sa_family
== AF_INET
) {
527 struct sockaddr_in
*sin_addr
= (void *) addr
;
528 inet_ntop(addr
->sa_family
, &sin_addr
->sin_addr
, addrbuf
, sizeof(addrbuf
));
529 port
= ntohs(sin_addr
->sin_port
);
531 } else if (addr
&& addr
->sa_family
== AF_INET6
) {
532 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
535 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
536 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
, sizeof(addrbuf
) - 1);
539 port
= ntohs(sin6_addr
->sin6_port
);
542 loginfo("Connection from %s:%d", addrbuf
, port
);
543 setenv("REMOTE_ADDR", addrbuf
, 1);
546 unsetenv("REMOTE_ADDR");
549 alarm(init_timeout
? init_timeout
: timeout
);
550 pktlen
= packet_read_line(0, line
, sizeof(line
));
555 loginfo("Extended attributes (%d bytes) exist <%.*s>",
557 (int) pktlen
- len
, line
+ len
+ 1);
558 if (len
&& line
[len
-1] == '\n') {
564 free(canon_hostname
);
567 hostname
= canon_hostname
= ip_address
= tcp_port
= NULL
;
570 parse_host_arg(line
+ len
+ 1, pktlen
- len
- 1);
572 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
573 struct daemon_service
*s
= &(daemon_service
[i
]);
574 int namelen
= strlen(s
->name
);
575 if (!prefixcmp(line
, "git-") &&
576 !strncmp(s
->name
, line
+ 4, namelen
) &&
577 line
[namelen
+ 4] == ' ') {
579 * Note: The directory here is probably context sensitive,
580 * and might depend on the actual service being performed.
582 return run_service(line
+ namelen
+ 5, s
);
586 logerror("Protocol error: '%s'", line
);
590 static int addrcmp(const struct sockaddr_storage
*s1
,
591 const struct sockaddr_storage
*s2
)
593 const struct sockaddr
*sa1
= (const struct sockaddr
*) s1
;
594 const struct sockaddr
*sa2
= (const struct sockaddr
*) s2
;
596 if (sa1
->sa_family
!= sa2
->sa_family
)
597 return sa1
->sa_family
- sa2
->sa_family
;
598 if (sa1
->sa_family
== AF_INET
)
599 return memcmp(&((struct sockaddr_in
*)s1
)->sin_addr
,
600 &((struct sockaddr_in
*)s2
)->sin_addr
,
601 sizeof(struct in_addr
));
603 if (sa1
->sa_family
== AF_INET6
)
604 return memcmp(&((struct sockaddr_in6
*)s1
)->sin6_addr
,
605 &((struct sockaddr_in6
*)s2
)->sin6_addr
,
606 sizeof(struct in6_addr
));
611 static int max_connections
= 32;
613 static unsigned int live_children
;
615 static struct child
{
617 struct child_process cld
;
618 struct sockaddr_storage address
;
621 static void add_child(struct child_process
*cld
, struct sockaddr
*addr
, int addrlen
)
623 struct child
*newborn
, **cradle
;
625 newborn
= xcalloc(1, sizeof(*newborn
));
627 memcpy(&newborn
->cld
, cld
, sizeof(*cld
));
628 memcpy(&newborn
->address
, addr
, addrlen
);
629 for (cradle
= &firstborn
; *cradle
; cradle
= &(*cradle
)->next
)
630 if (!addrcmp(&(*cradle
)->address
, &newborn
->address
))
632 newborn
->next
= *cradle
;
637 * This gets called if the number of connections grows
638 * past "max_connections".
640 * We kill the newest connection from a duplicate IP.
642 static void kill_some_child(void)
644 const struct child
*blanket
, *next
;
646 if (!(blanket
= firstborn
))
649 for (; (next
= blanket
->next
); blanket
= next
)
650 if (!addrcmp(&blanket
->address
, &next
->address
)) {
651 kill(blanket
->cld
.pid
, SIGTERM
);
656 static void check_dead_children(void)
661 struct child
**cradle
, *blanket
;
662 for (cradle
= &firstborn
; (blanket
= *cradle
);)
663 if ((pid
= waitpid(blanket
->cld
.pid
, &status
, WNOHANG
)) > 1) {
664 const char *dead
= "";
666 dead
= " (with error)";
667 loginfo("[%"PRIuMAX
"] Disconnected%s", (uintmax_t)pid
, dead
);
669 /* remove the child */
670 *cradle
= blanket
->next
;
674 cradle
= &blanket
->next
;
677 static char **cld_argv
;
678 static void handle(int incoming
, struct sockaddr
*addr
, int addrlen
)
680 struct child_process cld
= { 0 };
682 if (max_connections
&& live_children
>= max_connections
) {
684 sleep(1); /* give it some time to die */
685 check_dead_children();
686 if (live_children
>= max_connections
) {
688 logerror("Too many children, dropping connection");
693 cld
.argv
= (const char **)cld_argv
;
695 cld
.out
= dup(incoming
);
697 if (start_command(&cld
))
698 logerror("unable to fork");
700 add_child(&cld
, addr
, addrlen
);
704 static void child_handler(int signo
)
707 * Otherwise empty handler because systemcalls will get interrupted
708 * upon signal receipt
709 * SysV needs the handler to be rearmed
711 signal(SIGCHLD
, child_handler
);
714 static int set_reuse_addr(int sockfd
)
720 return setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
732 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
736 char pbuf
[NI_MAXSERV
];
737 struct addrinfo hints
, *ai0
, *ai
;
741 sprintf(pbuf
, "%d", listen_port
);
742 memset(&hints
, 0, sizeof(hints
));
743 hints
.ai_family
= AF_UNSPEC
;
744 hints
.ai_socktype
= SOCK_STREAM
;
745 hints
.ai_protocol
= IPPROTO_TCP
;
746 hints
.ai_flags
= AI_PASSIVE
;
748 gai
= getaddrinfo(listen_addr
, pbuf
, &hints
, &ai0
);
750 logerror("getaddrinfo() for %s failed: %s", listen_addr
, gai_strerror(gai
));
754 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
757 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
760 if (sockfd
>= FD_SETSIZE
) {
761 logerror("Socket descriptor too large");
767 if (ai
->ai_family
== AF_INET6
) {
769 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
771 /* Note: error is not fatal */
775 if (set_reuse_addr(sockfd
)) {
780 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
782 continue; /* not fatal */
784 if (listen(sockfd
, 5) < 0) {
786 continue; /* not fatal */
789 flags
= fcntl(sockfd
, F_GETFD
, 0);
791 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
793 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
794 socklist
->list
[socklist
->nr
++] = sockfd
;
808 static int setup_named_sock(char *listen_addr
, int listen_port
, struct socketlist
*socklist
)
810 struct sockaddr_in sin
;
814 memset(&sin
, 0, sizeof sin
);
815 sin
.sin_family
= AF_INET
;
816 sin
.sin_port
= htons(listen_port
);
819 /* Well, host better be an IP address here. */
820 if (inet_pton(AF_INET
, listen_addr
, &sin
.sin_addr
.s_addr
) <= 0)
823 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
826 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
830 if (set_reuse_addr(sockfd
)) {
835 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
840 if (listen(sockfd
, 5) < 0) {
845 flags
= fcntl(sockfd
, F_GETFD
, 0);
847 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
849 ALLOC_GROW(socklist
->list
, socklist
->nr
+ 1, socklist
->alloc
);
850 socklist
->list
[socklist
->nr
++] = sockfd
;
856 static void socksetup(struct string_list
*listen_addr
, int listen_port
, struct socketlist
*socklist
)
858 if (!listen_addr
->nr
)
859 setup_named_sock(NULL
, listen_port
, socklist
);
862 for (i
= 0; i
< listen_addr
->nr
; i
++) {
863 socknum
= setup_named_sock(listen_addr
->items
[i
].string
,
864 listen_port
, socklist
);
867 logerror("unable to allocate any listen sockets for host %s on port %u",
868 listen_addr
->items
[i
].string
, listen_port
);
873 static int service_loop(struct socketlist
*socklist
)
878 pfd
= xcalloc(socklist
->nr
, sizeof(struct pollfd
));
880 for (i
= 0; i
< socklist
->nr
; i
++) {
881 pfd
[i
].fd
= socklist
->list
[i
];
882 pfd
[i
].events
= POLLIN
;
885 signal(SIGCHLD
, child_handler
);
890 check_dead_children();
892 if (poll(pfd
, socklist
->nr
, -1) < 0) {
893 if (errno
!= EINTR
) {
894 logerror("Poll failed, resuming: %s",
901 for (i
= 0; i
< socklist
->nr
; i
++) {
902 if (pfd
[i
].revents
& POLLIN
) {
903 struct sockaddr_storage ss
;
904 unsigned int sslen
= sizeof(ss
);
905 int incoming
= accept(pfd
[i
].fd
, (struct sockaddr
*)&ss
, &sslen
);
913 die_errno("accept returned");
916 handle(incoming
, (struct sockaddr
*)&ss
, sslen
);
922 /* if any standard file descriptor is missing open it to /dev/null */
923 static void sanitize_stdfds(void)
925 int fd
= open("/dev/null", O_RDWR
, 0);
926 while (fd
!= -1 && fd
< 2)
929 die_errno("open /dev/null or dup failed");
934 static void daemonize(void)
940 die_errno("fork failed");
945 die_errno("setsid failed");
952 static void store_pid(const char *path
)
954 FILE *f
= fopen(path
, "w");
956 die_errno("cannot open pid file '%s'", path
);
957 if (fprintf(f
, "%"PRIuMAX
"\n", (uintmax_t) getpid()) < 0 || fclose(f
) != 0)
958 die_errno("failed to write pid file '%s'", path
);
961 static int serve(struct string_list
*listen_addr
, int listen_port
, struct passwd
*pass
, gid_t gid
)
963 struct socketlist socklist
= { NULL
, 0, 0 };
965 socksetup(listen_addr
, listen_port
, &socklist
);
966 if (socklist
.nr
== 0)
967 die("unable to allocate any listen sockets on port %u",
971 (initgroups(pass
->pw_name
, gid
) || setgid (gid
) ||
972 setuid(pass
->pw_uid
)))
973 die("cannot drop privileges");
975 return service_loop(&socklist
);
978 int main(int argc
, char **argv
)
981 struct string_list listen_addr
= STRING_LIST_INIT_NODUP
;
982 int serve_mode
= 0, inetd_mode
= 0;
983 const char *pid_file
= NULL
, *user_name
= NULL
, *group_name
= NULL
;
985 struct passwd
*pass
= NULL
;
990 git_extract_argv0_path(argv
[0]);
992 for (i
= 1; i
< argc
; i
++) {
995 if (!prefixcmp(arg
, "--listen=")) {
996 string_list_append(&listen_addr
, xstrdup_tolower(arg
+ 9));
999 if (!prefixcmp(arg
, "--port=")) {
1002 n
= strtoul(arg
+7, &end
, 0);
1003 if (arg
[7] && !*end
) {
1008 if (!strcmp(arg
, "--serve")) {
1012 if (!strcmp(arg
, "--inetd")) {
1017 if (!strcmp(arg
, "--verbose")) {
1021 if (!strcmp(arg
, "--syslog")) {
1025 if (!strcmp(arg
, "--export-all")) {
1026 export_all_trees
= 1;
1029 if (!prefixcmp(arg
, "--timeout=")) {
1030 timeout
= atoi(arg
+10);
1033 if (!prefixcmp(arg
, "--init-timeout=")) {
1034 init_timeout
= atoi(arg
+15);
1037 if (!prefixcmp(arg
, "--max-connections=")) {
1038 max_connections
= atoi(arg
+18);
1039 if (max_connections
< 0)
1040 max_connections
= 0; /* unlimited */
1043 if (!strcmp(arg
, "--strict-paths")) {
1047 if (!prefixcmp(arg
, "--base-path=")) {
1051 if (!strcmp(arg
, "--base-path-relaxed")) {
1052 base_path_relaxed
= 1;
1055 if (!prefixcmp(arg
, "--interpolated-path=")) {
1056 interpolated_path
= arg
+20;
1059 if (!strcmp(arg
, "--reuseaddr")) {
1063 if (!strcmp(arg
, "--user-path")) {
1067 if (!prefixcmp(arg
, "--user-path=")) {
1068 user_path
= arg
+ 12;
1071 if (!prefixcmp(arg
, "--pid-file=")) {
1072 pid_file
= arg
+ 11;
1075 if (!strcmp(arg
, "--detach")) {
1080 if (!prefixcmp(arg
, "--user=")) {
1081 user_name
= arg
+ 7;
1084 if (!prefixcmp(arg
, "--group=")) {
1085 group_name
= arg
+ 8;
1088 if (!prefixcmp(arg
, "--enable=")) {
1089 enable_service(arg
+ 9, 1);
1092 if (!prefixcmp(arg
, "--disable=")) {
1093 enable_service(arg
+ 10, 0);
1096 if (!prefixcmp(arg
, "--allow-override=")) {
1097 make_service_overridable(arg
+ 17, 1);
1100 if (!prefixcmp(arg
, "--forbid-override=")) {
1101 make_service_overridable(arg
+ 18, 0);
1104 if (!strcmp(arg
, "--")) {
1105 ok_paths
= &argv
[i
+1];
1107 } else if (arg
[0] != '-') {
1108 ok_paths
= &argv
[i
];
1112 usage(daemon_usage
);
1116 openlog("git-daemon", LOG_PID
, LOG_DAEMON
);
1117 set_die_routine(daemon_die
);
1119 /* avoid splitting a message in the middle */
1120 setvbuf(stderr
, NULL
, _IOLBF
, 0);
1122 if (inetd_mode
&& (group_name
|| user_name
))
1123 die("--user and --group are incompatible with --inetd");
1125 if (inetd_mode
&& (listen_port
|| (listen_addr
.nr
> 0)))
1126 die("--listen= and --port= are incompatible with --inetd");
1127 else if (listen_port
== 0)
1128 listen_port
= DEFAULT_GIT_PORT
;
1130 if (group_name
&& !user_name
)
1131 die("--group supplied without --user");
1134 pass
= getpwnam(user_name
);
1136 die("user not found - %s", user_name
);
1141 group
= getgrnam(group_name
);
1143 die("group not found - %s", group_name
);
1145 gid
= group
->gr_gid
;
1149 if (strict_paths
&& (!ok_paths
|| !*ok_paths
))
1150 die("option --strict-paths requires a whitelist");
1152 if (base_path
&& !is_directory(base_path
))
1153 die("base-path '%s' does not exist or is not a directory",
1157 if (!freopen("/dev/null", "w", stderr
))
1158 die_errno("failed to redirect stderr to /dev/null");
1161 if (inetd_mode
|| serve_mode
) {
1162 struct sockaddr_storage ss
;
1163 struct sockaddr
*peer
= (struct sockaddr
*)&ss
;
1164 socklen_t slen
= sizeof(ss
);
1166 if (getpeername(0, peer
, &slen
))
1167 return execute(NULL
);
1169 return execute(peer
);
1174 loginfo("Ready to rumble");
1180 store_pid(pid_file
);
1182 /* prepare argv for serving-processes */
1183 cld_argv
= xmalloc(sizeof (char *) * (argc
+ 2));
1184 for (i
= 0; i
< argc
; ++i
)
1185 cld_argv
[i
] = argv
[i
];
1186 cld_argv
[argc
] = "--serve";
1187 cld_argv
[argc
+1] = NULL
;
1189 return serve(&listen_addr
, listen_port
, pass
, gid
);