1#include "cache.h"
2#include "pkt-line.h"
3#include <sys/socket.h>
4#include <netinet/in.h>
5
6static const char daemon_usage[] = "git-daemon [--inetd | --port=n]";
7
8static int upload(char *dir, int dirlen)
9{
10 if (chdir(dir) < 0)
11 return -1;
12 chdir(".git");
13
14 /*
15 * Security on the cheap.
16 *
17 * We want a readable HEAD, usable "objects" directory, and
18 * a "git-daemon-export-ok" flag that says that the other side
19 * is ok with us doing this.
20 */
21 if (access("git-daemon-export-ok", F_OK) ||
22 access("objects/00", X_OK) ||
23 access("HEAD", R_OK))
24 return -1;
25
26 /* git-upload-pack only ever reads stuff, so this is safe */
27 execlp("git-upload-pack", "git-upload-pack", ".", NULL);
28 return -1;
29}
30
31static int execute(void)
32{
33 static char line[1000];
34 int len;
35
36 len = packet_read_line(0, line, sizeof(line));
37
38 if (len && line[len-1] == '\n')
39 line[--len] = 0;
40
41 if (!strncmp("git-upload-pack /", line, 17))
42 return upload(line + 16, len - 16);
43
44 fprintf(stderr, "got bad connection '%s'\n", line);
45 return -1;
46}
47
48static void handle(int incoming, struct sockaddr_in *addr, int addrlen)
49{
50 if (fork()) {
51 close(incoming);
52 return;
53 }
54
55 dup2(incoming, 0);
56 dup2(incoming, 1);
57 close(incoming);
58 exit(execute());
59}
60
61static int serve(int port)
62{
63 int sockfd;
64 struct sockaddr_in addr;
65
66 sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
67 if (sockfd < 0)
68 die("unable to open socket (%s)", strerror(errno));
69 memset(&addr, 0, sizeof(addr));
70 addr.sin_port = htons(port);
71 addr.sin_family = AF_INET;
72 if (bind(sockfd, (void *)&addr, sizeof(addr)) < 0)
73 die("unable to bind to port %d (%s)", port, strerror(errno));
74 if (listen(sockfd, 5) < 0)
75 die("unable to listen to port %d (%s)", port, strerror(errno));
76
77 for (;;) {
78 struct sockaddr_in in;
79 socklen_t addrlen = sizeof(in);
80 int incoming = accept(sockfd, (void *)&in, &addrlen);
81
82 if (incoming < 0) {
83 switch (errno) {
84 case EAGAIN:
85 case EINTR:
86 case ECONNABORTED:
87 continue;
88 default:
89 die("accept returned %s", strerror(errno));
90 }
91 }
92 handle(incoming, &in, addrlen);
93 }
94}
95
96int main(int argc, char **argv)
97{
98 int port = DEFAULT_GIT_PORT;
99 int inetd_mode = 0;
100 int i;
101
102 for (i = 1; i < argc; i++) {
103 char *arg = argv[i];
104
105 if (!strncmp(arg, "--port=", 7)) {
106 char *end;
107 unsigned long n;
108 n = strtoul(arg+7, &end, 0);
109 if (arg[7] && !*end) {
110 port = n;
111 continue;
112 }
113 }
114
115 if (!strcmp(arg, "--inetd")) {
116 inetd_mode = 1;
117 continue;
118 }
119
120 usage(daemon_usage);
121 }
122
123 if (inetd_mode)
124 return execute();
125
126 return serve(port);
127}