1 #include "git-compat-util.h"
7 #include "run-command.h"
11 #include "string-list.h"
12 #include "oid-array.h"
13 #include "transport.h"
19 static char *server_capabilities_v1
;
20 static struct strvec server_capabilities_v2
= STRVEC_INIT
;
21 static const char *next_server_feature_value(const char *feature
, int *len
, int *offset
);
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
.nr
; i
++) {
75 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
76 (!*out
|| *out
== '='))
81 die(_("server doesn't support '%s'"), c
);
86 int server_feature_v2(const char *c
, const char **v
)
90 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
92 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
101 int server_supports_feature(const char *c
, const char *feature
,
106 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
108 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
109 (!*out
|| *(out
++) == '=')) {
110 if (parse_feature_request(out
, feature
))
118 die(_("server doesn't support feature '%s'"), feature
);
123 static void process_capabilities_v2(struct packet_reader
*reader
)
125 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
126 strvec_push(&server_capabilities_v2
, reader
->line
);
128 if (reader
->status
!= PACKET_READ_FLUSH
)
129 die(_("expected flush after capabilities"));
132 enum protocol_version
discover_version(struct packet_reader
*reader
)
134 enum protocol_version version
= protocol_unknown_version
;
137 * Peek the first line of the server's response to
138 * determine the protocol version the server is speaking.
140 switch (packet_reader_peek(reader
)) {
141 case PACKET_READ_EOF
:
142 die_initial_contact(0);
143 case PACKET_READ_FLUSH
:
144 case PACKET_READ_DELIM
:
145 case PACKET_READ_RESPONSE_END
:
146 version
= protocol_v0
;
148 case PACKET_READ_NORMAL
:
149 version
= determine_protocol_version_client(reader
->line
);
155 process_capabilities_v2(reader
);
158 /* Read the peeked version line */
159 packet_reader_read(reader
);
163 case protocol_unknown_version
:
164 BUG("unknown protocol version");
167 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
172 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
175 struct string_list_item
*item
;
178 return; /* just "symref" */
179 /* e.g. "symref=HEAD:refs/heads/master" */
180 sym
= xmemdupz(val
, len
);
181 target
= strchr(sym
, ':');
183 /* just "symref=something" */
186 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
187 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
188 /* "symref=bogus:pair */
190 item
= string_list_append_nodup(symref
, sym
);
198 static void annotate_refs_with_symref_info(struct ref
*ref
)
200 struct string_list symref
= STRING_LIST_INIT_DUP
;
207 val
= next_server_feature_value("symref", &len
, &offset
);
210 parse_one_symref_info(&symref
, val
, len
);
212 string_list_sort(&symref
);
214 for (; ref
; ref
= ref
->next
) {
215 struct string_list_item
*item
;
216 item
= string_list_lookup(&symref
, ref
->name
);
219 ref
->symref
= xstrdup((char *)item
->util
);
221 string_list_clear(&symref
, 0);
224 static void process_capabilities(struct packet_reader
*reader
, int *linelen
)
226 const char *feat_val
;
228 const char *line
= reader
->line
;
229 int nul_location
= strlen(line
);
230 if (nul_location
== *linelen
)
232 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
233 *linelen
= nul_location
;
235 feat_val
= server_feature_value("object-format", &feat_len
);
237 char *hash_name
= xstrndup(feat_val
, feat_len
);
238 int hash_algo
= hash_algo_by_name(hash_name
);
239 if (hash_algo
!= GIT_HASH_UNKNOWN
)
240 reader
->hash_algo
= &hash_algos
[hash_algo
];
243 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
247 static int process_dummy_ref(const struct packet_reader
*reader
)
249 const char *line
= reader
->line
;
250 struct object_id oid
;
253 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
259 return oideq(null_oid(), &oid
) && !strcmp(name
, "capabilities^{}");
262 static void check_no_capabilities(const char *line
, int len
)
264 if (strlen(line
) != len
)
265 warning(_("ignoring capabilities after first line '%s'"),
266 line
+ strlen(line
));
269 static int process_ref(const struct packet_reader
*reader
, int len
,
270 struct ref
***list
, unsigned int flags
,
271 struct oid_array
*extra_have
)
273 const char *line
= reader
->line
;
274 struct object_id old_oid
;
277 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
283 if (extra_have
&& !strcmp(name
, ".have")) {
284 oid_array_append(extra_have
, &old_oid
);
285 } else if (!strcmp(name
, "capabilities^{}")) {
286 die(_("protocol error: unexpected capabilities^{}"));
287 } else if (check_ref(name
, flags
)) {
288 struct ref
*ref
= alloc_ref(name
);
289 oidcpy(&ref
->old_oid
, &old_oid
);
293 check_no_capabilities(line
, len
);
297 static int process_shallow(const struct packet_reader
*reader
, int len
,
298 struct oid_array
*shallow_points
)
300 const char *line
= reader
->line
;
302 struct object_id old_oid
;
304 if (!skip_prefix(line
, "shallow ", &arg
))
307 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
308 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
310 die(_("repository on the other end cannot be shallow"));
311 oid_array_append(shallow_points
, &old_oid
);
312 check_no_capabilities(line
, len
);
316 enum get_remote_heads_state
{
317 EXPECTING_FIRST_REF
= 0,
324 * Read all the refs from the other end
326 struct ref
**get_remote_heads(struct packet_reader
*reader
,
327 struct ref
**list
, unsigned int flags
,
328 struct oid_array
*extra_have
,
329 struct oid_array
*shallow_points
)
331 struct ref
**orig_list
= list
;
333 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
337 while (state
!= EXPECTING_DONE
) {
338 switch (packet_reader_read(reader
)) {
339 case PACKET_READ_EOF
:
340 die_initial_contact(1);
341 case PACKET_READ_NORMAL
:
342 len
= reader
->pktlen
;
344 case PACKET_READ_FLUSH
:
345 state
= EXPECTING_DONE
;
347 case PACKET_READ_DELIM
:
348 case PACKET_READ_RESPONSE_END
:
349 die(_("invalid packet"));
353 case EXPECTING_FIRST_REF
:
354 process_capabilities(reader
, &len
);
355 if (process_dummy_ref(reader
)) {
356 state
= EXPECTING_SHALLOW
;
359 state
= EXPECTING_REF
;
362 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
364 state
= EXPECTING_SHALLOW
;
366 case EXPECTING_SHALLOW
:
367 if (process_shallow(reader
, len
, shallow_points
))
369 die(_("protocol error: unexpected '%s'"), reader
->line
);
375 annotate_refs_with_symref_info(*orig_list
);
380 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
381 static int process_ref_v2(struct packet_reader
*reader
, struct ref
***list
,
382 const char **unborn_head_target
)
386 struct object_id old_oid
;
388 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
390 const char *line
= reader
->line
;
393 * Ref lines have a number of fields which are space deliminated. The
394 * first field is the OID of the ref. The second field is the ref
395 * name. Subsequent fields (symref-target and peeled) are optional and
396 * don't have a particular order.
398 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
403 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
405 if (unborn_head_target
&&
406 !strcmp("HEAD", line_sections
.items
[i
++].string
)) {
408 * Look for the symref target (if any). If found,
409 * return it to the caller.
411 for (; i
< line_sections
.nr
; i
++) {
412 const char *arg
= line_sections
.items
[i
].string
;
414 if (skip_prefix(arg
, "symref-target:", &arg
)) {
415 *unborn_head_target
= xstrdup(arg
);
422 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
428 ref
= alloc_ref(line_sections
.items
[i
++].string
);
430 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
434 for (; i
< line_sections
.nr
; i
++) {
435 const char *arg
= line_sections
.items
[i
].string
;
436 if (skip_prefix(arg
, "symref-target:", &arg
))
437 ref
->symref
= xstrdup(arg
);
439 if (skip_prefix(arg
, "peeled:", &arg
)) {
440 struct object_id peeled_oid
;
443 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
444 reader
->hash_algo
) || *end
) {
449 peeled_name
= xstrfmt("%s^{}", ref
->name
);
450 peeled
= alloc_ref(peeled_name
);
452 memcpy(peeled
->old_oid
.hash
, peeled_oid
.hash
,
453 reader
->hash_algo
->rawsz
);
455 *list
= &peeled
->next
;
462 string_list_clear(&line_sections
, 0);
466 void check_stateless_delimiter(int stateless_rpc
,
467 struct packet_reader
*reader
,
471 return; /* not in stateless mode, no delimiter expected */
472 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
476 static void send_capabilities(int fd_out
, struct packet_reader
*reader
)
478 const char *hash_name
;
480 if (server_supports_v2("agent", 0))
481 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
483 if (server_feature_v2("object-format", &hash_name
)) {
484 int hash_algo
= hash_algo_by_name(hash_name
);
485 if (hash_algo
== GIT_HASH_UNKNOWN
)
486 die(_("unknown object format '%s' specified by server"), hash_name
);
487 reader
->hash_algo
= &hash_algos
[hash_algo
];
488 packet_write_fmt(fd_out
, "object-format=%s", reader
->hash_algo
->name
);
490 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
494 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
495 struct ref
**list
, int for_push
,
496 struct transport_ls_refs_options
*transport_options
,
497 const struct string_list
*server_options
,
501 struct strvec
*ref_prefixes
= transport_options
?
502 &transport_options
->ref_prefixes
: NULL
;
503 const char **unborn_head_target
= transport_options
?
504 &transport_options
->unborn_head_target
: NULL
;
507 if (server_supports_v2("ls-refs", 1))
508 packet_write_fmt(fd_out
, "command=ls-refs\n");
510 /* Send capabilities */
511 send_capabilities(fd_out
, reader
);
513 if (server_options
&& server_options
->nr
&&
514 server_supports_v2("server-option", 1))
515 for (i
= 0; i
< server_options
->nr
; i
++)
516 packet_write_fmt(fd_out
, "server-option=%s",
517 server_options
->items
[i
].string
);
519 packet_delim(fd_out
);
520 /* When pushing we don't want to request the peeled tags */
522 packet_write_fmt(fd_out
, "peel\n");
523 packet_write_fmt(fd_out
, "symrefs\n");
524 if (server_supports_feature("ls-refs", "unborn", 0))
525 packet_write_fmt(fd_out
, "unborn\n");
526 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->nr
; i
++) {
527 packet_write_fmt(fd_out
, "ref-prefix %s\n",
530 packet_flush(fd_out
);
532 /* Process response from server */
533 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
534 if (!process_ref_v2(reader
, &list
, unborn_head_target
))
535 die(_("invalid ls-refs response: %s"), reader
->line
);
538 if (reader
->status
!= PACKET_READ_FLUSH
)
539 die(_("expected flush after ref listing"));
541 check_stateless_delimiter(stateless_rpc
, reader
,
542 _("expected response end packet after ref listing"));
547 const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
, int *offset
)
554 len
= strlen(feature
);
556 feature_list
+= *offset
;
557 while (*feature_list
) {
558 const char *found
= strstr(feature_list
, feature
);
561 if (feature_list
== found
|| isspace(found
[-1])) {
562 const char *value
= found
+ len
;
563 /* feature with no value (e.g., "thin-pack") */
564 if (!*value
|| isspace(*value
)) {
568 *offset
= found
+ len
- feature_list
;
571 /* feature with a value (e.g., "agent=git/1.2.3") */
572 else if (*value
== '=') {
576 end
= strcspn(value
, " \t\n");
580 *offset
= value
+ end
- feature_list
;
584 * otherwise we matched a substring of another feature;
588 feature_list
= found
+ 1;
593 int server_supports_hash(const char *desired
, int *feature_supported
)
599 hash
= next_server_feature_value("object-format", &len
, &offset
);
600 if (feature_supported
)
601 *feature_supported
= !!hash
;
603 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
607 if (!xstrncmpz(desired
, hash
, len
))
610 hash
= next_server_feature_value("object-format", &len
, &offset
);
615 int parse_feature_request(const char *feature_list
, const char *feature
)
617 return !!parse_feature_value(feature_list
, feature
, NULL
, NULL
);
620 static const char *next_server_feature_value(const char *feature
, int *len
, int *offset
)
622 return parse_feature_value(server_capabilities_v1
, feature
, len
, offset
);
625 const char *server_feature_value(const char *feature
, int *len
)
627 return parse_feature_value(server_capabilities_v1
, feature
, len
, NULL
);
630 int server_supports(const char *feature
)
632 return !!server_feature_value(feature
, NULL
);
642 int url_is_local_not_ssh(const char *url
)
644 const char *colon
= strchr(url
, ':');
645 const char *slash
= strchr(url
, '/');
646 return !colon
|| (slash
&& slash
< colon
) ||
647 (has_dos_drive_prefix(url
) && is_valid_path(url
));
650 static const char *prot_name(enum protocol protocol
)
661 return "unknown protocol";
665 static enum protocol
get_protocol(const char *name
)
667 if (!strcmp(name
, "ssh"))
669 if (!strcmp(name
, "git"))
671 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
673 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
675 if (!strcmp(name
, "file"))
677 die(_("protocol '%s' is not supported"), name
);
680 static char *host_end(char **hoststart
, int removebrackets
)
682 char *host
= *hoststart
;
684 char *start
= strstr(host
, "@[");
686 start
++; /* Jump over '@' */
689 if (start
[0] == '[') {
690 end
= strchr(start
+ 1, ']');
692 if (removebrackets
) {
694 memmove(start
, start
+ 1, end
- start
);
705 #define STR(s) STR_(s)
707 static void get_host_and_port(char **host
, const char **port
)
710 end
= host_end(host
, 1);
711 colon
= strchr(end
, ':');
713 long portnr
= strtol(colon
+ 1, &end
, 10);
714 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
717 } else if (!colon
[1]) {
723 static void enable_keepalive(int sockfd
)
727 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
728 error_errno(_("unable to set SO_KEEPALIVE on socket"));
733 static const char *ai_name(const struct addrinfo
*ai
)
735 static char addr
[NI_MAXHOST
];
736 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
737 NI_NUMERICHOST
) != 0)
738 xsnprintf(addr
, sizeof(addr
), "(unknown)");
744 * Returns a connected socket() fd, or else die()s.
746 static int git_tcp_connect_sock(char *host
, int flags
)
748 struct strbuf error_message
= STRBUF_INIT
;
750 const char *port
= STR(DEFAULT_GIT_PORT
);
751 struct addrinfo hints
, *ai0
, *ai
;
755 get_host_and_port(&host
, &port
);
759 memset(&hints
, 0, sizeof(hints
));
760 if (flags
& CONNECT_IPV4
)
761 hints
.ai_family
= AF_INET
;
762 else if (flags
& CONNECT_IPV6
)
763 hints
.ai_family
= AF_INET6
;
764 hints
.ai_socktype
= SOCK_STREAM
;
765 hints
.ai_protocol
= IPPROTO_TCP
;
767 if (flags
& CONNECT_VERBOSE
)
768 fprintf(stderr
, _("Looking up %s ... "), host
);
770 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
772 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
774 if (flags
& CONNECT_VERBOSE
)
775 /* TRANSLATORS: this is the end of "Looking up %s ... " */
776 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
778 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
779 sockfd
= socket(ai
->ai_family
,
780 ai
->ai_socktype
, ai
->ai_protocol
);
782 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
783 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
784 host
, cnt
, ai_name(ai
), strerror(errno
));
790 if (flags
& CONNECT_VERBOSE
)
791 fprintf(stderr
, "%s ", ai_name(ai
));
798 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
800 enable_keepalive(sockfd
);
802 if (flags
& CONNECT_VERBOSE
)
803 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
804 fprintf_ln(stderr
, _("done."));
806 strbuf_release(&error_message
);
814 * Returns a connected socket() fd, or else die()s.
816 static int git_tcp_connect_sock(char *host
, int flags
)
818 struct strbuf error_message
= STRBUF_INIT
;
820 const char *port
= STR(DEFAULT_GIT_PORT
);
823 struct sockaddr_in sa
;
828 get_host_and_port(&host
, &port
);
830 if (flags
& CONNECT_VERBOSE
)
831 fprintf(stderr
, _("Looking up %s ... "), host
);
833 he
= gethostbyname(host
);
835 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
836 nport
= strtoul(port
, &ep
, 10);
837 if ( ep
== port
|| *ep
) {
839 struct servent
*se
= getservbyname(port
,"tcp");
841 die(_("unknown port %s"), port
);
845 if (flags
& CONNECT_VERBOSE
)
846 /* TRANSLATORS: this is the end of "Looking up %s ... " */
847 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
849 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
850 memset(&sa
, 0, sizeof sa
);
851 sa
.sin_family
= he
->h_addrtype
;
852 sa
.sin_port
= htons(nport
);
853 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
855 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
857 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
858 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
861 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
868 if (flags
& CONNECT_VERBOSE
)
869 fprintf(stderr
, "%s ",
870 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
875 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
877 enable_keepalive(sockfd
);
879 if (flags
& CONNECT_VERBOSE
)
880 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
881 fprintf_ln(stderr
, _("done."));
890 * Dummy child_process returned by git_connect() if the transport protocol
891 * does not need fork(2).
893 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
895 int git_connection_is_socket(struct child_process
*conn
)
897 return conn
== &no_fork
;
900 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
902 int sockfd
= git_tcp_connect_sock(host
, flags
);
911 static char *git_proxy_command
;
913 static int git_proxy_command_options(const char *var
, const char *value
,
916 if (!strcmp(var
, "core.gitproxy")) {
920 const char *rhost_name
= cb
;
921 int rhost_len
= strlen(rhost_name
);
923 if (git_proxy_command
)
926 return config_error_nonbool(var
);
928 * ;# matches www.kernel.org as well
929 * gitproxy = netcatter-1 for kernel.org
930 * gitproxy = netcatter-2 for sample.xz
931 * gitproxy = netcatter-default
933 for_pos
= strstr(value
, " for ");
935 /* matches everybody */
936 matchlen
= strlen(value
);
938 hostlen
= strlen(for_pos
+ 5);
939 if (rhost_len
< hostlen
)
941 else if (!strncmp(for_pos
+ 5,
942 rhost_name
+ rhost_len
- hostlen
,
944 ((rhost_len
== hostlen
) ||
945 rhost_name
[rhost_len
- hostlen
-1] == '.'))
946 matchlen
= for_pos
- value
;
951 /* core.gitproxy = none for kernel.org */
953 !memcmp(value
, "none", 4))
955 git_proxy_command
= xmemdupz(value
, matchlen
);
960 return git_default_config(var
, value
, cb
);
963 static int git_use_proxy(const char *host
)
965 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
966 git_config(git_proxy_command_options
, (void*)host
);
967 return (git_proxy_command
&& *git_proxy_command
);
970 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
972 const char *port
= STR(DEFAULT_GIT_PORT
);
973 struct child_process
*proxy
;
975 get_host_and_port(&host
, &port
);
977 if (looks_like_command_line_option(host
))
978 die(_("strange hostname '%s' blocked"), host
);
979 if (looks_like_command_line_option(port
))
980 die(_("strange port '%s' blocked"), port
);
982 proxy
= xmalloc(sizeof(*proxy
));
983 child_process_init(proxy
);
984 strvec_push(&proxy
->args
, git_proxy_command
);
985 strvec_push(&proxy
->args
, host
);
986 strvec_push(&proxy
->args
, port
);
989 if (start_command(proxy
))
990 die(_("cannot start proxy %s"), git_proxy_command
);
991 fd
[0] = proxy
->out
; /* read from proxy stdout */
992 fd
[1] = proxy
->in
; /* write to proxy stdin */
996 static char *get_port(char *host
)
999 char *p
= strchr(host
, ':');
1002 long port
= strtol(p
+ 1, &end
, 10);
1003 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
1013 * Extract protocol and relevant parts from the specified connection URL.
1014 * The caller must free() the returned strings.
1016 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
1022 int separator
= '/';
1023 enum protocol protocol
= PROTO_LOCAL
;
1025 if (is_url(url_orig
))
1026 url
= url_decode(url_orig
);
1028 url
= xstrdup(url_orig
);
1030 host
= strstr(url
, "://");
1033 protocol
= get_protocol(url
);
1037 if (!url_is_local_not_ssh(url
)) {
1038 protocol
= PROTO_SSH
;
1044 * Don't do destructive transforms as protocol code does
1045 * '[]' unwrapping in get_host_and_port()
1047 end
= host_end(&host
, 0);
1049 if (protocol
== PROTO_LOCAL
)
1051 else if (protocol
== PROTO_FILE
&& *host
!= '/' &&
1052 !has_dos_drive_prefix(host
) &&
1053 offset_1st_component(host
- 2) > 1)
1054 path
= host
- 2; /* include the leading "//" */
1055 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
1056 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1058 path
= strchr(end
, separator
);
1060 if (!path
|| !*path
)
1061 die(_("no path specified; see 'git help pull' for valid url syntax"));
1064 * null-terminate hostname and point path to ~ for URL's like this:
1065 * ssh://host.xz/~user/repo
1068 end
= path
; /* Need to \0 terminate host here */
1069 if (separator
== ':')
1070 path
++; /* path starts after ':' */
1071 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
1076 path
= xstrdup(path
);
1079 *ret_host
= xstrdup(host
);
1085 static const char *get_ssh_command(void)
1089 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1092 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
1104 VARIANT_TORTOISEPLINK
,
1107 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
1109 const char *variant
= getenv("GIT_SSH_VARIANT");
1111 if (!variant
&& git_config_get_string_tmp("ssh.variant", &variant
))
1114 if (!strcmp(variant
, "auto"))
1115 *ssh_variant
= VARIANT_AUTO
;
1116 else if (!strcmp(variant
, "plink"))
1117 *ssh_variant
= VARIANT_PLINK
;
1118 else if (!strcmp(variant
, "putty"))
1119 *ssh_variant
= VARIANT_PUTTY
;
1120 else if (!strcmp(variant
, "tortoiseplink"))
1121 *ssh_variant
= VARIANT_TORTOISEPLINK
;
1122 else if (!strcmp(variant
, "simple"))
1123 *ssh_variant
= VARIANT_SIMPLE
;
1125 *ssh_variant
= VARIANT_SSH
;
1128 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1131 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1132 const char *variant
;
1135 override_ssh_variant(&ssh_variant
);
1137 if (ssh_variant
!= VARIANT_AUTO
)
1141 p
= xstrdup(ssh_command
);
1142 variant
= basename(p
);
1144 const char **ssh_argv
;
1146 p
= xstrdup(ssh_command
);
1147 if (split_cmdline(p
, &ssh_argv
) > 0) {
1148 variant
= basename((char *)ssh_argv
[0]);
1150 * At this point, variant points into the buffer
1151 * referenced by p, hence we do not need ssh_argv
1161 if (!strcasecmp(variant
, "ssh") ||
1162 !strcasecmp(variant
, "ssh.exe"))
1163 ssh_variant
= VARIANT_SSH
;
1164 else if (!strcasecmp(variant
, "plink") ||
1165 !strcasecmp(variant
, "plink.exe"))
1166 ssh_variant
= VARIANT_PLINK
;
1167 else if (!strcasecmp(variant
, "tortoiseplink") ||
1168 !strcasecmp(variant
, "tortoiseplink.exe"))
1169 ssh_variant
= VARIANT_TORTOISEPLINK
;
1176 * Open a connection using Git's native protocol.
1178 * The caller is responsible for freeing hostandport, but this function may
1179 * modify it (for example, to truncate it to remove the port part).
1181 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1182 const char *path
, const char *prog
,
1183 enum protocol_version version
,
1186 struct child_process
*conn
;
1187 struct strbuf request
= STRBUF_INIT
;
1189 * Set up virtual host information based on where we will
1190 * connect, unless the user has overridden us in
1193 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1195 target_host
= xstrdup(target_host
);
1197 target_host
= xstrdup(hostandport
);
1199 transport_check_allowed("git");
1200 if (strchr(target_host
, '\n') || strchr(path
, '\n'))
1201 die(_("newline is forbidden in git:// hosts and repo paths"));
1204 * These underlying connection commands die() if they
1207 if (git_use_proxy(hostandport
))
1208 conn
= git_proxy_connect(fd
, hostandport
);
1210 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1212 * Separate original protocol components prog and path
1213 * from extended host header with a NUL byte.
1215 * Note: Do not add any other headers here! Doing so
1216 * will cause older git-daemon servers to crash.
1218 strbuf_addf(&request
,
1223 /* If using a new version put that stuff here after a second null byte */
1225 strbuf_addch(&request
, '\0');
1226 strbuf_addf(&request
, "version=%d%c",
1230 packet_write(fd
[1], request
.buf
, request
.len
);
1233 strbuf_release(&request
);
1238 * Append the appropriate environment variables to `env` and options to
1239 * `args` for running ssh in Git's SSH-tunneled transport.
1241 static void push_ssh_options(struct strvec
*args
, struct strvec
*env
,
1242 enum ssh_variant variant
, const char *port
,
1243 enum protocol_version version
, int flags
)
1245 if (variant
== VARIANT_SSH
&&
1247 strvec_push(args
, "-o");
1248 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1249 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1253 if (flags
& CONNECT_IPV4
) {
1256 BUG("VARIANT_AUTO passed to push_ssh_options");
1257 case VARIANT_SIMPLE
:
1258 die(_("ssh variant 'simple' does not support -4"));
1262 case VARIANT_TORTOISEPLINK
:
1263 strvec_push(args
, "-4");
1265 } else if (flags
& CONNECT_IPV6
) {
1268 BUG("VARIANT_AUTO passed to push_ssh_options");
1269 case VARIANT_SIMPLE
:
1270 die(_("ssh variant 'simple' does not support -6"));
1274 case VARIANT_TORTOISEPLINK
:
1275 strvec_push(args
, "-6");
1279 if (variant
== VARIANT_TORTOISEPLINK
)
1280 strvec_push(args
, "-batch");
1285 BUG("VARIANT_AUTO passed to push_ssh_options");
1286 case VARIANT_SIMPLE
:
1287 die(_("ssh variant 'simple' does not support setting port"));
1289 strvec_push(args
, "-p");
1293 case VARIANT_TORTOISEPLINK
:
1294 strvec_push(args
, "-P");
1297 strvec_push(args
, port
);
1301 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1302 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1303 const char *port
, enum protocol_version version
,
1307 enum ssh_variant variant
;
1309 if (looks_like_command_line_option(ssh_host
))
1310 die(_("strange hostname '%s' blocked"), ssh_host
);
1312 ssh
= get_ssh_command();
1314 variant
= determine_ssh_variant(ssh
, 1);
1317 * GIT_SSH is the no-shell version of
1318 * GIT_SSH_COMMAND (and must remain so for
1319 * historical compatibility).
1321 conn
->use_shell
= 0;
1323 ssh
= getenv("GIT_SSH");
1326 variant
= determine_ssh_variant(ssh
, 0);
1329 if (variant
== VARIANT_AUTO
) {
1330 struct child_process detect
= CHILD_PROCESS_INIT
;
1332 detect
.use_shell
= conn
->use_shell
;
1333 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1335 strvec_push(&detect
.args
, ssh
);
1336 strvec_push(&detect
.args
, "-G");
1337 push_ssh_options(&detect
.args
, &detect
.env
,
1338 VARIANT_SSH
, port
, version
, flags
);
1339 strvec_push(&detect
.args
, ssh_host
);
1341 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1344 strvec_push(&conn
->args
, ssh
);
1345 push_ssh_options(&conn
->args
, &conn
->env
, variant
, port
, version
,
1347 strvec_push(&conn
->args
, ssh_host
);
1351 * This returns the dummy child_process `no_fork` if the transport protocol
1352 * does not need fork(2), or a struct child_process object if it does. Once
1353 * done, finish the connection with finish_connect() with the value returned
1354 * from this function (it is safe to call finish_connect() with NULL to
1355 * support the former case).
1357 * If it returns, the connect is successful; it just dies on errors (this
1358 * will hopefully be changed in a libification effort, to return NULL when
1359 * the connection failed).
1361 struct child_process
*git_connect(int fd
[2], const char *url
,
1362 const char *prog
, int flags
)
1364 char *hostandport
, *path
;
1365 struct child_process
*conn
;
1366 enum protocol protocol
;
1367 enum protocol_version version
= get_protocol_version_config();
1370 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1371 * to perform a push, then fallback to v0 since the client doesn't know
1372 * how to push yet using v2.
1374 if (version
== protocol_v2
&& !strcmp("git-receive-pack", prog
))
1375 version
= protocol_v0
;
1377 /* Without this we cannot rely on waitpid() to tell
1378 * what happened to our children.
1380 signal(SIGCHLD
, SIG_DFL
);
1382 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1383 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1384 printf("Diag: url=%s\n", url
? url
: "NULL");
1385 printf("Diag: protocol=%s\n", prot_name(protocol
));
1386 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1387 printf("Diag: path=%s\n", path
? path
: "NULL");
1389 } else if (protocol
== PROTO_GIT
) {
1390 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1391 conn
->trace2_child_class
= "transport/git";
1393 struct strbuf cmd
= STRBUF_INIT
;
1394 const char *const *var
;
1396 conn
= xmalloc(sizeof(*conn
));
1397 child_process_init(conn
);
1399 if (looks_like_command_line_option(path
))
1400 die(_("strange pathname '%s' blocked"), path
);
1402 strbuf_addstr(&cmd
, prog
);
1403 strbuf_addch(&cmd
, ' ');
1404 sq_quote_buf(&cmd
, path
);
1406 /* remove repo-local variables from the environment */
1407 for (var
= local_repo_env
; *var
; var
++)
1408 strvec_push(&conn
->env
, *var
);
1410 conn
->use_shell
= 1;
1411 conn
->in
= conn
->out
= -1;
1412 if (protocol
== PROTO_SSH
) {
1413 char *ssh_host
= hostandport
;
1414 const char *port
= NULL
;
1415 transport_check_allowed("ssh");
1416 get_host_and_port(&ssh_host
, &port
);
1419 port
= get_port(ssh_host
);
1421 if (flags
& CONNECT_DIAG_URL
) {
1422 printf("Diag: url=%s\n", url
? url
: "NULL");
1423 printf("Diag: protocol=%s\n", prot_name(protocol
));
1424 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1425 printf("Diag: port=%s\n", port
? port
: "NONE");
1426 printf("Diag: path=%s\n", path
? path
: "NULL");
1431 strbuf_release(&cmd
);
1434 conn
->trace2_child_class
= "transport/ssh";
1435 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1437 transport_check_allowed("file");
1438 conn
->trace2_child_class
= "transport/file";
1440 strvec_pushf(&conn
->env
,
1441 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1445 strvec_push(&conn
->args
, cmd
.buf
);
1447 if (start_command(conn
))
1448 die(_("unable to fork"));
1450 fd
[0] = conn
->out
; /* read from child's stdout */
1451 fd
[1] = conn
->in
; /* write to child's stdin */
1452 strbuf_release(&cmd
);
1459 int finish_connect(struct child_process
*conn
)
1462 if (!conn
|| git_connection_is_socket(conn
))
1465 code
= finish_command(conn
);