git-proxy updates.
[git.git] / connect.c
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "quote.h"
4 #include "refs.h"
5 #include <sys/wait.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <netdb.h>
10
11 static char *server_capabilities = NULL;
12
13 /*
14  * Read all the refs from the other end
15  */
16 struct ref **get_remote_heads(int in, struct ref **list,
17                               int nr_match, char **match, int ignore_funny)
18 {
19         *list = NULL;
20         for (;;) {
21                 struct ref *ref;
22                 unsigned char old_sha1[20];
23                 static char buffer[1000];
24                 char *name;
25                 int len, name_len;
26
27                 len = packet_read_line(in, buffer, sizeof(buffer));
28                 if (!len)
29                         break;
30                 if (buffer[len-1] == '\n')
31                         buffer[--len] = 0;
32
33                 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
34                         die("protocol error: expected sha/ref, got '%s'", buffer);
35                 name = buffer + 41;
36
37                 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
38                     check_ref_format(name + 5))
39                         continue;
40
41                 name_len = strlen(name);
42                 if (len != name_len + 41) {
43                         if (server_capabilities)
44                                 free(server_capabilities);
45                         server_capabilities = strdup(name + name_len + 1);
46                 }
47
48                 if (nr_match && !path_match(name, nr_match, match))
49                         continue;
50                 ref = xcalloc(1, sizeof(*ref) + len - 40);
51                 memcpy(ref->old_sha1, old_sha1, 20);
52                 memcpy(ref->name, buffer + 41, len - 40);
53                 *list = ref;
54                 list = &ref->next;
55         }
56         return list;
57 }
58
59 int server_supports(const char *feature)
60 {
61         return server_capabilities &&
62                 strstr(server_capabilities, feature) != NULL;
63 }
64
65 int get_ack(int fd, unsigned char *result_sha1)
66 {
67         static char line[1000];
68         int len = packet_read_line(fd, line, sizeof(line));
69
70         if (!len)
71                 die("git-fetch-pack: expected ACK/NAK, got EOF");
72         if (line[len-1] == '\n')
73                 line[--len] = 0;
74         if (!strcmp(line, "NAK"))
75                 return 0;
76         if (!strncmp(line, "ACK ", 3)) {
77                 if (!get_sha1_hex(line+4, result_sha1)) {
78                         if (strstr(line+45, "continue"))
79                                 return 2;
80                         return 1;
81                 }
82         }
83         die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
84 }
85
86 int path_match(const char *path, int nr, char **match)
87 {
88         int i;
89         int pathlen = strlen(path);
90
91         for (i = 0; i < nr; i++) {
92                 char *s = match[i];
93                 int len = strlen(s);
94
95                 if (!len || len > pathlen)
96                         continue;
97                 if (memcmp(path + pathlen - len, s, len))
98                         continue;
99                 if (pathlen > len && path[pathlen - len - 1] != '/')
100                         continue;
101                 *s = 0;
102                 return 1;
103         }
104         return 0;
105 }
106
107 struct refspec {
108         char *src;
109         char *dst;
110         char force;
111 };
112
113 /*
114  * A:B means fast forward remote B with local A.
115  * +A:B means overwrite remote B with local A.
116  * +A is a shorthand for +A:A.
117  * A is a shorthand for A:A.
118  */
119 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
120 {
121         int i;
122         struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
123         for (i = 0; i < nr_refspec; i++) {
124                 char *sp, *dp, *ep;
125                 sp = refspec[i];
126                 if (*sp == '+') {
127                         rs[i].force = 1;
128                         sp++;
129                 }
130                 ep = strchr(sp, ':');
131                 if (ep) {
132                         dp = ep + 1;
133                         *ep = 0;
134                 }
135                 else
136                         dp = sp;
137                 rs[i].src = sp;
138                 rs[i].dst = dp;
139         }
140         rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
141         return rs;
142 }
143
144 static int count_refspec_match(const char *pattern,
145                                struct ref *refs,
146                                struct ref **matched_ref)
147 {
148         int match;
149         int patlen = strlen(pattern);
150
151         for (match = 0; refs; refs = refs->next) {
152                 char *name = refs->name;
153                 int namelen = strlen(name);
154                 if (namelen < patlen ||
155                     memcmp(name + namelen - patlen, pattern, patlen))
156                         continue;
157                 if (namelen != patlen && name[namelen - patlen - 1] != '/')
158                         continue;
159                 match++;
160                 *matched_ref = refs;
161         }
162         return match;
163 }
164
165 static void link_dst_tail(struct ref *ref, struct ref ***tail)
166 {
167         **tail = ref;
168         *tail = &ref->next;
169         **tail = NULL;
170 }
171
172 static struct ref *try_explicit_object_name(const char *name)
173 {
174         unsigned char sha1[20];
175         struct ref *ref;
176         int len;
177         if (get_sha1(name, sha1))
178                 return NULL;
179         len = strlen(name) + 1;
180         ref = xcalloc(1, sizeof(*ref) + len);
181         memcpy(ref->name, name, len);
182         memcpy(ref->new_sha1, sha1, 20);
183         return ref;
184 }
185
186 static int match_explicit_refs(struct ref *src, struct ref *dst,
187                                struct ref ***dst_tail, struct refspec *rs)
188 {
189         int i, errs;
190         for (i = errs = 0; rs[i].src; i++) {
191                 struct ref *matched_src, *matched_dst;
192
193                 matched_src = matched_dst = NULL;
194                 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
195                 case 1:
196                         break;
197                 case 0:
198                         /* The source could be in the get_sha1() format
199                          * not a reference name.
200                          */
201                         matched_src = try_explicit_object_name(rs[i].src);
202                         if (matched_src)
203                                 break;
204                         errs = 1;
205                         error("src refspec %s does not match any.",
206                               rs[i].src);
207                         break;
208                 default:
209                         errs = 1;
210                         error("src refspec %s matches more than one.",
211                               rs[i].src);
212                         break;
213                 }
214                 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
215                 case 1:
216                         break;
217                 case 0:
218                         if (!memcmp(rs[i].dst, "refs/", 5)) {
219                                 int len = strlen(rs[i].dst) + 1;
220                                 matched_dst = xcalloc(1, sizeof(*dst) + len);
221                                 memcpy(matched_dst->name, rs[i].dst, len);
222                                 link_dst_tail(matched_dst, dst_tail);
223                         }
224                         else if (!strcmp(rs[i].src, rs[i].dst) &&
225                                  matched_src) {
226                                 /* pushing "master:master" when
227                                  * remote does not have master yet.
228                                  */
229                                 int len = strlen(matched_src->name) + 1;
230                                 matched_dst = xcalloc(1, sizeof(*dst) + len);
231                                 memcpy(matched_dst->name, matched_src->name,
232                                        len);
233                                 link_dst_tail(matched_dst, dst_tail);
234                         }
235                         else {
236                                 errs = 1;
237                                 error("dst refspec %s does not match any "
238                                       "existing ref on the remote and does "
239                                       "not start with refs/.", rs[i].dst);
240                         }
241                         break;
242                 default:
243                         errs = 1;
244                         error("dst refspec %s matches more than one.",
245                               rs[i].dst);
246                         break;
247                 }
248                 if (errs)
249                         continue;
250                 if (matched_dst->peer_ref) {
251                         errs = 1;
252                         error("dst ref %s receives from more than one src.",
253                               matched_dst->name);
254                 }
255                 else {
256                         matched_dst->peer_ref = matched_src;
257                         matched_dst->force = rs[i].force;
258                 }
259         }
260         return -errs;
261 }
262
263 static struct ref *find_ref_by_name(struct ref *list, const char *name)
264 {
265         for ( ; list; list = list->next)
266                 if (!strcmp(list->name, name))
267                         return list;
268         return NULL;
269 }
270
271 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
272                int nr_refspec, char **refspec, int all)
273 {
274         struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
275
276         if (nr_refspec)
277                 return match_explicit_refs(src, dst, dst_tail, rs);
278
279         /* pick the remainder */
280         for ( ; src; src = src->next) {
281                 struct ref *dst_peer;
282                 if (src->peer_ref)
283                         continue;
284                 dst_peer = find_ref_by_name(dst, src->name);
285                 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
286                         continue;
287                 if (!dst_peer) {
288                         /* Create a new one and link it */
289                         int len = strlen(src->name) + 1;
290                         dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
291                         memcpy(dst_peer->name, src->name, len);
292                         memcpy(dst_peer->new_sha1, src->new_sha1, 20);
293                         link_dst_tail(dst_peer, dst_tail);
294                 }
295                 dst_peer->peer_ref = src;
296         }
297         return 0;
298 }
299
300 enum protocol {
301         PROTO_LOCAL = 1,
302         PROTO_SSH,
303         PROTO_GIT,
304 };
305
306 static enum protocol get_protocol(const char *name)
307 {
308         if (!strcmp(name, "ssh"))
309                 return PROTO_SSH;
310         if (!strcmp(name, "git"))
311                 return PROTO_GIT;
312         if (!strcmp(name, "git+ssh"))
313                 return PROTO_SSH;
314         if (!strcmp(name, "ssh+git"))
315                 return PROTO_SSH;
316         die("I don't handle protocol '%s'", name);
317 }
318
319 #define STR_(s) # s
320 #define STR(s)  STR_(s)
321
322 #ifndef NO_IPV6
323
324 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
325 {
326         int sockfd = -1;
327         char *colon, *end;
328         char *port = STR(DEFAULT_GIT_PORT);
329         struct addrinfo hints, *ai0, *ai;
330         int gai;
331
332         if (host[0] == '[') {
333                 end = strchr(host + 1, ']');
334                 if (end) {
335                         *end = 0;
336                         end++;
337                         host++;
338                 } else
339                         end = host;
340         } else
341                 end = host;
342         colon = strchr(end, ':');
343
344         if (colon) {
345                 *colon = 0;
346                 port = colon + 1;
347         }
348
349         memset(&hints, 0, sizeof(hints));
350         hints.ai_socktype = SOCK_STREAM;
351         hints.ai_protocol = IPPROTO_TCP;
352
353         gai = getaddrinfo(host, port, &hints, &ai);
354         if (gai)
355                 die("Unable to look up %s (%s)", host, gai_strerror(gai));
356
357         for (ai0 = ai; ai; ai = ai->ai_next) {
358                 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
359                 if (sockfd < 0)
360                         continue;
361                 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
362                         close(sockfd);
363                         sockfd = -1;
364                         continue;
365                 }
366                 break;
367         }
368
369         freeaddrinfo(ai0);
370
371         if (sockfd < 0)
372                 die("unable to connect a socket (%s)", strerror(errno));
373
374         fd[0] = sockfd;
375         fd[1] = sockfd;
376         packet_write(sockfd, "%s %s\n", prog, path);
377         return 0;
378 }
379
380 #else /* NO_IPV6 */
381
382 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
383 {
384         int sockfd = -1;
385         char *colon, *end;
386         char *port = STR(DEFAULT_GIT_PORT), *ep;
387         struct hostent *he;
388         struct sockaddr_in sa;
389         char **ap;
390         unsigned int nport;
391
392         if (host[0] == '[') {
393                 end = strchr(host + 1, ']');
394                 if (end) {
395                         *end = 0;
396                         end++;
397                         host++;
398                 } else
399                         end = host;
400         } else
401                 end = host;
402         colon = strchr(end, ':');
403
404         if (colon) {
405                 *colon = 0;
406                 port = colon + 1;
407         }
408
409
410         he = gethostbyname(host);
411         if (!he)
412                 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
413         nport = strtoul(port, &ep, 10);
414         if ( ep == port || *ep ) {
415                 /* Not numeric */
416                 struct servent *se = getservbyname(port,"tcp");
417                 if ( !se )
418                         die("Unknown port %s\n", port);
419                 nport = se->s_port;
420         }
421
422         for (ap = he->h_addr_list; *ap; ap++) {
423                 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
424                 if (sockfd < 0)
425                         continue;
426
427                 memset(&sa, 0, sizeof sa);
428                 sa.sin_family = he->h_addrtype;
429                 sa.sin_port = htons(nport);
430                 memcpy(&sa.sin_addr, ap, he->h_length);
431
432                 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
433                         close(sockfd);
434                         sockfd = -1;
435                         continue;
436                 }
437                 break;
438         }
439
440         if (sockfd < 0)
441                 die("unable to connect a socket (%s)", strerror(errno));
442
443         fd[0] = sockfd;
444         fd[1] = sockfd;
445         packet_write(sockfd, "%s %s\n", prog, path);
446         return 0;
447 }
448
449 #endif /* NO_IPV6 */
450
451 static char *git_proxy_command = NULL;
452 static const char *rhost_name = NULL;
453 static int rhost_len;
454
455 static int git_proxy_command_options(const char *var, const char *value)
456 {
457         if (!strcmp(var, "core.gitproxy")) {
458                 if (git_proxy_command)
459                         return 0;
460                 /* [core]
461                  * ;# matches www.kernel.org as well
462                  * gitproxy = netcatter-1 for kernel.org
463                  * gitproxy = netcatter-2 for sample.xz
464                  * gitproxy = netcatter-default
465                  */
466                 const char *for_pos = strstr(value, " for ");
467                 int matchlen = -1;
468                 int hostlen;
469
470                 if (!for_pos)
471                         /* matches everybody */
472                         matchlen = strlen(value);
473                 else {
474                         hostlen = strlen(for_pos + 5);
475                         if (rhost_len < hostlen)
476                                 matchlen = -1;
477                         else if (!strncmp(for_pos + 5,
478                                           rhost_name + rhost_len - hostlen,
479                                           hostlen) &&
480                                  ((rhost_len == hostlen) ||
481                                   rhost_name[rhost_len - hostlen -1] == '.'))
482                                 matchlen = for_pos - value;
483                         else
484                                 matchlen = -1;
485                 }
486                 if (0 <= matchlen) {
487                         /* core.gitproxy = none for kernel.org */
488                         if (matchlen == 4 && 
489                             !memcmp(value, "none", 4))
490                                 matchlen = 0;
491                         git_proxy_command = xmalloc(matchlen + 1);
492                         memcpy(git_proxy_command, value, matchlen);
493                         git_proxy_command[matchlen] = 0;
494                 }
495                 return 0;
496         }
497
498         return git_default_config(var, value);
499 }
500
501 static int git_use_proxy(const char *host)
502 {
503         rhost_name = host;
504         rhost_len = strlen(host);
505         git_proxy_command = getenv("GIT_PROXY_COMMAND");
506         git_config(git_proxy_command_options);
507         rhost_name = NULL;
508         return (git_proxy_command && *git_proxy_command);
509 }
510
511 static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path)
512 {
513         char *port = STR(DEFAULT_GIT_PORT);
514         char *colon, *end;
515         int pipefd[2][2];
516         pid_t pid;
517
518         if (host[0] == '[') {
519                 end = strchr(host + 1, ']');
520                 if (end) {
521                         *end = 0;
522                         end++;
523                         host++;
524                 } else
525                         end = host;
526         } else
527                 end = host;
528         colon = strchr(end, ':');
529
530         if (colon) {
531                 *colon = 0;
532                 port = colon + 1;
533         }
534
535         if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
536                 die("unable to create pipe pair for communication");
537         pid = fork();
538         if (!pid) {
539                 dup2(pipefd[1][0], 0);
540                 dup2(pipefd[0][1], 1);
541                 close(pipefd[0][0]);
542                 close(pipefd[0][1]);
543                 close(pipefd[1][0]);
544                 close(pipefd[1][1]);
545                 execlp(git_proxy_command, git_proxy_command, host, port, NULL);
546                 die("exec failed");
547         }
548         fd[0] = pipefd[0][0];
549         fd[1] = pipefd[1][1];
550         close(pipefd[0][1]);
551         close(pipefd[1][0]);
552         packet_write(fd[1], "%s %s\n", prog, path);
553         return pid;
554 }
555
556 /*
557  * Yeah, yeah, fixme. Need to pass in the heads etc.
558  */
559 int git_connect(int fd[2], char *url, const char *prog)
560 {
561         char command[1024];
562         char *host, *path = url;
563         char *colon = NULL;
564         int pipefd[2][2];
565         pid_t pid;
566         enum protocol protocol = PROTO_LOCAL;
567
568         host = strstr(url, "://");
569         if(host) {
570                 *host = '\0';
571                 protocol = get_protocol(url);
572                 host += 3;
573                 path = strchr(host, '/');
574         }
575         else {
576                 host = url;
577                 if ((colon = strchr(host, ':'))) {
578                         protocol = PROTO_SSH;
579                         *colon = '\0';
580                         path = colon + 1;
581                 }
582         }
583
584         if (!path || !*path)
585                 die("No path specified. See 'man git-pull' for valid url syntax");
586
587         /*
588          * null-terminate hostname and point path to ~ for URL's like this:
589          *    ssh://host.xz/~user/repo
590          */
591         if (protocol != PROTO_LOCAL && host != url) {
592                 char *ptr = path;
593                 if (path[1] == '~')
594                         path++;
595                 else
596                         path = strdup(ptr);
597
598                 *ptr = '\0';
599         }
600
601         if (protocol == PROTO_GIT) {
602                 if (git_use_proxy(host))
603                         return git_proxy_connect(fd, prog, host, path);
604                 return git_tcp_connect(fd, prog, host, path);
605         }
606
607         if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
608                 die("unable to create pipe pair for communication");
609         pid = fork();
610         if (!pid) {
611                 snprintf(command, sizeof(command), "%s %s", prog,
612                          sq_quote(path));
613                 dup2(pipefd[1][0], 0);
614                 dup2(pipefd[0][1], 1);
615                 close(pipefd[0][0]);
616                 close(pipefd[0][1]);
617                 close(pipefd[1][0]);
618                 close(pipefd[1][1]);
619                 if (protocol == PROTO_SSH) {
620                         const char *ssh, *ssh_basename;
621                         ssh = getenv("GIT_SSH");
622                         if (!ssh) ssh = "ssh";
623                         ssh_basename = strrchr(ssh, '/');
624                         if (!ssh_basename)
625                                 ssh_basename = ssh;
626                         else
627                                 ssh_basename++;
628                         execlp(ssh, ssh_basename, host, command, NULL);
629                 }
630                 else
631                         execlp("sh", "sh", "-c", command, NULL);
632                 die("exec failed");
633         }               
634         fd[0] = pipefd[0][0];
635         fd[1] = pipefd[1][1];
636         close(pipefd[0][1]);
637         close(pipefd[1][0]);
638         return pid;
639 }
640
641 int finish_connect(pid_t pid)
642 {
643         int ret;
644
645         for (;;) {
646                 ret = waitpid(pid, NULL, 0);
647                 if (!ret)
648                         break;
649                 if (errno != EINTR)
650                         break;
651         }
652         return ret;
653 }