]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
Ignore funny refname sent from remote
[thirdparty/git.git] / connect.c
CommitLineData
f7192598 1#include "cache.h"
41cb7488 2#include "pkt-line.h"
b10d0ec7 3#include "quote.h"
f7192598 4#include <sys/wait.h>
2386d658
LT
5#include <sys/socket.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <netdb.h>
f7192598 9
d1c133f5
LT
10/*
11 * Read all the refs from the other end
12 */
1a7141ff
JH
13struct ref **get_remote_heads(int in, struct ref **list,
14 int nr_match, char **match, int ignore_funny)
d1c133f5
LT
15{
16 *list = NULL;
17 for (;;) {
18 struct ref *ref;
19 unsigned char old_sha1[20];
20 static char buffer[1000];
21 char *name;
22 int len;
23
24 len = packet_read_line(in, buffer, sizeof(buffer));
25 if (!len)
26 break;
27 if (buffer[len-1] == '\n')
28 buffer[--len] = 0;
29
30 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
31 die("protocol error: expected sha/ref, got '%s'", buffer);
32 name = buffer + 41;
1a7141ff
JH
33
34 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
35 check_ref_format(name + 5))
36 continue;
37
d1c133f5
LT
38 if (nr_match && !path_match(name, nr_match, match))
39 continue;
f88395ac 40 ref = xcalloc(1, sizeof(*ref) + len - 40);
d1c133f5 41 memcpy(ref->old_sha1, old_sha1, 20);
d1c133f5 42 memcpy(ref->name, buffer + 41, len - 40);
d1c133f5
LT
43 *list = ref;
44 list = &ref->next;
45 }
46 return list;
47}
48
41cb7488
LT
49int get_ack(int fd, unsigned char *result_sha1)
50{
51 static char line[1000];
52 int len = packet_read_line(fd, line, sizeof(line));
53
54 if (!len)
55 die("git-fetch-pack: expected ACK/NAK, got EOF");
56 if (line[len-1] == '\n')
57 line[--len] = 0;
58 if (!strcmp(line, "NAK"))
59 return 0;
60 if (!strncmp(line, "ACK ", 3)) {
61 if (!get_sha1_hex(line+4, result_sha1))
62 return 1;
63 }
64 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
65}
66
013e7c7f
LT
67int path_match(const char *path, int nr, char **match)
68{
69 int i;
70 int pathlen = strlen(path);
71
72 for (i = 0; i < nr; i++) {
73 char *s = match[i];
74 int len = strlen(s);
75
76 if (!len || len > pathlen)
77 continue;
78 if (memcmp(path + pathlen - len, s, len))
79 continue;
80 if (pathlen > len && path[pathlen - len - 1] != '/')
81 continue;
82 *s = 0;
83 return 1;
84 }
85 return 0;
86}
87
f88395ac
JH
88struct refspec {
89 char *src;
90 char *dst;
ff27adf3 91 char force;
f88395ac
JH
92};
93
ff27adf3
JH
94/*
95 * A:B means fast forward remote B with local A.
96 * +A:B means overwrite remote B with local A.
97 * +A is a shorthand for +A:A.
98 * A is a shorthand for A:A.
99 */
f88395ac
JH
100static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
101{
102 int i;
ff27adf3 103 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
f88395ac
JH
104 for (i = 0; i < nr_refspec; i++) {
105 char *sp, *dp, *ep;
106 sp = refspec[i];
ff27adf3
JH
107 if (*sp == '+') {
108 rs[i].force = 1;
109 sp++;
110 }
f88395ac
JH
111 ep = strchr(sp, ':');
112 if (ep) {
113 dp = ep + 1;
114 *ep = 0;
115 }
116 else
117 dp = sp;
118 rs[i].src = sp;
119 rs[i].dst = dp;
120 }
121 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
122 return rs;
123}
124
125static int count_refspec_match(const char *pattern,
126 struct ref *refs,
127 struct ref **matched_ref)
128{
129 int match;
130 int patlen = strlen(pattern);
131
132 for (match = 0; refs; refs = refs->next) {
133 char *name = refs->name;
134 int namelen = strlen(name);
135 if (namelen < patlen ||
136 memcmp(name + namelen - patlen, pattern, patlen))
137 continue;
138 if (namelen != patlen && name[namelen - patlen - 1] != '/')
139 continue;
140 match++;
141 *matched_ref = refs;
142 }
143 return match;
144}
145
146static void link_dst_tail(struct ref *ref, struct ref ***tail)
147{
148 **tail = ref;
149 *tail = &ref->next;
150 **tail = NULL;
151}
152
15e02b37
JH
153static struct ref *try_explicit_object_name(const char *name)
154{
155 unsigned char sha1[20];
156 struct ref *ref;
157 int len;
158 if (get_sha1(name, sha1))
159 return NULL;
160 len = strlen(name) + 1;
161 ref = xcalloc(1, sizeof(*ref) + len);
162 memcpy(ref->name, name, len);
163 memcpy(ref->new_sha1, sha1, 20);
164 return ref;
165}
166
f88395ac
JH
167static int match_explicit_refs(struct ref *src, struct ref *dst,
168 struct ref ***dst_tail, struct refspec *rs)
169{
170 int i, errs;
171 for (i = errs = 0; rs[i].src; i++) {
172 struct ref *matched_src, *matched_dst;
173
174 matched_src = matched_dst = NULL;
175 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
176 case 1:
177 break;
178 case 0:
15e02b37
JH
179 /* The source could be in the get_sha1() format
180 * not a reference name.
181 */
182 matched_src = try_explicit_object_name(rs[i].src);
183 if (matched_src)
184 break;
f88395ac 185 errs = 1;
4ec99bf0
TS
186 error("src refspec %s does not match any.",
187 rs[i].src);
f88395ac
JH
188 break;
189 default:
190 errs = 1;
191 error("src refspec %s matches more than one.",
192 rs[i].src);
193 break;
194 }
195 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
196 case 1:
197 break;
198 case 0:
199 if (!memcmp(rs[i].dst, "refs/", 5)) {
200 int len = strlen(rs[i].dst) + 1;
201 matched_dst = xcalloc(1, sizeof(*dst) + len);
202 memcpy(matched_dst->name, rs[i].dst, len);
203 link_dst_tail(matched_dst, dst_tail);
204 }
205 else if (!strcmp(rs[i].src, rs[i].dst) &&
206 matched_src) {
207 /* pushing "master:master" when
208 * remote does not have master yet.
209 */
4fa1604f 210 int len = strlen(matched_src->name) + 1;
f88395ac
JH
211 matched_dst = xcalloc(1, sizeof(*dst) + len);
212 memcpy(matched_dst->name, matched_src->name,
213 len);
214 link_dst_tail(matched_dst, dst_tail);
215 }
216 else {
217 errs = 1;
218 error("dst refspec %s does not match any "
219 "existing ref on the remote and does "
220 "not start with refs/.", rs[i].dst);
221 }
222 break;
223 default:
224 errs = 1;
225 error("dst refspec %s matches more than one.",
226 rs[i].dst);
227 break;
228 }
229 if (errs)
230 continue;
f88395ac
JH
231 if (matched_dst->peer_ref) {
232 errs = 1;
233 error("dst ref %s receives from more than one src.",
234 matched_dst->name);
235 }
ff27adf3 236 else {
f88395ac 237 matched_dst->peer_ref = matched_src;
ff27adf3
JH
238 matched_dst->force = rs[i].force;
239 }
f88395ac
JH
240 }
241 return -errs;
242}
243
244static struct ref *find_ref_by_name(struct ref *list, const char *name)
245{
246 for ( ; list; list = list->next)
247 if (!strcmp(list->name, name))
248 return list;
249 return NULL;
250}
251
252int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
253 int nr_refspec, char **refspec, int all)
254{
255 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
256
257 if (nr_refspec)
258 return match_explicit_refs(src, dst, dst_tail, rs);
259
260 /* pick the remainder */
261 for ( ; src; src = src->next) {
262 struct ref *dst_peer;
263 if (src->peer_ref)
264 continue;
265 dst_peer = find_ref_by_name(dst, src->name);
635d37af 266 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
f88395ac
JH
267 continue;
268 if (!dst_peer) {
f88395ac
JH
269 /* Create a new one and link it */
270 int len = strlen(src->name) + 1;
271 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
272 memcpy(dst_peer->name, src->name, len);
273 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
274 link_dst_tail(dst_peer, dst_tail);
275 }
276 dst_peer->peer_ref = src;
277 }
278 return 0;
279}
280
2386d658
LT
281enum protocol {
282 PROTO_LOCAL = 1,
283 PROTO_SSH,
284 PROTO_GIT,
285};
286
287static enum protocol get_protocol(const char *name)
288{
289 if (!strcmp(name, "ssh"))
290 return PROTO_SSH;
291 if (!strcmp(name, "git"))
292 return PROTO_GIT;
c05186cc
LT
293 if (!strcmp(name, "git+ssh"))
294 return PROTO_SSH;
295 if (!strcmp(name, "ssh+git"))
296 return PROTO_SSH;
2386d658
LT
297 die("I don't handle protocol '%s'", name);
298}
299
5ba88448
YH
300#define STR_(s) # s
301#define STR(s) STR_(s)
2386d658 302
49744d63 303#ifndef NO_IPV6
4c505f71 304
2386d658
LT
305static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
306{
5ba88448
YH
307 int sockfd = -1;
308 char *colon, *end;
309 char *port = STR(DEFAULT_GIT_PORT);
310 struct addrinfo hints, *ai0, *ai;
311 int gai;
312
313 if (host[0] == '[') {
314 end = strchr(host + 1, ']');
315 if (end) {
316 *end = 0;
317 end++;
318 host++;
319 } else
320 end = host;
321 } else
322 end = host;
323 colon = strchr(end, ':');
324
ce6f8e7e
LT
325 if (colon) {
326 *colon = 0;
5ba88448 327 port = colon + 1;
ce6f8e7e 328 }
5ba88448
YH
329
330 memset(&hints, 0, sizeof(hints));
331 hints.ai_socktype = SOCK_STREAM;
332 hints.ai_protocol = IPPROTO_TCP;
333
334 gai = getaddrinfo(host, port, &hints, &ai);
335 if (gai)
336 die("Unable to look up %s (%s)", host, gai_strerror(gai));
337
338 for (ai0 = ai; ai; ai = ai->ai_next) {
339 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
340 if (sockfd < 0)
341 continue;
342 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
343 close(sockfd);
344 sockfd = -1;
345 continue;
2386d658 346 }
5ba88448 347 break;
2386d658
LT
348 }
349
5ba88448 350 freeaddrinfo(ai0);
2386d658 351
2386d658 352 if (sockfd < 0)
5ba88448
YH
353 die("unable to connect a socket (%s)", strerror(errno));
354
2386d658
LT
355 fd[0] = sockfd;
356 fd[1] = sockfd;
357 packet_write(sockfd, "%s %s\n", prog, path);
358 return 0;
359}
360
49744d63 361#else /* NO_IPV6 */
4c505f71
PA
362
363static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
364{
365 int sockfd = -1;
366 char *colon, *end;
367 char *port = STR(DEFAULT_GIT_PORT), *ep;
368 struct hostent *he;
369 struct sockaddr_in sa;
370 char **ap;
371 unsigned int nport;
372
373 if (host[0] == '[') {
374 end = strchr(host + 1, ']');
375 if (end) {
376 *end = 0;
377 end++;
378 host++;
379 } else
380 end = host;
381 } else
382 end = host;
383 colon = strchr(end, ':');
384
385 if (colon) {
386 *colon = 0;
387 port = colon + 1;
388 }
389
390
391 he = gethostbyname(host);
392 if (!he)
393 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
394 nport = strtoul(port, &ep, 10);
395 if ( ep == port || *ep ) {
396 /* Not numeric */
397 struct servent *se = getservbyname(port,"tcp");
398 if ( !se )
399 die("Unknown port %s\n", port);
400 nport = se->s_port;
401 }
402
403 for (ap = he->h_addr_list; *ap; ap++) {
404 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
405 if (sockfd < 0)
406 continue;
407
408 memset(&sa, 0, sizeof sa);
409 sa.sin_family = he->h_addrtype;
6573faff 410 sa.sin_port = htons(nport);
4c505f71
PA
411 memcpy(&sa.sin_addr, ap, he->h_length);
412
413 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
414 close(sockfd);
415 sockfd = -1;
416 continue;
417 }
418 break;
419 }
420
421 if (sockfd < 0)
422 die("unable to connect a socket (%s)", strerror(errno));
423
424 fd[0] = sockfd;
425 fd[1] = sockfd;
426 packet_write(sockfd, "%s %s\n", prog, path);
427 return 0;
428}
429
49744d63 430#endif /* NO_IPV6 */
4c505f71 431
f7192598
LT
432/*
433 * Yeah, yeah, fixme. Need to pass in the heads etc.
434 */
435int git_connect(int fd[2], char *url, const char *prog)
436{
437 char command[1024];
2386d658 438 char *host, *path;
f7192598
LT
439 char *colon;
440 int pipefd[2][2];
441 pid_t pid;
2386d658 442 enum protocol protocol;
f7192598 443
f7192598
LT
444 host = NULL;
445 path = url;
446 colon = strchr(url, ':');
2386d658 447 protocol = PROTO_LOCAL;
f7192598
LT
448 if (colon) {
449 *colon = 0;
450 host = url;
451 path = colon+1;
2386d658
LT
452 protocol = PROTO_SSH;
453 if (!memcmp(path, "//", 2)) {
454 char *slash = strchr(path + 2, '/');
455 if (slash) {
456 int nr = slash - path - 2;
457 memmove(path, path+2, nr);
458 path[nr] = 0;
459 protocol = get_protocol(url);
460 host = path;
461 path = slash;
462 }
463 }
f7192598 464 }
2386d658
LT
465
466 if (protocol == PROTO_GIT)
467 return git_tcp_connect(fd, prog, host, path);
468
f7192598
LT
469 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
470 die("unable to create pipe pair for communication");
471 pid = fork();
472 if (!pid) {
b10d0ec7
JH
473 snprintf(command, sizeof(command), "%s %s", prog,
474 sq_quote(path));
f7192598
LT
475 dup2(pipefd[1][0], 0);
476 dup2(pipefd[0][1], 1);
477 close(pipefd[0][0]);
478 close(pipefd[0][1]);
479 close(pipefd[1][0]);
480 close(pipefd[1][1]);
4852f723 481 if (protocol == PROTO_SSH) {
c7c81b3a
JR
482 const char *ssh, *ssh_basename;
483 ssh = getenv("GIT_SSH");
484 if (!ssh) ssh = "ssh";
485 ssh_basename = strrchr(ssh, '/');
4852f723
MS
486 if (!ssh_basename)
487 ssh_basename = ssh;
488 else
489 ssh_basename++;
490 execlp(ssh, ssh_basename, host, command, NULL);
491 }
f7192598
LT
492 else
493 execlp("sh", "sh", "-c", command, NULL);
494 die("exec failed");
495 }
496 fd[0] = pipefd[0][0];
497 fd[1] = pipefd[1][1];
498 close(pipefd[0][1]);
499 close(pipefd[1][0]);
500 return pid;
501}
502
503int finish_connect(pid_t pid)
504{
505 int ret;
506
507 for (;;) {
508 ret = waitpid(pid, NULL, 0);
509 if (!ret)
510 break;
511 if (errno != EINTR)
512 break;
513 }
514 return ret;
515}