fetch-pack: perform a fetch using v2
[git] / connect.c
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "quote.h"
6 #include "refs.h"
7 #include "run-command.h"
8 #include "remote.h"
9 #include "connect.h"
10 #include "url.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "transport.h"
14 #include "strbuf.h"
15 #include "version.h"
16 #include "protocol.h"
17
18 static char *server_capabilities;
19 static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
20 static const char *parse_feature_value(const char *, const char *, int *);
21
22 static int check_ref(const char *name, unsigned int flags)
23 {
24         if (!flags)
25                 return 1;
26
27         if (!skip_prefix(name, "refs/", &name))
28                 return 0;
29
30         /* REF_NORMAL means that we don't want the magic fake tag refs */
31         if ((flags & REF_NORMAL) && check_refname_format(name, 0))
32                 return 0;
33
34         /* REF_HEADS means that we want regular branch heads */
35         if ((flags & REF_HEADS) && starts_with(name, "heads/"))
36                 return 1;
37
38         /* REF_TAGS means that we want tags */
39         if ((flags & REF_TAGS) && starts_with(name, "tags/"))
40                 return 1;
41
42         /* All type bits clear means that we are ok with anything */
43         return !(flags & ~REF_NORMAL);
44 }
45
46 int check_ref_type(const struct ref *ref, int flags)
47 {
48         return check_ref(ref->name, flags);
49 }
50
51 static void die_initial_contact(int unexpected)
52 {
53         /*
54          * A hang-up after seeing some response from the other end
55          * means that it is unexpected, as we know the other end is
56          * willing to talk to us.  A hang-up before seeing any
57          * response does not necessarily mean an ACL problem, though.
58          */
59         if (unexpected)
60                 die(_("The remote end hung up upon initial contact"));
61         else
62                 die(_("Could not read from remote repository.\n\n"
63                       "Please make sure you have the correct access rights\n"
64                       "and the repository exists."));
65 }
66
67 /* Checks if the server supports the capability 'c' */
68 int server_supports_v2(const char *c, int die_on_error)
69 {
70         int i;
71
72         for (i = 0; i < server_capabilities_v2.argc; i++) {
73                 const char *out;
74                 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
75                     (!*out || *out == '='))
76                         return 1;
77         }
78
79         if (die_on_error)
80                 die("server doesn't support '%s'", c);
81
82         return 0;
83 }
84
85 static void process_capabilities_v2(struct packet_reader *reader)
86 {
87         while (packet_reader_read(reader) == PACKET_READ_NORMAL)
88                 argv_array_push(&server_capabilities_v2, reader->line);
89
90         if (reader->status != PACKET_READ_FLUSH)
91                 die("protocol error");
92 }
93
94 enum protocol_version discover_version(struct packet_reader *reader)
95 {
96         enum protocol_version version = protocol_unknown_version;
97
98         /*
99          * Peek the first line of the server's response to
100          * determine the protocol version the server is speaking.
101          */
102         switch (packet_reader_peek(reader)) {
103         case PACKET_READ_EOF:
104                 die_initial_contact(0);
105         case PACKET_READ_FLUSH:
106         case PACKET_READ_DELIM:
107                 version = protocol_v0;
108                 break;
109         case PACKET_READ_NORMAL:
110                 version = determine_protocol_version_client(reader->line);
111                 break;
112         }
113
114         /* Maybe process capabilities here, at least for v2 */
115         switch (version) {
116         case protocol_v2:
117                 process_capabilities_v2(reader);
118                 break;
119         case protocol_v1:
120                 /* Read the peeked version line */
121                 packet_reader_read(reader);
122                 break;
123         case protocol_v0:
124                 break;
125         case protocol_unknown_version:
126                 die("unknown protocol version: '%s'\n", reader->line);
127         }
128
129         return version;
130 }
131
132 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
133 {
134         char *sym, *target;
135         struct string_list_item *item;
136
137         if (!len)
138                 return; /* just "symref" */
139         /* e.g. "symref=HEAD:refs/heads/master" */
140         sym = xmemdupz(val, len);
141         target = strchr(sym, ':');
142         if (!target)
143                 /* just "symref=something" */
144                 goto reject;
145         *(target++) = '\0';
146         if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
147             check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
148                 /* "symref=bogus:pair */
149                 goto reject;
150         item = string_list_append_nodup(symref, sym);
151         item->util = target;
152         return;
153 reject:
154         free(sym);
155         return;
156 }
157
158 static void annotate_refs_with_symref_info(struct ref *ref)
159 {
160         struct string_list symref = STRING_LIST_INIT_DUP;
161         const char *feature_list = server_capabilities;
162
163         while (feature_list) {
164                 int len;
165                 const char *val;
166
167                 val = parse_feature_value(feature_list, "symref", &len);
168                 if (!val)
169                         break;
170                 parse_one_symref_info(&symref, val, len);
171                 feature_list = val + 1;
172         }
173         string_list_sort(&symref);
174
175         for (; ref; ref = ref->next) {
176                 struct string_list_item *item;
177                 item = string_list_lookup(&symref, ref->name);
178                 if (!item)
179                         continue;
180                 ref->symref = xstrdup((char *)item->util);
181         }
182         string_list_clear(&symref, 0);
183 }
184
185 static void process_capabilities(const char *line, int *len)
186 {
187         int nul_location = strlen(line);
188         if (nul_location == *len)
189                 return;
190         server_capabilities = xstrdup(line + nul_location + 1);
191         *len = nul_location;
192 }
193
194 static int process_dummy_ref(const char *line)
195 {
196         struct object_id oid;
197         const char *name;
198
199         if (parse_oid_hex(line, &oid, &name))
200                 return 0;
201         if (*name != ' ')
202                 return 0;
203         name++;
204
205         return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
206 }
207
208 static void check_no_capabilities(const char *line, int len)
209 {
210         if (strlen(line) != len)
211                 warning("Ignoring capabilities after first line '%s'",
212                         line + strlen(line));
213 }
214
215 static int process_ref(const char *line, int len, struct ref ***list,
216                        unsigned int flags, struct oid_array *extra_have)
217 {
218         struct object_id old_oid;
219         const char *name;
220
221         if (parse_oid_hex(line, &old_oid, &name))
222                 return 0;
223         if (*name != ' ')
224                 return 0;
225         name++;
226
227         if (extra_have && !strcmp(name, ".have")) {
228                 oid_array_append(extra_have, &old_oid);
229         } else if (!strcmp(name, "capabilities^{}")) {
230                 die("protocol error: unexpected capabilities^{}");
231         } else if (check_ref(name, flags)) {
232                 struct ref *ref = alloc_ref(name);
233                 oidcpy(&ref->old_oid, &old_oid);
234                 **list = ref;
235                 *list = &ref->next;
236         }
237         check_no_capabilities(line, len);
238         return 1;
239 }
240
241 static int process_shallow(const char *line, int len,
242                            struct oid_array *shallow_points)
243 {
244         const char *arg;
245         struct object_id old_oid;
246
247         if (!skip_prefix(line, "shallow ", &arg))
248                 return 0;
249
250         if (get_oid_hex(arg, &old_oid))
251                 die("protocol error: expected shallow sha-1, got '%s'", arg);
252         if (!shallow_points)
253                 die("repository on the other end cannot be shallow");
254         oid_array_append(shallow_points, &old_oid);
255         check_no_capabilities(line, len);
256         return 1;
257 }
258
259 enum get_remote_heads_state {
260         EXPECTING_FIRST_REF = 0,
261         EXPECTING_REF,
262         EXPECTING_SHALLOW,
263         EXPECTING_DONE,
264 };
265
266 /*
267  * Read all the refs from the other end
268  */
269 struct ref **get_remote_heads(struct packet_reader *reader,
270                               struct ref **list, unsigned int flags,
271                               struct oid_array *extra_have,
272                               struct oid_array *shallow_points)
273 {
274         struct ref **orig_list = list;
275         int len = 0;
276         enum get_remote_heads_state state = EXPECTING_FIRST_REF;
277         const char *arg;
278
279         *list = NULL;
280
281         while (state != EXPECTING_DONE) {
282                 switch (packet_reader_read(reader)) {
283                 case PACKET_READ_EOF:
284                         die_initial_contact(1);
285                 case PACKET_READ_NORMAL:
286                         len = reader->pktlen;
287                         if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
288                                 die("remote error: %s", arg);
289                         break;
290                 case PACKET_READ_FLUSH:
291                         state = EXPECTING_DONE;
292                         break;
293                 case PACKET_READ_DELIM:
294                         die("invalid packet");
295                 }
296
297                 switch (state) {
298                 case EXPECTING_FIRST_REF:
299                         process_capabilities(reader->line, &len);
300                         if (process_dummy_ref(reader->line)) {
301                                 state = EXPECTING_SHALLOW;
302                                 break;
303                         }
304                         state = EXPECTING_REF;
305                         /* fallthrough */
306                 case EXPECTING_REF:
307                         if (process_ref(reader->line, len, &list, flags, extra_have))
308                                 break;
309                         state = EXPECTING_SHALLOW;
310                         /* fallthrough */
311                 case EXPECTING_SHALLOW:
312                         if (process_shallow(reader->line, len, shallow_points))
313                                 break;
314                         die("protocol error: unexpected '%s'", reader->line);
315                 case EXPECTING_DONE:
316                         break;
317                 }
318         }
319
320         annotate_refs_with_symref_info(*orig_list);
321
322         return list;
323 }
324
325 static int process_ref_v2(const char *line, struct ref ***list)
326 {
327         int ret = 1;
328         int i = 0;
329         struct object_id old_oid;
330         struct ref *ref;
331         struct string_list line_sections = STRING_LIST_INIT_DUP;
332
333         if (string_list_split(&line_sections, line, ' ', -1) < 2) {
334                 ret = 0;
335                 goto out;
336         }
337
338         if (get_oid_hex(line_sections.items[i++].string, &old_oid)) {
339                 ret = 0;
340                 goto out;
341         }
342
343         ref = alloc_ref(line_sections.items[i++].string);
344
345         oidcpy(&ref->old_oid, &old_oid);
346         **list = ref;
347         *list = &ref->next;
348
349         for (; i < line_sections.nr; i++) {
350                 const char *arg = line_sections.items[i].string;
351                 if (skip_prefix(arg, "symref-target:", &arg))
352                         ref->symref = xstrdup(arg);
353
354                 if (skip_prefix(arg, "peeled:", &arg)) {
355                         struct object_id peeled_oid;
356                         char *peeled_name;
357                         struct ref *peeled;
358                         if (get_oid_hex(arg, &peeled_oid)) {
359                                 ret = 0;
360                                 goto out;
361                         }
362
363                         peeled_name = xstrfmt("%s^{}", ref->name);
364                         peeled = alloc_ref(peeled_name);
365
366                         oidcpy(&peeled->old_oid, &peeled_oid);
367                         **list = peeled;
368                         *list = &peeled->next;
369
370                         free(peeled_name);
371                 }
372         }
373
374 out:
375         string_list_clear(&line_sections, 0);
376         return ret;
377 }
378
379 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
380                              struct ref **list, int for_push,
381                              const struct argv_array *ref_patterns)
382 {
383         int i;
384         *list = NULL;
385
386         /* Check that the server supports the ls-refs command */
387         /* Issue request for ls-refs */
388         if (server_supports_v2("ls-refs", 1))
389                 packet_write_fmt(fd_out, "command=ls-refs\n");
390
391         if (server_supports_v2("agent", 0))
392             packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
393
394         packet_delim(fd_out);
395         /* When pushing we don't want to request the peeled tags */
396         if (!for_push)
397                 packet_write_fmt(fd_out, "peel\n");
398         packet_write_fmt(fd_out, "symrefs\n");
399         for (i = 0; ref_patterns && i < ref_patterns->argc; i++) {
400                 packet_write_fmt(fd_out, "ref-pattern %s\n",
401                                  ref_patterns->argv[i]);
402         }
403         packet_flush(fd_out);
404
405         /* Process response from server */
406         while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
407                 if (!process_ref_v2(reader->line, &list))
408                         die("invalid ls-refs response: %s", reader->line);
409         }
410
411         if (reader->status != PACKET_READ_FLUSH)
412                 die("protocol error");
413
414         return list;
415 }
416
417 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
418 {
419         int len;
420
421         if (!feature_list)
422                 return NULL;
423
424         len = strlen(feature);
425         while (*feature_list) {
426                 const char *found = strstr(feature_list, feature);
427                 if (!found)
428                         return NULL;
429                 if (feature_list == found || isspace(found[-1])) {
430                         const char *value = found + len;
431                         /* feature with no value (e.g., "thin-pack") */
432                         if (!*value || isspace(*value)) {
433                                 if (lenp)
434                                         *lenp = 0;
435                                 return value;
436                         }
437                         /* feature with a value (e.g., "agent=git/1.2.3") */
438                         else if (*value == '=') {
439                                 value++;
440                                 if (lenp)
441                                         *lenp = strcspn(value, " \t\n");
442                                 return value;
443                         }
444                         /*
445                          * otherwise we matched a substring of another feature;
446                          * keep looking
447                          */
448                 }
449                 feature_list = found + 1;
450         }
451         return NULL;
452 }
453
454 int parse_feature_request(const char *feature_list, const char *feature)
455 {
456         return !!parse_feature_value(feature_list, feature, NULL);
457 }
458
459 const char *server_feature_value(const char *feature, int *len)
460 {
461         return parse_feature_value(server_capabilities, feature, len);
462 }
463
464 int server_supports(const char *feature)
465 {
466         return !!server_feature_value(feature, NULL);
467 }
468
469 enum protocol {
470         PROTO_LOCAL = 1,
471         PROTO_FILE,
472         PROTO_SSH,
473         PROTO_GIT
474 };
475
476 int url_is_local_not_ssh(const char *url)
477 {
478         const char *colon = strchr(url, ':');
479         const char *slash = strchr(url, '/');
480         return !colon || (slash && slash < colon) ||
481                 has_dos_drive_prefix(url);
482 }
483
484 static const char *prot_name(enum protocol protocol)
485 {
486         switch (protocol) {
487                 case PROTO_LOCAL:
488                 case PROTO_FILE:
489                         return "file";
490                 case PROTO_SSH:
491                         return "ssh";
492                 case PROTO_GIT:
493                         return "git";
494                 default:
495                         return "unknown protocol";
496         }
497 }
498
499 static enum protocol get_protocol(const char *name)
500 {
501         if (!strcmp(name, "ssh"))
502                 return PROTO_SSH;
503         if (!strcmp(name, "git"))
504                 return PROTO_GIT;
505         if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
506                 return PROTO_SSH;
507         if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
508                 return PROTO_SSH;
509         if (!strcmp(name, "file"))
510                 return PROTO_FILE;
511         die("I don't handle protocol '%s'", name);
512 }
513
514 static char *host_end(char **hoststart, int removebrackets)
515 {
516         char *host = *hoststart;
517         char *end;
518         char *start = strstr(host, "@[");
519         if (start)
520                 start++; /* Jump over '@' */
521         else
522                 start = host;
523         if (start[0] == '[') {
524                 end = strchr(start + 1, ']');
525                 if (end) {
526                         if (removebrackets) {
527                                 *end = 0;
528                                 memmove(start, start + 1, end - start);
529                                 end++;
530                         }
531                 } else
532                         end = host;
533         } else
534                 end = host;
535         return end;
536 }
537
538 #define STR_(s) # s
539 #define STR(s)  STR_(s)
540
541 static void get_host_and_port(char **host, const char **port)
542 {
543         char *colon, *end;
544         end = host_end(host, 1);
545         colon = strchr(end, ':');
546         if (colon) {
547                 long portnr = strtol(colon + 1, &end, 10);
548                 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
549                         *colon = 0;
550                         *port = colon + 1;
551                 } else if (!colon[1]) {
552                         *colon = 0;
553                 }
554         }
555 }
556
557 static void enable_keepalive(int sockfd)
558 {
559         int ka = 1;
560
561         if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
562                 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
563                         strerror(errno));
564 }
565
566 #ifndef NO_IPV6
567
568 static const char *ai_name(const struct addrinfo *ai)
569 {
570         static char addr[NI_MAXHOST];
571         if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
572                         NI_NUMERICHOST) != 0)
573                 xsnprintf(addr, sizeof(addr), "(unknown)");
574
575         return addr;
576 }
577
578 /*
579  * Returns a connected socket() fd, or else die()s.
580  */
581 static int git_tcp_connect_sock(char *host, int flags)
582 {
583         struct strbuf error_message = STRBUF_INIT;
584         int sockfd = -1;
585         const char *port = STR(DEFAULT_GIT_PORT);
586         struct addrinfo hints, *ai0, *ai;
587         int gai;
588         int cnt = 0;
589
590         get_host_and_port(&host, &port);
591         if (!*port)
592                 port = "<none>";
593
594         memset(&hints, 0, sizeof(hints));
595         if (flags & CONNECT_IPV4)
596                 hints.ai_family = AF_INET;
597         else if (flags & CONNECT_IPV6)
598                 hints.ai_family = AF_INET6;
599         hints.ai_socktype = SOCK_STREAM;
600         hints.ai_protocol = IPPROTO_TCP;
601
602         if (flags & CONNECT_VERBOSE)
603                 fprintf(stderr, "Looking up %s ... ", host);
604
605         gai = getaddrinfo(host, port, &hints, &ai);
606         if (gai)
607                 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
608
609         if (flags & CONNECT_VERBOSE)
610                 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
611
612         for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
613                 sockfd = socket(ai->ai_family,
614                                 ai->ai_socktype, ai->ai_protocol);
615                 if ((sockfd < 0) ||
616                     (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
617                         strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
618                                     host, cnt, ai_name(ai), strerror(errno));
619                         if (0 <= sockfd)
620                                 close(sockfd);
621                         sockfd = -1;
622                         continue;
623                 }
624                 if (flags & CONNECT_VERBOSE)
625                         fprintf(stderr, "%s ", ai_name(ai));
626                 break;
627         }
628
629         freeaddrinfo(ai0);
630
631         if (sockfd < 0)
632                 die("unable to connect to %s:\n%s", host, error_message.buf);
633
634         enable_keepalive(sockfd);
635
636         if (flags & CONNECT_VERBOSE)
637                 fprintf(stderr, "done.\n");
638
639         strbuf_release(&error_message);
640
641         return sockfd;
642 }
643
644 #else /* NO_IPV6 */
645
646 /*
647  * Returns a connected socket() fd, or else die()s.
648  */
649 static int git_tcp_connect_sock(char *host, int flags)
650 {
651         struct strbuf error_message = STRBUF_INIT;
652         int sockfd = -1;
653         const char *port = STR(DEFAULT_GIT_PORT);
654         char *ep;
655         struct hostent *he;
656         struct sockaddr_in sa;
657         char **ap;
658         unsigned int nport;
659         int cnt;
660
661         get_host_and_port(&host, &port);
662
663         if (flags & CONNECT_VERBOSE)
664                 fprintf(stderr, "Looking up %s ... ", host);
665
666         he = gethostbyname(host);
667         if (!he)
668                 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
669         nport = strtoul(port, &ep, 10);
670         if ( ep == port || *ep ) {
671                 /* Not numeric */
672                 struct servent *se = getservbyname(port,"tcp");
673                 if ( !se )
674                         die("Unknown port %s", port);
675                 nport = se->s_port;
676         }
677
678         if (flags & CONNECT_VERBOSE)
679                 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
680
681         for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
682                 memset(&sa, 0, sizeof sa);
683                 sa.sin_family = he->h_addrtype;
684                 sa.sin_port = htons(nport);
685                 memcpy(&sa.sin_addr, *ap, he->h_length);
686
687                 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
688                 if ((sockfd < 0) ||
689                     connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
690                         strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
691                                 host,
692                                 cnt,
693                                 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
694                                 strerror(errno));
695                         if (0 <= sockfd)
696                                 close(sockfd);
697                         sockfd = -1;
698                         continue;
699                 }
700                 if (flags & CONNECT_VERBOSE)
701                         fprintf(stderr, "%s ",
702                                 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
703                 break;
704         }
705
706         if (sockfd < 0)
707                 die("unable to connect to %s:\n%s", host, error_message.buf);
708
709         enable_keepalive(sockfd);
710
711         if (flags & CONNECT_VERBOSE)
712                 fprintf(stderr, "done.\n");
713
714         return sockfd;
715 }
716
717 #endif /* NO_IPV6 */
718
719
720 /*
721  * Dummy child_process returned by git_connect() if the transport protocol
722  * does not need fork(2).
723  */
724 static struct child_process no_fork = CHILD_PROCESS_INIT;
725
726 int git_connection_is_socket(struct child_process *conn)
727 {
728         return conn == &no_fork;
729 }
730
731 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
732 {
733         int sockfd = git_tcp_connect_sock(host, flags);
734
735         fd[0] = sockfd;
736         fd[1] = dup(sockfd);
737
738         return &no_fork;
739 }
740
741
742 static char *git_proxy_command;
743
744 static int git_proxy_command_options(const char *var, const char *value,
745                 void *cb)
746 {
747         if (!strcmp(var, "core.gitproxy")) {
748                 const char *for_pos;
749                 int matchlen = -1;
750                 int hostlen;
751                 const char *rhost_name = cb;
752                 int rhost_len = strlen(rhost_name);
753
754                 if (git_proxy_command)
755                         return 0;
756                 if (!value)
757                         return config_error_nonbool(var);
758                 /* [core]
759                  * ;# matches www.kernel.org as well
760                  * gitproxy = netcatter-1 for kernel.org
761                  * gitproxy = netcatter-2 for sample.xz
762                  * gitproxy = netcatter-default
763                  */
764                 for_pos = strstr(value, " for ");
765                 if (!for_pos)
766                         /* matches everybody */
767                         matchlen = strlen(value);
768                 else {
769                         hostlen = strlen(for_pos + 5);
770                         if (rhost_len < hostlen)
771                                 matchlen = -1;
772                         else if (!strncmp(for_pos + 5,
773                                           rhost_name + rhost_len - hostlen,
774                                           hostlen) &&
775                                  ((rhost_len == hostlen) ||
776                                   rhost_name[rhost_len - hostlen -1] == '.'))
777                                 matchlen = for_pos - value;
778                         else
779                                 matchlen = -1;
780                 }
781                 if (0 <= matchlen) {
782                         /* core.gitproxy = none for kernel.org */
783                         if (matchlen == 4 &&
784                             !memcmp(value, "none", 4))
785                                 matchlen = 0;
786                         git_proxy_command = xmemdupz(value, matchlen);
787                 }
788                 return 0;
789         }
790
791         return git_default_config(var, value, cb);
792 }
793
794 static int git_use_proxy(const char *host)
795 {
796         git_proxy_command = getenv("GIT_PROXY_COMMAND");
797         git_config(git_proxy_command_options, (void*)host);
798         return (git_proxy_command && *git_proxy_command);
799 }
800
801 static struct child_process *git_proxy_connect(int fd[2], char *host)
802 {
803         const char *port = STR(DEFAULT_GIT_PORT);
804         struct child_process *proxy;
805
806         get_host_and_port(&host, &port);
807
808         if (looks_like_command_line_option(host))
809                 die("strange hostname '%s' blocked", host);
810         if (looks_like_command_line_option(port))
811                 die("strange port '%s' blocked", port);
812
813         proxy = xmalloc(sizeof(*proxy));
814         child_process_init(proxy);
815         argv_array_push(&proxy->args, git_proxy_command);
816         argv_array_push(&proxy->args, host);
817         argv_array_push(&proxy->args, port);
818         proxy->in = -1;
819         proxy->out = -1;
820         if (start_command(proxy))
821                 die("cannot start proxy %s", git_proxy_command);
822         fd[0] = proxy->out; /* read from proxy stdout */
823         fd[1] = proxy->in;  /* write to proxy stdin */
824         return proxy;
825 }
826
827 static char *get_port(char *host)
828 {
829         char *end;
830         char *p = strchr(host, ':');
831
832         if (p) {
833                 long port = strtol(p + 1, &end, 10);
834                 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
835                         *p = '\0';
836                         return p+1;
837                 }
838         }
839
840         return NULL;
841 }
842
843 /*
844  * Extract protocol and relevant parts from the specified connection URL.
845  * The caller must free() the returned strings.
846  */
847 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
848                                        char **ret_path)
849 {
850         char *url;
851         char *host, *path;
852         char *end;
853         int separator = '/';
854         enum protocol protocol = PROTO_LOCAL;
855
856         if (is_url(url_orig))
857                 url = url_decode(url_orig);
858         else
859                 url = xstrdup(url_orig);
860
861         host = strstr(url, "://");
862         if (host) {
863                 *host = '\0';
864                 protocol = get_protocol(url);
865                 host += 3;
866         } else {
867                 host = url;
868                 if (!url_is_local_not_ssh(url)) {
869                         protocol = PROTO_SSH;
870                         separator = ':';
871                 }
872         }
873
874         /*
875          * Don't do destructive transforms as protocol code does
876          * '[]' unwrapping in get_host_and_port()
877          */
878         end = host_end(&host, 0);
879
880         if (protocol == PROTO_LOCAL)
881                 path = end;
882         else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
883                 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
884         else
885                 path = strchr(end, separator);
886
887         if (!path || !*path)
888                 die("No path specified. See 'man git-pull' for valid url syntax");
889
890         /*
891          * null-terminate hostname and point path to ~ for URL's like this:
892          *    ssh://host.xz/~user/repo
893          */
894
895         end = path; /* Need to \0 terminate host here */
896         if (separator == ':')
897                 path++; /* path starts after ':' */
898         if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
899                 if (path[1] == '~')
900                         path++;
901         }
902
903         path = xstrdup(path);
904         *end = '\0';
905
906         *ret_host = xstrdup(host);
907         *ret_path = path;
908         free(url);
909         return protocol;
910 }
911
912 static const char *get_ssh_command(void)
913 {
914         const char *ssh;
915
916         if ((ssh = getenv("GIT_SSH_COMMAND")))
917                 return ssh;
918
919         if (!git_config_get_string_const("core.sshcommand", &ssh))
920                 return ssh;
921
922         return NULL;
923 }
924
925 enum ssh_variant {
926         VARIANT_AUTO,
927         VARIANT_SIMPLE,
928         VARIANT_SSH,
929         VARIANT_PLINK,
930         VARIANT_PUTTY,
931         VARIANT_TORTOISEPLINK,
932 };
933
934 static void override_ssh_variant(enum ssh_variant *ssh_variant)
935 {
936         const char *variant = getenv("GIT_SSH_VARIANT");
937
938         if (!variant && git_config_get_string_const("ssh.variant", &variant))
939                 return;
940
941         if (!strcmp(variant, "auto"))
942                 *ssh_variant = VARIANT_AUTO;
943         else if (!strcmp(variant, "plink"))
944                 *ssh_variant = VARIANT_PLINK;
945         else if (!strcmp(variant, "putty"))
946                 *ssh_variant = VARIANT_PUTTY;
947         else if (!strcmp(variant, "tortoiseplink"))
948                 *ssh_variant = VARIANT_TORTOISEPLINK;
949         else if (!strcmp(variant, "simple"))
950                 *ssh_variant = VARIANT_SIMPLE;
951         else
952                 *ssh_variant = VARIANT_SSH;
953 }
954
955 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
956                                               int is_cmdline)
957 {
958         enum ssh_variant ssh_variant = VARIANT_AUTO;
959         const char *variant;
960         char *p = NULL;
961
962         override_ssh_variant(&ssh_variant);
963
964         if (ssh_variant != VARIANT_AUTO)
965                 return ssh_variant;
966
967         if (!is_cmdline) {
968                 p = xstrdup(ssh_command);
969                 variant = basename(p);
970         } else {
971                 const char **ssh_argv;
972
973                 p = xstrdup(ssh_command);
974                 if (split_cmdline(p, &ssh_argv) > 0) {
975                         variant = basename((char *)ssh_argv[0]);
976                         /*
977                          * At this point, variant points into the buffer
978                          * referenced by p, hence we do not need ssh_argv
979                          * any longer.
980                          */
981                         free(ssh_argv);
982                 } else {
983                         free(p);
984                         return ssh_variant;
985                 }
986         }
987
988         if (!strcasecmp(variant, "ssh") ||
989             !strcasecmp(variant, "ssh.exe"))
990                 ssh_variant = VARIANT_SSH;
991         else if (!strcasecmp(variant, "plink") ||
992                  !strcasecmp(variant, "plink.exe"))
993                 ssh_variant = VARIANT_PLINK;
994         else if (!strcasecmp(variant, "tortoiseplink") ||
995                  !strcasecmp(variant, "tortoiseplink.exe"))
996                 ssh_variant = VARIANT_TORTOISEPLINK;
997
998         free(p);
999         return ssh_variant;
1000 }
1001
1002 /*
1003  * Open a connection using Git's native protocol.
1004  *
1005  * The caller is responsible for freeing hostandport, but this function may
1006  * modify it (for example, to truncate it to remove the port part).
1007  */
1008 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1009                                              const char *path, const char *prog,
1010                                              int flags)
1011 {
1012         struct child_process *conn;
1013         struct strbuf request = STRBUF_INIT;
1014         /*
1015          * Set up virtual host information based on where we will
1016          * connect, unless the user has overridden us in
1017          * the environment.
1018          */
1019         char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1020         if (target_host)
1021                 target_host = xstrdup(target_host);
1022         else
1023                 target_host = xstrdup(hostandport);
1024
1025         transport_check_allowed("git");
1026
1027         /*
1028          * These underlying connection commands die() if they
1029          * cannot connect.
1030          */
1031         if (git_use_proxy(hostandport))
1032                 conn = git_proxy_connect(fd, hostandport);
1033         else
1034                 conn = git_tcp_connect(fd, hostandport, flags);
1035         /*
1036          * Separate original protocol components prog and path
1037          * from extended host header with a NUL byte.
1038          *
1039          * Note: Do not add any other headers here!  Doing so
1040          * will cause older git-daemon servers to crash.
1041          */
1042         strbuf_addf(&request,
1043                     "%s %s%chost=%s%c",
1044                     prog, path, 0,
1045                     target_host, 0);
1046
1047         /* If using a new version put that stuff here after a second null byte */
1048         if (get_protocol_version_config() > 0) {
1049                 strbuf_addch(&request, '\0');
1050                 strbuf_addf(&request, "version=%d%c",
1051                             get_protocol_version_config(), '\0');
1052         }
1053
1054         packet_write(fd[1], request.buf, request.len);
1055
1056         free(target_host);
1057         strbuf_release(&request);
1058         return conn;
1059 }
1060
1061 /*
1062  * Append the appropriate environment variables to `env` and options to
1063  * `args` for running ssh in Git's SSH-tunneled transport.
1064  */
1065 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1066                              enum ssh_variant variant, const char *port,
1067                              int flags)
1068 {
1069         if (variant == VARIANT_SSH &&
1070             get_protocol_version_config() > 0) {
1071                 argv_array_push(args, "-o");
1072                 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1073                 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1074                                  get_protocol_version_config());
1075         }
1076
1077         if (flags & CONNECT_IPV4) {
1078                 switch (variant) {
1079                 case VARIANT_AUTO:
1080                         BUG("VARIANT_AUTO passed to push_ssh_options");
1081                 case VARIANT_SIMPLE:
1082                         die("ssh variant 'simple' does not support -4");
1083                 case VARIANT_SSH:
1084                 case VARIANT_PLINK:
1085                 case VARIANT_PUTTY:
1086                 case VARIANT_TORTOISEPLINK:
1087                         argv_array_push(args, "-4");
1088                 }
1089         } else if (flags & CONNECT_IPV6) {
1090                 switch (variant) {
1091                 case VARIANT_AUTO:
1092                         BUG("VARIANT_AUTO passed to push_ssh_options");
1093                 case VARIANT_SIMPLE:
1094                         die("ssh variant 'simple' does not support -6");
1095                 case VARIANT_SSH:
1096                 case VARIANT_PLINK:
1097                 case VARIANT_PUTTY:
1098                 case VARIANT_TORTOISEPLINK:
1099                         argv_array_push(args, "-6");
1100                 }
1101         }
1102
1103         if (variant == VARIANT_TORTOISEPLINK)
1104                 argv_array_push(args, "-batch");
1105
1106         if (port) {
1107                 switch (variant) {
1108                 case VARIANT_AUTO:
1109                         BUG("VARIANT_AUTO passed to push_ssh_options");
1110                 case VARIANT_SIMPLE:
1111                         die("ssh variant 'simple' does not support setting port");
1112                 case VARIANT_SSH:
1113                         argv_array_push(args, "-p");
1114                         break;
1115                 case VARIANT_PLINK:
1116                 case VARIANT_PUTTY:
1117                 case VARIANT_TORTOISEPLINK:
1118                         argv_array_push(args, "-P");
1119                 }
1120
1121                 argv_array_push(args, port);
1122         }
1123 }
1124
1125 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1126 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1127                           const char *port, int flags)
1128 {
1129         const char *ssh;
1130         enum ssh_variant variant;
1131
1132         if (looks_like_command_line_option(ssh_host))
1133                 die("strange hostname '%s' blocked", ssh_host);
1134
1135         ssh = get_ssh_command();
1136         if (ssh) {
1137                 variant = determine_ssh_variant(ssh, 1);
1138         } else {
1139                 /*
1140                  * GIT_SSH is the no-shell version of
1141                  * GIT_SSH_COMMAND (and must remain so for
1142                  * historical compatibility).
1143                  */
1144                 conn->use_shell = 0;
1145
1146                 ssh = getenv("GIT_SSH");
1147                 if (!ssh)
1148                         ssh = "ssh";
1149                 variant = determine_ssh_variant(ssh, 0);
1150         }
1151
1152         if (variant == VARIANT_AUTO) {
1153                 struct child_process detect = CHILD_PROCESS_INIT;
1154
1155                 detect.use_shell = conn->use_shell;
1156                 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1157
1158                 argv_array_push(&detect.args, ssh);
1159                 argv_array_push(&detect.args, "-G");
1160                 push_ssh_options(&detect.args, &detect.env_array,
1161                                  VARIANT_SSH, port, flags);
1162                 argv_array_push(&detect.args, ssh_host);
1163
1164                 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1165         }
1166
1167         argv_array_push(&conn->args, ssh);
1168         push_ssh_options(&conn->args, &conn->env_array, variant, port, flags);
1169         argv_array_push(&conn->args, ssh_host);
1170 }
1171
1172 /*
1173  * This returns the dummy child_process `no_fork` if the transport protocol
1174  * does not need fork(2), or a struct child_process object if it does.  Once
1175  * done, finish the connection with finish_connect() with the value returned
1176  * from this function (it is safe to call finish_connect() with NULL to
1177  * support the former case).
1178  *
1179  * If it returns, the connect is successful; it just dies on errors (this
1180  * will hopefully be changed in a libification effort, to return NULL when
1181  * the connection failed).
1182  */
1183 struct child_process *git_connect(int fd[2], const char *url,
1184                                   const char *prog, int flags)
1185 {
1186         char *hostandport, *path;
1187         struct child_process *conn;
1188         enum protocol protocol;
1189
1190         /* Without this we cannot rely on waitpid() to tell
1191          * what happened to our children.
1192          */
1193         signal(SIGCHLD, SIG_DFL);
1194
1195         protocol = parse_connect_url(url, &hostandport, &path);
1196         if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1197                 printf("Diag: url=%s\n", url ? url : "NULL");
1198                 printf("Diag: protocol=%s\n", prot_name(protocol));
1199                 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1200                 printf("Diag: path=%s\n", path ? path : "NULL");
1201                 conn = NULL;
1202         } else if (protocol == PROTO_GIT) {
1203                 conn = git_connect_git(fd, hostandport, path, prog, flags);
1204         } else {
1205                 struct strbuf cmd = STRBUF_INIT;
1206                 const char *const *var;
1207
1208                 conn = xmalloc(sizeof(*conn));
1209                 child_process_init(conn);
1210
1211                 if (looks_like_command_line_option(path))
1212                         die("strange pathname '%s' blocked", path);
1213
1214                 strbuf_addstr(&cmd, prog);
1215                 strbuf_addch(&cmd, ' ');
1216                 sq_quote_buf(&cmd, path);
1217
1218                 /* remove repo-local variables from the environment */
1219                 for (var = local_repo_env; *var; var++)
1220                         argv_array_push(&conn->env_array, *var);
1221
1222                 conn->use_shell = 1;
1223                 conn->in = conn->out = -1;
1224                 if (protocol == PROTO_SSH) {
1225                         char *ssh_host = hostandport;
1226                         const char *port = NULL;
1227                         transport_check_allowed("ssh");
1228                         get_host_and_port(&ssh_host, &port);
1229
1230                         if (!port)
1231                                 port = get_port(ssh_host);
1232
1233                         if (flags & CONNECT_DIAG_URL) {
1234                                 printf("Diag: url=%s\n", url ? url : "NULL");
1235                                 printf("Diag: protocol=%s\n", prot_name(protocol));
1236                                 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1237                                 printf("Diag: port=%s\n", port ? port : "NONE");
1238                                 printf("Diag: path=%s\n", path ? path : "NULL");
1239
1240                                 free(hostandport);
1241                                 free(path);
1242                                 free(conn);
1243                                 strbuf_release(&cmd);
1244                                 return NULL;
1245                         }
1246                         fill_ssh_args(conn, ssh_host, port, flags);
1247                 } else {
1248                         transport_check_allowed("file");
1249                         if (get_protocol_version_config() > 0) {
1250                                 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1251                                                  get_protocol_version_config());
1252                         }
1253                 }
1254                 argv_array_push(&conn->args, cmd.buf);
1255
1256                 if (start_command(conn))
1257                         die("unable to fork");
1258
1259                 fd[0] = conn->out; /* read from child's stdout */
1260                 fd[1] = conn->in;  /* write to child's stdin */
1261                 strbuf_release(&cmd);
1262         }
1263         free(hostandport);
1264         free(path);
1265         return conn;
1266 }
1267
1268 int finish_connect(struct child_process *conn)
1269 {
1270         int code;
1271         if (!conn || git_connection_is_socket(conn))
1272                 return 0;
1273
1274         code = finish_command(conn);
1275         free(conn);
1276         return code;
1277 }