sequencer: treat CHERRY_PICK_HEAD as a pseudo ref
[git/debian.git] / connect.c
blob0b6aba177e0f4dd3a999e3382d61cc71cf5b33e2
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"
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);
23 static int check_ref(const char *name, unsigned int flags)
25 if (!flags)
26 return 1;
28 if (!skip_prefix(name, "refs/", &name))
29 return 0;
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;
35 /* REF_HEADS means that we want regular branch heads */
36 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
37 return 1;
39 /* REF_TAGS means that we want tags */
40 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
41 return 1;
43 /* All type bits clear means that we are ok with anything */
44 return !(flags & ~REF_NORMAL);
47 int check_ref_type(const struct ref *ref, int flags)
49 return check_ref(ref->name, flags);
52 static NORETURN void die_initial_contact(int unexpected)
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.
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."));
68 /* Checks if the server supports the capability 'c' */
69 int server_supports_v2(const char *c, int die_on_error)
71 int i;
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;
80 if (die_on_error)
81 die(_("server doesn't support '%s'"), c);
83 return 0;
86 int server_feature_v2(const char *c, const char **v)
88 int i;
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;
98 return 0;
101 int server_supports_feature(const char *c, const char *feature,
102 int die_on_error)
104 int i;
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;
117 if (die_on_error)
118 die(_("server doesn't support feature '%s'"), feature);
120 return 0;
123 static void process_capabilities_v2(struct packet_reader *reader)
125 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
126 strvec_push(&server_capabilities_v2, reader->line);
128 if (reader->status != PACKET_READ_FLUSH)
129 die(_("expected flush after capabilities"));
132 enum protocol_version discover_version(struct packet_reader *reader)
134 enum protocol_version version = protocol_unknown_version;
137 * Peek the first line of the server's response to
138 * determine the protocol version the server is speaking.
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;
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");
167 return version;
170 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
172 char *sym, *target;
173 struct string_list_item *item;
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;
196 static void annotate_refs_with_symref_info(struct ref *ref)
198 struct string_list symref = STRING_LIST_INIT_DUP;
199 int offset = 0;
201 while (1) {
202 int len;
203 const char *val;
205 val = next_server_feature_value("symref", &len, &offset);
206 if (!val)
207 break;
208 parse_one_symref_info(&symref, val, len);
210 string_list_sort(&symref);
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);
219 string_list_clear(&symref, 0);
222 static void process_capabilities(struct packet_reader *reader, int *linelen)
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;
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];
245 static int process_dummy_ref(const struct packet_reader *reader)
247 const char *line = reader->line;
248 struct object_id oid;
249 const char *name;
251 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
252 return 0;
253 if (*name != ' ')
254 return 0;
255 name++;
257 return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
260 static void check_no_capabilities(const char *line, int len)
262 if (strlen(line) != len)
263 warning(_("ignoring capabilities after first line '%s'"),
264 line + strlen(line));
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)
271 const char *line = reader->line;
272 struct object_id old_oid;
273 const char *name;
275 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
276 return 0;
277 if (*name != ' ')
278 return 0;
279 name++;
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;
291 check_no_capabilities(line, len);
292 return 1;
295 static int process_shallow(const struct packet_reader *reader, int len,
296 struct oid_array *shallow_points)
298 const char *line = reader->line;
299 const char *arg;
300 struct object_id old_oid;
302 if (!skip_prefix(line, "shallow ", &arg))
303 return 0;
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;
314 enum get_remote_heads_state {
315 EXPECTING_FIRST_REF = 0,
316 EXPECTING_REF,
317 EXPECTING_SHALLOW,
318 EXPECTING_DONE,
322 * Read all the refs from the other end
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)
329 struct ref **orig_list = list;
330 int len = 0;
331 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
333 *list = NULL;
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"));
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;
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;
373 annotate_refs_with_symref_info(*orig_list);
375 return list;
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)
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;
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.
395 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
396 ret = 0;
397 goto out;
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;
406 ref = alloc_ref(line_sections.items[i++].string);
408 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
409 **list = ref;
410 *list = &ref->next;
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);
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;
427 peeled_name = xstrfmt("%s^{}", ref->name);
428 peeled = alloc_ref(peeled_name);
430 memcpy(peeled->old_oid.hash, peeled_oid.hash,
431 reader->hash_algo->rawsz);
432 **list = peeled;
433 *list = &peeled->next;
435 free(peeled_name);
439 out:
440 string_list_clear(&line_sections, 0);
441 return ret;
444 void check_stateless_delimiter(int stateless_rpc,
445 struct packet_reader *reader,
446 const char *error)
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);
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)
460 int i;
461 const char *hash_name;
462 *list = NULL;
464 if (server_supports_v2("ls-refs", 1))
465 packet_write_fmt(fd_out, "command=ls-refs\n");
467 if (server_supports_v2("agent", 0))
468 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
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];
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);
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]);
495 packet_flush(fd_out);
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);
503 if (reader->status != PACKET_READ_FLUSH)
504 die(_("expected flush after ref listing"));
506 check_stateless_delimiter(stateless_rpc, reader,
507 _("expected response end packet after ref listing"));
509 return list;
512 const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset)
514 int len;
516 if (!feature_list)
517 return NULL;
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;
534 /* feature with a value (e.g., "agent=git/1.2.3") */
535 else if (*value == '=') {
536 int end;
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;
547 * otherwise we matched a substring of another feature;
548 * keep looking
551 feature_list = found + 1;
553 return NULL;
556 int server_supports_hash(const char *desired, int *feature_supported)
558 int offset = 0;
559 int len;
560 const char *hash;
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);
569 while (hash) {
570 if (!xstrncmpz(desired, hash, len))
571 return 1;
573 hash = next_server_feature_value("object-format", &len, &offset);
575 return 0;
578 int parse_feature_request(const char *feature_list, const char *feature)
580 return !!parse_feature_value(feature_list, feature, NULL, NULL);
583 static const char *next_server_feature_value(const char *feature, int *len, int *offset)
585 return parse_feature_value(server_capabilities_v1, feature, len, offset);
588 const char *server_feature_value(const char *feature, int *len)
590 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
593 int server_supports(const char *feature)
595 return !!server_feature_value(feature, NULL);
598 enum protocol {
599 PROTO_LOCAL = 1,
600 PROTO_FILE,
601 PROTO_SSH,
602 PROTO_GIT
605 int url_is_local_not_ssh(const char *url)
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));
613 static const char *prot_name(enum protocol protocol)
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";
628 static enum protocol get_protocol(const char *name)
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);
643 static char *host_end(char **hoststart, int removebrackets)
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++;
660 } else
661 end = host;
662 } else
663 end = host;
664 return end;
667 #define STR_(s) # s
668 #define STR(s) STR_(s)
670 static void get_host_and_port(char **host, const char **port)
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;
686 static void enable_keepalive(int sockfd)
688 int ka = 1;
690 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
691 error_errno(_("unable to set SO_KEEPALIVE on socket"));
694 #ifndef NO_IPV6
696 static const char *ai_name(const struct addrinfo *ai)
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)");
703 return addr;
707 * Returns a connected socket() fd, or else die()s.
709 static int git_tcp_connect_sock(char *host, int flags)
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;
718 get_host_and_port(&host, &port);
719 if (!*port)
720 port = "<none>";
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;
730 if (flags & CONNECT_VERBOSE)
731 fprintf(stderr, _("Looking up %s ... "), host);
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));
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);
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;
753 if (flags & CONNECT_VERBOSE)
754 fprintf(stderr, "%s ", ai_name(ai));
755 break;
758 freeaddrinfo(ai0);
760 if (sockfd < 0)
761 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
763 enable_keepalive(sockfd);
765 if (flags & CONNECT_VERBOSE)
766 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
767 fprintf_ln(stderr, _("done."));
769 strbuf_release(&error_message);
771 return sockfd;
774 #else /* NO_IPV6 */
777 * Returns a connected socket() fd, or else die()s.
779 static int git_tcp_connect_sock(char *host, int flags)
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;
791 get_host_and_port(&host, &port);
793 if (flags & CONNECT_VERBOSE)
794 fprintf(stderr, _("Looking up %s ... "), host);
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;
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);
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);
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;
831 if (flags & CONNECT_VERBOSE)
832 fprintf(stderr, "%s ",
833 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
834 break;
837 if (sockfd < 0)
838 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
840 enable_keepalive(sockfd);
842 if (flags & CONNECT_VERBOSE)
843 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
844 fprintf_ln(stderr, _("done."));
846 return sockfd;
849 #endif /* NO_IPV6 */
853 * Dummy child_process returned by git_connect() if the transport protocol
854 * does not need fork(2).
856 static struct child_process no_fork = CHILD_PROCESS_INIT;
858 int git_connection_is_socket(struct child_process *conn)
860 return conn == &no_fork;
863 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
865 int sockfd = git_tcp_connect_sock(host, flags);
867 fd[0] = sockfd;
868 fd[1] = dup(sockfd);
870 return &no_fork;
874 static char *git_proxy_command;
876 static int git_proxy_command_options(const char *var, const char *value,
877 void *cb)
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);
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
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;
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);
920 return 0;
923 return git_default_config(var, value, cb);
926 static int git_use_proxy(const char *host)
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);
933 static struct child_process *git_proxy_connect(int fd[2], char *host)
935 const char *port = STR(DEFAULT_GIT_PORT);
936 struct child_process *proxy;
938 get_host_and_port(&host, &port);
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);
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;
959 static char *get_port(char *host)
961 char *end;
962 char *p = strchr(host, ':');
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;
972 return NULL;
976 * Extract protocol and relevant parts from the specified connection URL.
977 * The caller must free() the returned strings.
979 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
980 char **ret_path)
982 char *url;
983 char *host, *path;
984 char *end;
985 int separator = '/';
986 enum protocol protocol = PROTO_LOCAL;
988 if (is_url(url_orig))
989 url = url_decode(url_orig);
990 else
991 url = xstrdup(url_orig);
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 = ':';
1007 * Don't do destructive transforms as protocol code does
1008 * '[]' unwrapping in get_host_and_port()
1010 end = host_end(&host, 0);
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);
1023 if (!path || !*path)
1024 die(_("no path specified; see 'git help pull' for valid url syntax"));
1027 * null-terminate hostname and point path to ~ for URL's like this:
1028 * ssh://host.xz/~user/repo
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++;
1039 path = xstrdup(path);
1040 *end = '\0';
1042 *ret_host = xstrdup(host);
1043 *ret_path = path;
1044 free(url);
1045 return protocol;
1048 static const char *get_ssh_command(void)
1050 const char *ssh;
1052 if ((ssh = getenv("GIT_SSH_COMMAND")))
1053 return ssh;
1055 if (!git_config_get_string_const("core.sshcommand", &ssh))
1056 return ssh;
1058 return NULL;
1061 enum ssh_variant {
1062 VARIANT_AUTO,
1063 VARIANT_SIMPLE,
1064 VARIANT_SSH,
1065 VARIANT_PLINK,
1066 VARIANT_PUTTY,
1067 VARIANT_TORTOISEPLINK,
1070 static void override_ssh_variant(enum ssh_variant *ssh_variant)
1072 const char *variant = getenv("GIT_SSH_VARIANT");
1074 if (!variant && git_config_get_string_const("ssh.variant", &variant))
1075 return;
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;
1091 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1092 int is_cmdline)
1094 enum ssh_variant ssh_variant = VARIANT_AUTO;
1095 const char *variant;
1096 char *p = NULL;
1098 override_ssh_variant(&ssh_variant);
1100 if (ssh_variant != VARIANT_AUTO)
1101 return ssh_variant;
1103 if (!is_cmdline) {
1104 p = xstrdup(ssh_command);
1105 variant = basename(p);
1106 } else {
1107 const char **ssh_argv;
1109 p = xstrdup(ssh_command);
1110 if (split_cmdline(p, &ssh_argv) > 0) {
1111 variant = basename((char *)ssh_argv[0]);
1113 * At this point, variant points into the buffer
1114 * referenced by p, hence we do not need ssh_argv
1115 * any longer.
1117 free(ssh_argv);
1118 } else {
1119 free(p);
1120 return ssh_variant;
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;
1134 free(p);
1135 return ssh_variant;
1139 * Open a connection using Git's native protocol.
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).
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)
1149 struct child_process *conn;
1150 struct strbuf request = STRBUF_INIT;
1152 * Set up virtual host information based on where we will
1153 * connect, unless the user has overridden us in
1154 * the environment.
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);
1162 transport_check_allowed("git");
1165 * These underlying connection commands die() if they
1166 * cannot connect.
1168 if (git_use_proxy(hostandport))
1169 conn = git_proxy_connect(fd, hostandport);
1170 else
1171 conn = git_tcp_connect(fd, hostandport, flags);
1173 * Separate original protocol components prog and path
1174 * from extended host header with a NUL byte.
1176 * Note: Do not add any other headers here! Doing so
1177 * will cause older git-daemon servers to crash.
1179 strbuf_addf(&request,
1180 "%s %s%chost=%s%c",
1181 prog, path, 0,
1182 target_host, 0);
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');
1191 packet_write(fd[1], request.buf, request.len);
1193 free(target_host);
1194 strbuf_release(&request);
1195 return conn;
1199 * Append the appropriate environment variables to `env` and options to
1200 * `args` for running ssh in Git's SSH-tunneled transport.
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)
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);
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");
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");
1240 if (variant == VARIANT_TORTOISEPLINK)
1241 strvec_push(args, "-batch");
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");
1258 strvec_push(args, port);
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)
1267 const char *ssh;
1268 enum ssh_variant variant;
1270 if (looks_like_command_line_option(ssh_host))
1271 die(_("strange hostname '%s' blocked"), ssh_host);
1273 ssh = get_ssh_command();
1274 if (ssh) {
1275 variant = determine_ssh_variant(ssh, 1);
1276 } else {
1278 * GIT_SSH is the no-shell version of
1279 * GIT_SSH_COMMAND (and must remain so for
1280 * historical compatibility).
1282 conn->use_shell = 0;
1284 ssh = getenv("GIT_SSH");
1285 if (!ssh)
1286 ssh = "ssh";
1287 variant = determine_ssh_variant(ssh, 0);
1290 if (variant == VARIANT_AUTO) {
1291 struct child_process detect = CHILD_PROCESS_INIT;
1293 detect.use_shell = conn->use_shell;
1294 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
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);
1302 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
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);
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).
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).
1321 struct child_process *git_connect(int fd[2], const char *url,
1322 const char *prog, int flags)
1324 char *hostandport, *path;
1325 struct child_process *conn;
1326 enum protocol protocol;
1327 enum protocol_version version = get_protocol_version_config();
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.
1334 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1335 version = protocol_v0;
1337 /* Without this we cannot rely on waitpid() to tell
1338 * what happened to our children.
1340 signal(SIGCHLD, SIG_DFL);
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;
1356 conn = xmalloc(sizeof(*conn));
1357 child_process_init(conn);
1359 if (looks_like_command_line_option(path))
1360 die(_("strange pathname '%s' blocked"), path);
1362 strbuf_addstr(&cmd, prog);
1363 strbuf_addch(&cmd, ' ');
1364 sq_quote_buf(&cmd, path);
1366 /* remove repo-local variables from the environment */
1367 for (var = local_repo_env; *var; var++)
1368 strvec_push(&conn->env_array, *var);
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);
1378 if (!port)
1379 port = get_port(ssh_host);
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");
1388 free(hostandport);
1389 free(path);
1390 free(conn);
1391 strbuf_release(&cmd);
1392 return NULL;
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);
1405 strvec_push(&conn->args, cmd.buf);
1407 if (start_command(conn))
1408 die(_("unable to fork"));
1410 fd[0] = conn->out; /* read from child's stdout */
1411 fd[1] = conn->in; /* write to child's stdin */
1412 strbuf_release(&cmd);
1414 free(hostandport);
1415 free(path);
1416 return conn;
1419 int finish_connect(struct child_process *conn)
1421 int code;
1422 if (!conn || git_connection_is_socket(conn))
1423 return 0;
1425 code = finish_command(conn);
1426 free(conn);
1427 return code;