1 #include "git-compat-util.h"
7 #include "run-command.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "transport.h"
18 static char *server_capabilities;
19 static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
20 static const char *parse_feature_value(const char *, const char *, int *);
22 static int check_ref(const char *name, unsigned int flags)
27 if (!skip_prefix(name, "refs/", &name))
30 /* REF_NORMAL means that we don't want the magic fake tag refs */
31 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
34 /* REF_HEADS means that we want regular branch heads */
35 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
38 /* REF_TAGS means that we want tags */
39 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
42 /* All type bits clear means that we are ok with anything */
43 return !(flags & ~REF_NORMAL);
46 int check_ref_type(const struct ref *ref, int flags)
48 return check_ref(ref->name, flags);
51 static void die_initial_contact(int unexpected)
54 * A hang-up after seeing some response from the other end
55 * means that it is unexpected, as we know the other end is
56 * willing to talk to us. A hang-up before seeing any
57 * response does not necessarily mean an ACL problem, though.
60 die(_("The remote end hung up upon initial contact"));
62 die(_("Could not read from remote repository.\n\n"
63 "Please make sure you have the correct access rights\n"
64 "and the repository exists."));
67 /* Checks if the server supports the capability 'c' */
68 int server_supports_v2(const char *c, int die_on_error)
72 for (i = 0; i < server_capabilities_v2.argc; i++) {
74 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
75 (!*out || *out == '='))
80 die("server doesn't support '%s'", c);
85 static void process_capabilities_v2(struct packet_reader *reader)
87 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
88 argv_array_push(&server_capabilities_v2, reader->line);
90 if (reader->status != PACKET_READ_FLUSH)
91 die("protocol error");
94 enum protocol_version discover_version(struct packet_reader *reader)
96 enum protocol_version version = protocol_unknown_version;
99 * Peek the first line of the server's response to
100 * determine the protocol version the server is speaking.
102 switch (packet_reader_peek(reader)) {
103 case PACKET_READ_EOF:
104 die_initial_contact(0);
105 case PACKET_READ_FLUSH:
106 case PACKET_READ_DELIM:
107 version = protocol_v0;
109 case PACKET_READ_NORMAL:
110 version = determine_protocol_version_client(reader->line);
114 /* Maybe process capabilities here, at least for v2 */
117 process_capabilities_v2(reader);
120 /* Read the peeked version line */
121 packet_reader_read(reader);
125 case protocol_unknown_version:
126 die("unknown protocol version: '%s'\n", reader->line);
132 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
135 struct string_list_item *item;
138 return; /* just "symref" */
139 /* e.g. "symref=HEAD:refs/heads/master" */
140 sym = xmemdupz(val, len);
141 target = strchr(sym, ':');
143 /* just "symref=something" */
146 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
147 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
148 /* "symref=bogus:pair */
150 item = string_list_append_nodup(symref, sym);
158 static void annotate_refs_with_symref_info(struct ref *ref)
160 struct string_list symref = STRING_LIST_INIT_DUP;
161 const char *feature_list = server_capabilities;
163 while (feature_list) {
167 val = parse_feature_value(feature_list, "symref", &len);
170 parse_one_symref_info(&symref, val, len);
171 feature_list = val + 1;
173 string_list_sort(&symref);
175 for (; ref; ref = ref->next) {
176 struct string_list_item *item;
177 item = string_list_lookup(&symref, ref->name);
180 ref->symref = xstrdup((char *)item->util);
182 string_list_clear(&symref, 0);
185 static void process_capabilities(const char *line, int *len)
187 int nul_location = strlen(line);
188 if (nul_location == *len)
190 server_capabilities = xstrdup(line + nul_location + 1);
194 static int process_dummy_ref(const char *line)
196 struct object_id oid;
199 if (parse_oid_hex(line, &oid, &name))
205 return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
208 static void check_no_capabilities(const char *line, int len)
210 if (strlen(line) != len)
211 warning("Ignoring capabilities after first line '%s'",
212 line + strlen(line));
215 static int process_ref(const char *line, int len, struct ref ***list,
216 unsigned int flags, struct oid_array *extra_have)
218 struct object_id old_oid;
221 if (parse_oid_hex(line, &old_oid, &name))
227 if (extra_have && !strcmp(name, ".have")) {
228 oid_array_append(extra_have, &old_oid);
229 } else if (!strcmp(name, "capabilities^{}")) {
230 die("protocol error: unexpected capabilities^{}");
231 } else if (check_ref(name, flags)) {
232 struct ref *ref = alloc_ref(name);
233 oidcpy(&ref->old_oid, &old_oid);
237 check_no_capabilities(line, len);
241 static int process_shallow(const char *line, int len,
242 struct oid_array *shallow_points)
245 struct object_id old_oid;
247 if (!skip_prefix(line, "shallow ", &arg))
250 if (get_oid_hex(arg, &old_oid))
251 die("protocol error: expected shallow sha-1, got '%s'", arg);
253 die("repository on the other end cannot be shallow");
254 oid_array_append(shallow_points, &old_oid);
255 check_no_capabilities(line, len);
259 enum get_remote_heads_state {
260 EXPECTING_FIRST_REF = 0,
267 * Read all the refs from the other end
269 struct ref **get_remote_heads(struct packet_reader *reader,
270 struct ref **list, unsigned int flags,
271 struct oid_array *extra_have,
272 struct oid_array *shallow_points)
274 struct ref **orig_list = list;
276 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
281 while (state != EXPECTING_DONE) {
282 switch (packet_reader_read(reader)) {
283 case PACKET_READ_EOF:
284 die_initial_contact(1);
285 case PACKET_READ_NORMAL:
286 len = reader->pktlen;
287 if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
288 die("remote error: %s", arg);
290 case PACKET_READ_FLUSH:
291 state = EXPECTING_DONE;
293 case PACKET_READ_DELIM:
294 die("invalid packet");
298 case EXPECTING_FIRST_REF:
299 process_capabilities(reader->line, &len);
300 if (process_dummy_ref(reader->line)) {
301 state = EXPECTING_SHALLOW;
304 state = EXPECTING_REF;
307 if (process_ref(reader->line, len, &list, flags, extra_have))
309 state = EXPECTING_SHALLOW;
311 case EXPECTING_SHALLOW:
312 if (process_shallow(reader->line, len, shallow_points))
314 die("protocol error: unexpected '%s'", reader->line);
320 annotate_refs_with_symref_info(*orig_list);
325 static int process_ref_v2(const char *line, struct ref ***list)
329 struct object_id old_oid;
331 struct string_list line_sections = STRING_LIST_INIT_DUP;
333 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
338 if (get_oid_hex(line_sections.items[i++].string, &old_oid)) {
343 ref = alloc_ref(line_sections.items[i++].string);
345 oidcpy(&ref->old_oid, &old_oid);
349 for (; i < line_sections.nr; i++) {
350 const char *arg = line_sections.items[i].string;
351 if (skip_prefix(arg, "symref-target:", &arg))
352 ref->symref = xstrdup(arg);
354 if (skip_prefix(arg, "peeled:", &arg)) {
355 struct object_id peeled_oid;
358 if (get_oid_hex(arg, &peeled_oid)) {
363 peeled_name = xstrfmt("%s^{}", ref->name);
364 peeled = alloc_ref(peeled_name);
366 oidcpy(&peeled->old_oid, &peeled_oid);
368 *list = &peeled->next;
375 string_list_clear(&line_sections, 0);
379 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
380 struct ref **list, int for_push,
381 const struct argv_array *ref_patterns)
386 /* Check that the server supports the ls-refs command */
387 /* Issue request for ls-refs */
388 if (server_supports_v2("ls-refs", 1))
389 packet_write_fmt(fd_out, "command=ls-refs\n");
391 if (server_supports_v2("agent", 0))
392 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
394 packet_delim(fd_out);
395 /* When pushing we don't want to request the peeled tags */
397 packet_write_fmt(fd_out, "peel\n");
398 packet_write_fmt(fd_out, "symrefs\n");
399 for (i = 0; ref_patterns && i < ref_patterns->argc; i++) {
400 packet_write_fmt(fd_out, "ref-pattern %s\n",
401 ref_patterns->argv[i]);
403 packet_flush(fd_out);
405 /* Process response from server */
406 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
407 if (!process_ref_v2(reader->line, &list))
408 die("invalid ls-refs response: %s", reader->line);
411 if (reader->status != PACKET_READ_FLUSH)
412 die("protocol error");
417 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
424 len = strlen(feature);
425 while (*feature_list) {
426 const char *found = strstr(feature_list, feature);
429 if (feature_list == found || isspace(found[-1])) {
430 const char *value = found + len;
431 /* feature with no value (e.g., "thin-pack") */
432 if (!*value || isspace(*value)) {
437 /* feature with a value (e.g., "agent=git/1.2.3") */
438 else if (*value == '=') {
441 *lenp = strcspn(value, " \t\n");
445 * otherwise we matched a substring of another feature;
449 feature_list = found + 1;
454 int parse_feature_request(const char *feature_list, const char *feature)
456 return !!parse_feature_value(feature_list, feature, NULL);
459 const char *server_feature_value(const char *feature, int *len)
461 return parse_feature_value(server_capabilities, feature, len);
464 int server_supports(const char *feature)
466 return !!server_feature_value(feature, NULL);
476 int url_is_local_not_ssh(const char *url)
478 const char *colon = strchr(url, ':');
479 const char *slash = strchr(url, '/');
480 return !colon || (slash && slash < colon) ||
481 has_dos_drive_prefix(url);
484 static const char *prot_name(enum protocol protocol)
495 return "unknown protocol";
499 static enum protocol get_protocol(const char *name)
501 if (!strcmp(name, "ssh"))
503 if (!strcmp(name, "git"))
505 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
507 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
509 if (!strcmp(name, "file"))
511 die("I don't handle protocol '%s'", name);
514 static char *host_end(char **hoststart, int removebrackets)
516 char *host = *hoststart;
518 char *start = strstr(host, "@[");
520 start++; /* Jump over '@' */
523 if (start[0] == '[') {
524 end = strchr(start + 1, ']');
526 if (removebrackets) {
528 memmove(start, start + 1, end - start);
539 #define STR(s) STR_(s)
541 static void get_host_and_port(char **host, const char **port)
544 end = host_end(host, 1);
545 colon = strchr(end, ':');
547 long portnr = strtol(colon + 1, &end, 10);
548 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
551 } else if (!colon[1]) {
557 static void enable_keepalive(int sockfd)
561 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
562 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
568 static const char *ai_name(const struct addrinfo *ai)
570 static char addr[NI_MAXHOST];
571 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
572 NI_NUMERICHOST) != 0)
573 xsnprintf(addr, sizeof(addr), "(unknown)");
579 * Returns a connected socket() fd, or else die()s.
581 static int git_tcp_connect_sock(char *host, int flags)
583 struct strbuf error_message = STRBUF_INIT;
585 const char *port = STR(DEFAULT_GIT_PORT);
586 struct addrinfo hints, *ai0, *ai;
590 get_host_and_port(&host, &port);
594 memset(&hints, 0, sizeof(hints));
595 if (flags & CONNECT_IPV4)
596 hints.ai_family = AF_INET;
597 else if (flags & CONNECT_IPV6)
598 hints.ai_family = AF_INET6;
599 hints.ai_socktype = SOCK_STREAM;
600 hints.ai_protocol = IPPROTO_TCP;
602 if (flags & CONNECT_VERBOSE)
603 fprintf(stderr, "Looking up %s ... ", host);
605 gai = getaddrinfo(host, port, &hints, &ai);
607 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
609 if (flags & CONNECT_VERBOSE)
610 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
612 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
613 sockfd = socket(ai->ai_family,
614 ai->ai_socktype, ai->ai_protocol);
616 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
617 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
618 host, cnt, ai_name(ai), strerror(errno));
624 if (flags & CONNECT_VERBOSE)
625 fprintf(stderr, "%s ", ai_name(ai));
632 die("unable to connect to %s:\n%s", host, error_message.buf);
634 enable_keepalive(sockfd);
636 if (flags & CONNECT_VERBOSE)
637 fprintf(stderr, "done.\n");
639 strbuf_release(&error_message);
647 * Returns a connected socket() fd, or else die()s.
649 static int git_tcp_connect_sock(char *host, int flags)
651 struct strbuf error_message = STRBUF_INIT;
653 const char *port = STR(DEFAULT_GIT_PORT);
656 struct sockaddr_in sa;
661 get_host_and_port(&host, &port);
663 if (flags & CONNECT_VERBOSE)
664 fprintf(stderr, "Looking up %s ... ", host);
666 he = gethostbyname(host);
668 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
669 nport = strtoul(port, &ep, 10);
670 if ( ep == port || *ep ) {
672 struct servent *se = getservbyname(port,"tcp");
674 die("Unknown port %s", port);
678 if (flags & CONNECT_VERBOSE)
679 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
681 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
682 memset(&sa, 0, sizeof sa);
683 sa.sin_family = he->h_addrtype;
684 sa.sin_port = htons(nport);
685 memcpy(&sa.sin_addr, *ap, he->h_length);
687 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
689 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
690 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
693 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
700 if (flags & CONNECT_VERBOSE)
701 fprintf(stderr, "%s ",
702 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
707 die("unable to connect to %s:\n%s", host, error_message.buf);
709 enable_keepalive(sockfd);
711 if (flags & CONNECT_VERBOSE)
712 fprintf(stderr, "done.\n");
721 * Dummy child_process returned by git_connect() if the transport protocol
722 * does not need fork(2).
724 static struct child_process no_fork = CHILD_PROCESS_INIT;
726 int git_connection_is_socket(struct child_process *conn)
728 return conn == &no_fork;
731 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
733 int sockfd = git_tcp_connect_sock(host, flags);
742 static char *git_proxy_command;
744 static int git_proxy_command_options(const char *var, const char *value,
747 if (!strcmp(var, "core.gitproxy")) {
751 const char *rhost_name = cb;
752 int rhost_len = strlen(rhost_name);
754 if (git_proxy_command)
757 return config_error_nonbool(var);
759 * ;# matches www.kernel.org as well
760 * gitproxy = netcatter-1 for kernel.org
761 * gitproxy = netcatter-2 for sample.xz
762 * gitproxy = netcatter-default
764 for_pos = strstr(value, " for ");
766 /* matches everybody */
767 matchlen = strlen(value);
769 hostlen = strlen(for_pos + 5);
770 if (rhost_len < hostlen)
772 else if (!strncmp(for_pos + 5,
773 rhost_name + rhost_len - hostlen,
775 ((rhost_len == hostlen) ||
776 rhost_name[rhost_len - hostlen -1] == '.'))
777 matchlen = for_pos - value;
782 /* core.gitproxy = none for kernel.org */
784 !memcmp(value, "none", 4))
786 git_proxy_command = xmemdupz(value, matchlen);
791 return git_default_config(var, value, cb);
794 static int git_use_proxy(const char *host)
796 git_proxy_command = getenv("GIT_PROXY_COMMAND");
797 git_config(git_proxy_command_options, (void*)host);
798 return (git_proxy_command && *git_proxy_command);
801 static struct child_process *git_proxy_connect(int fd[2], char *host)
803 const char *port = STR(DEFAULT_GIT_PORT);
804 struct child_process *proxy;
806 get_host_and_port(&host, &port);
808 if (looks_like_command_line_option(host))
809 die("strange hostname '%s' blocked", host);
810 if (looks_like_command_line_option(port))
811 die("strange port '%s' blocked", port);
813 proxy = xmalloc(sizeof(*proxy));
814 child_process_init(proxy);
815 argv_array_push(&proxy->args, git_proxy_command);
816 argv_array_push(&proxy->args, host);
817 argv_array_push(&proxy->args, port);
820 if (start_command(proxy))
821 die("cannot start proxy %s", git_proxy_command);
822 fd[0] = proxy->out; /* read from proxy stdout */
823 fd[1] = proxy->in; /* write to proxy stdin */
827 static char *get_port(char *host)
830 char *p = strchr(host, ':');
833 long port = strtol(p + 1, &end, 10);
834 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
844 * Extract protocol and relevant parts from the specified connection URL.
845 * The caller must free() the returned strings.
847 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
854 enum protocol protocol = PROTO_LOCAL;
856 if (is_url(url_orig))
857 url = url_decode(url_orig);
859 url = xstrdup(url_orig);
861 host = strstr(url, "://");
864 protocol = get_protocol(url);
868 if (!url_is_local_not_ssh(url)) {
869 protocol = PROTO_SSH;
875 * Don't do destructive transforms as protocol code does
876 * '[]' unwrapping in get_host_and_port()
878 end = host_end(&host, 0);
880 if (protocol == PROTO_LOCAL)
882 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
883 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
885 path = strchr(end, separator);
888 die("No path specified. See 'man git-pull' for valid url syntax");
891 * null-terminate hostname and point path to ~ for URL's like this:
892 * ssh://host.xz/~user/repo
895 end = path; /* Need to \0 terminate host here */
896 if (separator == ':')
897 path++; /* path starts after ':' */
898 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
903 path = xstrdup(path);
906 *ret_host = xstrdup(host);
912 static const char *get_ssh_command(void)
916 if ((ssh = getenv("GIT_SSH_COMMAND")))
919 if (!git_config_get_string_const("core.sshcommand", &ssh))
931 VARIANT_TORTOISEPLINK,
934 static void override_ssh_variant(enum ssh_variant *ssh_variant)
936 const char *variant = getenv("GIT_SSH_VARIANT");
938 if (!variant && git_config_get_string_const("ssh.variant", &variant))
941 if (!strcmp(variant, "auto"))
942 *ssh_variant = VARIANT_AUTO;
943 else if (!strcmp(variant, "plink"))
944 *ssh_variant = VARIANT_PLINK;
945 else if (!strcmp(variant, "putty"))
946 *ssh_variant = VARIANT_PUTTY;
947 else if (!strcmp(variant, "tortoiseplink"))
948 *ssh_variant = VARIANT_TORTOISEPLINK;
949 else if (!strcmp(variant, "simple"))
950 *ssh_variant = VARIANT_SIMPLE;
952 *ssh_variant = VARIANT_SSH;
955 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
958 enum ssh_variant ssh_variant = VARIANT_AUTO;
962 override_ssh_variant(&ssh_variant);
964 if (ssh_variant != VARIANT_AUTO)
968 p = xstrdup(ssh_command);
969 variant = basename(p);
971 const char **ssh_argv;
973 p = xstrdup(ssh_command);
974 if (split_cmdline(p, &ssh_argv) > 0) {
975 variant = basename((char *)ssh_argv[0]);
977 * At this point, variant points into the buffer
978 * referenced by p, hence we do not need ssh_argv
988 if (!strcasecmp(variant, "ssh") ||
989 !strcasecmp(variant, "ssh.exe"))
990 ssh_variant = VARIANT_SSH;
991 else if (!strcasecmp(variant, "plink") ||
992 !strcasecmp(variant, "plink.exe"))
993 ssh_variant = VARIANT_PLINK;
994 else if (!strcasecmp(variant, "tortoiseplink") ||
995 !strcasecmp(variant, "tortoiseplink.exe"))
996 ssh_variant = VARIANT_TORTOISEPLINK;
1003 * Open a connection using Git's native protocol.
1005 * The caller is responsible for freeing hostandport, but this function may
1006 * modify it (for example, to truncate it to remove the port part).
1008 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1009 const char *path, const char *prog,
1012 struct child_process *conn;
1013 struct strbuf request = STRBUF_INIT;
1015 * Set up virtual host information based on where we will
1016 * connect, unless the user has overridden us in
1019 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1021 target_host = xstrdup(target_host);
1023 target_host = xstrdup(hostandport);
1025 transport_check_allowed("git");
1028 * These underlying connection commands die() if they
1031 if (git_use_proxy(hostandport))
1032 conn = git_proxy_connect(fd, hostandport);
1034 conn = git_tcp_connect(fd, hostandport, flags);
1036 * Separate original protocol components prog and path
1037 * from extended host header with a NUL byte.
1039 * Note: Do not add any other headers here! Doing so
1040 * will cause older git-daemon servers to crash.
1042 strbuf_addf(&request,
1047 /* If using a new version put that stuff here after a second null byte */
1048 if (get_protocol_version_config() > 0) {
1049 strbuf_addch(&request, '\0');
1050 strbuf_addf(&request, "version=%d%c",
1051 get_protocol_version_config(), '\0');
1054 packet_write(fd[1], request.buf, request.len);
1057 strbuf_release(&request);
1062 * Append the appropriate environment variables to `env` and options to
1063 * `args` for running ssh in Git's SSH-tunneled transport.
1065 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1066 enum ssh_variant variant, const char *port,
1069 if (variant == VARIANT_SSH &&
1070 get_protocol_version_config() > 0) {
1071 argv_array_push(args, "-o");
1072 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1073 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1074 get_protocol_version_config());
1077 if (flags & CONNECT_IPV4) {
1080 BUG("VARIANT_AUTO passed to push_ssh_options");
1081 case VARIANT_SIMPLE:
1082 die("ssh variant 'simple' does not support -4");
1086 case VARIANT_TORTOISEPLINK:
1087 argv_array_push(args, "-4");
1089 } else if (flags & CONNECT_IPV6) {
1092 BUG("VARIANT_AUTO passed to push_ssh_options");
1093 case VARIANT_SIMPLE:
1094 die("ssh variant 'simple' does not support -6");
1098 case VARIANT_TORTOISEPLINK:
1099 argv_array_push(args, "-6");
1103 if (variant == VARIANT_TORTOISEPLINK)
1104 argv_array_push(args, "-batch");
1109 BUG("VARIANT_AUTO passed to push_ssh_options");
1110 case VARIANT_SIMPLE:
1111 die("ssh variant 'simple' does not support setting port");
1113 argv_array_push(args, "-p");
1117 case VARIANT_TORTOISEPLINK:
1118 argv_array_push(args, "-P");
1121 argv_array_push(args, port);
1125 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1126 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1127 const char *port, int flags)
1130 enum ssh_variant variant;
1132 if (looks_like_command_line_option(ssh_host))
1133 die("strange hostname '%s' blocked", ssh_host);
1135 ssh = get_ssh_command();
1137 variant = determine_ssh_variant(ssh, 1);
1140 * GIT_SSH is the no-shell version of
1141 * GIT_SSH_COMMAND (and must remain so for
1142 * historical compatibility).
1144 conn->use_shell = 0;
1146 ssh = getenv("GIT_SSH");
1149 variant = determine_ssh_variant(ssh, 0);
1152 if (variant == VARIANT_AUTO) {
1153 struct child_process detect = CHILD_PROCESS_INIT;
1155 detect.use_shell = conn->use_shell;
1156 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1158 argv_array_push(&detect.args, ssh);
1159 argv_array_push(&detect.args, "-G");
1160 push_ssh_options(&detect.args, &detect.env_array,
1161 VARIANT_SSH, port, flags);
1162 argv_array_push(&detect.args, ssh_host);
1164 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1167 argv_array_push(&conn->args, ssh);
1168 push_ssh_options(&conn->args, &conn->env_array, variant, port, flags);
1169 argv_array_push(&conn->args, ssh_host);
1173 * This returns the dummy child_process `no_fork` if the transport protocol
1174 * does not need fork(2), or a struct child_process object if it does. Once
1175 * done, finish the connection with finish_connect() with the value returned
1176 * from this function (it is safe to call finish_connect() with NULL to
1177 * support the former case).
1179 * If it returns, the connect is successful; it just dies on errors (this
1180 * will hopefully be changed in a libification effort, to return NULL when
1181 * the connection failed).
1183 struct child_process *git_connect(int fd[2], const char *url,
1184 const char *prog, int flags)
1186 char *hostandport, *path;
1187 struct child_process *conn;
1188 enum protocol protocol;
1190 /* Without this we cannot rely on waitpid() to tell
1191 * what happened to our children.
1193 signal(SIGCHLD, SIG_DFL);
1195 protocol = parse_connect_url(url, &hostandport, &path);
1196 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1197 printf("Diag: url=%s\n", url ? url : "NULL");
1198 printf("Diag: protocol=%s\n", prot_name(protocol));
1199 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1200 printf("Diag: path=%s\n", path ? path : "NULL");
1202 } else if (protocol == PROTO_GIT) {
1203 conn = git_connect_git(fd, hostandport, path, prog, flags);
1205 struct strbuf cmd = STRBUF_INIT;
1206 const char *const *var;
1208 conn = xmalloc(sizeof(*conn));
1209 child_process_init(conn);
1211 if (looks_like_command_line_option(path))
1212 die("strange pathname '%s' blocked", path);
1214 strbuf_addstr(&cmd, prog);
1215 strbuf_addch(&cmd, ' ');
1216 sq_quote_buf(&cmd, path);
1218 /* remove repo-local variables from the environment */
1219 for (var = local_repo_env; *var; var++)
1220 argv_array_push(&conn->env_array, *var);
1222 conn->use_shell = 1;
1223 conn->in = conn->out = -1;
1224 if (protocol == PROTO_SSH) {
1225 char *ssh_host = hostandport;
1226 const char *port = NULL;
1227 transport_check_allowed("ssh");
1228 get_host_and_port(&ssh_host, &port);
1231 port = get_port(ssh_host);
1233 if (flags & CONNECT_DIAG_URL) {
1234 printf("Diag: url=%s\n", url ? url : "NULL");
1235 printf("Diag: protocol=%s\n", prot_name(protocol));
1236 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1237 printf("Diag: port=%s\n", port ? port : "NONE");
1238 printf("Diag: path=%s\n", path ? path : "NULL");
1243 strbuf_release(&cmd);
1246 fill_ssh_args(conn, ssh_host, port, flags);
1248 transport_check_allowed("file");
1249 if (get_protocol_version_config() > 0) {
1250 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1251 get_protocol_version_config());
1254 argv_array_push(&conn->args, cmd.buf);
1256 if (start_command(conn))
1257 die("unable to fork");
1259 fd[0] = conn->out; /* read from child's stdout */
1260 fd[1] = conn->in; /* write to child's stdin */
1261 strbuf_release(&cmd);
1268 int finish_connect(struct child_process *conn)
1271 if (!conn || git_connection_is_socket(conn))
1274 code = finish_command(conn);