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 *);
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.argc; i++) {
75 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
76 (!*out || *out == '='))
81 die(_("server doesn't support '%s'"), c);
86 int server_supports_feature(const char *c, const char *feature,
91 for (i = 0; i < server_capabilities_v2.argc; i++) {
93 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
94 (!*out || *(out++) == '=')) {
95 if (parse_feature_request(out, feature))
103 die(_("server doesn't support feature '%s'"), feature);
108 static void process_capabilities_v2(struct packet_reader *reader)
110 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
111 argv_array_push(&server_capabilities_v2, reader->line);
113 if (reader->status != PACKET_READ_FLUSH)
114 die(_("expected flush after capabilities"));
117 enum protocol_version discover_version(struct packet_reader *reader)
119 enum protocol_version version = protocol_unknown_version;
122 * Peek the first line of the server's response to
123 * determine the protocol version the server is speaking.
125 switch (packet_reader_peek(reader)) {
126 case PACKET_READ_EOF:
127 die_initial_contact(0);
128 case PACKET_READ_FLUSH:
129 case PACKET_READ_DELIM:
130 case PACKET_READ_RESPONSE_END:
131 version = protocol_v0;
133 case PACKET_READ_NORMAL:
134 version = determine_protocol_version_client(reader->line);
140 process_capabilities_v2(reader);
143 /* Read the peeked version line */
144 packet_reader_read(reader);
148 case protocol_unknown_version:
149 BUG("unknown protocol version");
155 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
158 struct string_list_item *item;
161 return; /* just "symref" */
162 /* e.g. "symref=HEAD:refs/heads/master" */
163 sym = xmemdupz(val, len);
164 target = strchr(sym, ':');
166 /* just "symref=something" */
169 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
170 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
171 /* "symref=bogus:pair */
173 item = string_list_append_nodup(symref, sym);
181 static void annotate_refs_with_symref_info(struct ref *ref)
183 struct string_list symref = STRING_LIST_INIT_DUP;
184 const char *feature_list = server_capabilities_v1;
186 while (feature_list) {
190 val = parse_feature_value(feature_list, "symref", &len);
193 parse_one_symref_info(&symref, val, len);
194 feature_list = val + 1;
196 string_list_sort(&symref);
198 for (; ref; ref = ref->next) {
199 struct string_list_item *item;
200 item = string_list_lookup(&symref, ref->name);
203 ref->symref = xstrdup((char *)item->util);
205 string_list_clear(&symref, 0);
208 static void process_capabilities(const char *line, int *len)
210 int nul_location = strlen(line);
211 if (nul_location == *len)
213 server_capabilities_v1 = xstrdup(line + nul_location + 1);
217 static int process_dummy_ref(const char *line)
219 struct object_id oid;
222 if (parse_oid_hex(line, &oid, &name))
228 return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
231 static void check_no_capabilities(const char *line, int len)
233 if (strlen(line) != len)
234 warning(_("ignoring capabilities after first line '%s'"),
235 line + strlen(line));
238 static int process_ref(const char *line, int len, struct ref ***list,
239 unsigned int flags, struct oid_array *extra_have)
241 struct object_id old_oid;
244 if (parse_oid_hex(line, &old_oid, &name))
250 if (extra_have && !strcmp(name, ".have")) {
251 oid_array_append(extra_have, &old_oid);
252 } else if (!strcmp(name, "capabilities^{}")) {
253 die(_("protocol error: unexpected capabilities^{}"));
254 } else if (check_ref(name, flags)) {
255 struct ref *ref = alloc_ref(name);
256 oidcpy(&ref->old_oid, &old_oid);
260 check_no_capabilities(line, len);
264 static int process_shallow(const char *line, int len,
265 struct oid_array *shallow_points)
268 struct object_id old_oid;
270 if (!skip_prefix(line, "shallow ", &arg))
273 if (get_oid_hex(arg, &old_oid))
274 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
276 die(_("repository on the other end cannot be shallow"));
277 oid_array_append(shallow_points, &old_oid);
278 check_no_capabilities(line, len);
282 enum get_remote_heads_state {
283 EXPECTING_FIRST_REF = 0,
290 * Read all the refs from the other end
292 struct ref **get_remote_heads(struct packet_reader *reader,
293 struct ref **list, unsigned int flags,
294 struct oid_array *extra_have,
295 struct oid_array *shallow_points)
297 struct ref **orig_list = list;
299 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
303 while (state != EXPECTING_DONE) {
304 switch (packet_reader_read(reader)) {
305 case PACKET_READ_EOF:
306 die_initial_contact(1);
307 case PACKET_READ_NORMAL:
308 len = reader->pktlen;
310 case PACKET_READ_FLUSH:
311 state = EXPECTING_DONE;
313 case PACKET_READ_DELIM:
314 case PACKET_READ_RESPONSE_END:
315 die(_("invalid packet"));
319 case EXPECTING_FIRST_REF:
320 process_capabilities(reader->line, &len);
321 if (process_dummy_ref(reader->line)) {
322 state = EXPECTING_SHALLOW;
325 state = EXPECTING_REF;
328 if (process_ref(reader->line, len, &list, flags, extra_have))
330 state = EXPECTING_SHALLOW;
332 case EXPECTING_SHALLOW:
333 if (process_shallow(reader->line, len, shallow_points))
335 die(_("protocol error: unexpected '%s'"), reader->line);
341 annotate_refs_with_symref_info(*orig_list);
346 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
347 static int process_ref_v2(const char *line, struct ref ***list)
351 struct object_id old_oid;
353 struct string_list line_sections = STRING_LIST_INIT_DUP;
357 * Ref lines have a number of fields which are space deliminated. The
358 * first field is the OID of the ref. The second field is the ref
359 * name. Subsequent fields (symref-target and peeled) are optional and
360 * don't have a particular order.
362 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
367 if (parse_oid_hex(line_sections.items[i++].string, &old_oid, &end) ||
373 ref = alloc_ref(line_sections.items[i++].string);
375 oidcpy(&ref->old_oid, &old_oid);
379 for (; i < line_sections.nr; i++) {
380 const char *arg = line_sections.items[i].string;
381 if (skip_prefix(arg, "symref-target:", &arg))
382 ref->symref = xstrdup(arg);
384 if (skip_prefix(arg, "peeled:", &arg)) {
385 struct object_id peeled_oid;
388 if (parse_oid_hex(arg, &peeled_oid, &end) || *end) {
393 peeled_name = xstrfmt("%s^{}", ref->name);
394 peeled = alloc_ref(peeled_name);
396 oidcpy(&peeled->old_oid, &peeled_oid);
398 *list = &peeled->next;
405 string_list_clear(&line_sections, 0);
409 void check_stateless_delimiter(int stateless_rpc,
410 struct packet_reader *reader,
414 return; /* not in stateless mode, no delimiter expected */
415 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
419 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
420 struct ref **list, int for_push,
421 const struct argv_array *ref_prefixes,
422 const struct string_list *server_options,
428 if (server_supports_v2("ls-refs", 1))
429 packet_write_fmt(fd_out, "command=ls-refs\n");
431 if (server_supports_v2("agent", 0))
432 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
434 if (server_options && server_options->nr &&
435 server_supports_v2("server-option", 1))
436 for (i = 0; i < server_options->nr; i++)
437 packet_write_fmt(fd_out, "server-option=%s",
438 server_options->items[i].string);
440 packet_delim(fd_out);
441 /* When pushing we don't want to request the peeled tags */
443 packet_write_fmt(fd_out, "peel\n");
444 packet_write_fmt(fd_out, "symrefs\n");
445 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
446 packet_write_fmt(fd_out, "ref-prefix %s\n",
447 ref_prefixes->argv[i]);
449 packet_flush(fd_out);
451 /* Process response from server */
452 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
453 if (!process_ref_v2(reader->line, &list))
454 die(_("invalid ls-refs response: %s"), reader->line);
457 if (reader->status != PACKET_READ_FLUSH)
458 die(_("expected flush after ref listing"));
460 check_stateless_delimiter(stateless_rpc, reader,
461 _("expected response end packet after ref listing"));
466 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
473 len = strlen(feature);
474 while (*feature_list) {
475 const char *found = strstr(feature_list, feature);
478 if (feature_list == found || isspace(found[-1])) {
479 const char *value = found + len;
480 /* feature with no value (e.g., "thin-pack") */
481 if (!*value || isspace(*value)) {
486 /* feature with a value (e.g., "agent=git/1.2.3") */
487 else if (*value == '=') {
490 *lenp = strcspn(value, " \t\n");
494 * otherwise we matched a substring of another feature;
498 feature_list = found + 1;
503 int parse_feature_request(const char *feature_list, const char *feature)
505 return !!parse_feature_value(feature_list, feature, NULL);
508 const char *server_feature_value(const char *feature, int *len)
510 return parse_feature_value(server_capabilities_v1, feature, len);
513 int server_supports(const char *feature)
515 return !!server_feature_value(feature, NULL);
525 int url_is_local_not_ssh(const char *url)
527 const char *colon = strchr(url, ':');
528 const char *slash = strchr(url, '/');
529 return !colon || (slash && slash < colon) ||
530 (has_dos_drive_prefix(url) && is_valid_path(url));
533 static const char *prot_name(enum protocol protocol)
544 return "unknown protocol";
548 static enum protocol get_protocol(const char *name)
550 if (!strcmp(name, "ssh"))
552 if (!strcmp(name, "git"))
554 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
556 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
558 if (!strcmp(name, "file"))
560 die(_("protocol '%s' is not supported"), name);
563 static char *host_end(char **hoststart, int removebrackets)
565 char *host = *hoststart;
567 char *start = strstr(host, "@[");
569 start++; /* Jump over '@' */
572 if (start[0] == '[') {
573 end = strchr(start + 1, ']');
575 if (removebrackets) {
577 memmove(start, start + 1, end - start);
588 #define STR(s) STR_(s)
590 static void get_host_and_port(char **host, const char **port)
593 end = host_end(host, 1);
594 colon = strchr(end, ':');
596 long portnr = strtol(colon + 1, &end, 10);
597 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
600 } else if (!colon[1]) {
606 static void enable_keepalive(int sockfd)
610 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
611 error_errno(_("unable to set SO_KEEPALIVE on socket"));
616 static const char *ai_name(const struct addrinfo *ai)
618 static char addr[NI_MAXHOST];
619 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
620 NI_NUMERICHOST) != 0)
621 xsnprintf(addr, sizeof(addr), "(unknown)");
627 * Returns a connected socket() fd, or else die()s.
629 static int git_tcp_connect_sock(char *host, int flags)
631 struct strbuf error_message = STRBUF_INIT;
633 const char *port = STR(DEFAULT_GIT_PORT);
634 struct addrinfo hints, *ai0, *ai;
638 get_host_and_port(&host, &port);
642 memset(&hints, 0, sizeof(hints));
643 if (flags & CONNECT_IPV4)
644 hints.ai_family = AF_INET;
645 else if (flags & CONNECT_IPV6)
646 hints.ai_family = AF_INET6;
647 hints.ai_socktype = SOCK_STREAM;
648 hints.ai_protocol = IPPROTO_TCP;
650 if (flags & CONNECT_VERBOSE)
651 fprintf(stderr, _("Looking up %s ... "), host);
653 gai = getaddrinfo(host, port, &hints, &ai);
655 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
657 if (flags & CONNECT_VERBOSE)
658 /* TRANSLATORS: this is the end of "Looking up %s ... " */
659 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
661 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
662 sockfd = socket(ai->ai_family,
663 ai->ai_socktype, ai->ai_protocol);
665 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
666 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
667 host, cnt, ai_name(ai), strerror(errno));
673 if (flags & CONNECT_VERBOSE)
674 fprintf(stderr, "%s ", ai_name(ai));
681 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
683 enable_keepalive(sockfd);
685 if (flags & CONNECT_VERBOSE)
686 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
687 fprintf_ln(stderr, _("done."));
689 strbuf_release(&error_message);
697 * Returns a connected socket() fd, or else die()s.
699 static int git_tcp_connect_sock(char *host, int flags)
701 struct strbuf error_message = STRBUF_INIT;
703 const char *port = STR(DEFAULT_GIT_PORT);
706 struct sockaddr_in sa;
711 get_host_and_port(&host, &port);
713 if (flags & CONNECT_VERBOSE)
714 fprintf(stderr, _("Looking up %s ... "), host);
716 he = gethostbyname(host);
718 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
719 nport = strtoul(port, &ep, 10);
720 if ( ep == port || *ep ) {
722 struct servent *se = getservbyname(port,"tcp");
724 die(_("unknown port %s"), port);
728 if (flags & CONNECT_VERBOSE)
729 /* TRANSLATORS: this is the end of "Looking up %s ... " */
730 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
732 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
733 memset(&sa, 0, sizeof sa);
734 sa.sin_family = he->h_addrtype;
735 sa.sin_port = htons(nport);
736 memcpy(&sa.sin_addr, *ap, he->h_length);
738 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
740 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
741 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
744 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
751 if (flags & CONNECT_VERBOSE)
752 fprintf(stderr, "%s ",
753 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
758 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
760 enable_keepalive(sockfd);
762 if (flags & CONNECT_VERBOSE)
763 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
764 fprintf_ln(stderr, _("done."));
773 * Dummy child_process returned by git_connect() if the transport protocol
774 * does not need fork(2).
776 static struct child_process no_fork = CHILD_PROCESS_INIT;
778 int git_connection_is_socket(struct child_process *conn)
780 return conn == &no_fork;
783 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
785 int sockfd = git_tcp_connect_sock(host, flags);
794 static char *git_proxy_command;
796 static int git_proxy_command_options(const char *var, const char *value,
799 if (!strcmp(var, "core.gitproxy")) {
803 const char *rhost_name = cb;
804 int rhost_len = strlen(rhost_name);
806 if (git_proxy_command)
809 return config_error_nonbool(var);
811 * ;# matches www.kernel.org as well
812 * gitproxy = netcatter-1 for kernel.org
813 * gitproxy = netcatter-2 for sample.xz
814 * gitproxy = netcatter-default
816 for_pos = strstr(value, " for ");
818 /* matches everybody */
819 matchlen = strlen(value);
821 hostlen = strlen(for_pos + 5);
822 if (rhost_len < hostlen)
824 else if (!strncmp(for_pos + 5,
825 rhost_name + rhost_len - hostlen,
827 ((rhost_len == hostlen) ||
828 rhost_name[rhost_len - hostlen -1] == '.'))
829 matchlen = for_pos - value;
834 /* core.gitproxy = none for kernel.org */
836 !memcmp(value, "none", 4))
838 git_proxy_command = xmemdupz(value, matchlen);
843 return git_default_config(var, value, cb);
846 static int git_use_proxy(const char *host)
848 git_proxy_command = getenv("GIT_PROXY_COMMAND");
849 git_config(git_proxy_command_options, (void*)host);
850 return (git_proxy_command && *git_proxy_command);
853 static struct child_process *git_proxy_connect(int fd[2], char *host)
855 const char *port = STR(DEFAULT_GIT_PORT);
856 struct child_process *proxy;
858 get_host_and_port(&host, &port);
860 if (looks_like_command_line_option(host))
861 die(_("strange hostname '%s' blocked"), host);
862 if (looks_like_command_line_option(port))
863 die(_("strange port '%s' blocked"), port);
865 proxy = xmalloc(sizeof(*proxy));
866 child_process_init(proxy);
867 argv_array_push(&proxy->args, git_proxy_command);
868 argv_array_push(&proxy->args, host);
869 argv_array_push(&proxy->args, port);
872 if (start_command(proxy))
873 die(_("cannot start proxy %s"), git_proxy_command);
874 fd[0] = proxy->out; /* read from proxy stdout */
875 fd[1] = proxy->in; /* write to proxy stdin */
879 static char *get_port(char *host)
882 char *p = strchr(host, ':');
885 long port = strtol(p + 1, &end, 10);
886 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
896 * Extract protocol and relevant parts from the specified connection URL.
897 * The caller must free() the returned strings.
899 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
906 enum protocol protocol = PROTO_LOCAL;
908 if (is_url(url_orig))
909 url = url_decode(url_orig);
911 url = xstrdup(url_orig);
913 host = strstr(url, "://");
916 protocol = get_protocol(url);
920 if (!url_is_local_not_ssh(url)) {
921 protocol = PROTO_SSH;
927 * Don't do destructive transforms as protocol code does
928 * '[]' unwrapping in get_host_and_port()
930 end = host_end(&host, 0);
932 if (protocol == PROTO_LOCAL)
934 else if (protocol == PROTO_FILE && *host != '/' &&
935 !has_dos_drive_prefix(host) &&
936 offset_1st_component(host - 2) > 1)
937 path = host - 2; /* include the leading "//" */
938 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
939 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
941 path = strchr(end, separator);
944 die(_("no path specified; see 'git help pull' for valid url syntax"));
947 * null-terminate hostname and point path to ~ for URL's like this:
948 * ssh://host.xz/~user/repo
951 end = path; /* Need to \0 terminate host here */
952 if (separator == ':')
953 path++; /* path starts after ':' */
954 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
959 path = xstrdup(path);
962 *ret_host = xstrdup(host);
968 static const char *get_ssh_command(void)
972 if ((ssh = getenv("GIT_SSH_COMMAND")))
975 if (!git_config_get_string_const("core.sshcommand", &ssh))
987 VARIANT_TORTOISEPLINK,
990 static void override_ssh_variant(enum ssh_variant *ssh_variant)
992 const char *variant = getenv("GIT_SSH_VARIANT");
994 if (!variant && git_config_get_string_const("ssh.variant", &variant))
997 if (!strcmp(variant, "auto"))
998 *ssh_variant = VARIANT_AUTO;
999 else if (!strcmp(variant, "plink"))
1000 *ssh_variant = VARIANT_PLINK;
1001 else if (!strcmp(variant, "putty"))
1002 *ssh_variant = VARIANT_PUTTY;
1003 else if (!strcmp(variant, "tortoiseplink"))
1004 *ssh_variant = VARIANT_TORTOISEPLINK;
1005 else if (!strcmp(variant, "simple"))
1006 *ssh_variant = VARIANT_SIMPLE;
1008 *ssh_variant = VARIANT_SSH;
1011 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1014 enum ssh_variant ssh_variant = VARIANT_AUTO;
1015 const char *variant;
1018 override_ssh_variant(&ssh_variant);
1020 if (ssh_variant != VARIANT_AUTO)
1024 p = xstrdup(ssh_command);
1025 variant = basename(p);
1027 const char **ssh_argv;
1029 p = xstrdup(ssh_command);
1030 if (split_cmdline(p, &ssh_argv) > 0) {
1031 variant = basename((char *)ssh_argv[0]);
1033 * At this point, variant points into the buffer
1034 * referenced by p, hence we do not need ssh_argv
1044 if (!strcasecmp(variant, "ssh") ||
1045 !strcasecmp(variant, "ssh.exe"))
1046 ssh_variant = VARIANT_SSH;
1047 else if (!strcasecmp(variant, "plink") ||
1048 !strcasecmp(variant, "plink.exe"))
1049 ssh_variant = VARIANT_PLINK;
1050 else if (!strcasecmp(variant, "tortoiseplink") ||
1051 !strcasecmp(variant, "tortoiseplink.exe"))
1052 ssh_variant = VARIANT_TORTOISEPLINK;
1059 * Open a connection using Git's native protocol.
1061 * The caller is responsible for freeing hostandport, but this function may
1062 * modify it (for example, to truncate it to remove the port part).
1064 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1065 const char *path, const char *prog,
1066 enum protocol_version version,
1069 struct child_process *conn;
1070 struct strbuf request = STRBUF_INIT;
1072 * Set up virtual host information based on where we will
1073 * connect, unless the user has overridden us in
1076 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1078 target_host = xstrdup(target_host);
1080 target_host = xstrdup(hostandport);
1082 transport_check_allowed("git");
1085 * These underlying connection commands die() if they
1088 if (git_use_proxy(hostandport))
1089 conn = git_proxy_connect(fd, hostandport);
1091 conn = git_tcp_connect(fd, hostandport, flags);
1093 * Separate original protocol components prog and path
1094 * from extended host header with a NUL byte.
1096 * Note: Do not add any other headers here! Doing so
1097 * will cause older git-daemon servers to crash.
1099 strbuf_addf(&request,
1104 /* If using a new version put that stuff here after a second null byte */
1106 strbuf_addch(&request, '\0');
1107 strbuf_addf(&request, "version=%d%c",
1111 packet_write(fd[1], request.buf, request.len);
1114 strbuf_release(&request);
1119 * Append the appropriate environment variables to `env` and options to
1120 * `args` for running ssh in Git's SSH-tunneled transport.
1122 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1123 enum ssh_variant variant, const char *port,
1124 enum protocol_version version, int flags)
1126 if (variant == VARIANT_SSH &&
1128 argv_array_push(args, "-o");
1129 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1130 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1134 if (flags & CONNECT_IPV4) {
1137 BUG("VARIANT_AUTO passed to push_ssh_options");
1138 case VARIANT_SIMPLE:
1139 die(_("ssh variant 'simple' does not support -4"));
1143 case VARIANT_TORTOISEPLINK:
1144 argv_array_push(args, "-4");
1146 } else if (flags & CONNECT_IPV6) {
1149 BUG("VARIANT_AUTO passed to push_ssh_options");
1150 case VARIANT_SIMPLE:
1151 die(_("ssh variant 'simple' does not support -6"));
1155 case VARIANT_TORTOISEPLINK:
1156 argv_array_push(args, "-6");
1160 if (variant == VARIANT_TORTOISEPLINK)
1161 argv_array_push(args, "-batch");
1166 BUG("VARIANT_AUTO passed to push_ssh_options");
1167 case VARIANT_SIMPLE:
1168 die(_("ssh variant 'simple' does not support setting port"));
1170 argv_array_push(args, "-p");
1174 case VARIANT_TORTOISEPLINK:
1175 argv_array_push(args, "-P");
1178 argv_array_push(args, port);
1182 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1183 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1184 const char *port, enum protocol_version version,
1188 enum ssh_variant variant;
1190 if (looks_like_command_line_option(ssh_host))
1191 die(_("strange hostname '%s' blocked"), ssh_host);
1193 ssh = get_ssh_command();
1195 variant = determine_ssh_variant(ssh, 1);
1198 * GIT_SSH is the no-shell version of
1199 * GIT_SSH_COMMAND (and must remain so for
1200 * historical compatibility).
1202 conn->use_shell = 0;
1204 ssh = getenv("GIT_SSH");
1207 variant = determine_ssh_variant(ssh, 0);
1210 if (variant == VARIANT_AUTO) {
1211 struct child_process detect = CHILD_PROCESS_INIT;
1213 detect.use_shell = conn->use_shell;
1214 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1216 argv_array_push(&detect.args, ssh);
1217 argv_array_push(&detect.args, "-G");
1218 push_ssh_options(&detect.args, &detect.env_array,
1219 VARIANT_SSH, port, version, flags);
1220 argv_array_push(&detect.args, ssh_host);
1222 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1225 argv_array_push(&conn->args, ssh);
1226 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
1227 argv_array_push(&conn->args, ssh_host);
1231 * This returns the dummy child_process `no_fork` if the transport protocol
1232 * does not need fork(2), or a struct child_process object if it does. Once
1233 * done, finish the connection with finish_connect() with the value returned
1234 * from this function (it is safe to call finish_connect() with NULL to
1235 * support the former case).
1237 * If it returns, the connect is successful; it just dies on errors (this
1238 * will hopefully be changed in a libification effort, to return NULL when
1239 * the connection failed).
1241 struct child_process *git_connect(int fd[2], const char *url,
1242 const char *prog, int flags)
1244 char *hostandport, *path;
1245 struct child_process *conn;
1246 enum protocol protocol;
1247 enum protocol_version version = get_protocol_version_config();
1250 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1251 * to perform a push, then fallback to v0 since the client doesn't know
1252 * how to push yet using v2.
1254 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1255 version = protocol_v0;
1257 /* Without this we cannot rely on waitpid() to tell
1258 * what happened to our children.
1260 signal(SIGCHLD, SIG_DFL);
1262 protocol = parse_connect_url(url, &hostandport, &path);
1263 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1264 printf("Diag: url=%s\n", url ? url : "NULL");
1265 printf("Diag: protocol=%s\n", prot_name(protocol));
1266 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1267 printf("Diag: path=%s\n", path ? path : "NULL");
1269 } else if (protocol == PROTO_GIT) {
1270 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1271 conn->trace2_child_class = "transport/git";
1273 struct strbuf cmd = STRBUF_INIT;
1274 const char *const *var;
1276 conn = xmalloc(sizeof(*conn));
1277 child_process_init(conn);
1279 if (looks_like_command_line_option(path))
1280 die(_("strange pathname '%s' blocked"), path);
1282 strbuf_addstr(&cmd, prog);
1283 strbuf_addch(&cmd, ' ');
1284 sq_quote_buf(&cmd, path);
1286 /* remove repo-local variables from the environment */
1287 for (var = local_repo_env; *var; var++)
1288 argv_array_push(&conn->env_array, *var);
1290 conn->use_shell = 1;
1291 conn->in = conn->out = -1;
1292 if (protocol == PROTO_SSH) {
1293 char *ssh_host = hostandport;
1294 const char *port = NULL;
1295 transport_check_allowed("ssh");
1296 get_host_and_port(&ssh_host, &port);
1299 port = get_port(ssh_host);
1301 if (flags & CONNECT_DIAG_URL) {
1302 printf("Diag: url=%s\n", url ? url : "NULL");
1303 printf("Diag: protocol=%s\n", prot_name(protocol));
1304 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1305 printf("Diag: port=%s\n", port ? port : "NONE");
1306 printf("Diag: path=%s\n", path ? path : "NULL");
1311 strbuf_release(&cmd);
1314 conn->trace2_child_class = "transport/ssh";
1315 fill_ssh_args(conn, ssh_host, port, version, flags);
1317 transport_check_allowed("file");
1318 conn->trace2_child_class = "transport/file";
1320 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1324 argv_array_push(&conn->args, cmd.buf);
1326 if (start_command(conn))
1327 die(_("unable to fork"));
1329 fd[0] = conn->out; /* read from child's stdout */
1330 fd[1] = conn->in; /* write to child's stdin */
1331 strbuf_release(&cmd);
1338 int finish_connect(struct child_process *conn)
1341 if (!conn || git_connection_is_socket(conn))
1344 code = finish_command(conn);