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