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