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 NORETURN 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,
412 const struct string_list *server_options)
417 if (server_supports_v2("ls-refs", 1))
418 packet_write_fmt(fd_out, "command=ls-refs\n");
420 if (server_supports_v2("agent", 0))
421 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
423 if (server_options && server_options->nr &&
424 server_supports_v2("server-option", 1))
425 for (i = 0; i < server_options->nr; i++)
426 packet_write_fmt(fd_out, "server-option=%s",
427 server_options->items[i].string);
429 packet_delim(fd_out);
430 /* When pushing we don't want to request the peeled tags */
432 packet_write_fmt(fd_out, "peel\n");
433 packet_write_fmt(fd_out, "symrefs\n");
434 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
435 packet_write_fmt(fd_out, "ref-prefix %s\n",
436 ref_prefixes->argv[i]);
438 packet_flush(fd_out);
440 /* Process response from server */
441 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
442 if (!process_ref_v2(reader->line, &list))
443 die("invalid ls-refs response: %s", reader->line);
446 if (reader->status != PACKET_READ_FLUSH)
447 die("expected flush after ref listing");
452 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
459 len = strlen(feature);
460 while (*feature_list) {
461 const char *found = strstr(feature_list, feature);
464 if (feature_list == found || isspace(found[-1])) {
465 const char *value = found + len;
466 /* feature with no value (e.g., "thin-pack") */
467 if (!*value || isspace(*value)) {
472 /* feature with a value (e.g., "agent=git/1.2.3") */
473 else if (*value == '=') {
476 *lenp = strcspn(value, " \t\n");
480 * otherwise we matched a substring of another feature;
484 feature_list = found + 1;
489 int parse_feature_request(const char *feature_list, const char *feature)
491 return !!parse_feature_value(feature_list, feature, NULL);
494 const char *server_feature_value(const char *feature, int *len)
496 return parse_feature_value(server_capabilities_v1, feature, len);
499 int server_supports(const char *feature)
501 return !!server_feature_value(feature, NULL);
511 int url_is_local_not_ssh(const char *url)
513 const char *colon = strchr(url, ':');
514 const char *slash = strchr(url, '/');
515 return !colon || (slash && slash < colon) ||
516 has_dos_drive_prefix(url);
519 static const char *prot_name(enum protocol protocol)
530 return "unknown protocol";
534 static enum protocol get_protocol(const char *name)
536 if (!strcmp(name, "ssh"))
538 if (!strcmp(name, "git"))
540 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
542 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
544 if (!strcmp(name, "file"))
546 die("I don't handle protocol '%s'", name);
549 static char *host_end(char **hoststart, int removebrackets)
551 char *host = *hoststart;
553 char *start = strstr(host, "@[");
555 start++; /* Jump over '@' */
558 if (start[0] == '[') {
559 end = strchr(start + 1, ']');
561 if (removebrackets) {
563 memmove(start, start + 1, end - start);
574 #define STR(s) STR_(s)
576 static void get_host_and_port(char **host, const char **port)
579 end = host_end(host, 1);
580 colon = strchr(end, ':');
582 long portnr = strtol(colon + 1, &end, 10);
583 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
586 } else if (!colon[1]) {
592 static void enable_keepalive(int sockfd)
596 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
597 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
603 static const char *ai_name(const struct addrinfo *ai)
605 static char addr[NI_MAXHOST];
606 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
607 NI_NUMERICHOST) != 0)
608 xsnprintf(addr, sizeof(addr), "(unknown)");
614 * Returns a connected socket() fd, or else die()s.
616 static int git_tcp_connect_sock(char *host, int flags)
618 struct strbuf error_message = STRBUF_INIT;
620 const char *port = STR(DEFAULT_GIT_PORT);
621 struct addrinfo hints, *ai0, *ai;
625 get_host_and_port(&host, &port);
629 memset(&hints, 0, sizeof(hints));
630 if (flags & CONNECT_IPV4)
631 hints.ai_family = AF_INET;
632 else if (flags & CONNECT_IPV6)
633 hints.ai_family = AF_INET6;
634 hints.ai_socktype = SOCK_STREAM;
635 hints.ai_protocol = IPPROTO_TCP;
637 if (flags & CONNECT_VERBOSE)
638 fprintf(stderr, "Looking up %s ... ", host);
640 gai = getaddrinfo(host, port, &hints, &ai);
642 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
644 if (flags & CONNECT_VERBOSE)
645 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
647 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
648 sockfd = socket(ai->ai_family,
649 ai->ai_socktype, ai->ai_protocol);
651 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
652 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
653 host, cnt, ai_name(ai), strerror(errno));
659 if (flags & CONNECT_VERBOSE)
660 fprintf(stderr, "%s ", ai_name(ai));
667 die("unable to connect to %s:\n%s", host, error_message.buf);
669 enable_keepalive(sockfd);
671 if (flags & CONNECT_VERBOSE)
672 fprintf(stderr, "done.\n");
674 strbuf_release(&error_message);
682 * Returns a connected socket() fd, or else die()s.
684 static int git_tcp_connect_sock(char *host, int flags)
686 struct strbuf error_message = STRBUF_INIT;
688 const char *port = STR(DEFAULT_GIT_PORT);
691 struct sockaddr_in sa;
696 get_host_and_port(&host, &port);
698 if (flags & CONNECT_VERBOSE)
699 fprintf(stderr, "Looking up %s ... ", host);
701 he = gethostbyname(host);
703 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
704 nport = strtoul(port, &ep, 10);
705 if ( ep == port || *ep ) {
707 struct servent *se = getservbyname(port,"tcp");
709 die("Unknown port %s", port);
713 if (flags & CONNECT_VERBOSE)
714 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
716 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
717 memset(&sa, 0, sizeof sa);
718 sa.sin_family = he->h_addrtype;
719 sa.sin_port = htons(nport);
720 memcpy(&sa.sin_addr, *ap, he->h_length);
722 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
724 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
725 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
728 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
735 if (flags & CONNECT_VERBOSE)
736 fprintf(stderr, "%s ",
737 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
742 die("unable to connect to %s:\n%s", host, error_message.buf);
744 enable_keepalive(sockfd);
746 if (flags & CONNECT_VERBOSE)
747 fprintf(stderr, "done.\n");
756 * Dummy child_process returned by git_connect() if the transport protocol
757 * does not need fork(2).
759 static struct child_process no_fork = CHILD_PROCESS_INIT;
761 int git_connection_is_socket(struct child_process *conn)
763 return conn == &no_fork;
766 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
768 int sockfd = git_tcp_connect_sock(host, flags);
777 static char *git_proxy_command;
779 static int git_proxy_command_options(const char *var, const char *value,
782 if (!strcmp(var, "core.gitproxy")) {
786 const char *rhost_name = cb;
787 int rhost_len = strlen(rhost_name);
789 if (git_proxy_command)
792 return config_error_nonbool(var);
794 * ;# matches www.kernel.org as well
795 * gitproxy = netcatter-1 for kernel.org
796 * gitproxy = netcatter-2 for sample.xz
797 * gitproxy = netcatter-default
799 for_pos = strstr(value, " for ");
801 /* matches everybody */
802 matchlen = strlen(value);
804 hostlen = strlen(for_pos + 5);
805 if (rhost_len < hostlen)
807 else if (!strncmp(for_pos + 5,
808 rhost_name + rhost_len - hostlen,
810 ((rhost_len == hostlen) ||
811 rhost_name[rhost_len - hostlen -1] == '.'))
812 matchlen = for_pos - value;
817 /* core.gitproxy = none for kernel.org */
819 !memcmp(value, "none", 4))
821 git_proxy_command = xmemdupz(value, matchlen);
826 return git_default_config(var, value, cb);
829 static int git_use_proxy(const char *host)
831 git_proxy_command = getenv("GIT_PROXY_COMMAND");
832 git_config(git_proxy_command_options, (void*)host);
833 return (git_proxy_command && *git_proxy_command);
836 static struct child_process *git_proxy_connect(int fd[2], char *host)
838 const char *port = STR(DEFAULT_GIT_PORT);
839 struct child_process *proxy;
841 get_host_and_port(&host, &port);
843 if (looks_like_command_line_option(host))
844 die("strange hostname '%s' blocked", host);
845 if (looks_like_command_line_option(port))
846 die("strange port '%s' blocked", port);
848 proxy = xmalloc(sizeof(*proxy));
849 child_process_init(proxy);
850 argv_array_push(&proxy->args, git_proxy_command);
851 argv_array_push(&proxy->args, host);
852 argv_array_push(&proxy->args, port);
855 if (start_command(proxy))
856 die("cannot start proxy %s", git_proxy_command);
857 fd[0] = proxy->out; /* read from proxy stdout */
858 fd[1] = proxy->in; /* write to proxy stdin */
862 static char *get_port(char *host)
865 char *p = strchr(host, ':');
868 long port = strtol(p + 1, &end, 10);
869 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
879 * Extract protocol and relevant parts from the specified connection URL.
880 * The caller must free() the returned strings.
882 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
889 enum protocol protocol = PROTO_LOCAL;
891 if (is_url(url_orig))
892 url = url_decode(url_orig);
894 url = xstrdup(url_orig);
896 host = strstr(url, "://");
899 protocol = get_protocol(url);
903 if (!url_is_local_not_ssh(url)) {
904 protocol = PROTO_SSH;
910 * Don't do destructive transforms as protocol code does
911 * '[]' unwrapping in get_host_and_port()
913 end = host_end(&host, 0);
915 if (protocol == PROTO_LOCAL)
917 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
918 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
920 path = strchr(end, separator);
923 die("No path specified. See 'man git-pull' for valid url syntax");
926 * null-terminate hostname and point path to ~ for URL's like this:
927 * ssh://host.xz/~user/repo
930 end = path; /* Need to \0 terminate host here */
931 if (separator == ':')
932 path++; /* path starts after ':' */
933 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
938 path = xstrdup(path);
941 *ret_host = xstrdup(host);
947 static const char *get_ssh_command(void)
951 if ((ssh = getenv("GIT_SSH_COMMAND")))
954 if (!git_config_get_string_const("core.sshcommand", &ssh))
966 VARIANT_TORTOISEPLINK,
969 static void override_ssh_variant(enum ssh_variant *ssh_variant)
971 const char *variant = getenv("GIT_SSH_VARIANT");
973 if (!variant && git_config_get_string_const("ssh.variant", &variant))
976 if (!strcmp(variant, "auto"))
977 *ssh_variant = VARIANT_AUTO;
978 else if (!strcmp(variant, "plink"))
979 *ssh_variant = VARIANT_PLINK;
980 else if (!strcmp(variant, "putty"))
981 *ssh_variant = VARIANT_PUTTY;
982 else if (!strcmp(variant, "tortoiseplink"))
983 *ssh_variant = VARIANT_TORTOISEPLINK;
984 else if (!strcmp(variant, "simple"))
985 *ssh_variant = VARIANT_SIMPLE;
987 *ssh_variant = VARIANT_SSH;
990 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
993 enum ssh_variant ssh_variant = VARIANT_AUTO;
997 override_ssh_variant(&ssh_variant);
999 if (ssh_variant != VARIANT_AUTO)
1003 p = xstrdup(ssh_command);
1004 variant = basename(p);
1006 const char **ssh_argv;
1008 p = xstrdup(ssh_command);
1009 if (split_cmdline(p, &ssh_argv) > 0) {
1010 variant = basename((char *)ssh_argv[0]);
1012 * At this point, variant points into the buffer
1013 * referenced by p, hence we do not need ssh_argv
1023 if (!strcasecmp(variant, "ssh") ||
1024 !strcasecmp(variant, "ssh.exe"))
1025 ssh_variant = VARIANT_SSH;
1026 else if (!strcasecmp(variant, "plink") ||
1027 !strcasecmp(variant, "plink.exe"))
1028 ssh_variant = VARIANT_PLINK;
1029 else if (!strcasecmp(variant, "tortoiseplink") ||
1030 !strcasecmp(variant, "tortoiseplink.exe"))
1031 ssh_variant = VARIANT_TORTOISEPLINK;
1038 * Open a connection using Git's native protocol.
1040 * The caller is responsible for freeing hostandport, but this function may
1041 * modify it (for example, to truncate it to remove the port part).
1043 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1044 const char *path, const char *prog,
1045 enum protocol_version version,
1048 struct child_process *conn;
1049 struct strbuf request = STRBUF_INIT;
1051 * Set up virtual host information based on where we will
1052 * connect, unless the user has overridden us in
1055 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1057 target_host = xstrdup(target_host);
1059 target_host = xstrdup(hostandport);
1061 transport_check_allowed("git");
1064 * These underlying connection commands die() if they
1067 if (git_use_proxy(hostandport))
1068 conn = git_proxy_connect(fd, hostandport);
1070 conn = git_tcp_connect(fd, hostandport, flags);
1072 * Separate original protocol components prog and path
1073 * from extended host header with a NUL byte.
1075 * Note: Do not add any other headers here! Doing so
1076 * will cause older git-daemon servers to crash.
1078 strbuf_addf(&request,
1083 /* If using a new version put that stuff here after a second null byte */
1085 strbuf_addch(&request, '\0');
1086 strbuf_addf(&request, "version=%d%c",
1090 packet_write(fd[1], request.buf, request.len);
1093 strbuf_release(&request);
1098 * Append the appropriate environment variables to `env` and options to
1099 * `args` for running ssh in Git's SSH-tunneled transport.
1101 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1102 enum ssh_variant variant, const char *port,
1103 enum protocol_version version, int flags)
1105 if (variant == VARIANT_SSH &&
1107 argv_array_push(args, "-o");
1108 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1109 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1113 if (flags & CONNECT_IPV4) {
1116 BUG("VARIANT_AUTO passed to push_ssh_options");
1117 case VARIANT_SIMPLE:
1118 die("ssh variant 'simple' does not support -4");
1122 case VARIANT_TORTOISEPLINK:
1123 argv_array_push(args, "-4");
1125 } else if (flags & CONNECT_IPV6) {
1128 BUG("VARIANT_AUTO passed to push_ssh_options");
1129 case VARIANT_SIMPLE:
1130 die("ssh variant 'simple' does not support -6");
1134 case VARIANT_TORTOISEPLINK:
1135 argv_array_push(args, "-6");
1139 if (variant == VARIANT_TORTOISEPLINK)
1140 argv_array_push(args, "-batch");
1145 BUG("VARIANT_AUTO passed to push_ssh_options");
1146 case VARIANT_SIMPLE:
1147 die("ssh variant 'simple' does not support setting port");
1149 argv_array_push(args, "-p");
1153 case VARIANT_TORTOISEPLINK:
1154 argv_array_push(args, "-P");
1157 argv_array_push(args, port);
1161 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1162 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1163 const char *port, enum protocol_version version,
1167 enum ssh_variant variant;
1169 if (looks_like_command_line_option(ssh_host))
1170 die("strange hostname '%s' blocked", ssh_host);
1172 ssh = get_ssh_command();
1174 variant = determine_ssh_variant(ssh, 1);
1177 * GIT_SSH is the no-shell version of
1178 * GIT_SSH_COMMAND (and must remain so for
1179 * historical compatibility).
1181 conn->use_shell = 0;
1183 ssh = getenv("GIT_SSH");
1186 variant = determine_ssh_variant(ssh, 0);
1189 if (variant == VARIANT_AUTO) {
1190 struct child_process detect = CHILD_PROCESS_INIT;
1192 detect.use_shell = conn->use_shell;
1193 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1195 argv_array_push(&detect.args, ssh);
1196 argv_array_push(&detect.args, "-G");
1197 push_ssh_options(&detect.args, &detect.env_array,
1198 VARIANT_SSH, port, version, flags);
1199 argv_array_push(&detect.args, ssh_host);
1201 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1204 argv_array_push(&conn->args, ssh);
1205 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
1206 argv_array_push(&conn->args, ssh_host);
1210 * This returns the dummy child_process `no_fork` if the transport protocol
1211 * does not need fork(2), or a struct child_process object if it does. Once
1212 * done, finish the connection with finish_connect() with the value returned
1213 * from this function (it is safe to call finish_connect() with NULL to
1214 * support the former case).
1216 * If it returns, the connect is successful; it just dies on errors (this
1217 * will hopefully be changed in a libification effort, to return NULL when
1218 * the connection failed).
1220 struct child_process *git_connect(int fd[2], const char *url,
1221 const char *prog, int flags)
1223 char *hostandport, *path;
1224 struct child_process *conn;
1225 enum protocol protocol;
1226 enum protocol_version version = get_protocol_version_config();
1229 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1230 * to perform a push, then fallback to v0 since the client doesn't know
1231 * how to push yet using v2.
1233 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1234 version = protocol_v0;
1236 /* Without this we cannot rely on waitpid() to tell
1237 * what happened to our children.
1239 signal(SIGCHLD, SIG_DFL);
1241 protocol = parse_connect_url(url, &hostandport, &path);
1242 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1243 printf("Diag: url=%s\n", url ? url : "NULL");
1244 printf("Diag: protocol=%s\n", prot_name(protocol));
1245 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1246 printf("Diag: path=%s\n", path ? path : "NULL");
1248 } else if (protocol == PROTO_GIT) {
1249 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1251 struct strbuf cmd = STRBUF_INIT;
1252 const char *const *var;
1254 conn = xmalloc(sizeof(*conn));
1255 child_process_init(conn);
1257 if (looks_like_command_line_option(path))
1258 die("strange pathname '%s' blocked", path);
1260 strbuf_addstr(&cmd, prog);
1261 strbuf_addch(&cmd, ' ');
1262 sq_quote_buf(&cmd, path);
1264 /* remove repo-local variables from the environment */
1265 for (var = local_repo_env; *var; var++)
1266 argv_array_push(&conn->env_array, *var);
1268 conn->use_shell = 1;
1269 conn->in = conn->out = -1;
1270 if (protocol == PROTO_SSH) {
1271 char *ssh_host = hostandport;
1272 const char *port = NULL;
1273 transport_check_allowed("ssh");
1274 get_host_and_port(&ssh_host, &port);
1277 port = get_port(ssh_host);
1279 if (flags & CONNECT_DIAG_URL) {
1280 printf("Diag: url=%s\n", url ? url : "NULL");
1281 printf("Diag: protocol=%s\n", prot_name(protocol));
1282 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1283 printf("Diag: port=%s\n", port ? port : "NONE");
1284 printf("Diag: path=%s\n", path ? path : "NULL");
1289 strbuf_release(&cmd);
1292 fill_ssh_args(conn, ssh_host, port, version, flags);
1294 transport_check_allowed("file");
1296 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1300 argv_array_push(&conn->args, cmd.buf);
1302 if (start_command(conn))
1303 die("unable to fork");
1305 fd[0] = conn->out; /* read from child's stdout */
1306 fd[1] = conn->in; /* write to child's stdin */
1307 strbuf_release(&cmd);
1314 int finish_connect(struct child_process *conn)
1317 if (!conn || git_connection_is_socket(conn))
1320 code = finish_command(conn);