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