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