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 argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
21 static const char *parse_feature_value(const char *, const char *, int *, int *);
22 static const char *next_server_feature_value(const char *feature, int *len, int *offset);
24 static int check_ref(const char *name, unsigned int flags)
29 if (!skip_prefix(name, "refs/", &name))
32 /* REF_NORMAL means that we don't want the magic fake tag refs */
33 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
36 /* REF_HEADS means that we want regular branch heads */
37 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
40 /* REF_TAGS means that we want tags */
41 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
44 /* All type bits clear means that we are ok with anything */
45 return !(flags & ~REF_NORMAL);
48 int check_ref_type(const struct ref *ref, int flags)
50 return check_ref(ref->name, flags);
53 static NORETURN void die_initial_contact(int unexpected)
56 * A hang-up after seeing some response from the other end
57 * means that it is unexpected, as we know the other end is
58 * willing to talk to us. A hang-up before seeing any
59 * response does not necessarily mean an ACL problem, though.
62 die(_("the remote end hung up upon initial contact"));
64 die(_("Could not read from remote repository.\n\n"
65 "Please make sure you have the correct access rights\n"
66 "and the repository exists."));
69 /* Checks if the server supports the capability 'c' */
70 int server_supports_v2(const char *c, int die_on_error)
74 for (i = 0; i < server_capabilities_v2.argc; i++) {
76 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
77 (!*out || *out == '='))
82 die(_("server doesn't support '%s'"), c);
87 int server_feature_v2(const char *c, const char **v)
91 for (i = 0; i < server_capabilities_v2.argc; i++) {
93 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
102 int server_supports_feature(const char *c, const char *feature,
107 for (i = 0; i < server_capabilities_v2.argc; i++) {
109 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
110 (!*out || *(out++) == '=')) {
111 if (parse_feature_request(out, feature))
119 die(_("server doesn't support feature '%s'"), feature);
124 static void process_capabilities_v2(struct packet_reader *reader)
126 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
127 argv_array_push(&server_capabilities_v2, reader->line);
129 if (reader->status != PACKET_READ_FLUSH)
130 die(_("expected flush after capabilities"));
133 enum protocol_version discover_version(struct packet_reader *reader)
135 enum protocol_version version = protocol_unknown_version;
138 * Peek the first line of the server's response to
139 * determine the protocol version the server is speaking.
141 switch (packet_reader_peek(reader)) {
142 case PACKET_READ_EOF:
143 die_initial_contact(0);
144 case PACKET_READ_FLUSH:
145 case PACKET_READ_DELIM:
146 version = protocol_v0;
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 *line = reader->line;
225 int nul_location = strlen(line);
226 if (nul_location == *linelen)
228 server_capabilities_v1 = xstrdup(line + nul_location + 1);
229 *linelen = nul_location;
232 static int process_dummy_ref(const struct packet_reader *reader)
234 const char *line = reader->line;
235 struct object_id oid;
238 if (parse_oid_hex(line, &oid, &name))
244 return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
247 static void check_no_capabilities(const char *line, int len)
249 if (strlen(line) != len)
250 warning(_("ignoring capabilities after first line '%s'"),
251 line + strlen(line));
254 static int process_ref(const struct packet_reader *reader, int len,
255 struct ref ***list, unsigned int flags,
256 struct oid_array *extra_have)
258 const char *line = reader->line;
259 struct object_id old_oid;
262 if (parse_oid_hex(line, &old_oid, &name))
268 if (extra_have && !strcmp(name, ".have")) {
269 oid_array_append(extra_have, &old_oid);
270 } else if (!strcmp(name, "capabilities^{}")) {
271 die(_("protocol error: unexpected capabilities^{}"));
272 } else if (check_ref(name, flags)) {
273 struct ref *ref = alloc_ref(name);
274 oidcpy(&ref->old_oid, &old_oid);
278 check_no_capabilities(line, len);
282 static int process_shallow(const struct packet_reader *reader, int len,
283 struct oid_array *shallow_points)
285 const char *line = reader->line;
287 struct object_id old_oid;
289 if (!skip_prefix(line, "shallow ", &arg))
292 if (get_oid_hex(arg, &old_oid))
293 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
295 die(_("repository on the other end cannot be shallow"));
296 oid_array_append(shallow_points, &old_oid);
297 check_no_capabilities(line, len);
301 enum get_remote_heads_state {
302 EXPECTING_FIRST_REF = 0,
309 * Read all the refs from the other end
311 struct ref **get_remote_heads(struct packet_reader *reader,
312 struct ref **list, unsigned int flags,
313 struct oid_array *extra_have,
314 struct oid_array *shallow_points)
316 struct ref **orig_list = list;
318 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
322 while (state != EXPECTING_DONE) {
323 switch (packet_reader_read(reader)) {
324 case PACKET_READ_EOF:
325 die_initial_contact(1);
326 case PACKET_READ_NORMAL:
327 len = reader->pktlen;
329 case PACKET_READ_FLUSH:
330 state = EXPECTING_DONE;
332 case PACKET_READ_DELIM:
333 die(_("invalid packet"));
337 case EXPECTING_FIRST_REF:
338 process_capabilities(reader, &len);
339 if (process_dummy_ref(reader)) {
340 state = EXPECTING_SHALLOW;
343 state = EXPECTING_REF;
346 if (process_ref(reader, len, &list, flags, extra_have))
348 state = EXPECTING_SHALLOW;
350 case EXPECTING_SHALLOW:
351 if (process_shallow(reader, len, shallow_points))
353 die(_("protocol error: unexpected '%s'"), reader->line);
359 annotate_refs_with_symref_info(*orig_list);
364 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
365 static int process_ref_v2(const char *line, struct ref ***list)
369 struct object_id old_oid;
371 struct string_list line_sections = STRING_LIST_INIT_DUP;
375 * Ref lines have a number of fields which are space deliminated. The
376 * first field is the OID of the ref. The second field is the ref
377 * name. Subsequent fields (symref-target and peeled) are optional and
378 * don't have a particular order.
380 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
385 if (parse_oid_hex(line_sections.items[i++].string, &old_oid, &end) ||
391 ref = alloc_ref(line_sections.items[i++].string);
393 oidcpy(&ref->old_oid, &old_oid);
397 for (; i < line_sections.nr; i++) {
398 const char *arg = line_sections.items[i].string;
399 if (skip_prefix(arg, "symref-target:", &arg))
400 ref->symref = xstrdup(arg);
402 if (skip_prefix(arg, "peeled:", &arg)) {
403 struct object_id peeled_oid;
406 if (parse_oid_hex(arg, &peeled_oid, &end) || *end) {
411 peeled_name = xstrfmt("%s^{}", ref->name);
412 peeled = alloc_ref(peeled_name);
414 oidcpy(&peeled->old_oid, &peeled_oid);
416 *list = &peeled->next;
423 string_list_clear(&line_sections, 0);
427 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
428 struct ref **list, int for_push,
429 const struct argv_array *ref_prefixes,
430 const struct string_list *server_options)
435 if (server_supports_v2("ls-refs", 1))
436 packet_write_fmt(fd_out, "command=ls-refs\n");
438 if (server_supports_v2("agent", 0))
439 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
441 if (server_options && server_options->nr &&
442 server_supports_v2("server-option", 1))
443 for (i = 0; i < server_options->nr; i++)
444 packet_write_fmt(fd_out, "server-option=%s",
445 server_options->items[i].string);
447 packet_delim(fd_out);
448 /* When pushing we don't want to request the peeled tags */
450 packet_write_fmt(fd_out, "peel\n");
451 packet_write_fmt(fd_out, "symrefs\n");
452 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
453 packet_write_fmt(fd_out, "ref-prefix %s\n",
454 ref_prefixes->argv[i]);
456 packet_flush(fd_out);
458 /* Process response from server */
459 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
460 if (!process_ref_v2(reader->line, &list))
461 die(_("invalid ls-refs response: %s"), reader->line);
464 if (reader->status != PACKET_READ_FLUSH)
465 die(_("expected flush after ref listing"));
470 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset)
477 len = strlen(feature);
479 feature_list += *offset;
480 while (*feature_list) {
481 const char *found = strstr(feature_list, feature);
484 if (feature_list == found || isspace(found[-1])) {
485 const char *value = found + len;
486 /* feature with no value (e.g., "thin-pack") */
487 if (!*value || isspace(*value)) {
492 /* feature with a value (e.g., "agent=git/1.2.3") */
493 else if (*value == '=') {
497 end = strcspn(value, " \t\n");
501 *offset = value + end - feature_list;
505 * otherwise we matched a substring of another feature;
509 feature_list = found + 1;
514 int parse_feature_request(const char *feature_list, const char *feature)
516 return !!parse_feature_value(feature_list, feature, NULL, NULL);
519 static const char *next_server_feature_value(const char *feature, int *len, int *offset)
521 return parse_feature_value(server_capabilities_v1, feature, len, offset);
524 const char *server_feature_value(const char *feature, int *len)
526 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
529 int server_supports(const char *feature)
531 return !!server_feature_value(feature, NULL);
541 int url_is_local_not_ssh(const char *url)
543 const char *colon = strchr(url, ':');
544 const char *slash = strchr(url, '/');
545 return !colon || (slash && slash < colon) ||
546 (has_dos_drive_prefix(url) && is_valid_path(url));
549 static const char *prot_name(enum protocol protocol)
560 return "unknown protocol";
564 static enum protocol get_protocol(const char *name)
566 if (!strcmp(name, "ssh"))
568 if (!strcmp(name, "git"))
570 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
572 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
574 if (!strcmp(name, "file"))
576 die(_("protocol '%s' is not supported"), name);
579 static char *host_end(char **hoststart, int removebrackets)
581 char *host = *hoststart;
583 char *start = strstr(host, "@[");
585 start++; /* Jump over '@' */
588 if (start[0] == '[') {
589 end = strchr(start + 1, ']');
591 if (removebrackets) {
593 memmove(start, start + 1, end - start);
604 #define STR(s) STR_(s)
606 static void get_host_and_port(char **host, const char **port)
609 end = host_end(host, 1);
610 colon = strchr(end, ':');
612 long portnr = strtol(colon + 1, &end, 10);
613 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
616 } else if (!colon[1]) {
622 static void enable_keepalive(int sockfd)
626 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
627 error_errno(_("unable to set SO_KEEPALIVE on socket"));
632 static const char *ai_name(const struct addrinfo *ai)
634 static char addr[NI_MAXHOST];
635 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
636 NI_NUMERICHOST) != 0)
637 xsnprintf(addr, sizeof(addr), "(unknown)");
643 * Returns a connected socket() fd, or else die()s.
645 static int git_tcp_connect_sock(char *host, int flags)
647 struct strbuf error_message = STRBUF_INIT;
649 const char *port = STR(DEFAULT_GIT_PORT);
650 struct addrinfo hints, *ai0, *ai;
654 get_host_and_port(&host, &port);
658 memset(&hints, 0, sizeof(hints));
659 if (flags & CONNECT_IPV4)
660 hints.ai_family = AF_INET;
661 else if (flags & CONNECT_IPV6)
662 hints.ai_family = AF_INET6;
663 hints.ai_socktype = SOCK_STREAM;
664 hints.ai_protocol = IPPROTO_TCP;
666 if (flags & CONNECT_VERBOSE)
667 fprintf(stderr, _("Looking up %s ... "), host);
669 gai = getaddrinfo(host, port, &hints, &ai);
671 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
673 if (flags & CONNECT_VERBOSE)
674 /* TRANSLATORS: this is the end of "Looking up %s ... " */
675 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
677 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
678 sockfd = socket(ai->ai_family,
679 ai->ai_socktype, ai->ai_protocol);
681 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
682 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
683 host, cnt, ai_name(ai), strerror(errno));
689 if (flags & CONNECT_VERBOSE)
690 fprintf(stderr, "%s ", ai_name(ai));
697 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
699 enable_keepalive(sockfd);
701 if (flags & CONNECT_VERBOSE)
702 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
703 fprintf_ln(stderr, _("done."));
705 strbuf_release(&error_message);
713 * Returns a connected socket() fd, or else die()s.
715 static int git_tcp_connect_sock(char *host, int flags)
717 struct strbuf error_message = STRBUF_INIT;
719 const char *port = STR(DEFAULT_GIT_PORT);
722 struct sockaddr_in sa;
727 get_host_and_port(&host, &port);
729 if (flags & CONNECT_VERBOSE)
730 fprintf(stderr, _("Looking up %s ... "), host);
732 he = gethostbyname(host);
734 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
735 nport = strtoul(port, &ep, 10);
736 if ( ep == port || *ep ) {
738 struct servent *se = getservbyname(port,"tcp");
740 die(_("unknown port %s"), port);
744 if (flags & CONNECT_VERBOSE)
745 /* TRANSLATORS: this is the end of "Looking up %s ... " */
746 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
748 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
749 memset(&sa, 0, sizeof sa);
750 sa.sin_family = he->h_addrtype;
751 sa.sin_port = htons(nport);
752 memcpy(&sa.sin_addr, *ap, he->h_length);
754 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
756 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
757 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
760 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
767 if (flags & CONNECT_VERBOSE)
768 fprintf(stderr, "%s ",
769 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
774 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
776 enable_keepalive(sockfd);
778 if (flags & CONNECT_VERBOSE)
779 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
780 fprintf_ln(stderr, _("done."));
789 * Dummy child_process returned by git_connect() if the transport protocol
790 * does not need fork(2).
792 static struct child_process no_fork = CHILD_PROCESS_INIT;
794 int git_connection_is_socket(struct child_process *conn)
796 return conn == &no_fork;
799 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
801 int sockfd = git_tcp_connect_sock(host, flags);
810 static char *git_proxy_command;
812 static int git_proxy_command_options(const char *var, const char *value,
815 if (!strcmp(var, "core.gitproxy")) {
819 const char *rhost_name = cb;
820 int rhost_len = strlen(rhost_name);
822 if (git_proxy_command)
825 return config_error_nonbool(var);
827 * ;# matches www.kernel.org as well
828 * gitproxy = netcatter-1 for kernel.org
829 * gitproxy = netcatter-2 for sample.xz
830 * gitproxy = netcatter-default
832 for_pos = strstr(value, " for ");
834 /* matches everybody */
835 matchlen = strlen(value);
837 hostlen = strlen(for_pos + 5);
838 if (rhost_len < hostlen)
840 else if (!strncmp(for_pos + 5,
841 rhost_name + rhost_len - hostlen,
843 ((rhost_len == hostlen) ||
844 rhost_name[rhost_len - hostlen -1] == '.'))
845 matchlen = for_pos - value;
850 /* core.gitproxy = none for kernel.org */
852 !memcmp(value, "none", 4))
854 git_proxy_command = xmemdupz(value, matchlen);
859 return git_default_config(var, value, cb);
862 static int git_use_proxy(const char *host)
864 git_proxy_command = getenv("GIT_PROXY_COMMAND");
865 git_config(git_proxy_command_options, (void*)host);
866 return (git_proxy_command && *git_proxy_command);
869 static struct child_process *git_proxy_connect(int fd[2], char *host)
871 const char *port = STR(DEFAULT_GIT_PORT);
872 struct child_process *proxy;
874 get_host_and_port(&host, &port);
876 if (looks_like_command_line_option(host))
877 die(_("strange hostname '%s' blocked"), host);
878 if (looks_like_command_line_option(port))
879 die(_("strange port '%s' blocked"), port);
881 proxy = xmalloc(sizeof(*proxy));
882 child_process_init(proxy);
883 argv_array_push(&proxy->args, git_proxy_command);
884 argv_array_push(&proxy->args, host);
885 argv_array_push(&proxy->args, port);
888 if (start_command(proxy))
889 die(_("cannot start proxy %s"), git_proxy_command);
890 fd[0] = proxy->out; /* read from proxy stdout */
891 fd[1] = proxy->in; /* write to proxy stdin */
895 static char *get_port(char *host)
898 char *p = strchr(host, ':');
901 long port = strtol(p + 1, &end, 10);
902 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
912 * Extract protocol and relevant parts from the specified connection URL.
913 * The caller must free() the returned strings.
915 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
922 enum protocol protocol = PROTO_LOCAL;
924 if (is_url(url_orig))
925 url = url_decode(url_orig);
927 url = xstrdup(url_orig);
929 host = strstr(url, "://");
932 protocol = get_protocol(url);
936 if (!url_is_local_not_ssh(url)) {
937 protocol = PROTO_SSH;
943 * Don't do destructive transforms as protocol code does
944 * '[]' unwrapping in get_host_and_port()
946 end = host_end(&host, 0);
948 if (protocol == PROTO_LOCAL)
950 else if (protocol == PROTO_FILE && *host != '/' &&
951 !has_dos_drive_prefix(host) &&
952 offset_1st_component(host - 2) > 1)
953 path = host - 2; /* include the leading "//" */
954 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
955 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
957 path = strchr(end, separator);
960 die(_("no path specified; see 'git help pull' for valid url syntax"));
963 * null-terminate hostname and point path to ~ for URL's like this:
964 * ssh://host.xz/~user/repo
967 end = path; /* Need to \0 terminate host here */
968 if (separator == ':')
969 path++; /* path starts after ':' */
970 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
975 path = xstrdup(path);
978 *ret_host = xstrdup(host);
984 static const char *get_ssh_command(void)
988 if ((ssh = getenv("GIT_SSH_COMMAND")))
991 if (!git_config_get_string_const("core.sshcommand", &ssh))
1003 VARIANT_TORTOISEPLINK,
1006 static void override_ssh_variant(enum ssh_variant *ssh_variant)
1008 const char *variant = getenv("GIT_SSH_VARIANT");
1010 if (!variant && git_config_get_string_const("ssh.variant", &variant))
1013 if (!strcmp(variant, "auto"))
1014 *ssh_variant = VARIANT_AUTO;
1015 else if (!strcmp(variant, "plink"))
1016 *ssh_variant = VARIANT_PLINK;
1017 else if (!strcmp(variant, "putty"))
1018 *ssh_variant = VARIANT_PUTTY;
1019 else if (!strcmp(variant, "tortoiseplink"))
1020 *ssh_variant = VARIANT_TORTOISEPLINK;
1021 else if (!strcmp(variant, "simple"))
1022 *ssh_variant = VARIANT_SIMPLE;
1024 *ssh_variant = VARIANT_SSH;
1027 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1030 enum ssh_variant ssh_variant = VARIANT_AUTO;
1031 const char *variant;
1034 override_ssh_variant(&ssh_variant);
1036 if (ssh_variant != VARIANT_AUTO)
1040 p = xstrdup(ssh_command);
1041 variant = basename(p);
1043 const char **ssh_argv;
1045 p = xstrdup(ssh_command);
1046 if (split_cmdline(p, &ssh_argv) > 0) {
1047 variant = basename((char *)ssh_argv[0]);
1049 * At this point, variant points into the buffer
1050 * referenced by p, hence we do not need ssh_argv
1060 if (!strcasecmp(variant, "ssh") ||
1061 !strcasecmp(variant, "ssh.exe"))
1062 ssh_variant = VARIANT_SSH;
1063 else if (!strcasecmp(variant, "plink") ||
1064 !strcasecmp(variant, "plink.exe"))
1065 ssh_variant = VARIANT_PLINK;
1066 else if (!strcasecmp(variant, "tortoiseplink") ||
1067 !strcasecmp(variant, "tortoiseplink.exe"))
1068 ssh_variant = VARIANT_TORTOISEPLINK;
1075 * Open a connection using Git's native protocol.
1077 * The caller is responsible for freeing hostandport, but this function may
1078 * modify it (for example, to truncate it to remove the port part).
1080 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1081 const char *path, const char *prog,
1082 enum protocol_version version,
1085 struct child_process *conn;
1086 struct strbuf request = STRBUF_INIT;
1088 * Set up virtual host information based on where we will
1089 * connect, unless the user has overridden us in
1092 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1094 target_host = xstrdup(target_host);
1096 target_host = xstrdup(hostandport);
1098 transport_check_allowed("git");
1101 * These underlying connection commands die() if they
1104 if (git_use_proxy(hostandport))
1105 conn = git_proxy_connect(fd, hostandport);
1107 conn = git_tcp_connect(fd, hostandport, flags);
1109 * Separate original protocol components prog and path
1110 * from extended host header with a NUL byte.
1112 * Note: Do not add any other headers here! Doing so
1113 * will cause older git-daemon servers to crash.
1115 strbuf_addf(&request,
1120 /* If using a new version put that stuff here after a second null byte */
1122 strbuf_addch(&request, '\0');
1123 strbuf_addf(&request, "version=%d%c",
1127 packet_write(fd[1], request.buf, request.len);
1130 strbuf_release(&request);
1135 * Append the appropriate environment variables to `env` and options to
1136 * `args` for running ssh in Git's SSH-tunneled transport.
1138 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1139 enum ssh_variant variant, const char *port,
1140 enum protocol_version version, int flags)
1142 if (variant == VARIANT_SSH &&
1144 argv_array_push(args, "-o");
1145 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1146 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1150 if (flags & CONNECT_IPV4) {
1153 BUG("VARIANT_AUTO passed to push_ssh_options");
1154 case VARIANT_SIMPLE:
1155 die(_("ssh variant 'simple' does not support -4"));
1159 case VARIANT_TORTOISEPLINK:
1160 argv_array_push(args, "-4");
1162 } else if (flags & CONNECT_IPV6) {
1165 BUG("VARIANT_AUTO passed to push_ssh_options");
1166 case VARIANT_SIMPLE:
1167 die(_("ssh variant 'simple' does not support -6"));
1171 case VARIANT_TORTOISEPLINK:
1172 argv_array_push(args, "-6");
1176 if (variant == VARIANT_TORTOISEPLINK)
1177 argv_array_push(args, "-batch");
1182 BUG("VARIANT_AUTO passed to push_ssh_options");
1183 case VARIANT_SIMPLE:
1184 die(_("ssh variant 'simple' does not support setting port"));
1186 argv_array_push(args, "-p");
1190 case VARIANT_TORTOISEPLINK:
1191 argv_array_push(args, "-P");
1194 argv_array_push(args, port);
1198 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1199 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1200 const char *port, enum protocol_version version,
1204 enum ssh_variant variant;
1206 if (looks_like_command_line_option(ssh_host))
1207 die(_("strange hostname '%s' blocked"), ssh_host);
1209 ssh = get_ssh_command();
1211 variant = determine_ssh_variant(ssh, 1);
1214 * GIT_SSH is the no-shell version of
1215 * GIT_SSH_COMMAND (and must remain so for
1216 * historical compatibility).
1218 conn->use_shell = 0;
1220 ssh = getenv("GIT_SSH");
1223 variant = determine_ssh_variant(ssh, 0);
1226 if (variant == VARIANT_AUTO) {
1227 struct child_process detect = CHILD_PROCESS_INIT;
1229 detect.use_shell = conn->use_shell;
1230 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1232 argv_array_push(&detect.args, ssh);
1233 argv_array_push(&detect.args, "-G");
1234 push_ssh_options(&detect.args, &detect.env_array,
1235 VARIANT_SSH, port, version, flags);
1236 argv_array_push(&detect.args, ssh_host);
1238 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1241 argv_array_push(&conn->args, ssh);
1242 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
1243 argv_array_push(&conn->args, ssh_host);
1247 * This returns the dummy child_process `no_fork` if the transport protocol
1248 * does not need fork(2), or a struct child_process object if it does. Once
1249 * done, finish the connection with finish_connect() with the value returned
1250 * from this function (it is safe to call finish_connect() with NULL to
1251 * support the former case).
1253 * If it returns, the connect is successful; it just dies on errors (this
1254 * will hopefully be changed in a libification effort, to return NULL when
1255 * the connection failed).
1257 struct child_process *git_connect(int fd[2], const char *url,
1258 const char *prog, int flags)
1260 char *hostandport, *path;
1261 struct child_process *conn;
1262 enum protocol protocol;
1263 enum protocol_version version = get_protocol_version_config();
1266 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1267 * to perform a push, then fallback to v0 since the client doesn't know
1268 * how to push yet using v2.
1270 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1271 version = protocol_v0;
1273 /* Without this we cannot rely on waitpid() to tell
1274 * what happened to our children.
1276 signal(SIGCHLD, SIG_DFL);
1278 protocol = parse_connect_url(url, &hostandport, &path);
1279 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1280 printf("Diag: url=%s\n", url ? url : "NULL");
1281 printf("Diag: protocol=%s\n", prot_name(protocol));
1282 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1283 printf("Diag: path=%s\n", path ? path : "NULL");
1285 } else if (protocol == PROTO_GIT) {
1286 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1287 conn->trace2_child_class = "transport/git";
1289 struct strbuf cmd = STRBUF_INIT;
1290 const char *const *var;
1292 conn = xmalloc(sizeof(*conn));
1293 child_process_init(conn);
1295 if (looks_like_command_line_option(path))
1296 die(_("strange pathname '%s' blocked"), path);
1298 strbuf_addstr(&cmd, prog);
1299 strbuf_addch(&cmd, ' ');
1300 sq_quote_buf(&cmd, path);
1302 /* remove repo-local variables from the environment */
1303 for (var = local_repo_env; *var; var++)
1304 argv_array_push(&conn->env_array, *var);
1306 conn->use_shell = 1;
1307 conn->in = conn->out = -1;
1308 if (protocol == PROTO_SSH) {
1309 char *ssh_host = hostandport;
1310 const char *port = NULL;
1311 transport_check_allowed("ssh");
1312 get_host_and_port(&ssh_host, &port);
1315 port = get_port(ssh_host);
1317 if (flags & CONNECT_DIAG_URL) {
1318 printf("Diag: url=%s\n", url ? url : "NULL");
1319 printf("Diag: protocol=%s\n", prot_name(protocol));
1320 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1321 printf("Diag: port=%s\n", port ? port : "NONE");
1322 printf("Diag: path=%s\n", path ? path : "NULL");
1327 strbuf_release(&cmd);
1330 conn->trace2_child_class = "transport/ssh";
1331 fill_ssh_args(conn, ssh_host, port, version, flags);
1333 transport_check_allowed("file");
1334 conn->trace2_child_class = "transport/file";
1336 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1340 argv_array_push(&conn->args, cmd.buf);
1342 if (start_command(conn))
1343 die(_("unable to fork"));
1345 fd[0] = conn->out; /* read from child's stdout */
1346 fd[1] = conn->in; /* write to child's stdin */
1347 strbuf_release(&cmd);
1354 int finish_connect(struct child_process *conn)
1357 if (!conn || git_connection_is_socket(conn))
1360 code = finish_command(conn);