1#include "cache.h"
2#include "pkt-line.h"
3#include "quote.h"
4#include <sys/wait.h>
5#include <sys/socket.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <netdb.h>
9
10/*
11 * Read all the refs from the other end
12 */
13struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match)
14{
15 *list = NULL;
16 for (;;) {
17 struct ref *ref;
18 unsigned char old_sha1[20];
19 static char buffer[1000];
20 char *name;
21 int len;
22
23 len = packet_read_line(in, buffer, sizeof(buffer));
24 if (!len)
25 break;
26 if (buffer[len-1] == '\n')
27 buffer[--len] = 0;
28
29 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
30 die("protocol error: expected sha/ref, got '%s'", buffer);
31 name = buffer + 41;
32 if (nr_match && !path_match(name, nr_match, match))
33 continue;
34 ref = xmalloc(sizeof(*ref) + len - 40);
35 memcpy(ref->old_sha1, old_sha1, 20);
36 memset(ref->new_sha1, 0, 20);
37 memcpy(ref->name, buffer + 41, len - 40);
38 ref->next = NULL;
39 *list = ref;
40 list = &ref->next;
41 }
42 return list;
43}
44
45int get_ack(int fd, unsigned char *result_sha1)
46{
47 static char line[1000];
48 int len = packet_read_line(fd, line, sizeof(line));
49
50 if (!len)
51 die("git-fetch-pack: expected ACK/NAK, got EOF");
52 if (line[len-1] == '\n')
53 line[--len] = 0;
54 if (!strcmp(line, "NAK"))
55 return 0;
56 if (!strncmp(line, "ACK ", 3)) {
57 if (!get_sha1_hex(line+4, result_sha1))
58 return 1;
59 }
60 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
61}
62
63int path_match(const char *path, int nr, char **match)
64{
65 int i;
66 int pathlen = strlen(path);
67
68 for (i = 0; i < nr; i++) {
69 char *s = match[i];
70 int len = strlen(s);
71
72 if (!len || len > pathlen)
73 continue;
74 if (memcmp(path + pathlen - len, s, len))
75 continue;
76 if (pathlen > len && path[pathlen - len - 1] != '/')
77 continue;
78 *s = 0;
79 return 1;
80 }
81 return 0;
82}
83
84enum protocol {
85 PROTO_LOCAL = 1,
86 PROTO_SSH,
87 PROTO_GIT,
88};
89
90static enum protocol get_protocol(const char *name)
91{
92 if (!strcmp(name, "ssh"))
93 return PROTO_SSH;
94 if (!strcmp(name, "git"))
95 return PROTO_GIT;
96 die("I don't handle protocol '%s'", name);
97}
98
99static void lookup_host(const char *host, struct sockaddr *in)
100{
101 struct addrinfo *res;
102 int ret;
103
104 ret = getaddrinfo(host, NULL, NULL, &res);
105 if (ret)
106 die("Unable to look up %s (%s)", host, gai_strerror(ret));
107 *in = *res->ai_addr;
108 freeaddrinfo(res);
109}
110
111static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
112{
113 struct sockaddr addr;
114 int port = DEFAULT_GIT_PORT, sockfd;
115 char *colon;
116
117 colon = strchr(host, ':');
118 if (colon) {
119 char *end;
120 unsigned long n = strtoul(colon+1, &end, 0);
121 if (colon[1] && !*end) {
122 *colon = 0;
123 port = n;
124 }
125 }
126
127 lookup_host(host, &addr);
128 ((struct sockaddr_in *)&addr)->sin_port = htons(port);
129
130 sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
131 if (sockfd < 0)
132 die("unable to create socket (%s)", strerror(errno));
133 if (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)
134 die("unable to connect (%s)", strerror(errno));
135 fd[0] = sockfd;
136 fd[1] = sockfd;
137 packet_write(sockfd, "%s %s\n", prog, path);
138 return 0;
139}
140
141/*
142 * Yeah, yeah, fixme. Need to pass in the heads etc.
143 */
144int git_connect(int fd[2], char *url, const char *prog)
145{
146 char command[1024];
147 char *host, *path;
148 char *colon;
149 int pipefd[2][2];
150 pid_t pid;
151 enum protocol protocol;
152
153 host = NULL;
154 path = url;
155 colon = strchr(url, ':');
156 protocol = PROTO_LOCAL;
157 if (colon) {
158 *colon = 0;
159 host = url;
160 path = colon+1;
161 protocol = PROTO_SSH;
162 if (!memcmp(path, "//", 2)) {
163 char *slash = strchr(path + 2, '/');
164 if (slash) {
165 int nr = slash - path - 2;
166 memmove(path, path+2, nr);
167 path[nr] = 0;
168 protocol = get_protocol(url);
169 host = path;
170 path = slash;
171 }
172 }
173 }
174
175 if (protocol == PROTO_GIT)
176 return git_tcp_connect(fd, prog, host, path);
177
178 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
179 die("unable to create pipe pair for communication");
180 pid = fork();
181 if (!pid) {
182 snprintf(command, sizeof(command), "%s %s", prog,
183 sq_quote(path));
184 dup2(pipefd[1][0], 0);
185 dup2(pipefd[0][1], 1);
186 close(pipefd[0][0]);
187 close(pipefd[0][1]);
188 close(pipefd[1][0]);
189 close(pipefd[1][1]);
190 if (protocol == PROTO_SSH)
191 execlp("ssh", "ssh", host, command, NULL);
192 else
193 execlp("sh", "sh", "-c", command, NULL);
194 die("exec failed");
195 }
196 fd[0] = pipefd[0][0];
197 fd[1] = pipefd[1][1];
198 close(pipefd[0][1]);
199 close(pipefd[1][0]);
200 return pid;
201}
202
203int finish_connect(pid_t pid)
204{
205 int ret;
206
207 for (;;) {
208 ret = waitpid(pid, NULL, 0);
209 if (!ret)
210 break;
211 if (errno != EINTR)
212 break;
213 }
214 return ret;
215}