4 #include "run-command.h"
10 #define HOST_NAME_MAX 256
17 static int log_syslog
;
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] [--detach] [--pid-file=file]\n"
28 " [--[enable|disable|allow-override|forbid-override]=service]\n"
29 " [--inetd | [--listen=host_or_ipaddr] [--port=n]\n"
30 " [--user=user [--group=group]]\n"
33 /* List of acceptable pathname prefixes */
34 static char **ok_paths
;
35 static int strict_paths
;
37 /* If this is set, git-daemon-export-ok is not required */
38 static int export_all_trees
;
40 /* Take all paths relative to this one if non-NULL */
41 static char *base_path
;
42 static char *interpolated_path
;
43 static int base_path_relaxed
;
45 /* Flag indicating client sent extra args. */
46 static int saw_extended_args
;
48 /* If defined, ~user notation is allowed and the string is inserted
49 * after ~user/. E.g. a request to git://host/~alice/frotz would
50 * go to /home/alice/pub_git/frotz with --user-path=pub_git.
52 static const char *user_path
;
54 /* Timeout, and initial timeout */
55 static unsigned int timeout
;
56 static unsigned int init_timeout
;
58 static char *hostname
;
59 static char *canon_hostname
;
60 static char *ip_address
;
61 static char *tcp_port
;
63 static void logreport(int priority
, const char *err
, va_list params
)
67 vsnprintf(buf
, sizeof(buf
), err
, params
);
68 syslog(priority
, "%s", buf
);
71 * Since stderr is set to linebuffered mode, the
72 * logging of different processes will not overlap
74 fprintf(stderr
, "[%"PRIuMAX
"] ", (uintmax_t)getpid());
75 vfprintf(stderr
, err
, params
);
80 __attribute__((format (printf
, 1, 2)))
81 static void logerror(const char *err
, ...)
84 va_start(params
, err
);
85 logreport(LOG_ERR
, err
, params
);
89 __attribute__((format (printf
, 1, 2)))
90 static void loginfo(const char *err
, ...)
95 va_start(params
, err
);
96 logreport(LOG_INFO
, err
, params
);
100 static void NORETURN
daemon_die(const char *err
, va_list params
)
102 logreport(LOG_ERR
, err
, params
);
106 static char *path_ok(char *directory
)
108 static char rpath
[PATH_MAX
];
109 static char interp_path
[PATH_MAX
];
115 if (daemon_avoid_alias(dir
)) {
116 logerror("'%s': aliased", dir
);
122 logerror("'%s': User-path not allowed", dir
);
126 /* Got either "~alice" or "~alice/foo";
127 * rewrite them to "~alice/%s" or
130 int namlen
, restlen
= strlen(dir
);
131 char *slash
= strchr(dir
, '/');
133 slash
= dir
+ restlen
;
134 namlen
= slash
- dir
;
136 loginfo("userpath <%s>, request <%s>, namlen %d, restlen %d, slash <%s>", user_path
, dir
, namlen
, restlen
, slash
);
137 snprintf(rpath
, PATH_MAX
, "%.*s/%s%.*s",
138 namlen
, dir
, user_path
, restlen
, slash
);
142 else if (interpolated_path
&& saw_extended_args
) {
143 struct strbuf expanded_path
= STRBUF_INIT
;
144 struct strbuf_expand_dict_entry dict
[] = {
146 { "CH", canon_hostname
},
147 { "IP", ip_address
},
154 /* Allow only absolute */
155 logerror("'%s': Non-absolute path denied (interpolated-path active)", dir
);
159 strbuf_expand(&expanded_path
, interpolated_path
,
160 strbuf_expand_dict_cb
, &dict
);
161 strlcpy(interp_path
, expanded_path
.buf
, PATH_MAX
);
162 strbuf_release(&expanded_path
);
163 loginfo("Interpolated dir '%s'", interp_path
);
167 else if (base_path
) {
169 /* Allow only absolute */
170 logerror("'%s': Non-absolute path denied (base-path active)", dir
);
173 snprintf(rpath
, PATH_MAX
, "%s%s", base_path
, dir
);
177 path
= enter_repo(dir
, strict_paths
);
178 if (!path
&& base_path
&& base_path_relaxed
) {
180 * if we fail and base_path_relaxed is enabled, try without
181 * prefixing the base path
184 path
= enter_repo(dir
, strict_paths
);
188 logerror("'%s' does not appear to be a git repository", dir
);
192 if ( ok_paths
&& *ok_paths
) {
194 int pathlen
= strlen(path
);
196 /* The validation is done on the paths after enter_repo
197 * appends optional {.git,.git/.git} and friends, but
198 * it does not use getcwd(). So if your /pub is
199 * a symlink to /mnt/pub, you can whitelist /pub and
200 * do not have to say /mnt/pub.
203 for ( pp
= ok_paths
; *pp
; pp
++ ) {
204 int len
= strlen(*pp
);
205 if (len
<= pathlen
&&
206 !memcmp(*pp
, path
, len
) &&
207 (path
[len
] == '\0' ||
208 (!strict_paths
&& path
[len
] == '/')))
213 /* be backwards compatible */
218 logerror("'%s': not in whitelist", path
);
219 return NULL
; /* Fallthrough. Deny by default */
222 typedef int (*daemon_service_fn
)(void);
223 struct daemon_service
{
225 const char *config_name
;
226 daemon_service_fn fn
;
231 static struct daemon_service
*service_looking_at
;
232 static int service_enabled
;
234 static int git_daemon_config(const char *var
, const char *value
, void *cb
)
236 if (!prefixcmp(var
, "daemon.") &&
237 !strcmp(var
+ 7, service_looking_at
->config_name
)) {
238 service_enabled
= git_config_bool(var
, value
);
242 /* we are not interested in parsing any other configuration here */
246 static int run_service(char *dir
, struct daemon_service
*service
)
249 int enabled
= service
->enabled
;
251 loginfo("Request %s for '%s'", service
->name
, dir
);
253 if (!enabled
&& !service
->overridable
) {
254 logerror("'%s': service not enabled.", service
->name
);
259 if (!(path
= path_ok(dir
)))
263 * Security on the cheap.
265 * We want a readable HEAD, usable "objects" directory, and
266 * a "git-daemon-export-ok" flag that says that the other side
267 * is ok with us doing this.
269 * path_ok() uses enter_repo() and does whitelist checking.
270 * We only need to make sure the repository is exported.
273 if (!export_all_trees
&& access("git-daemon-export-ok", F_OK
)) {
274 logerror("'%s': repository not exported.", path
);
279 if (service
->overridable
) {
280 service_looking_at
= service
;
281 service_enabled
= -1;
282 git_config(git_daemon_config
, NULL
);
283 if (0 <= service_enabled
)
284 enabled
= service_enabled
;
287 logerror("'%s': service not enabled for '%s'",
288 service
->name
, path
);
294 * We'll ignore SIGTERM from now on, we have a
297 signal(SIGTERM
, SIG_IGN
);
299 return service
->fn();
302 static void copy_to_log(int fd
)
304 struct strbuf line
= STRBUF_INIT
;
307 fp
= fdopen(fd
, "r");
309 logerror("fdopen of error channel failed");
314 while (strbuf_getline(&line
, fp
, '\n') != EOF
) {
315 logerror("%s", line
.buf
);
316 strbuf_setlen(&line
, 0);
319 strbuf_release(&line
);
323 static int run_service_command(const char **argv
)
325 struct child_process cld
;
327 memset(&cld
, 0, sizeof(cld
));
331 if (start_command(&cld
))
337 copy_to_log(cld
.err
);
339 return finish_command(&cld
);
342 static int upload_pack(void)
344 /* Timeout as string */
345 char timeout_buf
[64];
346 const char *argv
[] = { "upload-pack", "--strict", timeout_buf
, ".", NULL
};
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
++)
403 * Read the host as supplied by the client connection.
405 static void parse_host_arg(char *extra_args
, int buflen
)
409 char *end
= extra_args
+ buflen
;
411 if (extra_args
< end
&& *extra_args
) {
412 saw_extended_args
= 1;
413 if (strncasecmp("host=", extra_args
, 5) == 0) {
414 val
= extra_args
+ 5;
415 vallen
= strlen(val
) + 1;
417 /* Split <host>:<port> at colon. */
419 char *port
= strrchr(host
, ':');
424 tcp_port
= xstrdup(port
);
427 hostname
= xstrdup_tolower(host
);
430 /* On to the next one */
431 extra_args
= val
+ vallen
;
433 if (extra_args
< end
&& *extra_args
)
434 die("Invalid request");
438 * Locate canonical hostname and its IP address.
442 struct addrinfo hints
;
445 static char addrbuf
[HOST_NAME_MAX
+ 1];
447 memset(&hints
, 0, sizeof(hints
));
448 hints
.ai_flags
= AI_CANONNAME
;
450 gai
= getaddrinfo(hostname
, NULL
, &hints
, &ai
);
452 struct sockaddr_in
*sin_addr
= (void *)ai
->ai_addr
;
454 inet_ntop(AF_INET
, &sin_addr
->sin_addr
,
455 addrbuf
, sizeof(addrbuf
));
457 ip_address
= xstrdup(addrbuf
);
459 free(canon_hostname
);
460 canon_hostname
= xstrdup(ai
->ai_canonname
?
461 ai
->ai_canonname
: ip_address
);
466 struct hostent
*hent
;
467 struct sockaddr_in sa
;
469 static char addrbuf
[HOST_NAME_MAX
+ 1];
471 hent
= gethostbyname(hostname
);
473 ap
= hent
->h_addr_list
;
474 memset(&sa
, 0, sizeof sa
);
475 sa
.sin_family
= hent
->h_addrtype
;
476 sa
.sin_port
= htons(0);
477 memcpy(&sa
.sin_addr
, *ap
, hent
->h_length
);
479 inet_ntop(hent
->h_addrtype
, &sa
.sin_addr
,
480 addrbuf
, sizeof(addrbuf
));
482 free(canon_hostname
);
483 canon_hostname
= xstrdup(hent
->h_name
);
485 ip_address
= xstrdup(addrbuf
);
491 static int execute(struct sockaddr
*addr
)
493 static char line
[1000];
497 char addrbuf
[256] = "";
500 if (addr
->sa_family
== AF_INET
) {
501 struct sockaddr_in
*sin_addr
= (void *) addr
;
502 inet_ntop(addr
->sa_family
, &sin_addr
->sin_addr
, addrbuf
, sizeof(addrbuf
));
503 port
= ntohs(sin_addr
->sin_port
);
505 } else if (addr
&& addr
->sa_family
== AF_INET6
) {
506 struct sockaddr_in6
*sin6_addr
= (void *) addr
;
509 *buf
++ = '['; *buf
= '\0'; /* stpcpy() is cool */
510 inet_ntop(AF_INET6
, &sin6_addr
->sin6_addr
, buf
, sizeof(addrbuf
) - 1);
513 port
= ntohs(sin6_addr
->sin6_port
);
516 loginfo("Connection from %s:%d", addrbuf
, port
);
517 setenv("REMOTE_ADDR", addrbuf
, 1);
520 unsetenv("REMOTE_ADDR");
523 alarm(init_timeout
? init_timeout
: timeout
);
524 pktlen
= packet_read_line(0, line
, sizeof(line
));
529 loginfo("Extended attributes (%d bytes) exist <%.*s>",
531 (int) pktlen
- len
, line
+ len
+ 1);
532 if (len
&& line
[len
-1] == '\n') {
538 free(canon_hostname
);
541 hostname
= canon_hostname
= ip_address
= tcp_port
= NULL
;
544 parse_host_arg(line
+ len
+ 1, pktlen
- len
- 1);
546 for (i
= 0; i
< ARRAY_SIZE(daemon_service
); i
++) {
547 struct daemon_service
*s
= &(daemon_service
[i
]);
548 int namelen
= strlen(s
->name
);
549 if (!prefixcmp(line
, "git-") &&
550 !strncmp(s
->name
, line
+ 4, namelen
) &&
551 line
[namelen
+ 4] == ' ') {
553 * Note: The directory here is probably context sensitive,
554 * and might depend on the actual service being performed.
556 return run_service(line
+ namelen
+ 5, s
);
560 logerror("Protocol error: '%s'", line
);
564 static int addrcmp(const struct sockaddr_storage
*s1
,
565 const struct sockaddr_storage
*s2
)
567 if (s1
->ss_family
!= s2
->ss_family
)
568 return s1
->ss_family
- s2
->ss_family
;
569 if (s1
->ss_family
== AF_INET
)
570 return memcmp(&((struct sockaddr_in
*)s1
)->sin_addr
,
571 &((struct sockaddr_in
*)s2
)->sin_addr
,
572 sizeof(struct in_addr
));
574 if (s1
->ss_family
== AF_INET6
)
575 return memcmp(&((struct sockaddr_in6
*)s1
)->sin6_addr
,
576 &((struct sockaddr_in6
*)s2
)->sin6_addr
,
577 sizeof(struct in6_addr
));
582 static int max_connections
= 32;
584 static unsigned int live_children
;
586 static struct child
{
589 struct sockaddr_storage address
;
592 static void add_child(pid_t pid
, struct sockaddr
*addr
, int addrlen
)
594 struct child
*newborn
, **cradle
;
596 newborn
= xcalloc(1, sizeof(*newborn
));
599 memcpy(&newborn
->address
, addr
, addrlen
);
600 for (cradle
= &firstborn
; *cradle
; cradle
= &(*cradle
)->next
)
601 if (!addrcmp(&(*cradle
)->address
, &newborn
->address
))
603 newborn
->next
= *cradle
;
607 static void remove_child(pid_t pid
)
609 struct child
**cradle
, *blanket
;
611 for (cradle
= &firstborn
; (blanket
= *cradle
); cradle
= &blanket
->next
)
612 if (blanket
->pid
== pid
) {
613 *cradle
= blanket
->next
;
621 * This gets called if the number of connections grows
622 * past "max_connections".
624 * We kill the newest connection from a duplicate IP.
626 static void kill_some_child(void)
628 const struct child
*blanket
, *next
;
630 if (!(blanket
= firstborn
))
633 for (; (next
= blanket
->next
); blanket
= next
)
634 if (!addrcmp(&blanket
->address
, &next
->address
)) {
635 kill(blanket
->pid
, SIGTERM
);
640 static void check_dead_children(void)
645 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
646 const char *dead
= "";
648 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) > 0))
649 dead
= " (with error)";
650 loginfo("[%"PRIuMAX
"] Disconnected%s", (uintmax_t)pid
, dead
);
654 static void handle(int incoming
, struct sockaddr
*addr
, int addrlen
)
658 if (max_connections
&& live_children
>= max_connections
) {
660 sleep(1); /* give it some time to die */
661 check_dead_children();
662 if (live_children
>= max_connections
) {
664 logerror("Too many children, dropping connection");
669 if ((pid
= fork())) {
672 logerror("Couldn't fork %s", strerror(errno
));
676 add_child(pid
, addr
, addrlen
);
687 static void child_handler(int signo
)
690 * Otherwise empty handler because systemcalls will get interrupted
691 * upon signal receipt
692 * SysV needs the handler to be rearmed
694 signal(SIGCHLD
, child_handler
);
697 static int set_reuse_addr(int sockfd
)
703 return setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
709 static int socksetup(char *listen_addr
, int listen_port
, int **socklist_p
)
711 int socknum
= 0, *socklist
= NULL
;
713 char pbuf
[NI_MAXSERV
];
714 struct addrinfo hints
, *ai0
, *ai
;
718 sprintf(pbuf
, "%d", listen_port
);
719 memset(&hints
, 0, sizeof(hints
));
720 hints
.ai_family
= AF_UNSPEC
;
721 hints
.ai_socktype
= SOCK_STREAM
;
722 hints
.ai_protocol
= IPPROTO_TCP
;
723 hints
.ai_flags
= AI_PASSIVE
;
725 gai
= getaddrinfo(listen_addr
, pbuf
, &hints
, &ai0
);
727 die("getaddrinfo() failed: %s", gai_strerror(gai
));
729 for (ai
= ai0
; ai
; ai
= ai
->ai_next
) {
732 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
735 if (sockfd
>= FD_SETSIZE
) {
736 logerror("Socket descriptor too large");
742 if (ai
->ai_family
== AF_INET6
) {
744 setsockopt(sockfd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
746 /* Note: error is not fatal */
750 if (set_reuse_addr(sockfd
)) {
755 if (bind(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
757 continue; /* not fatal */
759 if (listen(sockfd
, 5) < 0) {
761 continue; /* not fatal */
764 flags
= fcntl(sockfd
, F_GETFD
, 0);
766 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
768 socklist
= xrealloc(socklist
, sizeof(int) * (socknum
+ 1));
769 socklist
[socknum
++] = sockfd
;
777 *socklist_p
= socklist
;
783 static int socksetup(char *listen_addr
, int listen_port
, int **socklist_p
)
785 struct sockaddr_in sin
;
789 memset(&sin
, 0, sizeof sin
);
790 sin
.sin_family
= AF_INET
;
791 sin
.sin_port
= htons(listen_port
);
794 /* Well, host better be an IP address here. */
795 if (inet_pton(AF_INET
, listen_addr
, &sin
.sin_addr
.s_addr
) <= 0)
798 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
801 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
805 if (set_reuse_addr(sockfd
)) {
810 if ( bind(sockfd
, (struct sockaddr
*)&sin
, sizeof sin
) < 0 ) {
815 if (listen(sockfd
, 5) < 0) {
820 flags
= fcntl(sockfd
, F_GETFD
, 0);
822 fcntl(sockfd
, F_SETFD
, flags
| FD_CLOEXEC
);
824 *socklist_p
= xmalloc(sizeof(int));
825 **socklist_p
= sockfd
;
831 static int service_loop(int socknum
, int *socklist
)
836 pfd
= xcalloc(socknum
, sizeof(struct pollfd
));
838 for (i
= 0; i
< socknum
; i
++) {
839 pfd
[i
].fd
= socklist
[i
];
840 pfd
[i
].events
= POLLIN
;
843 signal(SIGCHLD
, child_handler
);
848 check_dead_children();
850 if (poll(pfd
, socknum
, -1) < 0) {
851 if (errno
!= EINTR
) {
852 logerror("Poll failed, resuming: %s",
859 for (i
= 0; i
< socknum
; i
++) {
860 if (pfd
[i
].revents
& POLLIN
) {
861 struct sockaddr_storage ss
;
862 unsigned int sslen
= sizeof(ss
);
863 int incoming
= accept(pfd
[i
].fd
, (struct sockaddr
*)&ss
, &sslen
);
871 die_errno("accept returned");
874 handle(incoming
, (struct sockaddr
*)&ss
, sslen
);
880 /* if any standard file descriptor is missing open it to /dev/null */
881 static void sanitize_stdfds(void)
883 int fd
= open("/dev/null", O_RDWR
, 0);
884 while (fd
!= -1 && fd
< 2)
887 die_errno("open /dev/null or dup failed");
892 static void daemonize(void)
898 die_errno("fork failed");
903 die_errno("setsid failed");
910 static void store_pid(const char *path
)
912 FILE *f
= fopen(path
, "w");
914 die_errno("cannot open pid file '%s'", path
);
915 if (fprintf(f
, "%"PRIuMAX
"\n", (uintmax_t) getpid()) < 0 || fclose(f
) != 0)
916 die_errno("failed to write pid file '%s'", path
);
919 static int serve(char *listen_addr
, int listen_port
, struct passwd
*pass
, gid_t gid
)
921 int socknum
, *socklist
;
923 socknum
= socksetup(listen_addr
, listen_port
, &socklist
);
925 die("unable to allocate any listen sockets on host %s port %u",
926 listen_addr
, listen_port
);
929 (initgroups(pass
->pw_name
, gid
) || setgid (gid
) ||
930 setuid(pass
->pw_uid
)))
931 die("cannot drop privileges");
933 return service_loop(socknum
, socklist
);
936 int main(int argc
, char **argv
)
939 char *listen_addr
= NULL
;
941 const char *pid_file
= NULL
, *user_name
= NULL
, *group_name
= NULL
;
943 struct passwd
*pass
= NULL
;
948 git_extract_argv0_path(argv
[0]);
950 for (i
= 1; i
< argc
; i
++) {
953 if (!prefixcmp(arg
, "--listen=")) {
954 listen_addr
= xstrdup_tolower(arg
+ 9);
957 if (!prefixcmp(arg
, "--port=")) {
960 n
= strtoul(arg
+7, &end
, 0);
961 if (arg
[7] && !*end
) {
966 if (!strcmp(arg
, "--inetd")) {
971 if (!strcmp(arg
, "--verbose")) {
975 if (!strcmp(arg
, "--syslog")) {
979 if (!strcmp(arg
, "--export-all")) {
980 export_all_trees
= 1;
983 if (!prefixcmp(arg
, "--timeout=")) {
984 timeout
= atoi(arg
+10);
987 if (!prefixcmp(arg
, "--init-timeout=")) {
988 init_timeout
= atoi(arg
+15);
991 if (!prefixcmp(arg
, "--max-connections=")) {
992 max_connections
= atoi(arg
+18);
993 if (max_connections
< 0)
994 max_connections
= 0; /* unlimited */
997 if (!strcmp(arg
, "--strict-paths")) {
1001 if (!prefixcmp(arg
, "--base-path=")) {
1005 if (!strcmp(arg
, "--base-path-relaxed")) {
1006 base_path_relaxed
= 1;
1009 if (!prefixcmp(arg
, "--interpolated-path=")) {
1010 interpolated_path
= arg
+20;
1013 if (!strcmp(arg
, "--reuseaddr")) {
1017 if (!strcmp(arg
, "--user-path")) {
1021 if (!prefixcmp(arg
, "--user-path=")) {
1022 user_path
= arg
+ 12;
1025 if (!prefixcmp(arg
, "--pid-file=")) {
1026 pid_file
= arg
+ 11;
1029 if (!strcmp(arg
, "--detach")) {
1034 if (!prefixcmp(arg
, "--user=")) {
1035 user_name
= arg
+ 7;
1038 if (!prefixcmp(arg
, "--group=")) {
1039 group_name
= arg
+ 8;
1042 if (!prefixcmp(arg
, "--enable=")) {
1043 enable_service(arg
+ 9, 1);
1046 if (!prefixcmp(arg
, "--disable=")) {
1047 enable_service(arg
+ 10, 0);
1050 if (!prefixcmp(arg
, "--allow-override=")) {
1051 make_service_overridable(arg
+ 17, 1);
1054 if (!prefixcmp(arg
, "--forbid-override=")) {
1055 make_service_overridable(arg
+ 18, 0);
1058 if (!strcmp(arg
, "--")) {
1059 ok_paths
= &argv
[i
+1];
1061 } else if (arg
[0] != '-') {
1062 ok_paths
= &argv
[i
];
1066 usage(daemon_usage
);
1070 openlog("git-daemon", LOG_PID
, LOG_DAEMON
);
1071 set_die_routine(daemon_die
);
1073 /* avoid splitting a message in the middle */
1074 setvbuf(stderr
, NULL
, _IOLBF
, 0);
1076 if (inetd_mode
&& (group_name
|| user_name
))
1077 die("--user and --group are incompatible with --inetd");
1079 if (inetd_mode
&& (listen_port
|| listen_addr
))
1080 die("--listen= and --port= are incompatible with --inetd");
1081 else if (listen_port
== 0)
1082 listen_port
= DEFAULT_GIT_PORT
;
1084 if (group_name
&& !user_name
)
1085 die("--group supplied without --user");
1088 pass
= getpwnam(user_name
);
1090 die("user not found - %s", user_name
);
1095 group
= getgrnam(group_name
);
1097 die("group not found - %s", group_name
);
1099 gid
= group
->gr_gid
;
1103 if (strict_paths
&& (!ok_paths
|| !*ok_paths
))
1104 die("option --strict-paths requires a whitelist");
1106 if (base_path
&& !is_directory(base_path
))
1107 die("base-path '%s' does not exist or is not a directory",
1111 struct sockaddr_storage ss
;
1112 struct sockaddr
*peer
= (struct sockaddr
*)&ss
;
1113 socklen_t slen
= sizeof(ss
);
1115 if (!freopen("/dev/null", "w", stderr
))
1116 die_errno("failed to redirect stderr to /dev/null");
1118 if (getpeername(0, peer
, &slen
))
1121 return execute(peer
);
1126 loginfo("Ready to rumble");
1132 store_pid(pid_file
);
1134 return serve(listen_addr
, listen_port
, pass
, gid
);