1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "environment.h"
11 #include "run-command.h"
15 #include "string-list.h"
16 #include "oid-array.h"
18 #include "transport.h"
24 #include "bundle-uri.h"
25 #include "promisor-remote.h"
27 static char *server_capabilities_v1
;
28 static struct strvec server_capabilities_v2
= STRVEC_INIT
;
29 static const char *next_server_feature_value(const char *feature
, size_t *len
, size_t *offset
);
31 static int check_ref(const char *name
, unsigned int flags
)
36 if (!skip_prefix(name
, "refs/", &name
))
39 /* REF_NORMAL means that we don't want the magic fake tag refs */
40 if ((flags
& REF_NORMAL
) && check_refname_format(name
,
41 REFNAME_ALLOW_ONELEVEL
))
44 /* REF_BRANCHES means that we want regular branch heads */
45 if ((flags
& REF_BRANCHES
) && starts_with(name
, "heads/"))
48 /* REF_TAGS means that we want tags */
49 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
52 /* All type bits clear means that we are ok with anything */
53 return !(flags
& ~REF_NORMAL
);
56 int check_ref_type(const struct ref
*ref
, int flags
)
58 return check_ref(ref
->name
, flags
);
61 static NORETURN
void die_initial_contact(int unexpected
)
64 * A hang-up after seeing some response from the other end
65 * means that it is unexpected, as we know the other end is
66 * willing to talk to us. A hang-up before seeing any
67 * response does not necessarily mean an ACL problem, though.
70 die(_("the remote end hung up upon initial contact"));
72 die(_("Could not read from remote repository.\n\n"
73 "Please make sure you have the correct access rights\n"
74 "and the repository exists."));
77 /* Checks if the server supports the capability 'c' */
78 int server_supports_v2(const char *c
)
82 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
84 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
85 (!*out
|| *out
== '='))
91 void ensure_server_supports_v2(const char *c
)
93 if (!server_supports_v2(c
))
94 die(_("server doesn't support '%s'"), c
);
97 int server_feature_v2(const char *c
, const char **v
)
101 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
103 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
112 int server_supports_feature(const char *c
, const char *feature
,
117 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
119 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
120 (!*out
|| *(out
++) == '=')) {
121 if (parse_feature_request(out
, feature
))
129 die(_("server doesn't support feature '%s'"), feature
);
134 static void process_capabilities_v2(struct packet_reader
*reader
)
136 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
137 strvec_push(&server_capabilities_v2
, reader
->line
);
139 if (reader
->status
!= PACKET_READ_FLUSH
)
140 die(_("expected flush after capabilities"));
143 enum protocol_version
discover_version(struct packet_reader
*reader
)
145 enum protocol_version version
= protocol_unknown_version
;
148 * Peek the first line of the server's response to
149 * determine the protocol version the server is speaking.
151 switch (packet_reader_peek(reader
)) {
152 case PACKET_READ_EOF
:
153 die_initial_contact(0);
154 case PACKET_READ_FLUSH
:
155 case PACKET_READ_DELIM
:
156 case PACKET_READ_RESPONSE_END
:
157 version
= protocol_v0
;
159 case PACKET_READ_NORMAL
:
160 version
= determine_protocol_version_client(reader
->line
);
166 process_capabilities_v2(reader
);
169 /* Read the peeked version line */
170 packet_reader_read(reader
);
174 case protocol_unknown_version
:
175 BUG("unknown protocol version");
178 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
183 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
186 struct string_list_item
*item
;
189 return; /* just "symref" */
190 /* e.g. "symref=HEAD:refs/heads/master" */
191 sym
= xmemdupz(val
, len
);
192 target
= strchr(sym
, ':');
194 /* just "symref=something" */
197 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
198 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
199 /* "symref=bogus:pair */
201 item
= string_list_append_nodup(symref
, sym
);
209 static void annotate_refs_with_symref_info(struct ref
*ref
)
211 struct string_list symref
= STRING_LIST_INIT_DUP
;
218 val
= next_server_feature_value("symref", &len
, &offset
);
221 parse_one_symref_info(&symref
, val
, len
);
223 string_list_sort(&symref
);
225 for (; ref
; ref
= ref
->next
) {
226 struct string_list_item
*item
;
227 item
= string_list_lookup(&symref
, ref
->name
);
230 ref
->symref
= xstrdup((char *)item
->util
);
232 string_list_clear(&symref
, 0);
235 static void process_capabilities(struct packet_reader
*reader
, size_t *linelen
)
237 const char *feat_val
;
239 const char *line
= reader
->line
;
240 size_t nul_location
= strlen(line
);
241 if (nul_location
== *linelen
)
243 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
244 *linelen
= nul_location
;
246 feat_val
= server_feature_value("object-format", &feat_len
);
248 char *hash_name
= xstrndup(feat_val
, feat_len
);
249 int hash_algo
= hash_algo_by_name(hash_name
);
250 if (hash_algo
!= GIT_HASH_UNKNOWN
)
251 reader
->hash_algo
= &hash_algos
[hash_algo
];
254 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
258 static int process_dummy_ref(const struct packet_reader
*reader
)
260 const char *line
= reader
->line
;
261 struct object_id oid
;
264 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
270 return oideq(reader
->hash_algo
->null_oid
, &oid
) &&
271 !strcmp(name
, "capabilities^{}");
274 static void check_no_capabilities(const char *line
, size_t len
)
276 if (strlen(line
) != len
)
277 warning(_("ignoring capabilities after first line '%s'"),
278 line
+ strlen(line
));
281 static int process_ref(const struct packet_reader
*reader
, size_t len
,
282 struct ref
***list
, unsigned int flags
,
283 struct oid_array
*extra_have
)
285 const char *line
= reader
->line
;
286 struct object_id old_oid
;
289 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
295 if (extra_have
&& !strcmp(name
, ".have")) {
296 oid_array_append(extra_have
, &old_oid
);
297 } else if (!strcmp(name
, "capabilities^{}")) {
298 die(_("protocol error: unexpected capabilities^{}"));
299 } else if (check_ref(name
, flags
)) {
300 struct ref
*ref
= alloc_ref(name
);
301 oidcpy(&ref
->old_oid
, &old_oid
);
305 check_no_capabilities(line
, len
);
309 static int process_shallow(const struct packet_reader
*reader
, size_t len
,
310 struct oid_array
*shallow_points
)
312 const char *line
= reader
->line
;
314 struct object_id old_oid
;
316 if (!skip_prefix(line
, "shallow ", &arg
))
319 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
320 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
322 die(_("repository on the other end cannot be shallow"));
323 oid_array_append(shallow_points
, &old_oid
);
324 check_no_capabilities(line
, len
);
328 enum get_remote_heads_state
{
329 EXPECTING_FIRST_REF
= 0,
336 * Read all the refs from the other end
338 struct ref
**get_remote_heads(struct packet_reader
*reader
,
339 struct ref
**list
, unsigned int flags
,
340 struct oid_array
*extra_have
,
341 struct oid_array
*shallow_points
)
343 struct ref
**orig_list
= list
;
345 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
349 while (state
!= EXPECTING_DONE
) {
350 switch (packet_reader_read(reader
)) {
351 case PACKET_READ_EOF
:
352 die_initial_contact(1);
353 case PACKET_READ_NORMAL
:
354 len
= reader
->pktlen
;
356 case PACKET_READ_FLUSH
:
357 state
= EXPECTING_DONE
;
359 case PACKET_READ_DELIM
:
360 case PACKET_READ_RESPONSE_END
:
361 die(_("invalid packet"));
365 case EXPECTING_FIRST_REF
:
366 process_capabilities(reader
, &len
);
367 if (process_dummy_ref(reader
)) {
368 state
= EXPECTING_SHALLOW
;
371 state
= EXPECTING_REF
;
374 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
376 state
= EXPECTING_SHALLOW
;
378 case EXPECTING_SHALLOW
:
379 if (process_shallow(reader
, len
, shallow_points
))
381 die(_("protocol error: unexpected '%s'"), reader
->line
);
387 annotate_refs_with_symref_info(*orig_list
);
392 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
393 static int process_ref_v2(struct packet_reader
*reader
, struct ref
***list
,
394 const char **unborn_head_target
)
398 struct object_id old_oid
;
400 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
402 const char *line
= reader
->line
;
405 * Ref lines have a number of fields which are space deliminated. The
406 * first field is the OID of the ref. The second field is the ref
407 * name. Subsequent fields (symref-target and peeled) are optional and
408 * don't have a particular order.
410 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
415 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
417 if (unborn_head_target
&&
418 !strcmp("HEAD", line_sections
.items
[i
++].string
)) {
420 * Look for the symref target (if any). If found,
421 * return it to the caller.
423 for (; i
< line_sections
.nr
; i
++) {
424 const char *arg
= line_sections
.items
[i
].string
;
426 if (skip_prefix(arg
, "symref-target:", &arg
)) {
427 *unborn_head_target
= xstrdup(arg
);
434 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
440 ref
= alloc_ref(line_sections
.items
[i
++].string
);
442 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
446 for (; i
< line_sections
.nr
; i
++) {
447 const char *arg
= line_sections
.items
[i
].string
;
448 if (skip_prefix(arg
, "symref-target:", &arg
))
449 ref
->symref
= xstrdup(arg
);
451 if (skip_prefix(arg
, "peeled:", &arg
)) {
452 struct object_id peeled_oid
;
455 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
456 reader
->hash_algo
) || *end
) {
461 peeled_name
= xstrfmt("%s^{}", ref
->name
);
462 peeled
= alloc_ref(peeled_name
);
464 memcpy(peeled
->old_oid
.hash
, peeled_oid
.hash
,
465 reader
->hash_algo
->rawsz
);
467 *list
= &peeled
->next
;
474 string_list_clear(&line_sections
, 0);
478 void check_stateless_delimiter(int stateless_rpc
,
479 struct packet_reader
*reader
,
483 return; /* not in stateless mode, no delimiter expected */
484 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
488 static void send_capabilities(int fd_out
, struct packet_reader
*reader
)
490 const char *hash_name
;
491 const char *promisor_remote_info
;
493 if (server_supports_v2("agent"))
494 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
496 if (server_feature_v2("object-format", &hash_name
)) {
497 int hash_algo
= hash_algo_by_name(hash_name
);
498 if (hash_algo
== GIT_HASH_UNKNOWN
)
499 die(_("unknown object format '%s' specified by server"), hash_name
);
500 reader
->hash_algo
= &hash_algos
[hash_algo
];
501 packet_write_fmt(fd_out
, "object-format=%s", reader
->hash_algo
->name
);
503 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
505 if (server_feature_v2("promisor-remote", &promisor_remote_info
)) {
506 char *reply
= promisor_remote_reply(promisor_remote_info
);
508 packet_write_fmt(fd_out
, "promisor-remote=%s", reply
);
514 int get_remote_bundle_uri(int fd_out
, struct packet_reader
*reader
,
515 struct bundle_list
*bundles
, int stateless_rpc
)
519 /* Assert bundle-uri support */
520 ensure_server_supports_v2("bundle-uri");
522 /* (Re-)send capabilities */
523 send_capabilities(fd_out
, reader
);
526 packet_write_fmt(fd_out
, "command=bundle-uri\n");
527 packet_delim(fd_out
);
529 packet_flush(fd_out
);
531 /* Process response from server */
532 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
533 const char *line
= reader
->line
;
536 if (!bundle_uri_parse_line(bundles
, line
))
539 return error(_("error on bundle-uri response line %d: %s"),
543 if (reader
->status
!= PACKET_READ_FLUSH
)
544 return error(_("expected flush after bundle-uri listing"));
547 * Might die(), but obscure enough that that's OK, e.g. in
548 * serve.c we'll call BUG() on its equivalent (the
549 * PACKET_READ_RESPONSE_END check).
551 check_stateless_delimiter(stateless_rpc
, reader
,
552 _("expected response end packet after ref listing"));
557 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
558 struct ref
**list
, int for_push
,
559 struct transport_ls_refs_options
*transport_options
,
560 const struct string_list
*server_options
,
564 struct strvec
*ref_prefixes
= transport_options
?
565 &transport_options
->ref_prefixes
: NULL
;
566 const char **unborn_head_target
= transport_options
?
567 &transport_options
->unborn_head_target
: NULL
;
570 ensure_server_supports_v2("ls-refs");
571 packet_write_fmt(fd_out
, "command=ls-refs\n");
573 /* Send capabilities */
574 send_capabilities(fd_out
, reader
);
576 if (server_options
&& server_options
->nr
) {
577 ensure_server_supports_v2("server-option");
578 for (i
= 0; i
< server_options
->nr
; i
++)
579 packet_write_fmt(fd_out
, "server-option=%s",
580 server_options
->items
[i
].string
);
583 packet_delim(fd_out
);
584 /* When pushing we don't want to request the peeled tags */
586 packet_write_fmt(fd_out
, "peel\n");
587 packet_write_fmt(fd_out
, "symrefs\n");
588 if (server_supports_feature("ls-refs", "unborn", 0))
589 packet_write_fmt(fd_out
, "unborn\n");
590 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->nr
; i
++) {
591 packet_write_fmt(fd_out
, "ref-prefix %s\n",
594 packet_flush(fd_out
);
596 /* Process response from server */
597 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
598 if (!process_ref_v2(reader
, &list
, unborn_head_target
))
599 die(_("invalid ls-refs response: %s"), reader
->line
);
602 if (reader
->status
!= PACKET_READ_FLUSH
)
603 die(_("expected flush after ref listing"));
605 check_stateless_delimiter(stateless_rpc
, reader
,
606 _("expected response end packet after ref listing"));
611 const char *parse_feature_value(const char *feature_list
, const char *feature
, size_t *lenp
, size_t *offset
)
613 const char *orig_start
= feature_list
;
619 len
= strlen(feature
);
621 feature_list
+= *offset
;
622 while (*feature_list
) {
623 const char *found
= strstr(feature_list
, feature
);
626 if (feature_list
== found
|| isspace(found
[-1])) {
627 const char *value
= found
+ len
;
628 /* feature with no value (e.g., "thin-pack") */
629 if (!*value
|| isspace(*value
)) {
633 *offset
= found
+ len
- orig_start
;
636 /* feature with a value (e.g., "agent=git/1.2.3-Linux") */
637 else if (*value
== '=') {
641 end
= strcspn(value
, " \t\n");
645 *offset
= value
+ end
- orig_start
;
649 * otherwise we matched a substring of another feature;
653 feature_list
= found
+ 1;
658 int server_supports_hash(const char *desired
, int *feature_supported
)
664 hash
= next_server_feature_value("object-format", &len
, &offset
);
665 if (feature_supported
)
666 *feature_supported
= !!hash
;
668 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
672 if (!xstrncmpz(desired
, hash
, len
))
675 hash
= next_server_feature_value("object-format", &len
, &offset
);
680 int parse_feature_request(const char *feature_list
, const char *feature
)
682 return !!parse_feature_value(feature_list
, feature
, NULL
, NULL
);
685 static const char *next_server_feature_value(const char *feature
, size_t *len
, size_t *offset
)
687 return parse_feature_value(server_capabilities_v1
, feature
, len
, offset
);
690 const char *server_feature_value(const char *feature
, size_t *len
)
692 return parse_feature_value(server_capabilities_v1
, feature
, len
, NULL
);
695 int server_supports(const char *feature
)
697 return !!server_feature_value(feature
, NULL
);
707 int url_is_local_not_ssh(const char *url
)
709 const char *colon
= strchr(url
, ':');
710 const char *slash
= strchr(url
, '/');
711 return !colon
|| (slash
&& slash
< colon
) ||
712 (has_dos_drive_prefix(url
) && is_valid_path(url
));
715 static const char *prot_name(enum protocol protocol
)
726 return "unknown protocol";
730 static enum protocol
get_protocol(const char *name
)
732 if (!strcmp(name
, "ssh"))
734 if (!strcmp(name
, "git"))
736 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
738 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
740 if (!strcmp(name
, "file"))
742 die(_("protocol '%s' is not supported"), name
);
745 static char *host_end(char **hoststart
, int removebrackets
)
747 char *host
= *hoststart
;
749 char *start
= strstr(host
, "@[");
751 start
++; /* Jump over '@' */
754 if (start
[0] == '[') {
755 end
= strchr(start
+ 1, ']');
757 if (removebrackets
) {
759 memmove(start
, start
+ 1, end
- start
);
770 #define STR(s) STR_(s)
772 static void get_host_and_port(char **host
, const char **port
)
775 end
= host_end(host
, 1);
776 colon
= strchr(end
, ':');
778 long portnr
= strtol(colon
+ 1, &end
, 10);
779 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
782 } else if (!colon
[1]) {
788 static void enable_keepalive(int sockfd
)
792 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
793 error_errno(_("unable to set SO_KEEPALIVE on socket"));
798 static const char *ai_name(const struct addrinfo
*ai
)
800 static char addr
[NI_MAXHOST
];
801 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
802 NI_NUMERICHOST
) != 0)
803 xsnprintf(addr
, sizeof(addr
), "(unknown)");
809 * Returns a connected socket() fd, or else die()s.
811 static int git_tcp_connect_sock(char *host
, int flags
)
813 struct strbuf error_message
= STRBUF_INIT
;
815 const char *port
= STR(DEFAULT_GIT_PORT
);
816 struct addrinfo hints
, *ai0
, *ai
;
820 get_host_and_port(&host
, &port
);
824 memset(&hints
, 0, sizeof(hints
));
825 if (flags
& CONNECT_IPV4
)
826 hints
.ai_family
= AF_INET
;
827 else if (flags
& CONNECT_IPV6
)
828 hints
.ai_family
= AF_INET6
;
829 hints
.ai_socktype
= SOCK_STREAM
;
830 hints
.ai_protocol
= IPPROTO_TCP
;
832 if (flags
& CONNECT_VERBOSE
)
833 fprintf(stderr
, _("Looking up %s ... "), host
);
835 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
837 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
839 if (flags
& CONNECT_VERBOSE
)
840 /* TRANSLATORS: this is the end of "Looking up %s ... " */
841 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
843 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
844 sockfd
= socket(ai
->ai_family
,
845 ai
->ai_socktype
, ai
->ai_protocol
);
847 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
848 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
849 host
, cnt
, ai_name(ai
), strerror(errno
));
855 if (flags
& CONNECT_VERBOSE
)
856 fprintf(stderr
, "%s ", ai_name(ai
));
863 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
865 enable_keepalive(sockfd
);
867 if (flags
& CONNECT_VERBOSE
)
868 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
869 fprintf_ln(stderr
, _("done."));
871 strbuf_release(&error_message
);
879 * Returns a connected socket() fd, or else die()s.
881 static int git_tcp_connect_sock(char *host
, int flags
)
883 struct strbuf error_message
= STRBUF_INIT
;
885 const char *port
= STR(DEFAULT_GIT_PORT
);
888 struct sockaddr_in sa
;
893 get_host_and_port(&host
, &port
);
895 if (flags
& CONNECT_VERBOSE
)
896 fprintf(stderr
, _("Looking up %s ... "), host
);
898 he
= gethostbyname(host
);
900 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
901 nport
= strtoul(port
, &ep
, 10);
902 if ( ep
== port
|| *ep
) {
904 struct servent
*se
= getservbyname(port
,"tcp");
906 die(_("unknown port %s"), port
);
910 if (flags
& CONNECT_VERBOSE
)
911 /* TRANSLATORS: this is the end of "Looking up %s ... " */
912 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
914 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
915 memset(&sa
, 0, sizeof sa
);
916 sa
.sin_family
= he
->h_addrtype
;
917 sa
.sin_port
= htons(nport
);
918 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
920 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
922 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
923 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
926 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
933 if (flags
& CONNECT_VERBOSE
)
934 fprintf(stderr
, "%s ",
935 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
940 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
942 enable_keepalive(sockfd
);
944 if (flags
& CONNECT_VERBOSE
)
945 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
946 fprintf_ln(stderr
, _("done."));
955 * Dummy child_process returned by git_connect() if the transport protocol
956 * does not need fork(2).
958 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
960 int git_connection_is_socket(struct child_process
*conn
)
962 return conn
== &no_fork
;
965 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
967 int sockfd
= git_tcp_connect_sock(host
, flags
);
976 static char *git_proxy_command
;
978 static int git_proxy_command_options(const char *var
, const char *value
,
979 const struct config_context
*ctx
, void *cb
)
981 if (!strcmp(var
, "core.gitproxy")) {
985 const char *rhost_name
= cb
;
986 int rhost_len
= strlen(rhost_name
);
988 if (git_proxy_command
)
991 return config_error_nonbool(var
);
993 * ;# matches www.kernel.org as well
994 * gitproxy = netcatter-1 for kernel.org
995 * gitproxy = netcatter-2 for sample.xz
996 * gitproxy = netcatter-default
998 for_pos
= strstr(value
, " for ");
1000 /* matches everybody */
1001 matchlen
= strlen(value
);
1003 hostlen
= strlen(for_pos
+ 5);
1004 if (rhost_len
< hostlen
)
1006 else if (!strncmp(for_pos
+ 5,
1007 rhost_name
+ rhost_len
- hostlen
,
1009 ((rhost_len
== hostlen
) ||
1010 rhost_name
[rhost_len
- hostlen
-1] == '.'))
1011 matchlen
= for_pos
- value
;
1015 if (0 <= matchlen
) {
1016 /* core.gitproxy = none for kernel.org */
1017 if (matchlen
== 4 &&
1018 !memcmp(value
, "none", 4))
1020 git_proxy_command
= xmemdupz(value
, matchlen
);
1025 return git_default_config(var
, value
, ctx
, cb
);
1028 static int git_use_proxy(const char *host
)
1030 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
1031 git_config(git_proxy_command_options
, (void*)host
);
1032 return (git_proxy_command
&& *git_proxy_command
);
1035 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
1037 const char *port
= STR(DEFAULT_GIT_PORT
);
1038 struct child_process
*proxy
;
1040 get_host_and_port(&host
, &port
);
1042 if (looks_like_command_line_option(host
))
1043 die(_("strange hostname '%s' blocked"), host
);
1044 if (looks_like_command_line_option(port
))
1045 die(_("strange port '%s' blocked"), port
);
1047 proxy
= xmalloc(sizeof(*proxy
));
1048 child_process_init(proxy
);
1049 strvec_push(&proxy
->args
, git_proxy_command
);
1050 strvec_push(&proxy
->args
, host
);
1051 strvec_push(&proxy
->args
, port
);
1054 if (start_command(proxy
))
1055 die(_("cannot start proxy %s"), git_proxy_command
);
1056 fd
[0] = proxy
->out
; /* read from proxy stdout */
1057 fd
[1] = proxy
->in
; /* write to proxy stdin */
1061 static char *get_port(char *host
)
1064 char *p
= strchr(host
, ':');
1067 long port
= strtol(p
+ 1, &end
, 10);
1068 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
1078 * Extract protocol and relevant parts from the specified connection URL.
1079 * The caller must free() the returned strings.
1081 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
1087 int separator
= '/';
1088 enum protocol protocol
= PROTO_LOCAL
;
1090 if (is_url(url_orig
))
1091 url
= url_decode(url_orig
);
1093 url
= xstrdup(url_orig
);
1095 host
= strstr(url
, "://");
1098 protocol
= get_protocol(url
);
1102 if (!url_is_local_not_ssh(url
)) {
1103 protocol
= PROTO_SSH
;
1109 * Don't do destructive transforms as protocol code does
1110 * '[]' unwrapping in get_host_and_port()
1112 end
= host_end(&host
, 0);
1114 if (protocol
== PROTO_LOCAL
)
1116 else if (protocol
== PROTO_FILE
&& *host
!= '/' &&
1117 !has_dos_drive_prefix(host
) &&
1118 offset_1st_component(host
- 2) > 1)
1119 path
= host
- 2; /* include the leading "//" */
1120 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
1121 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1123 path
= strchr(end
, separator
);
1125 if (!path
|| !*path
)
1126 die(_("no path specified; see 'git help pull' for valid url syntax"));
1129 * null-terminate hostname and point path to ~ for URL's like this:
1130 * ssh://host.xz/~user/repo
1133 end
= path
; /* Need to \0 terminate host here */
1134 if (separator
== ':')
1135 path
++; /* path starts after ':' */
1136 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
1141 path
= xstrdup(path
);
1144 *ret_host
= xstrdup(host
);
1150 static const char *get_ssh_command(void)
1154 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1157 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
1169 VARIANT_TORTOISEPLINK
,
1172 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
1174 const char *variant
= getenv("GIT_SSH_VARIANT");
1176 if (!variant
&& git_config_get_string_tmp("ssh.variant", &variant
))
1179 if (!strcmp(variant
, "auto"))
1180 *ssh_variant
= VARIANT_AUTO
;
1181 else if (!strcmp(variant
, "plink"))
1182 *ssh_variant
= VARIANT_PLINK
;
1183 else if (!strcmp(variant
, "putty"))
1184 *ssh_variant
= VARIANT_PUTTY
;
1185 else if (!strcmp(variant
, "tortoiseplink"))
1186 *ssh_variant
= VARIANT_TORTOISEPLINK
;
1187 else if (!strcmp(variant
, "simple"))
1188 *ssh_variant
= VARIANT_SIMPLE
;
1190 *ssh_variant
= VARIANT_SSH
;
1193 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1196 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1197 const char *variant
;
1200 override_ssh_variant(&ssh_variant
);
1202 if (ssh_variant
!= VARIANT_AUTO
)
1206 p
= xstrdup(ssh_command
);
1207 variant
= basename(p
);
1209 const char **ssh_argv
;
1211 p
= xstrdup(ssh_command
);
1212 if (split_cmdline(p
, &ssh_argv
) > 0) {
1213 variant
= basename((char *)ssh_argv
[0]);
1215 * At this point, variant points into the buffer
1216 * referenced by p, hence we do not need ssh_argv
1226 if (!strcasecmp(variant
, "ssh") ||
1227 !strcasecmp(variant
, "ssh.exe"))
1228 ssh_variant
= VARIANT_SSH
;
1229 else if (!strcasecmp(variant
, "plink") ||
1230 !strcasecmp(variant
, "plink.exe"))
1231 ssh_variant
= VARIANT_PLINK
;
1232 else if (!strcasecmp(variant
, "tortoiseplink") ||
1233 !strcasecmp(variant
, "tortoiseplink.exe"))
1234 ssh_variant
= VARIANT_TORTOISEPLINK
;
1241 * Open a connection using Git's native protocol.
1243 * The caller is responsible for freeing hostandport, but this function may
1244 * modify it (for example, to truncate it to remove the port part).
1246 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1247 const char *path
, const char *prog
,
1248 enum protocol_version version
,
1251 struct child_process
*conn
;
1252 struct strbuf request
= STRBUF_INIT
;
1254 * Set up virtual host information based on where we will
1255 * connect, unless the user has overridden us in
1258 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1260 target_host
= xstrdup(target_host
);
1262 target_host
= xstrdup(hostandport
);
1264 transport_check_allowed("git");
1265 if (strchr(target_host
, '\n') || strchr(path
, '\n'))
1266 die(_("newline is forbidden in git:// hosts and repo paths"));
1269 * These underlying connection commands die() if they
1272 if (git_use_proxy(hostandport
))
1273 conn
= git_proxy_connect(fd
, hostandport
);
1275 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1277 * Separate original protocol components prog and path
1278 * from extended host header with a NUL byte.
1280 * Note: Do not add any other headers here! Doing so
1281 * will cause older git-daemon servers to crash.
1283 strbuf_addf(&request
,
1288 /* If using a new version put that stuff here after a second null byte */
1290 strbuf_addch(&request
, '\0');
1291 strbuf_addf(&request
, "version=%d%c",
1295 packet_write(fd
[1], request
.buf
, request
.len
);
1298 strbuf_release(&request
);
1303 * Append the appropriate environment variables to `env` and options to
1304 * `args` for running ssh in Git's SSH-tunneled transport.
1306 static void push_ssh_options(struct strvec
*args
, struct strvec
*env
,
1307 enum ssh_variant variant
, const char *port
,
1308 enum protocol_version version
, int flags
)
1310 if (variant
== VARIANT_SSH
&&
1312 strvec_push(args
, "-o");
1313 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1314 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1318 if (flags
& CONNECT_IPV4
) {
1321 BUG("VARIANT_AUTO passed to push_ssh_options");
1322 case VARIANT_SIMPLE
:
1323 die(_("ssh variant 'simple' does not support -4"));
1327 case VARIANT_TORTOISEPLINK
:
1328 strvec_push(args
, "-4");
1330 } else if (flags
& CONNECT_IPV6
) {
1333 BUG("VARIANT_AUTO passed to push_ssh_options");
1334 case VARIANT_SIMPLE
:
1335 die(_("ssh variant 'simple' does not support -6"));
1339 case VARIANT_TORTOISEPLINK
:
1340 strvec_push(args
, "-6");
1344 if (variant
== VARIANT_TORTOISEPLINK
)
1345 strvec_push(args
, "-batch");
1350 BUG("VARIANT_AUTO passed to push_ssh_options");
1351 case VARIANT_SIMPLE
:
1352 die(_("ssh variant 'simple' does not support setting port"));
1354 strvec_push(args
, "-p");
1358 case VARIANT_TORTOISEPLINK
:
1359 strvec_push(args
, "-P");
1362 strvec_push(args
, port
);
1366 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1367 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1368 const char *port
, enum protocol_version version
,
1372 enum ssh_variant variant
;
1374 if (looks_like_command_line_option(ssh_host
))
1375 die(_("strange hostname '%s' blocked"), ssh_host
);
1377 ssh
= get_ssh_command();
1379 variant
= determine_ssh_variant(ssh
, 1);
1382 * GIT_SSH is the no-shell version of
1383 * GIT_SSH_COMMAND (and must remain so for
1384 * historical compatibility).
1386 conn
->use_shell
= 0;
1388 ssh
= getenv("GIT_SSH");
1391 variant
= determine_ssh_variant(ssh
, 0);
1394 if (variant
== VARIANT_AUTO
) {
1395 struct child_process detect
= CHILD_PROCESS_INIT
;
1397 detect
.use_shell
= conn
->use_shell
;
1398 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1400 strvec_push(&detect
.args
, ssh
);
1401 strvec_push(&detect
.args
, "-G");
1402 push_ssh_options(&detect
.args
, &detect
.env
,
1403 VARIANT_SSH
, port
, version
, flags
);
1404 strvec_push(&detect
.args
, ssh_host
);
1406 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1409 strvec_push(&conn
->args
, ssh
);
1410 push_ssh_options(&conn
->args
, &conn
->env
, variant
, port
, version
,
1412 strvec_push(&conn
->args
, ssh_host
);
1416 * This returns the dummy child_process `no_fork` if the transport protocol
1417 * does not need fork(2), or a struct child_process object if it does. Once
1418 * done, finish the connection with finish_connect() with the value returned
1419 * from this function (it is safe to call finish_connect() with NULL to
1420 * support the former case).
1422 * If it returns, the connect is successful; it just dies on errors (this
1423 * will hopefully be changed in a libification effort, to return NULL when
1424 * the connection failed).
1426 struct child_process
*git_connect(int fd
[2], const char *url
,
1428 const char *prog
, int flags
)
1430 char *hostandport
, *path
;
1431 struct child_process
*conn
;
1432 enum protocol protocol
;
1433 enum protocol_version version
= get_protocol_version_config();
1436 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1437 * to perform any operation that doesn't involve upload-pack (i.e., a
1438 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1439 * to do anything else (like push or remote archive) via v2.
1441 if (version
== protocol_v2
&& strcmp("git-upload-pack", name
))
1442 version
= protocol_v0
;
1444 /* Without this we cannot rely on waitpid() to tell
1445 * what happened to our children.
1447 signal(SIGCHLD
, SIG_DFL
);
1449 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1450 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1451 printf("Diag: url=%s\n", url
? url
: "NULL");
1452 printf("Diag: protocol=%s\n", prot_name(protocol
));
1453 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1454 printf("Diag: path=%s\n", path
? path
: "NULL");
1456 } else if (protocol
== PROTO_GIT
) {
1457 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1458 conn
->trace2_child_class
= "transport/git";
1460 struct strbuf cmd
= STRBUF_INIT
;
1461 const char *const *var
;
1463 conn
= xmalloc(sizeof(*conn
));
1464 child_process_init(conn
);
1466 if (looks_like_command_line_option(path
))
1467 die(_("strange pathname '%s' blocked"), path
);
1469 strbuf_addstr(&cmd
, prog
);
1470 strbuf_addch(&cmd
, ' ');
1471 sq_quote_buf(&cmd
, path
);
1473 /* remove repo-local variables from the environment */
1474 for (var
= local_repo_env
; *var
; var
++)
1475 strvec_push(&conn
->env
, *var
);
1477 conn
->use_shell
= 1;
1478 conn
->in
= conn
->out
= -1;
1479 if (protocol
== PROTO_SSH
) {
1480 char *ssh_host
= hostandport
;
1481 const char *port
= NULL
;
1482 transport_check_allowed("ssh");
1483 get_host_and_port(&ssh_host
, &port
);
1486 port
= get_port(ssh_host
);
1488 if (flags
& CONNECT_DIAG_URL
) {
1489 printf("Diag: url=%s\n", url
? url
: "NULL");
1490 printf("Diag: protocol=%s\n", prot_name(protocol
));
1491 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1492 printf("Diag: port=%s\n", port
? port
: "NONE");
1493 printf("Diag: path=%s\n", path
? path
: "NULL");
1497 child_process_clear(conn
);
1499 strbuf_release(&cmd
);
1502 conn
->trace2_child_class
= "transport/ssh";
1503 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1505 transport_check_allowed("file");
1506 conn
->trace2_child_class
= "transport/file";
1508 strvec_pushf(&conn
->env
,
1509 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1513 strvec_push(&conn
->args
, cmd
.buf
);
1515 if (start_command(conn
))
1516 die(_("unable to fork"));
1518 fd
[0] = conn
->out
; /* read from child's stdout */
1519 fd
[1] = conn
->in
; /* write to child's stdin */
1520 strbuf_release(&cmd
);
1527 int finish_connect(struct child_process
*conn
)
1530 if (!conn
|| git_connection_is_socket(conn
))
1533 code
= finish_command(conn
);