1 #include "git-compat-util.h"
7 #include "run-command.h"
11 #include "string-list.h"
12 #include "oid-array.h"
13 #include "transport.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)
28 if (!skip_prefix(name, "refs/", &name))
31 /* REF_NORMAL means that we don't want the magic fake tag refs */
32 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
35 /* REF_HEADS means that we want regular branch heads */
36 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
39 /* REF_TAGS means that we want tags */
40 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
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.
61 die(_("the remote end hung up upon initial contact"));
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)
73 for (i = 0; i < server_capabilities_v2.nr; i++) {
75 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
76 (!*out || *out == '='))
81 die(_("server doesn't support '%s'"), c);
86 int server_feature_v2(const char *c, const char **v)
90 for (i = 0; i < server_capabilities_v2.nr; i++) {
92 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
101 int server_supports_feature(const char *c, const char *feature,
106 for (i = 0; i < server_capabilities_v2.nr; i++) {
108 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
109 (!*out || *(out++) == '=')) {
110 if (parse_feature_request(out, feature))
118 die(_("server doesn't support feature '%s'"), feature);
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;
148 case PACKET_READ_NORMAL:
149 version = determine_protocol_version_client(reader->line);
155 process_capabilities_v2(reader);
158 /* Read the peeked version line */
159 packet_reader_read(reader);
163 case protocol_unknown_version:
164 BUG("unknown protocol version");
170 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
173 struct string_list_item *item;
176 return; /* just "symref" */
177 /* e.g. "symref=HEAD:refs/heads/master" */
178 sym = xmemdupz(val, len);
179 target = strchr(sym, ':');
181 /* just "symref=something" */
184 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
185 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
186 /* "symref=bogus:pair */
188 item = string_list_append_nodup(symref, sym);
196 static void annotate_refs_with_symref_info(struct ref *ref)
198 struct string_list symref = STRING_LIST_INIT_DUP;
205 val = next_server_feature_value("symref", &len, &offset);
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);
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;
226 const char *line = reader->line;
227 int nul_location = strlen(line);
228 if (nul_location == *linelen)
230 server_capabilities_v1 = xstrdup(line + nul_location + 1);
231 *linelen = nul_location;
233 feat_val = server_feature_value("object-format", &feat_len);
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];
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;
251 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
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;
275 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
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);
291 check_no_capabilities(line, len);
295 static int process_shallow(const struct packet_reader *reader, int len,
296 struct oid_array *shallow_points)
298 const char *line = reader->line;
300 struct object_id old_oid;
302 if (!skip_prefix(line, "shallow ", &arg))
305 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
306 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
308 die(_("repository on the other end cannot be shallow"));
309 oid_array_append(shallow_points, &old_oid);
310 check_no_capabilities(line, len);
314 enum get_remote_heads_state {
315 EXPECTING_FIRST_REF = 0,
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;
331 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
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;
342 case PACKET_READ_FLUSH:
343 state = EXPECTING_DONE;
345 case PACKET_READ_DELIM:
346 case PACKET_READ_RESPONSE_END:
347 die(_("invalid packet"));
351 case EXPECTING_FIRST_REF:
352 process_capabilities(reader, &len);
353 if (process_dummy_ref(reader)) {
354 state = EXPECTING_SHALLOW;
357 state = EXPECTING_REF;
360 if (process_ref(reader, len, &list, flags, extra_have))
362 state = EXPECTING_SHALLOW;
364 case EXPECTING_SHALLOW:
365 if (process_shallow(reader, len, shallow_points))
367 die(_("protocol error: unexpected '%s'"), reader->line);
373 annotate_refs_with_symref_info(*orig_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)
383 struct object_id old_oid;
385 struct string_list line_sections = STRING_LIST_INIT_DUP;
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) {
400 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
406 ref = alloc_ref(line_sections.items[i++].string);
408 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
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;
421 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
422 reader->hash_algo) || *end) {
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);
433 *list = &peeled->next;
440 string_list_clear(&line_sections, 0);
444 void check_stateless_delimiter(int stateless_rpc,
445 struct packet_reader *reader,
449 return; /* not in stateless mode, no delimiter expected */
450 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
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,
461 const char *hash_name;
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);
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 */
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",
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"));
512 const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset)
519 len = strlen(feature);
521 feature_list += *offset;
522 while (*feature_list) {
523 const char *found = strstr(feature_list, feature);
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)) {
534 /* feature with a value (e.g., "agent=git/1.2.3") */
535 else if (*value == '=') {
539 end = strcspn(value, " \t\n");
543 *offset = value + end - feature_list;
547 * otherwise we matched a substring of another feature;
551 feature_list = found + 1;
556 int server_supports_hash(const char *desired, int *feature_supported)
562 hash = next_server_feature_value("object-format", &len, &offset);
563 if (feature_supported)
564 *feature_supported = !!hash;
566 hash = hash_algos[GIT_HASH_SHA1].name;
570 if (!xstrncmpz(desired, hash, len))
573 hash = next_server_feature_value("object-format", &len, &offset);
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);
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)
624 return "unknown protocol";
628 static enum protocol get_protocol(const char *name)
630 if (!strcmp(name, "ssh"))
632 if (!strcmp(name, "git"))
634 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
636 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
638 if (!strcmp(name, "file"))
640 die(_("protocol '%s' is not supported"), name);
643 static char *host_end(char **hoststart, int removebrackets)
645 char *host = *hoststart;
647 char *start = strstr(host, "@[");
649 start++; /* Jump over '@' */
652 if (start[0] == '[') {
653 end = strchr(start + 1, ']');
655 if (removebrackets) {
657 memmove(start, start + 1, end - start);
668 #define STR(s) STR_(s)
670 static void get_host_and_port(char **host, const char **port)
673 end = host_end(host, 1);
674 colon = strchr(end, ':');
676 long portnr = strtol(colon + 1, &end, 10);
677 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
680 } else if (!colon[1]) {
686 static void enable_keepalive(int sockfd)
690 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
691 error_errno(_("unable to set SO_KEEPALIVE on socket"));
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)");
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;
713 const char *port = STR(DEFAULT_GIT_PORT);
714 struct addrinfo hints, *ai0, *ai;
718 get_host_and_port(&host, &port);
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);
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);
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));
753 if (flags & CONNECT_VERBOSE)
754 fprintf(stderr, "%s ", ai_name(ai));
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);
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;
783 const char *port = STR(DEFAULT_GIT_PORT);
786 struct sockaddr_in sa;
791 get_host_and_port(&host, &port);
793 if (flags & CONNECT_VERBOSE)
794 fprintf(stderr, _("Looking up %s ... "), host);
796 he = gethostbyname(host);
798 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
799 nport = strtoul(port, &ep, 10);
800 if ( ep == port || *ep ) {
802 struct servent *se = getservbyname(port,"tcp");
804 die(_("unknown port %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);
820 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
821 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
824 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
831 if (flags & CONNECT_VERBOSE)
832 fprintf(stderr, "%s ",
833 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
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."));
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);
874 static char *git_proxy_command;
876 static int git_proxy_command_options(const char *var, const char *value,
879 if (!strcmp(var, "core.gitproxy")) {
883 const char *rhost_name = cb;
884 int rhost_len = strlen(rhost_name);
886 if (git_proxy_command)
889 return config_error_nonbool(var);
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 ");
898 /* matches everybody */
899 matchlen = strlen(value);
901 hostlen = strlen(for_pos + 5);
902 if (rhost_len < hostlen)
904 else if (!strncmp(for_pos + 5,
905 rhost_name + rhost_len - hostlen,
907 ((rhost_len == hostlen) ||
908 rhost_name[rhost_len - hostlen -1] == '.'))
909 matchlen = for_pos - value;
914 /* core.gitproxy = none for kernel.org */
916 !memcmp(value, "none", 4))
918 git_proxy_command = xmemdupz(value, matchlen);
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);
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 */
959 static char *get_port(char *host)
962 char *p = strchr(host, ':');
965 long port = strtol(p + 1, &end, 10);
966 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
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,
986 enum protocol protocol = PROTO_LOCAL;
988 if (is_url(url_orig))
989 url = url_decode(url_orig);
991 url = xstrdup(url_orig);
993 host = strstr(url, "://");
996 protocol = get_protocol(url);
1000 if (!url_is_local_not_ssh(url)) {
1001 protocol = PROTO_SSH;
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)
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" */
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) {
1039 path = xstrdup(path);
1042 *ret_host = xstrdup(host);
1048 static const char *get_ssh_command(void)
1052 if ((ssh = getenv("GIT_SSH_COMMAND")))
1055 if (!git_config_get_string_const("core.sshcommand", &ssh))
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))
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;
1088 *ssh_variant = VARIANT_SSH;
1091 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1094 enum ssh_variant ssh_variant = VARIANT_AUTO;
1095 const char *variant;
1098 override_ssh_variant(&ssh_variant);
1100 if (ssh_variant != VARIANT_AUTO)
1104 p = xstrdup(ssh_command);
1105 variant = basename(p);
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
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;
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,
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
1156 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1158 target_host = xstrdup(target_host);
1160 target_host = xstrdup(hostandport);
1162 transport_check_allowed("git");
1165 * These underlying connection commands die() if they
1168 if (git_use_proxy(hostandport))
1169 conn = git_proxy_connect(fd, hostandport);
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,
1184 /* If using a new version put that stuff here after a second null byte */
1186 strbuf_addch(&request, '\0');
1187 strbuf_addf(&request, "version=%d%c",
1191 packet_write(fd[1], request.buf, request.len);
1194 strbuf_release(&request);
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 &&
1208 strvec_push(args, "-o");
1209 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1210 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1214 if (flags & CONNECT_IPV4) {
1217 BUG("VARIANT_AUTO passed to push_ssh_options");
1218 case VARIANT_SIMPLE:
1219 die(_("ssh variant 'simple' does not support -4"));
1223 case VARIANT_TORTOISEPLINK:
1224 strvec_push(args, "-4");
1226 } else if (flags & CONNECT_IPV6) {
1229 BUG("VARIANT_AUTO passed to push_ssh_options");
1230 case VARIANT_SIMPLE:
1231 die(_("ssh variant 'simple' does not support -6"));
1235 case VARIANT_TORTOISEPLINK:
1236 strvec_push(args, "-6");
1240 if (variant == VARIANT_TORTOISEPLINK)
1241 strvec_push(args, "-batch");
1246 BUG("VARIANT_AUTO passed to push_ssh_options");
1247 case VARIANT_SIMPLE:
1248 die(_("ssh variant 'simple' does not support setting port"));
1250 strvec_push(args, "-p");
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,
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();
1275 variant = determine_ssh_variant(ssh, 1);
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");
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");
1349 } else if (protocol == PROTO_GIT) {
1350 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1351 conn->trace2_child_class = "transport/git";
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);
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");
1391 strbuf_release(&cmd);
1394 conn->trace2_child_class = "transport/ssh";
1395 fill_ssh_args(conn, ssh_host, port, version, flags);
1397 transport_check_allowed("file");
1398 conn->trace2_child_class = "transport/file";
1400 strvec_pushf(&conn->env_array,
1401 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
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);
1419 int finish_connect(struct child_process *conn)
1422 if (!conn || git_connection_is_socket(conn))
1425 code = finish_command(conn);