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"
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 version
= protocol_v0
;
132 case PACKET_READ_NORMAL
:
133 version
= determine_protocol_version_client(reader
->line
);
139 process_capabilities_v2(reader
);
142 /* Read the peeked version line */
143 packet_reader_read(reader
);
147 case protocol_unknown_version
:
148 BUG("unknown protocol version");
154 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
157 struct string_list_item
*item
;
160 return; /* just "symref" */
161 /* e.g. "symref=HEAD:refs/heads/master" */
162 sym
= xmemdupz(val
, len
);
163 target
= strchr(sym
, ':');
165 /* just "symref=something" */
168 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
169 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
170 /* "symref=bogus:pair */
172 item
= string_list_append_nodup(symref
, sym
);
180 static void annotate_refs_with_symref_info(struct ref
*ref
)
182 struct string_list symref
= STRING_LIST_INIT_DUP
;
183 const char *feature_list
= server_capabilities_v1
;
185 while (feature_list
) {
189 val
= parse_feature_value(feature_list
, "symref", &len
);
192 parse_one_symref_info(&symref
, val
, len
);
193 feature_list
= val
+ 1;
195 string_list_sort(&symref
);
197 for (; ref
; ref
= ref
->next
) {
198 struct string_list_item
*item
;
199 item
= string_list_lookup(&symref
, ref
->name
);
202 ref
->symref
= xstrdup((char *)item
->util
);
204 string_list_clear(&symref
, 0);
207 static void process_capabilities(const char *line
, int *len
)
209 int nul_location
= strlen(line
);
210 if (nul_location
== *len
)
212 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
216 static int process_dummy_ref(const char *line
)
218 struct object_id oid
;
221 if (parse_oid_hex(line
, &oid
, &name
))
227 return oideq(&null_oid
, &oid
) && !strcmp(name
, "capabilities^{}");
230 static void check_no_capabilities(const char *line
, int len
)
232 if (strlen(line
) != len
)
233 warning(_("ignoring capabilities after first line '%s'"),
234 line
+ strlen(line
));
237 static int process_ref(const char *line
, int len
, struct ref
***list
,
238 unsigned int flags
, struct oid_array
*extra_have
)
240 struct object_id old_oid
;
243 if (parse_oid_hex(line
, &old_oid
, &name
))
249 if (extra_have
&& !strcmp(name
, ".have")) {
250 oid_array_append(extra_have
, &old_oid
);
251 } else if (!strcmp(name
, "capabilities^{}")) {
252 die(_("protocol error: unexpected capabilities^{}"));
253 } else if (check_ref(name
, flags
)) {
254 struct ref
*ref
= alloc_ref(name
);
255 oidcpy(&ref
->old_oid
, &old_oid
);
259 check_no_capabilities(line
, len
);
263 static int process_shallow(const char *line
, int len
,
264 struct oid_array
*shallow_points
)
267 struct object_id old_oid
;
269 if (!skip_prefix(line
, "shallow ", &arg
))
272 if (get_oid_hex(arg
, &old_oid
))
273 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
275 die(_("repository on the other end cannot be shallow"));
276 oid_array_append(shallow_points
, &old_oid
);
277 check_no_capabilities(line
, len
);
281 enum get_remote_heads_state
{
282 EXPECTING_FIRST_REF
= 0,
289 * Read all the refs from the other end
291 struct ref
**get_remote_heads(struct packet_reader
*reader
,
292 struct ref
**list
, unsigned int flags
,
293 struct oid_array
*extra_have
,
294 struct oid_array
*shallow_points
)
296 struct ref
**orig_list
= list
;
298 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
;
309 case PACKET_READ_FLUSH
:
310 state
= EXPECTING_DONE
;
312 case PACKET_READ_DELIM
:
313 die(_("invalid packet"));
317 case EXPECTING_FIRST_REF
:
318 process_capabilities(reader
->line
, &len
);
319 if (process_dummy_ref(reader
->line
)) {
320 state
= EXPECTING_SHALLOW
;
323 state
= EXPECTING_REF
;
326 if (process_ref(reader
->line
, len
, &list
, flags
, extra_have
))
328 state
= EXPECTING_SHALLOW
;
330 case EXPECTING_SHALLOW
:
331 if (process_shallow(reader
->line
, len
, shallow_points
))
333 die(_("protocol error: unexpected '%s'"), reader
->line
);
339 annotate_refs_with_symref_info(*orig_list
);
344 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
345 static int process_ref_v2(const char *line
, struct ref
***list
)
349 struct object_id old_oid
;
351 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
355 * Ref lines have a number of fields which are space deliminated. The
356 * first field is the OID of the ref. The second field is the ref
357 * name. Subsequent fields (symref-target and peeled) are optional and
358 * don't have a particular order.
360 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
365 if (parse_oid_hex(line_sections
.items
[i
++].string
, &old_oid
, &end
) ||
371 ref
= alloc_ref(line_sections
.items
[i
++].string
);
373 oidcpy(&ref
->old_oid
, &old_oid
);
377 for (; i
< line_sections
.nr
; i
++) {
378 const char *arg
= line_sections
.items
[i
].string
;
379 if (skip_prefix(arg
, "symref-target:", &arg
))
380 ref
->symref
= xstrdup(arg
);
382 if (skip_prefix(arg
, "peeled:", &arg
)) {
383 struct object_id peeled_oid
;
386 if (parse_oid_hex(arg
, &peeled_oid
, &end
) || *end
) {
391 peeled_name
= xstrfmt("%s^{}", ref
->name
);
392 peeled
= alloc_ref(peeled_name
);
394 oidcpy(&peeled
->old_oid
, &peeled_oid
);
396 *list
= &peeled
->next
;
403 string_list_clear(&line_sections
, 0);
407 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
408 struct ref
**list
, int for_push
,
409 const struct argv_array
*ref_prefixes
,
410 const struct string_list
*server_options
)
415 if (server_supports_v2("ls-refs", 1))
416 packet_write_fmt(fd_out
, "command=ls-refs\n");
418 if (server_supports_v2("agent", 0))
419 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
421 if (server_options
&& server_options
->nr
&&
422 server_supports_v2("server-option", 1))
423 for (i
= 0; i
< server_options
->nr
; i
++)
424 packet_write_fmt(fd_out
, "server-option=%s",
425 server_options
->items
[i
].string
);
427 packet_delim(fd_out
);
428 /* When pushing we don't want to request the peeled tags */
430 packet_write_fmt(fd_out
, "peel\n");
431 packet_write_fmt(fd_out
, "symrefs\n");
432 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->argc
; i
++) {
433 packet_write_fmt(fd_out
, "ref-prefix %s\n",
434 ref_prefixes
->argv
[i
]);
436 packet_flush(fd_out
);
438 /* Process response from server */
439 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
440 if (!process_ref_v2(reader
->line
, &list
))
441 die(_("invalid ls-refs response: %s"), reader
->line
);
444 if (reader
->status
!= PACKET_READ_FLUSH
)
445 die(_("expected flush after ref listing"));
450 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
457 len
= strlen(feature
);
458 while (*feature_list
) {
459 const char *found
= strstr(feature_list
, feature
);
462 if (feature_list
== found
|| isspace(found
[-1])) {
463 const char *value
= found
+ len
;
464 /* feature with no value (e.g., "thin-pack") */
465 if (!*value
|| isspace(*value
)) {
470 /* feature with a value (e.g., "agent=git/1.2.3") */
471 else if (*value
== '=') {
474 *lenp
= strcspn(value
, " \t\n");
478 * otherwise we matched a substring of another feature;
482 feature_list
= found
+ 1;
487 int parse_feature_request(const char *feature_list
, const char *feature
)
489 return !!parse_feature_value(feature_list
, feature
, NULL
);
492 const char *server_feature_value(const char *feature
, int *len
)
494 return parse_feature_value(server_capabilities_v1
, feature
, len
);
497 int server_supports(const char *feature
)
499 return !!server_feature_value(feature
, NULL
);
509 int url_is_local_not_ssh(const char *url
)
511 const char *colon
= strchr(url
, ':');
512 const char *slash
= strchr(url
, '/');
513 return !colon
|| (slash
&& slash
< colon
) ||
514 has_dos_drive_prefix(url
);
517 static const char *prot_name(enum protocol protocol
)
528 return "unknown protocol";
532 static enum protocol
get_protocol(const char *name
)
534 if (!strcmp(name
, "ssh"))
536 if (!strcmp(name
, "git"))
538 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
540 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
542 if (!strcmp(name
, "file"))
544 die(_("protocol '%s' is not supported"), name
);
547 static char *host_end(char **hoststart
, int removebrackets
)
549 char *host
= *hoststart
;
551 char *start
= strstr(host
, "@[");
553 start
++; /* Jump over '@' */
556 if (start
[0] == '[') {
557 end
= strchr(start
+ 1, ']');
559 if (removebrackets
) {
561 memmove(start
, start
+ 1, end
- start
);
572 #define STR(s) STR_(s)
574 static void get_host_and_port(char **host
, const char **port
)
577 end
= host_end(host
, 1);
578 colon
= strchr(end
, ':');
580 long portnr
= strtol(colon
+ 1, &end
, 10);
581 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
584 } else if (!colon
[1]) {
590 static void enable_keepalive(int sockfd
)
594 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
595 error_errno(_("unable to set SO_KEEPALIVE on socket"));
600 static const char *ai_name(const struct addrinfo
*ai
)
602 static char addr
[NI_MAXHOST
];
603 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
604 NI_NUMERICHOST
) != 0)
605 xsnprintf(addr
, sizeof(addr
), "(unknown)");
611 * Returns a connected socket() fd, or else die()s.
613 static int git_tcp_connect_sock(char *host
, int flags
)
615 struct strbuf error_message
= STRBUF_INIT
;
617 const char *port
= STR(DEFAULT_GIT_PORT
);
618 struct addrinfo hints
, *ai0
, *ai
;
622 get_host_and_port(&host
, &port
);
626 memset(&hints
, 0, sizeof(hints
));
627 if (flags
& CONNECT_IPV4
)
628 hints
.ai_family
= AF_INET
;
629 else if (flags
& CONNECT_IPV6
)
630 hints
.ai_family
= AF_INET6
;
631 hints
.ai_socktype
= SOCK_STREAM
;
632 hints
.ai_protocol
= IPPROTO_TCP
;
634 if (flags
& CONNECT_VERBOSE
)
635 fprintf(stderr
, _("Looking up %s ... "), host
);
637 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
639 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
641 if (flags
& CONNECT_VERBOSE
)
642 /* TRANSLATORS: this is the end of "Looking up %s ... " */
643 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
645 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
646 sockfd
= socket(ai
->ai_family
,
647 ai
->ai_socktype
, ai
->ai_protocol
);
649 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
650 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
651 host
, cnt
, ai_name(ai
), strerror(errno
));
657 if (flags
& CONNECT_VERBOSE
)
658 fprintf(stderr
, "%s ", ai_name(ai
));
665 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
667 enable_keepalive(sockfd
);
669 if (flags
& CONNECT_VERBOSE
)
670 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
671 fprintf_ln(stderr
, _("done."));
673 strbuf_release(&error_message
);
681 * Returns a connected socket() fd, or else die()s.
683 static int git_tcp_connect_sock(char *host
, int flags
)
685 struct strbuf error_message
= STRBUF_INIT
;
687 const char *port
= STR(DEFAULT_GIT_PORT
);
690 struct sockaddr_in sa
;
695 get_host_and_port(&host
, &port
);
697 if (flags
& CONNECT_VERBOSE
)
698 fprintf(stderr
, _("Looking up %s ... "), host
);
700 he
= gethostbyname(host
);
702 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
703 nport
= strtoul(port
, &ep
, 10);
704 if ( ep
== port
|| *ep
) {
706 struct servent
*se
= getservbyname(port
,"tcp");
708 die(_("unknown port %s"), port
);
712 if (flags
& CONNECT_VERBOSE
)
713 /* TRANSLATORS: this is the end of "Looking up %s ... " */
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 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
748 fprintf_ln(stderr
, _("done."));
757 * Dummy child_process returned by git_connect() if the transport protocol
758 * does not need fork(2).
760 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
762 int git_connection_is_socket(struct child_process
*conn
)
764 return conn
== &no_fork
;
767 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
769 int sockfd
= git_tcp_connect_sock(host
, flags
);
778 static char *git_proxy_command
;
780 static int git_proxy_command_options(const char *var
, const char *value
,
783 if (!strcmp(var
, "core.gitproxy")) {
787 const char *rhost_name
= cb
;
788 int rhost_len
= strlen(rhost_name
);
790 if (git_proxy_command
)
793 return config_error_nonbool(var
);
795 * ;# matches www.kernel.org as well
796 * gitproxy = netcatter-1 for kernel.org
797 * gitproxy = netcatter-2 for sample.xz
798 * gitproxy = netcatter-default
800 for_pos
= strstr(value
, " for ");
802 /* matches everybody */
803 matchlen
= strlen(value
);
805 hostlen
= strlen(for_pos
+ 5);
806 if (rhost_len
< hostlen
)
808 else if (!strncmp(for_pos
+ 5,
809 rhost_name
+ rhost_len
- hostlen
,
811 ((rhost_len
== hostlen
) ||
812 rhost_name
[rhost_len
- hostlen
-1] == '.'))
813 matchlen
= for_pos
- value
;
818 /* core.gitproxy = none for kernel.org */
820 !memcmp(value
, "none", 4))
822 git_proxy_command
= xmemdupz(value
, matchlen
);
827 return git_default_config(var
, value
, cb
);
830 static int git_use_proxy(const char *host
)
832 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
833 git_config(git_proxy_command_options
, (void*)host
);
834 return (git_proxy_command
&& *git_proxy_command
);
837 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
839 const char *port
= STR(DEFAULT_GIT_PORT
);
840 struct child_process
*proxy
;
842 get_host_and_port(&host
, &port
);
844 if (looks_like_command_line_option(host
))
845 die(_("strange hostname '%s' blocked"), host
);
846 if (looks_like_command_line_option(port
))
847 die(_("strange port '%s' blocked"), port
);
849 proxy
= xmalloc(sizeof(*proxy
));
850 child_process_init(proxy
);
851 argv_array_push(&proxy
->args
, git_proxy_command
);
852 argv_array_push(&proxy
->args
, host
);
853 argv_array_push(&proxy
->args
, port
);
856 if (start_command(proxy
))
857 die(_("cannot start proxy %s"), git_proxy_command
);
858 fd
[0] = proxy
->out
; /* read from proxy stdout */
859 fd
[1] = proxy
->in
; /* write to proxy stdin */
863 static char *get_port(char *host
)
866 char *p
= strchr(host
, ':');
869 long port
= strtol(p
+ 1, &end
, 10);
870 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
880 * Extract protocol and relevant parts from the specified connection URL.
881 * The caller must free() the returned strings.
883 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
890 enum protocol protocol
= PROTO_LOCAL
;
892 if (is_url(url_orig
))
893 url
= url_decode(url_orig
);
895 url
= xstrdup(url_orig
);
897 host
= strstr(url
, "://");
900 protocol
= get_protocol(url
);
904 if (!url_is_local_not_ssh(url
)) {
905 protocol
= PROTO_SSH
;
911 * Don't do destructive transforms as protocol code does
912 * '[]' unwrapping in get_host_and_port()
914 end
= host_end(&host
, 0);
916 if (protocol
== PROTO_LOCAL
)
918 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
919 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
921 path
= strchr(end
, separator
);
924 die(_("no path specified; see 'git help pull' for valid url syntax"));
927 * null-terminate hostname and point path to ~ for URL's like this:
928 * ssh://host.xz/~user/repo
931 end
= path
; /* Need to \0 terminate host here */
932 if (separator
== ':')
933 path
++; /* path starts after ':' */
934 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
939 path
= xstrdup(path
);
942 *ret_host
= xstrdup(host
);
948 static const char *get_ssh_command(void)
952 if ((ssh
= getenv("GIT_SSH_COMMAND")))
955 if (!git_config_get_string_const("core.sshcommand", &ssh
))
967 VARIANT_TORTOISEPLINK
,
970 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
972 const char *variant
= getenv("GIT_SSH_VARIANT");
974 if (!variant
&& git_config_get_string_const("ssh.variant", &variant
))
977 if (!strcmp(variant
, "auto"))
978 *ssh_variant
= VARIANT_AUTO
;
979 else if (!strcmp(variant
, "plink"))
980 *ssh_variant
= VARIANT_PLINK
;
981 else if (!strcmp(variant
, "putty"))
982 *ssh_variant
= VARIANT_PUTTY
;
983 else if (!strcmp(variant
, "tortoiseplink"))
984 *ssh_variant
= VARIANT_TORTOISEPLINK
;
985 else if (!strcmp(variant
, "simple"))
986 *ssh_variant
= VARIANT_SIMPLE
;
988 *ssh_variant
= VARIANT_SSH
;
991 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
994 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
998 override_ssh_variant(&ssh_variant
);
1000 if (ssh_variant
!= VARIANT_AUTO
)
1004 p
= xstrdup(ssh_command
);
1005 variant
= basename(p
);
1007 const char **ssh_argv
;
1009 p
= xstrdup(ssh_command
);
1010 if (split_cmdline(p
, &ssh_argv
) > 0) {
1011 variant
= basename((char *)ssh_argv
[0]);
1013 * At this point, variant points into the buffer
1014 * referenced by p, hence we do not need ssh_argv
1024 if (!strcasecmp(variant
, "ssh") ||
1025 !strcasecmp(variant
, "ssh.exe"))
1026 ssh_variant
= VARIANT_SSH
;
1027 else if (!strcasecmp(variant
, "plink") ||
1028 !strcasecmp(variant
, "plink.exe"))
1029 ssh_variant
= VARIANT_PLINK
;
1030 else if (!strcasecmp(variant
, "tortoiseplink") ||
1031 !strcasecmp(variant
, "tortoiseplink.exe"))
1032 ssh_variant
= VARIANT_TORTOISEPLINK
;
1039 * Open a connection using Git's native protocol.
1041 * The caller is responsible for freeing hostandport, but this function may
1042 * modify it (for example, to truncate it to remove the port part).
1044 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1045 const char *path
, const char *prog
,
1046 enum protocol_version version
,
1049 struct child_process
*conn
;
1050 struct strbuf request
= STRBUF_INIT
;
1052 * Set up virtual host information based on where we will
1053 * connect, unless the user has overridden us in
1056 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1058 target_host
= xstrdup(target_host
);
1060 target_host
= xstrdup(hostandport
);
1062 transport_check_allowed("git");
1065 * These underlying connection commands die() if they
1068 if (git_use_proxy(hostandport
))
1069 conn
= git_proxy_connect(fd
, hostandport
);
1071 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1073 * Separate original protocol components prog and path
1074 * from extended host header with a NUL byte.
1076 * Note: Do not add any other headers here! Doing so
1077 * will cause older git-daemon servers to crash.
1079 strbuf_addf(&request
,
1084 /* If using a new version put that stuff here after a second null byte */
1086 strbuf_addch(&request
, '\0');
1087 strbuf_addf(&request
, "version=%d%c",
1091 packet_write(fd
[1], request
.buf
, request
.len
);
1094 strbuf_release(&request
);
1099 * Append the appropriate environment variables to `env` and options to
1100 * `args` for running ssh in Git's SSH-tunneled transport.
1102 static void push_ssh_options(struct argv_array
*args
, struct argv_array
*env
,
1103 enum ssh_variant variant
, const char *port
,
1104 enum protocol_version version
, int flags
)
1106 if (variant
== VARIANT_SSH
&&
1108 argv_array_push(args
, "-o");
1109 argv_array_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1110 argv_array_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1114 if (flags
& CONNECT_IPV4
) {
1117 BUG("VARIANT_AUTO passed to push_ssh_options");
1118 case VARIANT_SIMPLE
:
1119 die(_("ssh variant 'simple' does not support -4"));
1123 case VARIANT_TORTOISEPLINK
:
1124 argv_array_push(args
, "-4");
1126 } else if (flags
& CONNECT_IPV6
) {
1129 BUG("VARIANT_AUTO passed to push_ssh_options");
1130 case VARIANT_SIMPLE
:
1131 die(_("ssh variant 'simple' does not support -6"));
1135 case VARIANT_TORTOISEPLINK
:
1136 argv_array_push(args
, "-6");
1140 if (variant
== VARIANT_TORTOISEPLINK
)
1141 argv_array_push(args
, "-batch");
1146 BUG("VARIANT_AUTO passed to push_ssh_options");
1147 case VARIANT_SIMPLE
:
1148 die(_("ssh variant 'simple' does not support setting port"));
1150 argv_array_push(args
, "-p");
1154 case VARIANT_TORTOISEPLINK
:
1155 argv_array_push(args
, "-P");
1158 argv_array_push(args
, port
);
1162 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1163 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1164 const char *port
, enum protocol_version version
,
1168 enum ssh_variant variant
;
1170 if (looks_like_command_line_option(ssh_host
))
1171 die(_("strange hostname '%s' blocked"), ssh_host
);
1173 ssh
= get_ssh_command();
1175 variant
= determine_ssh_variant(ssh
, 1);
1178 * GIT_SSH is the no-shell version of
1179 * GIT_SSH_COMMAND (and must remain so for
1180 * historical compatibility).
1182 conn
->use_shell
= 0;
1184 ssh
= getenv("GIT_SSH");
1187 variant
= determine_ssh_variant(ssh
, 0);
1190 if (variant
== VARIANT_AUTO
) {
1191 struct child_process detect
= CHILD_PROCESS_INIT
;
1193 detect
.use_shell
= conn
->use_shell
;
1194 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1196 argv_array_push(&detect
.args
, ssh
);
1197 argv_array_push(&detect
.args
, "-G");
1198 push_ssh_options(&detect
.args
, &detect
.env_array
,
1199 VARIANT_SSH
, port
, version
, flags
);
1200 argv_array_push(&detect
.args
, ssh_host
);
1202 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1205 argv_array_push(&conn
->args
, ssh
);
1206 push_ssh_options(&conn
->args
, &conn
->env_array
, variant
, port
, version
, flags
);
1207 argv_array_push(&conn
->args
, ssh_host
);
1211 * This returns the dummy child_process `no_fork` if the transport protocol
1212 * does not need fork(2), or a struct child_process object if it does. Once
1213 * done, finish the connection with finish_connect() with the value returned
1214 * from this function (it is safe to call finish_connect() with NULL to
1215 * support the former case).
1217 * If it returns, the connect is successful; it just dies on errors (this
1218 * will hopefully be changed in a libification effort, to return NULL when
1219 * the connection failed).
1221 struct child_process
*git_connect(int fd
[2], const char *url
,
1222 const char *prog
, int flags
)
1224 char *hostandport
, *path
;
1225 struct child_process
*conn
;
1226 enum protocol protocol
;
1227 enum protocol_version version
= get_protocol_version_config();
1230 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1231 * to perform a push, then fallback to v0 since the client doesn't know
1232 * how to push yet using v2.
1234 if (version
== protocol_v2
&& !strcmp("git-receive-pack", prog
))
1235 version
= protocol_v0
;
1237 /* Without this we cannot rely on waitpid() to tell
1238 * what happened to our children.
1240 signal(SIGCHLD
, SIG_DFL
);
1242 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1243 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1244 printf("Diag: url=%s\n", url
? url
: "NULL");
1245 printf("Diag: protocol=%s\n", prot_name(protocol
));
1246 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1247 printf("Diag: path=%s\n", path
? path
: "NULL");
1249 } else if (protocol
== PROTO_GIT
) {
1250 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1252 struct strbuf cmd
= STRBUF_INIT
;
1253 const char *const *var
;
1255 conn
= xmalloc(sizeof(*conn
));
1256 child_process_init(conn
);
1258 if (looks_like_command_line_option(path
))
1259 die(_("strange pathname '%s' blocked"), path
);
1261 strbuf_addstr(&cmd
, prog
);
1262 strbuf_addch(&cmd
, ' ');
1263 sq_quote_buf(&cmd
, path
);
1265 /* remove repo-local variables from the environment */
1266 for (var
= local_repo_env
; *var
; var
++)
1267 argv_array_push(&conn
->env_array
, *var
);
1269 conn
->use_shell
= 1;
1270 conn
->in
= conn
->out
= -1;
1271 if (protocol
== PROTO_SSH
) {
1272 char *ssh_host
= hostandport
;
1273 const char *port
= NULL
;
1274 transport_check_allowed("ssh");
1275 get_host_and_port(&ssh_host
, &port
);
1278 port
= get_port(ssh_host
);
1280 if (flags
& CONNECT_DIAG_URL
) {
1281 printf("Diag: url=%s\n", url
? url
: "NULL");
1282 printf("Diag: protocol=%s\n", prot_name(protocol
));
1283 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1284 printf("Diag: port=%s\n", port
? port
: "NONE");
1285 printf("Diag: path=%s\n", path
? path
: "NULL");
1290 strbuf_release(&cmd
);
1293 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1295 transport_check_allowed("file");
1297 argv_array_pushf(&conn
->env_array
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1301 argv_array_push(&conn
->args
, cmd
.buf
);
1303 if (start_command(conn
))
1304 die(_("unable to fork"));
1306 fd
[0] = conn
->out
; /* read from child's stdout */
1307 fd
[1] = conn
->in
; /* write to child's stdin */
1308 strbuf_release(&cmd
);
1315 int finish_connect(struct child_process
*conn
)
1318 if (!conn
|| git_connection_is_socket(conn
))
1321 code
= finish_command(conn
);