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