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_v1;
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 int server_supports_feature(const char *c, const char *feature,
90 for (i = 0; i < server_capabilities_v2.argc; i++) {
92 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
93 (!*out || *(out++) == '=')) {
94 if (parse_feature_request(out, feature))
102 die("server doesn't support feature '%s'", feature);
107 static void process_capabilities_v2(struct packet_reader *reader)
109 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
110 argv_array_push(&server_capabilities_v2, reader->line);
112 if (reader->status != PACKET_READ_FLUSH)
113 die("expected flush after capabilities");
116 enum protocol_version discover_version(struct packet_reader *reader)
118 enum protocol_version version = protocol_unknown_version;
121 * Peek the first line of the server's response to
122 * determine the protocol version the server is speaking.
124 switch (packet_reader_peek(reader)) {
125 case PACKET_READ_EOF:
126 die_initial_contact(0);
127 case PACKET_READ_FLUSH:
128 case PACKET_READ_DELIM:
129 version = protocol_v0;
131 case PACKET_READ_NORMAL:
132 version = determine_protocol_version_client(reader->line);
138 process_capabilities_v2(reader);
141 /* Read the peeked version line */
142 packet_reader_read(reader);
146 case protocol_unknown_version:
147 BUG("unknown protocol version");
153 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
156 struct string_list_item *item;
159 return; /* just "symref" */
160 /* e.g. "symref=HEAD:refs/heads/master" */
161 sym = xmemdupz(val, len);
162 target = strchr(sym, ':');
164 /* just "symref=something" */
167 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
168 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
169 /* "symref=bogus:pair */
171 item = string_list_append_nodup(symref, sym);
179 static void annotate_refs_with_symref_info(struct ref *ref)
181 struct string_list symref = STRING_LIST_INIT_DUP;
182 const char *feature_list = server_capabilities_v1;
184 while (feature_list) {
188 val = parse_feature_value(feature_list, "symref", &len);
191 parse_one_symref_info(&symref, val, len);
192 feature_list = val + 1;
194 string_list_sort(&symref);
196 for (; ref; ref = ref->next) {
197 struct string_list_item *item;
198 item = string_list_lookup(&symref, ref->name);
201 ref->symref = xstrdup((char *)item->util);
203 string_list_clear(&symref, 0);
206 static void process_capabilities(const char *line, int *len)
208 int nul_location = strlen(line);
209 if (nul_location == *len)
211 server_capabilities_v1 = xstrdup(line + nul_location + 1);
215 static int process_dummy_ref(const char *line)
217 struct object_id oid;
220 if (parse_oid_hex(line, &oid, &name))
226 return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
229 static void check_no_capabilities(const char *line, int len)
231 if (strlen(line) != len)
232 warning("Ignoring capabilities after first line '%s'",
233 line + strlen(line));
236 static int process_ref(const char *line, int len, struct ref ***list,
237 unsigned int flags, struct oid_array *extra_have)
239 struct object_id old_oid;
242 if (parse_oid_hex(line, &old_oid, &name))
248 if (extra_have && !strcmp(name, ".have")) {
249 oid_array_append(extra_have, &old_oid);
250 } else if (!strcmp(name, "capabilities^{}")) {
251 die("protocol error: unexpected capabilities^{}");
252 } else if (check_ref(name, flags)) {
253 struct ref *ref = alloc_ref(name);
254 oidcpy(&ref->old_oid, &old_oid);
258 check_no_capabilities(line, len);
262 static int process_shallow(const char *line, int len,
263 struct oid_array *shallow_points)
266 struct object_id old_oid;
268 if (!skip_prefix(line, "shallow ", &arg))
271 if (get_oid_hex(arg, &old_oid))
272 die("protocol error: expected shallow sha-1, got '%s'", arg);
274 die("repository on the other end cannot be shallow");
275 oid_array_append(shallow_points, &old_oid);
276 check_no_capabilities(line, len);
280 enum get_remote_heads_state {
281 EXPECTING_FIRST_REF = 0,
288 * Read all the refs from the other end
290 struct ref **get_remote_heads(struct packet_reader *reader,
291 struct ref **list, unsigned int flags,
292 struct oid_array *extra_have,
293 struct oid_array *shallow_points)
295 struct ref **orig_list = list;
297 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
302 while (state != EXPECTING_DONE) {
303 switch (packet_reader_read(reader)) {
304 case PACKET_READ_EOF:
305 die_initial_contact(1);
306 case PACKET_READ_NORMAL:
307 len = reader->pktlen;
308 if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
309 die("remote error: %s", arg);
311 case PACKET_READ_FLUSH:
312 state = EXPECTING_DONE;
314 case PACKET_READ_DELIM:
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 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
410 struct ref **list, int for_push,
411 const struct argv_array *ref_prefixes)
416 if (server_supports_v2("ls-refs", 1))
417 packet_write_fmt(fd_out, "command=ls-refs\n");
419 if (server_supports_v2("agent", 0))
420 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
422 packet_delim(fd_out);
423 /* When pushing we don't want to request the peeled tags */
425 packet_write_fmt(fd_out, "peel\n");
426 packet_write_fmt(fd_out, "symrefs\n");
427 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
428 packet_write_fmt(fd_out, "ref-prefix %s\n",
429 ref_prefixes->argv[i]);
431 packet_flush(fd_out);
433 /* Process response from server */
434 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
435 if (!process_ref_v2(reader->line, &list))
436 die("invalid ls-refs response: %s", reader->line);
439 if (reader->status != PACKET_READ_FLUSH)
440 die("expected flush after ref listing");
445 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
452 len = strlen(feature);
453 while (*feature_list) {
454 const char *found = strstr(feature_list, feature);
457 if (feature_list == found || isspace(found[-1])) {
458 const char *value = found + len;
459 /* feature with no value (e.g., "thin-pack") */
460 if (!*value || isspace(*value)) {
465 /* feature with a value (e.g., "agent=git/1.2.3") */
466 else if (*value == '=') {
469 *lenp = strcspn(value, " \t\n");
473 * otherwise we matched a substring of another feature;
477 feature_list = found + 1;
482 int parse_feature_request(const char *feature_list, const char *feature)
484 return !!parse_feature_value(feature_list, feature, NULL);
487 const char *server_feature_value(const char *feature, int *len)
489 return parse_feature_value(server_capabilities_v1, feature, len);
492 int server_supports(const char *feature)
494 return !!server_feature_value(feature, NULL);
504 int url_is_local_not_ssh(const char *url)
506 const char *colon = strchr(url, ':');
507 const char *slash = strchr(url, '/');
508 return !colon || (slash && slash < colon) ||
509 has_dos_drive_prefix(url);
512 static const char *prot_name(enum protocol protocol)
523 return "unknown protocol";
527 static enum protocol get_protocol(const char *name)
529 if (!strcmp(name, "ssh"))
531 if (!strcmp(name, "git"))
533 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
535 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
537 if (!strcmp(name, "file"))
539 die("I don't handle protocol '%s'", name);
542 static char *host_end(char **hoststart, int removebrackets)
544 char *host = *hoststart;
546 char *start = strstr(host, "@[");
548 start++; /* Jump over '@' */
551 if (start[0] == '[') {
552 end = strchr(start + 1, ']');
554 if (removebrackets) {
556 memmove(start, start + 1, end - start);
567 #define STR(s) STR_(s)
569 static void get_host_and_port(char **host, const char **port)
572 end = host_end(host, 1);
573 colon = strchr(end, ':');
575 long portnr = strtol(colon + 1, &end, 10);
576 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
579 } else if (!colon[1]) {
585 static void enable_keepalive(int sockfd)
589 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
590 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
596 static const char *ai_name(const struct addrinfo *ai)
598 static char addr[NI_MAXHOST];
599 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
600 NI_NUMERICHOST) != 0)
601 xsnprintf(addr, sizeof(addr), "(unknown)");
607 * Returns a connected socket() fd, or else die()s.
609 static int git_tcp_connect_sock(char *host, int flags)
611 struct strbuf error_message = STRBUF_INIT;
613 const char *port = STR(DEFAULT_GIT_PORT);
614 struct addrinfo hints, *ai0, *ai;
618 get_host_and_port(&host, &port);
622 memset(&hints, 0, sizeof(hints));
623 if (flags & CONNECT_IPV4)
624 hints.ai_family = AF_INET;
625 else if (flags & CONNECT_IPV6)
626 hints.ai_family = AF_INET6;
627 hints.ai_socktype = SOCK_STREAM;
628 hints.ai_protocol = IPPROTO_TCP;
630 if (flags & CONNECT_VERBOSE)
631 fprintf(stderr, "Looking up %s ... ", host);
633 gai = getaddrinfo(host, port, &hints, &ai);
635 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
637 if (flags & CONNECT_VERBOSE)
638 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
640 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
641 sockfd = socket(ai->ai_family,
642 ai->ai_socktype, ai->ai_protocol);
644 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
645 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
646 host, cnt, ai_name(ai), strerror(errno));
652 if (flags & CONNECT_VERBOSE)
653 fprintf(stderr, "%s ", ai_name(ai));
660 die("unable to connect to %s:\n%s", host, error_message.buf);
662 enable_keepalive(sockfd);
664 if (flags & CONNECT_VERBOSE)
665 fprintf(stderr, "done.\n");
667 strbuf_release(&error_message);
675 * Returns a connected socket() fd, or else die()s.
677 static int git_tcp_connect_sock(char *host, int flags)
679 struct strbuf error_message = STRBUF_INIT;
681 const char *port = STR(DEFAULT_GIT_PORT);
684 struct sockaddr_in sa;
689 get_host_and_port(&host, &port);
691 if (flags & CONNECT_VERBOSE)
692 fprintf(stderr, "Looking up %s ... ", host);
694 he = gethostbyname(host);
696 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
697 nport = strtoul(port, &ep, 10);
698 if ( ep == port || *ep ) {
700 struct servent *se = getservbyname(port,"tcp");
702 die("Unknown port %s", port);
706 if (flags & CONNECT_VERBOSE)
707 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
709 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
710 memset(&sa, 0, sizeof sa);
711 sa.sin_family = he->h_addrtype;
712 sa.sin_port = htons(nport);
713 memcpy(&sa.sin_addr, *ap, he->h_length);
715 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
717 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
718 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
721 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
728 if (flags & CONNECT_VERBOSE)
729 fprintf(stderr, "%s ",
730 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
735 die("unable to connect to %s:\n%s", host, error_message.buf);
737 enable_keepalive(sockfd);
739 if (flags & CONNECT_VERBOSE)
740 fprintf(stderr, "done.\n");
749 * Dummy child_process returned by git_connect() if the transport protocol
750 * does not need fork(2).
752 static struct child_process no_fork = CHILD_PROCESS_INIT;
754 int git_connection_is_socket(struct child_process *conn)
756 return conn == &no_fork;
759 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
761 int sockfd = git_tcp_connect_sock(host, flags);
770 static char *git_proxy_command;
772 static int git_proxy_command_options(const char *var, const char *value,
775 if (!strcmp(var, "core.gitproxy")) {
779 const char *rhost_name = cb;
780 int rhost_len = strlen(rhost_name);
782 if (git_proxy_command)
785 return config_error_nonbool(var);
787 * ;# matches www.kernel.org as well
788 * gitproxy = netcatter-1 for kernel.org
789 * gitproxy = netcatter-2 for sample.xz
790 * gitproxy = netcatter-default
792 for_pos = strstr(value, " for ");
794 /* matches everybody */
795 matchlen = strlen(value);
797 hostlen = strlen(for_pos + 5);
798 if (rhost_len < hostlen)
800 else if (!strncmp(for_pos + 5,
801 rhost_name + rhost_len - hostlen,
803 ((rhost_len == hostlen) ||
804 rhost_name[rhost_len - hostlen -1] == '.'))
805 matchlen = for_pos - value;
810 /* core.gitproxy = none for kernel.org */
812 !memcmp(value, "none", 4))
814 git_proxy_command = xmemdupz(value, matchlen);
819 return git_default_config(var, value, cb);
822 static int git_use_proxy(const char *host)
824 git_proxy_command = getenv("GIT_PROXY_COMMAND");
825 git_config(git_proxy_command_options, (void*)host);
826 return (git_proxy_command && *git_proxy_command);
829 static struct child_process *git_proxy_connect(int fd[2], char *host)
831 const char *port = STR(DEFAULT_GIT_PORT);
832 struct child_process *proxy;
834 get_host_and_port(&host, &port);
836 if (looks_like_command_line_option(host))
837 die("strange hostname '%s' blocked", host);
838 if (looks_like_command_line_option(port))
839 die("strange port '%s' blocked", port);
841 proxy = xmalloc(sizeof(*proxy));
842 child_process_init(proxy);
843 argv_array_push(&proxy->args, git_proxy_command);
844 argv_array_push(&proxy->args, host);
845 argv_array_push(&proxy->args, port);
848 if (start_command(proxy))
849 die("cannot start proxy %s", git_proxy_command);
850 fd[0] = proxy->out; /* read from proxy stdout */
851 fd[1] = proxy->in; /* write to proxy stdin */
855 static char *get_port(char *host)
858 char *p = strchr(host, ':');
861 long port = strtol(p + 1, &end, 10);
862 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
872 * Extract protocol and relevant parts from the specified connection URL.
873 * The caller must free() the returned strings.
875 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
882 enum protocol protocol = PROTO_LOCAL;
884 if (is_url(url_orig))
885 url = url_decode(url_orig);
887 url = xstrdup(url_orig);
889 host = strstr(url, "://");
892 protocol = get_protocol(url);
896 if (!url_is_local_not_ssh(url)) {
897 protocol = PROTO_SSH;
903 * Don't do destructive transforms as protocol code does
904 * '[]' unwrapping in get_host_and_port()
906 end = host_end(&host, 0);
908 if (protocol == PROTO_LOCAL)
910 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
911 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
913 path = strchr(end, separator);
916 die("No path specified. See 'man git-pull' for valid url syntax");
919 * null-terminate hostname and point path to ~ for URL's like this:
920 * ssh://host.xz/~user/repo
923 end = path; /* Need to \0 terminate host here */
924 if (separator == ':')
925 path++; /* path starts after ':' */
926 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
931 path = xstrdup(path);
934 *ret_host = xstrdup(host);
940 static const char *get_ssh_command(void)
944 if ((ssh = getenv("GIT_SSH_COMMAND")))
947 if (!git_config_get_string_const("core.sshcommand", &ssh))
959 VARIANT_TORTOISEPLINK,
962 static void override_ssh_variant(enum ssh_variant *ssh_variant)
964 const char *variant = getenv("GIT_SSH_VARIANT");
966 if (!variant && git_config_get_string_const("ssh.variant", &variant))
969 if (!strcmp(variant, "auto"))
970 *ssh_variant = VARIANT_AUTO;
971 else if (!strcmp(variant, "plink"))
972 *ssh_variant = VARIANT_PLINK;
973 else if (!strcmp(variant, "putty"))
974 *ssh_variant = VARIANT_PUTTY;
975 else if (!strcmp(variant, "tortoiseplink"))
976 *ssh_variant = VARIANT_TORTOISEPLINK;
977 else if (!strcmp(variant, "simple"))
978 *ssh_variant = VARIANT_SIMPLE;
980 *ssh_variant = VARIANT_SSH;
983 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
986 enum ssh_variant ssh_variant = VARIANT_AUTO;
990 override_ssh_variant(&ssh_variant);
992 if (ssh_variant != VARIANT_AUTO)
996 p = xstrdup(ssh_command);
997 variant = basename(p);
999 const char **ssh_argv;
1001 p = xstrdup(ssh_command);
1002 if (split_cmdline(p, &ssh_argv) > 0) {
1003 variant = basename((char *)ssh_argv[0]);
1005 * At this point, variant points into the buffer
1006 * referenced by p, hence we do not need ssh_argv
1016 if (!strcasecmp(variant, "ssh") ||
1017 !strcasecmp(variant, "ssh.exe"))
1018 ssh_variant = VARIANT_SSH;
1019 else if (!strcasecmp(variant, "plink") ||
1020 !strcasecmp(variant, "plink.exe"))
1021 ssh_variant = VARIANT_PLINK;
1022 else if (!strcasecmp(variant, "tortoiseplink") ||
1023 !strcasecmp(variant, "tortoiseplink.exe"))
1024 ssh_variant = VARIANT_TORTOISEPLINK;
1031 * Open a connection using Git's native protocol.
1033 * The caller is responsible for freeing hostandport, but this function may
1034 * modify it (for example, to truncate it to remove the port part).
1036 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1037 const char *path, const char *prog,
1038 enum protocol_version version,
1041 struct child_process *conn;
1042 struct strbuf request = STRBUF_INIT;
1044 * Set up virtual host information based on where we will
1045 * connect, unless the user has overridden us in
1048 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1050 target_host = xstrdup(target_host);
1052 target_host = xstrdup(hostandport);
1054 transport_check_allowed("git");
1057 * These underlying connection commands die() if they
1060 if (git_use_proxy(hostandport))
1061 conn = git_proxy_connect(fd, hostandport);
1063 conn = git_tcp_connect(fd, hostandport, flags);
1065 * Separate original protocol components prog and path
1066 * from extended host header with a NUL byte.
1068 * Note: Do not add any other headers here! Doing so
1069 * will cause older git-daemon servers to crash.
1071 strbuf_addf(&request,
1076 /* If using a new version put that stuff here after a second null byte */
1078 strbuf_addch(&request, '\0');
1079 strbuf_addf(&request, "version=%d%c",
1083 packet_write(fd[1], request.buf, request.len);
1086 strbuf_release(&request);
1091 * Append the appropriate environment variables to `env` and options to
1092 * `args` for running ssh in Git's SSH-tunneled transport.
1094 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1095 enum ssh_variant variant, const char *port,
1096 enum protocol_version version, int flags)
1098 if (variant == VARIANT_SSH &&
1100 argv_array_push(args, "-o");
1101 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1102 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1106 if (flags & CONNECT_IPV4) {
1109 BUG("VARIANT_AUTO passed to push_ssh_options");
1110 case VARIANT_SIMPLE:
1111 die("ssh variant 'simple' does not support -4");
1115 case VARIANT_TORTOISEPLINK:
1116 argv_array_push(args, "-4");
1118 } else if (flags & CONNECT_IPV6) {
1121 BUG("VARIANT_AUTO passed to push_ssh_options");
1122 case VARIANT_SIMPLE:
1123 die("ssh variant 'simple' does not support -6");
1127 case VARIANT_TORTOISEPLINK:
1128 argv_array_push(args, "-6");
1132 if (variant == VARIANT_TORTOISEPLINK)
1133 argv_array_push(args, "-batch");
1138 BUG("VARIANT_AUTO passed to push_ssh_options");
1139 case VARIANT_SIMPLE:
1140 die("ssh variant 'simple' does not support setting port");
1142 argv_array_push(args, "-p");
1146 case VARIANT_TORTOISEPLINK:
1147 argv_array_push(args, "-P");
1150 argv_array_push(args, port);
1154 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1155 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1156 const char *port, enum protocol_version version,
1160 enum ssh_variant variant;
1162 if (looks_like_command_line_option(ssh_host))
1163 die("strange hostname '%s' blocked", ssh_host);
1165 ssh = get_ssh_command();
1167 variant = determine_ssh_variant(ssh, 1);
1170 * GIT_SSH is the no-shell version of
1171 * GIT_SSH_COMMAND (and must remain so for
1172 * historical compatibility).
1174 conn->use_shell = 0;
1176 ssh = getenv("GIT_SSH");
1179 variant = determine_ssh_variant(ssh, 0);
1182 if (variant == VARIANT_AUTO) {
1183 struct child_process detect = CHILD_PROCESS_INIT;
1185 detect.use_shell = conn->use_shell;
1186 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1188 argv_array_push(&detect.args, ssh);
1189 argv_array_push(&detect.args, "-G");
1190 push_ssh_options(&detect.args, &detect.env_array,
1191 VARIANT_SSH, port, version, flags);
1192 argv_array_push(&detect.args, ssh_host);
1194 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1197 argv_array_push(&conn->args, ssh);
1198 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
1199 argv_array_push(&conn->args, ssh_host);
1203 * This returns the dummy child_process `no_fork` if the transport protocol
1204 * does not need fork(2), or a struct child_process object if it does. Once
1205 * done, finish the connection with finish_connect() with the value returned
1206 * from this function (it is safe to call finish_connect() with NULL to
1207 * support the former case).
1209 * If it returns, the connect is successful; it just dies on errors (this
1210 * will hopefully be changed in a libification effort, to return NULL when
1211 * the connection failed).
1213 struct child_process *git_connect(int fd[2], const char *url,
1214 const char *prog, int flags)
1216 char *hostandport, *path;
1217 struct child_process *conn;
1218 enum protocol protocol;
1219 enum protocol_version version = get_protocol_version_config();
1222 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1223 * to perform a push, then fallback to v0 since the client doesn't know
1224 * how to push yet using v2.
1226 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1227 version = protocol_v0;
1229 /* Without this we cannot rely on waitpid() to tell
1230 * what happened to our children.
1232 signal(SIGCHLD, SIG_DFL);
1234 protocol = parse_connect_url(url, &hostandport, &path);
1235 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1236 printf("Diag: url=%s\n", url ? url : "NULL");
1237 printf("Diag: protocol=%s\n", prot_name(protocol));
1238 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1239 printf("Diag: path=%s\n", path ? path : "NULL");
1241 } else if (protocol == PROTO_GIT) {
1242 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1244 struct strbuf cmd = STRBUF_INIT;
1245 const char *const *var;
1247 conn = xmalloc(sizeof(*conn));
1248 child_process_init(conn);
1250 if (looks_like_command_line_option(path))
1251 die("strange pathname '%s' blocked", path);
1253 strbuf_addstr(&cmd, prog);
1254 strbuf_addch(&cmd, ' ');
1255 sq_quote_buf(&cmd, path);
1257 /* remove repo-local variables from the environment */
1258 for (var = local_repo_env; *var; var++)
1259 argv_array_push(&conn->env_array, *var);
1261 conn->use_shell = 1;
1262 conn->in = conn->out = -1;
1263 if (protocol == PROTO_SSH) {
1264 char *ssh_host = hostandport;
1265 const char *port = NULL;
1266 transport_check_allowed("ssh");
1267 get_host_and_port(&ssh_host, &port);
1270 port = get_port(ssh_host);
1272 if (flags & CONNECT_DIAG_URL) {
1273 printf("Diag: url=%s\n", url ? url : "NULL");
1274 printf("Diag: protocol=%s\n", prot_name(protocol));
1275 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1276 printf("Diag: port=%s\n", port ? port : "NONE");
1277 printf("Diag: path=%s\n", path ? path : "NULL");
1282 strbuf_release(&cmd);
1285 fill_ssh_args(conn, ssh_host, port, version, flags);
1287 transport_check_allowed("file");
1289 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1293 argv_array_push(&conn->args, cmd.buf);
1295 if (start_command(conn))
1296 die("unable to fork");
1298 fd[0] = conn->out; /* read from child's stdout */
1299 fd[1] = conn->in; /* write to child's stdin */
1300 strbuf_release(&cmd);
1307 int finish_connect(struct child_process *conn)
1310 if (!conn || git_connection_is_socket(conn))
1313 code = finish_command(conn);